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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ScopeGuard.h>
#include <Kernel/API/FB.h>
#include <Services/WindowServer/ScreenLayout.h>
#include <errno.h>
#include <fcntl.h>
#include <string.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;
}
bool ScreenLayout::normalize()
{
// Check for any overlaps and try to move screens
Vector<Gfx::IntRect, 8> screen_virtual_rects;
for (auto& screen : screens)
screen_virtual_rects.append(screen.virtual_rect());
bool did_change = false;
for (;;) {
// Separate any overlapping screens
if (Gfx::IntRect::disperse(screen_virtual_rects)) {
did_change = true;
continue;
}
// Check if all screens are still reachable
Vector<Gfx::IntRect*, 8> reachable_rects;
auto recalculate_reachable = [&]() {
reachable_rects = { &screen_virtual_rects[main_screen_index] };
bool did_reach_another;
do {
did_reach_another = false;
auto& latest_reachable_rect = *reachable_rects[reachable_rects.size() - 1];
for (auto& rect : screen_virtual_rects) {
if (&rect == &latest_reachable_rect || reachable_rects.contains_slow(&rect))
continue;
if (rect.is_adjacent(latest_reachable_rect)) {
reachable_rects.append(&rect);
did_reach_another = true;
break;
}
}
} while (did_reach_another);
};
recalculate_reachable();
if (reachable_rects.size() != screen_virtual_rects.size()) {
// Some screens were not reachable, try to move one somewhere closer
for (auto& screen_rect : screen_virtual_rects) {
if (reachable_rects.contains_slow(&screen_rect))
continue;
float closest_distance = 0;
Gfx::IntRect* closest_rect = nullptr;
for (auto& screen_rect2 : screen_virtual_rects) {
if (&screen_rect2 == &screen_rect)
continue;
if (!reachable_rects.contains_slow(&screen_rect2))
continue;
auto distance = screen_rect.outside_center_point_distance_to(screen_rect2);
if (!closest_rect || distance < closest_distance) {
closest_distance = distance;
closest_rect = &screen_rect2;
}
}
VERIFY(closest_rect); // We should always have one!
VERIFY(closest_rect != &screen_rect);
// Move the screen_rect closer to closest_rect
auto is_adjacent_to_reachable = [&]() {
for (auto* rect : reachable_rects) {
if (rect == &screen_rect)
continue;
if (screen_rect.is_adjacent(*rect))
return true;
}
return false;
};
// Move it until we're touching a reachable screen
do {
auto outside_center_points = screen_rect.closest_outside_center_points(*closest_rect);
int delta_x = 0;
if (outside_center_points[0].x() < outside_center_points[1].x())
delta_x = 1;
else if (outside_center_points[0].x() > outside_center_points[1].x())
delta_x = -1;
int delta_y = 0;
if (outside_center_points[0].y() < outside_center_points[1].y())
delta_y = 1;
else if (outside_center_points[0].y() > outside_center_points[1].y())
delta_y = -1;
VERIFY(delta_x != 0 || delta_y != 0);
screen_rect.translate_by(delta_x, delta_y);
} while (!is_adjacent_to_reachable());
recalculate_reachable();
did_change = true;
break; // We only try to move one at at time
}
// Moved the screen, re-evaluate
continue;
}
break;
}
int smallest_x = 0;
int smallest_y = 0;
for (size_t i = 0; i < screen_virtual_rects.size(); i++) {
auto& rect = screen_virtual_rects[i];
if (i == 0 || rect.x() < smallest_x)
smallest_x = rect.x();
if (i == 0 || rect.y() < smallest_y)
smallest_y = rect.y();
}
if (smallest_x != 0 || smallest_y != 0) {
for (auto& rect : screen_virtual_rects)
rect.translate_by(-smallest_x, -smallest_y);
did_change = true;
}
for (size_t i = 0; i < screens.size(); i++)
screens[i].location = screen_virtual_rects[i].location();
VERIFY(is_valid());
return did_change;
}
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);
});
// FIXME: Add multihead support for one framebuffer
FBHeadResolution resolution {};
memset(&resolution, 0, sizeof(FBHeadResolution));
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 append_screen = [&](Gfx::IntRect const& new_screen_rect) {
screens.append({ .device = device_path,
.location = new_screen_rect.location(),
.resolution = new_screen_rect.size(),
.scale_factor = 1 });
};
if (screens.is_empty()) {
append_screen({ 0, 0, (int)resolution.width, (int)resolution.height });
return true;
}
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) {
append_screen(new_screen_rect);
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;
}
}
|