GEventLoop.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. #include <sys/uio.h>
  23. //#define GEVENTLOOP_DEBUG
  24. //#define COALESCING_DEBUG
  25. int GEventLoop::s_event_fd = -1;
  26. int GEventLoop::s_my_client_id = -1;
  27. pid_t GEventLoop::s_server_pid = -1;
  28. void GEventLoop::connect_to_server()
  29. {
  30. ASSERT(s_event_fd == -1);
  31. s_event_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  32. if (s_event_fd < 0) {
  33. perror("socket");
  34. ASSERT_NOT_REACHED();
  35. }
  36. sockaddr_un address;
  37. address.sun_family = AF_LOCAL;
  38. strcpy(address.sun_path, "/tmp/wsportal");
  39. int retries = 1000;
  40. int rc = 0;
  41. while (retries) {
  42. rc = connect(s_event_fd, (const sockaddr*)&address, sizeof(address));
  43. if (rc == 0)
  44. break;
  45. #ifdef GEVENTLOOP_DEBUG
  46. dbgprintf("connect failed: %d, %s\n", errno, strerror(errno));
  47. #endif
  48. sleep(1);
  49. --retries;
  50. }
  51. if (rc < 0) {
  52. ASSERT_NOT_REACHED();
  53. }
  54. WSAPI_ClientMessage request;
  55. request.type = WSAPI_ClientMessage::Type::Greeting;
  56. request.greeting.client_pid = getpid();
  57. auto response = sync_request(request, WSAPI_ServerMessage::Type::Greeting);
  58. handle_greeting(response);
  59. }
  60. GEventLoop::GEventLoop()
  61. {
  62. static bool connected = false;
  63. if (!connected) {
  64. connect_to_server();
  65. connected = true;
  66. }
  67. #ifdef GEVENTLOOP_DEBUG
  68. dbgprintf("(%u) GEventLoop constructed :)\n", getpid());
  69. #endif
  70. }
  71. GEventLoop::~GEventLoop()
  72. {
  73. }
  74. void GEventLoop::handle_paint_event(const WSAPI_ServerMessage& event, GWindow& window, const ByteBuffer& extra_data)
  75. {
  76. #ifdef GEVENTLOOP_DEBUG
  77. 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);
  78. #endif
  79. Vector<Rect, 32> rects;
  80. for (int i = 0; i < min(WSAPI_ServerMessage::max_inline_rect_count, event.rect_count); ++i)
  81. rects.append(event.rects[i]);
  82. if (event.extra_size) {
  83. auto* extra_rects = reinterpret_cast<const WSAPI_Rect*>(extra_data.data());
  84. for (int i = 0; i < event.rect_count - WSAPI_ServerMessage::max_inline_rect_count; ++i)
  85. rects.append(extra_rects[i]);
  86. }
  87. post_event(window, make<GMultiPaintEvent>(rects, event.paint.window_size));
  88. }
  89. void GEventLoop::handle_resize_event(const WSAPI_ServerMessage& event, GWindow& window)
  90. {
  91. post_event(window, make<GResizeEvent>(event.window.old_rect.size, event.window.rect.size));
  92. }
  93. void GEventLoop::handle_window_activation_event(const WSAPI_ServerMessage& event, GWindow& window)
  94. {
  95. #ifdef GEVENTLOOP_DEBUG
  96. dbgprintf("WID=%x WindowActivation\n", event.window_id);
  97. #endif
  98. post_event(window, make<GEvent>(event.type == WSAPI_ServerMessage::Type::WindowActivated ? GEvent::WindowBecameActive : GEvent::WindowBecameInactive));
  99. }
  100. void GEventLoop::handle_window_close_request_event(const WSAPI_ServerMessage&, GWindow& window)
  101. {
  102. post_event(window, make<GEvent>(GEvent::WindowCloseRequest));
  103. }
  104. void GEventLoop::handle_window_entered_or_left_event(const WSAPI_ServerMessage& message, GWindow& window)
  105. {
  106. post_event(window, make<GEvent>(message.type == WSAPI_ServerMessage::Type::WindowEntered ? GEvent::WindowEntered : GEvent::WindowLeft));
  107. }
  108. void GEventLoop::handle_key_event(const WSAPI_ServerMessage& event, GWindow& window)
  109. {
  110. #ifdef GEVENTLOOP_DEBUG
  111. dbgprintf("WID=%x KeyEvent character=0x%b\n", event.window_id, event.key.character);
  112. #endif
  113. auto key_event = make<GKeyEvent>(event.type == WSAPI_ServerMessage::Type::KeyDown ? GEvent::KeyDown : GEvent::KeyUp, event.key.key, event.key.modifiers);
  114. if (event.key.character != '\0')
  115. key_event->m_text = String(&event.key.character, 1);
  116. if (event.type == WSAPI_ServerMessage::Type::KeyDown) {
  117. if (auto* focused_widget = window.focused_widget()) {
  118. if (auto* action = focused_widget->action_for_key_event(*key_event)) {
  119. if (action->is_enabled()) {
  120. action->activate();
  121. return;
  122. }
  123. }
  124. }
  125. if (auto* action = GApplication::the().action_for_key_event(*key_event)) {
  126. if (action->is_enabled()) {
  127. action->activate();
  128. return;
  129. }
  130. }
  131. }
  132. post_event(window, move(key_event));
  133. }
  134. void GEventLoop::handle_mouse_event(const WSAPI_ServerMessage& event, GWindow& window)
  135. {
  136. #ifdef GEVENTLOOP_DEBUG
  137. dbgprintf("WID=%x MouseEvent %d,%d\n", event.window_id, event.mouse.position.x, event.mouse.position.y);
  138. #endif
  139. GMouseEvent::Type type;
  140. switch (event.type) {
  141. case WSAPI_ServerMessage::Type::MouseMove: type = GEvent::MouseMove; break;
  142. case WSAPI_ServerMessage::Type::MouseUp: type = GEvent::MouseUp; break;
  143. case WSAPI_ServerMessage::Type::MouseDown: type = GEvent::MouseDown; break;
  144. default: ASSERT_NOT_REACHED(); break;
  145. }
  146. GMouseButton button { GMouseButton::None };
  147. switch (event.mouse.button) {
  148. case WSAPI_MouseButton::NoButton: button = GMouseButton::None; break;
  149. case WSAPI_MouseButton::Left: button = GMouseButton::Left; break;
  150. case WSAPI_MouseButton::Right: button = GMouseButton::Right; break;
  151. case WSAPI_MouseButton::Middle: button = GMouseButton::Middle; break;
  152. default: ASSERT_NOT_REACHED(); break;
  153. }
  154. post_event(window, make<GMouseEvent>(type, event.mouse.position, event.mouse.buttons, button, event.mouse.modifiers));
  155. }
  156. void GEventLoop::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 GEventLoop::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. 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));
  177. if (event.type == WSAPI_ServerMessage::WM_WindowRectChanged)
  178. return post_event(window, make<GWMWindowRectChangedEvent>(event.wm.client_id, event.wm.window_id, event.wm.rect));
  179. if (event.type == WSAPI_ServerMessage::WM_WindowIconChanged)
  180. return post_event(window, make<GWMWindowIconChangedEvent>(event.wm.client_id, event.wm.window_id, String(event.text, event.text_length)));
  181. if (event.type == WSAPI_ServerMessage::WM_WindowRemoved)
  182. return post_event(window, make<GWMWindowRemovedEvent>(event.wm.client_id, event.wm.window_id));
  183. ASSERT_NOT_REACHED();
  184. }
  185. void GEventLoop::process_unprocessed_bundles()
  186. {
  187. int coalesced_paints = 0;
  188. int coalesced_resizes = 0;
  189. auto unprocessed_bundles = move(m_unprocessed_bundles);
  190. HashMap<int, Size> latest_size_for_window_id;
  191. for (auto& bundle : unprocessed_bundles) {
  192. auto& event = bundle.message;
  193. if (event.type == WSAPI_ServerMessage::Type::WindowResized) {
  194. latest_size_for_window_id.set(event.window_id, event.window.rect.size);
  195. }
  196. }
  197. int paint_count = 0;
  198. HashMap<int, Size> latest_paint_size_for_window_id;
  199. for (auto& bundle : unprocessed_bundles) {
  200. auto& event = bundle.message;
  201. if (event.type == WSAPI_ServerMessage::Type::Paint) {
  202. ++paint_count;
  203. #ifdef COALESCING_DEBUG
  204. dbgprintf(" %s (window: %s)\n", Rect(event.paint.rect).to_string().characters(), Size(event.paint.window_size).to_string().characters());
  205. #endif
  206. latest_paint_size_for_window_id.set(event.window_id, event.paint.window_size);
  207. }
  208. }
  209. #ifdef COALESCING_DEBUG
  210. dbgprintf("paint_count: %d\n", paint_count);
  211. #endif
  212. for (auto& bundle : unprocessed_bundles) {
  213. auto& event = bundle.message;
  214. if (event.type == WSAPI_ServerMessage::Type::Greeting) {
  215. handle_greeting(event);
  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. fd_set rfds;
  315. FD_ZERO(&rfds);
  316. FD_SET(s_event_fd, &rfds);
  317. struct timeval timeout { 1, 0 };
  318. int rc = select(s_event_fd + 1, &rfds, nullptr, nullptr, &timeout);
  319. ASSERT(rc == 1);
  320. int extra_nread = read(s_event_fd, extra_data.data(), extra_data.size());
  321. ASSERT(extra_nread == message.extra_size);
  322. }
  323. m_unprocessed_bundles.append({ move(message), move(extra_data) });
  324. is_first_pass = false;
  325. }
  326. }
  327. bool GEventLoop::post_message_to_server(const WSAPI_ClientMessage& message, const ByteBuffer& extra_data)
  328. {
  329. if (!extra_data.is_empty())
  330. const_cast<WSAPI_ClientMessage&>(message).extra_size = extra_data.size();
  331. struct iovec iov[2];
  332. int iov_count = 1;
  333. iov[0].iov_base = (void*)&message;
  334. iov[0].iov_len = sizeof(message);
  335. if (!extra_data.is_empty()) {
  336. iov[1].iov_base = (void*)extra_data.data();
  337. iov[1].iov_len = extra_data.size();
  338. ++iov_count;
  339. }
  340. int nwritten = writev(s_event_fd, iov, iov_count);
  341. ASSERT(nwritten == sizeof(message) + extra_data.size());
  342. return true;
  343. }
  344. bool GEventLoop::wait_for_specific_event(WSAPI_ServerMessage::Type type, WSAPI_ServerMessage& event)
  345. {
  346. for (;;) {
  347. fd_set rfds;
  348. FD_ZERO(&rfds);
  349. FD_SET(s_event_fd, &rfds);
  350. int rc = select(s_event_fd + 1, &rfds, nullptr, nullptr, nullptr);
  351. if (rc < 0) {
  352. perror("select");
  353. }
  354. ASSERT(rc > 0);
  355. ASSERT(FD_ISSET(s_event_fd, &rfds));
  356. bool success = drain_messages_from_server();
  357. if (!success)
  358. return false;
  359. for (ssize_t i = 0; i < m_unprocessed_bundles.size(); ++i) {
  360. if (m_unprocessed_bundles[i].message.type == type) {
  361. event = move(m_unprocessed_bundles[i].message);
  362. m_unprocessed_bundles.remove(i);
  363. return true;
  364. }
  365. }
  366. }
  367. }
  368. WSAPI_ServerMessage GEventLoop::sync_request(const WSAPI_ClientMessage& request, WSAPI_ServerMessage::Type response_type)
  369. {
  370. bool success = post_message_to_server(request);
  371. ASSERT(success);
  372. WSAPI_ServerMessage response;
  373. success = wait_for_specific_event(response_type, response);
  374. ASSERT(success);
  375. return response;
  376. }
  377. void GEventLoop::handle_greeting(WSAPI_ServerMessage& message)
  378. {
  379. s_server_pid = message.greeting.server_pid;
  380. s_my_client_id = message.greeting.your_client_id;
  381. GDesktop::the().did_receive_screen_rect(Badge<GEventLoop>(), message.greeting.screen_rect);
  382. }