EventLoop.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Forward.h>
  28. #include <AK/Noncopyable.h>
  29. #include <AK/NonnullOwnPtr.h>
  30. #include <AK/Vector.h>
  31. #include <AK/WeakPtr.h>
  32. #include <LibCore/Forward.h>
  33. #include <sys/time.h>
  34. namespace Core {
  35. class EventLoop {
  36. public:
  37. EventLoop();
  38. ~EventLoop();
  39. int exec();
  40. enum class WaitMode {
  41. WaitForEvents,
  42. PollForEvents,
  43. };
  44. // processe events, generally called by exec() in a loop.
  45. // this should really only be used for integrating with other event loops
  46. void pump(WaitMode = WaitMode::WaitForEvents);
  47. void post_event(Object& receiver, NonnullOwnPtr<Event>&&);
  48. static EventLoop& main();
  49. static EventLoop& current();
  50. bool was_exit_requested() const { return m_exit_requested; }
  51. static int register_timer(Object&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible);
  52. static bool unregister_timer(int timer_id);
  53. static void register_notifier(Badge<Notifier>, Notifier&);
  54. static void unregister_notifier(Badge<Notifier>, Notifier&);
  55. void quit(int);
  56. void unquit();
  57. void take_pending_events_from(EventLoop& other)
  58. {
  59. m_queued_events.append(move(other.m_queued_events));
  60. }
  61. static void wake();
  62. private:
  63. void wait_for_event(WaitMode);
  64. Optional<struct timeval> get_next_timer_expiration();
  65. struct QueuedEvent {
  66. AK_MAKE_NONCOPYABLE(QueuedEvent);
  67. public:
  68. QueuedEvent(Object& receiver, NonnullOwnPtr<Event>);
  69. QueuedEvent(QueuedEvent&&);
  70. ~QueuedEvent();
  71. WeakPtr<Object> receiver;
  72. NonnullOwnPtr<Event> event;
  73. };
  74. Vector<QueuedEvent, 64> m_queued_events;
  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. }