summaryrefslogtreecommitdiff
path: root/Userland/Services/WindowServer/ScreenLayout.ipp
blob: 7428a4ebdbe38e66a1a2556f30253b650b6d14b3 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright (c) 2020, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <Kernel/API/FB.h>
#include <Services/WindowServer/ScreenLayout.h>
#include <errno.h>
#include <fcntl.h>

// Must be included after LibIPC/Forward.h
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>

namespace WindowServer {

bool ScreenLayout::is_valid(String* error_msg) const
{
    if (screens.is_empty()) {
        if (error_msg)
            *error_msg = "Must have at least one screen";
        return false;
    }
    if (main_screen_index >= screens.size()) {
        if (error_msg)
            *error_msg = String::formatted("Invalid main screen index: {}", main_screen_index);
        return false;
    }
    int smallest_x = 0;
    int smallest_y = 0;
    for (size_t i = 0; i < screens.size(); i++) {
        auto& screen = screens[i];
        if (screen.device.is_null() || screen.device.is_empty()) {
            if (error_msg)
                *error_msg = String::formatted("Screen #{} has no path", i);
            return false;
        }
        for (size_t j = 0; j < screens.size(); j++) {
            auto& other_screen = screens[j];
            if (&other_screen == &screen)
                continue;
            if (screen.device == other_screen.device) {
                if (error_msg)
                    *error_msg = String::formatted("Screen #{} is using same device as screen #{}", i, j);
                return false;
            }
            if (screen.virtual_rect().intersects(other_screen.virtual_rect())) {
                if (error_msg)
                    *error_msg = String::formatted("Screen #{} overlaps with screen #{}", i, j);
                return false;
            }
        }
        if (screen.location.x() < 0 || screen.location.y() < 0) {
            if (error_msg)
                *error_msg = String::formatted("Screen #{} has invalid location: {}", i, screen.location);
            return false;
        }
        if (screen.resolution.width() <= 0 || screen.resolution.height() <= 0) {
            if (error_msg)
                *error_msg = String::formatted("Screen #{} has invalid resolution: {}", i, screen.resolution);
            return false;
        }
        if (screen.scale_factor < 1) {
            if (error_msg)
                *error_msg = String::formatted("Screen #{} has invalid scale factor: {}", i, screen.scale_factor);
            return false;
        }
        if (i == 0 || screen.location.x() < smallest_x)
            smallest_x = screen.location.x();
        if (i == 0 || screen.location.y() < smallest_y)
            smallest_y = screen.location.y();
    }
    if (smallest_x != 0 || smallest_y != 0) {
        if (error_msg)
            *error_msg = "Screen layout has not been normalized";
        return false;
    }
    Vector<const Screen*, 16> reachable_screens { &screens[main_screen_index] };
    bool did_reach_another_screen;
    do {
        did_reach_another_screen = false;
        auto* latest_reachable_screen = reachable_screens[reachable_screens.size() - 1];
        for (auto& screen : screens) {
            if (&screen == latest_reachable_screen || reachable_screens.contains_slow(&screen))
                continue;
            if (screen.virtual_rect().is_adjacent(latest_reachable_screen->virtual_rect())) {
                reachable_screens.append(&screen);
                did_reach_another_screen = true;
                break;
            }
        }
    } while (did_reach_another_screen);
    if (reachable_screens.size() != screens.size()) {
        for (size_t i = 0; i < screens.size(); i++) {
            auto& screen = screens[i];
            if (!reachable_screens.contains_slow(&screen)) {
                if (error_msg)
                    *error_msg = String::formatted("Screen #{} {} cannot be reached from main screen #{} {}", i, screen.virtual_rect(), main_screen_index, screens[main_screen_index].virtual_rect());
                break;
            }
        }
        return false;
    }
    return true;
}

void ScreenLayout::normalize()
{
    int smallest_x = 0;
    int smallest_y = 0;
    for (size_t i = 0; i < screens.size(); i++) {
        auto& screen = screens[i];
        if (i == 0 || screen.location.x() < smallest_x)
            smallest_x = screen.location.x();
        if (i == 0 || screen.location.y() < smallest_y)
            smallest_y = screen.location.y();
    }
    if (smallest_x != 0 || smallest_y != 0) {
        for (auto& screen : screens)
            screen.location.translate_by(-smallest_x, -smallest_y);
    }
}

bool ScreenLayout::load_config(const Core::ConfigFile& config_file, String* error_msg)
{
    screens.clear_with_capacity();
    main_screen_index = config_file.read_num_entry("Screens", "DefaultScreen", 0);
    for (size_t index = 0;; index++) {
        auto group_name = String::formatted("Screen{}", index);
        if (!config_file.has_group(group_name))
            break;
        screens.append({ config_file.read_entry(group_name, "Device"),
            { config_file.read_num_entry(group_name, "Left"), config_file.read_num_entry(group_name, "Top") },
            { config_file.read_num_entry(group_name, "Width"), config_file.read_num_entry(group_name, "Height") },
            config_file.read_num_entry(group_name, "ScaleFactor", 1) });
    }
    if (!is_valid(error_msg)) {
        *this = {};
        return false;
    }
    return true;
}

bool ScreenLayout::save_config(Core::ConfigFile& config_file, bool sync) const
{
    config_file.write_num_entry("Screens", "DefaultScreen", main_screen_index);

    size_t index = 0;
    while (index < screens.size()) {
        auto& screen = screens[index];
        auto group_name = String::formatted("Screen{}", index);
        config_file.write_entry(group_name, "Device", screen.device);
        config_file.write_num_entry(group_name, "Left", screen.location.x());
        config_file.write_num_entry(group_name, "Top", screen.location.y());
        config_file.write_num_entry(group_name, "Width", screen.resolution.width());
        config_file.write_num_entry(group_name, "Height", screen.resolution.height());
        config_file.write_num_entry(group_name, "ScaleFactor", screen.scale_factor);
        index++;
    }
    // Prune screens no longer in the layout
    for (;;) {
        auto group_name = String::formatted("Screen{}", index++);
        if (!config_file.has_group(group_name))
            break;
        config_file.remove_group(group_name);
    }

    if (sync && !config_file.sync())
        return false;
    return true;
}

bool ScreenLayout::operator!=(const ScreenLayout& other) const
{
    if (this == &other)
        return false;
    if (main_screen_index != other.main_screen_index)
        return true;
    if (screens.size() != other.screens.size())
        return true;
    for (size_t i = 0; i < screens.size(); i++) {
        if (screens[i] != other.screens[i])
            return true;
    }
    return false;
}

bool ScreenLayout::try_auto_add_framebuffer(String const& device_path)
{
    int framebuffer_fd = open(device_path.characters(), O_RDWR | O_CLOEXEC);
    if (framebuffer_fd < 0) {
        int err = errno;
        dbgln("Error ({}) opening framebuffer device {}", err, device_path);
        return false;
    }
    ScopeGuard fd_guard([&] {
        close(framebuffer_fd);
    });
    FBResolution resolution {};
    if (fb_get_resolution(framebuffer_fd, &resolution) < 0) {
        int err = errno;
        dbgln("Error ({}) querying resolution from framebuffer device {}", err, device_path);
        return false;
    }
    if (resolution.width == 0 || resolution.height == 0) {
        // Looks like the display is not turned on. Since we don't know what the desired
        // resolution should be, use the main display as reference.
        if (screens.is_empty())
            return false;
        auto& main_screen = screens[main_screen_index];
        resolution.width = main_screen.resolution.width();
        resolution.height = main_screen.resolution.height();
    }

    auto original_screens = move(screens);
    screens = original_screens;
    ArmedScopeGuard screens_guard([&] {
        screens = move(original_screens);
    });
    // Now that we know the current resolution, try to find a location that we can add onto
    // TODO: make this a little more sophisticated in case a more complex layout is already configured
    for (auto& screen : screens) {
        auto screen_rect = screen.virtual_rect();
        Gfx::IntRect new_screen_rect {
            screen_rect.right() + 1,
            screen_rect.top(),
            (int)resolution.width,
            (int)resolution.height
        };

        bool collision = false;
        for (auto& other_screen : screens) {
            if (&screen == &other_screen)
                continue;
            if (other_screen.virtual_rect().intersects(new_screen_rect)) {
                collision = true;
                break;
            }
        }

        if (!collision) {
            screens.append({
                .device = device_path,
                .location = new_screen_rect.location(),
                .resolution = new_screen_rect.size(),
                .scale_factor = 1
            });

            if (is_valid()) {
                // We got lucky!
                screens_guard.disarm();
                return true;
            }
        }
    }

    dbgln("Failed to add framebuffer device {} with resolution {}x{} to screen layout", device_path, resolution.width, resolution.height);
    return false;
}

}

namespace IPC {

bool encode(Encoder& encoder, const WindowServer::ScreenLayout::Screen& screen)
{
    encoder << screen.device << screen.location << screen.resolution << screen.scale_factor;
    return true;
}

bool decode(Decoder& decoder, WindowServer::ScreenLayout::Screen& screen)
{
    String device;
    if (!decoder.decode(device))
        return false;
    Gfx::IntPoint location;
    if (!decoder.decode(location))
        return false;
    Gfx::IntSize resolution;
    if (!decoder.decode(resolution))
        return false;
    int scale_factor = 0;
    if (!decoder.decode(scale_factor))
        return false;
    screen = { device, location, resolution, scale_factor };
    return true;
}

bool encode(Encoder& encoder, const WindowServer::ScreenLayout& screen_layout)
{
    encoder << screen_layout.screens << screen_layout.main_screen_index;
    return true;
}

bool decode(Decoder& decoder, WindowServer::ScreenLayout& screen_layout)
{
    Vector<WindowServer::ScreenLayout::Screen> screens;
    if (!decoder.decode(screens))
        return false;
    unsigned main_screen_index = 0;
    if (!decoder.decode(main_screen_index))
        return false;
    screen_layout = { move(screens), main_screen_index };
    return true;
}

}