GEventLoop.cpp 15 KB

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