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 sdl->applyMonitorScaleOverride(monitor);
209 monitor.is_primary = true;
210 monitor.x = 0;
211 monitor.y = 0;
212 monitors.emplace_back(monitor);
213 return freerdp_settings_set_monitor_def_array_sorted(settings, monitors.data(),
214 monitors.size());
215 }
216 return TRUE;
217 }
218 for (const auto& id : sdl->monitorIds())
219 {
220 auto monitor = sdl->getDisplay(id);
221 sdl->applyMonitorScaleOverride(monitor);
222 monitors.emplace_back(monitor);
223 }
224 // With an explicit /monitors: list the first entry is the primary (like xfreerdp),
225 // not whatever the local desktop considers primary.
226 if (!monitors.empty() && (freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds) > 0))
227 {
228 for (auto& monitor : monitors)
229 monitor.is_primary = FALSE;
230 monitors.at(0).is_primary = TRUE;
231 }
232 // /monitors: may select a subset that excludes the SDL primary. The
233 // library rejects arrays without a primary, so promote the first entry
234 // when none is marked.
235 if (!monitors.empty() && std::none_of(monitors.cbegin(), monitors.cend(),
236 [](const rdpMonitor& m) { return m.is_primary; }))
237 monitors.at(0).is_primary = true;
238 return freerdp_settings_set_monitor_def_array_sorted(settings, monitors.data(),
239 monitors.size());
240}
241
242[[nodiscard]] static BOOL sdl_detect_single_window(SdlContext* sdl, UINT32* pMaxWidth,
243 UINT32* pMaxHeight)
244{
245 WINPR_ASSERT(sdl);
246 WINPR_ASSERT(pMaxWidth);
247 WINPR_ASSERT(pMaxHeight);
248
249 rdpSettings* settings = sdl->context()->settings;
250 WINPR_ASSERT(settings);
251
252 if ((!freerdp_settings_get_bool(settings, FreeRDP_UseMultimon) &&
253 !freerdp_settings_get_bool(settings, FreeRDP_SpanMonitors)) ||
254 (freerdp_settings_get_bool(settings, FreeRDP_Workarea) &&
255 !freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode)))
256 {
257 /* If no monitors were specified on the command-line then set the current monitor as active
258 */
259 if (freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds) == 0)
260 {
261 SDL_DisplayID id = 0;
262 const auto& ids = sdl->monitorIds();
263 if (!ids.empty())
264 {
265 id = ids.front();
266 }
267 sdl->setMonitorIds({ id });
268 }
269
270 if (!sdl_apply_display_properties(sdl))
271 return FALSE;
272 return sdl_apply_max_size(sdl, pMaxWidth, pMaxHeight);
273 }
274 return sdl_apply_mon_max_size(sdl, pMaxWidth, pMaxHeight);
275}
276
277BOOL sdl_detect_monitors(SdlContext* sdl, UINT32* pMaxWidth, UINT32* pMaxHeight)
278{
279 WINPR_ASSERT(sdl);
280 WINPR_ASSERT(pMaxWidth);
281 WINPR_ASSERT(pMaxHeight);
282
283 rdpSettings* settings = sdl->context()->settings;
284 WINPR_ASSERT(settings);
285
286 const auto& ids = sdl->getDisplayIds();
287 auto nr = freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds);
288 /* Announce all monitors in RemoteApp mode so windows can maximize on secondary displays. */
289 if ((nr == 0) && freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode) &&
290 !freerdp_settings_get_bool(settings, FreeRDP_UseMultimon))
291 {
292 if (!freerdp_settings_set_bool(settings, FreeRDP_UseMultimon, TRUE))
293 return FALSE;
294 }
295 if (nr == 0)
296 {
297 if (freerdp_settings_get_bool(settings, FreeRDP_UseMultimon))
298 sdl->setMonitorIds(ids);
299 else
300 {
301 sdl->setMonitorIds({ ids.front() });
302 }
303 }
304 else
305 {
306 /* There were more IDs supplied than there are monitors */
307 if (nr > ids.size())
308 {
309 WLog_ERR(TAG,
310 "Found %" PRIu32 " monitor IDs, but only have %" PRIuz " monitors connected",
311 nr, ids.size());
312 return FALSE;
313 }
314
315 std::vector<SDL_DisplayID> used;
316 for (size_t x = 0; x < nr; x++)
317 {
318 auto cur = static_cast<const UINT32*>(
319 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorIds, x));
320 WINPR_ASSERT(cur);
321
322 SDL_DisplayID id = *cur;
323
324 /* the ID is no valid monitor index */
325 if (std::find(ids.begin(), ids.end(), id) == ids.end())
326 {
327 WLog_ERR(TAG, "Supplied monitor ID[%" PRIuz "]=%" PRIu32 " is invalid", x, id);
328 return FALSE;
329 }
330
331 /* The ID is already taken */
332 if (std::find(used.begin(), used.end(), id) != used.end())
333 {
334 WLog_ERR(TAG, "Duplicate monitor ID[%" PRIuz "]=%" PRIu32 " detected", x, id);
335 return FALSE;
336 }
337 used.push_back(id);
338 }
339 sdl->setMonitorIds(used);
340 }
341
342 if (!sdl_apply_display_properties(sdl))
343 return FALSE;
344
345 auto size = static_cast<uint32_t>(sdl->monitorIds().size());
346 if (!freerdp_settings_set_uint32(settings, FreeRDP_NumMonitorIds, size))
347 return FALSE;
348
349 return sdl_detect_single_window(sdl, pMaxWidth, pMaxHeight);
350}
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.