| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | #include "GEventLoop.h"
 | 
					
						
							|  |  |  | #include "GEvent.h"
 | 
					
						
							|  |  |  | #include "GObject.h"
 | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  | #include "GWindow.h"
 | 
					
						
							|  |  |  | #include <LibC/unistd.h>
 | 
					
						
							|  |  |  | #include <LibC/stdio.h>
 | 
					
						
							|  |  |  | #include <LibC/fcntl.h>
 | 
					
						
							|  |  |  | #include <LibC/string.h>
 | 
					
						
							|  |  |  | #include <LibC/sys/select.h>
 | 
					
						
							|  |  |  | #include <LibC/gui.h>
 | 
					
						
							| 
									
										
										
										
											2018-10-10 15:12:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | static GEventLoop* s_mainGEventLoop; | 
					
						
							| 
									
										
										
										
											2018-10-10 15:12:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | void GEventLoop::initialize() | 
					
						
							| 
									
										
										
										
											2019-01-10 23:19:29 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     s_mainGEventLoop = nullptr; | 
					
						
							| 
									
										
										
										
											2019-01-10 23:19:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | GEventLoop::GEventLoop() | 
					
						
							| 
									
										
										
										
											2018-10-10 15:12:38 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     if (!s_mainGEventLoop) | 
					
						
							|  |  |  |         s_mainGEventLoop = this; | 
					
						
							| 
									
										
										
										
											2018-10-10 15:12:38 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | GEventLoop::~GEventLoop() | 
					
						
							| 
									
										
										
										
											2018-10-10 15:12:38 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | GEventLoop& GEventLoop::main() | 
					
						
							| 
									
										
										
										
											2018-10-10 15:12:38 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     ASSERT(s_mainGEventLoop); | 
					
						
							|  |  |  |     return *s_mainGEventLoop; | 
					
						
							| 
									
										
										
										
											2018-10-10 15:12:38 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | int GEventLoop::exec() | 
					
						
							| 
									
										
										
										
											2018-10-10 15:12:38 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  |     m_event_fd = open("/dev/gui_events", O_RDONLY); | 
					
						
							|  |  |  |     if (m_event_fd < 0) { | 
					
						
							|  |  |  |         perror("GEventLoop::exec(): open"); | 
					
						
							|  |  |  |         exit(1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 01:59:38 +01:00
										 |  |  |     m_running = true; | 
					
						
							| 
									
										
										
										
											2018-10-10 15:12:38 +02:00
										 |  |  |     for (;;) { | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  |         if (m_queued_events.is_empty()) | 
					
						
							|  |  |  |             wait_for_event(); | 
					
						
							| 
									
										
										
										
											2019-01-12 23:23:35 +01:00
										 |  |  |         Vector<QueuedEvent> events; | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  |             events = move(m_queued_events); | 
					
						
							| 
									
										
										
										
											2019-01-12 23:23:35 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-10-10 15:12:38 +02:00
										 |  |  |         for (auto& queuedEvent : events) { | 
					
						
							|  |  |  |             auto* receiver = queuedEvent.receiver; | 
					
						
							|  |  |  |             auto& event = *queuedEvent.event; | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |             //printf("GEventLoop: GObject{%p} event %u (%s)\n", receiver, (unsigned)event.type(), event.name());
 | 
					
						
							| 
									
										
										
										
											2018-10-10 15:12:38 +02:00
										 |  |  |             if (!receiver) { | 
					
						
							|  |  |  |                 switch (event.type()) { | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |                 case GEvent::Quit: | 
					
						
							| 
									
										
										
										
											2019-01-13 01:59:38 +01:00
										 |  |  |                     ASSERT_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2018-10-10 15:12:38 +02:00
										 |  |  |                     return 0; | 
					
						
							|  |  |  |                 default: | 
					
						
							| 
									
										
										
										
											2019-01-19 23:49:56 +01:00
										 |  |  |                     dbgprintf("event type %u with no receiver :(\n", event.type()); | 
					
						
							| 
									
										
										
										
											2019-01-13 01:59:38 +01:00
										 |  |  |                     ASSERT_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2018-10-10 15:12:38 +02:00
										 |  |  |                     return 1; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 receiver->event(event); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  | void GEventLoop::post_event(GObject* receiver, OwnPtr<GEvent>&& event) | 
					
						
							| 
									
										
										
										
											2018-10-10 15:12:38 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     //printf("GEventLoop::postGEvent: {%u} << receiver=%p, event=%p\n", m_queuedEvents.size(), receiver, event.ptr());
 | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  |     m_queued_events.append({ receiver, move(event) }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GEventLoop::handle_paint_event(const GUI_Event& event, GWindow& window) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     post_event(&window, make<GPaintEvent>(event.paint.rect)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GEventLoop::handle_mouse_event(const GUI_Event& event, GWindow& window) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     GMouseEvent::Type type; | 
					
						
							|  |  |  |     switch (event.type) { | 
					
						
							|  |  |  |     case GUI_Event::Type::MouseMove: type = GEvent::MouseMove; break; | 
					
						
							|  |  |  |     case GUI_Event::Type::MouseUp: type = GEvent::MouseUp; break; | 
					
						
							|  |  |  |     case GUI_Event::Type::MouseDown: type = GEvent::MouseDown; break; | 
					
						
							|  |  |  |     default: ASSERT_NOT_REACHED(); break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     GMouseButton button { GMouseButton::None }; | 
					
						
							|  |  |  |     switch (event.mouse.button) { | 
					
						
							|  |  |  |     case GUI_MouseButton::NoButton: button = GMouseButton::None; break; | 
					
						
							|  |  |  |     case GUI_MouseButton::Left: button = GMouseButton::Left; break; | 
					
						
							|  |  |  |     case GUI_MouseButton::Right: button = GMouseButton::Right; break; | 
					
						
							|  |  |  |     case GUI_MouseButton::Middle: button = GMouseButton::Middle; break; | 
					
						
							|  |  |  |     default: ASSERT_NOT_REACHED(); break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     auto mouse_event = make<GMouseEvent>(type, event.mouse.position, button); | 
					
						
							| 
									
										
										
										
											2019-01-10 23:19:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  | void GEventLoop::wait_for_event() | 
					
						
							| 
									
										
										
										
											2019-01-10 23:19:29 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  |     fd_set rfds; | 
					
						
							|  |  |  |     FD_ZERO(&rfds); | 
					
						
							|  |  |  |     FD_SET(m_event_fd, &rfds); | 
					
						
							|  |  |  |     int rc = select(m_event_fd + 1, &rfds, nullptr, nullptr, nullptr); | 
					
						
							|  |  |  |     if (rc < 0) { | 
					
						
							|  |  |  |         ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!FD_ISSET(m_event_fd, &rfds)) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (;;) { | 
					
						
							|  |  |  |         GUI_Event event; | 
					
						
							|  |  |  |         ssize_t nread = read(m_event_fd, &event, sizeof(GUI_Event)); | 
					
						
							|  |  |  |         if (nread < 0) { | 
					
						
							|  |  |  |             perror("read"); | 
					
						
							|  |  |  |             exit(1); // FIXME: This should cause EventLoop::exec() to return 1.
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (nread == 0) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         assert(nread == sizeof(event)); | 
					
						
							|  |  |  |         auto* window = GWindow::from_window_id(event.window_id); | 
					
						
							|  |  |  |         if (!window) { | 
					
						
							|  |  |  |             dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         switch (event.type) { | 
					
						
							|  |  |  |         case GUI_Event::Type::Paint: | 
					
						
							|  |  |  |             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); break; | 
					
						
							|  |  |  |             handle_paint_event(event, *window); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case GUI_Event::Type::MouseDown: | 
					
						
							|  |  |  |         case GUI_Event::Type::MouseUp: | 
					
						
							|  |  |  |         case GUI_Event::Type::MouseMove: | 
					
						
							|  |  |  |             dbgprintf("WID=%x MouseEvent %d,%d\n", event.window_id, event.mouse.position.x, event.mouse.position.y); | 
					
						
							|  |  |  |             handle_mouse_event(event, *window); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case GUI_Event::Type::WindowActivated: | 
					
						
							|  |  |  |             dbgprintf("WID=%x WindowActivated\n", event.window_id); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case GUI_Event::Type::WindowDeactivated: | 
					
						
							|  |  |  |             dbgprintf("WID=%x WindowDeactivated\n", event.window_id); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-10 15:12:38 +02:00
										 |  |  | } |