FreeRDP
Loading...
Searching...
No Matches
TestSDLDisplayOffsets.cpp
1
19
20/* Pixel origins of local displays with mixed scales. */
21#include "../sdl_display_offsets.hpp"
22
23#include <algorithm>
24#include <cstdio>
25#include <cstdint>
26#include <numeric>
27#include <vector>
28
29#include <freerdp/settings_types.h>
30
31[[nodiscard]]
32static bool spansRange(int64_t a, int64_t alen, int64_t b, int64_t blen)
33{
34 return (a < b + blen) && (b < a + alen);
35}
36
37[[nodiscard]]
38static bool intersect(const rdpMonitor& a, const rdpMonitor& b)
39{
40 return spansRange(a.x, a.width, b.x, b.width) && spansRange(a.y, a.height, b.y, b.height);
41}
42
43/* Two monitors are adjacent when they share an edge segment of positive length. */
44[[nodiscard]]
45static bool touch(const rdpMonitor& a, const rdpMonitor& b)
46{
47 const int64_t ax = a.x;
48 const int64_t ay = a.y;
49 const int64_t bx = b.x;
50 const int64_t by = b.y;
51 if (((ax + a.width == bx) || (bx + b.width == ax)) && spansRange(ay, a.height, by, b.height))
52 return true;
53 return ((ay + a.height == by) || (by + b.height == ay)) && spansRange(ax, a.width, bx, b.width);
54}
55
56/* MS-RDPEDISP 3.1.5.2: monitors must not overlap and must form one area, connected through
57 * shared edges. */
58[[nodiscard]]
59static bool valid(const std::vector<rdpMonitor>& list)
60{
61 for (size_t i = 0; i < list.size(); i++)
62 for (size_t j = i + 1; j < list.size(); j++)
63 if (intersect(list.at(i), list.at(j)))
64 return false;
65 if (list.size() < 2)
66 return true;
67
68 std::vector<bool> reached(list.size(), false);
69 std::vector<size_t> next{ 0 };
70 reached.at(0) = true;
71 while (!next.empty())
72 {
73 const auto i = next.back();
74 next.pop_back();
75 for (size_t j = 0; j < list.size(); j++)
76 {
77 if (!reached.at(j) && touch(list.at(i), list.at(j)))
78 {
79 reached.at(j) = true;
80 next.push_back(j);
81 }
82 }
83 }
84 return std::all_of(reached.begin(), reached.end(), [](bool r) { return r; });
85}
86
87[[nodiscard]]
88static bool expect(bool value, const char* message)
89{
90 if (!value)
91 (void)fprintf(stderr, "FAIL: %s\n", message);
92 return value;
93}
94
95struct Display
96{
97 const char* name;
98 SDL_Rect points;
99 int width;
100 int height;
101};
102
103[[nodiscard]]
104static sdl_display_offsets::Offsets offsets(const std::vector<Display>& displays,
105 const std::vector<SDL_DisplayID>& ids, size_t primary,
106 const std::vector<SDL_DisplayID>& selection = {})
107{
108 /* SDL reports bounds relative to the primary display, which starts at (0, 0). */
109 const auto origin = displays.at(primary).points;
110 sdl_display_offsets::Offsets result;
111 for (size_t i = 0; i < displays.size(); i++)
112 {
113 auto bounds = displays.at(i).points;
114 bounds.x -= origin.x;
115 bounds.y -= origin.y;
116 SDL_Rect pixel{ 0, 0, displays.at(i).width, displays.at(i).height };
117 result.emplace(ids.at(i), std::pair{ bounds, pixel });
118 }
119 sdl_display_offsets::update(result, ids.at(primary), selection);
120 return result;
121}
122
123[[nodiscard]]
124static rdpMonitor monitor(const sdl_display_offsets::Offsets& o, SDL_DisplayID id)
125{
126 const auto& pixel = o.at(id).second;
127 rdpMonitor m = {};
128 m.x = pixel.x;
129 m.y = pixel.y;
130 m.width = pixel.w;
131 m.height = pixel.h;
132 return m;
133}
134
135static void dump(const std::vector<Display>& displays, const std::vector<SDL_DisplayID>& ids,
136 const sdl_display_offsets::Offsets& o)
137{
138 for (size_t i = 0; i < displays.size(); i++)
139 {
140 const auto m = monitor(o, ids.at(i));
141 (void)fprintf(stderr, " %-8s id %u at %d,%d %dx%d\n", displays.at(i).name, ids.at(i), m.x,
142 m.y, m.width, m.height);
143 }
144}
145
146/* Calls check with the pixel layout for every primary (or those listed) and every assignment of
147 * display IDs (the displays are kept in a map ordered by ID). */
148template <typename Check>
149[[nodiscard]]
150static bool forEachOrder(const char* name, const std::vector<Display>& displays,
151 const std::vector<size_t>& selection, Check check,
152 const std::vector<size_t>& primaries = {})
153{
154 std::vector<SDL_DisplayID> ids(displays.size());
155 std::iota(ids.begin(), ids.end(), 1);
156 do
157 {
158 std::vector<SDL_DisplayID> sel;
159 sel.reserve(selection.size());
160 for (const auto i : selection)
161 sel.push_back(ids.at(i));
162 for (size_t primary = 0; primary < displays.size(); primary++)
163 {
164 if (!primaries.empty() &&
165 (std::find(primaries.begin(), primaries.end(), primary) == primaries.end()))
166 continue;
167 const auto o = offsets(displays, ids, primary, sel);
168 std::vector<rdpMonitor> list;
169 list.reserve(displays.size());
170 for (size_t i = 0; i < displays.size(); i++)
171 list.push_back(monitor(o, ids.at(i)));
172 if (!check(list))
173 {
174 (void)fprintf(stderr, "FAIL: %s (primary %s)\n", name, displays.at(primary).name);
175 dump(displays, ids, o);
176 return false;
177 }
178 }
179 } while (std::next_permutation(ids.begin(), ids.end()));
180 return true;
181}
182
183/* The selected displays (empty: all) form a valid layout. */
184[[nodiscard]]
185static bool checkValid(const char* name, const std::vector<Display>& displays,
186 const std::vector<size_t>& selection = {})
187{
188 return forEachOrder(
189 name, displays, selection,
190 [&](const std::vector<rdpMonitor>& list)
191 {
192 std::vector<rdpMonitor> sent;
193 for (size_t i = 0; i < list.size(); i++)
194 {
195 if (selection.empty() ||
196 (std::find(selection.begin(), selection.end(), i) != selection.end()))
197 sent.push_back(list.at(i));
198 }
199 return valid(sent);
200 });
201}
202
203/* Whenever the walk gives a valid layout, the result is valid too: every primary and every
204 * assignment of display IDs. */
205[[nodiscard]]
206static bool checkNotWorse(const char* name, const std::vector<Display>& displays,
207 const std::vector<size_t>& selection = {})
208{
209 std::vector<SDL_DisplayID> ids(displays.size());
210 std::iota(ids.begin(), ids.end(), 1);
211 do
212 {
213 std::vector<SDL_DisplayID> sel;
214 sel.reserve(selection.size());
215 for (const auto i : selection)
216 sel.push_back(ids.at(i));
217 for (size_t primary = 0; primary < displays.size(); primary++)
218 {
219 const auto o = offsets(displays, ids, primary, sel);
220 auto before = o;
221 sdl_display_offsets::walk(before, ids.at(primary));
222 std::vector<rdpMonitor> ours;
223 std::vector<rdpMonitor> walked;
224 for (size_t i = 0; i < displays.size(); i++)
225 {
226 if (selection.empty() ||
227 (std::find(selection.begin(), selection.end(), i) != selection.end()))
228 {
229 ours.push_back(monitor(o, ids.at(i)));
230 walked.push_back(monitor(before, ids.at(i)));
231 }
232 }
233 if (valid(walked) && !valid(ours))
234 {
235 (void)fprintf(stderr, "FAIL: %s is worse than the walk (primary %s)\n", name,
236 displays.at(primary).name);
237 dump(displays, ids, o);
238 return false;
239 }
240 }
241 } while (std::next_permutation(ids.begin(), ids.end()));
242 return true;
243}
244
245[[nodiscard]]
246static bool checkDesk()
247{
248 /* A macOS arrangement in points, with the IDs and the fullscreen built-in size the client
249 * reports on a MacBook Air: a 1x FHD display above the 2x built-in, left aligned, and a 2x 4K
250 * display right of the FHD one, top aligned. The 4K and the built-in do not touch. */
251 const std::vector<Display> desk{
252 { "built-in", { 0, 0, 1470, 956 }, 2940, 1846 },
253 { "4k", { 1920, -1080, 1920, 1080 }, 3840, 2160 },
254 { "fhd", { 0, -1080, 1920, 1080 }, 1920, 1080 },
255 };
256
257 /* Every selection with every primary and every assignment of display IDs. The built-in and
258 * the 4K do not touch: from the 4K, the built-in is placed through the FHD, which does not
259 * bring them together, so that selection is only not worse than the walk. */
260 if (!checkValid("all three displays", desk) ||
261 !checkValid("the built-in and the FHD", desk, { 0, 2 }) ||
262 !checkNotWorse("the built-in and the 4K", desk, { 0, 1 }) ||
263 !checkValid("the FHD and the 4K", desk, { 1, 2 }))
264 return false;
265
266 /* With the built-in as primary, the layout the walk got wrong: the FHD on the built-in, left
267 * aligned, and the 4K right of the FHD, on the built-in's top edge. */
268 const std::vector<SDL_DisplayID> ids{ 1, 2, 3 };
269 for (const auto& selection : std::vector<std::vector<SDL_DisplayID>>{ {}, { 1, 3 }, { 1, 2 } })
270 {
271 const auto o = offsets(desk, ids, 0, selection);
272 const auto uhd = monitor(o, 2);
273 const auto fhd = monitor(o, 3);
274 const bool withFhd = selection.empty() || (selection.at(1) == 3);
275 const bool withUhd = selection.empty() || (selection.at(1) == 2);
276 if (!expect((!withFhd || ((fhd.x == 0) && (fhd.y == -1080))) &&
277 (!withUhd || ((uhd.x == 1920) && (uhd.y == -2160))),
278 "the desk layout keeps the FHD above the built-in and the 4K next to it"))
279 {
280 dump(desk, ids, o);
281 return false;
282 }
283 }
284 return true;
285}
286
287[[nodiscard]]
288static bool checkMixed()
289{
290 /* Ordinary MacBook layouts: valid with every primary and ID assignment. */
291 const std::vector<Display> under{ { "built-in", { 0, 0, 1470, 956 }, 2940, 1912 },
292 { "external", { 320, 956, 1920, 1080 }, 1920, 1080 } };
293 const std::vector<Display> beside{ { "built-in", { 0, 0, 1470, 956 }, 2940, 1912 },
294 { "fhd", { 1470, -539, 1920, 1080 }, 1920, 1080 } };
295 const std::vector<Display> siblings{ { "1x", { 0, 0, 1920, 1080 }, 1920, 1080 },
296 { "upper", { 1920, -540, 1920, 1080 }, 3840, 2160 },
297 { "lower", { 1920, 540, 1920, 1080 }, 3840, 2160 } };
298 const std::vector<Display> stacked{ { "built-in", { 0, 0, 1728, 1117 }, 3456, 2234 },
299 { "fhd", { 1728, 576, 1920, 1080 }, 1920, 1080 },
300 { "qhd", { 1408, 1656, 2560, 1440 }, 2560, 1440 } };
301 if (!checkValid("a display below a 2x MacBook", under) ||
302 !checkValid("a display beside a 2x MacBook", beside) ||
303 !checkValid("two 2x displays right of a 1x one", siblings))
304 return false;
305
306 /* Layouts that the placement does not always solve: never worse than the walk. */
307 const std::vector<Display> below{ { "2x", { 0, 0, 1920, 1080 }, 3840, 2160 },
308 { "a", { -960, 1080, 1920, 1080 }, 1920, 1080 },
309 { "b", { 960, 1080, 1920, 1080 }, 1920, 1080 } };
310 const std::vector<Display> corner{ { "1x", { 0, 0, 1920, 1080 }, 1920, 1080 },
311 { "right", { 1920, 0, 1920, 1080 }, 1920, 1080 },
312 { "2x", { -1920, -1080, 1920, 1080 }, 3840, 2160 } };
313 const std::vector<Display> blocked{ { "1x", { 0, 0, 1920, 1080 }, 1920, 1080 },
314 { "portrait", { 1920, -840, 1080, 1920 }, 1080, 1920 },
315 { "right", { 3000, 540, 1920, 1080 }, 1920, 1080 },
316 { "2x", { 0, 1080, 1920, 1080 }, 3840, 2160 } };
317 const std::vector<Display> stack{ { "built-in", { 0, 0, 1728, 1117 }, 3456, 2234 },
318 { "portrait", { 0, 1117, 1080, 1920 }, 1080, 1920 },
319 { "left", { -1260, 3037, 1920, 1080 }, 1920, 1080 },
320 { "farleft", { -3180, 3037, 1920, 1080 }, 1920, 1080 } };
321 const std::vector<Display> sides{ { "built-in", { 0, 0, 1728, 1117 }, 3456, 2234 },
322 { "left", { -1920, 0, 1920, 1080 }, 1920, 1080 },
323 { "upper left", { -1920, -1080, 1920, 1080 }, 1920, 1080 },
324 { "right", { 1728, -720, 2560, 1440 }, 2560, 1440 } };
325 return checkNotWorse("two displays stacked right of a 2x MacBook", stacked) &&
326 checkNotWorse("two displays below an unselected 2x one", below, { 1, 2 }) &&
327 checkNotWorse("a 2x display at a corner", corner) &&
328 checkNotWorse("a 2x display between others", blocked) &&
329 checkNotWorse("a MacBook with displays below it", stack) &&
330 checkNotWorse("a MacBook with displays on both sides", sides, { 0, 2, 3 });
331}
332
333[[nodiscard]]
334static bool checkKept(const char* name, const std::vector<Display>& displays,
335 const std::vector<size_t>& selection = {})
336{
337 /* Relative to the first selected display, as the primary changes; all displays have the
338 * scale of the first one. */
339 const size_t ref = selection.empty() ? 0 : selection.front();
340 const int scale = displays.at(ref).width / displays.at(ref).points.w;
341 return forEachOrder(
342 name, displays, selection,
343 [&](const std::vector<rdpMonitor>& list)
344 {
345 for (size_t i = 0; i < list.size(); i++)
346 {
347 if (!selection.empty() &&
348 (std::find(selection.begin(), selection.end(), i) == selection.end()))
349 continue;
350 if ((list.at(i).x - list.at(ref).x !=
351 scale * (displays.at(i).points.x - displays.at(ref).points.x)) ||
352 (list.at(i).y - list.at(ref).y !=
353 scale * (displays.at(i).points.y - displays.at(ref).points.y)))
354 return false;
355 }
356 return true;
357 });
358}
359
360[[nodiscard]]
361static bool checkSameScale()
362{
363 const std::vector<Display> plain{ { "a", { 0, 0, 1920, 1080 }, 1920, 1080 },
364 { "b", { 1920, 0, 1920, 1080 }, 1920, 1080 },
365 { "c", { 0, 1080, 1920, 1080 }, 1920, 1080 } };
366 /* The layout from #13507 (wlroots). */
367 const std::vector<Display> wide{ { "ultrawide", { 0, 0, 3440, 1440 }, 3440, 1440 },
368 { "above", { -400, -1080, 3840, 1080 }, 3840, 1080 },
369 { "portrait", { 3440, -821, 1440, 2560 }, 1440, 2560 } };
370 /* Displays touching only at a corner, without the primary they touch. */
371 const std::vector<Display> corner{ { "primary", { 0, 0, 1920, 1080 }, 1920, 1080 },
372 { "a", { 1920, 1080, 1920, 1080 }, 1920, 1080 },
373 { "b", { 3840, 1080, 1920, 1080 }, 1920, 1080 } };
374 /* Closing the gap between "left" and "above" would break the edge "left" shares with
375 * "farleft". */
376 const std::vector<Display> row{ { "primary", { 0, 0, 1080, 1920 }, 1080, 1920 },
377 { "left", { -1920, 1080, 1920, 1080 }, 1920, 1080 },
378 { "farleft", { -3840, 1080, 1920, 1080 }, 1920, 1080 },
379 { "above", { -400, -1080, 1920, 1080 }, 1920, 1080 } };
380 /* The same at 2x. */
381 std::vector<Display> retina = row;
382 for (auto& d : retina)
383 {
384 d.width *= 2;
385 d.height *= 2;
386 }
387 return checkKept("same-scale displays keep their positions", plain) &&
388 checkKept("the wlroots layout is kept", wide) &&
389 checkKept("selected displays away from the primary keep their positions", corner,
390 { 1, 2 }) &&
391 checkKept("a row of displays below another keeps its positions", row) &&
392 checkKept("2x displays keep their point layout", retina);
393}
394
395[[nodiscard]]
396static bool checkEdgeCases()
397{
398 sdl_display_offsets::Offsets none;
399 sdl_display_offsets::update(none, 1);
400
401 /* A primary that is not in the list: the first display takes its place. */
402 const std::vector<Display> row{ { "a", { 0, 0, 1920, 1080 }, 1920, 1080 },
403 { "b", { 1920, 0, 1920, 1080 }, 1920, 1080 },
404 { "c", { 3840, 0, 1920, 1080 }, 3840, 2160 } };
405 sdl_display_offsets::Offsets missing;
406 for (size_t i = 0; i < row.size(); i++)
407 missing.emplace(
408 static_cast<SDL_DisplayID>(i + 1),
409 std::pair{ row.at(i).points, SDL_Rect{ 0, 0, row.at(i).width, row.at(i).height } });
410 sdl_display_offsets::update(missing, 42);
411 return expect((monitor(missing, 1).x == 0) && (monitor(missing, 2).x == 1920) &&
412 (monitor(missing, 3).x == 3840),
413 "a missing primary is replaced by the first display");
414}
415
416int main()
417{
418 if (!checkDesk() || !checkMixed() || !checkSameScale() || !checkEdgeCases())
419 return 1;
420 return 0;
421}