EventLoop.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Forward.h>
  8. #include <AK/Function.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/Noncopyable.h>
  11. #include <AK/NonnullOwnPtr.h>
  12. #include <AK/Time.h>
  13. #include <AK/Vector.h>
  14. #include <AK/WeakPtr.h>
  15. #include <LibCore/Forward.h>
  16. #include <sys/time.h>
  17. #include <sys/types.h>
  18. namespace Core {
  19. class EventLoop {
  20. public:
  21. enum class MakeInspectable {
  22. No,
  23. Yes,
  24. };
  25. explicit EventLoop(MakeInspectable = MakeInspectable::No);
  26. ~EventLoop();
  27. int exec();
  28. enum class WaitMode {
  29. WaitForEvents,
  30. PollForEvents,
  31. };
  32. // processe events, generally called by exec() in a loop.
  33. // this should really only be used for integrating with other event loops
  34. void pump(WaitMode = WaitMode::WaitForEvents);
  35. void post_event(Object& receiver, NonnullOwnPtr<Event>&&);
  36. static EventLoop& main();
  37. static EventLoop& current();
  38. bool was_exit_requested() const { return m_exit_requested; }
  39. static int register_timer(Object&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible);
  40. static bool unregister_timer(int timer_id);
  41. static void register_notifier(Badge<Notifier>, Notifier&);
  42. static void unregister_notifier(Badge<Notifier>, Notifier&);
  43. void quit(int);
  44. void unquit();
  45. void take_pending_events_from(EventLoop& other)
  46. {
  47. m_queued_events.extend(move(other.m_queued_events));
  48. }
  49. static void wake();
  50. static int register_signal(int signo, Function<void(int)> handler);
  51. static void unregister_signal(int handler_id);
  52. // Note: Boost uses Parent/Child/Prepare, but we don't really have anything
  53. // interesting to do in the parent or before forking.
  54. enum class ForkEvent {
  55. Child,
  56. };
  57. static void notify_forked(ForkEvent);
  58. static bool has_been_instantiated();
  59. private:
  60. void wait_for_event(WaitMode);
  61. Optional<Time> get_next_timer_expiration();
  62. static void dispatch_signal(int);
  63. static void handle_signal(int);
  64. struct QueuedEvent {
  65. AK_MAKE_NONCOPYABLE(QueuedEvent);
  66. public:
  67. QueuedEvent(Object& receiver, NonnullOwnPtr<Event>);
  68. QueuedEvent(QueuedEvent&&);
  69. ~QueuedEvent();
  70. WeakPtr<Object> receiver;
  71. NonnullOwnPtr<Event> event;
  72. };
  73. Vector<QueuedEvent, 64> m_queued_events;
  74. static pid_t s_pid;
  75. bool m_exit_requested { false };
  76. int m_exit_code { 0 };
  77. static int s_wake_pipe_fds[2];
  78. struct Private;
  79. NonnullOwnPtr<Private> m_private;
  80. };
  81. }