summaryrefslogtreecommitdiff
path: root/WindowServer/WSWindow.cpp
blob: 17ead6f47c0fe5e36096f515037b79649c3e138e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "WSWindow.h"
#include "WSWindowManager.h"
#include "WSMessage.h"
#include "WSMessageLoop.h"
#include <WindowServer/WSAPITypes.h>
#include <WindowServer/WSClientConnection.h>

WSWindow::WSWindow(WSMenu& menu)
    : m_type(WSWindowType::Menu)
    , m_menu(&menu)
{
    WSWindowManager::the().add_window(*this);
}

WSWindow::WSWindow(int client_id, int window_id)
    : m_client_id(client_id)
    , m_type(WSWindowType::Normal)
    , m_window_id(window_id)
{
    WSWindowManager::the().add_window(*this);
}

WSWindow::~WSWindow()
{
    WSWindowManager::the().remove_window(*this);
}

void WSWindow::set_title(String&& title)
{
    if (m_title == title)
        return;
    m_title = move(title);
    WSWindowManager::the().notify_title_changed(*this);
}

void WSWindow::set_rect(const Rect& rect)
{
    Rect old_rect;
    auto* client = WSClientConnection::from_client_id(m_client_id);
    if (!client && !m_menu)
        return;
    if (m_rect == rect)
        return;
    old_rect = m_rect;
    m_rect = rect;
    if (!m_backing || old_rect.size() != rect.size()) {
        if (m_menu)
            m_backing = GraphicsBitmap::create(m_rect.size());
        else if (client)
            m_backing = client->create_bitmap(m_rect.size());

    }
    WSWindowManager::the().notify_rect_changed(*this, old_rect, rect);
}

// FIXME: Just use the same types.
static WSAPI_MouseButton to_api(MouseButton button)
{
    switch (button) {
    case MouseButton::None: return WSAPI_MouseButton::NoButton;
    case MouseButton::Left: return WSAPI_MouseButton::Left;
    case MouseButton::Right: return WSAPI_MouseButton::Right;
    case MouseButton::Middle: return WSAPI_MouseButton::Middle;
    }
    ASSERT_NOT_REACHED();
}

void WSWindow::on_message(WSMessage& message)
{
    if (m_menu) {
        m_menu->on_window_message(message);
        return;
    }

    WSAPI_ServerMessage server_message;
    server_message.window_id = window_id();

    switch (message.type()) {
    case WSMessage::MouseMove:
        server_message.type = WSAPI_ServerMessage::Type::MouseMove;
        server_message.mouse.position = static_cast<WSMouseEvent&>(message).position();
        server_message.mouse.button = WSAPI_MouseButton::NoButton;
        server_message.mouse.buttons = static_cast<WSMouseEvent&>(message).buttons();
        break;
    case WSMessage::MouseDown:
        server_message.type = WSAPI_ServerMessage::Type::MouseDown;
        server_message.mouse.position = static_cast<WSMouseEvent&>(message).position();
        server_message.mouse.button = to_api(static_cast<WSMouseEvent&>(message).button());
        server_message.mouse.buttons = static_cast<WSMouseEvent&>(message).buttons();
        break;
    case WSMessage::MouseUp:
        server_message.type = WSAPI_ServerMessage::Type::MouseUp;
        server_message.mouse.position = static_cast<WSMouseEvent&>(message).position();
        server_message.mouse.button = to_api(static_cast<WSMouseEvent&>(message).button());
        server_message.mouse.buttons = static_cast<WSMouseEvent&>(message).buttons();
        break;
    case WSMessage::KeyDown:
        server_message.type = WSAPI_ServerMessage::Type::KeyDown;
        server_message.key.character = static_cast<WSKeyEvent&>(message).character();
        server_message.key.key = static_cast<WSKeyEvent&>(message).key();
        server_message.key.alt = static_cast<WSKeyEvent&>(message).alt();
        server_message.key.ctrl = static_cast<WSKeyEvent&>(message).ctrl();
        server_message.key.shift = static_cast<WSKeyEvent&>(message).shift();
        break;
    case WSMessage::KeyUp:
        server_message.type = WSAPI_ServerMessage::Type::KeyUp;
        server_message.key.character = static_cast<WSKeyEvent&>(message).character();
        server_message.key.key = static_cast<WSKeyEvent&>(message).key();
        server_message.key.alt = static_cast<WSKeyEvent&>(message).alt();
        server_message.key.ctrl = static_cast<WSKeyEvent&>(message).ctrl();
        server_message.key.shift = static_cast<WSKeyEvent&>(message).shift();
        break;
    case WSMessage::WindowActivated:
        server_message.type = WSAPI_ServerMessage::Type::WindowActivated;
        break;
    case WSMessage::WindowDeactivated:
        server_message.type = WSAPI_ServerMessage::Type::WindowDeactivated;
        break;
    case WSMessage::WindowCloseRequest:
        server_message.type = WSAPI_ServerMessage::Type::WindowCloseRequest;
        break;
    default:
        break;
    }

    if (server_message.type == WSAPI_ServerMessage::Type::Invalid)
        return;

    if (auto* client = WSClientConnection::from_client_id(m_client_id))
        client->post_message(server_message);
}

void WSWindow::set_global_cursor_tracking_enabled(bool enabled)
{
    dbgprintf("WSWindow{%p} global_cursor_tracking <- %u\n", enabled);
    m_global_cursor_tracking_enabled = enabled;
}

void WSWindow::set_visible(bool b)
{
    if (m_visible == b)
        return;
    m_visible = b;
    invalidate();
}

void WSWindow::invalidate()
{
    WSWindowManager::the().invalidate(*this);
}