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 return freerdp_settings_set_monitor_def_array_sorted(settings, monitors.data(),
207 monitors.size());
208}
209
210[[nodiscard]] static BOOL sdl_detect_single_window(SdlContext* sdl, UINT32* pMaxWidth,
211 UINT32* pMaxHeight)
212{
213 WINPR_ASSERT(sdl);
214 WINPR_ASSERT(pMaxWidth);
215 WINPR_ASSERT(pMaxHeight);
216
217 rdpSettings* settings = sdl->context()->settings;
218 WINPR_ASSERT(settings);
219
220 if ((!freerdp_settings_get_bool(settings, FreeRDP_UseMultimon) &&
221 !freerdp_settings_get_bool(settings, FreeRDP_SpanMonitors)) ||
222 (freerdp_settings_get_bool(settings, FreeRDP_Workarea) &&
223 !freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode)))
224 {
225 /* If no monitors were specified on the command-line then set the current monitor as active
226 */
227 if (freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds) == 0)
228 {
229 SDL_DisplayID id = 0;
230 const auto& ids = sdl->monitorIds();
231 if (!ids.empty())
232 {
233 id = ids.front();
234 }
235 sdl->setMonitorIds({ id });
236 }
237
238 if (!sdl_apply_display_properties(sdl))
239 return FALSE;
240 return sdl_apply_max_size(sdl, pMaxWidth, pMaxHeight);
241 }
242 return sdl_apply_mon_max_size(sdl, pMaxWidth, pMaxHeight);
243}
244
245BOOL sdl_detect_monitors(SdlContext* sdl, UINT32* pMaxWidth, UINT32* pMaxHeight)
246{
247 WINPR_ASSERT(sdl);
248 WINPR_ASSERT(pMaxWidth);
249 WINPR_ASSERT(pMaxHeight);
250
251 rdpSettings* settings = sdl->context()->settings;
252 WINPR_ASSERT(settings);
253
254 const auto& ids = sdl->getDisplayIds();
255 auto nr = freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds);
256 if (nr == 0)
257 {
258 if (freerdp_settings_get_bool(settings, FreeRDP_UseMultimon))
259 sdl->setMonitorIds(ids);
260 else
261 {
262 sdl->setMonitorIds({ ids.front() });
263 }
264 }
265 else
266 {
267 /* There were more IDs supplied than there are monitors */
268 if (nr > ids.size())
269 {
270 WLog_ERR(TAG,
271 "Found %" PRIu32 " monitor IDs, but only have %" PRIuz " monitors connected",
272 nr, ids.size());
273 return FALSE;
274 }
275
276 std::vector<SDL_DisplayID> used;
277 for (size_t x = 0; x < nr; x++)
278 {
279 auto cur = static_cast<const UINT32*>(
280 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorIds, x));
281 WINPR_ASSERT(cur);
282
283 SDL_DisplayID id = *cur;
284
285 /* the ID is no valid monitor index */
286 if (std::find(ids.begin(), ids.end(), id) == ids.end())
287 {
288 WLog_ERR(TAG, "Supplied monitor ID[%" PRIuz "]=%" PRIu32 " is invalid", x, id);
289 return FALSE;
290 }
291
292 /* The ID is already taken */
293 if (std::find(used.begin(), used.end(), id) != used.end())
294 {
295 WLog_ERR(TAG, "Duplicate monitor ID[%" PRIuz "]=%" PRIu32 " detected", x, id);
296 return FALSE;
297 }
298 used.push_back(id);
299 }
300 sdl->setMonitorIds(used);
301 }
302
303 if (!sdl_apply_display_properties(sdl))
304 return FALSE;
305
306 auto size = static_cast<uint32_t>(sdl->monitorIds().size());
307 if (!freerdp_settings_set_uint32(settings, FreeRDP_NumMonitorIds, size))
308 return FALSE;
309
310 return sdl_detect_single_window(sdl, pMaxWidth, pMaxHeight);
311}
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.