| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 22:29:06 +01:00
										 |  |  | #include <AK/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-06 15:48:02 -06:00
										 |  |  | #include <AK/Function.h>
 | 
					
						
							|  |  |  | #include <AK/HashMap.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-15 02:09:00 +01:00
										 |  |  | #include <AK/Noncopyable.h>
 | 
					
						
							|  |  |  | #include <AK/NonnullOwnPtr.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | #include <AK/Vector.h>
 | 
					
						
							|  |  |  | #include <AK/WeakPtr.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-14 22:29:06 +01:00
										 |  |  | #include <LibCore/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:48:06 +02:00
										 |  |  | #include <sys/time.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-06 15:48:02 -06:00
										 |  |  | #include <sys/types.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | namespace Core { | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | class EventLoop { | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-05-13 22:42:11 +02:00
										 |  |  |     enum class MakeInspectable { | 
					
						
							|  |  |  |         No, | 
					
						
							|  |  |  |         Yes, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 23:26:51 +02:00
										 |  |  |     explicit EventLoop(MakeInspectable = MakeInspectable::No); | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     ~EventLoop(); | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     void post_event(Object& receiver, NonnullOwnPtr<Event>&&); | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     static EventLoop& main(); | 
					
						
							|  |  |  |     static EventLoop& current(); | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     static int register_timer(Object&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible); | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  |     static bool unregister_timer(int timer_id); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     static void register_notifier(Badge<Notifier>, Notifier&); | 
					
						
							|  |  |  |     static void unregister_notifier(Badge<Notifier>, Notifier&); | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void quit(int); | 
					
						
							| 
									
										
										
										
											2019-10-25 00:18:31 -05:00
										 |  |  |     void unquit(); | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     void take_pending_events_from(EventLoop& 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(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 15:48:02 -06:00
										 |  |  |     static int register_signal(int signo, Function<void(int)> handler); | 
					
						
							|  |  |  |     static void unregister_signal(int handler_id); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 22:44:42 +04:30
										 |  |  |     // Note: Boost uses Parent/Child/Prepare, but we don't really have anything
 | 
					
						
							|  |  |  |     //       interesting to do in the parent or before forking.
 | 
					
						
							|  |  |  |     enum class ForkEvent { | 
					
						
							|  |  |  |         Child, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     static void notify_forked(ForkEvent); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2019-05-18 13:39:21 +02:00
										 |  |  |     void wait_for_event(WaitMode); | 
					
						
							| 
									
										
										
										
											2020-05-15 18:21:40 +03:00
										 |  |  |     Optional<struct timeval> get_next_timer_expiration(); | 
					
						
							| 
									
										
										
										
											2020-07-06 15:48:02 -06:00
										 |  |  |     static void dispatch_signal(int); | 
					
						
							|  |  |  |     static void handle_signal(int); | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     struct QueuedEvent { | 
					
						
							| 
									
										
										
										
											2020-02-14 22:29:06 +01:00
										 |  |  |         AK_MAKE_NONCOPYABLE(QueuedEvent); | 
					
						
							| 
									
										
										
										
											2020-02-15 02:09:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 22:29:06 +01:00
										 |  |  |     public: | 
					
						
							|  |  |  |         QueuedEvent(Object& receiver, NonnullOwnPtr<Event>); | 
					
						
							|  |  |  |         QueuedEvent(QueuedEvent&&); | 
					
						
							|  |  |  |         ~QueuedEvent(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |         WeakPtr<Object> receiver; | 
					
						
							|  |  |  |         NonnullOwnPtr<Event> 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; | 
					
						
							| 
									
										
										
										
											2020-07-06 15:48:02 -06:00
										 |  |  |     static pid_t s_pid; | 
					
						
							| 
									
										
										
										
											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]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 02:09:00 +01:00
										 |  |  |     struct Private; | 
					
						
							|  |  |  |     NonnullOwnPtr<Private> m_private; | 
					
						
							| 
									
										
										
										
											2019-04-10 17:30:34 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |