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