GEventLoop.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include <LibCore/CObject.h>
  2. #include "GEventLoop.h"
  3. #include "GEvent.h"
  4. #include "GWindow.h"
  5. #include <LibGUI/GApplication.h>
  6. #include <LibGUI/GAction.h>
  7. #include <LibCore/CNotifier.h>
  8. #include <LibGUI/GMenu.h>
  9. #include <LibGUI/GDesktop.h>
  10. #include <LibC/unistd.h>
  11. #include <LibC/stdio.h>
  12. #include <LibC/fcntl.h>
  13. #include <LibC/string.h>
  14. #include <LibC/time.h>
  15. #include <LibC/sys/select.h>
  16. #include <LibC/sys/socket.h>
  17. #include <LibC/sys/time.h>
  18. #include <LibC/errno.h>
  19. #include <LibC/string.h>
  20. #include <LibC/stdlib.h>
  21. //#define GEVENTLOOP_DEBUG
  22. //#define COALESCING_DEBUG
  23. static HashMap<GShortcut, GAction*>* g_actions;
  24. int GEventLoop::s_event_fd = -1;
  25. pid_t GEventLoop::s_server_pid = -1;
  26. void GEventLoop::connect_to_server()
  27. {
  28. ASSERT(s_event_fd == -1);
  29. s_event_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  30. if (s_event_fd < 0) {
  31. perror("socket");
  32. ASSERT_NOT_REACHED();
  33. }
  34. sockaddr_un address;
  35. address.sun_family = AF_LOCAL;
  36. strcpy(address.sun_path, "/tmp/wsportal");
  37. int retries = 1000;
  38. int rc = 0;
  39. while (retries) {
  40. rc = connect(s_event_fd, (const sockaddr*)&address, sizeof(address));
  41. if (rc == 0)
  42. break;
  43. #ifdef GEVENTLOOP_DEBUG
  44. dbgprintf("connect failed: %d, %s\n", errno, strerror(errno));
  45. #endif
  46. sleep(1);
  47. --retries;
  48. }
  49. if (rc < 0) {
  50. ASSERT_NOT_REACHED();
  51. }
  52. WSAPI_ClientMessage request;
  53. request.type = WSAPI_ClientMessage::Type::Greeting;
  54. request.greeting.client_pid = getpid();
  55. auto response = sync_request(request, WSAPI_ServerMessage::Type::Greeting);
  56. s_server_pid = response.greeting.server_pid;
  57. GDesktop::the().did_receive_screen_rect(Badge<GEventLoop>(), response.greeting.screen_rect);
  58. }
  59. GEventLoop::GEventLoop()
  60. {
  61. static bool connected = false;
  62. if (!connected) {
  63. connect_to_server();
  64. connected = true;
  65. }
  66. if (!g_actions)
  67. g_actions = new HashMap<GShortcut, GAction*>;
  68. #ifdef GEVENTLOOP_DEBUG
  69. dbgprintf("(%u) GEventLoop constructed :)\n", getpid());
  70. #endif
  71. }
  72. GEventLoop::~GEventLoop()
  73. {
  74. }
  75. void GEventLoop::handle_paint_event(const WSAPI_ServerMessage& event, GWindow& window)
  76. {
  77. #ifdef GEVENTLOOP_DEBUG
  78. dbgprintf("WID=%x Paint [%d,%d %dx%d]\n", event.window_id, event.paint.rect.location.x, event.paint.rect.location.y, event.paint.rect.size.width, event.paint.rect.size.height);
  79. #endif
  80. post_event(window, make<GPaintEvent>(event.paint.rect, event.paint.window_size));
  81. }
  82. void GEventLoop::handle_resize_event(const WSAPI_ServerMessage& event, GWindow& window)
  83. {
  84. post_event(window, make<GResizeEvent>(event.window.old_rect.size, event.window.rect.size));
  85. }
  86. void GEventLoop::handle_window_activation_event(const WSAPI_ServerMessage& event, GWindow& window)
  87. {
  88. #ifdef GEVENTLOOP_DEBUG
  89. dbgprintf("WID=%x WindowActivation\n", event.window_id);
  90. #endif
  91. post_event(window, make<GEvent>(event.type == WSAPI_ServerMessage::Type::WindowActivated ? GEvent::WindowBecameActive : GEvent::WindowBecameInactive));
  92. }
  93. void GEventLoop::handle_window_close_request_event(const WSAPI_ServerMessage&, GWindow& window)
  94. {
  95. post_event(window, make<GEvent>(GEvent::WindowCloseRequest));
  96. }
  97. void GEventLoop::handle_window_entered_or_left_event(const WSAPI_ServerMessage& message, GWindow& window)
  98. {
  99. post_event(window, make<GEvent>(message.type == WSAPI_ServerMessage::Type::WindowEntered ? GEvent::WindowEntered : GEvent::WindowLeft));
  100. }
  101. void GEventLoop::handle_key_event(const WSAPI_ServerMessage& event, GWindow& window)
  102. {
  103. #ifdef GEVENTLOOP_DEBUG
  104. dbgprintf("WID=%x KeyEvent character=0x%b\n", event.window_id, event.key.character);
  105. #endif
  106. auto key_event = make<GKeyEvent>(event.type == WSAPI_ServerMessage::Type::KeyDown ? GEvent::KeyDown : GEvent::KeyUp, event.key.key, event.key.modifiers);
  107. if (event.key.character != '\0')
  108. key_event->m_text = String(&event.key.character, 1);
  109. if (event.type == WSAPI_ServerMessage::Type::KeyDown) {
  110. if (auto* action = GApplication::the().action_for_key_event(*key_event)) {
  111. action->activate();
  112. return;
  113. }
  114. }
  115. post_event(window, move(key_event));
  116. }
  117. void GEventLoop::handle_mouse_event(const WSAPI_ServerMessage& event, GWindow& window)
  118. {
  119. #ifdef GEVENTLOOP_DEBUG
  120. dbgprintf("WID=%x MouseEvent %d,%d\n", event.window_id, event.mouse.position.x, event.mouse.position.y);
  121. #endif
  122. GMouseEvent::Type type;
  123. switch (event.type) {
  124. case WSAPI_ServerMessage::Type::MouseMove: type = GEvent::MouseMove; break;
  125. case WSAPI_ServerMessage::Type::MouseUp: type = GEvent::MouseUp; break;
  126. case WSAPI_ServerMessage::Type::MouseDown: type = GEvent::MouseDown; break;
  127. default: ASSERT_NOT_REACHED(); break;
  128. }
  129. GMouseButton button { GMouseButton::None };
  130. switch (event.mouse.button) {
  131. case WSAPI_MouseButton::NoButton: button = GMouseButton::None; break;
  132. case WSAPI_MouseButton::Left: button = GMouseButton::Left; break;
  133. case WSAPI_MouseButton::Right: button = GMouseButton::Right; break;
  134. case WSAPI_MouseButton::Middle: button = GMouseButton::Middle; break;
  135. default: ASSERT_NOT_REACHED(); break;
  136. }
  137. post_event(window, make<GMouseEvent>(type, event.mouse.position, event.mouse.buttons, button, event.mouse.modifiers));
  138. }
  139. void GEventLoop::handle_menu_event(const WSAPI_ServerMessage& event)
  140. {
  141. if (event.type == WSAPI_ServerMessage::Type::MenuItemActivated) {
  142. auto* menu = GMenu::from_menu_id(event.menu.menu_id);
  143. if (!menu) {
  144. dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
  145. return;
  146. }
  147. if (auto* action = menu->action_at(event.menu.identifier))
  148. action->activate();
  149. return;
  150. }
  151. ASSERT_NOT_REACHED();
  152. }
  153. void GEventLoop::handle_wm_event(const WSAPI_ServerMessage& event, GWindow& window)
  154. {
  155. #ifdef GEVENTLOOP_DEBUG
  156. dbgprintf("GEventLoop: handle_wm_event: %d\n", (int)event.type);
  157. #endif
  158. if (event.type == WSAPI_ServerMessage::WM_WindowStateChanged)
  159. return 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));
  160. if (event.type == WSAPI_ServerMessage::WM_WindowRemoved)
  161. return post_event(window, make<GWMWindowRemovedEvent>(event.wm.client_id, event.wm.window_id));
  162. ASSERT_NOT_REACHED();
  163. }
  164. void GEventLoop::process_unprocessed_messages()
  165. {
  166. int coalesced_paints = 0;
  167. int coalesced_resizes = 0;
  168. auto unprocessed_events = move(m_unprocessed_messages);
  169. HashMap<int, Size> latest_size_for_window_id;
  170. for (auto& event : unprocessed_events) {
  171. if (event.type == WSAPI_ServerMessage::Type::WindowResized) {
  172. latest_size_for_window_id.set(event.window_id, event.window.rect.size);
  173. }
  174. }
  175. int paint_count = 0;
  176. HashMap<int, Size> latest_paint_size_for_window_id;
  177. for (auto& event : unprocessed_events) {
  178. if (event.type == WSAPI_ServerMessage::Type::Paint) {
  179. ++paint_count;
  180. #ifdef COALESCING_DEBUG
  181. dbgprintf(" %s (window: %s)\n", Rect(event.paint.rect).to_string().characters(), Size(event.paint.window_size).to_string().characters());
  182. #endif
  183. latest_paint_size_for_window_id.set(event.window_id, event.paint.window_size);
  184. }
  185. }
  186. #ifdef COALESCING_DEBUG
  187. dbgprintf("paint_count: %d\n", paint_count);
  188. #endif
  189. for (auto& event : unprocessed_events) {
  190. if (event.type == WSAPI_ServerMessage::Type::Greeting) {
  191. s_server_pid = event.greeting.server_pid;
  192. GDesktop::the().did_receive_screen_rect(Badge<GEventLoop>(), event.greeting.screen_rect);
  193. continue;
  194. }
  195. if (event.type == WSAPI_ServerMessage::Type::ScreenRectChanged) {
  196. GDesktop::the().did_receive_screen_rect(Badge<GEventLoop>(), event.screen.rect);
  197. continue;
  198. }
  199. if (event.type == WSAPI_ServerMessage::Error) {
  200. dbgprintf("GEventLoop got error message from server\n");
  201. dbgprintf(" - error message: %s\n", String(event.text, event.text_length).characters());
  202. quit(1);
  203. return;
  204. }
  205. switch (event.type) {
  206. case WSAPI_ServerMessage::MenuItemActivated:
  207. handle_menu_event(event);
  208. continue;
  209. default:
  210. break;
  211. }
  212. auto* window = GWindow::from_window_id(event.window_id);
  213. if (!window) {
  214. dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
  215. continue;
  216. }
  217. switch (event.type) {
  218. case WSAPI_ServerMessage::Type::Paint:
  219. if (Size(event.paint.window_size) != latest_paint_size_for_window_id.get(event.window_id)) {
  220. ++coalesced_paints;
  221. break;
  222. }
  223. handle_paint_event(event, *window);
  224. break;
  225. case WSAPI_ServerMessage::Type::MouseDown:
  226. case WSAPI_ServerMessage::Type::MouseUp:
  227. case WSAPI_ServerMessage::Type::MouseMove:
  228. handle_mouse_event(event, *window);
  229. break;
  230. case WSAPI_ServerMessage::Type::WindowActivated:
  231. case WSAPI_ServerMessage::Type::WindowDeactivated:
  232. handle_window_activation_event(event, *window);
  233. break;
  234. case WSAPI_ServerMessage::Type::WindowCloseRequest:
  235. handle_window_close_request_event(event, *window);
  236. break;
  237. case WSAPI_ServerMessage::Type::KeyDown:
  238. case WSAPI_ServerMessage::Type::KeyUp:
  239. handle_key_event(event, *window);
  240. break;
  241. case WSAPI_ServerMessage::Type::WindowEntered:
  242. case WSAPI_ServerMessage::Type::WindowLeft:
  243. handle_window_entered_or_left_event(event, *window);
  244. break;
  245. case WSAPI_ServerMessage::Type::WindowResized:
  246. if (Size(event.window.rect.size) != latest_size_for_window_id.get(event.window_id)) {
  247. ++coalesced_resizes;
  248. break;
  249. }
  250. handle_resize_event(event, *window);
  251. break;
  252. case WSAPI_ServerMessage::Type::WM_WindowRemoved:
  253. case WSAPI_ServerMessage::Type::WM_WindowStateChanged:
  254. handle_wm_event(event, *window);
  255. break;
  256. default:
  257. break;
  258. }
  259. }
  260. #ifdef COALESCING_DEBUG
  261. if (coalesced_paints)
  262. dbgprintf("Coalesced %d paints\n", coalesced_paints);
  263. if (coalesced_resizes)
  264. dbgprintf("Coalesced %d resizes\n", coalesced_resizes);
  265. #endif
  266. if (!m_unprocessed_messages.is_empty())
  267. process_unprocessed_messages();
  268. }
  269. bool GEventLoop::drain_messages_from_server()
  270. {
  271. bool is_first_pass = true;
  272. for (;;) {
  273. WSAPI_ServerMessage message;
  274. ssize_t nread = read(s_event_fd, &message, sizeof(WSAPI_ServerMessage));
  275. if (nread < 0) {
  276. perror("read");
  277. quit(1);
  278. return false;
  279. }
  280. if (nread == 0) {
  281. if (is_first_pass) {
  282. fprintf(stderr, "EOF on WindowServer fd\n");
  283. quit(1);
  284. return false;
  285. }
  286. return true;
  287. }
  288. assert(nread == sizeof(message));
  289. m_unprocessed_messages.append(move(message));
  290. is_first_pass = false;
  291. }
  292. }
  293. bool GEventLoop::post_message_to_server(const WSAPI_ClientMessage& message)
  294. {
  295. int nwritten = write(s_event_fd, &message, sizeof(WSAPI_ClientMessage));
  296. return nwritten == sizeof(WSAPI_ClientMessage);
  297. }
  298. bool GEventLoop::wait_for_specific_event(WSAPI_ServerMessage::Type type, WSAPI_ServerMessage& event)
  299. {
  300. for (;;) {
  301. fd_set rfds;
  302. FD_ZERO(&rfds);
  303. FD_SET(s_event_fd, &rfds);
  304. int rc = select(s_event_fd + 1, &rfds, nullptr, nullptr, nullptr);
  305. ASSERT(rc > 0);
  306. ASSERT(FD_ISSET(s_event_fd, &rfds));
  307. bool success = drain_messages_from_server();
  308. if (!success)
  309. return false;
  310. for (ssize_t i = 0; i < m_unprocessed_messages.size(); ++i) {
  311. if (m_unprocessed_messages[i].type == type) {
  312. event = move(m_unprocessed_messages[i]);
  313. m_unprocessed_messages.remove(i);
  314. return true;
  315. }
  316. }
  317. }
  318. }
  319. WSAPI_ServerMessage GEventLoop::sync_request(const WSAPI_ClientMessage& request, WSAPI_ServerMessage::Type response_type)
  320. {
  321. bool success = post_message_to_server(request);
  322. ASSERT(success);
  323. WSAPI_ServerMessage response;
  324. success = wait_for_specific_event(response_type, response);
  325. ASSERT(success);
  326. return response;
  327. }