FreeRDP
Loading...
Searching...
No Matches
SDL3/sdl_monitor.cpp
1
21
22#include <freerdp/config.h>
23
24#include <cmath>
25#include <cstdio>
26#include <cstdlib>
27#include <cstring>
28#include <algorithm>
29
30#include <SDL3/SDL.h>
31
32#include <winpr/assert.h>
33#include <winpr/crt.h>
34
35#include <freerdp/log.h>
36
37#define TAG CLIENT_TAG("sdl")
38
39#include "sdl_monitor.hpp"
40#include "sdl_context.hpp"
41
42typedef struct
43{
44 RECTANGLE_16 area;
45 RECTANGLE_16 workarea;
46 BOOL primary;
47} MONITOR_INFO;
48
49typedef struct
50{
51 int nmonitors;
52 RECTANGLE_16 area;
53 RECTANGLE_16 workarea;
54 MONITOR_INFO* monitors;
55} VIRTUAL_SCREEN;
56
57/* See MSDN Section on Multiple Display Monitors: http://msdn.microsoft.com/en-us/library/dd145071
58 */
59
60int sdl_list_monitors(SdlContext* sdl)
61{
62 WINPR_ASSERT(sdl);
63 if (!SDL_Init(SDL_INIT_VIDEO))
64 {
65 WLog_Print(sdl->getWLog(), WLOG_ERROR, "SDL_Init failed: %s", SDL_GetError());
66 return -1;
67 }
68
69 int nmonitors = 0;
70 auto ids = SDL_GetDisplays(&nmonitors);
71
72 printf("listing %d monitors:\n", nmonitors);
73 for (int i = 0; i < nmonitors; i++)
74 {
75 SDL_Rect rect = {};
76 auto id = ids[i];
77 const auto brc = SDL_GetDisplayBounds(id, &rect);
78 const char* name = SDL_GetDisplayName(id);
79
80 if (!brc)
81 continue;
82 printf(" %s [%u] [%s] %dx%d\t+%d+%d\n", (i == 0) ? "*" : " ", id, name, rect.w, rect.h,
83 rect.x, rect.y);
84 }
85 SDL_free(ids);
86 SDL_Quit();
87 return 0;
88}
89
90[[nodiscard]] static BOOL sdl_apply_mon_max_size(SdlContext* sdl, UINT32* pMaxWidth,
91 UINT32* pMaxHeight)
92{
93 int32_t left = 0;
94 int32_t right = 0;
95 int32_t top = 0;
96 int32_t bottom = 0;
97
98 WINPR_ASSERT(pMaxWidth);
99 WINPR_ASSERT(pMaxHeight);
100
101 auto settings = sdl->context()->settings;
102 WINPR_ASSERT(settings);
103
104 for (size_t x = 0; x < freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount); x++)
105 {
106 auto monitor = static_cast<const rdpMonitor*>(
107 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, x));
108
109 left = std::min(monitor->x, left);
110 top = std::min(monitor->y, top);
111 right = std::max(monitor->x + monitor->width, right);
112 bottom = std::max(monitor->y + monitor->height, bottom);
113 }
114
115 const int32_t w = right - left;
116 const int32_t h = bottom - top;
117
118 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, w);
119 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, h);
120 return TRUE;
121}
122
123[[nodiscard]] static BOOL sdl_apply_max_size(SdlContext* sdl, UINT32* pMaxWidth, UINT32* pMaxHeight)
124{
125 WINPR_ASSERT(sdl);
126 WINPR_ASSERT(pMaxWidth);
127 WINPR_ASSERT(pMaxHeight);
128
129 auto settings = sdl->context()->settings;
130 WINPR_ASSERT(settings);
131
132 *pMaxWidth = 0;
133 *pMaxHeight = 0;
134
135 for (size_t x = 0; x < freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount); x++)
136 {
137 auto monitor = static_cast<const rdpMonitor*>(
138 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, x));
139
140 if (freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode))
141 {
142 /* RAIL requires full display coverage for 1:1 coordinates. Work area insets
143 * are reported via SPI_SET_WORK_AREA instead of clipping session size. */
144 SDL_Rect rect = {};
145 if (SDL_GetDisplayBounds(monitor->orig_screen, &rect))
146 {
147 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, rect.w);
148 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, rect.h);
149 }
150 }
151 else if (freerdp_settings_get_bool(settings, FreeRDP_Fullscreen))
152 {
153 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, monitor->width);
154 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, monitor->height);
155 }
156 else if (freerdp_settings_get_bool(settings, FreeRDP_Workarea))
157 {
158 SDL_Rect rect = {};
159 SDL_GetDisplayUsableBounds(monitor->orig_screen, &rect);
160 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, rect.w);
161 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, rect.h);
162 }
163 else if (freerdp_settings_get_uint32(settings, FreeRDP_PercentScreen) > 0)
164 {
165 SDL_Rect rect = {};
166 SDL_GetDisplayUsableBounds(monitor->orig_screen, &rect);
167
168 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, rect.w);
169 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, rect.h);
170
171 if (freerdp_settings_get_bool(settings, FreeRDP_PercentScreenUseWidth))
172 *pMaxWidth = (WINPR_ASSERTING_INT_CAST(uint32_t, rect.w) *
173 freerdp_settings_get_uint32(settings, FreeRDP_PercentScreen)) /
174 100;
175
176 if (freerdp_settings_get_bool(settings, FreeRDP_PercentScreenUseHeight))
177 *pMaxHeight = (WINPR_ASSERTING_INT_CAST(uint32_t, rect.h) *
178 freerdp_settings_get_uint32(settings, FreeRDP_PercentScreen)) /
179 100;
180 }
181 else if (freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) &&
182 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight))
183 {
184 *pMaxWidth = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
185 *pMaxHeight = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
186 }
187 }
188 return TRUE;
189}
190
191[[nodiscard]] static BOOL sdl_apply_display_properties(SdlContext* sdl)
192{
193 WINPR_ASSERT(sdl);
194
195 rdpSettings* settings = sdl->context()->settings;
196 WINPR_ASSERT(settings);
197
198 std::vector<rdpMonitor> monitors;
199 if (!freerdp_settings_get_bool(settings, FreeRDP_Fullscreen) &&
200 !freerdp_settings_get_bool(settings, FreeRDP_UseMultimon))
201 {
202 if (freerdp_settings_get_bool(settings, FreeRDP_Workarea))
203 {
204 if (sdl->monitorIds().empty())
205 return FALSE;
206 const auto id = sdl->monitorIds().front();
207 auto monitor = sdl->getDisplay(id);
208 monitor.is_primary = true;
209 monitor.x = 0;
210 monitor.y = 0;
211 monitors.emplace_back(monitor);
212 return freerdp_settings_set_monitor_def_array_sorted(settings, monitors.data(),
213 monitors.size());
214 }
215 return TRUE;
216 }
217 for (const auto& id : sdl->monitorIds())
218 {
219 const auto monitor = sdl->getDisplay(id);
220 monitors.emplace_back(monitor);
221 }
222 // With an explicit /monitors: list the first entry is the primary (like xfreerdp),
223 // not whatever the local desktop considers primary.
224 if (!monitors.empty() && (freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds) > 0))
225 {
226 for (auto& monitor : monitors)
227 monitor.is_primary = FALSE;
228 monitors.at(0).is_primary = TRUE;
229 }
230 // /monitors: may select a subset that excludes the SDL primary. The
231 // library rejects arrays without a primary, so promote the first entry
232 // when none is marked.
233 if (!monitors.empty() && std::none_of(monitors.cbegin(), monitors.cend(),
234 [](const rdpMonitor& m) { return m.is_primary; }))
235 monitors.at(0).is_primary = true;
236 return freerdp_settings_set_monitor_def_array_sorted(settings, monitors.data(),
237 monitors.size());
238}
239
240[[nodiscard]] static BOOL sdl_detect_single_window(SdlContext* sdl, UINT32* pMaxWidth,
241 UINT32* pMaxHeight)
242{
243 WINPR_ASSERT(sdl);
244 WINPR_ASSERT(pMaxWidth);
245 WINPR_ASSERT(pMaxHeight);
246
247 rdpSettings* settings = sdl->context()->settings;
248 WINPR_ASSERT(settings);
249
250 if ((!freerdp_settings_get_bool(settings, FreeRDP_UseMultimon) &&
251 !freerdp_settings_get_bool(settings, FreeRDP_SpanMonitors)) ||
252 (freerdp_settings_get_bool(settings, FreeRDP_Workarea) &&
253 !freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode)))
254 {
255 /* If no monitors were specified on the command-line then set the current monitor as active
256 */
257 if (freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds) == 0)
258 {
259 SDL_DisplayID id = 0;
260 const auto& ids = sdl->monitorIds();
261 if (!ids.empty())
262 {
263 id = ids.front();
264 }
265 sdl->setMonitorIds({ id });
266 }
267
268 if (!sdl_apply_display_properties(sdl))
269 return FALSE;
270 return sdl_apply_max_size(sdl, pMaxWidth, pMaxHeight);
271 }
272 return sdl_apply_mon_max_size(sdl, pMaxWidth, pMaxHeight);
273}
274
275BOOL sdl_detect_monitors(SdlContext* sdl, UINT32* pMaxWidth, UINT32* pMaxHeight)
276{
277 WINPR_ASSERT(sdl);
278 WINPR_ASSERT(pMaxWidth);
279 WINPR_ASSERT(pMaxHeight);
280
281 rdpSettings* settings = sdl->context()->settings;
282 WINPR_ASSERT(settings);
283
284 const auto& ids = sdl->getDisplayIds();
285 auto nr = freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds);
286 /* Announce all monitors in RemoteApp mode so windows can maximize on secondary displays. */
287 if ((nr == 0) && freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode) &&
288 !freerdp_settings_get_bool(settings, FreeRDP_UseMultimon))
289 {
290 if (!freerdp_settings_set_bool(settings, FreeRDP_UseMultimon, TRUE))
291 return FALSE;
292 }
293 if (nr == 0)
294 {
295 if (freerdp_settings_get_bool(settings, FreeRDP_UseMultimon))
296 sdl->setMonitorIds(ids);
297 else
298 {
299 sdl->setMonitorIds({ ids.front() });
300 }
301 }
302 else
303 {
304 /* There were more IDs supplied than there are monitors */
305 if (nr > ids.size())
306 {
307 WLog_ERR(TAG,
308 "Found %" PRIu32 " monitor IDs, but only have %" PRIuz " monitors connected",
309 nr, ids.size());
310 return FALSE;
311 }
312
313 std::vector<SDL_DisplayID> used;
314 for (size_t x = 0; x < nr; x++)
315 {
316 auto cur = static_cast<const UINT32*>(
317 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorIds, x));
318 WINPR_ASSERT(cur);
319
320 SDL_DisplayID id = *cur;
321
322 /* the ID is no valid monitor index */
323 if (std::find(ids.begin(), ids.end(), id) == ids.end())
324 {
325 WLog_ERR(TAG, "Supplied monitor ID[%" PRIuz "]=%" PRIu32 " is invalid", x, id);
326 return FALSE;
327 }
328
329 /* The ID is already taken */
330 if (std::find(used.begin(), used.end(), id) != used.end())
331 {
332 WLog_ERR(TAG, "Duplicate monitor ID[%" PRIuz "]=%" PRIu32 " detected", x, id);
333 return FALSE;
334 }
335 used.push_back(id);
336 }
337 sdl->setMonitorIds(used);
338 }
339
340 if (!sdl_apply_display_properties(sdl))
341 return FALSE;
342
343 auto size = static_cast<uint32_t>(sdl->monitorIds().size());
344 if (!freerdp_settings_set_uint32(settings, FreeRDP_NumMonitorIds, size))
345 return FALSE;
346
347 return sdl_detect_single_window(sdl, pMaxWidth, pMaxHeight);
348}
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL val)
Sets a BOOL settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_monitor_def_array_sorted(rdpSettings *settings, const rdpMonitor *monitors, size_t count)
Sort monitor array according to:
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.