Coverage Report

Created: 2026-02-26 06:54

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
} DISP_PLUGIN;
55
56
/**
57
 * Function description
58
 *
59
 * @return 0 on success, otherwise a Win32 error code
60
 */
61
static UINT
62
disp_send_display_control_monitor_layout_pdu(GENERIC_CHANNEL_CALLBACK* callback, UINT32 NumMonitors,
63
                                             const DISPLAY_CONTROL_MONITOR_LAYOUT* Monitors)
64
0
{
65
0
  UINT status = 0;
66
0
  wStream* s = NULL;
67
0
  DISP_PLUGIN* disp = NULL;
68
0
  UINT32 MonitorLayoutSize = 0;
69
0
  DISPLAY_CONTROL_HEADER header = WINPR_C_ARRAY_INIT;
70
71
0
  WINPR_ASSERT(callback);
72
0
  WINPR_ASSERT(Monitors || (NumMonitors == 0));
73
74
0
  disp = (DISP_PLUGIN*)callback->plugin;
75
0
  WINPR_ASSERT(disp);
76
77
0
  MonitorLayoutSize = DISPLAY_CONTROL_MONITOR_LAYOUT_SIZE;
78
0
  header.length = 8 + 8 + (NumMonitors * MonitorLayoutSize);
79
0
  header.type = DISPLAY_CONTROL_PDU_TYPE_MONITOR_LAYOUT;
80
81
0
  s = Stream_New(NULL, header.length);
82
83
0
  if (!s)
84
0
  {
85
0
    WLog_ERR(TAG, "Stream_New failed!");
86
0
    return CHANNEL_RC_NO_MEMORY;
87
0
  }
88
89
0
  if ((status = disp_write_header(s, &header)))
90
0
  {
91
0
    WLog_ERR(TAG, "Failed to write header with error %" PRIu32 "!", status);
92
0
    goto out;
93
0
  }
94
95
0
  if (NumMonitors > disp->MaxNumMonitors)
96
0
    NumMonitors = disp->MaxNumMonitors;
97
98
0
  Stream_Write_UINT32(s, MonitorLayoutSize); /* MonitorLayoutSize (4 bytes) */
99
0
  Stream_Write_UINT32(s, NumMonitors);       /* NumMonitors (4 bytes) */
100
0
  WLog_DBG(TAG, "NumMonitors=%" PRIu32 "", NumMonitors);
101
102
0
  for (UINT32 index = 0; index < NumMonitors; index++)
103
0
  {
104
0
    DISPLAY_CONTROL_MONITOR_LAYOUT current = Monitors[index];
105
0
    current.Width -= (current.Width % 2);
106
107
0
    if (current.Width < 200)
108
0
      current.Width = 200;
109
110
0
    if (current.Width > 8192)
111
0
      current.Width = 8192;
112
113
0
    if (current.Width % 2)
114
0
      current.Width++;
115
116
0
    if (current.Height < 200)
117
0
      current.Height = 200;
118
119
0
    if (current.Height > 8192)
120
0
      current.Height = 8192;
121
122
0
    Stream_Write_UINT32(s, current.Flags);              /* Flags (4 bytes) */
123
0
    Stream_Write_INT32(s, current.Left);                /* Left (4 bytes) */
124
0
    Stream_Write_INT32(s, current.Top);                 /* Top (4 bytes) */
125
0
    Stream_Write_UINT32(s, current.Width);              /* Width (4 bytes) */
126
0
    Stream_Write_UINT32(s, current.Height);             /* Height (4 bytes) */
127
0
    Stream_Write_UINT32(s, current.PhysicalWidth);      /* PhysicalWidth (4 bytes) */
128
0
    Stream_Write_UINT32(s, current.PhysicalHeight);     /* PhysicalHeight (4 bytes) */
129
0
    Stream_Write_UINT32(s, current.Orientation);        /* Orientation (4 bytes) */
130
0
    Stream_Write_UINT32(s, current.DesktopScaleFactor); /* DesktopScaleFactor (4 bytes) */
131
0
    Stream_Write_UINT32(s, current.DeviceScaleFactor);  /* DeviceScaleFactor (4 bytes) */
132
0
    WLog_DBG(TAG,
133
0
             "\t%" PRIu32 " : Flags: 0x%08" PRIX32 " Left/Top: (%" PRId32 ",%" PRId32
134
0
             ") W/H=%" PRIu32 "x%" PRIu32 ")",
135
0
             index, current.Flags, current.Left, current.Top, current.Width, current.Height);
136
0
    WLog_DBG(TAG,
137
0
             "\t   PhysicalWidth: %" PRIu32 " PhysicalHeight: %" PRIu32
138
0
             " Orientation: %s DesktopScaleFactor=%" PRIu32 " DeviceScaleFactor=%" PRIu32 "",
139
0
             current.PhysicalWidth, current.PhysicalHeight,
140
0
             freerdp_desktop_rotation_flags_to_string(current.Orientation),
141
0
             current.DesktopScaleFactor, current.DeviceScaleFactor);
142
0
  }
143
144
0
out:
145
0
  Stream_SealLength(s);
146
0
  status = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
147
0
                                    NULL);
148
0
  Stream_Free(s, TRUE);
149
0
  return status;
150
0
}
151
152
/**
153
 * Function description
154
 *
155
 * @return 0 on success, otherwise a Win32 error code
156
 */
157
static UINT disp_recv_display_control_caps_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
158
0
{
159
0
  DISP_PLUGIN* disp = NULL;
160
0
  DispClientContext* context = NULL;
161
0
  UINT ret = CHANNEL_RC_OK;
162
163
0
  WINPR_ASSERT(callback);
164
0
  WINPR_ASSERT(s);
165
166
0
  disp = (DISP_PLUGIN*)callback->plugin;
167
0
  WINPR_ASSERT(disp);
168
169
0
  context = disp->context;
170
0
  WINPR_ASSERT(context);
171
172
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
173
0
    return ERROR_INVALID_DATA;
174
175
0
  Stream_Read_UINT32(s, disp->MaxNumMonitors);        /* MaxNumMonitors (4 bytes) */
176
0
  Stream_Read_UINT32(s, disp->MaxMonitorAreaFactorA); /* MaxMonitorAreaFactorA (4 bytes) */
177
0
  Stream_Read_UINT32(s, disp->MaxMonitorAreaFactorB); /* MaxMonitorAreaFactorB (4 bytes) */
178
179
0
  if (context->DisplayControlCaps)
180
0
    ret = context->DisplayControlCaps(context, disp->MaxNumMonitors,
181
0
                                      disp->MaxMonitorAreaFactorA, disp->MaxMonitorAreaFactorB);
182
183
0
  return ret;
184
0
}
185
186
/**
187
 * Function description
188
 *
189
 * @return 0 on success, otherwise a Win32 error code
190
 */
191
static UINT disp_recv_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
192
0
{
193
0
  UINT32 error = 0;
194
0
  DISPLAY_CONTROL_HEADER header = WINPR_C_ARRAY_INIT;
195
196
0
  WINPR_ASSERT(callback);
197
0
  WINPR_ASSERT(s);
198
199
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
200
0
    return ERROR_INVALID_DATA;
201
202
0
  if ((error = disp_read_header(s, &header)))
203
0
  {
204
0
    WLog_ERR(TAG, "disp_read_header failed with error %" PRIu32 "!", error);
205
0
    return error;
206
0
  }
207
208
0
  if (!Stream_EnsureRemainingCapacity(s, header.length))
209
0
  {
210
0
    WLog_ERR(TAG, "not enough remaining data");
211
0
    return ERROR_INVALID_DATA;
212
0
  }
213
214
0
  switch (header.type)
215
0
  {
216
0
    case DISPLAY_CONTROL_PDU_TYPE_CAPS:
217
0
      return disp_recv_display_control_caps_pdu(callback, s);
218
219
0
    default:
220
0
      WLog_ERR(TAG, "Type %" PRIu32 " not recognized!", header.type);
221
0
      return ERROR_INTERNAL_ERROR;
222
0
  }
223
0
}
224
225
/**
226
 * Function description
227
 *
228
 * @return 0 on success, otherwise a Win32 error code
229
 */
230
static UINT disp_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
231
0
{
232
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
233
0
  return disp_recv_pdu(callback, data);
234
0
}
235
236
/**
237
 * Function description
238
 *
239
 * @return 0 on success, otherwise a Win32 error code
240
 */
241
static UINT disp_on_close(IWTSVirtualChannelCallback* pChannelCallback)
242
0
{
243
0
  free(pChannelCallback);
244
0
  return CHANNEL_RC_OK;
245
0
}
246
247
/**
248
 * Channel Client Interface
249
 */
250
251
/**
252
 * Function description
253
 *
254
 * @return 0 on success, otherwise a Win32 error code
255
 */
256
static UINT disp_send_monitor_layout(DispClientContext* context, UINT32 NumMonitors,
257
                                     DISPLAY_CONTROL_MONITOR_LAYOUT* Monitors)
258
0
{
259
0
  DISP_PLUGIN* disp = NULL;
260
0
  GENERIC_CHANNEL_CALLBACK* callback = NULL;
261
262
0
  WINPR_ASSERT(context);
263
264
0
  disp = (DISP_PLUGIN*)context->handle;
265
0
  WINPR_ASSERT(disp);
266
267
0
  callback = disp->base.listener_callback->channel_callback;
268
269
0
  return disp_send_display_control_monitor_layout_pdu(callback, NumMonitors, Monitors);
270
0
}
271
272
/**
273
 * Function description
274
 *
275
 * @return 0 on success, otherwise a Win32 error code
276
 */
277
static UINT disp_plugin_initialize(GENERIC_DYNVC_PLUGIN* base,
278
                                   WINPR_ATTR_UNUSED rdpContext* rcontext,
279
                                   WINPR_ATTR_UNUSED rdpSettings* settings)
280
0
{
281
0
  DispClientContext* context = NULL;
282
0
  DISP_PLUGIN* disp = (DISP_PLUGIN*)base;
283
284
0
  WINPR_ASSERT(disp);
285
0
  disp->MaxNumMonitors = 16;
286
0
  disp->MaxMonitorAreaFactorA = 8192;
287
0
  disp->MaxMonitorAreaFactorB = 8192;
288
289
0
  context = (DispClientContext*)calloc(1, sizeof(DispClientContext));
290
0
  if (!context)
291
0
  {
292
0
    WLog_Print(base->log, WLOG_ERROR, "unable to allocate DispClientContext");
293
0
    return CHANNEL_RC_NO_MEMORY;
294
0
  }
295
296
0
  context->handle = (void*)disp;
297
0
  context->SendMonitorLayout = disp_send_monitor_layout;
298
299
0
  disp->base.iface.pInterface = disp->context = context;
300
301
0
  return CHANNEL_RC_OK;
302
0
}
303
304
static void disp_plugin_terminated(GENERIC_DYNVC_PLUGIN* base)
305
0
{
306
0
  DISP_PLUGIN* disp = (DISP_PLUGIN*)base;
307
308
0
  WINPR_ASSERT(disp);
309
310
0
  free(disp->context);
311
0
}
312
313
static const IWTSVirtualChannelCallback disp_callbacks = { disp_on_data_received, NULL, /* Open */
314
                                                         disp_on_close, NULL };
315
316
/**
317
 * Function description
318
 *
319
 * @return 0 on success, otherwise a Win32 error code
320
 */
321
FREERDP_ENTRY_POINT(UINT VCAPITYPE disp_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
322
0
{
323
0
  return freerdp_generic_DVCPluginEntry(pEntryPoints, TAG, DISP_DVC_CHANNEL_NAME,
324
0
                                        sizeof(DISP_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
325
0
                                        &disp_callbacks, disp_plugin_initialize,
326
0
                                        disp_plugin_terminated);
327
0
}