FreeRDP
Loading...
Searching...
No Matches
sdl_display_offsets Namespace Reference

Typedefs

using Offsets = std::map<SDL_DisplayID, std::pair<SDL_Rect, SDL_Rect>>

Functions

bool alignX (const SDL_Rect &a, const SDL_Rect &b)
bool alignY (const SDL_Rect &a, const SDL_Rect &b)
std::vector< SDL_DisplayID > updateDisplayOffsetsForNeighbours (Offsets &offsets, SDL_DisplayID id, const std::vector< SDL_DisplayID > &ignore={})
void walk (Offsets &offsets, SDL_DisplayID primary)
bool spans (int a, int alen, int b, int blen)
bool touchX (const SDL_Rect &a, const SDL_Rect &b)
bool touchY (const SDL_Rect &a, const SDL_Rect &b)
bool overlap (const SDL_Rect &a, const SDL_Rect &b)
int scaled (int distance, int logical, int pixel)
int along (int ePos, int eLogical, int ePixel, int fPos, int fLogical, int fPixel, int fPixelPos, bool fromEnd)
bool isPlaced (const std::vector< SDL_DisplayID > &placed, SDL_DisplayID id)
std::vector< SDL_DisplayID > placeNeighbours (Offsets &offsets, SDL_DisplayID id, std::vector< SDL_DisplayID > &placed)
void placeOnce (Offsets &offsets, SDL_DisplayID primary)
bool valid (const Offsets &offsets, const std::vector< SDL_DisplayID > &selection)
void update (Offsets &offsets, SDL_DisplayID primary, const std::vector< SDL_DisplayID > &selection={})

Detailed Description

FreeRDP: A Remote Desktop Protocol Implementation SDL Client

Copyright 2026 FreeRDP contributors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Typedef Documentation

◆ Offsets

using sdl_display_offsets::Offsets = std::map<SDL_DisplayID, std::pair<SDL_Rect, SDL_Rect>>

Definition at line 35 of file sdl_display_offsets.hpp.

Function Documentation

◆ alignX()

bool sdl_display_offsets::alignX ( const SDL_Rect & a,
const SDL_Rect & b )
inline

Definition at line 37 of file sdl_display_offsets.hpp.

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 }

◆ alignY()

bool sdl_display_offsets::alignY ( const SDL_Rect & a,
const SDL_Rect & b )
inline

Definition at line 46 of file sdl_display_offsets.hpp.

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 }

◆ along()

int sdl_display_offsets::along ( int ePos,
int eLogical,
int ePixel,
int fPos,
int fLogical,
int fPixel,
int fPixelPos,
bool fromEnd )
inline

Definition at line 185 of file sdl_display_offsets.hpp.

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 }

◆ isPlaced()

bool sdl_display_offsets::isPlaced ( const std::vector< SDL_DisplayID > & placed,
SDL_DisplayID id )
inline

Definition at line 200 of file sdl_display_offsets.hpp.

201 {
202 return std::find(placed.begin(), placed.end(), id) != placed.end();
203 }

◆ overlap()

bool sdl_display_offsets::overlap ( const SDL_Rect & a,
const SDL_Rect & b )
inline

Definition at line 169 of file sdl_display_offsets.hpp.

170 {
171 return spans(a.x, a.w, b.x, b.w) && spans(a.y, a.h, b.y, b.h);
172 }

◆ placeNeighbours()

std::vector< SDL_DisplayID > sdl_display_offsets::placeNeighbours ( Offsets & offsets,
SDL_DisplayID id,
std::vector< SDL_DisplayID > & placed )
inline

Definition at line 207 of file sdl_display_offsets.hpp.

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 }

◆ placeOnce()

void sdl_display_offsets::placeOnce ( Offsets & offsets,
SDL_DisplayID primary )
inline

Definition at line 251 of file sdl_display_offsets.hpp.

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 }

◆ scaled()

int sdl_display_offsets::scaled ( int distance,
int logical,
int pixel )
inline

Definition at line 175 of file sdl_display_offsets.hpp.

176 {
177 if (logical <= 0)
178 return distance;
179 return static_cast<int>(std::lround(static_cast<double>(distance) * pixel / logical));
180 }

◆ spans()

bool sdl_display_offsets::spans ( int a,
int alen,
int b,
int blen )
inline

Definition at line 152 of file sdl_display_offsets.hpp.

153 {
154 return (a < b + blen) && (b < a + alen);
155 }

◆ touchX()

bool sdl_display_offsets::touchX ( const SDL_Rect & a,
const SDL_Rect & b )
inline

Definition at line 159 of file sdl_display_offsets.hpp.

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 }

◆ touchY()

bool sdl_display_offsets::touchY ( const SDL_Rect & a,
const SDL_Rect & b )
inline

Definition at line 164 of file sdl_display_offsets.hpp.

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 }

◆ update()

void sdl_display_offsets::update ( Offsets & offsets,
SDL_DisplayID primary,
const std::vector< SDL_DisplayID > & selection = {} )
inline

Definition at line 300 of file sdl_display_offsets.hpp.

301 {})
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 }

◆ updateDisplayOffsetsForNeighbours()

std::vector< SDL_DisplayID > sdl_display_offsets::updateDisplayOffsetsForNeighbours ( Offsets & offsets,
SDL_DisplayID id,
const std::vector< SDL_DisplayID > & ignore = {} )
inline

Definition at line 56 of file sdl_display_offsets.hpp.

57 {})
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 }

◆ valid()

bool sdl_display_offsets::valid ( const Offsets & offsets,
const std::vector< SDL_DisplayID > & selection )
inline

Definition at line 267 of file sdl_display_offsets.hpp.

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 }

◆ walk()

void sdl_display_offsets::walk ( Offsets & offsets,
SDL_DisplayID primary )
inline

Definition at line 122 of file sdl_display_offsets.hpp.

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 }