ConnectionToWindowServer.cpp 15 KB

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