GEventLoop.cpp 10 KB

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