FreeRDP
Loading...
Searching...
No Matches
sdl_display_offsets.hpp
1
19#pragma once
20
21#include <algorithm>
22#include <cmath>
23#include <map>
24#include <tuple>
25#include <utility>
26#include <vector>
27
28#include <SDL3/SDL.h>
29
30/* Pixel origins of the local displays, derived from their logical (point) bounds and their pixel
31 * sizes. Each entry maps a display to its logical bounds and its pixel rectangle; only the pixel
32 * origin is computed. */
34{
35 using Offsets = std::map<SDL_DisplayID, std::pair<SDL_Rect, SDL_Rect>>;
36
37 inline bool alignX(const SDL_Rect& a, const SDL_Rect& b)
38 {
39 if (a.x + a.w == b.x)
40 return true;
41 if (b.x + b.w == a.x)
42 return true;
43 return false;
44 }
45
46 inline bool alignY(const SDL_Rect& a, const SDL_Rect& b)
47 {
48 if (a.y + a.h == b.y)
49 return true;
50 if (b.y + b.h == a.y)
51 return true;
52 return false;
53 }
54
55 inline std::vector<SDL_DisplayID>
56 updateDisplayOffsetsForNeighbours(Offsets& offsets, SDL_DisplayID id,
57 const std::vector<SDL_DisplayID>& ignore = {})
58 {
59 auto first = offsets.at(id);
60 std::vector<SDL_DisplayID> neighbours;
61
62 for (auto& entry : offsets)
63 {
64 if (entry.first == id)
65 continue;
66 if (std::find(ignore.begin(), ignore.end(), entry.first) != ignore.end())
67 continue;
68
69 bool neighbor = false;
70 const bool ax = alignX(entry.second.first, first.first);
71 const bool ay = alignY(entry.second.first, first.first);
72
73 /* Offset along the axis that does not touch, converted from logical to pixel
74 * units of the reference display. Without this, a monitor that is adjacent on
75 * one axis but shifted on the other (e.g. a wider monitor above another, or a
76 * rotated monitor that is not top aligned) keeps offset 0 on that axis, the
77 * layout sent to the server no longer matches the local one and pointer input
78 * lands offset by that difference. */
79 auto crossOffset = [](int ePos, int fPos, int fLogical, int fPixel) -> int
80 {
81 if (fLogical <= 0)
82 return ePos - fPos;
83 return static_cast<int>(
84 std::lround(static_cast<double>(ePos - fPos) * fPixel / fLogical));
85 };
86
87 if (ax)
88 {
89 if (entry.second.first.x < first.first.x)
90 entry.second.second.x = first.second.x - entry.second.second.w;
91 else
92 entry.second.second.x = first.second.x + first.second.w;
93 if (!ay)
94 entry.second.second.y =
95 first.second.y + crossOffset(entry.second.first.y, first.first.y,
96 first.first.h, first.second.h);
97 neighbor = true;
98 }
99 if (ay)
100 {
101 if (entry.second.first.y < first.first.y)
102 entry.second.second.y = first.second.y - entry.second.second.h;
103 else
104 entry.second.second.y = first.second.y + first.second.h;
105 if (!ax)
106 entry.second.second.x =
107 first.second.x + crossOffset(entry.second.first.x, first.first.x,
108 first.first.w, first.second.w);
109 neighbor = true;
110 }
111
112 if (neighbor)
113 neighbours.push_back(entry.first);
114 }
115 return neighbours;
116 }
117
118 /* Places the primary display at its origin, then walks outwards over its neighbours. With
119 * mixed scales this can leave gaps or overlaps: displays whose edges are merely in line count
120 * as neighbours, and a display can be placed again from a later neighbour. update() uses it
121 * only when placeOnce() gives no valid layout. */
122 inline void walk(Offsets& offsets, SDL_DisplayID primary)
123 {
124 /* 1. Find primary and update all neighbors
125 * 2. For each neighbor update all neighbors
126 * 3. repeat until all displays updated.
127 */
128 /* Align display offsets to the primary display origin. */
129 auto it = offsets.find(primary);
130 if (it != offsets.end())
131 {
132 it->second.second.x = it->second.first.x;
133 it->second.second.y = it->second.first.y;
134 }
135 std::vector<SDL_DisplayID> handled;
136 handled.push_back(primary);
137
138 auto neighbors = updateDisplayOffsetsForNeighbours(offsets, primary);
139 while (!neighbors.empty())
140 {
141 auto neighbor = neighbors.front();
142 neighbors.erase(neighbors.begin());
143
144 if (std::find(handled.begin(), handled.end(), neighbor) != handled.end())
145 continue;
146 handled.push_back(neighbor);
147
148 auto next = updateDisplayOffsetsForNeighbours(offsets, neighbor, handled);
149 neighbors.insert(neighbors.end(), next.begin(), next.end());
150 }
151 }
152 inline bool spans(int a, int alen, int b, int blen)
153 {
154 return (a < b + blen) && (b < a + alen);
155 }
156
157 /* The two displays share an edge segment of positive length beside each other (touchX) or
158 * above each other (touchY); edges that are merely in line do not count. */
159 inline bool touchX(const SDL_Rect& a, const SDL_Rect& b)
160 {
161 return ((a.x + a.w == b.x) || (b.x + b.w == a.x)) && spans(a.y, a.h, b.y, b.h);
162 }
163
164 inline bool touchY(const SDL_Rect& a, const SDL_Rect& b)
165 {
166 return ((a.y + a.h == b.y) || (b.y + b.h == a.y)) && spans(a.x, a.w, b.x, b.w);
167 }
168
169 inline bool overlap(const SDL_Rect& a, const SDL_Rect& b)
170 {
171 return spans(a.x, a.w, b.x, b.w) && spans(a.y, a.h, b.y, b.h);
172 }
173
174 /* Distance in points, converted to pixels of the display it lies on. */
175 inline int scaled(int distance, int logical, int pixel)
176 {
177 if (logical <= 0)
178 return distance;
179 return static_cast<int>(std::lround(static_cast<double>(distance) * pixel / logical));
180 }
181
182 /* Pixel position of display e along the edge it shares with the placed display f. The
183 * offset between their starts lies on whichever display starts first, so it is converted
184 * by that one. With fromEnd, the offset between their ends is kept instead. */
185 inline int along(int ePos, int eLogical, int ePixel, int fPos, int fLogical, int fPixel,
186 int fPixelPos, bool fromEnd)
187 {
188 if (!fromEnd)
189 {
190 const int d = ePos - fPos;
191 return fPixelPos +
192 ((d >= 0) ? scaled(d, fLogical, fPixel) : scaled(d, eLogical, ePixel));
193 }
194 const int d = (ePos + eLogical) - (fPos + fLogical);
195 const int end = fPixelPos + fPixel +
196 ((d <= 0) ? scaled(d, fLogical, fPixel) : scaled(d, eLogical, ePixel));
197 return end - ePixel;
198 }
199
200 inline bool isPlaced(const std::vector<SDL_DisplayID>& placed, SDL_DisplayID id)
201 {
202 return std::find(placed.begin(), placed.end(), id) != placed.end();
203 }
204
205 /* Places each display that shares an edge with display id and is not placed yet next to
206 * it, and returns them. */
207 inline std::vector<SDL_DisplayID> placeNeighbours(Offsets& offsets, SDL_DisplayID id,
208 std::vector<SDL_DisplayID>& placed)
209 {
210 const auto f = offsets.at(id);
211 std::vector<SDL_DisplayID> result;
212 for (auto& entry : offsets)
213 {
214 const auto& e = entry.second.first;
215 auto& pixel = entry.second.second;
216 const bool beside = touchX(e, f.first);
217 if (isPlaced(placed, entry.first) || (!beside && !touchY(e, f.first)))
218 continue;
219
220 const auto place = [&](bool fromEnd)
221 {
222 if (beside)
223 {
224 pixel.x = (e.x < f.first.x) ? f.second.x - pixel.w : f.second.x + f.second.w;
225 pixel.y = along(e.y, e.h, pixel.h, f.first.y, f.first.h, f.second.h, f.second.y,
226 fromEnd);
227 }
228 else
229 {
230 pixel.y = (e.y < f.first.y) ? f.second.y - pixel.h : f.second.y + f.second.h;
231 pixel.x = along(e.x, e.w, pixel.w, f.first.x, f.first.w, f.second.w, f.second.x,
232 fromEnd);
233 }
234 return std::none_of(placed.begin(), placed.end(), [&](SDL_DisplayID other)
235 { return overlap(pixel, offsets.at(other).second); });
236 };
237
238 /* A 2x display is larger in pixels than in points, so keeping the near edges in
239 * line can overlap a display placed before; keep the far edges in line then. */
240 if (!place(false) && !place(true))
241 std::ignore = place(false);
242 placed.push_back(entry.first);
243 result.push_back(entry.first);
244 }
245 return result;
246 }
247
248 /* The walk without its two flaws: only displays sharing an edge segment are neighbours,
249 * and each display is placed once, next to the first placed display it shares an edge
250 * with. Displays not reached keep their position. */
251 inline void placeOnce(Offsets& offsets, SDL_DisplayID primary)
252 {
253 auto& origin = offsets.at(primary);
254 origin.second.x = origin.first.x;
255 origin.second.y = origin.first.y;
256 std::vector<SDL_DisplayID> placed{ primary };
257 std::vector<SDL_DisplayID> next{ primary };
258 for (size_t i = 0; i < next.size(); i++)
259 {
260 const auto more = placeNeighbours(offsets, next[i], placed);
261 next.insert(next.end(), more.begin(), more.end());
262 }
263 }
264
265 /* No two selected displays overlap, and they form one area, connected through shared
266 * edges. */
267 inline bool valid(const Offsets& offsets, const std::vector<SDL_DisplayID>& selection)
268 {
269 std::vector<SDL_DisplayID> ids;
270 for (const auto& entry : offsets)
271 {
272 if (selection.empty() || isPlaced(selection, entry.first))
273 ids.push_back(entry.first);
274 }
275 for (size_t i = 0; i < ids.size(); i++)
276 for (size_t j = i + 1; j < ids.size(); j++)
277 if (overlap(offsets.at(ids[i]).second, offsets.at(ids[j]).second))
278 return false;
279 if (ids.empty())
280 return true;
281
282 std::vector<SDL_DisplayID> reached{ ids.front() };
283 for (size_t i = 0; i < reached.size(); i++)
284 {
285 const auto& a = offsets.at(reached[i]).second;
286 for (const auto id : ids)
287 {
288 const auto& b = offsets.at(id).second;
289 if (!isPlaced(reached, id) && (touchX(a, b) || touchY(a, b)))
290 reached.push_back(id);
291 }
292 }
293 return reached.size() == ids.size();
294 }
295
296 /* Pixel origins of all displays, starting with the primary at its point origin.
297 * selection lists the displays sent to the server (empty: all). The displays are placed
298 * with placeOnce(); if the selected ones then overlap or do not form one area, the walk is
299 * used, as before, and the layout check reports the problem. */
300 inline void update(Offsets& offsets, SDL_DisplayID primary,
301 const std::vector<SDL_DisplayID>& selection = {})
302 {
303 if (offsets.empty())
304 return;
305 if (offsets.count(primary) == 0)
306 primary = offsets.begin()->first;
307
308 auto placed = offsets;
309 placeOnce(placed, primary);
310 if (valid(placed, selection))
311 offsets = placed;
312 else
313 walk(offsets, primary);
314 }
315} // namespace sdl_display_offsets