Event.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Function.h>
  9. #include <AK/Types.h>
  10. #include <AK/WeakPtr.h>
  11. #include <LibCore/DeferredInvocationContext.h>
  12. #include <LibCore/Forward.h>
  13. namespace Core {
  14. class Event {
  15. public:
  16. enum Type {
  17. Invalid = 0,
  18. Quit,
  19. Timer,
  20. NotifierActivation,
  21. DeferredInvoke,
  22. ChildAdded,
  23. ChildRemoved,
  24. Custom,
  25. };
  26. Event() = default;
  27. explicit Event(unsigned type)
  28. : m_type(type)
  29. {
  30. }
  31. virtual ~Event() = default;
  32. unsigned type() const { return m_type; }
  33. bool is_accepted() const { return m_accepted; }
  34. void accept() { m_accepted = true; }
  35. void ignore() { m_accepted = false; }
  36. private:
  37. unsigned m_type { Type::Invalid };
  38. bool m_accepted { true };
  39. };
  40. class DeferredInvocationEvent : public Event {
  41. friend class EventLoop;
  42. friend class ThreadEventQueue;
  43. public:
  44. DeferredInvocationEvent(NonnullRefPtr<DeferredInvocationContext> context, Function<void()> invokee)
  45. : Event(Event::Type::DeferredInvoke)
  46. , m_context(move(context))
  47. , m_invokee(move(invokee))
  48. {
  49. }
  50. private:
  51. NonnullRefPtr<DeferredInvocationContext> m_context;
  52. Function<void()> m_invokee;
  53. };
  54. class TimerEvent final : public Event {
  55. public:
  56. explicit TimerEvent(int timer_id)
  57. : Event(Event::Timer)
  58. , m_timer_id(timer_id)
  59. {
  60. }
  61. ~TimerEvent() = default;
  62. int timer_id() const { return m_timer_id; }
  63. private:
  64. int m_timer_id;
  65. };
  66. class NotifierActivationEvent final : public Event {
  67. public:
  68. explicit NotifierActivationEvent(int fd)
  69. : Event(Event::NotifierActivation)
  70. , m_fd(fd)
  71. {
  72. }
  73. ~NotifierActivationEvent() = default;
  74. int fd() const { return m_fd; }
  75. private:
  76. int m_fd;
  77. };
  78. class ChildEvent final : public Event {
  79. public:
  80. ChildEvent(Type, Object& child, Object* insertion_before_child = nullptr);
  81. ~ChildEvent() = default;
  82. Object* child();
  83. Object const* child() const;
  84. Object* insertion_before_child();
  85. Object const* insertion_before_child() const;
  86. private:
  87. WeakPtr<Object> m_child;
  88. WeakPtr<Object> m_insertion_before_child;
  89. };
  90. class CustomEvent : public Event {
  91. public:
  92. CustomEvent(int custom_type)
  93. : Event(Event::Type::Custom)
  94. , m_custom_type(custom_type)
  95. {
  96. }
  97. ~CustomEvent() = default;
  98. int custom_type() const { return m_custom_type; }
  99. private:
  100. int m_custom_type { 0 };
  101. };
  102. }