Event.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/String.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. NotifierRead,
  22. NotifierWrite,
  23. DeferredInvoke,
  24. ChildAdded,
  25. ChildRemoved,
  26. Custom,
  27. };
  28. Event() = default;
  29. explicit Event(unsigned type)
  30. : m_type(type)
  31. {
  32. }
  33. virtual ~Event() = default;
  34. unsigned type() const { return m_type; }
  35. bool is_accepted() const { return m_accepted; }
  36. void accept() { m_accepted = true; }
  37. void ignore() { m_accepted = false; }
  38. private:
  39. unsigned m_type { Type::Invalid };
  40. bool m_accepted { true };
  41. };
  42. class DeferredInvocationEvent : public Event {
  43. friend class EventLoop;
  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. class NotifierReadEvent final : public Event {
  68. public:
  69. explicit NotifierReadEvent(int fd)
  70. : Event(Event::NotifierRead)
  71. , m_fd(fd)
  72. {
  73. }
  74. ~NotifierReadEvent() = default;
  75. int fd() const { return m_fd; }
  76. private:
  77. int m_fd;
  78. };
  79. class NotifierWriteEvent final : public Event {
  80. public:
  81. explicit NotifierWriteEvent(int fd)
  82. : Event(Event::NotifierWrite)
  83. , m_fd(fd)
  84. {
  85. }
  86. ~NotifierWriteEvent() = default;
  87. int fd() const { return m_fd; }
  88. private:
  89. int m_fd;
  90. };
  91. class ChildEvent final : public Event {
  92. public:
  93. ChildEvent(Type, Object& child, Object* insertion_before_child = nullptr);
  94. ~ChildEvent() = default;
  95. Object* child();
  96. Object const* child() const;
  97. Object* insertion_before_child();
  98. Object const* insertion_before_child() const;
  99. private:
  100. WeakPtr<Object> m_child;
  101. WeakPtr<Object> m_insertion_before_child;
  102. };
  103. class CustomEvent : public Event {
  104. public:
  105. CustomEvent(int custom_type)
  106. : Event(Event::Type::Custom)
  107. , m_custom_type(custom_type)
  108. {
  109. }
  110. ~CustomEvent() = default;
  111. int custom_type() const { return m_custom_type; }
  112. private:
  113. int m_custom_type { 0 };
  114. };
  115. }