| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Badge.h>
 | 
					
						
							|  |  |  | #include <AK/HashMap.h>
 | 
					
						
							|  |  |  | #include <AK/OwnPtr.h>
 | 
					
						
							|  |  |  | #include <AK/Vector.h>
 | 
					
						
							|  |  |  | #include <AK/WeakPtr.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-13 19:42:03 +02:00
										 |  |  | #include <LibCore/CEvent.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-17 11:35:09 +02:00
										 |  |  | #include <LibCore/CLocalServer.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  | #include <LibThread/Lock.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | #include <sys/select.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:48:06 +02:00
										 |  |  | #include <sys/time.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | #include <time.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class CObject; | 
					
						
							| 
									
										
										
										
											2019-04-10 17:35:43 +02:00
										 |  |  | class CNotifier; | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | class CEventLoop { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     CEventLoop(); | 
					
						
							|  |  |  |     virtual ~CEventLoop(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int exec(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-07 17:13:23 +02:00
										 |  |  |     enum class WaitMode { | 
					
						
							| 
									
										
										
										
											2019-05-18 13:39:21 +02:00
										 |  |  |         WaitForEvents, | 
					
						
							|  |  |  |         PollForEvents, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // processe events, generally called by exec() in a loop.
 | 
					
						
							|  |  |  |     // this should really only be used for integrating with other event loops
 | 
					
						
							|  |  |  |     void pump(WaitMode = WaitMode::WaitForEvents); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 08:38:00 +02:00
										 |  |  |     void post_event(CObject& receiver, NonnullOwnPtr<CEvent>&&); | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     static CEventLoop& main(); | 
					
						
							|  |  |  |     static CEventLoop& current(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-26 17:53:03 +02:00
										 |  |  |     bool was_exit_requested() const { return m_exit_requested; } | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     static int register_timer(CObject&, int milliseconds, bool should_reload); | 
					
						
							|  |  |  |     static bool unregister_timer(int timer_id); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 17:35:43 +02:00
										 |  |  |     static void register_notifier(Badge<CNotifier>, CNotifier&); | 
					
						
							|  |  |  |     static void unregister_notifier(Badge<CNotifier>, CNotifier&); | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void quit(int); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-02 13:50:16 +02:00
										 |  |  |     void take_pending_events_from(CEventLoop& other) | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         m_queued_events.append(move(other.m_queued_events)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-14 10:20:57 +02:00
										 |  |  |     static void wake(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2019-05-18 13:39:21 +02:00
										 |  |  |     void wait_for_event(WaitMode); | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  |     void get_next_timer_expiration(timeval&); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct QueuedEvent { | 
					
						
							|  |  |  |         WeakPtr<CObject> receiver; | 
					
						
							| 
									
										
										
										
											2019-07-24 08:38:00 +02:00
										 |  |  |         NonnullOwnPtr<CEvent> event; | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-05-18 13:39:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-20 14:02:19 +02:00
										 |  |  |     Vector<QueuedEvent, 64> m_queued_events; | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool m_exit_requested { false }; | 
					
						
							|  |  |  |     int m_exit_code { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-14 10:20:57 +02:00
										 |  |  |     static int s_wake_pipe_fds[2]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  |     LibThread::Lock m_lock; | 
					
						
							| 
									
										
										
										
											2019-04-29 15:57:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  |     struct EventLoopTimer { | 
					
						
							|  |  |  |         int timer_id { 0 }; | 
					
						
							|  |  |  |         int interval { 0 }; | 
					
						
							| 
									
										
										
										
											2019-08-01 10:49:31 +02:00
										 |  |  |         timeval fire_time { 0, 0 }; | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  |         bool should_reload { false }; | 
					
						
							|  |  |  |         WeakPtr<CObject> owner; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 01:37:23 +02:00
										 |  |  |         void reload(const timeval& now); | 
					
						
							|  |  |  |         bool has_expired(const timeval& now) const; | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 08:42:55 +02:00
										 |  |  |     static HashMap<int, NonnullOwnPtr<EventLoopTimer>>* s_timers; | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  |     static int s_next_timer_id; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 17:35:43 +02:00
										 |  |  |     static HashTable<CNotifier*>* s_notifiers; | 
					
						
							| 
									
										
										
										
											2019-08-17 11:35:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 10:46:55 +02:00
										 |  |  |     static ObjectPtr<CLocalServer> s_rpc_server; | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | }; |