Serenity Operating System
at portability 163 lines 4.2 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#pragma once 28 29#include <AK/Function.h> 30#include <AK/String.h> 31#include <AK/Types.h> 32#include <AK/WeakPtr.h> 33#include <LibCore/Forward.h> 34 35namespace Core { 36 37class Event { 38public: 39 enum Type { 40 Invalid = 0, 41 Quit, 42 Timer, 43 NotifierRead, 44 NotifierWrite, 45 DeferredInvoke, 46 ChildAdded, 47 ChildRemoved, 48 Custom, 49 }; 50 51 Event() {} 52 explicit Event(unsigned type) 53 : m_type(type) 54 { 55 } 56 virtual ~Event() {} 57 58 unsigned type() const { return m_type; } 59 60 bool is_accepted() const { return m_accepted; } 61 void accept() { m_accepted = true; } 62 void ignore() { m_accepted = false; } 63 64private: 65 unsigned m_type { Type::Invalid }; 66 bool m_accepted { true }; 67}; 68 69class DeferredInvocationEvent : public Event { 70 friend class EventLoop; 71 72public: 73 DeferredInvocationEvent(Function<void(Object&)> invokee) 74 : Event(Event::Type::DeferredInvoke) 75 , m_invokee(move(invokee)) 76 { 77 } 78 79private: 80 Function<void(Object&)> m_invokee; 81}; 82 83class TimerEvent final : public Event { 84public: 85 explicit TimerEvent(int timer_id) 86 : Event(Event::Timer) 87 , m_timer_id(timer_id) 88 { 89 } 90 ~TimerEvent() {} 91 92 int timer_id() const { return m_timer_id; } 93 94private: 95 int m_timer_id; 96}; 97 98class NotifierReadEvent final : public Event { 99public: 100 explicit NotifierReadEvent(int fd) 101 : Event(Event::NotifierRead) 102 , m_fd(fd) 103 { 104 } 105 ~NotifierReadEvent() {} 106 107 int fd() const { return m_fd; } 108 109private: 110 int m_fd; 111}; 112 113class NotifierWriteEvent final : public Event { 114public: 115 explicit NotifierWriteEvent(int fd) 116 : Event(Event::NotifierWrite) 117 , m_fd(fd) 118 { 119 } 120 ~NotifierWriteEvent() {} 121 122 int fd() const { return m_fd; } 123 124private: 125 int m_fd; 126}; 127 128class ChildEvent final : public Event { 129public: 130 ChildEvent(Type, Object& child, Object* insertion_before_child = nullptr); 131 ~ChildEvent(); 132 133 Object* child() { return m_child.ptr(); } 134 const Object* child() const { return m_child.ptr(); } 135 136 Object* insertion_before_child() { return m_insertion_before_child.ptr(); } 137 const Object* insertion_before_child() const { return m_insertion_before_child.ptr(); } 138 139private: 140 WeakPtr<Object> m_child; 141 WeakPtr<Object> m_insertion_before_child; 142}; 143 144class CustomEvent : public Event { 145public: 146 CustomEvent(int custom_type, void* data = nullptr) 147 : Event(Event::Type::Custom) 148 , m_custom_type(custom_type) 149 , m_data(data) 150 { 151 } 152 ~CustomEvent() {} 153 154 int custom_type() const { return m_custom_type; } 155 void* data() { return m_data; } 156 const void* data() const { return m_data; } 157 158private: 159 int m_custom_type { 0 }; 160 void* m_data { nullptr }; 161}; 162 163}