Event.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/EnumBits.h>
  9. #include <AK/Function.h>
  10. #include <AK/Types.h>
  11. #include <AK/WeakPtr.h>
  12. #include <LibCore/DeferredInvocationContext.h>
  13. #include <LibCore/Forward.h>
  14. namespace Core {
  15. class Event {
  16. public:
  17. enum Type {
  18. Invalid = 0,
  19. Quit,
  20. Timer,
  21. NotifierActivation,
  22. DeferredInvoke,
  23. ChildAdded,
  24. ChildRemoved,
  25. Custom,
  26. };
  27. Event() = default;
  28. explicit Event(unsigned type)
  29. : m_type(type)
  30. {
  31. }
  32. virtual ~Event() = default;
  33. unsigned type() const { return m_type; }
  34. bool is_accepted() const { return m_accepted; }
  35. void accept() { m_accepted = true; }
  36. void ignore() { m_accepted = false; }
  37. private:
  38. unsigned m_type { Type::Invalid };
  39. bool m_accepted { true };
  40. };
  41. class DeferredInvocationEvent : public Event {
  42. friend class EventLoop;
  43. friend class ThreadEventQueue;
  44. public:
  45. DeferredInvocationEvent(NonnullRefPtr<DeferredInvocationContext> context, Function<void()> invokee)
  46. : Event(Event::Type::DeferredInvoke)
  47. , m_context(move(context))
  48. , m_invokee(move(invokee))
  49. {
  50. }
  51. private:
  52. NonnullRefPtr<DeferredInvocationContext> m_context;
  53. Function<void()> m_invokee;
  54. };
  55. class TimerEvent final : public Event {
  56. public:
  57. explicit TimerEvent(int timer_id)
  58. : Event(Event::Timer)
  59. , m_timer_id(timer_id)
  60. {
  61. }
  62. ~TimerEvent() = default;
  63. int timer_id() const { return m_timer_id; }
  64. private:
  65. int m_timer_id;
  66. };
  67. enum class NotificationType {
  68. None = 0,
  69. Read = 1,
  70. Write = 2,
  71. HangUp = 4,
  72. Error = 8,
  73. };
  74. AK_ENUM_BITWISE_OPERATORS(NotificationType);
  75. class NotifierActivationEvent final : public Event {
  76. public:
  77. explicit NotifierActivationEvent(int fd, NotificationType type)
  78. : Event(Event::NotifierActivation)
  79. , m_fd(fd)
  80. , m_type(type)
  81. {
  82. }
  83. ~NotifierActivationEvent() = default;
  84. int fd() const { return m_fd; }
  85. NotificationType type() const { return m_type; }
  86. private:
  87. int m_fd;
  88. NotificationType m_type;
  89. };
  90. class ChildEvent final : public Event {
  91. public:
  92. ChildEvent(Type, EventReceiver& child, EventReceiver* insertion_before_child = nullptr);
  93. ~ChildEvent() = default;
  94. EventReceiver* child();
  95. EventReceiver const* child() const;
  96. EventReceiver* insertion_before_child();
  97. EventReceiver const* insertion_before_child() const;
  98. private:
  99. WeakPtr<EventReceiver> m_child;
  100. WeakPtr<EventReceiver> m_insertion_before_child;
  101. };
  102. class CustomEvent : public Event {
  103. public:
  104. CustomEvent(int custom_type)
  105. : Event(Event::Type::Custom)
  106. , m_custom_type(custom_type)
  107. {
  108. }
  109. ~CustomEvent() = default;
  110. int custom_type() const { return m_custom_type; }
  111. private:
  112. int m_custom_type { 0 };
  113. };
  114. }