GEventLoop.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. int GEventLoop::s_windowserver_fd = -1;
  25. int GEventLoop::s_my_client_id = -1;
  26. pid_t GEventLoop::s_server_pid = -1;
  27. void GEventLoop::connect_to_server()
  28. {
  29. ASSERT(s_windowserver_fd == -1);
  30. s_windowserver_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
  31. if (s_windowserver_fd < 0) {
  32. perror("socket");
  33. ASSERT_NOT_REACHED();
  34. }
  35. sockaddr_un address;
  36. address.sun_family = AF_LOCAL;
  37. strcpy(address.sun_path, "/tmp/wsportal");
  38. int retries = 1000;
  39. int rc = 0;
  40. while (retries) {
  41. rc = connect(s_windowserver_fd, (const sockaddr*)&address, sizeof(address));
  42. if (rc == 0)
  43. break;
  44. #ifdef GEVENTLOOP_DEBUG
  45. dbgprintf("connect failed: %d, %s\n", errno, strerror(errno));
  46. #endif
  47. sleep(1);
  48. --retries;
  49. }
  50. if (rc < 0) {
  51. ASSERT_NOT_REACHED();
  52. }
  53. WSAPI_ClientMessage request;
  54. request.type = WSAPI_ClientMessage::Type::Greeting;
  55. request.greeting.client_pid = getpid();
  56. auto response = sync_request(request, WSAPI_ServerMessage::Type::Greeting);
  57. handle_greeting(response);
  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,%d\n", event.window_id, event.mouse.position.x, event.mouse.position.y, event.mouse.wheel_delta);
  137. #endif
  138. GMouseEvent::Type type;
  139. switch (event.type) {
  140. case WSAPI_ServerMessage::Type::MouseMove:
  141. type = GEvent::MouseMove;
  142. break;
  143. case WSAPI_ServerMessage::Type::MouseUp:
  144. type = GEvent::MouseUp;
  145. break;
  146. case WSAPI_ServerMessage::Type::MouseDown:
  147. type = GEvent::MouseDown;
  148. break;
  149. case WSAPI_ServerMessage::Type::MouseDoubleClick:
  150. type = GEvent::MouseDoubleClick;
  151. break;
  152. case WSAPI_ServerMessage::Type::MouseWheel:
  153. type = GEvent::MouseWheel;
  154. break;
  155. default:
  156. ASSERT_NOT_REACHED();
  157. break;
  158. }
  159. GMouseButton button { GMouseButton::None };
  160. switch (event.mouse.button) {
  161. case WSAPI_MouseButton::NoButton:
  162. button = GMouseButton::None;
  163. break;
  164. case WSAPI_MouseButton::Left:
  165. button = GMouseButton::Left;
  166. break;
  167. case WSAPI_MouseButton::Right:
  168. button = GMouseButton::Right;
  169. break;
  170. case WSAPI_MouseButton::Middle:
  171. button = GMouseButton::Middle;
  172. break;
  173. default:
  174. ASSERT_NOT_REACHED();
  175. break;
  176. }
  177. post_event(window, make<GMouseEvent>(type, event.mouse.position, event.mouse.buttons, button, event.mouse.modifiers, event.mouse.wheel_delta));
  178. }
  179. void GEventLoop::handle_menu_event(const WSAPI_ServerMessage& event)
  180. {
  181. if (event.type == WSAPI_ServerMessage::Type::MenuItemActivated) {
  182. auto* menu = GMenu::from_menu_id(event.menu.menu_id);
  183. if (!menu) {
  184. dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
  185. return;
  186. }
  187. if (auto* action = menu->action_at(event.menu.identifier))
  188. action->activate();
  189. return;
  190. }
  191. ASSERT_NOT_REACHED();
  192. }
  193. void GEventLoop::handle_wm_event(const WSAPI_ServerMessage& event, GWindow& window)
  194. {
  195. #ifdef GEVENTLOOP_DEBUG
  196. dbgprintf("GEventLoop: handle_wm_event: %d\n", (int)event.type);
  197. #endif
  198. if (event.type == WSAPI_ServerMessage::WM_WindowStateChanged)
  199. 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));
  200. if (event.type == WSAPI_ServerMessage::WM_WindowRectChanged)
  201. return post_event(window, make<GWMWindowRectChangedEvent>(event.wm.client_id, event.wm.window_id, event.wm.rect));
  202. if (event.type == WSAPI_ServerMessage::WM_WindowIconChanged)
  203. return post_event(window, make<GWMWindowIconChangedEvent>(event.wm.client_id, event.wm.window_id, String(event.text, event.text_length)));
  204. if (event.type == WSAPI_ServerMessage::WM_WindowRemoved)
  205. return post_event(window, make<GWMWindowRemovedEvent>(event.wm.client_id, event.wm.window_id));
  206. ASSERT_NOT_REACHED();
  207. }
  208. void GEventLoop::process_unprocessed_bundles()
  209. {
  210. int coalesced_paints = 0;
  211. int coalesced_resizes = 0;
  212. auto unprocessed_bundles = move(m_unprocessed_bundles);
  213. HashMap<int, Size> latest_size_for_window_id;
  214. for (auto& bundle : unprocessed_bundles) {
  215. auto& event = bundle.message;
  216. if (event.type == WSAPI_ServerMessage::Type::WindowResized) {
  217. latest_size_for_window_id.set(event.window_id, event.window.rect.size);
  218. }
  219. }
  220. int paint_count = 0;
  221. HashMap<int, Size> latest_paint_size_for_window_id;
  222. for (auto& bundle : unprocessed_bundles) {
  223. auto& event = bundle.message;
  224. if (event.type == WSAPI_ServerMessage::Type::Paint) {
  225. ++paint_count;
  226. #ifdef COALESCING_DEBUG
  227. dbgprintf(" %s (window: %s)\n", Rect(event.paint.rect).to_string().characters(), Size(event.paint.window_size).to_string().characters());
  228. #endif
  229. latest_paint_size_for_window_id.set(event.window_id, event.paint.window_size);
  230. }
  231. }
  232. #ifdef COALESCING_DEBUG
  233. dbgprintf("paint_count: %d\n", paint_count);
  234. #endif
  235. for (auto& bundle : unprocessed_bundles) {
  236. auto& event = bundle.message;
  237. if (event.type == WSAPI_ServerMessage::Type::Greeting) {
  238. handle_greeting(event);
  239. continue;
  240. }
  241. if (event.type == WSAPI_ServerMessage::Type::ScreenRectChanged) {
  242. GDesktop::the().did_receive_screen_rect({}, event.screen.rect);
  243. continue;
  244. }
  245. if (event.type == WSAPI_ServerMessage::Error) {
  246. dbgprintf("GEventLoop got error message from server\n");
  247. dbgprintf(" - error message: %s\n", String(event.text, event.text_length).characters());
  248. quit(1);
  249. return;
  250. }
  251. switch (event.type) {
  252. case WSAPI_ServerMessage::MenuItemActivated:
  253. handle_menu_event(event);
  254. continue;
  255. default:
  256. break;
  257. }
  258. auto* window = GWindow::from_window_id(event.window_id);
  259. if (!window) {
  260. dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
  261. continue;
  262. }
  263. switch (event.type) {
  264. case WSAPI_ServerMessage::Type::Paint:
  265. if (Size(event.paint.window_size) != latest_paint_size_for_window_id.get(event.window_id)) {
  266. ++coalesced_paints;
  267. break;
  268. }
  269. handle_paint_event(event, *window, bundle.extra_data);
  270. break;
  271. case WSAPI_ServerMessage::Type::MouseDown:
  272. case WSAPI_ServerMessage::Type::MouseDoubleClick:
  273. case WSAPI_ServerMessage::Type::MouseUp:
  274. case WSAPI_ServerMessage::Type::MouseMove:
  275. case WSAPI_ServerMessage::Type::MouseWheel:
  276. handle_mouse_event(event, *window);
  277. break;
  278. case WSAPI_ServerMessage::Type::WindowActivated:
  279. case WSAPI_ServerMessage::Type::WindowDeactivated:
  280. handle_window_activation_event(event, *window);
  281. break;
  282. case WSAPI_ServerMessage::Type::WindowCloseRequest:
  283. handle_window_close_request_event(event, *window);
  284. break;
  285. case WSAPI_ServerMessage::Type::KeyDown:
  286. case WSAPI_ServerMessage::Type::KeyUp:
  287. handle_key_event(event, *window);
  288. break;
  289. case WSAPI_ServerMessage::Type::WindowEntered:
  290. case WSAPI_ServerMessage::Type::WindowLeft:
  291. handle_window_entered_or_left_event(event, *window);
  292. break;
  293. case WSAPI_ServerMessage::Type::WindowResized:
  294. if (Size(event.window.rect.size) != latest_size_for_window_id.get(event.window_id)) {
  295. ++coalesced_resizes;
  296. break;
  297. }
  298. handle_resize_event(event, *window);
  299. break;
  300. case WSAPI_ServerMessage::Type::WM_WindowRemoved:
  301. case WSAPI_ServerMessage::Type::WM_WindowStateChanged:
  302. case WSAPI_ServerMessage::Type::WM_WindowIconChanged:
  303. handle_wm_event(event, *window);
  304. break;
  305. default:
  306. break;
  307. }
  308. }
  309. #ifdef COALESCING_DEBUG
  310. if (coalesced_paints)
  311. dbgprintf("Coalesced %d paints\n", coalesced_paints);
  312. if (coalesced_resizes)
  313. dbgprintf("Coalesced %d resizes\n", coalesced_resizes);
  314. #endif
  315. }
  316. bool GEventLoop::drain_messages_from_server()
  317. {
  318. for (;;) {
  319. WSAPI_ServerMessage message;
  320. ssize_t nread = recv(s_windowserver_fd, &message, sizeof(WSAPI_ServerMessage), MSG_DONTWAIT);
  321. if (nread < 0) {
  322. if (errno == EAGAIN) {
  323. return true;
  324. }
  325. perror("read");
  326. quit(1);
  327. return false;
  328. }
  329. if (nread == 0) {
  330. fprintf(stderr, "EOF on WindowServer fd\n");
  331. quit(1);
  332. exit(-1);
  333. return false;
  334. }
  335. ASSERT(nread == sizeof(message));
  336. ByteBuffer extra_data;
  337. if (message.extra_size) {
  338. extra_data = ByteBuffer::create_uninitialized(message.extra_size);
  339. int extra_nread = read(s_windowserver_fd, extra_data.data(), extra_data.size());
  340. if (extra_nread < 0) {
  341. perror("read");
  342. ASSERT_NOT_REACHED();
  343. }
  344. ASSERT((size_t)extra_nread == message.extra_size);
  345. }
  346. m_unprocessed_bundles.append({ move(message), move(extra_data) });
  347. }
  348. }
  349. bool GEventLoop::post_message_to_server(const WSAPI_ClientMessage& message, const ByteBuffer& extra_data)
  350. {
  351. if (!extra_data.is_empty())
  352. const_cast<WSAPI_ClientMessage&>(message).extra_size = extra_data.size();
  353. struct iovec iov[2];
  354. int iov_count = 1;
  355. iov[0].iov_base = const_cast<WSAPI_ClientMessage*>(&message);
  356. iov[0].iov_len = sizeof(message);
  357. if (!extra_data.is_empty()) {
  358. iov[1].iov_base = const_cast<u8*>(extra_data.data());
  359. iov[1].iov_len = extra_data.size();
  360. ++iov_count;
  361. }
  362. int nwritten = writev(s_windowserver_fd, iov, iov_count);
  363. if (nwritten < 0) {
  364. perror("writev");
  365. ASSERT_NOT_REACHED();
  366. }
  367. ASSERT((size_t)nwritten == sizeof(message) + extra_data.size());
  368. return true;
  369. }
  370. bool GEventLoop::wait_for_specific_event(WSAPI_ServerMessage::Type type, WSAPI_ServerMessage& event)
  371. {
  372. for (;;) {
  373. fd_set rfds;
  374. FD_ZERO(&rfds);
  375. FD_SET(s_windowserver_fd, &rfds);
  376. int rc = select(s_windowserver_fd + 1, &rfds, nullptr, nullptr, nullptr);
  377. if (rc < 0) {
  378. perror("select");
  379. }
  380. ASSERT(rc > 0);
  381. ASSERT(FD_ISSET(s_windowserver_fd, &rfds));
  382. bool success = drain_messages_from_server();
  383. if (!success)
  384. return false;
  385. for (ssize_t i = 0; i < m_unprocessed_bundles.size(); ++i) {
  386. if (m_unprocessed_bundles[i].message.type == type) {
  387. event = move(m_unprocessed_bundles[i].message);
  388. m_unprocessed_bundles.remove(i);
  389. return true;
  390. }
  391. }
  392. }
  393. }
  394. WSAPI_ServerMessage GEventLoop::sync_request(const WSAPI_ClientMessage& request, WSAPI_ServerMessage::Type response_type)
  395. {
  396. bool success = post_message_to_server(request);
  397. ASSERT(success);
  398. WSAPI_ServerMessage response;
  399. success = wait_for_specific_event(response_type, response);
  400. ASSERT(success);
  401. return response;
  402. }
  403. void GEventLoop::handle_greeting(WSAPI_ServerMessage& message)
  404. {
  405. s_server_pid = message.greeting.server_pid;
  406. s_my_client_id = message.greeting.your_client_id;
  407. GDesktop::the().did_receive_screen_rect({}, message.greeting.screen_rect);
  408. }