2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-01-16 16:03:50 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-09-06 15:34:26 +02:00
|
|
|
#include <AK/String.h>
|
2019-03-03 12:32:15 +01:00
|
|
|
#include <Kernel/KeyCode.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/Event.h>
|
2020-02-06 12:04:00 +01:00
|
|
|
#include <LibGfx/Rect.h>
|
2020-02-06 20:03:37 +01:00
|
|
|
#include <WindowServer/Cursor.h>
|
|
|
|
#include <WindowServer/WindowType.h>
|
2019-01-16 16:03:50 +01:00
|
|
|
|
2020-02-06 20:03:37 +01:00
|
|
|
namespace WindowServer {
|
|
|
|
|
|
|
|
class Event : public Core::Event {
|
2019-01-16 16:03:50 +01:00
|
|
|
public:
|
2019-06-07 17:13:23 +02:00
|
|
|
enum Type {
|
2019-07-17 10:31:52 +02:00
|
|
|
Invalid = 3000,
|
2019-01-16 16:03:50 +01:00
|
|
|
MouseMove,
|
|
|
|
MouseDown,
|
2019-05-15 22:17:09 +02:00
|
|
|
MouseDoubleClick,
|
2019-01-16 16:03:50 +01:00
|
|
|
MouseUp,
|
2019-05-13 19:52:57 +02:00
|
|
|
MouseWheel,
|
2019-02-20 10:12:19 +01:00
|
|
|
WindowEntered,
|
|
|
|
WindowLeft,
|
2019-01-16 16:03:50 +01:00
|
|
|
KeyDown,
|
|
|
|
KeyUp,
|
2019-01-17 17:38:04 +01:00
|
|
|
WindowActivated,
|
|
|
|
WindowDeactivated,
|
2019-02-05 10:31:37 +01:00
|
|
|
WindowCloseRequest,
|
2019-02-20 15:34:55 +01:00
|
|
|
WindowResized,
|
2019-01-16 16:03:50 +01:00
|
|
|
};
|
|
|
|
|
2020-02-06 20:03:37 +01:00
|
|
|
Event() {}
|
|
|
|
explicit Event(Type type)
|
2020-02-02 12:34:39 +01:00
|
|
|
: Core::Event(type)
|
2019-05-28 11:53:16 +02:00
|
|
|
{
|
|
|
|
}
|
2020-02-06 20:03:37 +01:00
|
|
|
virtual ~Event() {}
|
2019-01-16 16:03:50 +01:00
|
|
|
|
2019-05-15 22:17:09 +02:00
|
|
|
bool is_mouse_event() const { return type() == MouseMove || type() == MouseDown || type() == MouseDoubleClick || type() == MouseUp || type() == MouseWheel; }
|
2019-04-14 05:15:22 +02:00
|
|
|
bool is_key_event() const { return type() == KeyUp || type() == KeyDown; }
|
2019-01-21 07:28:04 +01:00
|
|
|
};
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
enum class MouseButton : u8 {
|
2019-01-16 16:03:50 +01:00
|
|
|
None = 0,
|
2019-01-20 05:48:43 +01:00
|
|
|
Left = 1,
|
|
|
|
Right = 2,
|
|
|
|
Middle = 4,
|
2020-05-02 22:07:43 +02:00
|
|
|
Back = 8,
|
|
|
|
Forward = 16,
|
2019-01-16 16:03:50 +01:00
|
|
|
};
|
|
|
|
|
2020-02-06 20:03:37 +01:00
|
|
|
class KeyEvent final : public Event {
|
2019-01-16 16:03:50 +01:00
|
|
|
public:
|
2020-05-30 15:54:02 +03:00
|
|
|
KeyEvent(Type type, int key, char character, u8 modifiers, u32 scancode)
|
2020-02-06 20:03:37 +01:00
|
|
|
: Event(type)
|
2019-01-16 16:03:50 +01:00
|
|
|
, m_key(key)
|
2019-01-21 07:28:04 +01:00
|
|
|
, m_character(character)
|
2019-03-03 12:32:15 +01:00
|
|
|
, m_modifiers(modifiers)
|
2020-05-30 15:54:02 +03:00
|
|
|
, m_scancode(scancode)
|
2019-01-16 16:03:50 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int key() const { return m_key; }
|
2019-03-03 12:32:15 +01:00
|
|
|
bool ctrl() const { return m_modifiers & Mod_Ctrl; }
|
|
|
|
bool alt() const { return m_modifiers & Mod_Alt; }
|
|
|
|
bool shift() const { return m_modifiers & Mod_Shift; }
|
2019-03-03 12:56:48 +01:00
|
|
|
bool logo() const { return m_modifiers & Mod_Logo; }
|
2019-07-03 21:17:35 +02:00
|
|
|
u8 modifiers() const { return m_modifiers; }
|
2019-01-21 07:28:04 +01:00
|
|
|
char character() const { return m_character; }
|
2020-05-30 15:54:02 +03:00
|
|
|
u32 scancode() const { return m_scancode; }
|
2019-01-16 16:03:50 +01:00
|
|
|
|
|
|
|
private:
|
2020-02-06 20:03:37 +01:00
|
|
|
friend class EventLoop;
|
|
|
|
friend class Screen;
|
2019-01-16 16:03:50 +01:00
|
|
|
int m_key { 0 };
|
2019-01-21 07:28:04 +01:00
|
|
|
char m_character { 0 };
|
2019-07-03 21:17:35 +02:00
|
|
|
u8 m_modifiers { 0 };
|
2020-05-30 15:54:02 +03:00
|
|
|
u32 m_scancode { 0 };
|
2019-01-16 16:03:50 +01:00
|
|
|
};
|
|
|
|
|
2020-02-06 20:03:37 +01:00
|
|
|
class MouseEvent final : public Event {
|
2019-01-16 16:03:50 +01:00
|
|
|
public:
|
2020-02-06 20:03:37 +01:00
|
|
|
MouseEvent(Type type, const Gfx::Point& position, unsigned buttons, MouseButton button, unsigned modifiers, int wheel_delta = 0)
|
|
|
|
: Event(type)
|
2019-01-20 05:48:43 +01:00
|
|
|
, m_position(position)
|
|
|
|
, m_buttons(buttons)
|
2019-01-16 16:03:50 +01:00
|
|
|
, m_button(button)
|
2019-03-08 17:53:02 +01:00
|
|
|
, m_modifiers(modifiers)
|
2019-05-13 19:52:57 +02:00
|
|
|
, m_wheel_delta(wheel_delta)
|
2019-01-16 16:03:50 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:08:32 +01:00
|
|
|
Gfx::Point position() const { return m_position; }
|
2019-01-16 16:03:50 +01:00
|
|
|
int x() const { return m_position.x(); }
|
|
|
|
int y() const { return m_position.y(); }
|
|
|
|
MouseButton button() const { return m_button; }
|
2019-01-20 05:48:43 +01:00
|
|
|
unsigned buttons() const { return m_buttons; }
|
2019-03-08 17:53:02 +01:00
|
|
|
unsigned modifiers() const { return m_modifiers; }
|
2019-05-13 19:52:57 +02:00
|
|
|
int wheel_delta() const { return m_wheel_delta; }
|
2020-02-13 21:43:32 +01:00
|
|
|
bool is_drag() const { return m_drag; }
|
|
|
|
const String& drag_data_type() const { return m_drag_data_type; }
|
|
|
|
|
|
|
|
void set_drag(bool b) { m_drag = b; }
|
|
|
|
void set_drag_data_type(const String& drag_data_type) { m_drag_data_type = drag_data_type; }
|
2019-01-16 16:03:50 +01:00
|
|
|
|
2020-02-06 20:03:37 +01:00
|
|
|
MouseEvent translated(const Gfx::Point& delta) const { return MouseEvent((Type)type(), m_position.translated(delta), m_buttons, m_button, m_modifiers, m_wheel_delta); }
|
2019-04-05 21:33:34 +02:00
|
|
|
|
2019-01-16 16:03:50 +01:00
|
|
|
private:
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::Point m_position;
|
2019-01-20 05:48:43 +01:00
|
|
|
unsigned m_buttons { 0 };
|
2019-01-16 16:03:50 +01:00
|
|
|
MouseButton m_button { MouseButton::None };
|
2019-03-08 17:53:02 +01:00
|
|
|
unsigned m_modifiers { 0 };
|
2019-05-13 19:52:57 +02:00
|
|
|
int m_wheel_delta { 0 };
|
2020-02-13 21:43:32 +01:00
|
|
|
bool m_drag { false };
|
|
|
|
String m_drag_data_type;
|
2019-01-16 16:03:50 +01:00
|
|
|
};
|
2019-02-20 15:34:55 +01:00
|
|
|
|
2020-02-06 20:03:37 +01:00
|
|
|
class ResizeEvent final : public Event {
|
2019-02-20 15:34:55 +01:00
|
|
|
public:
|
2020-02-06 20:03:37 +01:00
|
|
|
ResizeEvent(const Gfx::Rect& old_rect, const Gfx::Rect& rect)
|
|
|
|
: Event(Event::WindowResized)
|
2019-02-20 15:34:55 +01:00
|
|
|
, m_old_rect(old_rect)
|
|
|
|
, m_rect(rect)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:02:38 +01:00
|
|
|
Gfx::Rect old_rect() const { return m_old_rect; }
|
|
|
|
Gfx::Rect rect() const { return m_rect; }
|
2019-02-20 15:34:55 +01:00
|
|
|
|
|
|
|
private:
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::Rect m_old_rect;
|
|
|
|
Gfx::Rect m_rect;
|
2019-02-20 15:34:55 +01:00
|
|
|
};
|
2020-02-06 20:03:37 +01:00
|
|
|
|
|
|
|
}
|