WindowServerConnection.cpp 14 KB

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