GEventLoop.cpp 12 KB

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