Serenity Operating System
at hosted 155 lines 5.0 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/String.h> 30#include <Kernel/KeyCode.h> 31#include <LibCore/Event.h> 32#include <LibGfx/Rect.h> 33#include <WindowServer/Cursor.h> 34#include <WindowServer/WindowType.h> 35 36namespace WindowServer { 37 38class Event : public Core::Event { 39public: 40 enum Type { 41 Invalid = 3000, 42 MouseMove, 43 MouseDown, 44 MouseDoubleClick, 45 MouseUp, 46 MouseWheel, 47 WindowEntered, 48 WindowLeft, 49 KeyDown, 50 KeyUp, 51 WindowActivated, 52 WindowDeactivated, 53 WindowCloseRequest, 54 WindowResized, 55 }; 56 57 Event() {} 58 explicit Event(Type type) 59 : Core::Event(type) 60 { 61 } 62 virtual ~Event() {} 63 64 bool is_mouse_event() const { return type() == MouseMove || type() == MouseDown || type() == MouseDoubleClick || type() == MouseUp || type() == MouseWheel; } 65 bool is_key_event() const { return type() == KeyUp || type() == KeyDown; } 66}; 67 68enum class MouseButton : u8 { 69 None = 0, 70 Left = 1, 71 Right = 2, 72 Middle = 4, 73}; 74 75class KeyEvent final : public Event { 76public: 77 KeyEvent(Type type, int key, char character, u8 modifiers) 78 : Event(type) 79 , m_key(key) 80 , m_character(character) 81 , m_modifiers(modifiers) 82 { 83 } 84 85 int key() const { return m_key; } 86 bool ctrl() const { return m_modifiers & Mod_Ctrl; } 87 bool alt() const { return m_modifiers & Mod_Alt; } 88 bool shift() const { return m_modifiers & Mod_Shift; } 89 bool logo() const { return m_modifiers & Mod_Logo; } 90 u8 modifiers() const { return m_modifiers; } 91 char character() const { return m_character; } 92 93private: 94 friend class EventLoop; 95 friend class Screen; 96 int m_key { 0 }; 97 char m_character { 0 }; 98 u8 m_modifiers { 0 }; 99}; 100 101class MouseEvent final : public Event { 102public: 103 MouseEvent(Type type, const Gfx::Point& position, unsigned buttons, MouseButton button, unsigned modifiers, int wheel_delta = 0) 104 : Event(type) 105 , m_position(position) 106 , m_buttons(buttons) 107 , m_button(button) 108 , m_modifiers(modifiers) 109 , m_wheel_delta(wheel_delta) 110 { 111 } 112 113 Gfx::Point position() const { return m_position; } 114 int x() const { return m_position.x(); } 115 int y() const { return m_position.y(); } 116 MouseButton button() const { return m_button; } 117 unsigned buttons() const { return m_buttons; } 118 unsigned modifiers() const { return m_modifiers; } 119 int wheel_delta() const { return m_wheel_delta; } 120 bool is_drag() const { return m_drag; } 121 const String& drag_data_type() const { return m_drag_data_type; } 122 123 void set_drag(bool b) { m_drag = b; } 124 void set_drag_data_type(const String& drag_data_type) { m_drag_data_type = drag_data_type; } 125 126 MouseEvent translated(const Gfx::Point& delta) const { return MouseEvent((Type)type(), m_position.translated(delta), m_buttons, m_button, m_modifiers, m_wheel_delta); } 127 128private: 129 Gfx::Point m_position; 130 unsigned m_buttons { 0 }; 131 MouseButton m_button { MouseButton::None }; 132 unsigned m_modifiers { 0 }; 133 int m_wheel_delta { 0 }; 134 bool m_drag { false }; 135 String m_drag_data_type; 136}; 137 138class ResizeEvent final : public Event { 139public: 140 ResizeEvent(const Gfx::Rect& old_rect, const Gfx::Rect& rect) 141 : Event(Event::WindowResized) 142 , m_old_rect(old_rect) 143 , m_rect(rect) 144 { 145 } 146 147 Gfx::Rect old_rect() const { return m_old_rect; } 148 Gfx::Rect rect() const { return m_rect; } 149 150private: 151 Gfx::Rect m_old_rect; 152 Gfx::Rect m_rect; 153}; 154 155}