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