WindowServerConnection.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/StringBuilder.h>
  8. #include <LibCore/EventLoop.h>
  9. #include <LibCore/MimeData.h>
  10. #include <LibGUI/Action.h>
  11. #include <LibGUI/Application.h>
  12. #include <LibGUI/Clipboard.h>
  13. #include <LibGUI/Desktop.h>
  14. #include <LibGUI/DisplayLink.h>
  15. #include <LibGUI/DragOperation.h>
  16. #include <LibGUI/EmojiInputDialog.h>
  17. #include <LibGUI/Event.h>
  18. #include <LibGUI/Menu.h>
  19. #include <LibGUI/Widget.h>
  20. #include <LibGUI/Window.h>
  21. #include <LibGUI/WindowServerConnection.h>
  22. #include <LibGfx/Bitmap.h>
  23. #include <LibGfx/Palette.h>
  24. #include <LibGfx/SystemTheme.h>
  25. namespace GUI {
  26. WindowServerConnection& WindowServerConnection::the()
  27. {
  28. static WindowServerConnection* s_connection = nullptr;
  29. if (!s_connection)
  30. s_connection = new WindowServerConnection;
  31. return *s_connection;
  32. }
  33. static void set_system_theme_from_anonymous_buffer(Core::AnonymousBuffer buffer)
  34. {
  35. Gfx::set_system_theme(buffer);
  36. Application::the()->set_system_palette(buffer);
  37. }
  38. void WindowServerConnection::handshake()
  39. {
  40. auto response = greet();
  41. set_system_theme_from_anonymous_buffer(response.theme_buffer());
  42. Desktop::the().did_receive_screen_rect({}, response.screen_rect());
  43. }
  44. void WindowServerConnection::update_system_theme(Core::AnonymousBuffer const& theme_buffer)
  45. {
  46. set_system_theme_from_anonymous_buffer(theme_buffer);
  47. Window::update_all_windows({});
  48. Window::for_each_window({}, [](auto& window) {
  49. Core::EventLoop::current().post_event(window, make<ThemeChangeEvent>());
  50. });
  51. }
  52. void WindowServerConnection::paint(i32 window_id, Gfx::IntSize const& window_size, Vector<Gfx::IntRect> const& rects)
  53. {
  54. if (auto* window = Window::from_window_id(window_id))
  55. Core::EventLoop::current().post_event(*window, make<MultiPaintEvent>(rects, window_size));
  56. }
  57. void WindowServerConnection::window_resized(i32 window_id, Gfx::IntRect const& new_rect)
  58. {
  59. if (auto* window = Window::from_window_id(window_id)) {
  60. Core::EventLoop::current().post_event(*window, make<ResizeEvent>(new_rect.size()));
  61. }
  62. }
  63. void WindowServerConnection::window_activated(i32 window_id)
  64. {
  65. if (auto* window = Window::from_window_id(window_id))
  66. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowBecameActive));
  67. }
  68. void WindowServerConnection::window_deactivated(i32 window_id)
  69. {
  70. if (auto* window = Window::from_window_id(window_id))
  71. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowBecameInactive));
  72. }
  73. void WindowServerConnection::window_input_entered(i32 window_id)
  74. {
  75. if (auto* window = Window::from_window_id(window_id))
  76. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowInputEntered));
  77. }
  78. void WindowServerConnection::window_input_left(i32 window_id)
  79. {
  80. if (auto* window = Window::from_window_id(window_id))
  81. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowInputLeft));
  82. }
  83. void WindowServerConnection::window_close_request(i32 window_id)
  84. {
  85. if (auto* window = Window::from_window_id(window_id))
  86. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowCloseRequest));
  87. }
  88. void WindowServerConnection::window_entered(i32 window_id)
  89. {
  90. if (auto* window = Window::from_window_id(window_id))
  91. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowEntered));
  92. }
  93. void WindowServerConnection::window_left(i32 window_id)
  94. {
  95. if (auto* window = Window::from_window_id(window_id))
  96. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowLeft));
  97. }
  98. static Action* action_for_key_event(Window& window, KeyEvent const& event)
  99. {
  100. if (event.key() == KeyCode::Key_Invalid)
  101. return nullptr;
  102. dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, "Looking up action for {}", event.to_string());
  103. for (auto* widget = window.focused_widget(); widget; widget = widget->parent_widget()) {
  104. if (auto* action = widget->action_for_key_event(event)) {
  105. dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, " > Focused widget {} gave action: {}", *widget, action);
  106. return action;
  107. }
  108. }
  109. if (auto* action = window.action_for_key_event(event)) {
  110. dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, " > Asked window {}, got action: {}", window, action);
  111. return action;
  112. }
  113. // NOTE: Application-global shortcuts are ignored while a modal window is up.
  114. if (!window.is_modal()) {
  115. if (auto* action = Application::the()->action_for_key_event(event)) {
  116. dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, " > Asked application, got action: {}", action);
  117. return action;
  118. }
  119. }
  120. return nullptr;
  121. }
  122. void WindowServerConnection::key_down(i32 window_id, u32 code_point, u32 key, u32 modifiers, u32 scancode)
  123. {
  124. auto* window = Window::from_window_id(window_id);
  125. if (!window)
  126. return;
  127. auto key_event = make<KeyEvent>(Event::KeyDown, (KeyCode)key, modifiers, code_point, scancode);
  128. if (auto* action = action_for_key_event(*window, *key_event)) {
  129. if (action->is_enabled()) {
  130. action->activate();
  131. return;
  132. }
  133. if (action->swallow_key_event_when_disabled())
  134. return;
  135. }
  136. bool focused_widget_accepts_emoji_input = window->focused_widget() && window->focused_widget()->accepts_emoji_input();
  137. if (focused_widget_accepts_emoji_input && (modifiers == (Mod_Ctrl | Mod_Alt)) && key == Key_Space) {
  138. auto emoji_input_dialog = EmojiInputDialog::construct(window);
  139. if (emoji_input_dialog->exec() != EmojiInputDialog::ExecOK)
  140. return;
  141. key_event->m_key = Key_Invalid;
  142. key_event->m_modifiers = 0;
  143. Utf8View m_utf8_view(emoji_input_dialog->selected_emoji_text().characters());
  144. u32 emoji_code_point = *m_utf8_view.begin();
  145. key_event->m_code_point = emoji_code_point;
  146. }
  147. Core::EventLoop::current().post_event(*window, move(key_event));
  148. }
  149. void WindowServerConnection::key_up(i32 window_id, u32 code_point, u32 key, u32 modifiers, u32 scancode)
  150. {
  151. auto* window = Window::from_window_id(window_id);
  152. if (!window)
  153. return;
  154. auto key_event = make<KeyEvent>(Event::KeyUp, (KeyCode)key, modifiers, code_point, scancode);
  155. Core::EventLoop::current().post_event(*window, move(key_event));
  156. }
  157. static MouseButton to_gmousebutton(u32 button)
  158. {
  159. switch (button) {
  160. case 0:
  161. return MouseButton::None;
  162. case 1:
  163. return MouseButton::Left;
  164. case 2:
  165. return MouseButton::Right;
  166. case 4:
  167. return MouseButton::Middle;
  168. case 8:
  169. return MouseButton::Back;
  170. case 16:
  171. return MouseButton::Forward;
  172. default:
  173. VERIFY_NOT_REACHED();
  174. break;
  175. }
  176. }
  177. void WindowServerConnection::mouse_down(i32 window_id, Gfx::IntPoint const& mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta)
  178. {
  179. if (auto* window = Window::from_window_id(window_id))
  180. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDown, mouse_position, buttons, to_gmousebutton(button), modifiers, wheel_delta));
  181. }
  182. void WindowServerConnection::mouse_up(i32 window_id, Gfx::IntPoint const& mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta)
  183. {
  184. if (auto* window = Window::from_window_id(window_id))
  185. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseUp, mouse_position, buttons, to_gmousebutton(button), modifiers, wheel_delta));
  186. }
  187. void WindowServerConnection::mouse_move(i32 window_id, Gfx::IntPoint const& mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta, bool is_drag, Vector<String> const& mime_types)
  188. {
  189. if (auto* window = Window::from_window_id(window_id)) {
  190. if (is_drag)
  191. Core::EventLoop::current().post_event(*window, make<DragEvent>(Event::DragMove, mouse_position, mime_types));
  192. else
  193. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseMove, mouse_position, buttons, to_gmousebutton(button), modifiers, wheel_delta));
  194. }
  195. }
  196. void WindowServerConnection::mouse_double_click(i32 window_id, Gfx::IntPoint const& mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta)
  197. {
  198. if (auto* window = Window::from_window_id(window_id))
  199. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDoubleClick, mouse_position, buttons, to_gmousebutton(button), modifiers, wheel_delta));
  200. }
  201. void WindowServerConnection::mouse_wheel(i32 window_id, Gfx::IntPoint const& mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta)
  202. {
  203. if (auto* window = Window::from_window_id(window_id))
  204. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseWheel, mouse_position, buttons, to_gmousebutton(button), modifiers, wheel_delta));
  205. }
  206. void WindowServerConnection::menu_visibility_did_change(i32 menu_id, bool visible)
  207. {
  208. auto* menu = Menu::from_menu_id(menu_id);
  209. if (!menu) {
  210. dbgln("EventLoop received visibility change event for invalid menu ID {}", menu_id);
  211. return;
  212. }
  213. menu->visibility_did_change({}, visible);
  214. }
  215. void WindowServerConnection::menu_item_activated(i32 menu_id, u32 identifier)
  216. {
  217. auto* menu = Menu::from_menu_id(menu_id);
  218. if (!menu) {
  219. dbgln("EventLoop received event for invalid menu ID {}", menu_id);
  220. return;
  221. }
  222. if (auto* action = menu->action_at(identifier))
  223. action->activate(menu);
  224. }
  225. void WindowServerConnection::menu_item_entered(i32 menu_id, u32 identifier)
  226. {
  227. auto* menu = Menu::from_menu_id(menu_id);
  228. if (!menu) {
  229. dbgln("WindowServerConnection received MenuItemEntered for invalid menu ID {}", menu_id);
  230. return;
  231. }
  232. auto* action = menu->action_at(identifier);
  233. if (!action)
  234. return;
  235. auto* app = Application::the();
  236. if (!app)
  237. return;
  238. Core::EventLoop::current().post_event(*app, make<ActionEvent>(GUI::Event::ActionEnter, *action));
  239. }
  240. void WindowServerConnection::menu_item_left(i32 menu_id, u32 identifier)
  241. {
  242. auto* menu = Menu::from_menu_id(menu_id);
  243. if (!menu) {
  244. dbgln("WindowServerConnection received MenuItemLeft for invalid menu ID {}", menu_id);
  245. return;
  246. }
  247. auto* action = menu->action_at(identifier);
  248. if (!action)
  249. return;
  250. auto* app = Application::the();
  251. if (!app)
  252. return;
  253. Core::EventLoop::current().post_event(*app, make<ActionEvent>(GUI::Event::ActionLeave, *action));
  254. }
  255. void WindowServerConnection::screen_rect_changed(Gfx::IntRect const& rect)
  256. {
  257. Desktop::the().did_receive_screen_rect({}, rect);
  258. Window::for_each_window({}, [rect](auto& window) {
  259. Core::EventLoop::current().post_event(window, make<ScreenRectChangeEvent>(rect));
  260. });
  261. }
  262. void WindowServerConnection::set_wallpaper_finished(bool)
  263. {
  264. // This is handled manually by Desktop::set_wallpaper().
  265. }
  266. void WindowServerConnection::drag_dropped(i32 window_id, Gfx::IntPoint const& mouse_position, String const& text, HashMap<String, ByteBuffer> const& mime_data)
  267. {
  268. if (auto* window = Window::from_window_id(window_id)) {
  269. auto mime_data_obj = Core::MimeData::construct(mime_data);
  270. Core::EventLoop::current().post_event(*window, make<DropEvent>(mouse_position, text, mime_data_obj));
  271. }
  272. }
  273. void WindowServerConnection::drag_accepted()
  274. {
  275. DragOperation::notify_accepted({});
  276. }
  277. void WindowServerConnection::drag_cancelled()
  278. {
  279. DragOperation::notify_cancelled({});
  280. Application::the()->notify_drag_cancelled({});
  281. }
  282. void WindowServerConnection::window_state_changed(i32 window_id, bool minimized, bool occluded)
  283. {
  284. if (auto* window = Window::from_window_id(window_id))
  285. window->notify_state_changed({}, minimized, occluded);
  286. }
  287. void WindowServerConnection::display_link_notification()
  288. {
  289. if (m_display_link_notification_pending)
  290. return;
  291. m_display_link_notification_pending = true;
  292. deferred_invoke([this](auto&) {
  293. DisplayLink::notify({});
  294. m_display_link_notification_pending = false;
  295. });
  296. }
  297. void WindowServerConnection::ping()
  298. {
  299. async_pong();
  300. }
  301. }