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