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 1000)) /* frameOffset (EIGHT_BYTE_UNSIGNED_INTEGER) */
630 return ERROR_OUTOFMEMORY;
631
632 if (!Stream_EnsureRemainingCapacity(s, (size_t)frame->contactCount * 64))
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_SafeSeek(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 if (rdpei->base.listener_callback->channel_callback == callback)
963 rdpei->base.listener_callback->channel_callback = nullptr;
964 }
965 }
966 free(callback);
967 return CHANNEL_RC_OK;
968}
969
974static UINT32 rdpei_get_version(RdpeiClientContext* context)
975{
976 if (!context || !context->handle)
977 return 0;
978
979 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
980 return rdpei->version;
981}
982
983static UINT32 rdpei_get_features(RdpeiClientContext* context)
984{
985 if (!context || !context->handle)
986 return 0;
987
988 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
989 return rdpei->features;
990}
991
997UINT rdpei_send_frame(RdpeiClientContext* context, RDPINPUT_TOUCH_FRAME* frame)
998{
999 UINT64 currentTime = GetTickCount64();
1000 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1001
1002 GENERIC_CHANNEL_CALLBACK* callback = rdpei->base.listener_callback->channel_callback;
1003
1004 /* Just ignore the event if the channel is not connected */
1005 if (!callback)
1006 return CHANNEL_RC_OK;
1007
1008 if (!rdpei->previousFrameTime && !rdpei->currentFrameTime)
1009 {
1010 rdpei->currentFrameTime = currentTime;
1011 frame->frameOffset = 0;
1012 }
1013 else
1014 {
1015 rdpei->currentFrameTime = currentTime;
1016 frame->frameOffset = rdpei->currentFrameTime - rdpei->previousFrameTime;
1017 }
1018
1019 const UINT error = rdpei_send_touch_event_pdu(callback, frame);
1020 if (error)
1021 {
1022 WLog_Print(rdpei->base.log, WLOG_ERROR,
1023 "rdpei_send_touch_event_pdu failed with error %" PRIu32 "!", error);
1024 return error;
1025 }
1026
1027 rdpei->previousFrameTime = rdpei->currentFrameTime;
1028 return error;
1029}
1030
1036static UINT rdpei_add_contact(RdpeiClientContext* context, const RDPINPUT_CONTACT_DATA* contact)
1037{
1038 UINT error = CHANNEL_RC_OK;
1039 if (!context || !contact || !context->handle)
1040 return ERROR_INTERNAL_ERROR;
1041
1042 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1043
1044 EnterCriticalSection(&rdpei->lock);
1045 RDPINPUT_CONTACT_POINT* contactPoint = &rdpei->contactPoints[contact->contactId];
1046
1047 if (contactPoint->dirty && contactPoint->data.contactFlags != contact->contactFlags)
1048 {
1049 const INT32 externalId = contactPoint->externalId;
1050 error = rdpei_add_frame(context);
1051 if (!contactPoint->active)
1052 {
1053 contactPoint->active = TRUE;
1054 contactPoint->externalId = externalId;
1055 contactPoint->contactId = contact->contactId;
1056 }
1057 }
1058
1059 contactPoint->data = *contact;
1060 contactPoint->dirty = TRUE;
1061 (void)SetEvent(rdpei->event);
1062 LeaveCriticalSection(&rdpei->lock);
1063
1064 return error;
1065}
1066
1067static UINT rdpei_touch_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1068 INT32 x, INT32 y, INT32* contactId, UINT32 fieldFlags, va_list ap)
1069{
1070 INT64 contactIdlocal = -1;
1071 UINT error = CHANNEL_RC_OK;
1072
1073 if (!context || !contactId || !context->handle)
1074 return ERROR_INTERNAL_ERROR;
1075
1076 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1077 /* Create a new contact point in an empty slot */
1078 EnterCriticalSection(&rdpei->lock);
1079 const BOOL begin = (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN) != 0;
1080 RDPINPUT_CONTACT_POINT* contactPoint = rdpei_contact(rdpei, externalId, !begin);
1081 if (contactPoint)
1082 contactIdlocal = contactPoint->contactId;
1083
1084 if (contactIdlocal > UINT32_MAX)
1085 {
1086 error = ERROR_INVALID_PARAMETER;
1087 goto fail;
1088 }
1089
1090 if (contactIdlocal >= 0)
1091 {
1092 RDPINPUT_CONTACT_DATA contact = WINPR_C_ARRAY_INIT;
1093 contact.x = x;
1094 contact.y = y;
1095 contact.contactId = (UINT32)contactIdlocal;
1096 contact.contactFlags = contactFlags;
1097 contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1098
1099 if (fieldFlags & CONTACT_DATA_CONTACTRECT_PRESENT)
1100 {
1101 INT32 val = va_arg(ap, INT32);
1102 contact.contactRectLeft = WINPR_ASSERTING_INT_CAST(INT16, val);
1103
1104 val = va_arg(ap, INT32);
1105 contact.contactRectTop = WINPR_ASSERTING_INT_CAST(INT16, val);
1106
1107 val = va_arg(ap, INT32);
1108 contact.contactRectRight = WINPR_ASSERTING_INT_CAST(INT16, val);
1109
1110 val = va_arg(ap, INT32);
1111 contact.contactRectBottom = WINPR_ASSERTING_INT_CAST(INT16, val);
1112 }
1113 if (fieldFlags & CONTACT_DATA_ORIENTATION_PRESENT)
1114 {
1115 UINT32 p = va_arg(ap, UINT32);
1116 if (p >= 360)
1117 {
1118 WLog_Print(rdpei->base.log, WLOG_WARN,
1119 "TouchContact %" PRId64 ": Invalid orientation value %" PRIu32
1120 "degree, clamping to 359 degree",
1121 contactIdlocal, p);
1122 p = 359;
1123 }
1124 contact.orientation = p;
1125 }
1126 if (fieldFlags & CONTACT_DATA_PRESSURE_PRESENT)
1127 {
1128 UINT32 p = va_arg(ap, UINT32);
1129 if (p > 1024)
1130 {
1131 WLog_Print(rdpei->base.log, WLOG_WARN,
1132 "TouchContact %" PRId64 ": Invalid pressure value %" PRIu32
1133 ", clamping to 1024",
1134 contactIdlocal, p);
1135 p = 1024;
1136 }
1137 contact.pressure = p;
1138 }
1139
1140 error = context->AddContact(context, &contact);
1141 }
1142
1143fail:
1144 if (contactId)
1145 *contactId = (INT32)contactIdlocal;
1146
1147 LeaveCriticalSection(&rdpei->lock);
1148 return error;
1149}
1150
1156static UINT rdpei_touch_begin(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1157 INT32* contactId)
1158{
1159 UINT rc = 0;
1160 va_list ap = WINPR_C_ARRAY_INIT;
1161 rc = rdpei_touch_process(context, externalId,
1162 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1163 RDPINPUT_CONTACT_FLAG_INCONTACT,
1164 x, y, contactId, 0, ap);
1165 return rc;
1166}
1167
1173static UINT rdpei_touch_update(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1174 INT32* contactId)
1175{
1176 UINT rc = 0;
1177 va_list ap = WINPR_C_ARRAY_INIT;
1178 rc = rdpei_touch_process(context, externalId,
1179 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1180 RDPINPUT_CONTACT_FLAG_INCONTACT,
1181 x, y, contactId, 0, ap);
1182 return rc;
1183}
1184
1190static UINT rdpei_touch_end(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1191 INT32* contactId)
1192{
1193 UINT error = 0;
1194 va_list ap = WINPR_C_ARRAY_INIT;
1195 error = rdpei_touch_process(context, externalId,
1196 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1197 RDPINPUT_CONTACT_FLAG_INCONTACT,
1198 x, y, contactId, 0, ap);
1199 if (error != CHANNEL_RC_OK)
1200 return error;
1201 error =
1202 rdpei_touch_process(context, externalId, RDPINPUT_CONTACT_FLAG_UP, x, y, contactId, 0, ap);
1203 return error;
1204}
1205
1211static UINT rdpei_touch_cancel(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1212 INT32* contactId)
1213{
1214 UINT rc = 0;
1215 va_list ap = WINPR_C_ARRAY_INIT;
1216 rc = rdpei_touch_process(context, externalId,
1217 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_CANCELED, x, y,
1218 contactId, 0, ap);
1219 return rc;
1220}
1221
1222static UINT rdpei_touch_raw_event(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1223 INT32* contactId, UINT32 flags, UINT32 fieldFlags, ...)
1224{
1225 UINT rc = 0;
1226 va_list ap = WINPR_C_ARRAY_INIT;
1227 va_start(ap, fieldFlags);
1228 rc = rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, ap);
1229 va_end(ap);
1230 return rc;
1231}
1232
1233static 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{
1237 return rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, args);
1238}
1239
1240static RDPINPUT_PEN_CONTACT_POINT* rdpei_pen_contact(RDPEI_PLUGIN* rdpei, INT32 externalId,
1241 BOOL active)
1242{
1243 if (!rdpei)
1244 return nullptr;
1245
1246 for (UINT32 x = 0; x < rdpei->maxPenContacts; x++)
1247 {
1248 RDPINPUT_PEN_CONTACT_POINT* contact = &rdpei->penContactPoints[x];
1249 if (active)
1250 {
1251 if (contact->active)
1252 {
1253 if (contact->externalId == externalId)
1254 return contact;
1255 }
1256 }
1257 else
1258 {
1259 if (!contact->active)
1260 {
1261 contact->externalId = externalId;
1262 contact->active = TRUE;
1263 return contact;
1264 }
1265 }
1266 }
1267 return nullptr;
1268}
1269
1270static UINT rdpei_add_pen(RdpeiClientContext* context, INT32 externalId,
1271 const RDPINPUT_PEN_CONTACT* contact)
1272{
1273 if (!context || !contact || !context->handle)
1274 return ERROR_INTERNAL_ERROR;
1275
1276 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1277
1278 EnterCriticalSection(&rdpei->lock);
1279
1280 RDPINPUT_PEN_CONTACT_POINT* contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1281 if (contactPoint)
1282 {
1283 contactPoint->data = *contact;
1284 contactPoint->dirty = TRUE;
1285 (void)SetEvent(rdpei->event);
1286 }
1287
1288 LeaveCriticalSection(&rdpei->lock);
1289
1290 return CHANNEL_RC_OK;
1291}
1292
1293static UINT rdpei_pen_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1294 UINT32 fieldFlags, INT32 x, INT32 y, va_list ap)
1295{
1296 RDPINPUT_PEN_CONTACT_POINT* contactPoint = nullptr;
1297 UINT error = CHANNEL_RC_OK;
1298
1299 if (!context || !context->handle)
1300 return ERROR_INTERNAL_ERROR;
1301
1302 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1303
1304 EnterCriticalSection(&rdpei->lock);
1305 // Start a new contact only when it is not active.
1306 contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1307 if (!contactPoint)
1308 {
1309 const UINT32 mask = RDPINPUT_CONTACT_FLAG_INRANGE;
1310 if ((contactFlags & mask) == mask)
1311 {
1312 contactPoint = rdpei_pen_contact(rdpei, externalId, FALSE);
1313 }
1314 }
1315
1316 if (contactPoint != nullptr)
1317 {
1318 RDPINPUT_PEN_CONTACT contact = WINPR_C_ARRAY_INIT;
1319
1320 contact.x = x;
1321 contact.y = y;
1322 contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1323
1324 contact.contactFlags = contactFlags;
1325 if (fieldFlags & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
1326 {
1327 const UINT32 val = va_arg(ap, UINT32);
1328 contact.penFlags = WINPR_ASSERTING_INT_CAST(UINT16, val);
1329 }
1330 if (fieldFlags & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
1331 {
1332 const UINT32 val = va_arg(ap, UINT32);
1333 contact.pressure = WINPR_ASSERTING_INT_CAST(UINT16, val);
1334 }
1335 if (fieldFlags & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
1336 {
1337 const UINT32 val = va_arg(ap, UINT32);
1338 contact.rotation = WINPR_ASSERTING_INT_CAST(UINT16, val);
1339 }
1340 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
1341 {
1342 const INT32 val = va_arg(ap, INT32);
1343 contact.tiltX = WINPR_ASSERTING_INT_CAST(INT16, val);
1344 }
1345 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
1346 {
1347 const INT32 val = va_arg(ap, INT32);
1348 WINPR_ASSERT((val >= INT16_MIN) && (val <= INT16_MAX));
1349 contact.tiltY = WINPR_ASSERTING_INT_CAST(INT16, val);
1350 }
1351
1352 error = context->AddPen(context, externalId, &contact);
1353 }
1354
1355 LeaveCriticalSection(&rdpei->lock);
1356
1357 return error;
1358}
1359
1365static UINT rdpei_pen_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1366 INT32 x, INT32 y, ...)
1367{
1368 UINT error = 0;
1369 va_list ap = WINPR_C_ARRAY_INIT;
1370
1371 va_start(ap, y);
1372 error = rdpei_pen_process(context, externalId,
1373 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1374 RDPINPUT_CONTACT_FLAG_INCONTACT,
1375 fieldFlags, x, y, ap);
1376 va_end(ap);
1377
1378 return error;
1379}
1380
1386static UINT rdpei_pen_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1387 INT32 x, INT32 y, ...)
1388{
1389 UINT error = 0;
1390 va_list ap = WINPR_C_ARRAY_INIT;
1391
1392 va_start(ap, y);
1393 error = rdpei_pen_process(context, externalId,
1394 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1395 RDPINPUT_CONTACT_FLAG_INCONTACT,
1396 fieldFlags, x, y, ap);
1397 va_end(ap);
1398 return error;
1399}
1400
1406static UINT rdpei_pen_end(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, INT32 x,
1407 INT32 y, ...)
1408{
1409 UINT error = 0;
1410 va_list ap = WINPR_C_ARRAY_INIT;
1411 va_start(ap, y);
1412 error = rdpei_pen_process(context, externalId,
1413 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_INRANGE, fieldFlags,
1414 x, y, ap);
1415 va_end(ap);
1416 return error;
1417}
1418
1424static UINT rdpei_pen_hover_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1425 INT32 x, INT32 y, ...)
1426{
1427 UINT error = 0;
1428 va_list ap = WINPR_C_ARRAY_INIT;
1429
1430 va_start(ap, y);
1431 error = rdpei_pen_process(context, externalId,
1432 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1433 fieldFlags, x, y, ap);
1434 va_end(ap);
1435
1436 return error;
1437}
1438
1444static UINT rdpei_pen_hover_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1445 INT32 x, INT32 y, ...)
1446{
1447 UINT error = 0;
1448 va_list ap = WINPR_C_ARRAY_INIT;
1449
1450 va_start(ap, y);
1451 error = rdpei_pen_process(context, externalId,
1452 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1453 fieldFlags, x, y, ap);
1454 va_end(ap);
1455
1456 return error;
1457}
1458
1464static UINT rdpei_pen_hover_cancel(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1465 INT32 x, INT32 y, ...)
1466{
1467 UINT error = 0;
1468 va_list ap = WINPR_C_ARRAY_INIT;
1469
1470 va_start(ap, y);
1471 error = rdpei_pen_process(context, externalId,
1472 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_CANCELED,
1473 fieldFlags, x, y, ap);
1474 va_end(ap);
1475
1476 return error;
1477}
1478
1479static UINT rdpei_pen_raw_event(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1480 UINT32 fieldFlags, INT32 x, INT32 y, ...)
1481{
1482 UINT error = 0;
1483 va_list ap = WINPR_C_ARRAY_INIT;
1484
1485 va_start(ap, y);
1486 error = rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, ap);
1487 va_end(ap);
1488 return error;
1489}
1490
1491static UINT rdpei_pen_raw_event_va(RdpeiClientContext* context, INT32 externalId,
1492 UINT32 contactFlags, UINT32 fieldFlags, INT32 x, INT32 y,
1493 va_list args)
1494{
1495 return rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, args);
1496}
1497
1498static UINT init_plugin_cb(GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext, rdpSettings* settings)
1499{
1500 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1501
1502 WINPR_ASSERT(base);
1503 WINPR_UNUSED(settings);
1504
1505 rdpei->version = RDPINPUT_PROTOCOL_V300;
1506 rdpei->currentFrameTime = 0;
1507 rdpei->previousFrameTime = 0;
1508 rdpei->maxTouchContacts = MAX_CONTACTS;
1509 rdpei->maxPenContacts = MAX_PEN_CONTACTS;
1510 rdpei->rdpcontext = rcontext;
1511
1512 WINPR_ASSERT(rdpei->base.log);
1513
1514 InitializeCriticalSection(&rdpei->lock);
1515 rdpei->event = CreateEventA(nullptr, TRUE, FALSE, nullptr);
1516 if (!rdpei->event)
1517 {
1518 WLog_Print(rdpei->base.log, WLOG_ERROR, "calloc failed!");
1519 return CHANNEL_RC_NO_MEMORY;
1520 }
1521
1522 RdpeiClientContext* context = (RdpeiClientContext*)calloc(1, sizeof(RdpeiClientContext));
1523 if (!context)
1524 {
1525 WLog_Print(rdpei->base.log, WLOG_ERROR, "calloc failed!");
1526 return CHANNEL_RC_NO_MEMORY;
1527 }
1528
1529 context->clientFeaturesMask = UINT32_MAX;
1530 context->handle = (void*)rdpei;
1531 context->GetVersion = rdpei_get_version;
1532 context->GetFeatures = rdpei_get_features;
1533 context->AddContact = rdpei_add_contact;
1534 context->TouchBegin = rdpei_touch_begin;
1535 context->TouchUpdate = rdpei_touch_update;
1536 context->TouchEnd = rdpei_touch_end;
1537 context->TouchCancel = rdpei_touch_cancel;
1538 context->TouchRawEvent = rdpei_touch_raw_event;
1539 context->TouchRawEventVA = rdpei_touch_raw_event_va;
1540 context->AddPen = rdpei_add_pen;
1541 context->PenBegin = rdpei_pen_begin;
1542 context->PenUpdate = rdpei_pen_update;
1543 context->PenEnd = rdpei_pen_end;
1544 context->PenHoverBegin = rdpei_pen_hover_begin;
1545 context->PenHoverUpdate = rdpei_pen_hover_update;
1546 context->PenHoverCancel = rdpei_pen_hover_cancel;
1547 context->PenRawEvent = rdpei_pen_raw_event;
1548 context->PenRawEventVA = rdpei_pen_raw_event_va;
1549
1550 rdpei->context = context;
1551 rdpei->base.iface.pInterface = (void*)context;
1552
1553 rdpei->async =
1554 !freerdp_settings_get_bool(rdpei->rdpcontext->settings, FreeRDP_SynchronousDynamicChannels);
1555 if (rdpei->async)
1556 {
1557 rdpei->running = TRUE;
1558
1559 rdpei->thread = CreateThread(nullptr, 0, rdpei_periodic_update, rdpei, 0, nullptr);
1560 if (!rdpei->thread)
1561 {
1562 WLog_Print(rdpei->base.log, WLOG_ERROR, "calloc failed!");
1563 return CHANNEL_RC_NO_MEMORY;
1564 }
1565 }
1566 else
1567 {
1568 if (!freerdp_client_channel_register(rdpei->rdpcontext->channels, rdpei->event,
1569 rdpei_poll_run, rdpei))
1570 return ERROR_INTERNAL_ERROR;
1571 }
1572
1573 return CHANNEL_RC_OK;
1574}
1575
1576static void terminate_plugin_cb(GENERIC_DYNVC_PLUGIN* base)
1577{
1578 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1579 WINPR_ASSERT(rdpei);
1580
1581 rdpei->running = FALSE;
1582 if (rdpei->event)
1583 (void)SetEvent(rdpei->event);
1584
1585 if (rdpei->thread)
1586 {
1587 (void)WaitForSingleObject(rdpei->thread, INFINITE);
1588 (void)CloseHandle(rdpei->thread);
1589 }
1590
1591 if (rdpei->event && !rdpei->async)
1592 freerdp_client_channel_unregister(rdpei->rdpcontext->channels, rdpei->event);
1593
1594 if (rdpei->event)
1595 (void)CloseHandle(rdpei->event);
1596
1597 DeleteCriticalSection(&rdpei->lock);
1598 free(rdpei->context);
1599}
1600
1601static const IWTSVirtualChannelCallback rdpei_callbacks = { rdpei_on_data_received,
1602 nullptr, /* Open */
1603 rdpei_on_close, nullptr };
1604
1610FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpei_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1611{
1612 return freerdp_generic_DVCPluginEntry(pEntryPoints, RDPEI_TAG, RDPEI_DVC_CHANNEL_NAME,
1613 sizeof(RDPEI_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
1614 &rdpei_callbacks, init_plugin_cb, terminate_plugin_cb);
1615}
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