WindowServerConnection.cpp 12 KB

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