WindowServerConnection.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/SharedBuffer.h>
  27. #include <AK/StringBuilder.h>
  28. #include <LibCore/EventLoop.h>
  29. #include <LibCore/MimeData.h>
  30. #include <LibGUI/Action.h>
  31. #include <LibGUI/Application.h>
  32. #include <LibGUI/Clipboard.h>
  33. #include <LibGUI/Desktop.h>
  34. #include <LibGUI/DisplayLink.h>
  35. #include <LibGUI/DragOperation.h>
  36. #include <LibGUI/EmojiInputDialog.h>
  37. #include <LibGUI/Event.h>
  38. #include <LibGUI/Menu.h>
  39. #include <LibGUI/Widget.h>
  40. #include <LibGUI/Window.h>
  41. #include <LibGUI/WindowServerConnection.h>
  42. #include <LibGfx/Bitmap.h>
  43. #include <LibGfx/Palette.h>
  44. #include <LibGfx/SystemTheme.h>
  45. //#define KEYBOARD_SHORTCUTS_DEBUG
  46. namespace GUI {
  47. WindowServerConnection& WindowServerConnection::the()
  48. {
  49. static WindowServerConnection* s_connection = nullptr;
  50. if (!s_connection)
  51. s_connection = new WindowServerConnection;
  52. return *s_connection;
  53. }
  54. static void set_system_theme_from_shbuf_id(int id)
  55. {
  56. auto system_theme = SharedBuffer::create_from_shbuf_id(id);
  57. ASSERT(system_theme);
  58. Gfx::set_system_theme(*system_theme);
  59. Application::the()->set_system_palette(*system_theme);
  60. }
  61. void WindowServerConnection::handshake()
  62. {
  63. auto response = send_sync<Messages::WindowServer::Greet>();
  64. set_my_client_id(response->client_id());
  65. set_system_theme_from_shbuf_id(response->system_theme_buffer_id());
  66. Desktop::the().did_receive_screen_rect({}, response->screen_rect());
  67. }
  68. void WindowServerConnection::handle(const Messages::WindowClient::UpdateSystemTheme& message)
  69. {
  70. set_system_theme_from_shbuf_id(message.system_theme_buffer_id());
  71. Window::update_all_windows({});
  72. Window::for_each_window({}, [](auto& window) {
  73. Core::EventLoop::current().post_event(window, make<ThemeChangeEvent>());
  74. });
  75. }
  76. void WindowServerConnection::handle(const Messages::WindowClient::Paint& message)
  77. {
  78. if (auto* window = Window::from_window_id(message.window_id()))
  79. Core::EventLoop::current().post_event(*window, make<MultiPaintEvent>(message.rects(), message.window_size()));
  80. }
  81. void WindowServerConnection::handle(const Messages::WindowClient::WindowResized& message)
  82. {
  83. if (auto* window = Window::from_window_id(message.window_id())) {
  84. Core::EventLoop::current().post_event(*window, make<ResizeEvent>(message.new_rect().size()));
  85. }
  86. }
  87. void WindowServerConnection::handle(const Messages::WindowClient::WindowActivated& message)
  88. {
  89. if (auto* window = Window::from_window_id(message.window_id()))
  90. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowBecameActive));
  91. }
  92. void WindowServerConnection::handle(const Messages::WindowClient::WindowDeactivated& message)
  93. {
  94. if (auto* window = Window::from_window_id(message.window_id()))
  95. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowBecameInactive));
  96. }
  97. void WindowServerConnection::handle(const Messages::WindowClient::WindowInputEntered& message)
  98. {
  99. if (auto* window = Window::from_window_id(message.window_id()))
  100. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowInputEntered));
  101. }
  102. void WindowServerConnection::handle(const Messages::WindowClient::WindowInputLeft& message)
  103. {
  104. if (auto* window = Window::from_window_id(message.window_id()))
  105. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowInputLeft));
  106. }
  107. void WindowServerConnection::handle(const Messages::WindowClient::WindowCloseRequest& message)
  108. {
  109. if (auto* window = Window::from_window_id(message.window_id()))
  110. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowCloseRequest));
  111. }
  112. void WindowServerConnection::handle(const Messages::WindowClient::WindowEntered& message)
  113. {
  114. if (auto* window = Window::from_window_id(message.window_id()))
  115. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowEntered));
  116. }
  117. void WindowServerConnection::handle(const Messages::WindowClient::WindowLeft& message)
  118. {
  119. if (auto* window = Window::from_window_id(message.window_id()))
  120. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowLeft));
  121. }
  122. void WindowServerConnection::handle(const Messages::WindowClient::KeyDown& message)
  123. {
  124. auto* window = Window::from_window_id(message.window_id());
  125. if (!window)
  126. return;
  127. auto key_event = make<KeyEvent>(Event::KeyDown, (KeyCode)message.key(), message.modifiers(), message.code_point(), message.scancode());
  128. Action* action = nullptr;
  129. #ifdef KEYBOARD_SHORTCUTS_DEBUG
  130. dbg() << "Looking up action for " << key_event->to_string();
  131. #endif
  132. if (auto* focused_widget = window->focused_widget()) {
  133. for (auto* widget = focused_widget; widget && !action; widget = widget->parent_widget()) {
  134. action = widget->action_for_key_event(*key_event);
  135. #ifdef KEYBOARD_SHORTCUTS_DEBUG
  136. dbg() << " > Focused widget " << *widget << " gave action: " << action;
  137. #endif
  138. }
  139. }
  140. if (!action) {
  141. action = window->action_for_key_event(*key_event);
  142. #ifdef KEYBOARD_SHORTCUTS_DEBUG
  143. dbg() << " > Asked window " << *window << ", got action: " << action;
  144. #endif
  145. }
  146. // NOTE: Application-global shortcuts are ignored while a modal window is up.
  147. if (!action && !window->is_modal()) {
  148. action = Application::the()->action_for_key_event(*key_event);
  149. #ifdef KEYBOARD_SHORTCUTS_DEBUG
  150. dbg() << " > Asked application, got action: " << action;
  151. #endif
  152. }
  153. if (action && action->is_enabled()) {
  154. action->activate();
  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 && (message.modifiers() == (Mod_Ctrl | Mod_Alt)) && message.key() == Key_Space) {
  159. auto emoji_input_dialog = EmojiInputDialog::construct(window);
  160. if (emoji_input_dialog->exec() != EmojiInputDialog::ExecOK)
  161. return;
  162. key_event->m_key = Key_Invalid;
  163. key_event->m_modifiers = 0;
  164. AK::Utf8View m_utf8_view(emoji_input_dialog->selected_emoji_text().characters());
  165. u32 code_point = *m_utf8_view.begin();
  166. key_event->m_code_point = code_point;
  167. }
  168. Core::EventLoop::current().post_event(*window, move(key_event));
  169. }
  170. void WindowServerConnection::handle(const Messages::WindowClient::KeyUp& message)
  171. {
  172. auto* window = Window::from_window_id(message.window_id());
  173. if (!window)
  174. return;
  175. auto key_event = make<KeyEvent>(Event::KeyUp, (KeyCode)message.key(), message.modifiers(), message.code_point(), message.scancode());
  176. Core::EventLoop::current().post_event(*window, move(key_event));
  177. }
  178. static MouseButton to_gmousebutton(u32 button)
  179. {
  180. switch (button) {
  181. case 0:
  182. return MouseButton::None;
  183. case 1:
  184. return MouseButton::Left;
  185. case 2:
  186. return MouseButton::Right;
  187. case 4:
  188. return MouseButton::Middle;
  189. case 8:
  190. return MouseButton::Back;
  191. case 16:
  192. return MouseButton::Forward;
  193. default:
  194. ASSERT_NOT_REACHED();
  195. break;
  196. }
  197. }
  198. void WindowServerConnection::handle(const Messages::WindowClient::MouseDown& message)
  199. {
  200. if (auto* window = Window::from_window_id(message.window_id()))
  201. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDown, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  202. }
  203. void WindowServerConnection::handle(const Messages::WindowClient::MouseUp& message)
  204. {
  205. if (auto* window = Window::from_window_id(message.window_id()))
  206. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseUp, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  207. }
  208. void WindowServerConnection::handle(const Messages::WindowClient::MouseMove& message)
  209. {
  210. if (auto* window = Window::from_window_id(message.window_id())) {
  211. if (message.is_drag())
  212. Core::EventLoop::current().post_event(*window, make<DragEvent>(Event::DragMove, message.mouse_position(), message.drag_data_type()));
  213. else
  214. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseMove, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  215. }
  216. }
  217. void WindowServerConnection::handle(const Messages::WindowClient::MouseDoubleClick& message)
  218. {
  219. if (auto* window = Window::from_window_id(message.window_id()))
  220. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDoubleClick, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  221. }
  222. void WindowServerConnection::handle(const Messages::WindowClient::MouseWheel& message)
  223. {
  224. if (auto* window = Window::from_window_id(message.window_id()))
  225. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseWheel, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  226. }
  227. void WindowServerConnection::handle(const Messages::WindowClient::MenuItemActivated& message)
  228. {
  229. auto* menu = Menu::from_menu_id(message.menu_id());
  230. if (!menu) {
  231. dbgprintf("EventLoop received event for invalid menu ID %d\n", message.menu_id());
  232. return;
  233. }
  234. if (auto* action = menu->action_at(message.identifier()))
  235. action->activate(menu);
  236. }
  237. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowStateChanged& message)
  238. {
  239. if (auto* window = Window::from_window_id(message.wm_id()))
  240. Core::EventLoop::current().post_event(*window, make<WMWindowStateChangedEvent>(message.client_id(), message.window_id(), message.parent_client_id(), message.parent_window_id(), message.title(), message.rect(), message.is_active(), message.is_modal(), static_cast<WindowType>(message.window_type()), message.is_minimized(), message.is_frameless(), message.progress()));
  241. }
  242. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowRectChanged& message)
  243. {
  244. if (auto* window = Window::from_window_id(message.wm_id()))
  245. Core::EventLoop::current().post_event(*window, make<WMWindowRectChangedEvent>(message.client_id(), message.window_id(), message.rect()));
  246. }
  247. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowIconBitmapChanged& message)
  248. {
  249. if (auto* window = Window::from_window_id(message.wm_id()))
  250. Core::EventLoop::current().post_event(*window, make<WMWindowIconBitmapChangedEvent>(message.client_id(), message.window_id(), message.icon_buffer_id(), message.icon_size()));
  251. }
  252. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowRemoved& message)
  253. {
  254. if (auto* window = Window::from_window_id(message.wm_id()))
  255. Core::EventLoop::current().post_event(*window, make<WMWindowRemovedEvent>(message.client_id(), message.window_id()));
  256. }
  257. void WindowServerConnection::handle(const Messages::WindowClient::ScreenRectChanged& message)
  258. {
  259. Desktop::the().did_receive_screen_rect({}, message.rect());
  260. }
  261. void WindowServerConnection::handle(const Messages::WindowClient::AsyncSetWallpaperFinished&)
  262. {
  263. // This is handled manually by Desktop::set_wallpaper().
  264. }
  265. void WindowServerConnection::handle(const Messages::WindowClient::DragDropped& message)
  266. {
  267. if (auto* window = Window::from_window_id(message.window_id())) {
  268. auto mime_data = Core::MimeData::construct(message.mime_data());
  269. Core::EventLoop::current().post_event(*window, make<DropEvent>(message.mouse_position(), message.text(), mime_data));
  270. }
  271. }
  272. void WindowServerConnection::handle(const Messages::WindowClient::DragAccepted&)
  273. {
  274. DragOperation::notify_accepted({});
  275. }
  276. void WindowServerConnection::handle(const Messages::WindowClient::DragCancelled&)
  277. {
  278. DragOperation::notify_cancelled({});
  279. }
  280. void WindowServerConnection::handle(const Messages::WindowClient::WindowStateChanged& message)
  281. {
  282. if (auto* window = Window::from_window_id(message.window_id()))
  283. window->notify_state_changed({}, message.minimized(), message.occluded());
  284. }
  285. void WindowServerConnection::handle(const Messages::WindowClient::DisplayLinkNotification&)
  286. {
  287. if (m_display_link_notification_pending)
  288. return;
  289. m_display_link_notification_pending = true;
  290. deferred_invoke([this](auto&) {
  291. DisplayLink::notify({});
  292. m_display_link_notification_pending = false;
  293. });
  294. }
  295. void WindowServerConnection::handle(const Messages::WindowClient::Ping&)
  296. {
  297. post_message(Messages::WindowServer::Pong());
  298. }
  299. }