GWindowServerConnection.cpp 13 KB

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