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