Coverage Report

Created: 2026-11-10 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/channels/disp/client/disp_main.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Display Update Virtual Channel Extension
4
 *
5
 * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2015 Thincast Technologies GmbH
7
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
8
 * Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 *     http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22
23
#include <freerdp/config.h>
24
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <string.h>
28
29
#include <winpr/crt.h>
30
#include <winpr/assert.h>
31
#include <winpr/synch.h>
32
#include <winpr/print.h>
33
#include <winpr/thread.h>
34
#include <winpr/stream.h>
35
#include <winpr/sysinfo.h>
36
#include <winpr/cmdline.h>
37
#include <winpr/collections.h>
38
39
#include <freerdp/addin.h>
40
#include <freerdp/utils/string.h>
41
#include <freerdp/client/channels.h>
42
43
#include "disp_main.h"
44
#include "../disp_common.h"
45
46
typedef struct
47
{
48
  GENERIC_DYNVC_PLUGIN base;
49
50
  DispClientContext* context;
51
  UINT32 MaxNumMonitors;
52
  UINT32 MaxMonitorAreaFactorA;
53
  UINT32 MaxMonitorAreaFactorB;
54
  CRITICAL_SECTION lock;
55
} DISP_PLUGIN;
56
57
/**
58
 * Function description
59
 *
60
 * @return 0 on success, otherwise a Win32 error code
61
 */
62
static UINT
63
disp_send_display_control_monitor_layout_pdu(GENERIC_CHANNEL_CALLBACK* callback, UINT32 NumMonitors,
64
                                             const DISPLAY_CONTROL_MONITOR_LAYOUT* Monitors)
65
0
{
66
0
  const DWORD level = WLOG_DEBUG;
67
0
  UINT status = 0;
68
69
0
  WINPR_ASSERT(callback);
70
0
  WINPR_ASSERT(Monitors || (NumMonitors == 0));
71
72
0
  DISP_PLUGIN* disp = (DISP_PLUGIN*)callback->plugin;
73
0
  WINPR_ASSERT(disp);
74
75
0
  const UINT32 MonitorLayoutSize = DISPLAY_CONTROL_MONITOR_LAYOUT_SIZE;
76
0
  const DISPLAY_CONTROL_HEADER header = { .length = 8 + 8 + (NumMonitors * MonitorLayoutSize),
77
0
                                        .type = DISPLAY_CONTROL_PDU_TYPE_MONITOR_LAYOUT };
78
79
0
  wStream* s = Stream_New(nullptr, header.length);
80
81
0
  if (!s)
82
0
  {
83
0
    WLog_Print(disp->base.log, WLOG_ERROR, "Stream_New failed!");
84
0
    return CHANNEL_RC_NO_MEMORY;
85
0
  }
86
87
0
  if ((status = disp_write_header(s, &header)))
88
0
  {
89
0
    WLog_Print(disp->base.log, WLOG_ERROR, "Failed to write header with error %" PRIu32 "!",
90
0
               status);
91
0
    goto out;
92
0
  }
93
94
0
  if (NumMonitors > disp->MaxNumMonitors)
95
0
    NumMonitors = disp->MaxNumMonitors;
96
97
0
  Stream_Write_UINT32(s, MonitorLayoutSize); /* MonitorLayoutSize (4 bytes) */
98
0
  Stream_Write_UINT32(s, NumMonitors);       /* NumMonitors (4 bytes) */
99
0
  WLog_Print(disp->base.log, level, "NumMonitors=%" PRIu32 "", NumMonitors);
100
101
0
  for (UINT32 index = 0; index < NumMonitors; index++)
102
0
  {
103
0
    DISPLAY_CONTROL_MONITOR_LAYOUT current = Monitors[index];
104
0
    current.Width -= (current.Width % 2);
105
106
0
    if (current.Width < 200)
107
0
      current.Width = 200;
108
109
0
    if (current.Width > 8192)
110
0
      current.Width = 8192;
111
112
0
    if (current.Width % 2)
113
0
      current.Width++;
114
115
0
    if (current.Height < 200)
116
0
      current.Height = 200;
117
118
0
    if (current.Height > 8192)
119
0
      current.Height = 8192;
120
121
0
    Stream_Write_UINT32(s, current.Flags);              /* Flags (4 bytes) */
122
0
    Stream_Write_INT32(s, current.Left);                /* Left (4 bytes) */
123
0
    Stream_Write_INT32(s, current.Top);                 /* Top (4 bytes) */
124
0
    Stream_Write_UINT32(s, current.Width);              /* Width (4 bytes) */
125
0
    Stream_Write_UINT32(s, current.Height);             /* Height (4 bytes) */
126
0
    Stream_Write_UINT32(s, current.PhysicalWidth);      /* PhysicalWidth (4 bytes) */
127
0
    Stream_Write_UINT32(s, current.PhysicalHeight);     /* PhysicalHeight (4 bytes) */
128
0
    Stream_Write_UINT32(s, current.Orientation);        /* Orientation (4 bytes) */
129
0
    Stream_Write_UINT32(s, current.DesktopScaleFactor); /* DesktopScaleFactor (4 bytes) */
130
0
    Stream_Write_UINT32(s, current.DeviceScaleFactor);  /* DeviceScaleFactor (4 bytes) */
131
0
    WLog_Print(disp->base.log, level,
132
0
               "\t%" PRIu32 " : Flags: 0x%08" PRIX32 " Left/Top: (%" PRId32 ",%" PRId32
133
0
               ") W/H=%" PRIu32 "x%" PRIu32 ")",
134
0
               index, current.Flags, current.Left, current.Top, current.Width, current.Height);
135
0
    WLog_Print(disp->base.log, level,
136
0
               "\t   PhysicalWidth: %" PRIu32 " PhysicalHeight: %" PRIu32
137
0
               " Orientation: %s DesktopScaleFactor=%" PRIu32 " DeviceScaleFactor=%" PRIu32 "",
138
0
               current.PhysicalWidth, current.PhysicalHeight,
139
0
               freerdp_desktop_rotation_flags_to_string(current.Orientation),
140
0
               current.DesktopScaleFactor, current.DeviceScaleFactor);
141
0
  }
142
143
0
out:
144
0
  Stream_SealLength(s);
145
0
  status = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
146
0
                                    nullptr);
147
0
  Stream_Free(s, TRUE);
148
0
  return status;
149
0
}
150
151
/**
152
 * Function description
153
 *
154
 * @return 0 on success, otherwise a Win32 error code
155
 */
156
static UINT disp_recv_display_control_caps_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
157
0
{
158
0
  DISP_PLUGIN* disp = nullptr;
159
0
  DispClientContext* context = nullptr;
160
0
  UINT ret = CHANNEL_RC_OK;
161
162
0
  WINPR_ASSERT(callback);
163
0
  WINPR_ASSERT(s);
164
165
0
  disp = (DISP_PLUGIN*)callback->plugin;
166
0
  WINPR_ASSERT(disp);
167
168
0
  context = disp->context;
169
0
  WINPR_ASSERT(context);
170
171
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
172
0
    return ERROR_INVALID_DATA;
173
174
0
  Stream_Read_UINT32(s, disp->MaxNumMonitors);        /* MaxNumMonitors (4 bytes) */
175
0
  Stream_Read_UINT32(s, disp->MaxMonitorAreaFactorA); /* MaxMonitorAreaFactorA (4 bytes) */
176
0
  Stream_Read_UINT32(s, disp->MaxMonitorAreaFactorB); /* MaxMonitorAreaFactorB (4 bytes) */
177
178
0
  if (context->DisplayControlCaps)
179
0
    ret = context->DisplayControlCaps(context, disp->MaxNumMonitors,
180
0
                                      disp->MaxMonitorAreaFactorA, disp->MaxMonitorAreaFactorB);
181
182
0
  return ret;
183
0
}
184
185
/**
186
 * Function description
187
 *
188
 * @return 0 on success, otherwise a Win32 error code
189
 */
190
static UINT disp_recv_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
191
0
{
192
0
  UINT32 error = 0;
193
0
  DISPLAY_CONTROL_HEADER header = WINPR_C_ARRAY_INIT;
194
195
0
  WINPR_ASSERT(callback);
196
0
  WINPR_ASSERT(s);
197
198
0
  DISP_PLUGIN* disp = (DISP_PLUGIN*)callback->plugin;
199
0
  WINPR_ASSERT(disp);
200
201
0
  if ((error = disp_read_header(s, &header, nullptr)))
202
0
  {
203
0
    WLog_Print(disp->base.log, WLOG_ERROR, "disp_read_header failed with error %" PRIu32 "!",
204
0
               error);
205
0
    return error;
206
0
  }
207
208
0
  switch (header.type)
209
0
  {
210
0
    case DISPLAY_CONTROL_PDU_TYPE_CAPS:
211
0
      return disp_recv_display_control_caps_pdu(callback, s);
212
213
0
    default:
214
0
      WLog_Print(disp->base.log, WLOG_ERROR, "Type %" PRIu32 " not recognized!", header.type);
215
0
      return ERROR_INTERNAL_ERROR;
216
0
  }
217
0
}
218
219
/**
220
 * Function description
221
 *
222
 * @return 0 on success, otherwise a Win32 error code
223
 */
224
static UINT disp_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
225
0
{
226
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
227
0
  return disp_recv_pdu(callback, data);
228
0
}
229
230
/**
231
 * Function description
232
 *
233
 * @return 0 on success, otherwise a Win32 error code
234
 */
235
static UINT disp_on_close(IWTSVirtualChannelCallback* pChannelCallback)
236
0
{
237
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
238
239
0
  if (callback)
240
0
  {
241
0
    DISP_PLUGIN* disp = (DISP_PLUGIN*)callback->plugin;
242
0
    WINPR_ASSERT(disp);
243
244
    /* Lock here to ensure that no disp_send_monitor_layout is in progress. */
245
0
    EnterCriticalSection(&disp->lock);
246
0
    if (disp->base.listener_callback->channel_callback == callback)
247
0
      disp->base.listener_callback->channel_callback = nullptr;
248
0
    free(callback);
249
0
    LeaveCriticalSection(&disp->lock);
250
0
  }
251
0
  return CHANNEL_RC_OK;
252
0
}
253
254
/**
255
 * Channel Client Interface
256
 */
257
258
/**
259
 * Function description
260
 *
261
 * @return 0 on success, otherwise a Win32 error code
262
 */
263
static UINT disp_send_monitor_layout(DispClientContext* context, UINT32 NumMonitors,
264
                                     DISPLAY_CONTROL_MONITOR_LAYOUT* Monitors)
265
0
{
266
0
  DISP_PLUGIN* disp = nullptr;
267
0
  GENERIC_CHANNEL_CALLBACK* callback = nullptr;
268
269
0
  WINPR_ASSERT(context);
270
271
0
  disp = (DISP_PLUGIN*)context->handle;
272
0
  WINPR_ASSERT(disp);
273
274
0
  UINT rc = CHANNEL_RC_NOT_OPEN;
275
0
  EnterCriticalSection(&disp->lock);
276
0
  callback = disp->base.listener_callback->channel_callback;
277
  /* The channel might have been closed by the server in the meantime */
278
0
  if (callback)
279
0
    rc = disp_send_display_control_monitor_layout_pdu(callback, NumMonitors, Monitors);
280
0
  LeaveCriticalSection(&disp->lock);
281
0
  return rc;
282
0
}
283
284
/**
285
 * Function description
286
 *
287
 * @return 0 on success, otherwise a Win32 error code
288
 */
289
static UINT disp_plugin_initialize(GENERIC_DYNVC_PLUGIN* base,
290
                                   WINPR_ATTR_UNUSED rdpContext* rcontext,
291
                                   WINPR_ATTR_UNUSED rdpSettings* settings)
292
0
{
293
0
  DispClientContext* context = nullptr;
294
0
  DISP_PLUGIN* disp = (DISP_PLUGIN*)base;
295
296
0
  WINPR_ASSERT(disp);
297
0
  disp->MaxNumMonitors = 16;
298
0
  disp->MaxMonitorAreaFactorA = 8192;
299
0
  disp->MaxMonitorAreaFactorB = 8192;
300
0
  InitializeCriticalSection(&disp->lock);
301
302
0
  context = (DispClientContext*)calloc(1, sizeof(DispClientContext));
303
0
  if (!context)
304
0
  {
305
0
    WLog_Print(base->log, WLOG_ERROR, "unable to allocate DispClientContext");
306
0
    return CHANNEL_RC_NO_MEMORY;
307
0
  }
308
309
0
  context->handle = (void*)disp;
310
0
  context->SendMonitorLayout = disp_send_monitor_layout;
311
312
0
  disp->base.iface.pInterface = disp->context = context;
313
314
0
  return CHANNEL_RC_OK;
315
0
}
316
317
static void disp_plugin_terminated(GENERIC_DYNVC_PLUGIN* base)
318
0
{
319
0
  DISP_PLUGIN* disp = (DISP_PLUGIN*)base;
320
321
0
  WINPR_ASSERT(disp);
322
323
0
  DeleteCriticalSection(&disp->lock);
324
0
  free(disp->context);
325
0
}
326
327
static const IWTSVirtualChannelCallback disp_callbacks = { disp_on_data_received,
328
                                                         nullptr, /* Open */
329
                                                         disp_on_close, nullptr };
330
331
/**
332
 * Function description
333
 *
334
 * @return 0 on success, otherwise a Win32 error code
335
 */
336
FREERDP_ENTRY_POINT(UINT VCAPITYPE disp_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
337
0
{
338
0
  return freerdp_generic_DVCPluginEntry(pEntryPoints, TAG, DISP_DVC_CHANNEL_NAME,
339
0
                                        sizeof(DISP_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
340
0
                                        &disp_callbacks, disp_plugin_initialize,
341
0
                                        disp_plugin_terminated);
342
0
}