GWindowServerConnection.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include <LibC/errno.h>
  2. #include <LibC/fcntl.h>
  3. #include <LibC/stdio.h>
  4. #include <LibC/stdlib.h>
  5. #include <LibC/string.h>
  6. #include <LibC/sys/select.h>
  7. #include <LibC/sys/socket.h>
  8. #include <LibC/sys/time.h>
  9. #include <LibC/time.h>
  10. #include <LibC/unistd.h>
  11. #include <LibCore/CNotifier.h>
  12. #include <LibCore/CObject.h>
  13. #include <LibGUI/GAction.h>
  14. #include <LibGUI/GApplication.h>
  15. #include <LibGUI/GClipboard.h>
  16. #include <LibGUI/GDesktop.h>
  17. #include <LibGUI/GEvent.h>
  18. #include <LibGUI/GMenu.h>
  19. #include <LibGUI/GWidget.h>
  20. #include <LibGUI/GWindow.h>
  21. #include <LibGUI/GWindowServerConnection.h>
  22. #include <sys/uio.h>
  23. //#define GEVENTLOOP_DEBUG
  24. //#define COALESCING_DEBUG
  25. GWindowServerConnection& GWindowServerConnection::the()
  26. {
  27. static GWindowServerConnection* s_connection = nullptr;
  28. if (!s_connection) {
  29. s_connection = new GWindowServerConnection();
  30. s_connection->handshake();
  31. }
  32. return *s_connection;
  33. }
  34. void GWindowServerConnection::handshake()
  35. {
  36. WSAPI_ClientMessage request;
  37. request.type = WSAPI_ClientMessage::Type::Greeting;
  38. request.greeting.client_pid = getpid();
  39. auto response = sync_request(request, WSAPI_ServerMessage::Type::Greeting);
  40. handle_greeting(response);
  41. }
  42. void GWindowServerConnection::handle_paint_event(const WSAPI_ServerMessage& event, GWindow& window, const ByteBuffer& extra_data)
  43. {
  44. #ifdef GEVENTLOOP_DEBUG
  45. dbgprintf("WID=%x Paint\n", event.window_id);
  46. #endif
  47. Vector<Rect, 32> rects;
  48. for (int i = 0; i < min(WSAPI_ServerMessage::max_inline_rect_count, event.rect_count); ++i)
  49. rects.append(event.rects[i]);
  50. if (event.extra_size) {
  51. auto* extra_rects = reinterpret_cast<const WSAPI_Rect*>(extra_data.data());
  52. for (int i = 0; i < event.rect_count - WSAPI_ServerMessage::max_inline_rect_count; ++i)
  53. rects.append(extra_rects[i]);
  54. }
  55. CEventLoop::current().post_event(window, make<GMultiPaintEvent>(rects, event.paint.window_size));
  56. }
  57. void GWindowServerConnection::handle_resize_event(const WSAPI_ServerMessage& event, GWindow& window)
  58. {
  59. CEventLoop::current().post_event(window, make<GResizeEvent>(event.window.old_rect.size, event.window.rect.size));
  60. }
  61. void GWindowServerConnection::handle_window_activation_event(const WSAPI_ServerMessage& event, GWindow& window)
  62. {
  63. #ifdef GEVENTLOOP_DEBUG
  64. dbgprintf("WID=%x WindowActivation\n", event.window_id);
  65. #endif
  66. CEventLoop::current().post_event(window, make<GEvent>(event.type == WSAPI_ServerMessage::Type::WindowActivated ? GEvent::WindowBecameActive : GEvent::WindowBecameInactive));
  67. }
  68. void GWindowServerConnection::handle_window_close_request_event(const WSAPI_ServerMessage&, GWindow& window)
  69. {
  70. CEventLoop::current().post_event(window, make<GEvent>(GEvent::WindowCloseRequest));
  71. }
  72. void GWindowServerConnection::handle_window_entered_or_left_event(const WSAPI_ServerMessage& message, GWindow& window)
  73. {
  74. CEventLoop::current().post_event(window, make<GEvent>(message.type == WSAPI_ServerMessage::Type::WindowEntered ? GEvent::WindowEntered : GEvent::WindowLeft));
  75. }
  76. void GWindowServerConnection::handle_key_event(const WSAPI_ServerMessage& event, GWindow& window)
  77. {
  78. #ifdef GEVENTLOOP_DEBUG
  79. dbgprintf("WID=%x KeyEvent character=0x%b\n", event.window_id, event.key.character);
  80. #endif
  81. auto key_event = make<GKeyEvent>(event.type == WSAPI_ServerMessage::Type::KeyDown ? GEvent::KeyDown : GEvent::KeyUp, event.key.key, event.key.modifiers);
  82. if (event.key.character != '\0')
  83. key_event->m_text = String(&event.key.character, 1);
  84. if (event.type == WSAPI_ServerMessage::Type::KeyDown) {
  85. if (auto* focused_widget = window.focused_widget()) {
  86. if (auto* action = focused_widget->action_for_key_event(*key_event)) {
  87. if (action->is_enabled()) {
  88. action->activate();
  89. return;
  90. }
  91. }
  92. }
  93. if (auto* action = GApplication::the().action_for_key_event(*key_event)) {
  94. if (action->is_enabled()) {
  95. action->activate();
  96. return;
  97. }
  98. }
  99. }
  100. CEventLoop::current().post_event(window, move(key_event));
  101. }
  102. void GWindowServerConnection::handle_mouse_event(const WSAPI_ServerMessage& event, GWindow& window)
  103. {
  104. #ifdef GEVENTLOOP_DEBUG
  105. dbgprintf("WID=%x MouseEvent %d,%d,%d\n", event.window_id, event.mouse.position.x, event.mouse.position.y, event.mouse.wheel_delta);
  106. #endif
  107. GMouseEvent::Type type;
  108. switch (event.type) {
  109. case WSAPI_ServerMessage::Type::MouseMove:
  110. type = GEvent::MouseMove;
  111. break;
  112. case WSAPI_ServerMessage::Type::MouseUp:
  113. type = GEvent::MouseUp;
  114. break;
  115. case WSAPI_ServerMessage::Type::MouseDown:
  116. type = GEvent::MouseDown;
  117. break;
  118. case WSAPI_ServerMessage::Type::MouseDoubleClick:
  119. type = GEvent::MouseDoubleClick;
  120. break;
  121. case WSAPI_ServerMessage::Type::MouseWheel:
  122. type = GEvent::MouseWheel;
  123. break;
  124. default:
  125. ASSERT_NOT_REACHED();
  126. break;
  127. }
  128. GMouseButton button { GMouseButton::None };
  129. switch (event.mouse.button) {
  130. case WSAPI_MouseButton::NoButton:
  131. button = GMouseButton::None;
  132. break;
  133. case WSAPI_MouseButton::Left:
  134. button = GMouseButton::Left;
  135. break;
  136. case WSAPI_MouseButton::Right:
  137. button = GMouseButton::Right;
  138. break;
  139. case WSAPI_MouseButton::Middle:
  140. button = GMouseButton::Middle;
  141. break;
  142. default:
  143. ASSERT_NOT_REACHED();
  144. break;
  145. }
  146. CEventLoop::current().post_event(window, make<GMouseEvent>(type, event.mouse.position, event.mouse.buttons, button, event.mouse.modifiers, event.mouse.wheel_delta));
  147. }
  148. void GWindowServerConnection::handle_menu_event(const WSAPI_ServerMessage& event)
  149. {
  150. if (event.type == WSAPI_ServerMessage::Type::MenuItemActivated) {
  151. auto* menu = GMenu::from_menu_id(event.menu.menu_id);
  152. if (!menu) {
  153. dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
  154. return;
  155. }
  156. if (auto* action = menu->action_at(event.menu.identifier))
  157. action->activate();
  158. return;
  159. }
  160. ASSERT_NOT_REACHED();
  161. }
  162. void GWindowServerConnection::handle_wm_event(const WSAPI_ServerMessage& event, GWindow& window)
  163. {
  164. #ifdef GEVENTLOOP_DEBUG
  165. dbgprintf("GEventLoop: handle_wm_event: %d\n", (int)event.type);
  166. #endif
  167. if (event.type == WSAPI_ServerMessage::WM_WindowStateChanged)
  168. CEventLoop::current().post_event(window, make<GWMWindowStateChangedEvent>(event.wm.client_id, event.wm.window_id, String(event.text, event.text_length), event.wm.rect, event.wm.is_active, (GWindowType)event.wm.window_type, event.wm.is_minimized));
  169. else if (event.type == WSAPI_ServerMessage::WM_WindowRectChanged)
  170. CEventLoop::current().post_event(window, make<GWMWindowRectChangedEvent>(event.wm.client_id, event.wm.window_id, event.wm.rect));
  171. else if (event.type == WSAPI_ServerMessage::WM_WindowIconBitmapChanged)
  172. CEventLoop::current().post_event(window, make<GWMWindowIconBitmapChangedEvent>(event.wm.client_id, event.wm.window_id, event.wm.icon_buffer_id, event.wm.icon_size));
  173. else if (event.type == WSAPI_ServerMessage::WM_WindowRemoved)
  174. CEventLoop::current().post_event(window, make<GWMWindowRemovedEvent>(event.wm.client_id, event.wm.window_id));
  175. else
  176. ASSERT_NOT_REACHED();
  177. }
  178. void GWindowServerConnection::postprocess_bundles(Vector<IncomingMessageBundle>& bundles)
  179. {
  180. int coalesced_paints = 0;
  181. int coalesced_resizes = 0;
  182. auto unprocessed_bundles = move(bundles);
  183. HashMap<int, Size> latest_size_for_window_id;
  184. for (auto& bundle : unprocessed_bundles) {
  185. auto& event = bundle.message;
  186. if (event.type == WSAPI_ServerMessage::Type::WindowResized) {
  187. latest_size_for_window_id.set(event.window_id, event.window.rect.size);
  188. }
  189. }
  190. int paint_count = 0;
  191. HashMap<int, Size> latest_paint_size_for_window_id;
  192. for (auto& bundle : unprocessed_bundles) {
  193. auto& event = bundle.message;
  194. if (event.type == WSAPI_ServerMessage::Type::Paint) {
  195. ++paint_count;
  196. #ifdef COALESCING_DEBUG
  197. dbgprintf(" (window: %s)\n", Size(event.paint.window_size).to_string().characters());
  198. #endif
  199. latest_paint_size_for_window_id.set(event.window_id, event.paint.window_size);
  200. }
  201. }
  202. #ifdef COALESCING_DEBUG
  203. dbgprintf("paint_count: %d\n", paint_count);
  204. #endif
  205. for (auto& bundle : unprocessed_bundles) {
  206. auto& event = bundle.message;
  207. if (event.type == WSAPI_ServerMessage::Type::Greeting) {
  208. // Shouldn't get a second greeting
  209. dbg() << "Got second Greeting!?";
  210. ASSERT_NOT_REACHED();
  211. continue;
  212. }
  213. if (event.type == WSAPI_ServerMessage::Type::ScreenRectChanged) {
  214. GDesktop::the().did_receive_screen_rect({}, event.screen.rect);
  215. continue;
  216. }
  217. if (event.type == WSAPI_ServerMessage::Type::ClipboardContentsChanged) {
  218. GClipboard::the().did_receive_clipboard_contents_changed({}, String(event.text, event.text_length));
  219. continue;
  220. }
  221. if (event.type == WSAPI_ServerMessage::Error) {
  222. dbgprintf("GEventLoop got error message from server\n");
  223. dbgprintf(" - error message: %s\n", String(event.text, event.text_length).characters());
  224. CEventLoop::current().quit(1);
  225. return;
  226. }
  227. switch (event.type) {
  228. case WSAPI_ServerMessage::MenuItemActivated:
  229. handle_menu_event(event);
  230. continue;
  231. default:
  232. break;
  233. }
  234. auto* window = GWindow::from_window_id(event.window_id);
  235. if (!window) {
  236. dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
  237. continue;
  238. }
  239. switch (event.type) {
  240. case WSAPI_ServerMessage::Type::Paint:
  241. if (Size(event.paint.window_size) != latest_paint_size_for_window_id.get(event.window_id).value_or({})) {
  242. ++coalesced_paints;
  243. break;
  244. }
  245. handle_paint_event(event, *window, bundle.extra_data);
  246. break;
  247. case WSAPI_ServerMessage::Type::MouseDown:
  248. case WSAPI_ServerMessage::Type::MouseDoubleClick:
  249. case WSAPI_ServerMessage::Type::MouseUp:
  250. case WSAPI_ServerMessage::Type::MouseMove:
  251. case WSAPI_ServerMessage::Type::MouseWheel:
  252. handle_mouse_event(event, *window);
  253. break;
  254. case WSAPI_ServerMessage::Type::WindowActivated:
  255. case WSAPI_ServerMessage::Type::WindowDeactivated:
  256. handle_window_activation_event(event, *window);
  257. break;
  258. case WSAPI_ServerMessage::Type::WindowCloseRequest:
  259. handle_window_close_request_event(event, *window);
  260. break;
  261. case WSAPI_ServerMessage::Type::KeyDown:
  262. case WSAPI_ServerMessage::Type::KeyUp:
  263. handle_key_event(event, *window);
  264. break;
  265. case WSAPI_ServerMessage::Type::WindowEntered:
  266. case WSAPI_ServerMessage::Type::WindowLeft:
  267. handle_window_entered_or_left_event(event, *window);
  268. break;
  269. case WSAPI_ServerMessage::Type::WindowResized:
  270. if (Size(event.window.rect.size) != latest_size_for_window_id.get(event.window_id).value_or({})) {
  271. ++coalesced_resizes;
  272. break;
  273. }
  274. handle_resize_event(event, *window);
  275. break;
  276. default:
  277. if (event.type > WSAPI_ServerMessage::__Begin_WM_Events__ && event.type < WSAPI_ServerMessage::Type::__End_WM_Events__)
  278. handle_wm_event(event, *window);
  279. break;
  280. }
  281. }
  282. #ifdef COALESCING_DEBUG
  283. if (coalesced_paints)
  284. dbgprintf("Coalesced %d paints\n", coalesced_paints);
  285. if (coalesced_resizes)
  286. dbgprintf("Coalesced %d resizes\n", coalesced_resizes);
  287. #endif
  288. }
  289. void GWindowServerConnection::handle_greeting(WSAPI_ServerMessage& message)
  290. {
  291. set_server_pid(message.greeting.server_pid);
  292. set_my_client_id(message.greeting.your_client_id);
  293. GDesktop::the().did_receive_screen_rect({}, message.greeting.screen_rect);
  294. }