ConnectionToWindowServer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Copyright (c) 2018-2022, 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/CommandPalette.h>
  13. #include <LibGUI/ConnectionToWindowServer.h>
  14. #include <LibGUI/Desktop.h>
  15. #include <LibGUI/DisplayLink.h>
  16. #include <LibGUI/DragOperation.h>
  17. #include <LibGUI/EmojiInputDialog.h>
  18. #include <LibGUI/Event.h>
  19. #include <LibGUI/Menu.h>
  20. #include <LibGUI/MouseTracker.h>
  21. #include <LibGUI/Shortcut.h>
  22. #include <LibGUI/Window.h>
  23. #include <LibGfx/Bitmap.h>
  24. #include <LibGfx/Font/FontDatabase.h>
  25. #include <LibGfx/Palette.h>
  26. #include <LibGfx/SystemTheme.h>
  27. namespace GUI {
  28. ConnectionToWindowServer& ConnectionToWindowServer::the()
  29. {
  30. static RefPtr<ConnectionToWindowServer> s_connection = nullptr;
  31. if (!s_connection)
  32. s_connection = ConnectionToWindowServer::try_create().release_value_but_fixme_should_propagate_errors();
  33. return *s_connection;
  34. }
  35. static void set_system_theme_from_anonymous_buffer(Core::AnonymousBuffer buffer)
  36. {
  37. Gfx::set_system_theme(buffer);
  38. Application::the()->set_system_palette(buffer);
  39. }
  40. ConnectionToWindowServer::ConnectionToWindowServer(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
  41. : IPC::ConnectionToServer<WindowClientEndpoint, WindowServerEndpoint>(*this, move(socket))
  42. {
  43. // NOTE: WindowServer automatically sends a "fast_greet" message to us when we connect.
  44. // All we have to do is wait for it to arrive. This avoids a round-trip during application startup.
  45. auto message = wait_for_specific_message<Messages::WindowClient::FastGreet>();
  46. set_system_theme_from_anonymous_buffer(message->theme_buffer());
  47. Desktop::the().did_receive_screen_rects({}, message->screen_rects(), message->main_screen_index(), message->workspace_rows(), message->workspace_columns());
  48. Gfx::FontDatabase::set_default_font_query(message->default_font_query());
  49. Gfx::FontDatabase::set_fixed_width_font_query(message->fixed_width_font_query());
  50. m_client_id = message->client_id();
  51. }
  52. void ConnectionToWindowServer::fast_greet(Vector<Gfx::IntRect> const&, u32, u32, u32, Core::AnonymousBuffer const&, String const&, String const&, i32)
  53. {
  54. // NOTE: This message is handled in the constructor.
  55. }
  56. void ConnectionToWindowServer::update_system_theme(Core::AnonymousBuffer const& theme_buffer)
  57. {
  58. set_system_theme_from_anonymous_buffer(theme_buffer);
  59. Window::update_all_windows({});
  60. Window::for_each_window({}, [](auto& window) {
  61. Core::EventLoop::current().post_event(window, make<ThemeChangeEvent>());
  62. });
  63. }
  64. void ConnectionToWindowServer::update_system_fonts(String const& default_font_query, String const& fixed_width_font_query)
  65. {
  66. Gfx::FontDatabase::set_default_font_query(default_font_query);
  67. Gfx::FontDatabase::set_fixed_width_font_query(fixed_width_font_query);
  68. Window::update_all_windows({});
  69. Window::for_each_window({}, [](auto& window) {
  70. Core::EventLoop::current().post_event(window, make<FontsChangeEvent>());
  71. });
  72. }
  73. void ConnectionToWindowServer::paint(i32 window_id, Gfx::IntSize const& window_size, Vector<Gfx::IntRect> const& rects)
  74. {
  75. if (auto* window = Window::from_window_id(window_id))
  76. Core::EventLoop::current().post_event(*window, make<MultiPaintEvent>(rects, window_size));
  77. }
  78. void ConnectionToWindowServer::window_resized(i32 window_id, Gfx::IntRect const& new_rect)
  79. {
  80. if (auto* window = Window::from_window_id(window_id)) {
  81. Core::EventLoop::current().post_event(*window, make<ResizeEvent>(new_rect.size()));
  82. }
  83. }
  84. void ConnectionToWindowServer::window_activated(i32 window_id)
  85. {
  86. if (auto* window = Window::from_window_id(window_id))
  87. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowBecameActive));
  88. }
  89. void ConnectionToWindowServer::window_deactivated(i32 window_id)
  90. {
  91. if (auto* window = Window::from_window_id(window_id))
  92. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowBecameInactive));
  93. }
  94. void ConnectionToWindowServer::window_input_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::WindowInputEntered));
  98. }
  99. void ConnectionToWindowServer::window_input_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::WindowInputLeft));
  103. }
  104. void ConnectionToWindowServer::window_close_request(i32 window_id)
  105. {
  106. if (auto* window = Window::from_window_id(window_id))
  107. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowCloseRequest));
  108. }
  109. void ConnectionToWindowServer::window_entered(i32 window_id)
  110. {
  111. if (auto* window = Window::from_window_id(window_id))
  112. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowEntered));
  113. }
  114. void ConnectionToWindowServer::window_left(i32 window_id)
  115. {
  116. if (auto* window = Window::from_window_id(window_id))
  117. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowLeft));
  118. }
  119. static Action* action_for_shortcut(Window& window, Shortcut const& shortcut)
  120. {
  121. if (!shortcut.is_valid())
  122. return nullptr;
  123. dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, "Looking up action for {}", shortcut.to_string());
  124. for (auto* widget = window.focused_widget(); widget; widget = widget->parent_widget()) {
  125. if (auto* action = widget->action_for_shortcut(shortcut)) {
  126. dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, " > Focused widget {} gave action: {}", *widget, action);
  127. return action;
  128. }
  129. }
  130. if (auto* action = window.action_for_shortcut(shortcut)) {
  131. dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, " > Asked window {}, got action: {}", window, action);
  132. return action;
  133. }
  134. // NOTE: Application-global shortcuts are ignored while a modal window is up.
  135. if (!window.is_modal()) {
  136. if (auto* action = Application::the()->action_for_shortcut(shortcut)) {
  137. dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, " > Asked application, got action: {}", action);
  138. return action;
  139. }
  140. }
  141. return nullptr;
  142. }
  143. void ConnectionToWindowServer::key_down(i32 window_id, u32 code_point, u32 key, u32 modifiers, u32 scancode)
  144. {
  145. auto* window = Window::from_window_id(window_id);
  146. if (!window)
  147. return;
  148. auto key_event = make<KeyEvent>(Event::KeyDown, (KeyCode)key, modifiers, code_point, scancode);
  149. if (auto* action = action_for_shortcut(*window, Shortcut(key_event->modifiers(), key_event->key()))) {
  150. if (action->is_enabled()) {
  151. action->flash_menubar_menu(*window);
  152. action->activate();
  153. return;
  154. }
  155. if (action->swallow_key_event_when_disabled())
  156. return;
  157. }
  158. bool focused_widget_accepts_emoji_input = window->focused_widget() && window->focused_widget()->accepts_emoji_input();
  159. if (focused_widget_accepts_emoji_input && (modifiers == (Mod_Ctrl | Mod_Alt)) && key == Key_Space) {
  160. auto emoji_input_dialog = EmojiInputDialog::construct(window);
  161. if (emoji_input_dialog->exec() != EmojiInputDialog::ExecResult::OK)
  162. return;
  163. key_event->m_key = Key_Invalid;
  164. key_event->m_modifiers = 0;
  165. Utf8View m_utf8_view(emoji_input_dialog->selected_emoji_text());
  166. u32 emoji_code_point = *m_utf8_view.begin();
  167. key_event->m_code_point = emoji_code_point;
  168. }
  169. bool accepts_command_palette = true;
  170. if (window->focused_widget())
  171. accepts_command_palette = window->focused_widget()->accepts_command_palette();
  172. // FIXME: This shortcut should be configurable.
  173. if (accepts_command_palette && !m_in_command_palette && modifiers == (Mod_Ctrl | Mod_Shift) && key == Key_A) {
  174. auto command_palette = CommandPalette::construct(*window);
  175. TemporaryChange change { m_in_command_palette, true };
  176. if (command_palette->exec() != GUI::Dialog::ExecResult::OK)
  177. return;
  178. auto* action = command_palette->selected_action();
  179. VERIFY(action);
  180. action->flash_menubar_menu(*window);
  181. action->activate();
  182. return;
  183. }
  184. Core::EventLoop::current().post_event(*window, move(key_event));
  185. }
  186. void ConnectionToWindowServer::key_up(i32 window_id, u32 code_point, u32 key, u32 modifiers, u32 scancode)
  187. {
  188. auto* window = Window::from_window_id(window_id);
  189. if (!window)
  190. return;
  191. auto key_event = make<KeyEvent>(Event::KeyUp, (KeyCode)key, modifiers, code_point, scancode);
  192. Core::EventLoop::current().post_event(*window, move(key_event));
  193. }
  194. static MouseButton to_mouse_button(u32 button)
  195. {
  196. switch (button) {
  197. case 0:
  198. return MouseButton::None;
  199. case 1:
  200. return MouseButton::Primary;
  201. case 2:
  202. return MouseButton::Secondary;
  203. case 4:
  204. return MouseButton::Middle;
  205. case 8:
  206. return MouseButton::Backward;
  207. case 16:
  208. return MouseButton::Forward;
  209. default:
  210. VERIFY_NOT_REACHED();
  211. break;
  212. }
  213. }
  214. void ConnectionToWindowServer::mouse_down(i32 window_id, Gfx::IntPoint const& mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta_x, i32 wheel_delta_y, i32 wheel_raw_delta_x, i32 wheel_raw_delta_y)
  215. {
  216. if (auto* window = Window::from_window_id(window_id))
  217. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDown, mouse_position, buttons, to_mouse_button(button), modifiers, wheel_delta_x, wheel_delta_y, wheel_raw_delta_x, wheel_raw_delta_y));
  218. }
  219. void ConnectionToWindowServer::mouse_up(i32 window_id, Gfx::IntPoint const& mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta_x, i32 wheel_delta_y, i32 wheel_raw_delta_x, i32 wheel_raw_delta_y)
  220. {
  221. if (auto* window = Window::from_window_id(window_id))
  222. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseUp, mouse_position, buttons, to_mouse_button(button), modifiers, wheel_delta_x, wheel_delta_y, wheel_raw_delta_x, wheel_raw_delta_y));
  223. }
  224. void ConnectionToWindowServer::mouse_move(i32 window_id, Gfx::IntPoint const& mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta_x, i32 wheel_delta_y, i32 wheel_raw_delta_x, i32 wheel_raw_delta_y, bool is_drag, Vector<String> const& mime_types)
  225. {
  226. if (auto* window = Window::from_window_id(window_id)) {
  227. if (is_drag)
  228. Core::EventLoop::current().post_event(*window, make<DragEvent>(Event::DragMove, mouse_position, mime_types));
  229. else
  230. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseMove, mouse_position, buttons, to_mouse_button(button), modifiers, wheel_delta_x, wheel_delta_y, wheel_raw_delta_x, wheel_raw_delta_y));
  231. }
  232. }
  233. void ConnectionToWindowServer::mouse_double_click(i32 window_id, Gfx::IntPoint const& mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta_x, i32 wheel_delta_y, i32 wheel_raw_delta_x, i32 wheel_raw_delta_y)
  234. {
  235. if (auto* window = Window::from_window_id(window_id))
  236. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDoubleClick, mouse_position, buttons, to_mouse_button(button), modifiers, wheel_delta_x, wheel_delta_y, wheel_raw_delta_x, wheel_raw_delta_y));
  237. }
  238. void ConnectionToWindowServer::mouse_wheel(i32 window_id, Gfx::IntPoint const& mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta_x, i32 wheel_delta_y, i32 wheel_raw_delta_x, i32 wheel_raw_delta_y)
  239. {
  240. if (auto* window = Window::from_window_id(window_id))
  241. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseWheel, mouse_position, buttons, to_mouse_button(button), modifiers, wheel_delta_x, wheel_delta_y, wheel_raw_delta_x, wheel_raw_delta_y));
  242. }
  243. void ConnectionToWindowServer::menu_visibility_did_change(i32 menu_id, bool visible)
  244. {
  245. auto* menu = Menu::from_menu_id(menu_id);
  246. if (!menu) {
  247. dbgln("EventLoop received visibility change event for invalid menu ID {}", menu_id);
  248. return;
  249. }
  250. menu->visibility_did_change({}, visible);
  251. }
  252. void ConnectionToWindowServer::menu_item_activated(i32 menu_id, u32 identifier)
  253. {
  254. auto* menu = Menu::from_menu_id(menu_id);
  255. if (!menu) {
  256. dbgln("EventLoop received event for invalid menu ID {}", menu_id);
  257. return;
  258. }
  259. if (auto* action = menu->action_at(identifier))
  260. action->activate(menu);
  261. }
  262. void ConnectionToWindowServer::menu_item_entered(i32 menu_id, u32 identifier)
  263. {
  264. auto* menu = Menu::from_menu_id(menu_id);
  265. if (!menu) {
  266. dbgln("ConnectionToWindowServer received MenuItemEntered for invalid menu ID {}", menu_id);
  267. return;
  268. }
  269. auto* action = menu->action_at(identifier);
  270. if (!action)
  271. return;
  272. auto* app = Application::the();
  273. if (!app)
  274. return;
  275. Core::EventLoop::current().post_event(*app, make<ActionEvent>(GUI::Event::ActionEnter, *action));
  276. }
  277. void ConnectionToWindowServer::menu_item_left(i32 menu_id, u32 identifier)
  278. {
  279. auto* menu = Menu::from_menu_id(menu_id);
  280. if (!menu) {
  281. dbgln("ConnectionToWindowServer received MenuItemLeft for invalid menu ID {}", menu_id);
  282. return;
  283. }
  284. auto* action = menu->action_at(identifier);
  285. if (!action)
  286. return;
  287. auto* app = Application::the();
  288. if (!app)
  289. return;
  290. Core::EventLoop::current().post_event(*app, make<ActionEvent>(GUI::Event::ActionLeave, *action));
  291. }
  292. void ConnectionToWindowServer::screen_rects_changed(Vector<Gfx::IntRect> const& rects, u32 main_screen_index, u32 workspace_rows, u32 workspace_columns)
  293. {
  294. Desktop::the().did_receive_screen_rects({}, rects, main_screen_index, workspace_rows, workspace_columns);
  295. Window::for_each_window({}, [&](auto& window) {
  296. Core::EventLoop::current().post_event(window, make<ScreenRectsChangeEvent>(rects, main_screen_index));
  297. });
  298. }
  299. void ConnectionToWindowServer::applet_area_rect_changed(Gfx::IntRect const& rect)
  300. {
  301. Window::for_each_window({}, [&](auto& window) {
  302. Core::EventLoop::current().post_event(window, make<AppletAreaRectChangeEvent>(rect));
  303. });
  304. }
  305. void ConnectionToWindowServer::set_wallpaper_finished(bool)
  306. {
  307. // This is handled manually by Desktop::set_wallpaper().
  308. }
  309. void ConnectionToWindowServer::drag_dropped(i32 window_id, Gfx::IntPoint const& mouse_position, String const& text, HashMap<String, ByteBuffer> const& mime_data)
  310. {
  311. if (auto* window = Window::from_window_id(window_id)) {
  312. auto mime_data_obj = Core::MimeData::construct(mime_data);
  313. Core::EventLoop::current().post_event(*window, make<DropEvent>(mouse_position, text, mime_data_obj));
  314. }
  315. }
  316. void ConnectionToWindowServer::drag_accepted()
  317. {
  318. DragOperation::notify_accepted({});
  319. }
  320. void ConnectionToWindowServer::drag_cancelled()
  321. {
  322. DragOperation::notify_cancelled({});
  323. Application::the()->notify_drag_cancelled({});
  324. }
  325. void ConnectionToWindowServer::window_state_changed(i32 window_id, bool minimized, bool maximized, bool occluded)
  326. {
  327. if (auto* window = Window::from_window_id(window_id))
  328. window->notify_state_changed({}, minimized, maximized, occluded);
  329. }
  330. void ConnectionToWindowServer::display_link_notification()
  331. {
  332. if (m_display_link_notification_pending)
  333. return;
  334. m_display_link_notification_pending = true;
  335. deferred_invoke([this] {
  336. DisplayLink::notify({});
  337. m_display_link_notification_pending = false;
  338. });
  339. }
  340. void ConnectionToWindowServer::track_mouse_move(Gfx::IntPoint const& mouse_position)
  341. {
  342. MouseTracker::track_mouse_move({}, mouse_position);
  343. }
  344. void ConnectionToWindowServer::ping()
  345. {
  346. async_pong();
  347. }
  348. }