WindowServerConnection.cpp 14 KB

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