FreeRDP
Loading...
Searching...
No Matches
SDL3/sdl_monitor.cpp
1
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([[maybe_unused]] SdlContext* sdl)
61{
62 SDL_Init(SDL_INIT_VIDEO);
63
64 int nmonitors = 0;
65 auto ids = SDL_GetDisplays(&nmonitors);
66
67 printf("listing %d monitors:\n", nmonitors);
68 for (int i = 0; i < nmonitors; i++)
69 {
70 SDL_Rect rect = {};
71 auto id = ids[i];
72 const auto brc = SDL_GetDisplayBounds(id, &rect);
73 const char* name = SDL_GetDisplayName(id);
74
75 if (!brc)
76 continue;
77 printf(" %s [%u] [%s] %dx%d\t+%d+%d\n", (i == 0) ? "*" : " ", id, name, rect.w, rect.h,
78 rect.x, rect.y);
79 }
80 SDL_free(ids);
81 SDL_Quit();
82 return 0;
83}
84
85[[nodiscard]] static BOOL sdl_apply_mon_max_size(SdlContext* sdl, UINT32* pMaxWidth,
86 UINT32* pMaxHeight)
87{
88 int32_t left = 0;
89 int32_t right = 0;
90 int32_t top = 0;
91 int32_t bottom = 0;
92
93 WINPR_ASSERT(pMaxWidth);
94 WINPR_ASSERT(pMaxHeight);
95
96 auto settings = sdl->context()->settings;
97 WINPR_ASSERT(settings);
98
99 for (size_t x = 0; x < freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount); x++)
100 {
101 auto monitor = static_cast<const rdpMonitor*>(
102 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, x));
103
104 left = std::min(monitor->x, left);
105 top = std::min(monitor->y, top);
106 right = std::max(monitor->x + monitor->width, right);
107 bottom = std::max(monitor->y + monitor->height, bottom);
108 }
109
110 const int32_t w = right - left;
111 const int32_t h = bottom - top;
112
113 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, w);
114 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, h);
115 return TRUE;
116}
117
118[[nodiscard]] static BOOL sdl_apply_max_size(SdlContext* sdl, UINT32* pMaxWidth, UINT32* pMaxHeight)
119{
120 WINPR_ASSERT(sdl);
121 WINPR_ASSERT(pMaxWidth);
122 WINPR_ASSERT(pMaxHeight);
123
124 auto settings = sdl->context()->settings;
125 WINPR_ASSERT(settings);
126
127 *pMaxWidth = 0;
128 *pMaxHeight = 0;
129
130 for (size_t x = 0; x < freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount); x++)
131 {
132 auto monitor = static_cast<const rdpMonitor*>(
133 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, x));
134
135 if (freerdp_settings_get_bool(settings, FreeRDP_Fullscreen))
136 {
137 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, monitor->width);
138 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, monitor->height);
139 }
140 else if (freerdp_settings_get_bool(settings, FreeRDP_Workarea))
141 {
142 SDL_Rect rect = {};
143 SDL_GetDisplayUsableBounds(monitor->orig_screen, &rect);
144 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, rect.w);
145 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, rect.h);
146 }
147 else if (freerdp_settings_get_uint32(settings, FreeRDP_PercentScreen) > 0)
148 {
149 SDL_Rect rect = {};
150 SDL_GetDisplayUsableBounds(monitor->orig_screen, &rect);
151
152 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, rect.w);
153 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, rect.h);
154
155 if (freerdp_settings_get_bool(settings, FreeRDP_PercentScreenUseWidth))
156 *pMaxWidth = (WINPR_ASSERTING_INT_CAST(uint32_t, rect.w) *
157 freerdp_settings_get_uint32(settings, FreeRDP_PercentScreen)) /
158 100;
159
160 if (freerdp_settings_get_bool(settings, FreeRDP_PercentScreenUseHeight))
161 *pMaxHeight = (WINPR_ASSERTING_INT_CAST(uint32_t, rect.h) *
162 freerdp_settings_get_uint32(settings, FreeRDP_PercentScreen)) /
163 100;
164 }
165 else if (freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) &&
166 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight))
167 {
168 *pMaxWidth = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
169 *pMaxHeight = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
170 }
171 }
172 return TRUE;
173}
174
175[[nodiscard]] static BOOL sdl_apply_display_properties(SdlContext* sdl)
176{
177 WINPR_ASSERT(sdl);
178
179 rdpSettings* settings = sdl->context()->settings;
180 WINPR_ASSERT(settings);
181
182 std::vector<rdpMonitor> monitors;
183 if (!freerdp_settings_get_bool(settings, FreeRDP_Fullscreen) &&
184 !freerdp_settings_get_bool(settings, FreeRDP_UseMultimon))
185 {
186 if (freerdp_settings_get_bool(settings, FreeRDP_Workarea))
187 {
188 if (sdl->monitorIds().empty())
189 return FALSE;
190 const auto id = sdl->monitorIds().front();
191 auto monitor = sdl->getDisplay(id);
192 monitor.is_primary = true;
193 monitor.x = 0;
194 monitor.y = 0;
195 monitors.emplace_back(monitor);
196 return freerdp_settings_set_monitor_def_array_sorted(settings, monitors.data(),
197 monitors.size());
198 }
199 return TRUE;
200 }
201 for (const auto& id : sdl->monitorIds())
202 {
203 const auto monitor = sdl->getDisplay(id);
204 monitors.emplace_back(monitor);
205 }
206 // /monitors: may select a subset that excludes the SDL primary. The
207 // library rejects arrays without a primary, so promote the first entry
208 // when none is marked.
209 if (!monitors.empty() && std::none_of(monitors.cbegin(), monitors.cend(),
210 [](const rdpMonitor& m) { return m.is_primary; }))
211 monitors.at(0).is_primary = true;
212 return freerdp_settings_set_monitor_def_array_sorted(settings, monitors.data(),
213 monitors.size());
214}
215
216[[nodiscard]] static BOOL sdl_detect_single_window(SdlContext* sdl, UINT32* pMaxWidth,
217 UINT32* pMaxHeight)
218{
219 WINPR_ASSERT(sdl);
220 WINPR_ASSERT(pMaxWidth);
221 WINPR_ASSERT(pMaxHeight);
222
223 rdpSettings* settings = sdl->context()->settings;
224 WINPR_ASSERT(settings);
225
226 if ((!freerdp_settings_get_bool(settings, FreeRDP_UseMultimon) &&
227 !freerdp_settings_get_bool(settings, FreeRDP_SpanMonitors)) ||
228 (freerdp_settings_get_bool(settings, FreeRDP_Workarea) &&
229 !freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode)))
230 {
231 /* If no monitors were specified on the command-line then set the current monitor as active
232 */
233 if (freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds) == 0)
234 {
235 SDL_DisplayID id = 0;
236 const auto& ids = sdl->monitorIds();
237 if (!ids.empty())
238 {
239 id = ids.front();
240 }
241 sdl->setMonitorIds({ id });
242 }
243
244 if (!sdl_apply_display_properties(sdl))
245 return FALSE;
246 return sdl_apply_max_size(sdl, pMaxWidth, pMaxHeight);
247 }
248 return sdl_apply_mon_max_size(sdl, pMaxWidth, pMaxHeight);
249}
250
251BOOL sdl_detect_monitors(SdlContext* sdl, UINT32* pMaxWidth, UINT32* pMaxHeight)
252{
253 WINPR_ASSERT(sdl);
254 WINPR_ASSERT(pMaxWidth);
255 WINPR_ASSERT(pMaxHeight);
256
257 rdpSettings* settings = sdl->context()->settings;
258 WINPR_ASSERT(settings);
259
260 const auto& ids = sdl->getDisplayIds();
261 auto nr = freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds);
262 if (nr == 0)
263 {
264 if (freerdp_settings_get_bool(settings, FreeRDP_UseMultimon))
265 sdl->setMonitorIds(ids);
266 else
267 {
268 sdl->setMonitorIds({ ids.front() });
269 }
270 }
271 else
272 {
273 /* There were more IDs supplied than there are monitors */
274 if (nr > ids.size())
275 {
276 WLog_ERR(TAG,
277 "Found %" PRIu32 " monitor IDs, but only have %" PRIuz " monitors connected",
278 nr, ids.size());
279 return FALSE;
280 }
281
282 std::vector<SDL_DisplayID> used;
283 for (size_t x = 0; x < nr; x++)
284 {
285 auto cur = static_cast<const UINT32*>(
286 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorIds, x));
287 WINPR_ASSERT(cur);
288
289 SDL_DisplayID id = *cur;
290
291 /* the ID is no valid monitor index */
292 if (std::find(ids.begin(), ids.end(), id) == ids.end())
293 {
294 WLog_ERR(TAG, "Supplied monitor ID[%" PRIuz "]=%" PRIu32 " is invalid", x, id);
295 return FALSE;
296 }
297
298 /* The ID is already taken */
299 if (std::find(used.begin(), used.end(), id) != used.end())
300 {
301 WLog_ERR(TAG, "Duplicate monitor ID[%" PRIuz "]=%" PRIu32 " detected", x, id);
302 return FALSE;
303 }
304 used.push_back(id);
305 }
306 sdl->setMonitorIds(used);
307 }
308
309 if (!sdl_apply_display_properties(sdl))
310 return FALSE;
311
312 auto size = static_cast<uint32_t>(sdl->monitorIds().size());
313 if (!freerdp_settings_set_uint32(settings, FreeRDP_NumMonitorIds, size))
314 return FALSE;
315
316 return sdl_detect_single_window(sdl, pMaxWidth, pMaxHeight);
317}
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.