Event.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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()
  58. : Event(Event::Timer)
  59. {
  60. }
  61. ~TimerEvent() = default;
  62. };
  63. enum class NotificationType {
  64. None = 0,
  65. Read = 1,
  66. Write = 2,
  67. HangUp = 4,
  68. Error = 8,
  69. };
  70. AK_ENUM_BITWISE_OPERATORS(NotificationType);
  71. class NotifierActivationEvent final : public Event {
  72. public:
  73. explicit NotifierActivationEvent(int fd, NotificationType type)
  74. : Event(Event::NotifierActivation)
  75. , m_fd(fd)
  76. , m_type(type)
  77. {
  78. }
  79. ~NotifierActivationEvent() = default;
  80. int fd() const { return m_fd; }
  81. NotificationType type() const { return m_type; }
  82. private:
  83. int m_fd;
  84. NotificationType m_type;
  85. };
  86. class ChildEvent final : public Event {
  87. public:
  88. ChildEvent(Type, EventReceiver& child, EventReceiver* insertion_before_child = nullptr);
  89. ~ChildEvent() = default;
  90. EventReceiver* child();
  91. EventReceiver const* child() const;
  92. EventReceiver* insertion_before_child();
  93. EventReceiver const* insertion_before_child() const;
  94. private:
  95. WeakPtr<EventReceiver> m_child;
  96. WeakPtr<EventReceiver> m_insertion_before_child;
  97. };
  98. class CustomEvent : public Event {
  99. public:
  100. CustomEvent(int custom_type)
  101. : Event(Event::Type::Custom)
  102. , m_custom_type(custom_type)
  103. {
  104. }
  105. ~CustomEvent() = default;
  106. int custom_type() const { return m_custom_type; }
  107. private:
  108. int m_custom_type { 0 };
  109. };
  110. }