19#include <freerdp/config.h>
21#include <winpr/assert.h>
22#include <winpr/synch.h>
23#include <winpr/sysinfo.h>
24#include <winpr/thread.h>
26#include <freerdp/log.h>
29#include "shadow_disp.h"
31#define TAG SERVER_TAG("shadow.disp")
33#if defined(CHANNEL_DISP_SERVER)
36#define SHADOW_DISP_MAX_MONITORS 16
37#define SHADOW_DISP_MAX_SIDE 8192
44#define SHADOW_DISP_SETTLE_MAX_MS 2000
45#define SHADOW_DISP_GAP_MS 300
46#define SHADOW_DISP_CHECK_MS 20
50 rdpShadowClient* client;
51 CRITICAL_SECTION lock;
64static void shadow_disp_client_size(
const ShadowDisp* sd, UINT32* width, UINT32* height)
67 const rdpSettings* settings = sd->client->context.settings;
74static BOOL shadow_disp_client_settled(
const ShadowDisp* sd, UINT32 width, UINT32 height,
75 UINT32 oldWidth, UINT32 oldHeight)
79 shadow_disp_client_size(sd, &w, &h);
80 if (!sd->client->activated)
82 return ((w == width) && (h == height)) || (w != oldWidth) || (h != oldHeight);
86static BOOL shadow_disp_wait(ShadowDisp* sd, DWORD ms)
88 return WaitForSingleObject(sd->stop, ms) != WAIT_OBJECT_0;
91static DWORD WINAPI shadow_disp_thread(LPVOID arg)
95 rdpShadowClient* client = sd->client;
97 rdpShadowSubsystem* subsystem = client->subsystem;
98 WINPR_ASSERT(subsystem);
100 HANDLE events[] = { sd->stop, sd->event };
101 while (WaitForMultipleObjects(ARRAYSIZE(events), events, FALSE, INFINITE) == WAIT_OBJECT_0 + 1)
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;
109 LeaveCriticalSection(&sd->lock);
115 UINT32 oldHeight = 0;
116 shadow_disp_client_size(sd, &oldWidth, &oldHeight);
118 if (!subsystem->ClientResize(subsystem, client, width, height))
119 WLog_WARN(TAG,
"cannot follow the client to %" PRIu32
"x%" PRIu32, width, height);
121 const UINT64 start = GetTickCount64();
122 while (!shadow_disp_client_settled(sd, width, height, oldWidth, oldHeight) &&
123 (GetTickCount64() - start < SHADOW_DISP_SETTLE_MAX_MS))
125 if (!shadow_disp_wait(sd, SHADOW_DISP_CHECK_MS))
128 if (!shadow_disp_wait(sd, SHADOW_DISP_GAP_MS))
131 EnterCriticalSection(&sd->lock);
134 WLog_DBG(TAG,
"%" PRIu64
" layouts received, %" PRIu64
" applied", sd->received,
136 LeaveCriticalSection(&sd->lock);
142static UINT shadow_disp_monitor_layout(DispServerContext* context,
145 WINPR_ASSERT(context);
148 ShadowDisp* sd = context->custom;
150 rdpShadowClient* client = sd->client;
151 WINPR_ASSERT(client);
153 if (!client->mayInteract || (pdu->NumMonitors == 0))
154 return CHANNEL_RC_OK;
157 for (UINT32 x = 0; x < pdu->NumMonitors; x++)
159 if (pdu->Monitors[x].Flags & DISPLAY_CONTROL_MONITOR_PRIMARY)
161 monitor = &pdu->Monitors[x];
167 EnterCriticalSection(&sd->lock);
169 sd->width = monitor->Width;
170 sd->height = monitor->Height;
172 (void)SetEvent(sd->event);
173 LeaveCriticalSection(&sd->lock);
175 return CHANNEL_RC_OK;
180BOOL shadow_client_disp_init(rdpShadowClient* client)
182 WINPR_ASSERT(client);
183 WINPR_ASSERT(client->subsystem);
185#if defined(CHANNEL_DISP_SERVER)
186 if (!client->subsystem->ClientResize)
189 ShadowDisp* sd = calloc(1,
sizeof(ShadowDisp));
193 if (!InitializeCriticalSectionAndSpinCount(&sd->lock, 4000))
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);
203 DispServerContext* disp = sd->thread ? disp_server_context_new(client->vcm) : nullptr;
208 (void)SetEvent(sd->stop);
209 (void)WaitForSingleObject(sd->thread, INFINITE);
210 (void)CloseHandle(sd->thread);
213 (void)CloseHandle(sd->event);
215 (void)CloseHandle(sd->stop);
216 DeleteCriticalSection(&sd->lock);
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;
232void shadow_client_disp_uninit(rdpShadowClient* client)
234 WINPR_ASSERT(client);
236#if defined(CHANNEL_DISP_SERVER)
237 DispServerContext* disp = client->disp;
240 ShadowDisp* sd = disp->custom;
243 disp_server_context_free(disp);
244 client->disp =
nullptr;
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);
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.