GWindowServerConnection.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include <LibGUI/GAction.h>
  2. #include <LibGUI/GApplication.h>
  3. #include <LibGUI/GClipboard.h>
  4. #include <LibGUI/GDesktop.h>
  5. #include <LibGUI/GEvent.h>
  6. #include <LibGUI/GMenu.h>
  7. #include <LibGUI/GWidget.h>
  8. #include <LibGUI/GWindow.h>
  9. #include <LibGUI/GWindowServerConnection.h>
  10. //#define GEVENTLOOP_DEBUG
  11. GWindowServerConnection& GWindowServerConnection::the()
  12. {
  13. static GWindowServerConnection* s_connection = nullptr;
  14. if (!s_connection)
  15. s_connection = new GWindowServerConnection;
  16. return *s_connection;
  17. }
  18. void GWindowServerConnection::handshake()
  19. {
  20. auto response = send_sync<WindowServer::Greet>();
  21. set_my_client_id(response->client_id());
  22. GDesktop::the().did_receive_screen_rect({}, response->screen_rect());
  23. }
  24. void GWindowServerConnection::handle(const WindowClient::Paint& message)
  25. {
  26. #ifdef GEVENTLOOP_DEBUG
  27. dbgprintf("WID=%d Paint\n", message.window_id());
  28. #endif
  29. if (auto* window = GWindow::from_window_id(message.window_id())) {
  30. Vector<Rect, 32> rects;
  31. for (auto& r : message.rects()) {
  32. rects.append(r);
  33. }
  34. CEventLoop::current().post_event(*window, make<GMultiPaintEvent>(rects, message.window_size()));
  35. }
  36. }
  37. void GWindowServerConnection::handle(const WindowClient::WindowResized& message)
  38. {
  39. if (auto* window = GWindow::from_window_id(message.window_id())) {
  40. CEventLoop::current().post_event(*window, make<GResizeEvent>(message.old_rect().size(), message.new_rect().size()));
  41. }
  42. }
  43. void GWindowServerConnection::handle(const WindowClient::WindowActivated& message)
  44. {
  45. #ifdef GEVENTLOOP_DEBUG
  46. dbgprintf("(%d) WID=%d WindowActivated\n", getpid(), message.window_id());
  47. #endif
  48. if (auto* window = GWindow::from_window_id(message.window_id()))
  49. CEventLoop::current().post_event(*window, make<GEvent>(GEvent::WindowBecameActive));
  50. }
  51. void GWindowServerConnection::handle(const WindowClient::WindowDeactivated& message)
  52. {
  53. #ifdef GEVENTLOOP_DEBUG
  54. dbgprintf("(%d) WID=%d WindowDeactivated\n", getpid(), message.window_id());
  55. #endif
  56. if (auto* window = GWindow::from_window_id(message.window_id()))
  57. CEventLoop::current().post_event(*window, make<GEvent>(GEvent::WindowBecameInactive));
  58. }
  59. void GWindowServerConnection::handle(const WindowClient::WindowCloseRequest& message)
  60. {
  61. if (auto* window = GWindow::from_window_id(message.window_id()))
  62. CEventLoop::current().post_event(*window, make<GEvent>(GEvent::WindowCloseRequest));
  63. }
  64. void GWindowServerConnection::handle(const WindowClient::WindowEntered& message)
  65. {
  66. if (auto* window = GWindow::from_window_id(message.window_id()))
  67. CEventLoop::current().post_event(*window, make<GEvent>(GEvent::WindowEntered));
  68. }
  69. void GWindowServerConnection::handle(const WindowClient::WindowLeft& message)
  70. {
  71. if (auto* window = GWindow::from_window_id(message.window_id()))
  72. CEventLoop::current().post_event(*window, make<GEvent>(GEvent::WindowLeft));
  73. }
  74. void GWindowServerConnection::handle(const WindowClient::KeyDown& message)
  75. {
  76. #ifdef GEVENTLOOP_DEBUG
  77. dbgprintf("WID=%d KeyDown character=0x%02x\n", message.window_id(), message.character());
  78. #endif
  79. auto* window = GWindow::from_window_id(message.window_id());
  80. if (!window)
  81. return;
  82. auto key_event = make<GKeyEvent>(GEvent::KeyDown, message.key(), message.modifiers());
  83. if (message.character() != '\0') {
  84. char ch = message.character();
  85. key_event->m_text = String(&ch, 1);
  86. }
  87. if (auto* focused_widget = window->focused_widget()) {
  88. if (auto* action = focused_widget->action_for_key_event(*key_event)) {
  89. if (action->is_enabled()) {
  90. action->activate();
  91. return;
  92. }
  93. }
  94. }
  95. if (auto* action = GApplication::the().action_for_key_event(*key_event)) {
  96. if (action->is_enabled()) {
  97. action->activate();
  98. return;
  99. }
  100. }
  101. CEventLoop::current().post_event(*window, move(key_event));
  102. }
  103. void GWindowServerConnection::handle(const WindowClient::KeyUp& message)
  104. {
  105. #ifdef GEVENTLOOP_DEBUG
  106. dbgprintf("WID=%d KeyUp character=0x%02x\n", message.window_id(), message.character());
  107. #endif
  108. auto* window = GWindow::from_window_id(message.window_id());
  109. if (!window)
  110. return;
  111. auto key_event = make<GKeyEvent>(GEvent::KeyUp, message.key(), message.modifiers());
  112. if (message.character() != '\0') {
  113. char ch = message.character();
  114. key_event->m_text = String(&ch, 1);
  115. }
  116. CEventLoop::current().post_event(*window, move(key_event));
  117. }
  118. GMouseButton to_gmousebutton(u32 button)
  119. {
  120. switch (button) {
  121. case 0:
  122. return GMouseButton::None;
  123. case 1:
  124. return GMouseButton::Left;
  125. case 2:
  126. return GMouseButton::Right;
  127. case 4:
  128. return GMouseButton::Middle;
  129. default:
  130. ASSERT_NOT_REACHED();
  131. break;
  132. }
  133. }
  134. void GWindowServerConnection::handle(const WindowClient::MouseDown& message)
  135. {
  136. #ifdef GEVENTLOOP_DEBUG
  137. dbgprintf("WID=%d MouseDown %d,%d,%d\n", message.window_id(), message.mouse_position().x(), message.mouse_position().y(), message.wheel_delta();
  138. #endif
  139. if (auto* window = GWindow::from_window_id(message.window_id()))
  140. CEventLoop::current().post_event(*window, make<GMouseEvent>(GEvent::MouseDown, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  141. }
  142. void GWindowServerConnection::handle(const WindowClient::MouseUp& message)
  143. {
  144. #ifdef GEVENTLOOP_DEBUG
  145. dbgprintf("WID=%d MouseUp %d,%d,%d\n", message.window_id(), message.mouse_position().x(), message.mouse_position().y(), message.wheel_delta();
  146. #endif
  147. if (auto* window = GWindow::from_window_id(message.window_id()))
  148. CEventLoop::current().post_event(*window, make<GMouseEvent>(GEvent::MouseUp, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  149. }
  150. void GWindowServerConnection::handle(const WindowClient::MouseMove& message)
  151. {
  152. #ifdef GEVENTLOOP_DEBUG
  153. dbgprintf("WID=%d MouseMove %d,%d,%d\n", message.window_id(), message.mouse_position().x(), message.mouse_position().y(), message.wheel_delta();
  154. #endif
  155. if (auto* window = GWindow::from_window_id(message.window_id()))
  156. CEventLoop::current().post_event(*window, make<GMouseEvent>(GEvent::MouseMove, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  157. }
  158. void GWindowServerConnection::handle(const WindowClient::MouseDoubleClick& message)
  159. {
  160. #ifdef GEVENTLOOP_DEBUG
  161. dbgprintf("WID=%d MouseDoubleClick %d,%d,%d\n", message.window_id(), message.mouse_position().x(), message.mouse_position().y(), message.wheel_delta();
  162. #endif
  163. if (auto* window = GWindow::from_window_id(message.window_id()))
  164. CEventLoop::current().post_event(*window, make<GMouseEvent>(GEvent::MouseDoubleClick, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  165. }
  166. void GWindowServerConnection::handle(const WindowClient::MouseWheel& message)
  167. {
  168. #ifdef GEVENTLOOP_DEBUG
  169. dbgprintf("WID=%d MouseWheel %d,%d,%d\n", message.window_id(), message.mouse_position().x(), message.mouse_position().y(), message.wheel_delta();
  170. #endif
  171. if (auto* window = GWindow::from_window_id(message.window_id()))
  172. CEventLoop::current().post_event(*window, make<GMouseEvent>(GEvent::MouseWheel, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  173. }
  174. void GWindowServerConnection::handle(const WindowClient::MenuItemActivated& message)
  175. {
  176. auto* menu = GMenu::from_menu_id(message.menu_id());
  177. if (!menu) {
  178. dbgprintf("GEventLoop received event for invalid menu ID %d\n", message.menu_id());
  179. return;
  180. }
  181. if (auto* action = menu->action_at(message.identifier()))
  182. action->activate();
  183. }
  184. void GWindowServerConnection::handle(const WindowClient::WM_WindowStateChanged& message)
  185. {
  186. #ifdef GEVENTLOOP_DEBUG
  187. dbgprintf("GEventLoop: handle_wm_event: %d\n", (int)event.type);
  188. #endif
  189. if (auto* window = GWindow::from_window_id(message.window_id()))
  190. 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()));
  191. }
  192. void GWindowServerConnection::handle(const WindowClient::WM_WindowRectChanged& message)
  193. {
  194. #ifdef GEVENTLOOP_DEBUG
  195. dbgprintf("GEventLoop: handle_wm_event: %d\n", (int)event.type);
  196. #endif
  197. if (auto* window = GWindow::from_window_id(message.window_id()))
  198. CEventLoop::current().post_event(*window, make<GWMWindowRectChangedEvent>(message.client_id(), message.window_id(), message.rect()));
  199. }
  200. void GWindowServerConnection::handle(const WindowClient::WM_WindowIconBitmapChanged& message)
  201. {
  202. #ifdef GEVENTLOOP_DEBUG
  203. dbgprintf("GEventLoop: handle_wm_event: %d\n", (int)event.type);
  204. #endif
  205. if (auto* window = GWindow::from_window_id(message.window_id()))
  206. CEventLoop::current().post_event(*window, make<GWMWindowIconBitmapChangedEvent>(message.client_id(), message.window_id(), message.icon_buffer_id(), message.icon_size()));
  207. }
  208. void GWindowServerConnection::handle(const WindowClient::WM_WindowRemoved& message)
  209. {
  210. #ifdef GEVENTLOOP_DEBUG
  211. dbgprintf("GEventLoop: handle_wm_event: %d\n", (int)event.type);
  212. #endif
  213. if (auto* window = GWindow::from_window_id(message.window_id()))
  214. CEventLoop::current().post_event(*window, make<GWMWindowRemovedEvent>(message.client_id(), message.window_id()));
  215. }
  216. void GWindowServerConnection::handle(const WindowClient::ScreenRectChanged& message)
  217. {
  218. GDesktop::the().did_receive_screen_rect({}, message.rect());
  219. }
  220. void GWindowServerConnection::handle(const WindowClient::ClipboardContentsChanged& message)
  221. {
  222. GClipboard::the().did_receive_clipboard_contents_changed({}, message.content_type());
  223. }
  224. void GWindowServerConnection::handle(const WindowClient::AsyncSetWallpaperFinished&)
  225. {
  226. // This is handled manually by GDesktop::set_wallpaper().
  227. }