Coverage Report

Created: 2025-07-01 06:46

/src/FreeRDP/channels/disp/client/disp_main.c
Line
Count
Source (jump to first uncovered line)
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/client/channels.h>
41
42
#include "disp_main.h"
43
#include "../disp_common.h"
44
45
typedef struct
46
{
47
  GENERIC_DYNVC_PLUGIN base;
48
49
  DispClientContext* context;
50
  UINT32 MaxNumMonitors;
51
  UINT32 MaxMonitorAreaFactorA;
52
  UINT32 MaxMonitorAreaFactorB;
53
} DISP_PLUGIN;
54
55
/**
56
 * Function description
57
 *
58
 * @return 0 on success, otherwise a Win32 error code
59
 */
60
static UINT
61
disp_send_display_control_monitor_layout_pdu(GENERIC_CHANNEL_CALLBACK* callback, UINT32 NumMonitors,
62
                                             const DISPLAY_CONTROL_MONITOR_LAYOUT* Monitors)
63
0
{
64
0
  UINT status = 0;
65
0
  wStream* s = NULL;
66
0
  DISP_PLUGIN* disp = NULL;
67
0
  UINT32 MonitorLayoutSize = 0;
68
0
  DISPLAY_CONTROL_HEADER header = { 0 };
69
70
0
  WINPR_ASSERT(callback);
71
0
  WINPR_ASSERT(Monitors || (NumMonitors == 0));
72
73
0
  disp = (DISP_PLUGIN*)callback->plugin;
74
0
  WINPR_ASSERT(disp);
75
76
0
  MonitorLayoutSize = DISPLAY_CONTROL_MONITOR_LAYOUT_SIZE;
77
0
  header.length = 8 + 8 + (NumMonitors * MonitorLayoutSize);
78
0
  header.type = DISPLAY_CONTROL_PDU_TYPE_MONITOR_LAYOUT;
79
80
0
  s = Stream_New(NULL, header.length);
81
82
0
  if (!s)
83
0
  {
84
0
    WLog_ERR(TAG, "Stream_New failed!");
85
0
    return CHANNEL_RC_NO_MEMORY;
86
0
  }
87
88
0
  if ((status = disp_write_header(s, &header)))
89
0
  {
90
0
    WLog_ERR(TAG, "Failed to write header with error %" PRIu32 "!", 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_DBG(TAG, "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_DBG(TAG,
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_DBG(TAG,
136
0
             "\t   PhysicalWidth: %" PRIu32 " PhysicalHeight: %" PRIu32 " Orientation: %" PRIu32
137
0
             "",
138
0
             current.PhysicalWidth, current.PhysicalHeight, current.Orientation);
139
0
  }
140
141
0
out:
142
0
  Stream_SealLength(s);
143
0
  status = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
144
0
                                    NULL);
145
0
  Stream_Free(s, TRUE);
146
0
  return status;
147
0
}
148
149
/**
150
 * Function description
151
 *
152
 * @return 0 on success, otherwise a Win32 error code
153
 */
154
static UINT disp_recv_display_control_caps_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
155
0
{
156
0
  DISP_PLUGIN* disp = NULL;
157
0
  DispClientContext* context = NULL;
158
0
  UINT ret = CHANNEL_RC_OK;
159
160
0
  WINPR_ASSERT(callback);
161
0
  WINPR_ASSERT(s);
162
163
0
  disp = (DISP_PLUGIN*)callback->plugin;
164
0
  WINPR_ASSERT(disp);
165
166
0
  context = disp->context;
167
0
  WINPR_ASSERT(context);
168
169
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
170
0
    return ERROR_INVALID_DATA;
171
172
0
  Stream_Read_UINT32(s, disp->MaxNumMonitors);        /* MaxNumMonitors (4 bytes) */
173
0
  Stream_Read_UINT32(s, disp->MaxMonitorAreaFactorA); /* MaxMonitorAreaFactorA (4 bytes) */
174
0
  Stream_Read_UINT32(s, disp->MaxMonitorAreaFactorB); /* MaxMonitorAreaFactorB (4 bytes) */
175
176
0
  if (context->DisplayControlCaps)
177
0
    ret = context->DisplayControlCaps(context, disp->MaxNumMonitors,
178
0
                                      disp->MaxMonitorAreaFactorA, disp->MaxMonitorAreaFactorB);
179
180
0
  return ret;
181
0
}
182
183
/**
184
 * Function description
185
 *
186
 * @return 0 on success, otherwise a Win32 error code
187
 */
188
static UINT disp_recv_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
189
0
{
190
0
  UINT32 error = 0;
191
0
  DISPLAY_CONTROL_HEADER header = { 0 };
192
193
0
  WINPR_ASSERT(callback);
194
0
  WINPR_ASSERT(s);
195
196
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
197
0
    return ERROR_INVALID_DATA;
198
199
0
  if ((error = disp_read_header(s, &header)))
200
0
  {
201
0
    WLog_ERR(TAG, "disp_read_header failed with error %" PRIu32 "!", error);
202
0
    return error;
203
0
  }
204
205
0
  if (!Stream_EnsureRemainingCapacity(s, header.length))
206
0
  {
207
0
    WLog_ERR(TAG, "not enough remaining data");
208
0
    return ERROR_INVALID_DATA;
209
0
  }
210
211
0
  switch (header.type)
212
0
  {
213
0
    case DISPLAY_CONTROL_PDU_TYPE_CAPS:
214
0
      return disp_recv_display_control_caps_pdu(callback, s);
215
216
0
    default:
217
0
      WLog_ERR(TAG, "Type %" PRIu32 " not recognized!", header.type);
218
0
      return ERROR_INTERNAL_ERROR;
219
0
  }
220
0
}
221
222
/**
223
 * Function description
224
 *
225
 * @return 0 on success, otherwise a Win32 error code
226
 */
227
static UINT disp_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
228
0
{
229
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
230
0
  return disp_recv_pdu(callback, data);
231
0
}
232
233
/**
234
 * Function description
235
 *
236
 * @return 0 on success, otherwise a Win32 error code
237
 */
238
static UINT disp_on_close(IWTSVirtualChannelCallback* pChannelCallback)
239
0
{
240
0
  free(pChannelCallback);
241
0
  return CHANNEL_RC_OK;
242
0
}
243
244
/**
245
 * Channel Client Interface
246
 */
247
248
/**
249
 * Function description
250
 *
251
 * @return 0 on success, otherwise a Win32 error code
252
 */
253
static UINT disp_send_monitor_layout(DispClientContext* context, UINT32 NumMonitors,
254
                                     DISPLAY_CONTROL_MONITOR_LAYOUT* Monitors)
255
0
{
256
0
  DISP_PLUGIN* disp = NULL;
257
0
  GENERIC_CHANNEL_CALLBACK* callback = NULL;
258
259
0
  WINPR_ASSERT(context);
260
261
0
  disp = (DISP_PLUGIN*)context->handle;
262
0
  WINPR_ASSERT(disp);
263
264
0
  callback = disp->base.listener_callback->channel_callback;
265
266
0
  return disp_send_display_control_monitor_layout_pdu(callback, NumMonitors, Monitors);
267
0
}
268
269
/**
270
 * Function description
271
 *
272
 * @return 0 on success, otherwise a Win32 error code
273
 */
274
static UINT disp_plugin_initialize(GENERIC_DYNVC_PLUGIN* base,
275
                                   WINPR_ATTR_UNUSED rdpContext* rcontext,
276
                                   WINPR_ATTR_UNUSED rdpSettings* settings)
277
0
{
278
0
  DispClientContext* context = NULL;
279
0
  DISP_PLUGIN* disp = (DISP_PLUGIN*)base;
280
281
0
  WINPR_ASSERT(disp);
282
0
  disp->MaxNumMonitors = 16;
283
0
  disp->MaxMonitorAreaFactorA = 8192;
284
0
  disp->MaxMonitorAreaFactorB = 8192;
285
286
0
  context = (DispClientContext*)calloc(1, sizeof(DispClientContext));
287
0
  if (!context)
288
0
  {
289
0
    WLog_Print(base->log, WLOG_ERROR, "unable to allocate DispClientContext");
290
0
    return CHANNEL_RC_NO_MEMORY;
291
0
  }
292
293
0
  context->handle = (void*)disp;
294
0
  context->SendMonitorLayout = disp_send_monitor_layout;
295
296
0
  disp->base.iface.pInterface = disp->context = context;
297
298
0
  return CHANNEL_RC_OK;
299
0
}
300
301
static void disp_plugin_terminated(GENERIC_DYNVC_PLUGIN* base)
302
0
{
303
0
  DISP_PLUGIN* disp = (DISP_PLUGIN*)base;
304
305
0
  WINPR_ASSERT(disp);
306
307
0
  free(disp->context);
308
0
}
309
310
static const IWTSVirtualChannelCallback disp_callbacks = { disp_on_data_received, NULL, /* Open */
311
                                                         disp_on_close, NULL };
312
313
/**
314
 * Function description
315
 *
316
 * @return 0 on success, otherwise a Win32 error code
317
 */
318
FREERDP_ENTRY_POINT(UINT VCAPITYPE disp_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
319
0
{
320
0
  return freerdp_generic_DVCPluginEntry(pEntryPoints, TAG, DISP_DVC_CHANNEL_NAME,
321
0
                                        sizeof(DISP_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
322
0
                                        &disp_callbacks, disp_plugin_initialize,
323
0
                                        disp_plugin_terminated);
324
0
}