/src/FreeRDP/channels/rdpei/client/rdpei_main.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Input 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 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <freerdp/config.h> |
23 | | |
24 | | #include <stdio.h> |
25 | | #include <stdlib.h> |
26 | | #include <string.h> |
27 | | #include <stdint.h> |
28 | | |
29 | | #include <winpr/crt.h> |
30 | | #include <winpr/cast.h> |
31 | | #include <winpr/synch.h> |
32 | | #include <winpr/thread.h> |
33 | | #include <winpr/stream.h> |
34 | | #include <winpr/sysinfo.h> |
35 | | #include <winpr/cmdline.h> |
36 | | #include <winpr/collections.h> |
37 | | |
38 | | #include <freerdp/addin.h> |
39 | | #include <freerdp/freerdp.h> |
40 | | #include <freerdp/client/channels.h> |
41 | | |
42 | | #include "rdpei_common.h" |
43 | | |
44 | | #include "rdpei_main.h" |
45 | | |
46 | 0 | #define RDPEI_TAG CHANNELS_TAG("rdpei.client") |
47 | | |
48 | | /** |
49 | | * Touch Input |
50 | | * http://msdn.microsoft.com/en-us/library/windows/desktop/dd562197/ |
51 | | * |
52 | | * Windows Touch Input |
53 | | * http://msdn.microsoft.com/en-us/library/windows/desktop/dd317321/ |
54 | | * |
55 | | * Input: Touch injection sample |
56 | | * http://code.msdn.microsoft.com/windowsdesktop/Touch-Injection-Sample-444d9bf7 |
57 | | * |
58 | | * Pointer Input Message Reference |
59 | | * http://msdn.microsoft.com/en-us/library/hh454916/ |
60 | | * |
61 | | * POINTER_INFO Structure |
62 | | * http://msdn.microsoft.com/en-us/library/hh454907/ |
63 | | * |
64 | | * POINTER_TOUCH_INFO Structure |
65 | | * http://msdn.microsoft.com/en-us/library/hh454910/ |
66 | | */ |
67 | | |
68 | 0 | #define MAX_CONTACTS 64 |
69 | 0 | #define MAX_PEN_CONTACTS 4 |
70 | | |
71 | | typedef struct |
72 | | { |
73 | | GENERIC_DYNVC_PLUGIN base; |
74 | | |
75 | | RdpeiClientContext* context; |
76 | | |
77 | | UINT32 version; |
78 | | UINT32 features; /* SC_READY_MULTIPEN_INJECTION_SUPPORTED */ |
79 | | UINT16 maxTouchContacts; |
80 | | UINT64 currentFrameTime; |
81 | | UINT64 previousFrameTime; |
82 | | RDPINPUT_CONTACT_POINT contactPoints[MAX_CONTACTS]; |
83 | | |
84 | | UINT64 currentPenFrameTime; |
85 | | UINT64 previousPenFrameTime; |
86 | | UINT16 maxPenContacts; |
87 | | RDPINPUT_PEN_CONTACT_POINT penContactPoints[MAX_PEN_CONTACTS]; |
88 | | |
89 | | CRITICAL_SECTION lock; |
90 | | rdpContext* rdpcontext; |
91 | | |
92 | | HANDLE thread; |
93 | | |
94 | | HANDLE event; |
95 | | UINT64 lastPollEventTime; |
96 | | BOOL running; |
97 | | BOOL async; |
98 | | } RDPEI_PLUGIN; |
99 | | |
100 | | /** |
101 | | * Function description |
102 | | * |
103 | | * @return 0 on success, otherwise a Win32 error code |
104 | | */ |
105 | | static UINT rdpei_send_frame(RdpeiClientContext* context, RDPINPUT_TOUCH_FRAME* frame); |
106 | | |
107 | | #ifdef WITH_DEBUG_RDPEI |
108 | | static const char* rdpei_eventid_string(UINT16 event) |
109 | | { |
110 | | switch (event) |
111 | | { |
112 | | case EVENTID_SC_READY: |
113 | | return "EVENTID_SC_READY"; |
114 | | case EVENTID_CS_READY: |
115 | | return "EVENTID_CS_READY"; |
116 | | case EVENTID_TOUCH: |
117 | | return "EVENTID_TOUCH"; |
118 | | case EVENTID_SUSPEND_TOUCH: |
119 | | return "EVENTID_SUSPEND_TOUCH"; |
120 | | case EVENTID_RESUME_TOUCH: |
121 | | return "EVENTID_RESUME_TOUCH"; |
122 | | case EVENTID_DISMISS_HOVERING_CONTACT: |
123 | | return "EVENTID_DISMISS_HOVERING_CONTACT"; |
124 | | case EVENTID_PEN: |
125 | | return "EVENTID_PEN"; |
126 | | default: |
127 | | return "EVENTID_UNKNOWN"; |
128 | | } |
129 | | } |
130 | | #endif |
131 | | |
132 | | static RDPINPUT_CONTACT_POINT* rdpei_contact(RDPEI_PLUGIN* rdpei, INT32 externalId, BOOL active) |
133 | 0 | { |
134 | 0 | for (UINT16 i = 0; i < rdpei->maxTouchContacts; i++) |
135 | 0 | { |
136 | 0 | RDPINPUT_CONTACT_POINT* contactPoint = &rdpei->contactPoints[i]; |
137 | |
|
138 | 0 | if (!contactPoint->active && active) |
139 | 0 | continue; |
140 | 0 | else if (!contactPoint->active && !active) |
141 | 0 | { |
142 | 0 | contactPoint->contactId = i; |
143 | 0 | contactPoint->externalId = externalId; |
144 | 0 | contactPoint->active = TRUE; |
145 | 0 | return contactPoint; |
146 | 0 | } |
147 | 0 | else if (contactPoint->externalId == externalId) |
148 | 0 | { |
149 | 0 | return contactPoint; |
150 | 0 | } |
151 | 0 | } |
152 | 0 | return nullptr; |
153 | 0 | } |
154 | | |
155 | | /** |
156 | | * Function description |
157 | | * |
158 | | * @return 0 on success, otherwise a Win32 error code |
159 | | */ |
160 | | static UINT rdpei_add_frame(RdpeiClientContext* context) |
161 | 0 | { |
162 | 0 | RDPINPUT_TOUCH_FRAME frame = WINPR_C_ARRAY_INIT; |
163 | 0 | RDPINPUT_CONTACT_DATA contacts[MAX_CONTACTS] = WINPR_C_ARRAY_INIT; |
164 | |
|
165 | 0 | if (!context || !context->handle) |
166 | 0 | return ERROR_INTERNAL_ERROR; |
167 | | |
168 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle; |
169 | 0 | frame.contacts = contacts; |
170 | |
|
171 | 0 | for (UINT16 i = 0; i < rdpei->maxTouchContacts; i++) |
172 | 0 | { |
173 | 0 | RDPINPUT_CONTACT_POINT* contactPoint = &rdpei->contactPoints[i]; |
174 | 0 | RDPINPUT_CONTACT_DATA* contact = &contactPoint->data; |
175 | |
|
176 | 0 | if (contactPoint->dirty) |
177 | 0 | { |
178 | 0 | contacts[frame.contactCount] = *contact; |
179 | 0 | rdpei->contactPoints[i].dirty = FALSE; |
180 | 0 | frame.contactCount++; |
181 | 0 | } |
182 | 0 | else if (contactPoint->active) |
183 | 0 | { |
184 | 0 | if (contact->contactFlags & RDPINPUT_CONTACT_FLAG_DOWN) |
185 | 0 | { |
186 | 0 | contact->contactFlags = RDPINPUT_CONTACT_FLAG_UPDATE; |
187 | 0 | contact->contactFlags |= RDPINPUT_CONTACT_FLAG_INRANGE; |
188 | 0 | contact->contactFlags |= RDPINPUT_CONTACT_FLAG_INCONTACT; |
189 | 0 | } |
190 | |
|
191 | 0 | contacts[frame.contactCount] = *contact; |
192 | 0 | frame.contactCount++; |
193 | 0 | } |
194 | 0 | if (contact->contactFlags & RDPINPUT_CONTACT_FLAG_UP) |
195 | 0 | { |
196 | 0 | contactPoint->active = FALSE; |
197 | 0 | contactPoint->externalId = 0; |
198 | 0 | contactPoint->contactId = 0; |
199 | 0 | } |
200 | 0 | } |
201 | |
|
202 | 0 | if (frame.contactCount > 0) |
203 | 0 | { |
204 | 0 | UINT error = rdpei_send_frame(context, &frame); |
205 | 0 | if (error != CHANNEL_RC_OK) |
206 | 0 | { |
207 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, |
208 | 0 | "rdpei_send_frame failed with error %" PRIu32 "!", error); |
209 | 0 | return error; |
210 | 0 | } |
211 | 0 | } |
212 | 0 | return CHANNEL_RC_OK; |
213 | 0 | } |
214 | | |
215 | | /** |
216 | | * Function description |
217 | | * |
218 | | * @return 0 on success, otherwise a Win32 error code |
219 | | */ |
220 | | static UINT rdpei_send_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s, UINT16 eventId, |
221 | | size_t pduLength) |
222 | 0 | { |
223 | 0 | if (!callback || !s || !callback->channel || !callback->channel->Write) |
224 | 0 | return ERROR_INTERNAL_ERROR; |
225 | | |
226 | 0 | if (pduLength > UINT32_MAX) |
227 | 0 | return ERROR_INVALID_PARAMETER; |
228 | | |
229 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin; |
230 | 0 | if (!rdpei) |
231 | 0 | return ERROR_INTERNAL_ERROR; |
232 | | |
233 | 0 | Stream_ResetPosition(s); |
234 | 0 | Stream_Write_UINT16(s, eventId); /* eventId (2 bytes) */ |
235 | 0 | Stream_Write_UINT32(s, (UINT32)pduLength); /* pduLength (4 bytes) */ |
236 | 0 | if (!Stream_SetPosition(s, Stream_Length(s))) |
237 | 0 | return ERROR_INVALID_DATA; |
238 | 0 | const UINT status = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), |
239 | 0 | Stream_Buffer(s), nullptr); |
240 | | #ifdef WITH_DEBUG_RDPEI |
241 | | WLog_Print(rdpei->base.log, WLOG_DEBUG, |
242 | | "rdpei_send_pdu: eventId: %" PRIu16 " (%s) length: %" PRIuz " status: %" PRIu32 "", |
243 | | eventId, rdpei_eventid_string(eventId), pduLength, status); |
244 | | #endif |
245 | 0 | return status; |
246 | 0 | } |
247 | | |
248 | | static UINT rdpei_write_pen_frame(wStream* s, const RDPINPUT_PEN_FRAME* frame) |
249 | 0 | { |
250 | 0 | if (!s || !frame) |
251 | 0 | return ERROR_INTERNAL_ERROR; |
252 | | |
253 | 0 | if (!rdpei_write_2byte_unsigned(s, frame->contactCount)) |
254 | 0 | return ERROR_OUTOFMEMORY; |
255 | 0 | if (!rdpei_write_8byte_unsigned(s, frame->frameOffset)) |
256 | 0 | return ERROR_OUTOFMEMORY; |
257 | 0 | for (UINT16 x = 0; x < frame->contactCount; x++) |
258 | 0 | { |
259 | 0 | const RDPINPUT_PEN_CONTACT* contact = &frame->contacts[x]; |
260 | |
|
261 | 0 | if (!Stream_EnsureRemainingCapacity(s, 1)) |
262 | 0 | return ERROR_OUTOFMEMORY; |
263 | 0 | Stream_Write_UINT8(s, contact->deviceId); |
264 | 0 | if (!rdpei_write_2byte_unsigned(s, contact->fieldsPresent)) |
265 | 0 | return ERROR_OUTOFMEMORY; |
266 | 0 | if (!rdpei_write_4byte_signed(s, contact->x)) |
267 | 0 | return ERROR_OUTOFMEMORY; |
268 | 0 | if (!rdpei_write_4byte_signed(s, contact->y)) |
269 | 0 | return ERROR_OUTOFMEMORY; |
270 | 0 | if (!rdpei_write_4byte_unsigned(s, contact->contactFlags)) |
271 | 0 | return ERROR_OUTOFMEMORY; |
272 | 0 | if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT) |
273 | 0 | { |
274 | 0 | if (!rdpei_write_4byte_unsigned(s, contact->penFlags)) |
275 | 0 | return ERROR_OUTOFMEMORY; |
276 | 0 | } |
277 | 0 | if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT) |
278 | 0 | { |
279 | 0 | if (!rdpei_write_4byte_unsigned(s, contact->pressure)) |
280 | 0 | return ERROR_OUTOFMEMORY; |
281 | 0 | } |
282 | 0 | if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT) |
283 | 0 | { |
284 | 0 | if (!rdpei_write_2byte_unsigned(s, contact->rotation)) |
285 | 0 | return ERROR_OUTOFMEMORY; |
286 | 0 | } |
287 | 0 | if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTX_PRESENT) |
288 | 0 | { |
289 | 0 | if (!rdpei_write_2byte_signed(s, contact->tiltX)) |
290 | 0 | return ERROR_OUTOFMEMORY; |
291 | 0 | } |
292 | 0 | if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTY_PRESENT) |
293 | 0 | { |
294 | 0 | if (!rdpei_write_2byte_signed(s, contact->tiltY)) |
295 | 0 | return ERROR_OUTOFMEMORY; |
296 | 0 | } |
297 | 0 | } |
298 | 0 | return CHANNEL_RC_OK; |
299 | 0 | } |
300 | | |
301 | | static UINT rdpei_send_pen_event_pdu(GENERIC_CHANNEL_CALLBACK* callback, size_t frameOffset, |
302 | | const RDPINPUT_PEN_FRAME* frames, size_t count) |
303 | 0 | { |
304 | 0 | UINT status = ERROR_OUTOFMEMORY; |
305 | 0 | WINPR_ASSERT(callback); |
306 | |
|
307 | 0 | if (frameOffset > UINT32_MAX) |
308 | 0 | return ERROR_INVALID_PARAMETER; |
309 | 0 | if (count > UINT16_MAX) |
310 | 0 | return ERROR_INVALID_PARAMETER; |
311 | | |
312 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin; |
313 | 0 | if (!rdpei) |
314 | 0 | return ERROR_INTERNAL_ERROR; |
315 | | |
316 | 0 | if (!frames || (count == 0)) |
317 | 0 | return ERROR_INTERNAL_ERROR; |
318 | | |
319 | 0 | wStream* s = Stream_New(nullptr, 64); |
320 | |
|
321 | 0 | if (!s) |
322 | 0 | { |
323 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, "Stream_New failed!"); |
324 | 0 | return CHANNEL_RC_NO_MEMORY; |
325 | 0 | } |
326 | | |
327 | 0 | Stream_Seek(s, RDPINPUT_HEADER_LENGTH); |
328 | | /** |
329 | | * the time that has elapsed (in milliseconds) from when the oldest touch frame |
330 | | * was generated to when it was encoded for transmission by the client. |
331 | | */ |
332 | 0 | if (!rdpei_write_4byte_unsigned( |
333 | 0 | s, (UINT32)frameOffset)) /* encodeTime (FOUR_BYTE_UNSIGNED_INTEGER) */ |
334 | 0 | goto fail; |
335 | 0 | if (!rdpei_write_2byte_unsigned(s, (UINT16)count)) /* (frameCount) TWO_BYTE_UNSIGNED_INTEGER */ |
336 | 0 | goto fail; |
337 | | |
338 | 0 | for (size_t x = 0; x < count; x++) |
339 | 0 | { |
340 | 0 | status = rdpei_write_pen_frame(s, &frames[x]); |
341 | 0 | if (status) |
342 | 0 | { |
343 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, |
344 | 0 | "rdpei_write_pen_frame failed with error %" PRIu32 "!", status); |
345 | 0 | goto fail; |
346 | 0 | } |
347 | 0 | } |
348 | 0 | Stream_SealLength(s); |
349 | |
|
350 | 0 | status = rdpei_send_pdu(callback, s, EVENTID_PEN, Stream_Length(s)); |
351 | 0 | fail: |
352 | 0 | Stream_Free(s, TRUE); |
353 | 0 | return status; |
354 | 0 | } |
355 | | |
356 | | static UINT rdpei_send_pen_frame(RdpeiClientContext* context, RDPINPUT_PEN_FRAME* frame) |
357 | 0 | { |
358 | 0 | const UINT64 currentTime = GetTickCount64(); |
359 | |
|
360 | 0 | if (!context) |
361 | 0 | return ERROR_INTERNAL_ERROR; |
362 | | |
363 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle; |
364 | 0 | if (!rdpei || !rdpei->base.listener_callback) |
365 | 0 | return ERROR_INTERNAL_ERROR; |
366 | 0 | if (!rdpei || !rdpei->rdpcontext) |
367 | 0 | return ERROR_INTERNAL_ERROR; |
368 | 0 | if (freerdp_settings_get_bool(rdpei->rdpcontext->settings, FreeRDP_SuspendInput)) |
369 | 0 | return CHANNEL_RC_OK; |
370 | | |
371 | 0 | GENERIC_CHANNEL_CALLBACK* callback = rdpei->base.listener_callback->channel_callback; |
372 | | /* Just ignore the event if the channel is not connected */ |
373 | 0 | if (!callback) |
374 | 0 | return CHANNEL_RC_OK; |
375 | | |
376 | 0 | if (!rdpei->previousPenFrameTime && !rdpei->currentPenFrameTime) |
377 | 0 | { |
378 | 0 | rdpei->currentPenFrameTime = currentTime; |
379 | 0 | frame->frameOffset = 0; |
380 | 0 | } |
381 | 0 | else |
382 | 0 | { |
383 | 0 | rdpei->currentPenFrameTime = currentTime; |
384 | 0 | frame->frameOffset = rdpei->currentPenFrameTime - rdpei->previousPenFrameTime; |
385 | 0 | } |
386 | |
|
387 | 0 | const size_t off = WINPR_ASSERTING_INT_CAST(size_t, frame->frameOffset); |
388 | 0 | const UINT error = rdpei_send_pen_event_pdu(callback, off, frame, 1); |
389 | 0 | if (error) |
390 | 0 | return error; |
391 | | |
392 | 0 | rdpei->previousPenFrameTime = rdpei->currentPenFrameTime; |
393 | 0 | return error; |
394 | 0 | } |
395 | | |
396 | | static UINT rdpei_add_pen_frame(RdpeiClientContext* context) |
397 | 0 | { |
398 | 0 | RDPINPUT_PEN_FRAME penFrame = WINPR_C_ARRAY_INIT; |
399 | 0 | RDPINPUT_PEN_CONTACT penContacts[MAX_PEN_CONTACTS] = WINPR_C_ARRAY_INIT; |
400 | |
|
401 | 0 | if (!context || !context->handle) |
402 | 0 | return ERROR_INTERNAL_ERROR; |
403 | | |
404 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle; |
405 | |
|
406 | 0 | penFrame.contacts = penContacts; |
407 | |
|
408 | 0 | for (UINT16 i = 0; i < rdpei->maxPenContacts; i++) |
409 | 0 | { |
410 | 0 | RDPINPUT_PEN_CONTACT_POINT* contact = &(rdpei->penContactPoints[i]); |
411 | |
|
412 | 0 | if (contact->dirty) |
413 | 0 | { |
414 | 0 | penContacts[penFrame.contactCount++] = contact->data; |
415 | 0 | contact->dirty = FALSE; |
416 | 0 | } |
417 | 0 | else if (contact->active) |
418 | 0 | { |
419 | 0 | if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_DOWN) |
420 | 0 | { |
421 | 0 | contact->data.contactFlags = RDPINPUT_CONTACT_FLAG_UPDATE; |
422 | 0 | contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INRANGE; |
423 | 0 | contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INCONTACT; |
424 | 0 | } |
425 | |
|
426 | 0 | penContacts[penFrame.contactCount++] = contact->data; |
427 | 0 | } |
428 | 0 | if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED) |
429 | 0 | { |
430 | 0 | contact->externalId = 0; |
431 | 0 | contact->active = FALSE; |
432 | 0 | } |
433 | 0 | } |
434 | |
|
435 | 0 | if (penFrame.contactCount > 0) |
436 | 0 | return rdpei_send_pen_frame(context, &penFrame); |
437 | 0 | return CHANNEL_RC_OK; |
438 | 0 | } |
439 | | |
440 | | static UINT rdpei_update(wLog* log, RdpeiClientContext* context) |
441 | 0 | { |
442 | 0 | UINT error = rdpei_add_frame(context); |
443 | 0 | if (error != CHANNEL_RC_OK) |
444 | 0 | { |
445 | 0 | WLog_Print(log, WLOG_ERROR, "rdpei_add_frame failed with error %" PRIu32 "!", error); |
446 | 0 | return error; |
447 | 0 | } |
448 | | |
449 | 0 | return rdpei_add_pen_frame(context); |
450 | 0 | } |
451 | | |
452 | | static BOOL rdpei_poll_run_unlocked(rdpContext* context, void* userdata) |
453 | 0 | { |
454 | 0 | RDPEI_PLUGIN* rdpei = userdata; |
455 | 0 | WINPR_ASSERT(rdpei); |
456 | 0 | WINPR_ASSERT(context); |
457 | |
|
458 | 0 | const UINT64 now = GetTickCount64(); |
459 | | |
460 | | /* Send an event every ~20ms */ |
461 | 0 | if ((now < rdpei->lastPollEventTime) || (now - rdpei->lastPollEventTime < 20ULL)) |
462 | 0 | return TRUE; |
463 | | |
464 | 0 | rdpei->lastPollEventTime = now; |
465 | |
|
466 | 0 | const UINT error = rdpei_update(rdpei->base.log, rdpei->context); |
467 | |
|
468 | 0 | (void)ResetEvent(rdpei->event); |
469 | |
|
470 | 0 | if (error != CHANNEL_RC_OK) |
471 | 0 | { |
472 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, "rdpei_add_frame failed with error %" PRIu32 "!", |
473 | 0 | error); |
474 | 0 | setChannelError(context, error, "rdpei_add_frame reported an error"); |
475 | 0 | return FALSE; |
476 | 0 | } |
477 | | |
478 | 0 | return TRUE; |
479 | 0 | } |
480 | | |
481 | | static BOOL rdpei_poll_run(rdpContext* context, void* userdata) |
482 | 0 | { |
483 | 0 | RDPEI_PLUGIN* rdpei = userdata; |
484 | 0 | WINPR_ASSERT(rdpei); |
485 | |
|
486 | 0 | EnterCriticalSection(&rdpei->lock); |
487 | 0 | BOOL rc = rdpei_poll_run_unlocked(context, userdata); |
488 | 0 | LeaveCriticalSection(&rdpei->lock); |
489 | 0 | return rc; |
490 | 0 | } |
491 | | |
492 | | static DWORD WINAPI rdpei_periodic_update(LPVOID arg) |
493 | 0 | { |
494 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)arg; |
495 | 0 | UINT error = CHANNEL_RC_OK; |
496 | |
|
497 | 0 | if (!rdpei) |
498 | 0 | { |
499 | 0 | error = ERROR_INVALID_PARAMETER; |
500 | 0 | goto out; |
501 | 0 | } |
502 | | |
503 | 0 | if (!rdpei->context) |
504 | 0 | { |
505 | 0 | error = ERROR_INVALID_PARAMETER; |
506 | 0 | goto out; |
507 | 0 | } |
508 | | |
509 | 0 | while (rdpei->running) |
510 | 0 | { |
511 | 0 | const DWORD status = WaitForSingleObject(rdpei->event, 20); |
512 | |
|
513 | 0 | if (status == WAIT_FAILED) |
514 | 0 | { |
515 | 0 | error = GetLastError(); |
516 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, |
517 | 0 | "WaitForMultipleObjects failed with error %" PRIu32 "!", error); |
518 | 0 | break; |
519 | 0 | } |
520 | | |
521 | 0 | if (!rdpei_poll_run(rdpei->rdpcontext, rdpei)) |
522 | 0 | error = ERROR_INTERNAL_ERROR; |
523 | 0 | } |
524 | |
|
525 | 0 | out: |
526 | |
|
527 | 0 | if (error && rdpei && rdpei->rdpcontext) |
528 | 0 | setChannelError(rdpei->rdpcontext, error, "rdpei_schedule_thread reported an error"); |
529 | |
|
530 | 0 | if (rdpei) |
531 | 0 | rdpei->running = FALSE; |
532 | |
|
533 | 0 | ExitThread(error); |
534 | 0 | return error; |
535 | 0 | } |
536 | | |
537 | | /** |
538 | | * Function description |
539 | | * |
540 | | * @return 0 on success, otherwise a Win32 error code |
541 | | */ |
542 | | static UINT rdpei_send_cs_ready_pdu(GENERIC_CHANNEL_CALLBACK* callback) |
543 | 0 | { |
544 | 0 | if (!callback || !callback->plugin) |
545 | 0 | return ERROR_INTERNAL_ERROR; |
546 | | |
547 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin; |
548 | |
|
549 | 0 | UINT32 flags = CS_READY_FLAGS_SHOW_TOUCH_VISUALS & rdpei->context->clientFeaturesMask; |
550 | 0 | if (rdpei->version > RDPINPUT_PROTOCOL_V10) |
551 | 0 | flags |= CS_READY_FLAGS_DISABLE_TIMESTAMP_INJECTION & rdpei->context->clientFeaturesMask; |
552 | 0 | if (rdpei->features & SC_READY_MULTIPEN_INJECTION_SUPPORTED) |
553 | 0 | flags |= CS_READY_FLAGS_ENABLE_MULTIPEN_INJECTION & rdpei->context->clientFeaturesMask; |
554 | |
|
555 | 0 | UINT32 pduLength = RDPINPUT_HEADER_LENGTH + 10; |
556 | 0 | wStream* s = Stream_New(nullptr, pduLength); |
557 | |
|
558 | 0 | if (!s) |
559 | 0 | { |
560 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, "Stream_New failed!"); |
561 | 0 | return CHANNEL_RC_NO_MEMORY; |
562 | 0 | } |
563 | | |
564 | 0 | Stream_Seek(s, RDPINPUT_HEADER_LENGTH); |
565 | 0 | Stream_Write_UINT32(s, flags); /* flags (4 bytes) */ |
566 | 0 | Stream_Write_UINT32(s, rdpei->version); /* protocolVersion (4 bytes) */ |
567 | 0 | Stream_Write_UINT16(s, rdpei->maxTouchContacts); /* maxTouchContacts (2 bytes) */ |
568 | 0 | Stream_SealLength(s); |
569 | |
|
570 | 0 | const UINT status = rdpei_send_pdu(callback, s, EVENTID_CS_READY, pduLength); |
571 | 0 | Stream_Free(s, TRUE); |
572 | 0 | return status; |
573 | 0 | } |
574 | | |
575 | | #if defined(WITH_DEBUG_RDPEI) |
576 | | static void rdpei_print_contact_flags(wLog* log, UINT32 contactFlags) |
577 | | { |
578 | | if (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN) |
579 | | WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_DOWN"); |
580 | | |
581 | | if (contactFlags & RDPINPUT_CONTACT_FLAG_UPDATE) |
582 | | WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_UPDATE"); |
583 | | |
584 | | if (contactFlags & RDPINPUT_CONTACT_FLAG_UP) |
585 | | WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_UP"); |
586 | | |
587 | | if (contactFlags & RDPINPUT_CONTACT_FLAG_INRANGE) |
588 | | WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_INRANGE"); |
589 | | |
590 | | if (contactFlags & RDPINPUT_CONTACT_FLAG_INCONTACT) |
591 | | WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_INCONTACT"); |
592 | | |
593 | | if (contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED) |
594 | | WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_CANCELED"); |
595 | | } |
596 | | #endif |
597 | | |
598 | | static INT16 bounded(INT32 val) |
599 | 0 | { |
600 | 0 | if (val < INT16_MIN) |
601 | 0 | return INT16_MIN; |
602 | 0 | if (val > INT16_MAX) |
603 | 0 | return INT16_MAX; |
604 | 0 | return (INT16)val; |
605 | 0 | } |
606 | | |
607 | | /** |
608 | | * Function description |
609 | | * |
610 | | * @return 0 on success, otherwise a Win32 error code |
611 | | */ |
612 | | static UINT rdpei_write_touch_frame(wLog* log, wStream* s, RDPINPUT_TOUCH_FRAME* frame) |
613 | 0 | { |
614 | 0 | int rectSize = 2; |
615 | 0 | if (!s || !frame) |
616 | 0 | return ERROR_INTERNAL_ERROR; |
617 | | #ifdef WITH_DEBUG_RDPEI |
618 | | WLog_Print(log, WLOG_DEBUG, "contactCount: %" PRIu32 "", frame->contactCount); |
619 | | WLog_Print(log, WLOG_DEBUG, "frameOffset: 0x%016" PRIX64 "", frame->frameOffset); |
620 | | #endif |
621 | 0 | if (!rdpei_write_2byte_unsigned( |
622 | 0 | s, frame->contactCount)) /* contactCount (TWO_BYTE_UNSIGNED_INTEGER) */ |
623 | 0 | return ERROR_OUTOFMEMORY; |
624 | | /** |
625 | | * the time offset from the previous frame (in microseconds). |
626 | | * If this is the first frame being transmitted then this field MUST be set to zero. |
627 | | */ |
628 | 0 | if (!rdpei_write_8byte_unsigned(s, frame->frameOffset * |
629 | 0 | 1000)) /* frameOffset (EIGHT_BYTE_UNSIGNED_INTEGER) */ |
630 | 0 | return ERROR_OUTOFMEMORY; |
631 | | |
632 | 0 | if (!Stream_EnsureRemainingCapacity(s, (size_t)frame->contactCount * 64)) |
633 | 0 | { |
634 | 0 | WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!"); |
635 | 0 | return CHANNEL_RC_NO_MEMORY; |
636 | 0 | } |
637 | | |
638 | 0 | for (UINT32 index = 0; index < frame->contactCount; index++) |
639 | 0 | { |
640 | 0 | RDPINPUT_CONTACT_DATA* contact = &frame->contacts[index]; |
641 | |
|
642 | 0 | contact->fieldsPresent |= CONTACT_DATA_CONTACTRECT_PRESENT; |
643 | 0 | contact->contactRectLeft = bounded(contact->x - rectSize); |
644 | 0 | contact->contactRectTop = bounded(contact->y - rectSize); |
645 | 0 | contact->contactRectRight = bounded(contact->x + rectSize); |
646 | 0 | contact->contactRectBottom = bounded(contact->y + rectSize); |
647 | | #ifdef WITH_DEBUG_RDPEI |
648 | | WLog_Print(log, WLOG_DEBUG, "contact[%" PRIu32 "].contactId: %" PRIu32 "", index, |
649 | | contact->contactId); |
650 | | WLog_Print(log, WLOG_DEBUG, "contact[%" PRIu32 "].fieldsPresent: %" PRIu32 "", index, |
651 | | contact->fieldsPresent); |
652 | | WLog_Print(log, WLOG_DEBUG, "contact[%" PRIu32 "].x: %" PRId32 "", index, contact->x); |
653 | | WLog_Print(log, WLOG_DEBUG, "contact[%" PRIu32 "].y: %" PRId32 "", index, contact->y); |
654 | | WLog_Print(log, WLOG_DEBUG, "contact[%" PRIu32 "].contactFlags: 0x%08" PRIX32 "", index, |
655 | | contact->contactFlags); |
656 | | rdpei_print_contact_flags(log, contact->contactFlags); |
657 | | #endif |
658 | 0 | Stream_Write_UINT8( |
659 | 0 | s, WINPR_ASSERTING_INT_CAST(uint8_t, contact->contactId)); /* contactId (1 byte) */ |
660 | | /* fieldsPresent (TWO_BYTE_UNSIGNED_INTEGER) */ |
661 | 0 | if (!rdpei_write_2byte_unsigned(s, contact->fieldsPresent)) |
662 | 0 | return ERROR_OUTOFMEMORY; |
663 | 0 | if (!rdpei_write_4byte_signed(s, contact->x)) /* x (FOUR_BYTE_SIGNED_INTEGER) */ |
664 | 0 | return ERROR_OUTOFMEMORY; |
665 | 0 | if (!rdpei_write_4byte_signed(s, contact->y)) /* y (FOUR_BYTE_SIGNED_INTEGER) */ |
666 | 0 | return ERROR_OUTOFMEMORY; |
667 | | /* contactFlags (FOUR_BYTE_UNSIGNED_INTEGER) */ |
668 | 0 | if (!rdpei_write_4byte_unsigned(s, contact->contactFlags)) |
669 | 0 | return ERROR_OUTOFMEMORY; |
670 | | |
671 | 0 | if (contact->fieldsPresent & CONTACT_DATA_CONTACTRECT_PRESENT) |
672 | 0 | { |
673 | | /* contactRectLeft (TWO_BYTE_SIGNED_INTEGER) */ |
674 | 0 | if (!rdpei_write_2byte_signed(s, contact->contactRectLeft)) |
675 | 0 | return ERROR_OUTOFMEMORY; |
676 | | /* contactRectTop (TWO_BYTE_SIGNED_INTEGER) */ |
677 | 0 | if (!rdpei_write_2byte_signed(s, contact->contactRectTop)) |
678 | 0 | return ERROR_OUTOFMEMORY; |
679 | | /* contactRectRight (TWO_BYTE_SIGNED_INTEGER) */ |
680 | 0 | if (!rdpei_write_2byte_signed(s, contact->contactRectRight)) |
681 | 0 | return ERROR_OUTOFMEMORY; |
682 | | /* contactRectBottom (TWO_BYTE_SIGNED_INTEGER) */ |
683 | 0 | if (!rdpei_write_2byte_signed(s, contact->contactRectBottom)) |
684 | 0 | return ERROR_OUTOFMEMORY; |
685 | 0 | } |
686 | | |
687 | 0 | if (contact->fieldsPresent & CONTACT_DATA_ORIENTATION_PRESENT) |
688 | 0 | { |
689 | | /* orientation (FOUR_BYTE_UNSIGNED_INTEGER) */ |
690 | 0 | if (!rdpei_write_4byte_unsigned(s, contact->orientation)) |
691 | 0 | return ERROR_OUTOFMEMORY; |
692 | 0 | } |
693 | | |
694 | 0 | if (contact->fieldsPresent & CONTACT_DATA_PRESSURE_PRESENT) |
695 | 0 | { |
696 | | /* pressure (FOUR_BYTE_UNSIGNED_INTEGER) */ |
697 | 0 | if (!rdpei_write_4byte_unsigned(s, contact->pressure)) |
698 | 0 | return ERROR_OUTOFMEMORY; |
699 | 0 | } |
700 | 0 | } |
701 | | |
702 | 0 | return CHANNEL_RC_OK; |
703 | 0 | } |
704 | | |
705 | | /** |
706 | | * Function description |
707 | | * |
708 | | * @return 0 on success, otherwise a Win32 error code |
709 | | */ |
710 | | static UINT rdpei_send_touch_event_pdu(GENERIC_CHANNEL_CALLBACK* callback, |
711 | | RDPINPUT_TOUCH_FRAME* frame) |
712 | 0 | { |
713 | 0 | UINT status = ERROR_OUTOFMEMORY; |
714 | 0 | WINPR_ASSERT(callback); |
715 | |
|
716 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin; |
717 | 0 | if (!rdpei || !rdpei->rdpcontext) |
718 | 0 | return ERROR_INTERNAL_ERROR; |
719 | 0 | if (freerdp_settings_get_bool(rdpei->rdpcontext->settings, FreeRDP_SuspendInput)) |
720 | 0 | return CHANNEL_RC_OK; |
721 | | |
722 | 0 | if (!frame) |
723 | 0 | return ERROR_INTERNAL_ERROR; |
724 | | |
725 | 0 | size_t pduLength = 64ULL + (64ULL * frame->contactCount); |
726 | 0 | wStream* s = Stream_New(nullptr, pduLength); |
727 | |
|
728 | 0 | if (!s) |
729 | 0 | { |
730 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, "Stream_New failed!"); |
731 | 0 | return CHANNEL_RC_NO_MEMORY; |
732 | 0 | } |
733 | | |
734 | 0 | if (!Stream_SafeSeek(s, RDPINPUT_HEADER_LENGTH)) |
735 | 0 | goto fail; |
736 | | /** |
737 | | * the time that has elapsed (in milliseconds) from when the oldest touch frame |
738 | | * was generated to when it was encoded for transmission by the client. |
739 | | */ |
740 | 0 | if (!rdpei_write_4byte_unsigned( |
741 | 0 | s, (UINT32)frame->frameOffset)) /* encodeTime (FOUR_BYTE_UNSIGNED_INTEGER) */ |
742 | 0 | goto fail; |
743 | 0 | if (!rdpei_write_2byte_unsigned(s, 1)) /* (frameCount) TWO_BYTE_UNSIGNED_INTEGER */ |
744 | 0 | goto fail; |
745 | | |
746 | 0 | const UINT rc = rdpei_write_touch_frame(rdpei->base.log, s, frame); |
747 | 0 | if (rc) |
748 | 0 | { |
749 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, |
750 | 0 | "rdpei_write_touch_frame failed with error %" PRIu32 "!", rc); |
751 | 0 | status = rc; |
752 | 0 | goto fail; |
753 | 0 | } |
754 | | |
755 | 0 | Stream_SealLength(s); |
756 | |
|
757 | 0 | status = rdpei_send_pdu(callback, s, EVENTID_TOUCH, Stream_Length(s)); |
758 | 0 | fail: |
759 | 0 | Stream_Free(s, TRUE); |
760 | 0 | return status; |
761 | 0 | } |
762 | | |
763 | | /** |
764 | | * Function description |
765 | | * |
766 | | * @return 0 on success, otherwise a Win32 error code |
767 | | */ |
768 | | static UINT rdpei_recv_sc_ready_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s) |
769 | 0 | { |
770 | 0 | UINT32 features = 0; |
771 | 0 | UINT32 protocolVersion = 0; |
772 | |
|
773 | 0 | if (!callback || !callback->plugin) |
774 | 0 | return ERROR_INTERNAL_ERROR; |
775 | | |
776 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin; |
777 | |
|
778 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4)) |
779 | 0 | return ERROR_INVALID_DATA; |
780 | 0 | Stream_Read_UINT32(s, protocolVersion); /* protocolVersion (4 bytes) */ |
781 | |
|
782 | 0 | if (protocolVersion >= RDPINPUT_PROTOCOL_V300) |
783 | 0 | { |
784 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4)) |
785 | 0 | return ERROR_INVALID_DATA; |
786 | 0 | } |
787 | | |
788 | 0 | if (Stream_GetRemainingLength(s) >= 4) |
789 | 0 | Stream_Read_UINT32(s, features); |
790 | |
|
791 | 0 | if (rdpei->version > protocolVersion) |
792 | 0 | rdpei->version = protocolVersion; |
793 | 0 | rdpei->features = features; |
794 | |
|
795 | 0 | if (protocolVersion > RDPINPUT_PROTOCOL_V300) |
796 | 0 | { |
797 | 0 | WLog_Print(rdpei->base.log, WLOG_WARN, |
798 | 0 | "Unknown [MS-RDPEI] protocolVersion: 0x%08" PRIX32 "", protocolVersion); |
799 | 0 | } |
800 | |
|
801 | 0 | return CHANNEL_RC_OK; |
802 | 0 | } |
803 | | |
804 | | /** |
805 | | * Function description |
806 | | * |
807 | | * @return 0 on success, otherwise a Win32 error code |
808 | | */ |
809 | | static UINT rdpei_recv_suspend_touch_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s) |
810 | 0 | { |
811 | 0 | UINT error = CHANNEL_RC_OK; |
812 | |
|
813 | 0 | WINPR_UNUSED(s); |
814 | |
|
815 | 0 | if (!callback || !callback->plugin) |
816 | 0 | return ERROR_INTERNAL_ERROR; |
817 | | |
818 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin; |
819 | 0 | RdpeiClientContext* context = rdpei->context; |
820 | 0 | if (!rdpei) |
821 | 0 | return ERROR_INTERNAL_ERROR; |
822 | | |
823 | 0 | IFCALLRET(context->SuspendTouch, error, context); |
824 | |
|
825 | 0 | if (error) |
826 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, |
827 | 0 | "rdpei->SuspendTouch failed with error %" PRIu32 "!", error); |
828 | |
|
829 | 0 | return error; |
830 | 0 | } |
831 | | |
832 | | /** |
833 | | * Function description |
834 | | * |
835 | | * @return 0 on success, otherwise a Win32 error code |
836 | | */ |
837 | | static UINT rdpei_recv_resume_touch_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s) |
838 | 0 | { |
839 | 0 | UINT error = CHANNEL_RC_OK; |
840 | 0 | if (!s || !callback) |
841 | 0 | return ERROR_INTERNAL_ERROR; |
842 | | |
843 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin; |
844 | 0 | if (!rdpei) |
845 | 0 | return ERROR_INTERNAL_ERROR; |
846 | | |
847 | 0 | RdpeiClientContext* context = (RdpeiClientContext*)callback->plugin->pInterface; |
848 | 0 | if (!context) |
849 | 0 | return ERROR_INTERNAL_ERROR; |
850 | | |
851 | 0 | IFCALLRET(context->ResumeTouch, error, context); |
852 | |
|
853 | 0 | if (error) |
854 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, "rdpei->ResumeTouch failed with error %" PRIu32 "!", |
855 | 0 | error); |
856 | |
|
857 | 0 | return error; |
858 | 0 | } |
859 | | |
860 | | /** |
861 | | * Function description |
862 | | * |
863 | | * @return 0 on success, otherwise a Win32 error code |
864 | | */ |
865 | | static UINT rdpei_recv_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s) |
866 | 0 | { |
867 | 0 | UINT16 eventId = 0; |
868 | 0 | UINT32 pduLength = 0; |
869 | 0 | UINT error = 0; |
870 | |
|
871 | 0 | if (!callback || !s) |
872 | 0 | return ERROR_INTERNAL_ERROR; |
873 | | |
874 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin; |
875 | 0 | if (!rdpei) |
876 | 0 | return ERROR_INTERNAL_ERROR; |
877 | | |
878 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 6)) |
879 | 0 | return ERROR_INVALID_DATA; |
880 | | |
881 | 0 | Stream_Read_UINT16(s, eventId); /* eventId (2 bytes) */ |
882 | 0 | Stream_Read_UINT32(s, pduLength); /* pduLength (4 bytes) */ |
883 | | #ifdef WITH_DEBUG_RDPEI |
884 | | WLog_Print(rdpei->base.log, WLOG_DEBUG, |
885 | | "rdpei_recv_pdu: eventId: %" PRIu16 " (%s) length: %" PRIu32 "", eventId, |
886 | | rdpei_eventid_string(eventId), pduLength); |
887 | | #endif |
888 | |
|
889 | 0 | if ((pduLength < 6) || !Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, pduLength - 6)) |
890 | 0 | return ERROR_INVALID_DATA; |
891 | | |
892 | 0 | switch (eventId) |
893 | 0 | { |
894 | 0 | case EVENTID_SC_READY: |
895 | 0 | if ((error = rdpei_recv_sc_ready_pdu(callback, s))) |
896 | 0 | { |
897 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, |
898 | 0 | "rdpei_recv_sc_ready_pdu failed with error %" PRIu32 "!", error); |
899 | 0 | return error; |
900 | 0 | } |
901 | | |
902 | 0 | if ((error = rdpei_send_cs_ready_pdu(callback))) |
903 | 0 | { |
904 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, |
905 | 0 | "rdpei_send_cs_ready_pdu failed with error %" PRIu32 "!", error); |
906 | 0 | return error; |
907 | 0 | } |
908 | | |
909 | 0 | break; |
910 | | |
911 | 0 | case EVENTID_SUSPEND_TOUCH: |
912 | 0 | if ((error = rdpei_recv_suspend_touch_pdu(callback, s))) |
913 | 0 | { |
914 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, |
915 | 0 | "rdpei_recv_suspend_touch_pdu failed with error %" PRIu32 "!", error); |
916 | 0 | return error; |
917 | 0 | } |
918 | | |
919 | 0 | break; |
920 | | |
921 | 0 | case EVENTID_RESUME_TOUCH: |
922 | 0 | if ((error = rdpei_recv_resume_touch_pdu(callback, s))) |
923 | 0 | { |
924 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, |
925 | 0 | "rdpei_recv_resume_touch_pdu failed with error %" PRIu32 "!", error); |
926 | 0 | return error; |
927 | 0 | } |
928 | | |
929 | 0 | break; |
930 | | |
931 | 0 | default: |
932 | 0 | break; |
933 | 0 | } |
934 | | |
935 | 0 | return CHANNEL_RC_OK; |
936 | 0 | } |
937 | | |
938 | | /** |
939 | | * Function description |
940 | | * |
941 | | * @return 0 on success, otherwise a Win32 error code |
942 | | */ |
943 | | static UINT rdpei_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data) |
944 | 0 | { |
945 | 0 | GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback; |
946 | 0 | return rdpei_recv_pdu(callback, data); |
947 | 0 | } |
948 | | |
949 | | /** |
950 | | * Function description |
951 | | * |
952 | | * @return 0 on success, otherwise a Win32 error code |
953 | | */ |
954 | | static UINT rdpei_on_close(IWTSVirtualChannelCallback* pChannelCallback) |
955 | 0 | { |
956 | 0 | GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback; |
957 | 0 | if (callback) |
958 | 0 | { |
959 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin; |
960 | 0 | if (rdpei && rdpei->base.listener_callback) |
961 | 0 | { |
962 | 0 | if (rdpei->base.listener_callback->channel_callback == callback) |
963 | 0 | rdpei->base.listener_callback->channel_callback = nullptr; |
964 | 0 | } |
965 | 0 | } |
966 | 0 | free(callback); |
967 | 0 | return CHANNEL_RC_OK; |
968 | 0 | } |
969 | | |
970 | | /** |
971 | | * Channel Client Interface |
972 | | */ |
973 | | |
974 | | static UINT32 rdpei_get_version(RdpeiClientContext* context) |
975 | 0 | { |
976 | 0 | if (!context || !context->handle) |
977 | 0 | return 0; |
978 | | |
979 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle; |
980 | 0 | return rdpei->version; |
981 | 0 | } |
982 | | |
983 | | static UINT32 rdpei_get_features(RdpeiClientContext* context) |
984 | 0 | { |
985 | 0 | if (!context || !context->handle) |
986 | 0 | return 0; |
987 | | |
988 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle; |
989 | 0 | return rdpei->features; |
990 | 0 | } |
991 | | |
992 | | /** |
993 | | * Function description |
994 | | * |
995 | | * @return 0 on success, otherwise a Win32 error code |
996 | | */ |
997 | | UINT rdpei_send_frame(RdpeiClientContext* context, RDPINPUT_TOUCH_FRAME* frame) |
998 | 0 | { |
999 | 0 | UINT64 currentTime = GetTickCount64(); |
1000 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle; |
1001 | |
|
1002 | 0 | GENERIC_CHANNEL_CALLBACK* callback = rdpei->base.listener_callback->channel_callback; |
1003 | | |
1004 | | /* Just ignore the event if the channel is not connected */ |
1005 | 0 | if (!callback) |
1006 | 0 | return CHANNEL_RC_OK; |
1007 | | |
1008 | 0 | if (!rdpei->previousFrameTime && !rdpei->currentFrameTime) |
1009 | 0 | { |
1010 | 0 | rdpei->currentFrameTime = currentTime; |
1011 | 0 | frame->frameOffset = 0; |
1012 | 0 | } |
1013 | 0 | else |
1014 | 0 | { |
1015 | 0 | rdpei->currentFrameTime = currentTime; |
1016 | 0 | frame->frameOffset = rdpei->currentFrameTime - rdpei->previousFrameTime; |
1017 | 0 | } |
1018 | |
|
1019 | 0 | const UINT error = rdpei_send_touch_event_pdu(callback, frame); |
1020 | 0 | if (error) |
1021 | 0 | { |
1022 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, |
1023 | 0 | "rdpei_send_touch_event_pdu failed with error %" PRIu32 "!", error); |
1024 | 0 | return error; |
1025 | 0 | } |
1026 | | |
1027 | 0 | rdpei->previousFrameTime = rdpei->currentFrameTime; |
1028 | 0 | return error; |
1029 | 0 | } |
1030 | | |
1031 | | /** |
1032 | | * Function description |
1033 | | * |
1034 | | * @return 0 on success, otherwise a Win32 error code |
1035 | | */ |
1036 | | static UINT rdpei_add_contact(RdpeiClientContext* context, const RDPINPUT_CONTACT_DATA* contact) |
1037 | 0 | { |
1038 | 0 | UINT error = CHANNEL_RC_OK; |
1039 | 0 | if (!context || !contact || !context->handle) |
1040 | 0 | return ERROR_INTERNAL_ERROR; |
1041 | | |
1042 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle; |
1043 | |
|
1044 | 0 | EnterCriticalSection(&rdpei->lock); |
1045 | 0 | RDPINPUT_CONTACT_POINT* contactPoint = &rdpei->contactPoints[contact->contactId]; |
1046 | |
|
1047 | 0 | if (contactPoint->dirty && contactPoint->data.contactFlags != contact->contactFlags) |
1048 | 0 | { |
1049 | 0 | const INT32 externalId = contactPoint->externalId; |
1050 | 0 | error = rdpei_add_frame(context); |
1051 | 0 | if (!contactPoint->active) |
1052 | 0 | { |
1053 | 0 | contactPoint->active = TRUE; |
1054 | 0 | contactPoint->externalId = externalId; |
1055 | 0 | contactPoint->contactId = contact->contactId; |
1056 | 0 | } |
1057 | 0 | } |
1058 | |
|
1059 | 0 | contactPoint->data = *contact; |
1060 | 0 | contactPoint->dirty = TRUE; |
1061 | 0 | (void)SetEvent(rdpei->event); |
1062 | 0 | LeaveCriticalSection(&rdpei->lock); |
1063 | |
|
1064 | 0 | return error; |
1065 | 0 | } |
1066 | | |
1067 | | static UINT rdpei_touch_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags, |
1068 | | INT32 x, INT32 y, INT32* contactId, UINT32 fieldFlags, va_list ap) |
1069 | 0 | { |
1070 | 0 | INT64 contactIdlocal = -1; |
1071 | 0 | UINT error = CHANNEL_RC_OK; |
1072 | |
|
1073 | 0 | if (!context || !contactId || !context->handle) |
1074 | 0 | return ERROR_INTERNAL_ERROR; |
1075 | | |
1076 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle; |
1077 | | /* Create a new contact point in an empty slot */ |
1078 | 0 | EnterCriticalSection(&rdpei->lock); |
1079 | 0 | const BOOL begin = (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN) != 0; |
1080 | 0 | RDPINPUT_CONTACT_POINT* contactPoint = rdpei_contact(rdpei, externalId, !begin); |
1081 | 0 | if (contactPoint) |
1082 | 0 | contactIdlocal = contactPoint->contactId; |
1083 | |
|
1084 | 0 | if (contactIdlocal > UINT32_MAX) |
1085 | 0 | { |
1086 | 0 | error = ERROR_INVALID_PARAMETER; |
1087 | 0 | goto fail; |
1088 | 0 | } |
1089 | | |
1090 | 0 | if (contactIdlocal >= 0) |
1091 | 0 | { |
1092 | 0 | RDPINPUT_CONTACT_DATA contact = WINPR_C_ARRAY_INIT; |
1093 | 0 | contact.x = x; |
1094 | 0 | contact.y = y; |
1095 | 0 | contact.contactId = (UINT32)contactIdlocal; |
1096 | 0 | contact.contactFlags = contactFlags; |
1097 | 0 | contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags); |
1098 | |
|
1099 | 0 | if (fieldFlags & CONTACT_DATA_CONTACTRECT_PRESENT) |
1100 | 0 | { |
1101 | 0 | INT32 val = va_arg(ap, INT32); |
1102 | 0 | contact.contactRectLeft = WINPR_ASSERTING_INT_CAST(INT16, val); |
1103 | |
|
1104 | 0 | val = va_arg(ap, INT32); |
1105 | 0 | contact.contactRectTop = WINPR_ASSERTING_INT_CAST(INT16, val); |
1106 | |
|
1107 | 0 | val = va_arg(ap, INT32); |
1108 | 0 | contact.contactRectRight = WINPR_ASSERTING_INT_CAST(INT16, val); |
1109 | |
|
1110 | 0 | val = va_arg(ap, INT32); |
1111 | 0 | contact.contactRectBottom = WINPR_ASSERTING_INT_CAST(INT16, val); |
1112 | 0 | } |
1113 | 0 | if (fieldFlags & CONTACT_DATA_ORIENTATION_PRESENT) |
1114 | 0 | { |
1115 | 0 | UINT32 p = va_arg(ap, UINT32); |
1116 | 0 | if (p >= 360) |
1117 | 0 | { |
1118 | 0 | WLog_Print(rdpei->base.log, WLOG_WARN, |
1119 | 0 | "TouchContact %" PRId64 ": Invalid orientation value %" PRIu32 |
1120 | 0 | "degree, clamping to 359 degree", |
1121 | 0 | contactIdlocal, p); |
1122 | 0 | p = 359; |
1123 | 0 | } |
1124 | 0 | contact.orientation = p; |
1125 | 0 | } |
1126 | 0 | if (fieldFlags & CONTACT_DATA_PRESSURE_PRESENT) |
1127 | 0 | { |
1128 | 0 | UINT32 p = va_arg(ap, UINT32); |
1129 | 0 | if (p > 1024) |
1130 | 0 | { |
1131 | 0 | WLog_Print(rdpei->base.log, WLOG_WARN, |
1132 | 0 | "TouchContact %" PRId64 ": Invalid pressure value %" PRIu32 |
1133 | 0 | ", clamping to 1024", |
1134 | 0 | contactIdlocal, p); |
1135 | 0 | p = 1024; |
1136 | 0 | } |
1137 | 0 | contact.pressure = p; |
1138 | 0 | } |
1139 | |
|
1140 | 0 | error = context->AddContact(context, &contact); |
1141 | 0 | } |
1142 | |
|
1143 | 0 | fail: |
1144 | 0 | if (contactId) |
1145 | 0 | *contactId = (INT32)contactIdlocal; |
1146 | |
|
1147 | 0 | LeaveCriticalSection(&rdpei->lock); |
1148 | 0 | return error; |
1149 | 0 | } |
1150 | | |
1151 | | /** |
1152 | | * Function description |
1153 | | * |
1154 | | * @return 0 on success, otherwise a Win32 error code |
1155 | | */ |
1156 | | static UINT rdpei_touch_begin(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y, |
1157 | | INT32* contactId) |
1158 | 0 | { |
1159 | 0 | UINT rc = 0; |
1160 | 0 | va_list ap = WINPR_C_ARRAY_INIT; |
1161 | 0 | rc = rdpei_touch_process(context, externalId, |
1162 | 0 | RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE | |
1163 | 0 | RDPINPUT_CONTACT_FLAG_INCONTACT, |
1164 | 0 | x, y, contactId, 0, ap); |
1165 | 0 | return rc; |
1166 | 0 | } |
1167 | | |
1168 | | /** |
1169 | | * Function description |
1170 | | * |
1171 | | * @return 0 on success, otherwise a Win32 error code |
1172 | | */ |
1173 | | static UINT rdpei_touch_update(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y, |
1174 | | INT32* contactId) |
1175 | 0 | { |
1176 | 0 | UINT rc = 0; |
1177 | 0 | va_list ap = WINPR_C_ARRAY_INIT; |
1178 | 0 | rc = rdpei_touch_process(context, externalId, |
1179 | 0 | RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE | |
1180 | 0 | RDPINPUT_CONTACT_FLAG_INCONTACT, |
1181 | 0 | x, y, contactId, 0, ap); |
1182 | 0 | return rc; |
1183 | 0 | } |
1184 | | |
1185 | | /** |
1186 | | * Function description |
1187 | | * |
1188 | | * @return 0 on success, otherwise a Win32 error code |
1189 | | */ |
1190 | | static UINT rdpei_touch_end(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y, |
1191 | | INT32* contactId) |
1192 | 0 | { |
1193 | 0 | UINT error = 0; |
1194 | 0 | va_list ap = WINPR_C_ARRAY_INIT; |
1195 | 0 | error = rdpei_touch_process(context, externalId, |
1196 | 0 | RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE | |
1197 | 0 | RDPINPUT_CONTACT_FLAG_INCONTACT, |
1198 | 0 | x, y, contactId, 0, ap); |
1199 | 0 | if (error != CHANNEL_RC_OK) |
1200 | 0 | return error; |
1201 | 0 | error = |
1202 | 0 | rdpei_touch_process(context, externalId, RDPINPUT_CONTACT_FLAG_UP, x, y, contactId, 0, ap); |
1203 | 0 | return error; |
1204 | 0 | } |
1205 | | |
1206 | | /** |
1207 | | * Function description |
1208 | | * |
1209 | | * @return 0 on success, otherwise a Win32 error code |
1210 | | */ |
1211 | | static UINT rdpei_touch_cancel(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y, |
1212 | | INT32* contactId) |
1213 | 0 | { |
1214 | 0 | UINT rc = 0; |
1215 | 0 | va_list ap = WINPR_C_ARRAY_INIT; |
1216 | 0 | rc = rdpei_touch_process(context, externalId, |
1217 | 0 | RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_CANCELED, x, y, |
1218 | 0 | contactId, 0, ap); |
1219 | 0 | return rc; |
1220 | 0 | } |
1221 | | |
1222 | | static UINT rdpei_touch_raw_event(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y, |
1223 | | INT32* contactId, UINT32 flags, UINT32 fieldFlags, ...) |
1224 | 0 | { |
1225 | 0 | UINT rc = 0; |
1226 | 0 | va_list ap = WINPR_C_ARRAY_INIT; |
1227 | 0 | va_start(ap, fieldFlags); |
1228 | 0 | rc = rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, ap); |
1229 | 0 | va_end(ap); |
1230 | 0 | return rc; |
1231 | 0 | } |
1232 | | |
1233 | | static UINT rdpei_touch_raw_event_va(RdpeiClientContext* context, INT32 externalId, INT32 x, |
1234 | | INT32 y, INT32* contactId, UINT32 flags, UINT32 fieldFlags, |
1235 | | va_list args) |
1236 | 0 | { |
1237 | 0 | return rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, args); |
1238 | 0 | } |
1239 | | |
1240 | | static RDPINPUT_PEN_CONTACT_POINT* rdpei_pen_contact(RDPEI_PLUGIN* rdpei, INT32 externalId, |
1241 | | BOOL active) |
1242 | 0 | { |
1243 | 0 | if (!rdpei) |
1244 | 0 | return nullptr; |
1245 | | |
1246 | 0 | for (UINT32 x = 0; x < rdpei->maxPenContacts; x++) |
1247 | 0 | { |
1248 | 0 | RDPINPUT_PEN_CONTACT_POINT* contact = &rdpei->penContactPoints[x]; |
1249 | 0 | if (active) |
1250 | 0 | { |
1251 | 0 | if (contact->active) |
1252 | 0 | { |
1253 | 0 | if (contact->externalId == externalId) |
1254 | 0 | return contact; |
1255 | 0 | } |
1256 | 0 | } |
1257 | 0 | else |
1258 | 0 | { |
1259 | 0 | if (!contact->active) |
1260 | 0 | { |
1261 | 0 | contact->externalId = externalId; |
1262 | 0 | contact->active = TRUE; |
1263 | 0 | return contact; |
1264 | 0 | } |
1265 | 0 | } |
1266 | 0 | } |
1267 | 0 | return nullptr; |
1268 | 0 | } |
1269 | | |
1270 | | static UINT rdpei_add_pen(RdpeiClientContext* context, INT32 externalId, |
1271 | | const RDPINPUT_PEN_CONTACT* contact) |
1272 | 0 | { |
1273 | 0 | if (!context || !contact || !context->handle) |
1274 | 0 | return ERROR_INTERNAL_ERROR; |
1275 | | |
1276 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle; |
1277 | |
|
1278 | 0 | EnterCriticalSection(&rdpei->lock); |
1279 | |
|
1280 | 0 | RDPINPUT_PEN_CONTACT_POINT* contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE); |
1281 | 0 | if (contactPoint) |
1282 | 0 | { |
1283 | 0 | contactPoint->data = *contact; |
1284 | 0 | contactPoint->dirty = TRUE; |
1285 | 0 | (void)SetEvent(rdpei->event); |
1286 | 0 | } |
1287 | |
|
1288 | 0 | LeaveCriticalSection(&rdpei->lock); |
1289 | |
|
1290 | 0 | return CHANNEL_RC_OK; |
1291 | 0 | } |
1292 | | |
1293 | | static UINT rdpei_pen_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags, |
1294 | | UINT32 fieldFlags, INT32 x, INT32 y, va_list ap) |
1295 | 0 | { |
1296 | 0 | RDPINPUT_PEN_CONTACT_POINT* contactPoint = nullptr; |
1297 | 0 | UINT error = CHANNEL_RC_OK; |
1298 | |
|
1299 | 0 | if (!context || !context->handle) |
1300 | 0 | return ERROR_INTERNAL_ERROR; |
1301 | | |
1302 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle; |
1303 | |
|
1304 | 0 | EnterCriticalSection(&rdpei->lock); |
1305 | | // Start a new contact only when it is not active. |
1306 | 0 | contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE); |
1307 | 0 | if (!contactPoint) |
1308 | 0 | { |
1309 | 0 | const UINT32 mask = RDPINPUT_CONTACT_FLAG_INRANGE; |
1310 | 0 | if ((contactFlags & mask) == mask) |
1311 | 0 | { |
1312 | 0 | contactPoint = rdpei_pen_contact(rdpei, externalId, FALSE); |
1313 | 0 | } |
1314 | 0 | } |
1315 | |
|
1316 | 0 | if (contactPoint != nullptr) |
1317 | 0 | { |
1318 | 0 | RDPINPUT_PEN_CONTACT contact = WINPR_C_ARRAY_INIT; |
1319 | |
|
1320 | 0 | contact.x = x; |
1321 | 0 | contact.y = y; |
1322 | 0 | contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags); |
1323 | |
|
1324 | 0 | contact.contactFlags = contactFlags; |
1325 | 0 | if (fieldFlags & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT) |
1326 | 0 | { |
1327 | 0 | const UINT32 val = va_arg(ap, UINT32); |
1328 | 0 | contact.penFlags = WINPR_ASSERTING_INT_CAST(UINT16, val); |
1329 | 0 | } |
1330 | 0 | if (fieldFlags & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT) |
1331 | 0 | { |
1332 | 0 | const UINT32 val = va_arg(ap, UINT32); |
1333 | 0 | contact.pressure = WINPR_ASSERTING_INT_CAST(UINT16, val); |
1334 | 0 | } |
1335 | 0 | if (fieldFlags & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT) |
1336 | 0 | { |
1337 | 0 | const UINT32 val = va_arg(ap, UINT32); |
1338 | 0 | contact.rotation = WINPR_ASSERTING_INT_CAST(UINT16, val); |
1339 | 0 | } |
1340 | 0 | if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTX_PRESENT) |
1341 | 0 | { |
1342 | 0 | const INT32 val = va_arg(ap, INT32); |
1343 | 0 | contact.tiltX = WINPR_ASSERTING_INT_CAST(INT16, val); |
1344 | 0 | } |
1345 | 0 | if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTY_PRESENT) |
1346 | 0 | { |
1347 | 0 | const INT32 val = va_arg(ap, INT32); |
1348 | 0 | WINPR_ASSERT((val >= INT16_MIN) && (val <= INT16_MAX)); |
1349 | 0 | contact.tiltY = WINPR_ASSERTING_INT_CAST(INT16, val); |
1350 | 0 | } |
1351 | |
|
1352 | 0 | error = context->AddPen(context, externalId, &contact); |
1353 | 0 | } |
1354 | |
|
1355 | 0 | LeaveCriticalSection(&rdpei->lock); |
1356 | |
|
1357 | 0 | return error; |
1358 | 0 | } |
1359 | | |
1360 | | /** |
1361 | | * Function description |
1362 | | * |
1363 | | * @return 0 on success, otherwise a Win32 error code |
1364 | | */ |
1365 | | static UINT rdpei_pen_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, |
1366 | | INT32 x, INT32 y, ...) |
1367 | 0 | { |
1368 | 0 | UINT error = 0; |
1369 | 0 | va_list ap = WINPR_C_ARRAY_INIT; |
1370 | |
|
1371 | 0 | va_start(ap, y); |
1372 | 0 | error = rdpei_pen_process(context, externalId, |
1373 | 0 | RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE | |
1374 | 0 | RDPINPUT_CONTACT_FLAG_INCONTACT, |
1375 | 0 | fieldFlags, x, y, ap); |
1376 | 0 | va_end(ap); |
1377 | |
|
1378 | 0 | return error; |
1379 | 0 | } |
1380 | | |
1381 | | /** |
1382 | | * Function description |
1383 | | * |
1384 | | * @return 0 on success, otherwise a Win32 error code |
1385 | | */ |
1386 | | static UINT rdpei_pen_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, |
1387 | | INT32 x, INT32 y, ...) |
1388 | 0 | { |
1389 | 0 | UINT error = 0; |
1390 | 0 | va_list ap = WINPR_C_ARRAY_INIT; |
1391 | |
|
1392 | 0 | va_start(ap, y); |
1393 | 0 | error = rdpei_pen_process(context, externalId, |
1394 | 0 | RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE | |
1395 | 0 | RDPINPUT_CONTACT_FLAG_INCONTACT, |
1396 | 0 | fieldFlags, x, y, ap); |
1397 | 0 | va_end(ap); |
1398 | 0 | return error; |
1399 | 0 | } |
1400 | | |
1401 | | /** |
1402 | | * Function description |
1403 | | * |
1404 | | * @return 0 on success, otherwise a Win32 error code |
1405 | | */ |
1406 | | static UINT rdpei_pen_end(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, INT32 x, |
1407 | | INT32 y, ...) |
1408 | 0 | { |
1409 | 0 | UINT error = 0; |
1410 | 0 | va_list ap = WINPR_C_ARRAY_INIT; |
1411 | 0 | va_start(ap, y); |
1412 | 0 | error = rdpei_pen_process(context, externalId, |
1413 | 0 | RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_INRANGE, fieldFlags, |
1414 | 0 | x, y, ap); |
1415 | 0 | va_end(ap); |
1416 | 0 | return error; |
1417 | 0 | } |
1418 | | |
1419 | | /** |
1420 | | * Function description |
1421 | | * |
1422 | | * @return 0 on success, otherwise a Win32 error code |
1423 | | */ |
1424 | | static UINT rdpei_pen_hover_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, |
1425 | | INT32 x, INT32 y, ...) |
1426 | 0 | { |
1427 | 0 | UINT error = 0; |
1428 | 0 | va_list ap = WINPR_C_ARRAY_INIT; |
1429 | |
|
1430 | 0 | va_start(ap, y); |
1431 | 0 | error = rdpei_pen_process(context, externalId, |
1432 | 0 | RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE, |
1433 | 0 | fieldFlags, x, y, ap); |
1434 | 0 | va_end(ap); |
1435 | |
|
1436 | 0 | return error; |
1437 | 0 | } |
1438 | | |
1439 | | /** |
1440 | | * Function description |
1441 | | * |
1442 | | * @return 0 on success, otherwise a Win32 error code |
1443 | | */ |
1444 | | static UINT rdpei_pen_hover_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, |
1445 | | INT32 x, INT32 y, ...) |
1446 | 0 | { |
1447 | 0 | UINT error = 0; |
1448 | 0 | va_list ap = WINPR_C_ARRAY_INIT; |
1449 | |
|
1450 | 0 | va_start(ap, y); |
1451 | 0 | error = rdpei_pen_process(context, externalId, |
1452 | 0 | RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE, |
1453 | 0 | fieldFlags, x, y, ap); |
1454 | 0 | va_end(ap); |
1455 | |
|
1456 | 0 | return error; |
1457 | 0 | } |
1458 | | |
1459 | | /** |
1460 | | * Function description |
1461 | | * |
1462 | | * @return 0 on success, otherwise a Win32 error code |
1463 | | */ |
1464 | | static UINT rdpei_pen_hover_cancel(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, |
1465 | | INT32 x, INT32 y, ...) |
1466 | 0 | { |
1467 | 0 | UINT error = 0; |
1468 | 0 | va_list ap = WINPR_C_ARRAY_INIT; |
1469 | |
|
1470 | 0 | va_start(ap, y); |
1471 | 0 | error = rdpei_pen_process(context, externalId, |
1472 | 0 | RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_CANCELED, |
1473 | 0 | fieldFlags, x, y, ap); |
1474 | 0 | va_end(ap); |
1475 | |
|
1476 | 0 | return error; |
1477 | 0 | } |
1478 | | |
1479 | | static UINT rdpei_pen_raw_event(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags, |
1480 | | UINT32 fieldFlags, INT32 x, INT32 y, ...) |
1481 | 0 | { |
1482 | 0 | UINT error = 0; |
1483 | 0 | va_list ap = WINPR_C_ARRAY_INIT; |
1484 | |
|
1485 | 0 | va_start(ap, y); |
1486 | 0 | error = rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, ap); |
1487 | 0 | va_end(ap); |
1488 | 0 | return error; |
1489 | 0 | } |
1490 | | |
1491 | | static UINT rdpei_pen_raw_event_va(RdpeiClientContext* context, INT32 externalId, |
1492 | | UINT32 contactFlags, UINT32 fieldFlags, INT32 x, INT32 y, |
1493 | | va_list args) |
1494 | 0 | { |
1495 | 0 | return rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, args); |
1496 | 0 | } |
1497 | | |
1498 | | static UINT init_plugin_cb(GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext, rdpSettings* settings) |
1499 | 0 | { |
1500 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base; |
1501 | |
|
1502 | 0 | WINPR_ASSERT(base); |
1503 | 0 | WINPR_UNUSED(settings); |
1504 | |
|
1505 | 0 | rdpei->version = RDPINPUT_PROTOCOL_V300; |
1506 | 0 | rdpei->currentFrameTime = 0; |
1507 | 0 | rdpei->previousFrameTime = 0; |
1508 | 0 | rdpei->maxTouchContacts = MAX_CONTACTS; |
1509 | 0 | rdpei->maxPenContacts = MAX_PEN_CONTACTS; |
1510 | 0 | rdpei->rdpcontext = rcontext; |
1511 | |
|
1512 | 0 | WINPR_ASSERT(rdpei->base.log); |
1513 | |
|
1514 | 0 | InitializeCriticalSection(&rdpei->lock); |
1515 | 0 | rdpei->event = CreateEventA(nullptr, TRUE, FALSE, nullptr); |
1516 | 0 | if (!rdpei->event) |
1517 | 0 | { |
1518 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, "calloc failed!"); |
1519 | 0 | return CHANNEL_RC_NO_MEMORY; |
1520 | 0 | } |
1521 | | |
1522 | 0 | RdpeiClientContext* context = (RdpeiClientContext*)calloc(1, sizeof(RdpeiClientContext)); |
1523 | 0 | if (!context) |
1524 | 0 | { |
1525 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, "calloc failed!"); |
1526 | 0 | return CHANNEL_RC_NO_MEMORY; |
1527 | 0 | } |
1528 | | |
1529 | 0 | context->clientFeaturesMask = UINT32_MAX; |
1530 | 0 | context->handle = (void*)rdpei; |
1531 | 0 | context->GetVersion = rdpei_get_version; |
1532 | 0 | context->GetFeatures = rdpei_get_features; |
1533 | 0 | context->AddContact = rdpei_add_contact; |
1534 | 0 | context->TouchBegin = rdpei_touch_begin; |
1535 | 0 | context->TouchUpdate = rdpei_touch_update; |
1536 | 0 | context->TouchEnd = rdpei_touch_end; |
1537 | 0 | context->TouchCancel = rdpei_touch_cancel; |
1538 | 0 | context->TouchRawEvent = rdpei_touch_raw_event; |
1539 | 0 | context->TouchRawEventVA = rdpei_touch_raw_event_va; |
1540 | 0 | context->AddPen = rdpei_add_pen; |
1541 | 0 | context->PenBegin = rdpei_pen_begin; |
1542 | 0 | context->PenUpdate = rdpei_pen_update; |
1543 | 0 | context->PenEnd = rdpei_pen_end; |
1544 | 0 | context->PenHoverBegin = rdpei_pen_hover_begin; |
1545 | 0 | context->PenHoverUpdate = rdpei_pen_hover_update; |
1546 | 0 | context->PenHoverCancel = rdpei_pen_hover_cancel; |
1547 | 0 | context->PenRawEvent = rdpei_pen_raw_event; |
1548 | 0 | context->PenRawEventVA = rdpei_pen_raw_event_va; |
1549 | |
|
1550 | 0 | rdpei->context = context; |
1551 | 0 | rdpei->base.iface.pInterface = (void*)context; |
1552 | |
|
1553 | 0 | rdpei->async = |
1554 | 0 | !freerdp_settings_get_bool(rdpei->rdpcontext->settings, FreeRDP_SynchronousDynamicChannels); |
1555 | 0 | if (rdpei->async) |
1556 | 0 | { |
1557 | 0 | rdpei->running = TRUE; |
1558 | |
|
1559 | 0 | rdpei->thread = CreateThread(nullptr, 0, rdpei_periodic_update, rdpei, 0, nullptr); |
1560 | 0 | if (!rdpei->thread) |
1561 | 0 | { |
1562 | 0 | WLog_Print(rdpei->base.log, WLOG_ERROR, "calloc failed!"); |
1563 | 0 | return CHANNEL_RC_NO_MEMORY; |
1564 | 0 | } |
1565 | 0 | } |
1566 | 0 | else |
1567 | 0 | { |
1568 | 0 | if (!freerdp_client_channel_register(rdpei->rdpcontext->channels, rdpei->event, |
1569 | 0 | rdpei_poll_run, rdpei)) |
1570 | 0 | return ERROR_INTERNAL_ERROR; |
1571 | 0 | } |
1572 | | |
1573 | 0 | return CHANNEL_RC_OK; |
1574 | 0 | } |
1575 | | |
1576 | | static void terminate_plugin_cb(GENERIC_DYNVC_PLUGIN* base) |
1577 | 0 | { |
1578 | 0 | RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base; |
1579 | 0 | WINPR_ASSERT(rdpei); |
1580 | |
|
1581 | 0 | rdpei->running = FALSE; |
1582 | 0 | if (rdpei->event) |
1583 | 0 | (void)SetEvent(rdpei->event); |
1584 | |
|
1585 | 0 | if (rdpei->thread) |
1586 | 0 | { |
1587 | 0 | (void)WaitForSingleObject(rdpei->thread, INFINITE); |
1588 | 0 | (void)CloseHandle(rdpei->thread); |
1589 | 0 | } |
1590 | |
|
1591 | 0 | if (rdpei->event && !rdpei->async) |
1592 | 0 | freerdp_client_channel_unregister(rdpei->rdpcontext->channels, rdpei->event); |
1593 | |
|
1594 | 0 | if (rdpei->event) |
1595 | 0 | (void)CloseHandle(rdpei->event); |
1596 | |
|
1597 | 0 | DeleteCriticalSection(&rdpei->lock); |
1598 | 0 | free(rdpei->context); |
1599 | 0 | } |
1600 | | |
1601 | | static const IWTSVirtualChannelCallback rdpei_callbacks = { rdpei_on_data_received, |
1602 | | nullptr, /* Open */ |
1603 | | rdpei_on_close, nullptr }; |
1604 | | |
1605 | | /** |
1606 | | * Function description |
1607 | | * |
1608 | | * @return 0 on success, otherwise a Win32 error code |
1609 | | */ |
1610 | | FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpei_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints)) |
1611 | 0 | { |
1612 | 0 | return freerdp_generic_DVCPluginEntry(pEntryPoints, RDPEI_TAG, RDPEI_DVC_CHANNEL_NAME, |
1613 | 0 | sizeof(RDPEI_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK), |
1614 | 0 | &rdpei_callbacks, init_plugin_cb, terminate_plugin_cb); |
1615 | 0 | } |