ConnectionToWindowServer.cpp 17 KB

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