FreeRDP
Loading...
Searching...
No Matches
shadow_disp.c
1
18
19#include <freerdp/config.h>
20
21#include <winpr/assert.h>
22#include <winpr/synch.h>
23#include <winpr/sysinfo.h>
24#include <winpr/thread.h>
25
26#include <freerdp/log.h>
27#include "shadow.h"
28
29#include "shadow_disp.h"
30
31#define TAG SERVER_TAG("shadow.disp")
32
33#if defined(CHANNEL_DISP_SERVER)
34
35/* Limits of the client's layout PDU, not of what the subsystem then does with it */
36#define SHADOW_DISP_MAX_MONITORS 16
37#define SHADOW_DISP_MAX_SIDE 8192
38
39/* A client that is resized by hand sends a layout every few hundred milliseconds. Each one
40 * the subsystem follows is a new screen size and a resize of every client, so the layouts
41 * are applied one at a time: the next only once the client shows the size of the last one
42 * (at most SHADOW_DISP_SETTLE_MAX_MS), and SHADOW_DISP_GAP_MS after it. What came in
43 * meanwhile is reduced to the newest layout. */
44#define SHADOW_DISP_SETTLE_MAX_MS 2000
45#define SHADOW_DISP_GAP_MS 300
46#define SHADOW_DISP_CHECK_MS 20
47
48typedef struct
49{
50 rdpShadowClient* client;
51 CRITICAL_SECTION lock;
52 HANDLE event; /* a layout is pending, manual reset */
53 HANDLE stop;
54 HANDLE thread;
55 BOOL pending;
56 UINT32 width;
57 UINT32 height;
58 UINT64 received;
59 UINT64 applied;
60} ShadowDisp;
61
62/* the client's desktop size; only a hint for when to apply the next layout, read from
63 * another thread */
64static void shadow_disp_client_size(const ShadowDisp* sd, UINT32* width, UINT32* height)
65{
66 WINPR_ASSERT(sd);
67 const rdpSettings* settings = sd->client->context.settings;
68 *width = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
69 *height = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
70}
71
72/* the client took the layout: it is active with the size asked for, or with another size
73 * than before (the subsystem may round, or arrange several monitors) */
74static BOOL shadow_disp_client_settled(const ShadowDisp* sd, UINT32 width, UINT32 height,
75 UINT32 oldWidth, UINT32 oldHeight)
76{
77 UINT32 w = 0;
78 UINT32 h = 0;
79 shadow_disp_client_size(sd, &w, &h);
80 if (!sd->client->activated)
81 return FALSE;
82 return ((w == width) && (h == height)) || (w != oldWidth) || (h != oldHeight);
83}
84
85/* @return FALSE when stopped */
86static BOOL shadow_disp_wait(ShadowDisp* sd, DWORD ms)
87{
88 return WaitForSingleObject(sd->stop, ms) != WAIT_OBJECT_0;
89}
90
91static DWORD WINAPI shadow_disp_thread(LPVOID arg)
92{
93 ShadowDisp* sd = arg;
94 WINPR_ASSERT(sd);
95 rdpShadowClient* client = sd->client;
96 WINPR_ASSERT(client);
97 rdpShadowSubsystem* subsystem = client->subsystem;
98 WINPR_ASSERT(subsystem);
99
100 HANDLE events[] = { sd->stop, sd->event };
101 while (WaitForMultipleObjects(ARRAYSIZE(events), events, FALSE, INFINITE) == WAIT_OBJECT_0 + 1)
102 {
103 EnterCriticalSection(&sd->lock);
104 (void)ResetEvent(sd->event);
105 const BOOL pending = sd->pending;
106 const UINT32 width = sd->width;
107 const UINT32 height = sd->height;
108 sd->pending = FALSE;
109 LeaveCriticalSection(&sd->lock);
110
111 if (!pending)
112 continue;
113
114 UINT32 oldWidth = 0;
115 UINT32 oldHeight = 0;
116 shadow_disp_client_size(sd, &oldWidth, &oldHeight);
117
118 if (!subsystem->ClientResize(subsystem, client, width, height))
119 WLog_WARN(TAG, "cannot follow the client to %" PRIu32 "x%" PRIu32, width, height);
120
121 const UINT64 start = GetTickCount64();
122 while (!shadow_disp_client_settled(sd, width, height, oldWidth, oldHeight) &&
123 (GetTickCount64() - start < SHADOW_DISP_SETTLE_MAX_MS))
124 {
125 if (!shadow_disp_wait(sd, SHADOW_DISP_CHECK_MS))
126 return 0;
127 }
128 if (!shadow_disp_wait(sd, SHADOW_DISP_GAP_MS))
129 return 0;
130
131 EnterCriticalSection(&sd->lock);
132 sd->applied++;
133 if (sd->pending)
134 WLog_DBG(TAG, "%" PRIu64 " layouts received, %" PRIu64 " applied", sd->received,
135 sd->applied);
136 LeaveCriticalSection(&sd->lock);
137 }
138 return 0;
139}
140
141WINPR_ATTR_NODISCARD
142static UINT shadow_disp_monitor_layout(DispServerContext* context,
144{
145 WINPR_ASSERT(context);
146 WINPR_ASSERT(pdu);
147
148 ShadowDisp* sd = context->custom;
149 WINPR_ASSERT(sd);
150 rdpShadowClient* client = sd->client;
151 WINPR_ASSERT(client);
152
153 if (!client->mayInteract || (pdu->NumMonitors == 0))
154 return CHANNEL_RC_OK;
155
156 const DISPLAY_CONTROL_MONITOR_LAYOUT* monitor = &pdu->Monitors[0];
157 for (UINT32 x = 0; x < pdu->NumMonitors; x++)
158 {
159 if (pdu->Monitors[x].Flags & DISPLAY_CONTROL_MONITOR_PRIMARY)
160 {
161 monitor = &pdu->Monitors[x];
162 break;
163 }
164 }
165
166 /* the newest layout replaces one that was not applied yet */
167 EnterCriticalSection(&sd->lock);
168 sd->pending = TRUE;
169 sd->width = monitor->Width;
170 sd->height = monitor->Height;
171 sd->received++;
172 (void)SetEvent(sd->event);
173 LeaveCriticalSection(&sd->lock);
174
175 return CHANNEL_RC_OK;
176}
177
178#endif
179
180BOOL shadow_client_disp_init(rdpShadowClient* client)
181{
182 WINPR_ASSERT(client);
183 WINPR_ASSERT(client->subsystem);
184
185#if defined(CHANNEL_DISP_SERVER)
186 if (!client->subsystem->ClientResize)
187 return TRUE;
188
189 ShadowDisp* sd = calloc(1, sizeof(ShadowDisp));
190 if (!sd)
191 return FALSE;
192 sd->client = client;
193 if (!InitializeCriticalSectionAndSpinCount(&sd->lock, 4000))
194 {
195 free(sd);
196 return FALSE;
197 }
198 sd->event = CreateEvent(nullptr, TRUE, FALSE, nullptr);
199 sd->stop = CreateEvent(nullptr, TRUE, FALSE, nullptr);
200 if (sd->event && sd->stop)
201 sd->thread = CreateThread(nullptr, 0, shadow_disp_thread, sd, 0, nullptr);
202
203 DispServerContext* disp = sd->thread ? disp_server_context_new(client->vcm) : nullptr;
204 if (!disp)
205 {
206 if (sd->thread)
207 {
208 (void)SetEvent(sd->stop);
209 (void)WaitForSingleObject(sd->thread, INFINITE);
210 (void)CloseHandle(sd->thread);
211 }
212 if (sd->event)
213 (void)CloseHandle(sd->event);
214 if (sd->stop)
215 (void)CloseHandle(sd->stop);
216 DeleteCriticalSection(&sd->lock);
217 free(sd);
218 return FALSE;
219 }
220 client->disp = disp;
221
222 disp->custom = sd;
223 disp->rdpcontext = &client->context;
224 disp->MaxNumMonitors = SHADOW_DISP_MAX_MONITORS;
225 disp->MaxMonitorAreaFactorA = SHADOW_DISP_MAX_SIDE;
226 disp->MaxMonitorAreaFactorB = SHADOW_DISP_MAX_SIDE;
227 disp->DispMonitorLayout = shadow_disp_monitor_layout;
228#endif
229 return TRUE;
230}
231
232void shadow_client_disp_uninit(rdpShadowClient* client)
233{
234 WINPR_ASSERT(client);
235
236#if defined(CHANNEL_DISP_SERVER)
237 DispServerContext* disp = client->disp;
238 if (!disp)
239 return;
240 ShadowDisp* sd = disp->custom;
241
242 /* no layout callback after this */
243 disp_server_context_free(disp);
244 client->disp = nullptr;
245
246 if (sd)
247 {
248 (void)SetEvent(sd->stop);
249 (void)WaitForSingleObject(sd->thread, INFINITE);
250 (void)CloseHandle(sd->thread);
251 (void)CloseHandle(sd->event);
252 (void)CloseHandle(sd->stop);
253 DeleteCriticalSection(&sd->lock);
254 free(sd);
255 }
256#endif
257}
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.