WindowServerConnection.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 <LibCore/EventLoop.h>
  28. #include <LibCore/MimeData.h>
  29. #include <LibGUI/Action.h>
  30. #include <LibGUI/Application.h>
  31. #include <LibGUI/Clipboard.h>
  32. #include <LibGUI/Desktop.h>
  33. #include <LibGUI/DisplayLink.h>
  34. #include <LibGUI/DragOperation.h>
  35. #include <LibGUI/Event.h>
  36. #include <LibGUI/Menu.h>
  37. #include <LibGUI/Widget.h>
  38. #include <LibGUI/Window.h>
  39. #include <LibGUI/WindowServerConnection.h>
  40. #include <LibGfx/Bitmap.h>
  41. #include <LibGfx/Palette.h>
  42. #include <LibGfx/SystemTheme.h>
  43. namespace GUI {
  44. WindowServerConnection& WindowServerConnection::the()
  45. {
  46. static WindowServerConnection* s_connection = nullptr;
  47. if (!s_connection)
  48. s_connection = new WindowServerConnection;
  49. return *s_connection;
  50. }
  51. static void set_system_theme_from_shbuf_id(int id)
  52. {
  53. auto system_theme = SharedBuffer::create_from_shbuf_id(id);
  54. ASSERT(system_theme);
  55. Gfx::set_system_theme(*system_theme);
  56. Application::the().set_system_palette(*system_theme);
  57. }
  58. void WindowServerConnection::handshake()
  59. {
  60. auto response = send_sync<Messages::WindowServer::Greet>();
  61. set_my_client_id(response->client_id());
  62. set_system_theme_from_shbuf_id(response->system_theme_buffer_id());
  63. Desktop::the().did_receive_screen_rect({}, response->screen_rect());
  64. }
  65. void WindowServerConnection::handle(const Messages::WindowClient::UpdateSystemTheme& message)
  66. {
  67. set_system_theme_from_shbuf_id(message.system_theme_buffer_id());
  68. Window::update_all_windows({});
  69. Window::for_each_window({}, [](auto& window) {
  70. Core::EventLoop::current().post_event(window, make<ThemeChangeEvent>());
  71. });
  72. }
  73. void WindowServerConnection::handle(const Messages::WindowClient::Paint& message)
  74. {
  75. if (auto* window = Window::from_window_id(message.window_id()))
  76. Core::EventLoop::current().post_event(*window, make<MultiPaintEvent>(message.rects(), message.window_size()));
  77. }
  78. void WindowServerConnection::handle(const Messages::WindowClient::WindowResized& message)
  79. {
  80. if (auto* window = Window::from_window_id(message.window_id())) {
  81. Core::EventLoop::current().post_event(*window, make<ResizeEvent>(message.old_rect().size(), message.new_rect().size()));
  82. }
  83. }
  84. void WindowServerConnection::handle(const Messages::WindowClient::WindowActivated& message)
  85. {
  86. if (auto* window = Window::from_window_id(message.window_id()))
  87. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowBecameActive));
  88. }
  89. void WindowServerConnection::handle(const Messages::WindowClient::WindowDeactivated& message)
  90. {
  91. if (auto* window = Window::from_window_id(message.window_id()))
  92. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowBecameInactive));
  93. }
  94. void WindowServerConnection::handle(const Messages::WindowClient::WindowCloseRequest& message)
  95. {
  96. if (auto* window = Window::from_window_id(message.window_id()))
  97. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowCloseRequest));
  98. }
  99. void WindowServerConnection::handle(const Messages::WindowClient::WindowEntered& message)
  100. {
  101. if (auto* window = Window::from_window_id(message.window_id()))
  102. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowEntered));
  103. }
  104. void WindowServerConnection::handle(const Messages::WindowClient::WindowLeft& message)
  105. {
  106. if (auto* window = Window::from_window_id(message.window_id()))
  107. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowLeft));
  108. }
  109. void WindowServerConnection::handle(const Messages::WindowClient::KeyDown& message)
  110. {
  111. auto* window = Window::from_window_id(message.window_id());
  112. if (!window)
  113. return;
  114. auto key_event = make<KeyEvent>(Event::KeyDown, message.key(), message.modifiers());
  115. if (message.character() != '\0') {
  116. char ch = message.character();
  117. key_event->m_text = String(&ch, 1);
  118. }
  119. Action* action = nullptr;
  120. if (auto* focused_widget = window->focused_widget()) {
  121. for (auto* widget = focused_widget; widget && !action; widget = widget->parent_widget())
  122. action = focused_widget->action_for_key_event(*key_event);
  123. }
  124. if (!action)
  125. action = window->action_for_key_event(*key_event);
  126. if (!action)
  127. action = Application::the().action_for_key_event(*key_event);
  128. if (action && action->is_enabled()) {
  129. action->activate();
  130. return;
  131. }
  132. Core::EventLoop::current().post_event(*window, move(key_event));
  133. }
  134. void WindowServerConnection::handle(const Messages::WindowClient::KeyUp& message)
  135. {
  136. auto* window = Window::from_window_id(message.window_id());
  137. if (!window)
  138. return;
  139. auto key_event = make<KeyEvent>(Event::KeyUp, message.key(), message.modifiers());
  140. if (message.character() != '\0') {
  141. char ch = message.character();
  142. key_event->m_text = String(&ch, 1);
  143. }
  144. Core::EventLoop::current().post_event(*window, move(key_event));
  145. }
  146. MouseButton to_gmousebutton(u32 button)
  147. {
  148. switch (button) {
  149. case 0:
  150. return MouseButton::None;
  151. case 1:
  152. return MouseButton::Left;
  153. case 2:
  154. return MouseButton::Right;
  155. case 4:
  156. return MouseButton::Middle;
  157. default:
  158. ASSERT_NOT_REACHED();
  159. break;
  160. }
  161. }
  162. void WindowServerConnection::handle(const Messages::WindowClient::MouseDown& message)
  163. {
  164. if (auto* window = Window::from_window_id(message.window_id()))
  165. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDown, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  166. }
  167. void WindowServerConnection::handle(const Messages::WindowClient::MouseUp& message)
  168. {
  169. if (auto* window = Window::from_window_id(message.window_id()))
  170. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseUp, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  171. }
  172. void WindowServerConnection::handle(const Messages::WindowClient::MouseMove& message)
  173. {
  174. if (auto* window = Window::from_window_id(message.window_id())) {
  175. if (message.is_drag())
  176. Core::EventLoop::current().post_event(*window, make<DragEvent>(Event::DragMove, message.mouse_position(), message.drag_data_type()));
  177. else
  178. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseMove, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  179. }
  180. }
  181. void WindowServerConnection::handle(const Messages::WindowClient::MouseDoubleClick& message)
  182. {
  183. if (auto* window = Window::from_window_id(message.window_id()))
  184. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDoubleClick, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  185. }
  186. void WindowServerConnection::handle(const Messages::WindowClient::MouseWheel& message)
  187. {
  188. if (auto* window = Window::from_window_id(message.window_id()))
  189. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseWheel, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  190. }
  191. void WindowServerConnection::handle(const Messages::WindowClient::MenuItemActivated& message)
  192. {
  193. auto* menu = Menu::from_menu_id(message.menu_id());
  194. if (!menu) {
  195. dbgprintf("EventLoop received event for invalid menu ID %d\n", message.menu_id());
  196. return;
  197. }
  198. if (auto* action = menu->action_at(message.identifier()))
  199. action->activate(menu);
  200. }
  201. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowStateChanged& message)
  202. {
  203. if (auto* window = Window::from_window_id(message.wm_id()))
  204. Core::EventLoop::current().post_event(*window, make<WMWindowStateChangedEvent>(message.client_id(), message.window_id(), message.title(), message.rect(), message.is_active(), static_cast<WindowType>(message.window_type()), message.is_minimized(), message.is_frameless()));
  205. }
  206. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowRectChanged& message)
  207. {
  208. if (auto* window = Window::from_window_id(message.wm_id()))
  209. Core::EventLoop::current().post_event(*window, make<WMWindowRectChangedEvent>(message.client_id(), message.window_id(), message.rect()));
  210. }
  211. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowIconBitmapChanged& message)
  212. {
  213. if (auto* window = Window::from_window_id(message.wm_id()))
  214. Core::EventLoop::current().post_event(*window, make<WMWindowIconBitmapChangedEvent>(message.client_id(), message.window_id(), message.icon_buffer_id(), message.icon_size()));
  215. }
  216. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowRemoved& message)
  217. {
  218. if (auto* window = Window::from_window_id(message.wm_id()))
  219. Core::EventLoop::current().post_event(*window, make<WMWindowRemovedEvent>(message.client_id(), message.window_id()));
  220. }
  221. void WindowServerConnection::handle(const Messages::WindowClient::ScreenRectChanged& message)
  222. {
  223. Desktop::the().did_receive_screen_rect({}, message.rect());
  224. }
  225. void WindowServerConnection::handle(const Messages::WindowClient::ClipboardContentsChanged& message)
  226. {
  227. Clipboard::the().did_receive_clipboard_contents_changed({}, message.content_type());
  228. }
  229. void WindowServerConnection::handle(const Messages::WindowClient::AsyncSetWallpaperFinished&)
  230. {
  231. // This is handled manually by Desktop::set_wallpaper().
  232. }
  233. void WindowServerConnection::handle(const Messages::WindowClient::DragDropped& message)
  234. {
  235. if (auto* window = Window::from_window_id(message.window_id())) {
  236. auto mime_data = Core::MimeData::construct();
  237. mime_data->set_data(message.data_type(), message.data().to_byte_buffer());
  238. Core::EventLoop::current().post_event(*window, make<DropEvent>(message.mouse_position(), message.text(), mime_data));
  239. }
  240. }
  241. void WindowServerConnection::handle(const Messages::WindowClient::DragAccepted&)
  242. {
  243. DragOperation::notify_accepted({});
  244. }
  245. void WindowServerConnection::handle(const Messages::WindowClient::DragCancelled&)
  246. {
  247. DragOperation::notify_cancelled({});
  248. }
  249. void WindowServerConnection::handle(const Messages::WindowClient::WindowStateChanged& message)
  250. {
  251. if (auto* window = Window::from_window_id(message.window_id()))
  252. window->notify_state_changed({}, message.minimized(), message.occluded());
  253. }
  254. void WindowServerConnection::handle(const Messages::WindowClient::DisplayLinkNotification&)
  255. {
  256. if (m_display_link_notification_pending)
  257. return;
  258. m_display_link_notification_pending = true;
  259. deferred_invoke([this](auto&) {
  260. DisplayLink::notify({});
  261. m_display_link_notification_pending = false;
  262. });
  263. }
  264. }