21#include "../sdl_display_offsets.hpp"
29#include <freerdp/settings_types.h>
32static bool spansRange(int64_t a, int64_t alen, int64_t b, int64_t blen)
34 return (a < b + blen) && (b < a + alen);
40 return spansRange(a.x, a.width, b.x, b.width) && spansRange(a.y, a.height, b.y, b.height);
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))
53 return ((ay + a.height == by) || (by + b.height == ay)) && spansRange(ax, a.width, bx, b.width);
59static bool valid(
const std::vector<rdpMonitor>& list)
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)))
68 std::vector<bool> reached(list.size(),
false);
69 std::vector<size_t> next{ 0 };
73 const auto i = next.back();
75 for (
size_t j = 0; j < list.size(); j++)
77 if (!reached.at(j) && touch(list.at(i), list.at(j)))
84 return std::all_of(reached.begin(), reached.end(), [](
bool r) { return r; });
88static bool expect(
bool value,
const char* message)
91 (void)fprintf(stderr,
"FAIL: %s\n", message);
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 = {})
109 const auto origin = displays.at(primary).points;
110 sdl_display_offsets::Offsets result;
111 for (
size_t i = 0; i < displays.size(); i++)
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 });
119 sdl_display_offsets::update(result, ids.at(primary), selection);
124static rdpMonitor monitor(
const sdl_display_offsets::Offsets& o, SDL_DisplayID
id)
126 const auto& pixel = o.at(
id).second;
135static void dump(
const std::vector<Display>& displays,
const std::vector<SDL_DisplayID>& ids,
136 const sdl_display_offsets::Offsets& o)
138 for (
size_t i = 0; i < displays.size(); i++)
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);
148template <
typename Check>
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 = {})
154 std::vector<SDL_DisplayID> ids(displays.size());
155 std::iota(ids.begin(), ids.end(), 1);
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++)
164 if (!primaries.empty() &&
165 (std::find(primaries.begin(), primaries.end(), primary) == primaries.end()))
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)));
174 (void)fprintf(stderr,
"FAIL: %s (primary %s)\n", name, displays.at(primary).name);
175 dump(displays, ids, o);
179 }
while (std::next_permutation(ids.begin(), ids.end()));
185static bool checkValid(
const char* name,
const std::vector<Display>& displays,
186 const std::vector<size_t>& selection = {})
189 name, displays, selection,
190 [&](
const std::vector<rdpMonitor>& list)
192 std::vector<rdpMonitor> sent;
193 for (
size_t i = 0; i < list.size(); i++)
195 if (selection.empty() ||
196 (std::find(selection.begin(), selection.end(), i) != selection.end()))
197 sent.push_back(list.at(i));
206static bool checkNotWorse(
const char* name,
const std::vector<Display>& displays,
207 const std::vector<size_t>& selection = {})
209 std::vector<SDL_DisplayID> ids(displays.size());
210 std::iota(ids.begin(), ids.end(), 1);
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++)
219 const auto o = offsets(displays, ids, primary, sel);
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++)
226 if (selection.empty() ||
227 (std::find(selection.begin(), selection.end(), i) != selection.end()))
229 ours.push_back(monitor(o, ids.at(i)));
230 walked.push_back(monitor(before, ids.at(i)));
233 if (valid(walked) && !valid(ours))
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);
241 }
while (std::next_permutation(ids.begin(), ids.end()));
246static bool checkDesk()
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 },
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 }))
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 } })
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"))
288static bool checkMixed()
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))
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 });
334static bool checkKept(
const char* name,
const std::vector<Display>& displays,
335 const std::vector<size_t>& selection = {})
339 const size_t ref = selection.empty() ? 0 : selection.front();
340 const int scale = displays.at(ref).width / displays.at(ref).points.w;
342 name, displays, selection,
343 [&](
const std::vector<rdpMonitor>& list)
345 for (
size_t i = 0; i < list.size(); i++)
347 if (!selection.empty() &&
348 (std::find(selection.begin(), selection.end(), i) == selection.end()))
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)))
361static bool checkSameScale()
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 } };
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 } };
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 } };
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 } };
381 std::vector<Display> retina = row;
382 for (
auto& d : retina)
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,
391 checkKept(
"a row of displays below another keeps its positions", row) &&
392 checkKept(
"2x displays keep their point layout", retina);
396static bool checkEdgeCases()
398 sdl_display_offsets::Offsets none;
399 sdl_display_offsets::update(none, 1);
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++)
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");
418 if (!checkDesk() || !checkMixed() || !checkSameScale() || !checkEdgeCases())