GWindowServerConnection.cpp 13 KB

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