GEventLoop.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 <LibGUI/GWidget.h>
  11. #include <LibC/unistd.h>
  12. #include <LibC/stdio.h>
  13. #include <LibC/fcntl.h>
  14. #include <LibC/string.h>
  15. #include <LibC/time.h>
  16. #include <LibC/sys/select.h>
  17. #include <LibC/sys/socket.h>
  18. #include <LibC/sys/time.h>
  19. #include <LibC/errno.h>
  20. #include <LibC/string.h>
  21. #include <LibC/stdlib.h>
  22. //#define GEVENTLOOP_DEBUG
  23. //#define COALESCING_DEBUG
  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. #ifdef GEVENTLOOP_DEBUG
  67. dbgprintf("(%u) GEventLoop constructed :)\n", getpid());
  68. #endif
  69. }
  70. GEventLoop::~GEventLoop()
  71. {
  72. }
  73. void GEventLoop::handle_paint_event(const WSAPI_ServerMessage& event, GWindow& window, const ByteBuffer& extra_data)
  74. {
  75. #ifdef GEVENTLOOP_DEBUG
  76. 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);
  77. #endif
  78. Vector<Rect, 32> rects;
  79. for (int i = 0; i < min(WSAPI_ServerMessage::max_inline_rect_count, event.rect_count); ++i)
  80. rects.append(event.rects[i]);
  81. if (event.extra_size) {
  82. auto* extra_rects = reinterpret_cast<const WSAPI_Rect*>(extra_data.data());
  83. for (int i = 0; i < event.rect_count - WSAPI_ServerMessage::max_inline_rect_count; ++i)
  84. rects.append(extra_rects[i]);
  85. }
  86. post_event(window, make<GMultiPaintEvent>(rects, event.paint.window_size));
  87. }
  88. void GEventLoop::handle_resize_event(const WSAPI_ServerMessage& event, GWindow& window)
  89. {
  90. post_event(window, make<GResizeEvent>(event.window.old_rect.size, event.window.rect.size));
  91. }
  92. void GEventLoop::handle_window_activation_event(const WSAPI_ServerMessage& event, GWindow& window)
  93. {
  94. #ifdef GEVENTLOOP_DEBUG
  95. dbgprintf("WID=%x WindowActivation\n", event.window_id);
  96. #endif
  97. post_event(window, make<GEvent>(event.type == WSAPI_ServerMessage::Type::WindowActivated ? GEvent::WindowBecameActive : GEvent::WindowBecameInactive));
  98. }
  99. void GEventLoop::handle_window_close_request_event(const WSAPI_ServerMessage&, GWindow& window)
  100. {
  101. post_event(window, make<GEvent>(GEvent::WindowCloseRequest));
  102. }
  103. void GEventLoop::handle_window_entered_or_left_event(const WSAPI_ServerMessage& message, GWindow& window)
  104. {
  105. post_event(window, make<GEvent>(message.type == WSAPI_ServerMessage::Type::WindowEntered ? GEvent::WindowEntered : GEvent::WindowLeft));
  106. }
  107. void GEventLoop::handle_key_event(const WSAPI_ServerMessage& event, GWindow& window)
  108. {
  109. #ifdef GEVENTLOOP_DEBUG
  110. dbgprintf("WID=%x KeyEvent character=0x%b\n", event.window_id, event.key.character);
  111. #endif
  112. auto key_event = make<GKeyEvent>(event.type == WSAPI_ServerMessage::Type::KeyDown ? GEvent::KeyDown : GEvent::KeyUp, event.key.key, event.key.modifiers);
  113. if (event.key.character != '\0')
  114. key_event->m_text = String(&event.key.character, 1);
  115. if (event.type == WSAPI_ServerMessage::Type::KeyDown) {
  116. if (auto* focused_widget = window.focused_widget()) {
  117. if (auto* action = focused_widget->action_for_key_event(*key_event)) {
  118. if (action->is_enabled()) {
  119. action->activate();
  120. return;
  121. }
  122. }
  123. }
  124. if (auto* action = GApplication::the().action_for_key_event(*key_event)) {
  125. if (action->is_enabled()) {
  126. action->activate();
  127. return;
  128. }
  129. }
  130. }
  131. post_event(window, move(key_event));
  132. }
  133. void GEventLoop::handle_mouse_event(const WSAPI_ServerMessage& event, GWindow& window)
  134. {
  135. #ifdef GEVENTLOOP_DEBUG
  136. dbgprintf("WID=%x MouseEvent %d,%d\n", event.window_id, event.mouse.position.x, event.mouse.position.y);
  137. #endif
  138. GMouseEvent::Type type;
  139. switch (event.type) {
  140. case WSAPI_ServerMessage::Type::MouseMove: type = GEvent::MouseMove; break;
  141. case WSAPI_ServerMessage::Type::MouseUp: type = GEvent::MouseUp; break;
  142. case WSAPI_ServerMessage::Type::MouseDown: type = GEvent::MouseDown; break;
  143. default: ASSERT_NOT_REACHED(); break;
  144. }
  145. GMouseButton button { GMouseButton::None };
  146. switch (event.mouse.button) {
  147. case WSAPI_MouseButton::NoButton: button = GMouseButton::None; break;
  148. case WSAPI_MouseButton::Left: button = GMouseButton::Left; break;
  149. case WSAPI_MouseButton::Right: button = GMouseButton::Right; break;
  150. case WSAPI_MouseButton::Middle: button = GMouseButton::Middle; break;
  151. default: ASSERT_NOT_REACHED(); break;
  152. }
  153. post_event(window, make<GMouseEvent>(type, event.mouse.position, event.mouse.buttons, button, event.mouse.modifiers));
  154. }
  155. void GEventLoop::handle_menu_event(const WSAPI_ServerMessage& event)
  156. {
  157. if (event.type == WSAPI_ServerMessage::Type::MenuItemActivated) {
  158. auto* menu = GMenu::from_menu_id(event.menu.menu_id);
  159. if (!menu) {
  160. dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
  161. return;
  162. }
  163. if (auto* action = menu->action_at(event.menu.identifier))
  164. action->activate();
  165. return;
  166. }
  167. ASSERT_NOT_REACHED();
  168. }
  169. void GEventLoop::handle_wm_event(const WSAPI_ServerMessage& event, GWindow& window)
  170. {
  171. #ifdef GEVENTLOOP_DEBUG
  172. dbgprintf("GEventLoop: handle_wm_event: %d\n", (int)event.type);
  173. #endif
  174. if (event.type == WSAPI_ServerMessage::WM_WindowStateChanged)
  175. 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));
  176. if (event.type == WSAPI_ServerMessage::WM_WindowRectChanged)
  177. return post_event(window, make<GWMWindowRectChangedEvent>(event.wm.client_id, event.wm.window_id, event.wm.rect));
  178. if (event.type == WSAPI_ServerMessage::WM_WindowIconChanged)
  179. return post_event(window, make<GWMWindowIconChangedEvent>(event.wm.client_id, event.wm.window_id, String(event.text, event.text_length)));
  180. if (event.type == WSAPI_ServerMessage::WM_WindowRemoved)
  181. return post_event(window, make<GWMWindowRemovedEvent>(event.wm.client_id, event.wm.window_id));
  182. ASSERT_NOT_REACHED();
  183. }
  184. void GEventLoop::process_unprocessed_bundles()
  185. {
  186. int coalesced_paints = 0;
  187. int coalesced_resizes = 0;
  188. auto unprocessed_bundles = move(m_unprocessed_bundles);
  189. HashMap<int, Size> latest_size_for_window_id;
  190. for (auto& bundle : unprocessed_bundles) {
  191. auto& event = bundle.message;
  192. if (event.type == WSAPI_ServerMessage::Type::WindowResized) {
  193. latest_size_for_window_id.set(event.window_id, event.window.rect.size);
  194. }
  195. }
  196. int paint_count = 0;
  197. HashMap<int, Size> latest_paint_size_for_window_id;
  198. for (auto& bundle : unprocessed_bundles) {
  199. auto& event = bundle.message;
  200. if (event.type == WSAPI_ServerMessage::Type::Paint) {
  201. ++paint_count;
  202. #ifdef COALESCING_DEBUG
  203. dbgprintf(" %s (window: %s)\n", Rect(event.paint.rect).to_string().characters(), Size(event.paint.window_size).to_string().characters());
  204. #endif
  205. latest_paint_size_for_window_id.set(event.window_id, event.paint.window_size);
  206. }
  207. }
  208. #ifdef COALESCING_DEBUG
  209. dbgprintf("paint_count: %d\n", paint_count);
  210. #endif
  211. for (auto& bundle : unprocessed_bundles) {
  212. auto& event = bundle.message;
  213. if (event.type == WSAPI_ServerMessage::Type::Greeting) {
  214. s_server_pid = event.greeting.server_pid;
  215. GDesktop::the().did_receive_screen_rect(Badge<GEventLoop>(), event.greeting.screen_rect);
  216. continue;
  217. }
  218. if (event.type == WSAPI_ServerMessage::Type::ScreenRectChanged) {
  219. GDesktop::the().did_receive_screen_rect(Badge<GEventLoop>(), event.screen.rect);
  220. continue;
  221. }
  222. if (event.type == WSAPI_ServerMessage::Error) {
  223. dbgprintf("GEventLoop got error message from server\n");
  224. dbgprintf(" - error message: %s\n", String(event.text, event.text_length).characters());
  225. quit(1);
  226. return;
  227. }
  228. switch (event.type) {
  229. case WSAPI_ServerMessage::MenuItemActivated:
  230. handle_menu_event(event);
  231. continue;
  232. default:
  233. break;
  234. }
  235. auto* window = GWindow::from_window_id(event.window_id);
  236. if (!window) {
  237. dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
  238. continue;
  239. }
  240. switch (event.type) {
  241. case WSAPI_ServerMessage::Type::Paint:
  242. if (Size(event.paint.window_size) != latest_paint_size_for_window_id.get(event.window_id)) {
  243. ++coalesced_paints;
  244. break;
  245. }
  246. handle_paint_event(event, *window, bundle.extra_data);
  247. break;
  248. case WSAPI_ServerMessage::Type::MouseDown:
  249. case WSAPI_ServerMessage::Type::MouseUp:
  250. case WSAPI_ServerMessage::Type::MouseMove:
  251. handle_mouse_event(event, *window);
  252. break;
  253. case WSAPI_ServerMessage::Type::WindowActivated:
  254. case WSAPI_ServerMessage::Type::WindowDeactivated:
  255. handle_window_activation_event(event, *window);
  256. break;
  257. case WSAPI_ServerMessage::Type::WindowCloseRequest:
  258. handle_window_close_request_event(event, *window);
  259. break;
  260. case WSAPI_ServerMessage::Type::KeyDown:
  261. case WSAPI_ServerMessage::Type::KeyUp:
  262. handle_key_event(event, *window);
  263. break;
  264. case WSAPI_ServerMessage::Type::WindowEntered:
  265. case WSAPI_ServerMessage::Type::WindowLeft:
  266. handle_window_entered_or_left_event(event, *window);
  267. break;
  268. case WSAPI_ServerMessage::Type::WindowResized:
  269. if (Size(event.window.rect.size) != latest_size_for_window_id.get(event.window_id)) {
  270. ++coalesced_resizes;
  271. break;
  272. }
  273. handle_resize_event(event, *window);
  274. break;
  275. case WSAPI_ServerMessage::Type::WM_WindowRemoved:
  276. case WSAPI_ServerMessage::Type::WM_WindowStateChanged:
  277. case WSAPI_ServerMessage::Type::WM_WindowIconChanged:
  278. handle_wm_event(event, *window);
  279. break;
  280. default:
  281. break;
  282. }
  283. }
  284. #ifdef COALESCING_DEBUG
  285. if (coalesced_paints)
  286. dbgprintf("Coalesced %d paints\n", coalesced_paints);
  287. if (coalesced_resizes)
  288. dbgprintf("Coalesced %d resizes\n", coalesced_resizes);
  289. #endif
  290. }
  291. bool GEventLoop::drain_messages_from_server()
  292. {
  293. bool is_first_pass = true;
  294. for (;;) {
  295. WSAPI_ServerMessage message;
  296. ssize_t nread = read(s_event_fd, &message, sizeof(WSAPI_ServerMessage));
  297. if (nread < 0) {
  298. perror("read");
  299. quit(1);
  300. return false;
  301. }
  302. if (nread == 0) {
  303. if (is_first_pass) {
  304. fprintf(stderr, "EOF on WindowServer fd\n");
  305. quit(1);
  306. return false;
  307. }
  308. return true;
  309. }
  310. assert(nread == sizeof(message));
  311. ByteBuffer extra_data;
  312. if (message.extra_size) {
  313. extra_data = ByteBuffer::create_uninitialized(message.extra_size);
  314. int extra_nread = read(s_event_fd, extra_data.data(), extra_data.size());
  315. ASSERT(extra_nread == message.extra_size);
  316. }
  317. m_unprocessed_bundles.append({ move(message), move(extra_data) });
  318. is_first_pass = false;
  319. }
  320. }
  321. bool GEventLoop::post_message_to_server(const WSAPI_ClientMessage& message, const ByteBuffer& extra_data)
  322. {
  323. if (!extra_data.is_empty())
  324. const_cast<WSAPI_ClientMessage&>(message).extra_size = extra_data.size();
  325. int nwritten = write(s_event_fd, &message, sizeof(WSAPI_ClientMessage));
  326. ASSERT(nwritten == sizeof(WSAPI_ClientMessage));
  327. if (!extra_data.is_empty()) {
  328. nwritten = write(s_event_fd, extra_data.data(), extra_data.size());
  329. ASSERT(nwritten == extra_data.size());
  330. }
  331. return true;
  332. }
  333. bool GEventLoop::wait_for_specific_event(WSAPI_ServerMessage::Type type, WSAPI_ServerMessage& event)
  334. {
  335. for (;;) {
  336. fd_set rfds;
  337. FD_ZERO(&rfds);
  338. FD_SET(s_event_fd, &rfds);
  339. int rc = select(s_event_fd + 1, &rfds, nullptr, nullptr, nullptr);
  340. ASSERT(rc > 0);
  341. ASSERT(FD_ISSET(s_event_fd, &rfds));
  342. bool success = drain_messages_from_server();
  343. if (!success)
  344. return false;
  345. for (ssize_t i = 0; i < m_unprocessed_bundles.size(); ++i) {
  346. if (m_unprocessed_bundles[i].message.type == type) {
  347. event = move(m_unprocessed_bundles[i].message);
  348. m_unprocessed_bundles.remove(i);
  349. return true;
  350. }
  351. }
  352. }
  353. }
  354. WSAPI_ServerMessage GEventLoop::sync_request(const WSAPI_ClientMessage& request, WSAPI_ServerMessage::Type response_type)
  355. {
  356. bool success = post_message_to_server(request);
  357. ASSERT(success);
  358. WSAPI_ServerMessage response;
  359. success = wait_for_specific_event(response_type, response);
  360. ASSERT(success);
  361. return response;
  362. }