summaryrefslogtreecommitdiff
path: root/Userland/Services/WindowServer/WMConnectionFromClient.cpp
blob: 0ce4a6e7a6f4f0f7b794f1a96e700f50d8ad55dd (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (c) 2021, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <WindowServer/AppletManager.h>
#include <WindowServer/ConnectionFromClient.h>
#include <WindowServer/Screen.h>
#include <WindowServer/WMConnectionFromClient.h>

namespace WindowServer {

HashMap<int, NonnullRefPtr<WMConnectionFromClient>> WMConnectionFromClient::s_connections {};

WMConnectionFromClient::WMConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> client_socket, int client_id)
    : IPC::ConnectionFromClient<WindowManagerClientEndpoint, WindowManagerServerEndpoint>(*this, move(client_socket), client_id)
{
    s_connections.set(client_id, *this);
}

WMConnectionFromClient::~WMConnectionFromClient()
{
    // The WM has gone away, so take away the applet manager (cause there's nowhere
    // to draw it in).
    AppletManager::the().set_position({});
}

void WMConnectionFromClient::die()
{
    deferred_invoke([this] {
        s_connections.remove(client_id());
    });
}

void WMConnectionFromClient::set_applet_area_position(Gfx::IntPoint const& position)
{
    if (m_window_id < 0) {
        did_misbehave("SetAppletAreaPosition: WM didn't assign window as manager yet");
        // FIXME: return ok boolean?
        return;
    }

    AppletManager::the().set_position(position);

    WindowServer::ConnectionFromClient::for_each_client([](auto& connection) {
        if (auto result = connection.post_message(Messages::WindowClient::AppletAreaRectChanged(AppletManager::the().window()->rect())); result.is_error()) {
            dbgln("WMConnectionFromClient::set_applet_area_position: {}", result.error());
        }
    });
}

void WMConnectionFromClient::set_active_window(i32 client_id, i32 window_id)
{
    auto* client = WindowServer::ConnectionFromClient::from_client_id(client_id);
    if (!client) {
        did_misbehave("SetActiveWindow: Bad client ID");
        return;
    }
    auto it = client->m_windows.find(window_id);
    if (it == client->m_windows.end()) {
        did_misbehave("SetActiveWindow: Bad window ID");
        return;
    }
    auto& window = *(*it).value;
    WindowManager::the().minimize_windows(window, false);
    WindowManager::the().move_to_front_and_make_active(window);
}

void WMConnectionFromClient::popup_window_menu(i32 client_id, i32 window_id, Gfx::IntPoint const& screen_position)
{
    auto* client = WindowServer::ConnectionFromClient::from_client_id(client_id);
    if (!client) {
        did_misbehave("PopupWindowMenu: Bad client ID");
        return;
    }
    auto it = client->m_windows.find(window_id);
    if (it == client->m_windows.end()) {
        did_misbehave("PopupWindowMenu: Bad window ID");
        return;
    }
    auto& window = *(*it).value;
    if (auto* modal_window = window.blocking_modal_window()) {
        modal_window->popup_window_menu(screen_position, WindowMenuDefaultAction::BasedOnWindowState);
    } else {
        window.popup_window_menu(screen_position, WindowMenuDefaultAction::BasedOnWindowState);
    }
}

void WMConnectionFromClient::start_window_resize(i32 client_id, i32 window_id)
{
    auto* client = WindowServer::ConnectionFromClient::from_client_id(client_id);
    if (!client) {
        did_misbehave("WM_StartWindowResize: Bad client ID");
        return;
    }
    auto it = client->m_windows.find(window_id);
    if (it == client->m_windows.end()) {
        did_misbehave("WM_StartWindowResize: Bad window ID");
        return;
    }
    auto& window = *(*it).value;
    // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
    //        Maybe the client should be allowed to specify what initiated this request?
    WindowManager::the().start_window_resize(window, ScreenInput::the().cursor_location(), MouseButton::Primary);
}

void WMConnectionFromClient::set_window_minimized(i32 client_id, i32 window_id, bool minimized)
{
    auto* client = WindowServer::ConnectionFromClient::from_client_id(client_id);
    if (!client) {
        did_misbehave("WM_SetWindowMinimized: Bad client ID");
        return;
    }
    auto it = client->m_windows.find(window_id);
    if (it == client->m_windows.end()) {
        did_misbehave("WM_SetWindowMinimized: Bad window ID");
        return;
    }
    auto& window = *(*it).value;
    WindowManager::the().minimize_windows(window, minimized);
}

void WMConnectionFromClient::toggle_show_desktop()
{
    bool should_hide = false;
    auto& current_window_stack = WindowManager::the().current_window_stack();
    current_window_stack.for_each_window([&](auto& window) {
        if (window.type() == WindowType::Normal && window.is_minimizable()) {
            if (!window.is_hidden() && !window.is_minimized()) {
                should_hide = true;
                return IterationDecision::Break;
            }
        }
        return IterationDecision::Continue;
    });

    current_window_stack.for_each_window([&](auto& window) {
        if (window.type() == WindowType::Normal && window.is_minimizable()) {
            auto state = window.minimized_state();
            if (state == WindowMinimizedState::None || state == WindowMinimizedState::Hidden) {
                WindowManager::the().hide_windows(window, should_hide);
            }
        }
        return IterationDecision::Continue;
    });
}

void WMConnectionFromClient::set_event_mask(u32 event_mask)
{
    m_event_mask = event_mask;
}

void WMConnectionFromClient::set_manager_window(i32 window_id)
{
    m_window_id = window_id;

    // Let the window manager know that we obtained a manager window, and should
    // receive information about other windows.
    WindowManager::the().greet_window_manager(*this);
}

void WMConnectionFromClient::set_workspace(u32 row, u32 col)
{
    WindowManager::the().switch_to_window_stack(row, col);
}

void WMConnectionFromClient::set_window_taskbar_rect(i32 client_id, i32 window_id, Gfx::IntRect const& rect)
{
    // Because the Taskbar (which should be the only user of this API) does not own the
    // window or the client id, there is a possibility that it may send this message for
    // a window or client that may have been destroyed already. This is not an error,
    // and we should not call did_misbehave() for either.
    auto* client = WindowServer::ConnectionFromClient::from_client_id(client_id);
    if (!client)
        return;

    auto it = client->m_windows.find(window_id);
    if (it == client->m_windows.end())
        return;

    auto& window = *(*it).value;
    window.set_taskbar_rect(rect);
}

}