Event.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/StringBuilder.h>
  9. #include <AK/Vector.h>
  10. #include <Kernel/API/KeyCode.h>
  11. #include <LibCore/Event.h>
  12. #include <LibGUI/FocusSource.h>
  13. #include <LibGUI/Forward.h>
  14. #include <LibGUI/WindowType.h>
  15. #include <LibGfx/Bitmap.h>
  16. #include <LibGfx/Point.h>
  17. #include <LibGfx/Rect.h>
  18. namespace GUI {
  19. class Event : public Core::Event {
  20. public:
  21. enum Type {
  22. Show = 1000,
  23. Hide,
  24. Paint,
  25. MultiPaint,
  26. Resize,
  27. MouseMove,
  28. MouseDown,
  29. MouseDoubleClick,
  30. MouseUp,
  31. MouseWheel,
  32. Enter,
  33. Leave,
  34. KeyDown,
  35. KeyUp,
  36. WindowEntered,
  37. WindowLeft,
  38. WindowBecameInactive,
  39. WindowBecameActive,
  40. WindowInputEntered,
  41. WindowInputLeft,
  42. FocusIn,
  43. FocusOut,
  44. WindowCloseRequest,
  45. ContextMenu,
  46. EnabledChange,
  47. DragEnter,
  48. DragLeave,
  49. DragMove,
  50. Drop,
  51. ThemeChange,
  52. FontsChange,
  53. ScreenRectsChange,
  54. ActionEnter,
  55. ActionLeave,
  56. AppletAreaRectChange,
  57. __Begin_WM_Events,
  58. WM_WindowRemoved,
  59. WM_WindowStateChanged,
  60. WM_WindowRectChanged,
  61. WM_WindowIconBitmapChanged,
  62. WM_AppletAreaSizeChanged,
  63. WM_SuperKeyPressed,
  64. WM_SuperSpaceKeyPressed,
  65. WM_SuperDKeyPressed,
  66. WM_SuperDigitKeyPressed,
  67. WM_WorkspaceChanged,
  68. WM_KeymapChanged,
  69. __End_WM_Events,
  70. };
  71. Event() = default;
  72. explicit Event(Type type)
  73. : Core::Event(type)
  74. {
  75. }
  76. virtual ~Event() = default;
  77. bool is_key_event() const { return type() == KeyUp || type() == KeyDown; }
  78. bool is_paint_event() const { return type() == Paint; }
  79. };
  80. class WMEvent : public Event {
  81. public:
  82. WMEvent(Type type, int client_id, int window_id)
  83. : Event(type)
  84. , m_client_id(client_id)
  85. , m_window_id(window_id)
  86. {
  87. }
  88. int client_id() const { return m_client_id; }
  89. int window_id() const { return m_window_id; }
  90. private:
  91. int m_client_id { -1 };
  92. int m_window_id { -1 };
  93. };
  94. class WMSuperKeyPressedEvent : public WMEvent {
  95. public:
  96. explicit WMSuperKeyPressedEvent(int client_id)
  97. : WMEvent(Event::Type::WM_SuperKeyPressed, client_id, 0)
  98. {
  99. }
  100. };
  101. class WMSuperSpaceKeyPressedEvent : public WMEvent {
  102. public:
  103. explicit WMSuperSpaceKeyPressedEvent(int client_id)
  104. : WMEvent(Event::Type::WM_SuperSpaceKeyPressed, client_id, 0)
  105. {
  106. }
  107. };
  108. class WMSuperDKeyPressedEvent : public WMEvent {
  109. public:
  110. explicit WMSuperDKeyPressedEvent(int client_id)
  111. : WMEvent(Event::Type::WM_SuperDKeyPressed, client_id, 0)
  112. {
  113. }
  114. };
  115. class WMSuperDigitKeyPressedEvent : public WMEvent {
  116. public:
  117. WMSuperDigitKeyPressedEvent(int client_id, u8 digit)
  118. : WMEvent(Event::Type::WM_SuperDigitKeyPressed, client_id, 0)
  119. , m_digit(digit)
  120. {
  121. }
  122. u8 digit() const { return m_digit; }
  123. private:
  124. u8 m_digit { 0 };
  125. };
  126. class WMAppletAreaSizeChangedEvent : public WMEvent {
  127. public:
  128. explicit WMAppletAreaSizeChangedEvent(Gfx::IntSize const& size)
  129. : WMEvent(Event::Type::WM_AppletAreaSizeChanged, 0, 0)
  130. , m_size(size)
  131. {
  132. }
  133. Gfx::IntSize const& size() const { return m_size; }
  134. private:
  135. Gfx::IntSize m_size;
  136. };
  137. class WMWindowRemovedEvent : public WMEvent {
  138. public:
  139. WMWindowRemovedEvent(int client_id, int window_id)
  140. : WMEvent(Event::Type::WM_WindowRemoved, client_id, window_id)
  141. {
  142. }
  143. };
  144. class WMWindowStateChangedEvent : public WMEvent {
  145. public:
  146. WMWindowStateChangedEvent(int client_id, int window_id, StringView title, Gfx::IntRect const& rect, unsigned workspace_row, unsigned workspace_column, bool is_active, bool is_blocked, WindowType window_type, bool is_minimized, bool is_frameless, Optional<int> progress)
  147. : WMEvent(Event::Type::WM_WindowStateChanged, client_id, window_id)
  148. , m_title(title)
  149. , m_rect(rect)
  150. , m_window_type(window_type)
  151. , m_workspace_row(workspace_row)
  152. , m_workspace_column(workspace_column)
  153. , m_active(is_active)
  154. , m_blocked(is_blocked)
  155. , m_minimized(is_minimized)
  156. , m_frameless(is_frameless)
  157. , m_progress(progress)
  158. {
  159. }
  160. String const& title() const { return m_title; }
  161. Gfx::IntRect const& rect() const { return m_rect; }
  162. bool is_active() const { return m_active; }
  163. bool is_blocked() const { return m_blocked; }
  164. WindowType window_type() const { return m_window_type; }
  165. bool is_minimized() const { return m_minimized; }
  166. bool is_frameless() const { return m_frameless; }
  167. Optional<int> progress() const { return m_progress; }
  168. unsigned workspace_row() const { return m_workspace_row; }
  169. unsigned workspace_column() const { return m_workspace_column; }
  170. private:
  171. String m_title;
  172. Gfx::IntRect m_rect;
  173. WindowType m_window_type;
  174. unsigned m_workspace_row;
  175. unsigned m_workspace_column;
  176. bool m_active;
  177. bool m_blocked;
  178. bool m_minimized;
  179. bool m_frameless;
  180. Optional<int> m_progress;
  181. };
  182. class WMWindowRectChangedEvent : public WMEvent {
  183. public:
  184. WMWindowRectChangedEvent(int client_id, int window_id, Gfx::IntRect const& rect)
  185. : WMEvent(Event::Type::WM_WindowRectChanged, client_id, window_id)
  186. , m_rect(rect)
  187. {
  188. }
  189. Gfx::IntRect const& rect() const { return m_rect; }
  190. private:
  191. Gfx::IntRect m_rect;
  192. };
  193. class WMWindowIconBitmapChangedEvent : public WMEvent {
  194. public:
  195. WMWindowIconBitmapChangedEvent(int client_id, int window_id, Gfx::Bitmap const* bitmap)
  196. : WMEvent(Event::Type::WM_WindowIconBitmapChanged, client_id, window_id)
  197. , m_bitmap(move(bitmap))
  198. {
  199. }
  200. Gfx::Bitmap const* bitmap() const { return m_bitmap; }
  201. private:
  202. RefPtr<Gfx::Bitmap> m_bitmap;
  203. };
  204. class WMWorkspaceChangedEvent : public WMEvent {
  205. public:
  206. explicit WMWorkspaceChangedEvent(int client_id, unsigned current_row, unsigned current_column)
  207. : WMEvent(Event::Type::WM_WorkspaceChanged, client_id, 0)
  208. , m_current_row(current_row)
  209. , m_current_column(current_column)
  210. {
  211. }
  212. unsigned current_row() const { return m_current_row; }
  213. unsigned current_column() const { return m_current_column; }
  214. private:
  215. unsigned const m_current_row;
  216. unsigned const m_current_column;
  217. };
  218. class WMKeymapChangedEvent : public WMEvent {
  219. public:
  220. explicit WMKeymapChangedEvent(int client_id, String const& keymap)
  221. : WMEvent(Event::Type::WM_KeymapChanged, client_id, 0)
  222. , m_keymap(keymap)
  223. {
  224. }
  225. String const& keymap() const { return m_keymap; }
  226. private:
  227. const String m_keymap;
  228. };
  229. class MultiPaintEvent final : public Event {
  230. public:
  231. explicit MultiPaintEvent(Vector<Gfx::IntRect, 32> rects, Gfx::IntSize const& window_size)
  232. : Event(Event::MultiPaint)
  233. , m_rects(move(rects))
  234. , m_window_size(window_size)
  235. {
  236. }
  237. Vector<Gfx::IntRect, 32> const& rects() const { return m_rects; }
  238. Gfx::IntSize const& window_size() const { return m_window_size; }
  239. private:
  240. Vector<Gfx::IntRect, 32> m_rects;
  241. Gfx::IntSize m_window_size;
  242. };
  243. class PaintEvent final : public Event {
  244. public:
  245. explicit PaintEvent(Gfx::IntRect const& rect, Gfx::IntSize const& window_size = {})
  246. : Event(Event::Paint)
  247. , m_rect(rect)
  248. , m_window_size(window_size)
  249. {
  250. }
  251. Gfx::IntRect const& rect() const { return m_rect; }
  252. Gfx::IntSize const& window_size() const { return m_window_size; }
  253. private:
  254. Gfx::IntRect m_rect;
  255. Gfx::IntSize m_window_size;
  256. };
  257. class ResizeEvent final : public Event {
  258. public:
  259. explicit ResizeEvent(Gfx::IntSize const& size)
  260. : Event(Event::Resize)
  261. , m_size(size)
  262. {
  263. }
  264. Gfx::IntSize const& size() const { return m_size; }
  265. private:
  266. Gfx::IntSize m_size;
  267. };
  268. class ContextMenuEvent final : public Event {
  269. public:
  270. explicit ContextMenuEvent(Gfx::IntPoint const& position, Gfx::IntPoint const& screen_position)
  271. : Event(Event::ContextMenu)
  272. , m_position(position)
  273. , m_screen_position(screen_position)
  274. {
  275. }
  276. Gfx::IntPoint const& position() const { return m_position; }
  277. Gfx::IntPoint const& screen_position() const { return m_screen_position; }
  278. private:
  279. Gfx::IntPoint m_position;
  280. Gfx::IntPoint m_screen_position;
  281. };
  282. class ShowEvent final : public Event {
  283. public:
  284. ShowEvent()
  285. : Event(Event::Show)
  286. {
  287. }
  288. };
  289. class HideEvent final : public Event {
  290. public:
  291. HideEvent()
  292. : Event(Event::Hide)
  293. {
  294. }
  295. };
  296. enum MouseButton : u8 {
  297. None = 0,
  298. Primary = 1,
  299. Secondary = 2,
  300. Middle = 4,
  301. Backward = 8,
  302. Forward = 16,
  303. };
  304. class KeyEvent final : public Event {
  305. public:
  306. KeyEvent(Type type, KeyCode key, u8 modifiers, u32 code_point, u32 scancode)
  307. : Event(type)
  308. , m_key(key)
  309. , m_modifiers(modifiers)
  310. , m_code_point(code_point)
  311. , m_scancode(scancode)
  312. {
  313. }
  314. KeyCode key() const { return m_key; }
  315. bool ctrl() const { return m_modifiers & Mod_Ctrl; }
  316. bool alt() const { return m_modifiers & Mod_Alt; }
  317. bool altgr() const { return m_modifiers & Mod_AltGr; }
  318. bool shift() const { return m_modifiers & Mod_Shift; }
  319. bool super() const { return m_modifiers & Mod_Super; }
  320. u8 modifiers() const { return m_modifiers; }
  321. u32 code_point() const { return m_code_point; }
  322. String text() const
  323. {
  324. StringBuilder sb;
  325. sb.append_code_point(m_code_point);
  326. return sb.to_string();
  327. }
  328. u32 scancode() const { return m_scancode; }
  329. String to_string() const;
  330. bool is_arrow_key() const
  331. {
  332. switch (m_key) {
  333. case KeyCode::Key_Up:
  334. case KeyCode::Key_Down:
  335. case KeyCode::Key_Left:
  336. case KeyCode::Key_Right:
  337. return true;
  338. default:
  339. return false;
  340. }
  341. }
  342. private:
  343. friend class ConnectionToWindowServer;
  344. KeyCode m_key { KeyCode::Key_Invalid };
  345. u8 m_modifiers { 0 };
  346. u32 m_code_point { 0 };
  347. u32 m_scancode { 0 };
  348. };
  349. class MouseEvent final : public Event {
  350. public:
  351. MouseEvent(Type type, Gfx::IntPoint const& position, unsigned buttons, MouseButton button, unsigned modifiers, int wheel_delta_x, int wheel_delta_y, int wheel_raw_delta_x, int wheel_raw_delta_y)
  352. : Event(type)
  353. , m_position(position)
  354. , m_buttons(buttons)
  355. , m_button(button)
  356. , m_modifiers(modifiers)
  357. , m_wheel_delta_x(wheel_delta_x)
  358. , m_wheel_delta_y(wheel_delta_y)
  359. , m_wheel_raw_delta_x(wheel_raw_delta_x)
  360. , m_wheel_raw_delta_y(wheel_raw_delta_y)
  361. {
  362. }
  363. Gfx::IntPoint const& position() const { return m_position; }
  364. int x() const { return m_position.x(); }
  365. int y() const { return m_position.y(); }
  366. MouseButton button() const { return m_button; }
  367. unsigned buttons() const { return m_buttons; }
  368. bool ctrl() const { return m_modifiers & Mod_Ctrl; }
  369. bool alt() const { return m_modifiers & Mod_Alt; }
  370. bool shift() const { return m_modifiers & Mod_Shift; }
  371. bool super() const { return m_modifiers & Mod_Super; }
  372. unsigned modifiers() const { return m_modifiers; }
  373. int wheel_delta_x() const { return m_wheel_delta_x; }
  374. int wheel_delta_y() const { return m_wheel_delta_y; }
  375. int wheel_raw_delta_x() const { return m_wheel_raw_delta_x; }
  376. int wheel_raw_delta_y() const { return m_wheel_raw_delta_y; }
  377. private:
  378. Gfx::IntPoint m_position;
  379. unsigned m_buttons { 0 };
  380. MouseButton m_button { MouseButton::None };
  381. unsigned m_modifiers { 0 };
  382. int m_wheel_delta_x { 0 };
  383. int m_wheel_delta_y { 0 };
  384. int m_wheel_raw_delta_x { 0 };
  385. int m_wheel_raw_delta_y { 0 };
  386. };
  387. class DragEvent final : public Event {
  388. public:
  389. DragEvent(Type type, Gfx::IntPoint const& position, Vector<String> mime_types)
  390. : Event(type)
  391. , m_position(position)
  392. , m_mime_types(move(mime_types))
  393. {
  394. }
  395. Gfx::IntPoint const& position() const { return m_position; }
  396. Vector<String> const& mime_types() const { return m_mime_types; }
  397. private:
  398. Gfx::IntPoint m_position;
  399. Vector<String> m_mime_types;
  400. };
  401. class DropEvent final : public Event {
  402. public:
  403. DropEvent(Gfx::IntPoint const&, String const& text, NonnullRefPtr<Core::MimeData> mime_data);
  404. ~DropEvent() = default;
  405. Gfx::IntPoint const& position() const { return m_position; }
  406. String const& text() const { return m_text; }
  407. Core::MimeData const& mime_data() const { return m_mime_data; }
  408. private:
  409. Gfx::IntPoint m_position;
  410. String m_text;
  411. NonnullRefPtr<Core::MimeData> m_mime_data;
  412. };
  413. class ThemeChangeEvent final : public Event {
  414. public:
  415. ThemeChangeEvent()
  416. : Event(Type::ThemeChange)
  417. {
  418. }
  419. };
  420. class FontsChangeEvent final : public Event {
  421. public:
  422. FontsChangeEvent()
  423. : Event(Type::FontsChange)
  424. {
  425. }
  426. };
  427. class ScreenRectsChangeEvent final : public Event {
  428. public:
  429. explicit ScreenRectsChangeEvent(Vector<Gfx::IntRect, 4> const& rects, size_t main_screen_index)
  430. : Event(Type::ScreenRectsChange)
  431. , m_rects(rects)
  432. , m_main_screen_index(main_screen_index)
  433. {
  434. }
  435. Vector<Gfx::IntRect, 4> const& rects() const { return m_rects; }
  436. size_t main_screen_index() const { return m_main_screen_index; }
  437. private:
  438. Vector<Gfx::IntRect, 4> m_rects;
  439. size_t m_main_screen_index;
  440. };
  441. class AppletAreaRectChangeEvent final : public Event {
  442. public:
  443. explicit AppletAreaRectChangeEvent(Gfx::IntRect rect)
  444. : Event(Type::AppletAreaRectChange)
  445. , m_rect(rect)
  446. {
  447. }
  448. Gfx::IntRect rect() const { return m_rect; }
  449. private:
  450. Gfx::IntRect const m_rect;
  451. };
  452. class FocusEvent final : public Event {
  453. public:
  454. explicit FocusEvent(Type type, FocusSource source)
  455. : Event(type)
  456. , m_source(source)
  457. {
  458. }
  459. FocusSource source() const { return m_source; }
  460. private:
  461. FocusSource m_source { FocusSource::Programmatic };
  462. };
  463. class ActionEvent final : public Event {
  464. public:
  465. ActionEvent(Type, Action&);
  466. ~ActionEvent() = default;
  467. Action const& action() const { return *m_action; }
  468. Action& action() { return *m_action; }
  469. private:
  470. NonnullRefPtr<Action> m_action;
  471. };
  472. inline StringView mouse_button_to_string(MouseButton key)
  473. {
  474. switch (key) {
  475. case MouseButton::None:
  476. return "None"sv;
  477. case MouseButton::Primary:
  478. return "Primary"sv;
  479. case MouseButton::Secondary:
  480. return "Secondary"sv;
  481. case MouseButton::Middle:
  482. return "Middle"sv;
  483. case MouseButton::Backward:
  484. return "Backward"sv;
  485. case MouseButton::Forward:
  486. return "Forward"sv;
  487. default:
  488. VERIFY_NOT_REACHED();
  489. }
  490. }
  491. }