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