Event.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2018-2020, 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. NotifierRead,
  21. NotifierWrite,
  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. 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 NotifierReadEvent final : public Event {
  67. public:
  68. explicit NotifierReadEvent(int fd)
  69. : Event(Event::NotifierRead)
  70. , m_fd(fd)
  71. {
  72. }
  73. ~NotifierReadEvent() = default;
  74. int fd() const { return m_fd; }
  75. private:
  76. int m_fd;
  77. };
  78. class NotifierWriteEvent final : public Event {
  79. public:
  80. explicit NotifierWriteEvent(int fd)
  81. : Event(Event::NotifierWrite)
  82. , m_fd(fd)
  83. {
  84. }
  85. ~NotifierWriteEvent() = default;
  86. int fd() const { return m_fd; }
  87. private:
  88. int m_fd;
  89. };
  90. class ChildEvent final : public Event {
  91. public:
  92. ChildEvent(Type, Object& child, Object* insertion_before_child = nullptr);
  93. ~ChildEvent() = default;
  94. Object* child();
  95. Object const* child() const;
  96. Object* insertion_before_child();
  97. Object const* insertion_before_child() const;
  98. private:
  99. WeakPtr<Object> m_child;
  100. WeakPtr<Object> 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. }