1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* X11 Monitor Handling
*
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Copyright 2017 David Fort <contact@hardening-consulting.com>
* Copyright 2018 Kai Harms <kharms@rangee.com>
*
* 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.
*/
#include <freerdp/config.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <SDL.h>
#include <winpr/assert.h>
#include <winpr/crt.h>
#include <freerdp/log.h>
#define TAG CLIENT_TAG("sdl")
#include "sdl_monitor.hpp"
#include "sdl_freerdp.hpp"
typedef struct
{
RECTANGLE_16 area;
RECTANGLE_16 workarea;
BOOL primary;
} MONITOR_INFO;
typedef struct
{
int nmonitors;
RECTANGLE_16 area;
RECTANGLE_16 workarea;
MONITOR_INFO* monitors;
} VIRTUAL_SCREEN;
/* See MSDN Section on Multiple Display Monitors: http://msdn.microsoft.com/en-us/library/dd145071
*/
int sdl_list_monitors(SdlContext* sdl)
{
SDL_Init(SDL_INIT_VIDEO);
const int nmonitors = SDL_GetNumVideoDisplays();
printf("listing %d monitors:\n", nmonitors);
for (int i = 0; i < nmonitors; i++)
{
SDL_Rect rect = {};
const int brc = SDL_GetDisplayBounds(i, &rect);
const char* name = SDL_GetDisplayName(i);
if (brc != 0)
continue;
printf(" %s [%d] [%s] %dx%d\t+%d+%d\n", (i == 0) ? "*" : " ", i, name, rect.w, rect.h,
rect.x, rect.y);
}
SDL_Quit();
return 0;
}
static BOOL sdl_is_monitor_id_active(SdlContext* sdl, UINT32 id)
{
const rdpSettings* settings = nullptr;
WINPR_ASSERT(sdl);
settings = sdl->context()->settings;
WINPR_ASSERT(settings);
const UINT32 NumMonitorIds = freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds);
if (!NumMonitorIds)
return TRUE;
for (UINT32 index = 0; index < NumMonitorIds; index++)
{
auto cur = static_cast<const UINT32*>(
freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorIds, index));
if (cur && (*cur == id))
return TRUE;
}
return FALSE;
}
static BOOL sdl_apply_max_size(SdlContext* sdl, UINT32* pMaxWidth, UINT32* pMaxHeight)
{
WINPR_ASSERT(sdl);
WINPR_ASSERT(pMaxWidth);
WINPR_ASSERT(pMaxHeight);
auto settings = sdl->context()->settings;
WINPR_ASSERT(settings);
*pMaxWidth = 0;
*pMaxHeight = 0;
for (size_t x = 0; x < freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount); x++)
{
auto monitor = static_cast<const rdpMonitor*>(
freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, x));
if (freerdp_settings_get_bool(settings, FreeRDP_Fullscreen))
{
*pMaxWidth = monitor->width;
*pMaxHeight = monitor->height;
}
else if (freerdp_settings_get_bool(settings, FreeRDP_Workarea))
{
SDL_Rect rect = {};
SDL_GetDisplayUsableBounds(monitor->orig_screen, &rect);
*pMaxWidth = rect.w;
*pMaxHeight = rect.h;
}
else if (freerdp_settings_get_uint32(settings, FreeRDP_PercentScreen) > 0)
{
SDL_Rect rect = {};
SDL_GetDisplayUsableBounds(monitor->orig_screen, &rect);
*pMaxWidth = rect.w;
*pMaxHeight = rect.h;
if (freerdp_settings_get_bool(settings, FreeRDP_PercentScreenUseWidth))
*pMaxWidth =
(rect.w * freerdp_settings_get_uint32(settings, FreeRDP_PercentScreen)) / 100;
if (freerdp_settings_get_bool(settings, FreeRDP_PercentScreenUseHeight))
*pMaxHeight =
(rect.h * freerdp_settings_get_uint32(settings, FreeRDP_PercentScreen)) / 100;
}
else if (freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) &&
freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight))
{
*pMaxWidth = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
*pMaxHeight = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
}
}
return TRUE;
}
#if SDL_VERSION_ATLEAST(2, 0, 10)
static UINT32 sdl_orientaion_to_rdp(SDL_DisplayOrientation orientation)
{
switch (orientation)
{
case SDL_ORIENTATION_LANDSCAPE:
return ORIENTATION_LANDSCAPE;
case SDL_ORIENTATION_LANDSCAPE_FLIPPED:
return ORIENTATION_LANDSCAPE_FLIPPED;
case SDL_ORIENTATION_PORTRAIT_FLIPPED:
return ORIENTATION_PORTRAIT_FLIPPED;
case SDL_ORIENTATION_PORTRAIT:
default:
return ORIENTATION_PORTRAIT;
}
}
#endif
static BOOL sdl_apply_display_properties(SdlContext* sdl)
{
WINPR_ASSERT(sdl);
rdpSettings* settings = sdl->context()->settings;
WINPR_ASSERT(settings);
const UINT32 numIds = freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds);
if (!freerdp_settings_set_pointer_len(settings, FreeRDP_MonitorDefArray, nullptr, numIds))
return FALSE;
if (!freerdp_settings_set_uint32(settings, FreeRDP_MonitorCount, numIds))
return FALSE;
for (UINT32 x = 0; x < numIds; x++)
{
auto id = static_cast<const UINT32*>(
freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorIds, x));
WINPR_ASSERT(id);
float ddpi = 1.0f;
float hdpi = 1.0f;
float vdpi = 1.0f;
SDL_Rect rect = {};
SDL_GetDisplayBounds(*id, &rect);
SDL_GetDisplayDPI(*id, &ddpi, &hdpi, &vdpi);
bool highDpi = hdpi > 100;
if (highDpi)
{
// HighDPI is problematic with SDL: We can only get native resolution by creating a
// window. Work around this by checking the supported resolutions (and keep maximum)
// Also scale the DPI
const SDL_Rect scaleRect = rect;
for (int i = 0; i < SDL_GetNumDisplayModes(*id); i++)
{
SDL_DisplayMode mode = {};
SDL_GetDisplayMode(x, i, &mode);
if (mode.w > rect.w)
{
rect.w = mode.w;
rect.h = mode.h;
}
else if (mode.w == rect.w)
{
if (mode.h > rect.h)
{
rect.w = mode.w;
rect.h = mode.h;
}
}
}
const float dw = 1.0f * rect.w / scaleRect.w;
const float dh = 1.0f * rect.h / scaleRect.h;
hdpi /= dw;
vdpi /= dh;
}
#if SDL_VERSION_ATLEAST(2, 0, 10)
const SDL_DisplayOrientation orientation = SDL_GetDisplayOrientation(*id);
const UINT32 rdp_orientation = sdl_orientaion_to_rdp(orientation);
#else
const UINT32 rdp_orientation = ORIENTATION_LANDSCAPE;
#endif
auto monitor = static_cast<rdpMonitor*>(
freerdp_settings_get_pointer_array_writable(settings, FreeRDP_MonitorDefArray, x));
WINPR_ASSERT(monitor);
/* windows uses 96 dpi as 'default' and the scale factors are in percent. */
const auto factor = ddpi / 96.0f * 100.0f;
monitor->orig_screen = x;
monitor->x = rect.x;
monitor->y = rect.y;
monitor->width = rect.w;
monitor->height = rect.h;
monitor->is_primary = x == 0;
monitor->attributes.desktopScaleFactor = factor;
monitor->attributes.deviceScaleFactor = 100;
monitor->attributes.orientation = rdp_orientation;
monitor->attributes.physicalWidth = rect.w / hdpi;
monitor->attributes.physicalHeight = rect.h / vdpi;
}
return TRUE;
}
static BOOL sdl_detect_single_window(SdlContext* sdl, UINT32* pMaxWidth, UINT32* pMaxHeight)
{
WINPR_ASSERT(sdl);
WINPR_ASSERT(pMaxWidth);
WINPR_ASSERT(pMaxHeight);
rdpSettings* settings = sdl->context()->settings;
WINPR_ASSERT(settings);
if ((!freerdp_settings_get_bool(settings, FreeRDP_UseMultimon) &&
!freerdp_settings_get_bool(settings, FreeRDP_SpanMonitors)) ||
(freerdp_settings_get_bool(settings, FreeRDP_Workarea) &&
!freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode)))
{
/* If no monitors were specified on the command-line then set the current monitor as active
*/
if (freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds) == 0)
{
const size_t id =
(sdl->windows.size() > 0) ? sdl->windows.begin()->second.displayIndex() : 0;
if (!freerdp_settings_set_pointer_len(settings, FreeRDP_MonitorIds, &id, 1))
return FALSE;
}
else
{
/* Always sets number of monitors from command-line to just 1.
* If the monitor is invalid then we will default back to current monitor
* later as a fallback. So, there is no need to validate command-line entry here.
*/
if (!freerdp_settings_set_uint32(settings, FreeRDP_NumMonitorIds, 1))
return FALSE;
}
// TODO: Fill monitor struct
if (!sdl_apply_display_properties(sdl))
return FALSE;
return sdl_apply_max_size(sdl, pMaxWidth, pMaxHeight);
}
return TRUE;
}
BOOL sdl_detect_monitors(SdlContext* sdl, UINT32* pMaxWidth, UINT32* pMaxHeight)
{
WINPR_ASSERT(sdl);
WINPR_ASSERT(pMaxWidth);
WINPR_ASSERT(pMaxHeight);
rdpSettings* settings = sdl->context()->settings;
WINPR_ASSERT(settings);
const int numDisplays = SDL_GetNumVideoDisplays();
if (!freerdp_settings_set_pointer_len(settings, FreeRDP_MonitorIds, nullptr, numDisplays))
return FALSE;
for (size_t x = 0; x < numDisplays; x++)
{
if (!freerdp_settings_set_pointer_array(settings, FreeRDP_MonitorIds, x, &x))
return FALSE;
}
if (!sdl_apply_display_properties(sdl))
return FALSE;
return sdl_detect_single_window(sdl, pMaxWidth, pMaxHeight);
}
|