GEventLoop.cpp 12 KB

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