GEventLoop.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #include "GEventLoop.h"
  2. #include "GEvent.h"
  3. #include "GObject.h"
  4. #include "GWindow.h"
  5. #include <LibGUI/GNotifier.h>
  6. #include <LibGUI/GMenu.h>
  7. #include <LibC/unistd.h>
  8. #include <LibC/stdio.h>
  9. #include <LibC/fcntl.h>
  10. #include <LibC/string.h>
  11. #include <LibC/time.h>
  12. #include <LibC/sys/select.h>
  13. #include <LibC/gui.h>
  14. //#define GEVENTLOOP_DEBUG
  15. static GEventLoop* s_mainGEventLoop;
  16. void GEventLoop::initialize()
  17. {
  18. s_mainGEventLoop = nullptr;
  19. }
  20. GEventLoop::GEventLoop()
  21. {
  22. if (!s_mainGEventLoop)
  23. s_mainGEventLoop = this;
  24. }
  25. GEventLoop::~GEventLoop()
  26. {
  27. }
  28. GEventLoop& GEventLoop::main()
  29. {
  30. ASSERT(s_mainGEventLoop);
  31. return *s_mainGEventLoop;
  32. }
  33. void GEventLoop::exit(int code)
  34. {
  35. m_exit_requested = true;
  36. m_exit_code = code;
  37. }
  38. int GEventLoop::exec()
  39. {
  40. m_event_fd = open("/dev/gui_events", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
  41. if (m_event_fd < 0) {
  42. perror("GEventLoop::exec(): open");
  43. exit(1);
  44. }
  45. m_running = true;
  46. for (;;) {
  47. if (m_exit_requested)
  48. return m_exit_code;
  49. if (m_queued_events.is_empty())
  50. wait_for_event();
  51. Vector<QueuedEvent> events = move(m_queued_events);
  52. for (auto& queued_event : events) {
  53. auto* receiver = queued_event.receiver;
  54. auto& event = *queued_event.event;
  55. #ifdef GEVENTLOOP_DEBUG
  56. dbgprintf("GEventLoop: %s{%p} event %u\n", receiver->class_name(), receiver, (unsigned)event.type());
  57. #endif
  58. if (!receiver) {
  59. switch (event.type()) {
  60. case GEvent::Quit:
  61. ASSERT_NOT_REACHED();
  62. return 0;
  63. default:
  64. dbgprintf("event type %u with no receiver :(\n", event.type());
  65. ASSERT_NOT_REACHED();
  66. return 1;
  67. }
  68. } else {
  69. receiver->event(event);
  70. }
  71. }
  72. }
  73. ASSERT_NOT_REACHED();
  74. }
  75. void GEventLoop::post_event(GObject* receiver, OwnPtr<GEvent>&& event)
  76. {
  77. #ifdef GEVENTLOOP_DEBUG
  78. dbgprintf("GEventLoop::post_event: {%u} << receiver=%p, event=%p\n", m_queued_events.size(), receiver, event.ptr());
  79. #endif
  80. m_queued_events.append({ receiver, move(event) });
  81. }
  82. void GEventLoop::handle_paint_event(const GUI_Event& event, GWindow& window)
  83. {
  84. #ifdef GEVENTLOOP_DEBUG
  85. 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);
  86. #endif
  87. post_event(&window, make<GPaintEvent>(event.paint.rect));
  88. }
  89. void GEventLoop::handle_window_activation_event(const GUI_Event& event, GWindow& window)
  90. {
  91. #ifdef GEVENTLOOP_DEBUG
  92. dbgprintf("WID=%x WindowActivation\n", event.window_id);
  93. #endif
  94. post_event(&window, make<GEvent>(event.type == GUI_Event::Type::WindowActivated ? GEvent::WindowBecameActive : GEvent::WindowBecameInactive));
  95. }
  96. void GEventLoop::handle_window_close_request_event(const GUI_Event&, GWindow& window)
  97. {
  98. post_event(&window, make<GEvent>(GEvent::WindowCloseRequest));
  99. }
  100. void GEventLoop::handle_key_event(const GUI_Event& event, GWindow& window)
  101. {
  102. #ifdef GEVENTLOOP_DEBUG
  103. dbgprintf("WID=%x KeyEvent character=0x%b\n", event.window_id, event.key.character);
  104. #endif
  105. auto key_event = make<GKeyEvent>(event.type == GUI_Event::Type::KeyDown ? GEvent::KeyDown : GEvent::KeyUp, event.key.key);
  106. key_event->m_alt = event.key.alt;
  107. key_event->m_ctrl = event.key.ctrl;
  108. key_event->m_shift = event.key.shift;
  109. if (event.key.character != '\0')
  110. key_event->m_text = String(&event.key.character, 1);
  111. post_event(&window, move(key_event));
  112. }
  113. void GEventLoop::handle_mouse_event(const GUI_Event& event, GWindow& window)
  114. {
  115. #ifdef GEVENTLOOP_DEBUG
  116. dbgprintf("WID=%x MouseEvent %d,%d\n", event.window_id, event.mouse.position.x, event.mouse.position.y);
  117. #endif
  118. GMouseEvent::Type type;
  119. switch (event.type) {
  120. case GUI_Event::Type::MouseMove: type = GEvent::MouseMove; break;
  121. case GUI_Event::Type::MouseUp: type = GEvent::MouseUp; break;
  122. case GUI_Event::Type::MouseDown: type = GEvent::MouseDown; break;
  123. default: ASSERT_NOT_REACHED(); break;
  124. }
  125. GMouseButton button { GMouseButton::None };
  126. switch (event.mouse.button) {
  127. case GUI_MouseButton::NoButton: button = GMouseButton::None; break;
  128. case GUI_MouseButton::Left: button = GMouseButton::Left; break;
  129. case GUI_MouseButton::Right: button = GMouseButton::Right; break;
  130. case GUI_MouseButton::Middle: button = GMouseButton::Middle; break;
  131. default: ASSERT_NOT_REACHED(); break;
  132. }
  133. post_event(&window, make<GMouseEvent>(type, event.mouse.position, event.mouse.buttons, button));
  134. }
  135. void GEventLoop::handle_menu_event(const GUI_Event& event)
  136. {
  137. if (event.type == GUI_Event::Type::MenuItemActivated) {
  138. auto* menu = GMenu::from_menu_id(event.menu.menu_id);
  139. if (!menu) {
  140. dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
  141. return;
  142. }
  143. if (menu->on_item_activation)
  144. menu->on_item_activation(event.menu.identifier);
  145. return;
  146. }
  147. ASSERT_NOT_REACHED();
  148. }
  149. void GEventLoop::wait_for_event()
  150. {
  151. fd_set rfds;
  152. fd_set wfds;
  153. FD_ZERO(&rfds);
  154. FD_ZERO(&wfds);
  155. int max_fd = 0;
  156. auto add_fd_to_set = [&max_fd] (int fd, fd_set& set){
  157. FD_SET(fd, &set);
  158. if (fd > max_fd)
  159. max_fd = fd;
  160. };
  161. add_fd_to_set(m_event_fd, rfds);
  162. for (auto& notifier : m_notifiers) {
  163. if (notifier->event_mask() & GNotifier::Read)
  164. add_fd_to_set(notifier->fd(), rfds);
  165. if (notifier->event_mask() & GNotifier::Write)
  166. add_fd_to_set(notifier->fd(), wfds);
  167. if (notifier->event_mask() & GNotifier::Exceptional)
  168. ASSERT_NOT_REACHED();
  169. }
  170. struct timeval timeout = { 0, 0 };
  171. if (!m_timers.is_empty())
  172. get_next_timer_expiration(timeout);
  173. int rc = select(m_event_fd + 1, &rfds, &wfds, nullptr, (m_queued_events.is_empty() && m_timers.is_empty()) ? nullptr : &timeout);
  174. if (rc < 0) {
  175. ASSERT_NOT_REACHED();
  176. }
  177. for (auto& it : m_timers) {
  178. auto& timer = *it.value;
  179. if (!timer.has_expired())
  180. continue;
  181. #ifdef GEVENTLOOP_DEBUG
  182. dbgprintf("GEventLoop: Timer %d has expired, sending GTimerEvent to %p\n", timer.timer_id, timer.owner);
  183. #endif
  184. post_event(timer.owner, make<GTimerEvent>(timer.timer_id));
  185. if (timer.should_reload) {
  186. timer.reload();
  187. } else {
  188. // FIXME: Support removing expired timers that don't want to reload.
  189. ASSERT_NOT_REACHED();
  190. }
  191. }
  192. for (auto& notifier : m_notifiers) {
  193. if (FD_ISSET(notifier->fd(), &rfds)) {
  194. if (notifier->on_ready_to_read)
  195. notifier->on_ready_to_read(*notifier);
  196. }
  197. if (FD_ISSET(notifier->fd(), &wfds)) {
  198. if (notifier->on_ready_to_write)
  199. notifier->on_ready_to_write(*notifier);
  200. }
  201. }
  202. if (!FD_ISSET(m_event_fd, &rfds))
  203. return;
  204. for (;;) {
  205. GUI_Event event;
  206. ssize_t nread = read(m_event_fd, &event, sizeof(GUI_Event));
  207. if (nread < 0) {
  208. perror("read");
  209. exit(1); // FIXME: This should cause EventLoop::exec() to return 1.
  210. }
  211. if (nread == 0)
  212. break;
  213. assert(nread == sizeof(event));
  214. switch (event.type) {
  215. case GUI_Event::MenuItemActivated:
  216. handle_menu_event(event);
  217. continue;
  218. }
  219. auto* window = GWindow::from_window_id(event.window_id);
  220. if (!window) {
  221. dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
  222. continue;
  223. }
  224. switch (event.type) {
  225. case GUI_Event::Type::Paint:
  226. handle_paint_event(event, *window);
  227. break;
  228. case GUI_Event::Type::MouseDown:
  229. case GUI_Event::Type::MouseUp:
  230. case GUI_Event::Type::MouseMove:
  231. handle_mouse_event(event, *window);
  232. break;
  233. case GUI_Event::Type::WindowActivated:
  234. case GUI_Event::Type::WindowDeactivated:
  235. handle_window_activation_event(event, *window);
  236. break;
  237. case GUI_Event::Type::WindowCloseRequest:
  238. handle_window_close_request_event(event, *window);
  239. break;
  240. case GUI_Event::Type::KeyDown:
  241. case GUI_Event::Type::KeyUp:
  242. handle_key_event(event, *window);
  243. break;
  244. default:
  245. break;
  246. }
  247. }
  248. }
  249. bool GEventLoop::EventLoopTimer::has_expired() const
  250. {
  251. timeval now;
  252. gettimeofday(&now, nullptr);
  253. return now.tv_sec > fire_time.tv_sec || (now.tv_sec == fire_time.tv_sec && now.tv_usec >= fire_time.tv_usec);
  254. }
  255. void GEventLoop::EventLoopTimer::reload()
  256. {
  257. gettimeofday(&fire_time, nullptr);
  258. fire_time.tv_sec += interval / 1000;
  259. fire_time.tv_usec += interval % 1000;
  260. }
  261. void GEventLoop::get_next_timer_expiration(timeval& soonest)
  262. {
  263. ASSERT(!m_timers.is_empty());
  264. bool has_checked_any = false;
  265. for (auto& it : m_timers) {
  266. auto& fire_time = it.value->fire_time;
  267. if (!has_checked_any || fire_time.tv_sec < soonest.tv_sec || (fire_time.tv_sec == soonest.tv_sec && fire_time.tv_usec < soonest.tv_usec))
  268. soonest = fire_time;
  269. has_checked_any = true;
  270. }
  271. }
  272. int GEventLoop::register_timer(GObject& object, int milliseconds, bool should_reload)
  273. {
  274. ASSERT(milliseconds >= 0);
  275. auto timer = make<EventLoopTimer>();
  276. timer->owner = &object;
  277. timer->interval = milliseconds;
  278. timer->reload();
  279. timer->should_reload = should_reload;
  280. int timer_id = ++m_next_timer_id; // FIXME: This will eventually wrap around.
  281. ASSERT(timer_id); // FIXME: Aforementioned wraparound.
  282. timer->timer_id = timer_id;
  283. m_timers.set(timer->timer_id, move(timer));
  284. return timer_id;
  285. }
  286. bool GEventLoop::unregister_timer(int timer_id)
  287. {
  288. auto it = m_timers.find(timer_id);
  289. if (it == m_timers.end())
  290. return false;
  291. m_timers.remove(it);
  292. return true;
  293. }
  294. void GEventLoop::register_notifier(Badge<GNotifier>, GNotifier& notifier)
  295. {
  296. m_notifiers.set(&notifier);
  297. }
  298. void GEventLoop::unregister_notifier(Badge<GNotifier>, GNotifier& notifier)
  299. {
  300. m_notifiers.remove(&notifier);
  301. }