WindowServerConnection.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (c) 2018-2020, 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. void WindowServerConnection::key_down(i32 window_id, u32 code_point, u32 key, u32 modifiers, u32 scancode)
  99. {
  100. auto* window = Window::from_window_id(window_id);
  101. if (!window)
  102. return;
  103. auto key_event = make<KeyEvent>(Event::KeyDown, (KeyCode)key, modifiers, code_point, scancode);
  104. Action* action = nullptr;
  105. dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, "Looking up action for {}", key_event->to_string());
  106. if (auto* focused_widget = window->focused_widget()) {
  107. for (auto* widget = focused_widget; widget && !action; widget = widget->parent_widget()) {
  108. action = widget->action_for_key_event(*key_event);
  109. dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, " > Focused widget {} gave action: {}", *widget, action);
  110. }
  111. }
  112. if (!action) {
  113. action = window->action_for_key_event(*key_event);
  114. dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, " > Asked window {}, got action: {}", *window, action);
  115. }
  116. // NOTE: Application-global shortcuts are ignored while a modal window is up.
  117. if (!action && !window->is_modal()) {
  118. action = Application::the()->action_for_key_event(*key_event);
  119. dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, " > Asked application, got action: {}", action);
  120. }
  121. if (action) {
  122. if (action->is_enabled()) {
  123. action->activate();
  124. return;
  125. }
  126. if (action->swallow_key_event_when_disabled())
  127. return;
  128. }
  129. bool focused_widget_accepts_emoji_input = window->focused_widget() && window->focused_widget()->accepts_emoji_input();
  130. if (focused_widget_accepts_emoji_input && (modifiers == (Mod_Ctrl | Mod_Alt)) && key == Key_Space) {
  131. auto emoji_input_dialog = EmojiInputDialog::construct(window);
  132. if (emoji_input_dialog->exec() != EmojiInputDialog::ExecOK)
  133. return;
  134. key_event->m_key = Key_Invalid;
  135. key_event->m_modifiers = 0;
  136. Utf8View m_utf8_view(emoji_input_dialog->selected_emoji_text().characters());
  137. u32 emoji_code_point = *m_utf8_view.begin();
  138. key_event->m_code_point = emoji_code_point;
  139. }
  140. Core::EventLoop::current().post_event(*window, move(key_event));
  141. }
  142. void WindowServerConnection::key_up(i32 window_id, u32 code_point, u32 key, u32 modifiers, u32 scancode)
  143. {
  144. auto* window = Window::from_window_id(window_id);
  145. if (!window)
  146. return;
  147. auto key_event = make<KeyEvent>(Event::KeyUp, (KeyCode)key, modifiers, code_point, scancode);
  148. Core::EventLoop::current().post_event(*window, move(key_event));
  149. }
  150. static MouseButton to_gmousebutton(u32 button)
  151. {
  152. switch (button) {
  153. case 0:
  154. return MouseButton::None;
  155. case 1:
  156. return MouseButton::Left;
  157. case 2:
  158. return MouseButton::Right;
  159. case 4:
  160. return MouseButton::Middle;
  161. case 8:
  162. return MouseButton::Back;
  163. case 16:
  164. return MouseButton::Forward;
  165. default:
  166. VERIFY_NOT_REACHED();
  167. break;
  168. }
  169. }
  170. void WindowServerConnection::mouse_down(i32 window_id, Gfx::IntPoint const& mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta)
  171. {
  172. if (auto* window = Window::from_window_id(window_id))
  173. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDown, mouse_position, buttons, to_gmousebutton(button), modifiers, wheel_delta));
  174. }
  175. void WindowServerConnection::mouse_up(i32 window_id, Gfx::IntPoint const& mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta)
  176. {
  177. if (auto* window = Window::from_window_id(window_id))
  178. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseUp, mouse_position, buttons, to_gmousebutton(button), modifiers, wheel_delta));
  179. }
  180. 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)
  181. {
  182. if (auto* window = Window::from_window_id(window_id)) {
  183. if (is_drag)
  184. Core::EventLoop::current().post_event(*window, make<DragEvent>(Event::DragMove, mouse_position, mime_types));
  185. else
  186. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseMove, mouse_position, buttons, to_gmousebutton(button), modifiers, wheel_delta));
  187. }
  188. }
  189. void WindowServerConnection::mouse_double_click(i32 window_id, Gfx::IntPoint const& mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta)
  190. {
  191. if (auto* window = Window::from_window_id(window_id))
  192. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDoubleClick, mouse_position, buttons, to_gmousebutton(button), modifiers, wheel_delta));
  193. }
  194. void WindowServerConnection::mouse_wheel(i32 window_id, Gfx::IntPoint const& mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta)
  195. {
  196. if (auto* window = Window::from_window_id(window_id))
  197. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseWheel, mouse_position, buttons, to_gmousebutton(button), modifiers, wheel_delta));
  198. }
  199. void WindowServerConnection::menu_visibility_did_change(i32 menu_id, bool visible)
  200. {
  201. auto* menu = Menu::from_menu_id(menu_id);
  202. if (!menu) {
  203. dbgln("EventLoop received visibility change event for invalid menu ID {}", menu_id);
  204. return;
  205. }
  206. menu->visibility_did_change({}, visible);
  207. }
  208. void WindowServerConnection::menu_item_activated(i32 menu_id, u32 identifier)
  209. {
  210. auto* menu = Menu::from_menu_id(menu_id);
  211. if (!menu) {
  212. dbgln("EventLoop received event for invalid menu ID {}", menu_id);
  213. return;
  214. }
  215. if (auto* action = menu->action_at(identifier))
  216. action->activate(menu);
  217. }
  218. void WindowServerConnection::menu_item_entered(i32 menu_id, u32 identifier)
  219. {
  220. auto* menu = Menu::from_menu_id(menu_id);
  221. if (!menu) {
  222. dbgln("WindowServerConnection received MenuItemEntered for invalid menu ID {}", menu_id);
  223. return;
  224. }
  225. auto* action = menu->action_at(identifier);
  226. if (!action)
  227. return;
  228. auto* app = Application::the();
  229. if (!app)
  230. return;
  231. Core::EventLoop::current().post_event(*app, make<ActionEvent>(GUI::Event::ActionEnter, *action));
  232. }
  233. void WindowServerConnection::menu_item_left(i32 menu_id, u32 identifier)
  234. {
  235. auto* menu = Menu::from_menu_id(menu_id);
  236. if (!menu) {
  237. dbgln("WindowServerConnection received MenuItemLeft for invalid menu ID {}", menu_id);
  238. return;
  239. }
  240. auto* action = menu->action_at(identifier);
  241. if (!action)
  242. return;
  243. auto* app = Application::the();
  244. if (!app)
  245. return;
  246. Core::EventLoop::current().post_event(*app, make<ActionEvent>(GUI::Event::ActionLeave, *action));
  247. }
  248. void WindowServerConnection::screen_rect_changed(Gfx::IntRect const& rect)
  249. {
  250. Desktop::the().did_receive_screen_rect({}, rect);
  251. Window::for_each_window({}, [rect](auto& window) {
  252. Core::EventLoop::current().post_event(window, make<ScreenRectChangeEvent>(rect));
  253. });
  254. }
  255. void WindowServerConnection::set_wallpaper_finished(bool)
  256. {
  257. // This is handled manually by Desktop::set_wallpaper().
  258. }
  259. void WindowServerConnection::drag_dropped(i32 window_id, Gfx::IntPoint const& mouse_position, String const& text, HashMap<String, ByteBuffer> const& mime_data)
  260. {
  261. if (auto* window = Window::from_window_id(window_id)) {
  262. auto mime_data_obj = Core::MimeData::construct(mime_data);
  263. Core::EventLoop::current().post_event(*window, make<DropEvent>(mouse_position, text, mime_data_obj));
  264. }
  265. }
  266. void WindowServerConnection::drag_accepted()
  267. {
  268. DragOperation::notify_accepted({});
  269. }
  270. void WindowServerConnection::drag_cancelled()
  271. {
  272. DragOperation::notify_cancelled({});
  273. Application::the()->notify_drag_cancelled({});
  274. }
  275. void WindowServerConnection::window_state_changed(i32 window_id, bool minimized, bool occluded)
  276. {
  277. if (auto* window = Window::from_window_id(window_id))
  278. window->notify_state_changed({}, minimized, occluded);
  279. }
  280. void WindowServerConnection::display_link_notification()
  281. {
  282. if (m_display_link_notification_pending)
  283. return;
  284. m_display_link_notification_pending = true;
  285. deferred_invoke([this](auto&) {
  286. DisplayLink::notify({});
  287. m_display_link_notification_pending = false;
  288. });
  289. }
  290. void WindowServerConnection::ping()
  291. {
  292. async_pong();
  293. }
  294. }