FreeRDP
Loading...
Searching...
No Matches
TestClientMonitorScale.c
1
19
20#include <freerdp/client.h>
21#include <freerdp/client/cmdline.h>
22#include <freerdp/client/monitor.h>
23#include <winpr/crt.h>
24
25#define CHECK(condition) \
26 do \
27 { \
28 if (!(condition)) \
29 { \
30 (void)fprintf(stderr, "%s:%d: %s\n", __func__, __LINE__, #condition); \
31 goto fail; \
32 } \
33 } while (0)
34
35static rdpSettings* parse(const char* monitors, int* status)
36{
37 char option[256] = { 0 };
38 char* argv[] = { "client", "/v:localhost", option };
39 rdpSettings* settings = freerdp_settings_new(0);
40 if (!settings)
41 return nullptr;
42 (void)_snprintf(option, sizeof(option), "/monitors:%s", monitors);
43 *status = freerdp_client_settings_parse_command_line(settings, ARRAYSIZE(argv), argv, FALSE);
44 return settings;
45}
46
47static BOOL check_entry(const rdpSettings* settings, size_t index, UINT32 id, UINT32 desktop,
48 UINT32 device)
49{
50 const UINT32* pid = freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorIds, index);
51 const rdpMonitorScale* scale =
52 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorScales, index);
53 if (pid && scale && (*pid == id) && (scale->desktopScaleFactor == desktop) &&
54 (scale->deviceScaleFactor == device))
55 return TRUE;
56 (void)fprintf(stderr,
57 "entry %" PRIuz " is %" PRIu32 ":%" PRIu32 ":%" PRIu32 ", expected %" PRIu32
58 ":%" PRIu32 ":%" PRIu32 "\n",
59 index, pid ? *pid : UINT32_MAX, scale ? scale->desktopScaleFactor : UINT32_MAX,
60 scale ? scale->deviceScaleFactor : UINT32_MAX, id, desktop, device);
61 return FALSE;
62}
63
64static BOOL check_scale(const rdpSettings* settings, size_t index, UINT32 desktop, UINT32 device)
65{
66 const rdpMonitorScale* scale =
67 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorScales, index);
68 if (scale && (scale->desktopScaleFactor == desktop) && (scale->deviceScaleFactor == device))
69 return TRUE;
70 (void)fprintf(stderr, "scale %" PRIuz " does not match %" PRIu32 ":%" PRIu32 "\n", index,
71 desktop, device);
72 return FALSE;
73}
74
75static BOOL test_parse(void)
76{
77 static const char* invalid[] = {
78 "1:", "1::100", "1:175:", "1:175:100:", "1:175:100:5", "1:99",
79 "1:501", "1:175:175", "1:175:0", "1:-175", "1:+175", "1: 175",
80 "1:175 ", "1:175.0", "1:175x", "1:175:100x", "1:4294967396", "1:175:4294967396"
81 };
82 BOOL rc = FALSE;
83 int status = 0;
84 rdpSettings* settings = parse("3:175:140,0,4:500:180,2:100:100,5:125", &status);
85 CHECK(settings && (status == 0));
86 CHECK(freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds) == 5);
87 CHECK(check_entry(settings, 0, 3, 175, 140));
88 CHECK(check_entry(settings, 1, 0, 0, 0));
89 CHECK(check_entry(settings, 2, 4, 500, 180));
90 CHECK(check_entry(settings, 3, 2, 100, 100));
91 // The device scale defaults to 100 when only the desktop scale is given.
92 CHECK(check_entry(settings, 4, 5, 125, 100));
93 // The scales are only sent with the monitor attributes.
94 CHECK(freerdp_settings_get_bool(settings, FreeRDP_HasMonitorAttributes));
95 freerdp_settings_free(settings);
96
97 // Plain monitor lists keep their previous meaning.
98 settings = parse("3,2", &status);
99 CHECK(settings && (status == 0));
100 CHECK(check_entry(settings, 0, 3, 0, 0));
101 CHECK(check_entry(settings, 1, 2, 0, 0));
102 CHECK(!freerdp_settings_get_bool(settings, FreeRDP_HasMonitorAttributes));
103 freerdp_settings_free(settings);
104 settings = nullptr;
105
106 for (size_t i = 0; i < ARRAYSIZE(invalid); i++)
107 {
108 settings = parse(invalid[i], &status);
109 CHECK(settings);
110 if (status >= 0)
111 {
112 (void)fprintf(stderr, "/monitors:%s was accepted\n", invalid[i]);
113 goto fail;
114 }
115 freerdp_settings_free(settings);
116 settings = nullptr;
117 }
118 rc = TRUE;
119fail:
120 freerdp_settings_free(settings);
121 return rc;
122}
123
124static BOOL test_apply(void)
125{
126 BOOL rc = FALSE;
127 int status = 0;
128 rdpMonitor monitors[] = {
129 { .x = 0,
130 .y = 0,
131 .is_primary = TRUE,
132 .orig_screen = 3,
133 .attributes = { 600, 340, 90, 200, 180 } },
134 { .x = -1920, .y = 0, .orig_screen = 0, .attributes = { 530, 300, 0, 125, 100 } },
135 { .x = 3840, .y = 0, .orig_screen = 2, .attributes = { 400, 240, 0, 150, 140 } },
136 { .x = 5760, .y = 0, .orig_screen = 7, .attributes = { 400, 240, 0, 150, 140 } }
137 };
138 rdpSettings* settings = parse("0:100,3:175:140,2", &status);
139 CHECK(settings && (status == 0));
140 // Native IDs, not array indices or primary-monitor ordering, select the overrides.
141 for (size_t i = 0; i < ARRAYSIZE(monitors); i++)
142 freerdp_client_apply_monitor_scale(settings, &monitors[i]);
143 CHECK(monitors[0].attributes.desktopScaleFactor == 175 &&
144 monitors[0].attributes.deviceScaleFactor == 140);
145 CHECK(monitors[0].attributes.physicalWidth == 600 && monitors[0].attributes.orientation == 90);
146 CHECK(monitors[1].attributes.desktopScaleFactor == 100 &&
147 monitors[1].attributes.deviceScaleFactor == 100 && monitors[1].x == -1920);
148 // A monitor without a scale, or not in the list, keeps the detected values.
149 CHECK(monitors[2].attributes.desktopScaleFactor == 150 &&
150 monitors[2].attributes.deviceScaleFactor == 140);
151 CHECK(monitors[3].attributes.desktopScaleFactor == 150);
152
153 // Redetection for a later Display Control layout keeps the override.
154 monitors[0].attributes.desktopScaleFactor = 200;
155 freerdp_client_apply_monitor_scale(settings, &monitors[0]);
156 CHECK(monitors[0].attributes.desktopScaleFactor == 175);
157
158 // Clients may report detected monitors through the count only; nothing is applied then.
159 CHECK(freerdp_settings_set_pointer_len(settings, FreeRDP_MonitorIds, nullptr, 0));
160 CHECK(freerdp_settings_set_uint32(settings, FreeRDP_NumMonitorIds, 3));
161 monitors[0].attributes.desktopScaleFactor = 200;
162 freerdp_client_apply_monitor_scale(settings, &monitors[0]);
163 CHECK(monitors[0].attributes.desktopScaleFactor == 200);
164 rc = TRUE;
165fail:
166 freerdp_settings_free(settings);
167 return rc;
168}
169
170static BOOL test_settings(void)
171{
172 BOOL rc = FALSE;
173 int status = 0;
174 size_t length = 0;
175 char* json = nullptr;
176 rdpSettings* copy = nullptr;
177 rdpSettings* restored = nullptr;
178 rdpSettings* settings = freerdp_settings_new(0);
179 CHECK(settings);
180 // Empty settings must also survive JSON round trips.
181 json = freerdp_settings_serialize(settings, FALSE, &length);
182 CHECK(json);
183 restored = freerdp_settings_deserialize(json, length);
184 CHECK(restored);
185 CHECK(!freerdp_settings_get_pointer(restored, FreeRDP_MonitorScales));
186 freerdp_settings_free(restored);
187 restored = nullptr;
188 freerdp_settings_free(settings);
189 free(json);
190 json = nullptr;
191
192 settings = parse("0:175:140,3", &status);
193 CHECK(settings && (status == 0));
194 copy = freerdp_settings_clone(settings);
195 CHECK(copy);
196 CHECK(freerdp_settings_get_pointer(copy, FreeRDP_MonitorScales) !=
197 freerdp_settings_get_pointer(settings, FreeRDP_MonitorScales));
198 CHECK(check_entry(copy, 0, 0, 175, 140));
199 CHECK(check_entry(copy, 1, 3, 0, 0));
200
201 json = freerdp_settings_serialize(copy, FALSE, &length);
202 CHECK(json);
203 restored = freerdp_settings_deserialize(json, length);
204 CHECK(restored);
205 CHECK(freerdp_settings_get_uint32(restored, FreeRDP_NumMonitorIds) == 2);
206 CHECK(check_scale(restored, 0, 175, 140));
207 CHECK(check_scale(restored, 1, 0, 0));
208
209 const rdpMonitorScale replacement = { 200, 180 };
210 CHECK(freerdp_settings_set_pointer_array(restored, FreeRDP_MonitorScales, 1, &replacement));
211 CHECK(check_scale(restored, 1, 200, 180));
212 CHECK(check_entry(copy, 1, 3, 0, 0));
213 CHECK(!freerdp_settings_set_pointer_array(restored, FreeRDP_MonitorScales, 2, &replacement));
214
215 // The scales always have one entry per monitor ID.
216 const rdpMonitorScale scales[3] = { 0 };
217 CHECK(!freerdp_settings_set_pointer_len(restored, FreeRDP_MonitorScales, scales, 3));
218 CHECK(freerdp_settings_set_pointer_len(restored, FreeRDP_MonitorScales, scales, 2));
219 CHECK(check_scale(restored, 1, 0, 0));
220
221 // A new monitor ID list starts without scales.
222 const UINT32 ids[] = { 5, 6, 7 };
223 CHECK(freerdp_settings_set_pointer_len(copy, FreeRDP_MonitorIds, ids, ARRAYSIZE(ids)));
224 CHECK(check_entry(copy, 0, 5, 0, 0));
225 CHECK(check_entry(copy, 2, 7, 0, 0));
226 CHECK(freerdp_settings_set_pointer_len(copy, FreeRDP_MonitorIds, nullptr, 0));
227 CHECK(!freerdp_settings_get_pointer(copy, FreeRDP_MonitorScales));
228 rc = TRUE;
229fail:
230 free(json);
231 freerdp_settings_free(restored);
233 freerdp_settings_free(settings);
234 return rc;
235}
236
237int TestClientMonitorScale(int argc, char* argv[])
238{
239 WINPR_UNUSED(argc);
240 WINPR_UNUSED(argv);
241 return (test_parse() && test_apply() && test_settings()) ? 0 : -1;
242}
WINPR_ATTR_NODISCARD FREERDP_API const void * freerdp_settings_get_pointer(const rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a immutable pointer settings value.
FREERDP_API rdpSettings * freerdp_settings_new(DWORD flags)
creates a new setting struct
FREERDP_API rdpSettings * freerdp_settings_clone(const rdpSettings *settings)
Creates a deep copy of settings.
FREERDP_API rdpSettings * freerdp_settings_deserialize(const char *jstr, size_t length)
A function that converts a JSON string to a rdpSettings struct.
FREERDP_API char * freerdp_settings_serialize(const rdpSettings *settings, BOOL pretty, size_t *plength)
A function that converts a rdpSettings struct to a JSON serialized string.
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 BOOL freerdp_settings_set_pointer_len(rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id, const void *data, size_t len)
Set a pointer to value data.
FREERDP_API void freerdp_settings_free(rdpSettings *settings)
Free a settings struct with all data in it.
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_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.