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