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