WindowServerConnection.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. case 8:
  158. return MouseButton::Back;
  159. case 16:
  160. return MouseButton::Forward;
  161. default:
  162. ASSERT_NOT_REACHED();
  163. break;
  164. }
  165. }
  166. void WindowServerConnection::handle(const Messages::WindowClient::MouseDown& message)
  167. {
  168. if (auto* window = Window::from_window_id(message.window_id()))
  169. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDown, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  170. }
  171. void WindowServerConnection::handle(const Messages::WindowClient::MouseUp& message)
  172. {
  173. if (auto* window = Window::from_window_id(message.window_id()))
  174. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseUp, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  175. }
  176. void WindowServerConnection::handle(const Messages::WindowClient::MouseMove& message)
  177. {
  178. if (auto* window = Window::from_window_id(message.window_id())) {
  179. if (message.is_drag())
  180. Core::EventLoop::current().post_event(*window, make<DragEvent>(Event::DragMove, message.mouse_position(), message.drag_data_type()));
  181. else
  182. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseMove, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  183. }
  184. }
  185. void WindowServerConnection::handle(const Messages::WindowClient::MouseDoubleClick& message)
  186. {
  187. if (auto* window = Window::from_window_id(message.window_id()))
  188. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDoubleClick, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  189. }
  190. void WindowServerConnection::handle(const Messages::WindowClient::MouseWheel& message)
  191. {
  192. if (auto* window = Window::from_window_id(message.window_id()))
  193. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseWheel, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  194. }
  195. void WindowServerConnection::handle(const Messages::WindowClient::MenuItemActivated& message)
  196. {
  197. auto* menu = Menu::from_menu_id(message.menu_id());
  198. if (!menu) {
  199. dbgprintf("EventLoop received event for invalid menu ID %d\n", message.menu_id());
  200. return;
  201. }
  202. if (auto* action = menu->action_at(message.identifier()))
  203. action->activate(menu);
  204. }
  205. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowStateChanged& message)
  206. {
  207. if (auto* window = Window::from_window_id(message.wm_id()))
  208. 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()));
  209. }
  210. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowRectChanged& message)
  211. {
  212. if (auto* window = Window::from_window_id(message.wm_id()))
  213. Core::EventLoop::current().post_event(*window, make<WMWindowRectChangedEvent>(message.client_id(), message.window_id(), message.rect()));
  214. }
  215. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowIconBitmapChanged& message)
  216. {
  217. if (auto* window = Window::from_window_id(message.wm_id()))
  218. Core::EventLoop::current().post_event(*window, make<WMWindowIconBitmapChangedEvent>(message.client_id(), message.window_id(), message.icon_buffer_id(), message.icon_size()));
  219. }
  220. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowRemoved& message)
  221. {
  222. if (auto* window = Window::from_window_id(message.wm_id()))
  223. Core::EventLoop::current().post_event(*window, make<WMWindowRemovedEvent>(message.client_id(), message.window_id()));
  224. }
  225. void WindowServerConnection::handle(const Messages::WindowClient::ScreenRectChanged& message)
  226. {
  227. Desktop::the().did_receive_screen_rect({}, message.rect());
  228. }
  229. void WindowServerConnection::handle(const Messages::WindowClient::ClipboardContentsChanged& message)
  230. {
  231. Clipboard::the().did_receive_clipboard_contents_changed({}, message.content_type());
  232. }
  233. void WindowServerConnection::handle(const Messages::WindowClient::AsyncSetWallpaperFinished&)
  234. {
  235. // This is handled manually by Desktop::set_wallpaper().
  236. }
  237. void WindowServerConnection::handle(const Messages::WindowClient::DragDropped& message)
  238. {
  239. if (auto* window = Window::from_window_id(message.window_id())) {
  240. auto mime_data = Core::MimeData::construct();
  241. mime_data->set_data(message.data_type(), message.data().to_byte_buffer());
  242. Core::EventLoop::current().post_event(*window, make<DropEvent>(message.mouse_position(), message.text(), mime_data));
  243. }
  244. }
  245. void WindowServerConnection::handle(const Messages::WindowClient::DragAccepted&)
  246. {
  247. DragOperation::notify_accepted({});
  248. }
  249. void WindowServerConnection::handle(const Messages::WindowClient::DragCancelled&)
  250. {
  251. DragOperation::notify_cancelled({});
  252. }
  253. void WindowServerConnection::handle(const Messages::WindowClient::WindowStateChanged& message)
  254. {
  255. if (auto* window = Window::from_window_id(message.window_id()))
  256. window->notify_state_changed({}, message.minimized(), message.occluded());
  257. }
  258. void WindowServerConnection::handle(const Messages::WindowClient::DisplayLinkNotification&)
  259. {
  260. if (m_display_link_notification_pending)
  261. return;
  262. m_display_link_notification_pending = true;
  263. deferred_invoke([this](auto&) {
  264. DisplayLink::notify({});
  265. m_display_link_notification_pending = false;
  266. });
  267. }
  268. }