FreeRDP
Loading...
Searching...
No Matches
client/rdpei_main.c
1
22#include <freerdp/config.h>
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdint.h>
28
29#include <winpr/crt.h>
30#include <winpr/cast.h>
31#include <winpr/synch.h>
32#include <winpr/thread.h>
33#include <winpr/stream.h>
34#include <winpr/sysinfo.h>
35#include <winpr/cmdline.h>
36#include <winpr/collections.h>
37
38#include <freerdp/addin.h>
39#include <freerdp/freerdp.h>
40#include <freerdp/client/channels.h>
41
42#include "rdpei_common.h"
43
44#include "rdpei_main.h"
45
46#define RDPEI_TAG CHANNELS_TAG("rdpei.client")
47
68#define MAX_CONTACTS 64
69#define MAX_PEN_CONTACTS 4
70
71typedef struct
72{
74
75 RdpeiClientContext* context;
76
77 UINT32 version;
78 UINT32 features; /* SC_READY_MULTIPEN_INJECTION_SUPPORTED */
79 UINT16 maxTouchContacts;
80 UINT64 currentFrameTime;
81 UINT64 previousFrameTime;
82 RDPINPUT_CONTACT_POINT contactPoints[MAX_CONTACTS];
83
84 UINT64 currentPenFrameTime;
85 UINT64 previousPenFrameTime;
86 UINT16 maxPenContacts;
87 RDPINPUT_PEN_CONTACT_POINT penContactPoints[MAX_PEN_CONTACTS];
88
90 rdpContext* rdpcontext;
91
92 HANDLE thread;
93
94 HANDLE event;
95 UINT64 lastPollEventTime;
96 BOOL running;
97 BOOL async;
98} RDPEI_PLUGIN;
99
105static UINT rdpei_send_frame(RdpeiClientContext* context, RDPINPUT_TOUCH_FRAME* frame);
106
107#ifdef WITH_DEBUG_RDPEI
108static const char* rdpei_eventid_string(UINT16 event)
109{
110 switch (event)
111 {
112 case EVENTID_SC_READY:
113 return "EVENTID_SC_READY";
114 case EVENTID_CS_READY:
115 return "EVENTID_CS_READY";
116 case EVENTID_TOUCH:
117 return "EVENTID_TOUCH";
118 case EVENTID_SUSPEND_TOUCH:
119 return "EVENTID_SUSPEND_TOUCH";
120 case EVENTID_RESUME_TOUCH:
121 return "EVENTID_RESUME_TOUCH";
122 case EVENTID_DISMISS_HOVERING_CONTACT:
123 return "EVENTID_DISMISS_HOVERING_CONTACT";
124 case EVENTID_PEN:
125 return "EVENTID_PEN";
126 default:
127 return "EVENTID_UNKNOWN";
128 }
129}
130#endif
131
132static RDPINPUT_CONTACT_POINT* rdpei_contact(RDPEI_PLUGIN* rdpei, INT32 externalId, BOOL active)
133{
134 for (UINT16 i = 0; i < rdpei->maxTouchContacts; i++)
135 {
136 RDPINPUT_CONTACT_POINT* contactPoint = &rdpei->contactPoints[i];
137
138 if (!contactPoint->active && active)
139 continue;
140 else if (!contactPoint->active && !active)
141 {
142 contactPoint->contactId = i;
143 contactPoint->externalId = externalId;
144 contactPoint->active = TRUE;
145 return contactPoint;
146 }
147 else if (contactPoint->externalId == externalId)
148 {
149 return contactPoint;
150 }
151 }
152 return NULL;
153}
154
160static UINT rdpei_add_frame(RdpeiClientContext* context)
161{
162 RDPINPUT_TOUCH_FRAME frame = { 0 };
163 RDPINPUT_CONTACT_DATA contacts[MAX_CONTACTS] = { 0 };
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), NULL);
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 WINPR_ASSERT(callback);
304
305 if (frameOffset > UINT32_MAX)
306 return ERROR_INVALID_PARAMETER;
307 if (count > UINT16_MAX)
308 return ERROR_INVALID_PARAMETER;
309
310 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
311 if (!rdpei)
312 return ERROR_INTERNAL_ERROR;
313
314 if (!frames || (count == 0))
315 return ERROR_INTERNAL_ERROR;
316
317 wStream* s = Stream_New(NULL, 64);
318
319 if (!s)
320 {
321 WLog_Print(rdpei->base.log, WLOG_ERROR, "Stream_New failed!");
322 return CHANNEL_RC_NO_MEMORY;
323 }
324
325 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
330 rdpei_write_4byte_unsigned(s,
331 (UINT32)frameOffset); /* encodeTime (FOUR_BYTE_UNSIGNED_INTEGER) */
332 rdpei_write_2byte_unsigned(s, (UINT16)count); /* (frameCount) TWO_BYTE_UNSIGNED_INTEGER */
333
334 for (size_t x = 0; x < count; x++)
335 {
336 const UINT status = rdpei_write_pen_frame(s, &frames[x]);
337 if (status)
338 {
339 WLog_Print(rdpei->base.log, WLOG_ERROR,
340 "rdpei_write_pen_frame failed with error %" PRIu32 "!", status);
341 Stream_Free(s, TRUE);
342 return status;
343 }
344 }
345 Stream_SealLength(s);
346
347 const UINT status = rdpei_send_pdu(callback, s, EVENTID_PEN, Stream_Length(s));
348 Stream_Free(s, TRUE);
349 return status;
350}
351
352static UINT rdpei_send_pen_frame(RdpeiClientContext* context, RDPINPUT_PEN_FRAME* frame)
353{
354 const UINT64 currentTime = GetTickCount64();
355
356 if (!context)
357 return ERROR_INTERNAL_ERROR;
358
359 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
360 if (!rdpei || !rdpei->base.listener_callback)
361 return ERROR_INTERNAL_ERROR;
362 if (!rdpei || !rdpei->rdpcontext)
363 return ERROR_INTERNAL_ERROR;
364 if (freerdp_settings_get_bool(rdpei->rdpcontext->settings, FreeRDP_SuspendInput))
365 return CHANNEL_RC_OK;
366
367 GENERIC_CHANNEL_CALLBACK* callback = rdpei->base.listener_callback->channel_callback;
368 /* Just ignore the event if the channel is not connected */
369 if (!callback)
370 return CHANNEL_RC_OK;
371
372 if (!rdpei->previousPenFrameTime && !rdpei->currentPenFrameTime)
373 {
374 rdpei->currentPenFrameTime = currentTime;
375 frame->frameOffset = 0;
376 }
377 else
378 {
379 rdpei->currentPenFrameTime = currentTime;
380 frame->frameOffset = rdpei->currentPenFrameTime - rdpei->previousPenFrameTime;
381 }
382
383 const size_t off = WINPR_ASSERTING_INT_CAST(size_t, frame->frameOffset);
384 const UINT error = rdpei_send_pen_event_pdu(callback, off, frame, 1);
385 if (error)
386 return error;
387
388 rdpei->previousPenFrameTime = rdpei->currentPenFrameTime;
389 return error;
390}
391
392static UINT rdpei_add_pen_frame(RdpeiClientContext* context)
393{
394 RDPINPUT_PEN_FRAME penFrame = { 0 };
395 RDPINPUT_PEN_CONTACT penContacts[MAX_PEN_CONTACTS] = { 0 };
396
397 if (!context || !context->handle)
398 return ERROR_INTERNAL_ERROR;
399
400 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
401
402 penFrame.contacts = penContacts;
403
404 for (UINT16 i = 0; i < rdpei->maxPenContacts; i++)
405 {
406 RDPINPUT_PEN_CONTACT_POINT* contact = &(rdpei->penContactPoints[i]);
407
408 if (contact->dirty)
409 {
410 penContacts[penFrame.contactCount++] = contact->data;
411 contact->dirty = FALSE;
412 }
413 else if (contact->active)
414 {
415 if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
416 {
417 contact->data.contactFlags = RDPINPUT_CONTACT_FLAG_UPDATE;
418 contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INRANGE;
419 contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INCONTACT;
420 }
421
422 penContacts[penFrame.contactCount++] = contact->data;
423 }
424 if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
425 {
426 contact->externalId = 0;
427 contact->active = FALSE;
428 }
429 }
430
431 if (penFrame.contactCount > 0)
432 return rdpei_send_pen_frame(context, &penFrame);
433 return CHANNEL_RC_OK;
434}
435
436static UINT rdpei_update(wLog* log, RdpeiClientContext* context)
437{
438 UINT error = rdpei_add_frame(context);
439 if (error != CHANNEL_RC_OK)
440 {
441 WLog_Print(log, WLOG_ERROR, "rdpei_add_frame failed with error %" PRIu32 "!", error);
442 return error;
443 }
444
445 return rdpei_add_pen_frame(context);
446}
447
448static BOOL rdpei_poll_run_unlocked(rdpContext* context, void* userdata)
449{
450 RDPEI_PLUGIN* rdpei = userdata;
451 WINPR_ASSERT(rdpei);
452 WINPR_ASSERT(context);
453
454 const UINT64 now = GetTickCount64();
455
456 /* Send an event every ~20ms */
457 if ((now < rdpei->lastPollEventTime) || (now - rdpei->lastPollEventTime < 20ULL))
458 return TRUE;
459
460 rdpei->lastPollEventTime = now;
461
462 const UINT error = rdpei_update(rdpei->base.log, rdpei->context);
463
464 (void)ResetEvent(rdpei->event);
465
466 if (error != CHANNEL_RC_OK)
467 {
468 WLog_Print(rdpei->base.log, WLOG_ERROR, "rdpei_add_frame failed with error %" PRIu32 "!",
469 error);
470 setChannelError(context, error, "rdpei_add_frame reported an error");
471 return FALSE;
472 }
473
474 return TRUE;
475}
476
477static BOOL rdpei_poll_run(rdpContext* context, void* userdata)
478{
479 RDPEI_PLUGIN* rdpei = userdata;
480 WINPR_ASSERT(rdpei);
481
482 EnterCriticalSection(&rdpei->lock);
483 BOOL rc = rdpei_poll_run_unlocked(context, userdata);
484 LeaveCriticalSection(&rdpei->lock);
485 return rc;
486}
487
488static DWORD WINAPI rdpei_periodic_update(LPVOID arg)
489{
490 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)arg;
491 UINT error = CHANNEL_RC_OK;
492
493 if (!rdpei)
494 {
495 error = ERROR_INVALID_PARAMETER;
496 goto out;
497 }
498
499 if (!rdpei->context)
500 {
501 error = ERROR_INVALID_PARAMETER;
502 goto out;
503 }
504
505 while (rdpei->running)
506 {
507 const DWORD status = WaitForSingleObject(rdpei->event, 20);
508
509 if (status == WAIT_FAILED)
510 {
511 error = GetLastError();
512 WLog_Print(rdpei->base.log, WLOG_ERROR,
513 "WaitForMultipleObjects failed with error %" PRIu32 "!", error);
514 break;
515 }
516
517 if (!rdpei_poll_run(rdpei->rdpcontext, rdpei))
518 error = ERROR_INTERNAL_ERROR;
519 }
520
521out:
522
523 if (error && rdpei && rdpei->rdpcontext)
524 setChannelError(rdpei->rdpcontext, error, "rdpei_schedule_thread reported an error");
525
526 if (rdpei)
527 rdpei->running = FALSE;
528
529 ExitThread(error);
530 return error;
531}
532
538static UINT rdpei_send_cs_ready_pdu(GENERIC_CHANNEL_CALLBACK* callback)
539{
540 if (!callback || !callback->plugin)
541 return ERROR_INTERNAL_ERROR;
542
543 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
544
545 UINT32 flags = CS_READY_FLAGS_SHOW_TOUCH_VISUALS & rdpei->context->clientFeaturesMask;
546 if (rdpei->version > RDPINPUT_PROTOCOL_V10)
547 flags |= CS_READY_FLAGS_DISABLE_TIMESTAMP_INJECTION & rdpei->context->clientFeaturesMask;
548 if (rdpei->features & SC_READY_MULTIPEN_INJECTION_SUPPORTED)
549 flags |= CS_READY_FLAGS_ENABLE_MULTIPEN_INJECTION & rdpei->context->clientFeaturesMask;
550
551 UINT32 pduLength = RDPINPUT_HEADER_LENGTH + 10;
552 wStream* s = Stream_New(NULL, pduLength);
553
554 if (!s)
555 {
556 WLog_Print(rdpei->base.log, WLOG_ERROR, "Stream_New failed!");
557 return CHANNEL_RC_NO_MEMORY;
558 }
559
560 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
561 Stream_Write_UINT32(s, flags); /* flags (4 bytes) */
562 Stream_Write_UINT32(s, rdpei->version); /* protocolVersion (4 bytes) */
563 Stream_Write_UINT16(s, rdpei->maxTouchContacts); /* maxTouchContacts (2 bytes) */
564 Stream_SealLength(s);
565
566 const UINT status = rdpei_send_pdu(callback, s, EVENTID_CS_READY, pduLength);
567 Stream_Free(s, TRUE);
568 return status;
569}
570
571#if defined(WITH_DEBUG_RDPEI)
572static void rdpei_print_contact_flags(wLog* log, UINT32 contactFlags)
573{
574 if (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
575 WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_DOWN");
576
577 if (contactFlags & RDPINPUT_CONTACT_FLAG_UPDATE)
578 WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_UPDATE");
579
580 if (contactFlags & RDPINPUT_CONTACT_FLAG_UP)
581 WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_UP");
582
583 if (contactFlags & RDPINPUT_CONTACT_FLAG_INRANGE)
584 WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_INRANGE");
585
586 if (contactFlags & RDPINPUT_CONTACT_FLAG_INCONTACT)
587 WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_INCONTACT");
588
589 if (contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
590 WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_CANCELED");
591}
592#endif
593
594static INT16 bounded(INT32 val)
595{
596 if (val < INT16_MIN)
597 return INT16_MIN;
598 if (val > INT16_MAX)
599 return INT16_MAX;
600 return (INT16)val;
601}
602
608static UINT rdpei_write_touch_frame(wLog* log, wStream* s, RDPINPUT_TOUCH_FRAME* frame)
609{
610 int rectSize = 2;
611 if (!s || !frame)
612 return ERROR_INTERNAL_ERROR;
613#ifdef WITH_DEBUG_RDPEI
614 WLog_Print(log, WLOG_DEBUG, "contactCount: %" PRIu32 "", frame->contactCount);
615 WLog_Print(log, WLOG_DEBUG, "frameOffset: 0x%016" PRIX64 "", frame->frameOffset);
616#endif
617 rdpei_write_2byte_unsigned(s,
618 frame->contactCount); /* contactCount (TWO_BYTE_UNSIGNED_INTEGER) */
623 rdpei_write_8byte_unsigned(s, frame->frameOffset *
624 1000); /* frameOffset (EIGHT_BYTE_UNSIGNED_INTEGER) */
625
626 if (!Stream_EnsureRemainingCapacity(s, (size_t)frame->contactCount * 64))
627 {
628 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
629 return CHANNEL_RC_NO_MEMORY;
630 }
631
632 for (UINT32 index = 0; index < frame->contactCount; index++)
633 {
634 RDPINPUT_CONTACT_DATA* contact = &frame->contacts[index];
635
636 contact->fieldsPresent |= CONTACT_DATA_CONTACTRECT_PRESENT;
637 contact->contactRectLeft = bounded(contact->x - rectSize);
638 contact->contactRectTop = bounded(contact->y - rectSize);
639 contact->contactRectRight = bounded(contact->x + rectSize);
640 contact->contactRectBottom = bounded(contact->y + rectSize);
641#ifdef WITH_DEBUG_RDPEI
642 WLog_Print(log, WLOG_DEBUG, "contact[%" PRIu32 "].contactId: %" PRIu32 "", index,
643 contact->contactId);
644 WLog_Print(log, WLOG_DEBUG, "contact[%" PRIu32 "].fieldsPresent: %" PRIu32 "", index,
645 contact->fieldsPresent);
646 WLog_Print(log, WLOG_DEBUG, "contact[%" PRIu32 "].x: %" PRId32 "", index, contact->x);
647 WLog_Print(log, WLOG_DEBUG, "contact[%" PRIu32 "].y: %" PRId32 "", index, contact->y);
648 WLog_Print(log, WLOG_DEBUG, "contact[%" PRIu32 "].contactFlags: 0x%08" PRIX32 "", index,
649 contact->contactFlags);
650 rdpei_print_contact_flags(log, contact->contactFlags);
651#endif
652 Stream_Write_UINT8(
653 s, WINPR_ASSERTING_INT_CAST(uint8_t, contact->contactId)); /* contactId (1 byte) */
654 /* fieldsPresent (TWO_BYTE_UNSIGNED_INTEGER) */
655 rdpei_write_2byte_unsigned(s, contact->fieldsPresent);
656 rdpei_write_4byte_signed(s, contact->x); /* x (FOUR_BYTE_SIGNED_INTEGER) */
657 rdpei_write_4byte_signed(s, contact->y); /* y (FOUR_BYTE_SIGNED_INTEGER) */
658 /* contactFlags (FOUR_BYTE_UNSIGNED_INTEGER) */
659 rdpei_write_4byte_unsigned(s, contact->contactFlags);
660
661 if (contact->fieldsPresent & CONTACT_DATA_CONTACTRECT_PRESENT)
662 {
663 /* contactRectLeft (TWO_BYTE_SIGNED_INTEGER) */
664 rdpei_write_2byte_signed(s, contact->contactRectLeft);
665 /* contactRectTop (TWO_BYTE_SIGNED_INTEGER) */
666 rdpei_write_2byte_signed(s, contact->contactRectTop);
667 /* contactRectRight (TWO_BYTE_SIGNED_INTEGER) */
668 rdpei_write_2byte_signed(s, contact->contactRectRight);
669 /* contactRectBottom (TWO_BYTE_SIGNED_INTEGER) */
670 rdpei_write_2byte_signed(s, contact->contactRectBottom);
671 }
672
673 if (contact->fieldsPresent & CONTACT_DATA_ORIENTATION_PRESENT)
674 {
675 /* orientation (FOUR_BYTE_UNSIGNED_INTEGER) */
676 rdpei_write_4byte_unsigned(s, contact->orientation);
677 }
678
679 if (contact->fieldsPresent & CONTACT_DATA_PRESSURE_PRESENT)
680 {
681 /* pressure (FOUR_BYTE_UNSIGNED_INTEGER) */
682 rdpei_write_4byte_unsigned(s, contact->pressure);
683 }
684 }
685
686 return CHANNEL_RC_OK;
687}
688
694static UINT rdpei_send_touch_event_pdu(GENERIC_CHANNEL_CALLBACK* callback,
696{
697 WINPR_ASSERT(callback);
698
699 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
700 if (!rdpei || !rdpei->rdpcontext)
701 return ERROR_INTERNAL_ERROR;
702 if (freerdp_settings_get_bool(rdpei->rdpcontext->settings, FreeRDP_SuspendInput))
703 return CHANNEL_RC_OK;
704
705 if (!frame)
706 return ERROR_INTERNAL_ERROR;
707
708 size_t pduLength = 64ULL + (64ULL * frame->contactCount);
709 wStream* s = Stream_New(NULL, pduLength);
710
711 if (!s)
712 {
713 WLog_Print(rdpei->base.log, WLOG_ERROR, "Stream_New failed!");
714 return CHANNEL_RC_NO_MEMORY;
715 }
716
717 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
722 rdpei_write_4byte_unsigned(
723 s, (UINT32)frame->frameOffset); /* encodeTime (FOUR_BYTE_UNSIGNED_INTEGER) */
724 rdpei_write_2byte_unsigned(s, 1); /* (frameCount) TWO_BYTE_UNSIGNED_INTEGER */
725
726 const UINT rc = rdpei_write_touch_frame(rdpei->base.log, s, frame);
727 if (rc)
728 {
729 WLog_Print(rdpei->base.log, WLOG_ERROR,
730 "rdpei_write_touch_frame failed with error %" PRIu32 "!", rc);
731 Stream_Free(s, TRUE);
732 return rc;
733 }
734
735 Stream_SealLength(s);
736
737 const UINT status = rdpei_send_pdu(callback, s, EVENTID_TOUCH, Stream_Length(s));
738 Stream_Free(s, TRUE);
739 return status;
740}
741
747static UINT rdpei_recv_sc_ready_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
748{
749 UINT32 features = 0;
750 UINT32 protocolVersion = 0;
751
752 if (!callback || !callback->plugin)
753 return ERROR_INTERNAL_ERROR;
754
755 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
756
757 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
758 return ERROR_INVALID_DATA;
759 Stream_Read_UINT32(s, protocolVersion); /* protocolVersion (4 bytes) */
760
761 if (protocolVersion >= RDPINPUT_PROTOCOL_V300)
762 {
763 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
764 return ERROR_INVALID_DATA;
765 }
766
767 if (Stream_GetRemainingLength(s) >= 4)
768 Stream_Read_UINT32(s, features);
769
770 if (rdpei->version > protocolVersion)
771 rdpei->version = protocolVersion;
772 rdpei->features = features;
773
774 if (protocolVersion > RDPINPUT_PROTOCOL_V300)
775 {
776 WLog_Print(rdpei->base.log, WLOG_WARN,
777 "Unknown [MS-RDPEI] protocolVersion: 0x%08" PRIX32 "", protocolVersion);
778 }
779
780 return CHANNEL_RC_OK;
781}
782
788static UINT rdpei_recv_suspend_touch_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
789{
790 UINT error = CHANNEL_RC_OK;
791
792 WINPR_UNUSED(s);
793
794 if (!callback || !callback->plugin)
795 return ERROR_INTERNAL_ERROR;
796
797 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
798 RdpeiClientContext* context = rdpei->context;
799 if (!rdpei)
800 return ERROR_INTERNAL_ERROR;
801
802 IFCALLRET(context->SuspendTouch, error, context);
803
804 if (error)
805 WLog_Print(rdpei->base.log, WLOG_ERROR,
806 "rdpei->SuspendTouch failed with error %" PRIu32 "!", error);
807
808 return error;
809}
810
816static UINT rdpei_recv_resume_touch_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
817{
818 UINT error = CHANNEL_RC_OK;
819 if (!s || !callback)
820 return ERROR_INTERNAL_ERROR;
821
822 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
823 if (!rdpei)
824 return ERROR_INTERNAL_ERROR;
825
826 RdpeiClientContext* context = (RdpeiClientContext*)callback->plugin->pInterface;
827 if (!context)
828 return ERROR_INTERNAL_ERROR;
829
830 IFCALLRET(context->ResumeTouch, error, context);
831
832 if (error)
833 WLog_Print(rdpei->base.log, WLOG_ERROR, "rdpei->ResumeTouch failed with error %" PRIu32 "!",
834 error);
835
836 return error;
837}
838
844static UINT rdpei_recv_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
845{
846 UINT16 eventId = 0;
847 UINT32 pduLength = 0;
848 UINT error = 0;
849
850 if (!callback || !s)
851 return ERROR_INTERNAL_ERROR;
852
853 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
854 if (!rdpei)
855 return ERROR_INTERNAL_ERROR;
856
857 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 6))
858 return ERROR_INVALID_DATA;
859
860 Stream_Read_UINT16(s, eventId); /* eventId (2 bytes) */
861 Stream_Read_UINT32(s, pduLength); /* pduLength (4 bytes) */
862#ifdef WITH_DEBUG_RDPEI
863 WLog_Print(rdpei->base.log, WLOG_DEBUG,
864 "rdpei_recv_pdu: eventId: %" PRIu16 " (%s) length: %" PRIu32 "", eventId,
865 rdpei_eventid_string(eventId), pduLength);
866#endif
867
868 if ((pduLength < 6) || !Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, pduLength - 6))
869 return ERROR_INVALID_DATA;
870
871 switch (eventId)
872 {
873 case EVENTID_SC_READY:
874 if ((error = rdpei_recv_sc_ready_pdu(callback, s)))
875 {
876 WLog_Print(rdpei->base.log, WLOG_ERROR,
877 "rdpei_recv_sc_ready_pdu failed with error %" PRIu32 "!", error);
878 return error;
879 }
880
881 if ((error = rdpei_send_cs_ready_pdu(callback)))
882 {
883 WLog_Print(rdpei->base.log, WLOG_ERROR,
884 "rdpei_send_cs_ready_pdu failed with error %" PRIu32 "!", error);
885 return error;
886 }
887
888 break;
889
890 case EVENTID_SUSPEND_TOUCH:
891 if ((error = rdpei_recv_suspend_touch_pdu(callback, s)))
892 {
893 WLog_Print(rdpei->base.log, WLOG_ERROR,
894 "rdpei_recv_suspend_touch_pdu failed with error %" PRIu32 "!", error);
895 return error;
896 }
897
898 break;
899
900 case EVENTID_RESUME_TOUCH:
901 if ((error = rdpei_recv_resume_touch_pdu(callback, s)))
902 {
903 WLog_Print(rdpei->base.log, WLOG_ERROR,
904 "rdpei_recv_resume_touch_pdu failed with error %" PRIu32 "!", error);
905 return error;
906 }
907
908 break;
909
910 default:
911 break;
912 }
913
914 return CHANNEL_RC_OK;
915}
916
922static UINT rdpei_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
923{
924 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
925 return rdpei_recv_pdu(callback, data);
926}
927
933static UINT rdpei_on_close(IWTSVirtualChannelCallback* pChannelCallback)
934{
935 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
936 if (callback)
937 {
938 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
939 if (rdpei && rdpei->base.listener_callback)
940 {
941 if (rdpei->base.listener_callback->channel_callback == callback)
942 rdpei->base.listener_callback->channel_callback = NULL;
943 }
944 }
945 free(callback);
946 return CHANNEL_RC_OK;
947}
948
953static UINT32 rdpei_get_version(RdpeiClientContext* context)
954{
955 if (!context || !context->handle)
956 return 0;
957
958 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
959 return rdpei->version;
960}
961
962static UINT32 rdpei_get_features(RdpeiClientContext* context)
963{
964 if (!context || !context->handle)
965 return 0;
966
967 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
968 return rdpei->features;
969}
970
976UINT rdpei_send_frame(RdpeiClientContext* context, RDPINPUT_TOUCH_FRAME* frame)
977{
978 UINT64 currentTime = GetTickCount64();
979 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
980
981 GENERIC_CHANNEL_CALLBACK* callback = rdpei->base.listener_callback->channel_callback;
982
983 /* Just ignore the event if the channel is not connected */
984 if (!callback)
985 return CHANNEL_RC_OK;
986
987 if (!rdpei->previousFrameTime && !rdpei->currentFrameTime)
988 {
989 rdpei->currentFrameTime = currentTime;
990 frame->frameOffset = 0;
991 }
992 else
993 {
994 rdpei->currentFrameTime = currentTime;
995 frame->frameOffset = rdpei->currentFrameTime - rdpei->previousFrameTime;
996 }
997
998 const UINT error = rdpei_send_touch_event_pdu(callback, frame);
999 if (error)
1000 {
1001 WLog_Print(rdpei->base.log, WLOG_ERROR,
1002 "rdpei_send_touch_event_pdu failed with error %" PRIu32 "!", error);
1003 return error;
1004 }
1005
1006 rdpei->previousFrameTime = rdpei->currentFrameTime;
1007 return error;
1008}
1009
1015static UINT rdpei_add_contact(RdpeiClientContext* context, const RDPINPUT_CONTACT_DATA* contact)
1016{
1017 UINT error = CHANNEL_RC_OK;
1018 if (!context || !contact || !context->handle)
1019 return ERROR_INTERNAL_ERROR;
1020
1021 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1022
1023 EnterCriticalSection(&rdpei->lock);
1024 RDPINPUT_CONTACT_POINT* contactPoint = &rdpei->contactPoints[contact->contactId];
1025
1026 if (contactPoint->dirty && contactPoint->data.contactFlags != contact->contactFlags)
1027 {
1028 const INT32 externalId = contactPoint->externalId;
1029 error = rdpei_add_frame(context);
1030 if (!contactPoint->active)
1031 {
1032 contactPoint->active = TRUE;
1033 contactPoint->externalId = externalId;
1034 contactPoint->contactId = contact->contactId;
1035 }
1036 }
1037
1038 contactPoint->data = *contact;
1039 contactPoint->dirty = TRUE;
1040 (void)SetEvent(rdpei->event);
1041 LeaveCriticalSection(&rdpei->lock);
1042
1043 return error;
1044}
1045
1046static UINT rdpei_touch_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1047 INT32 x, INT32 y, INT32* contactId, UINT32 fieldFlags, va_list ap)
1048{
1049 INT64 contactIdlocal = -1;
1050 UINT error = CHANNEL_RC_OK;
1051
1052 if (!context || !contactId || !context->handle)
1053 return ERROR_INTERNAL_ERROR;
1054
1055 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1056 /* Create a new contact point in an empty slot */
1057 EnterCriticalSection(&rdpei->lock);
1058 const BOOL begin = (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN) != 0;
1059 RDPINPUT_CONTACT_POINT* contactPoint = rdpei_contact(rdpei, externalId, !begin);
1060 if (contactPoint)
1061 contactIdlocal = contactPoint->contactId;
1062
1063 if (contactIdlocal > UINT32_MAX)
1064 {
1065 error = ERROR_INVALID_PARAMETER;
1066 goto fail;
1067 }
1068
1069 if (contactIdlocal >= 0)
1070 {
1071 RDPINPUT_CONTACT_DATA contact = { 0 };
1072 contact.x = x;
1073 contact.y = y;
1074 contact.contactId = (UINT32)contactIdlocal;
1075 contact.contactFlags = contactFlags;
1076 contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1077
1078 if (fieldFlags & CONTACT_DATA_CONTACTRECT_PRESENT)
1079 {
1080 INT32 val = va_arg(ap, INT32);
1081 contact.contactRectLeft = WINPR_ASSERTING_INT_CAST(INT16, val);
1082
1083 val = va_arg(ap, INT32);
1084 contact.contactRectTop = WINPR_ASSERTING_INT_CAST(INT16, val);
1085
1086 val = va_arg(ap, INT32);
1087 contact.contactRectRight = WINPR_ASSERTING_INT_CAST(INT16, val);
1088
1089 val = va_arg(ap, INT32);
1090 contact.contactRectBottom = WINPR_ASSERTING_INT_CAST(INT16, val);
1091 }
1092 if (fieldFlags & CONTACT_DATA_ORIENTATION_PRESENT)
1093 {
1094 UINT32 p = va_arg(ap, UINT32);
1095 if (p >= 360)
1096 {
1097 WLog_Print(rdpei->base.log, WLOG_WARN,
1098 "TouchContact %" PRId64 ": Invalid orientation value %" PRIu32
1099 "degree, clamping to 359 degree",
1100 contactIdlocal, p);
1101 p = 359;
1102 }
1103 contact.orientation = p;
1104 }
1105 if (fieldFlags & CONTACT_DATA_PRESSURE_PRESENT)
1106 {
1107 UINT32 p = va_arg(ap, UINT32);
1108 if (p > 1024)
1109 {
1110 WLog_Print(rdpei->base.log, WLOG_WARN,
1111 "TouchContact %" PRId64 ": Invalid pressure value %" PRIu32
1112 ", clamping to 1024",
1113 contactIdlocal, p);
1114 p = 1024;
1115 }
1116 contact.pressure = p;
1117 }
1118
1119 error = context->AddContact(context, &contact);
1120 }
1121
1122fail:
1123 if (contactId)
1124 *contactId = (INT32)contactIdlocal;
1125
1126 LeaveCriticalSection(&rdpei->lock);
1127 return error;
1128}
1129
1135static UINT rdpei_touch_begin(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1136 INT32* contactId)
1137{
1138 UINT rc = 0;
1139 va_list ap = { 0 };
1140 rc = rdpei_touch_process(context, externalId,
1141 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1142 RDPINPUT_CONTACT_FLAG_INCONTACT,
1143 x, y, contactId, 0, ap);
1144 return rc;
1145}
1146
1152static UINT rdpei_touch_update(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1153 INT32* contactId)
1154{
1155 UINT rc = 0;
1156 va_list ap = { 0 };
1157 rc = rdpei_touch_process(context, externalId,
1158 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1159 RDPINPUT_CONTACT_FLAG_INCONTACT,
1160 x, y, contactId, 0, ap);
1161 return rc;
1162}
1163
1169static UINT rdpei_touch_end(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1170 INT32* contactId)
1171{
1172 UINT error = 0;
1173 va_list ap = { 0 };
1174 error = rdpei_touch_process(context, externalId,
1175 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1176 RDPINPUT_CONTACT_FLAG_INCONTACT,
1177 x, y, contactId, 0, ap);
1178 if (error != CHANNEL_RC_OK)
1179 return error;
1180 error =
1181 rdpei_touch_process(context, externalId, RDPINPUT_CONTACT_FLAG_UP, x, y, contactId, 0, ap);
1182 return error;
1183}
1184
1190static UINT rdpei_touch_cancel(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1191 INT32* contactId)
1192{
1193 UINT rc = 0;
1194 va_list ap = { 0 };
1195 rc = rdpei_touch_process(context, externalId,
1196 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_CANCELED, x, y,
1197 contactId, 0, ap);
1198 return rc;
1199}
1200
1201static UINT rdpei_touch_raw_event(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1202 INT32* contactId, UINT32 flags, UINT32 fieldFlags, ...)
1203{
1204 UINT rc = 0;
1205 va_list ap;
1206 va_start(ap, fieldFlags);
1207 rc = rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, ap);
1208 va_end(ap);
1209 return rc;
1210}
1211
1212static UINT rdpei_touch_raw_event_va(RdpeiClientContext* context, INT32 externalId, INT32 x,
1213 INT32 y, INT32* contactId, UINT32 flags, UINT32 fieldFlags,
1214 va_list args)
1215{
1216 return rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, args);
1217}
1218
1219static RDPINPUT_PEN_CONTACT_POINT* rdpei_pen_contact(RDPEI_PLUGIN* rdpei, INT32 externalId,
1220 BOOL active)
1221{
1222 if (!rdpei)
1223 return NULL;
1224
1225 for (UINT32 x = 0; x < rdpei->maxPenContacts; x++)
1226 {
1227 RDPINPUT_PEN_CONTACT_POINT* contact = &rdpei->penContactPoints[x];
1228 if (active)
1229 {
1230 if (contact->active)
1231 {
1232 if (contact->externalId == externalId)
1233 return contact;
1234 }
1235 }
1236 else
1237 {
1238 if (!contact->active)
1239 {
1240 contact->externalId = externalId;
1241 contact->active = TRUE;
1242 return contact;
1243 }
1244 }
1245 }
1246 return NULL;
1247}
1248
1249static UINT rdpei_add_pen(RdpeiClientContext* context, INT32 externalId,
1250 const RDPINPUT_PEN_CONTACT* contact)
1251{
1252 if (!context || !contact || !context->handle)
1253 return ERROR_INTERNAL_ERROR;
1254
1255 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1256
1257 EnterCriticalSection(&rdpei->lock);
1258
1259 RDPINPUT_PEN_CONTACT_POINT* contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1260 if (contactPoint)
1261 {
1262 contactPoint->data = *contact;
1263 contactPoint->dirty = TRUE;
1264 (void)SetEvent(rdpei->event);
1265 }
1266
1267 LeaveCriticalSection(&rdpei->lock);
1268
1269 return CHANNEL_RC_OK;
1270}
1271
1272static UINT rdpei_pen_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1273 UINT32 fieldFlags, INT32 x, INT32 y, va_list ap)
1274{
1275 RDPINPUT_PEN_CONTACT_POINT* contactPoint = NULL;
1276 UINT error = CHANNEL_RC_OK;
1277
1278 if (!context || !context->handle)
1279 return ERROR_INTERNAL_ERROR;
1280
1281 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1282
1283 EnterCriticalSection(&rdpei->lock);
1284 // Start a new contact only when it is not active.
1285 contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1286 if (!contactPoint)
1287 {
1288 const UINT32 mask = RDPINPUT_CONTACT_FLAG_INRANGE;
1289 if ((contactFlags & mask) == mask)
1290 {
1291 contactPoint = rdpei_pen_contact(rdpei, externalId, FALSE);
1292 }
1293 }
1294
1295 if (contactPoint != NULL)
1296 {
1297 RDPINPUT_PEN_CONTACT contact = { 0 };
1298
1299 contact.x = x;
1300 contact.y = y;
1301 contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1302
1303 contact.contactFlags = contactFlags;
1304 if (fieldFlags & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
1305 {
1306 const UINT32 val = va_arg(ap, UINT32);
1307 contact.penFlags = WINPR_ASSERTING_INT_CAST(UINT16, val);
1308 }
1309 if (fieldFlags & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
1310 {
1311 const UINT32 val = va_arg(ap, UINT32);
1312 contact.pressure = WINPR_ASSERTING_INT_CAST(UINT16, val);
1313 }
1314 if (fieldFlags & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
1315 {
1316 const UINT32 val = va_arg(ap, UINT32);
1317 contact.rotation = WINPR_ASSERTING_INT_CAST(UINT16, val);
1318 }
1319 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
1320 {
1321 const INT32 val = va_arg(ap, INT32);
1322 contact.tiltX = WINPR_ASSERTING_INT_CAST(INT16, val);
1323 }
1324 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
1325 {
1326 const INT32 val = va_arg(ap, INT32);
1327 WINPR_ASSERT((val >= INT16_MIN) && (val <= INT16_MAX));
1328 contact.tiltY = WINPR_ASSERTING_INT_CAST(INT16, val);
1329 }
1330
1331 error = context->AddPen(context, externalId, &contact);
1332 }
1333
1334 LeaveCriticalSection(&rdpei->lock);
1335
1336 return error;
1337}
1338
1344static UINT rdpei_pen_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1345 INT32 x, INT32 y, ...)
1346{
1347 UINT error = 0;
1348 va_list ap;
1349
1350 va_start(ap, y);
1351 error = rdpei_pen_process(context, externalId,
1352 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1353 RDPINPUT_CONTACT_FLAG_INCONTACT,
1354 fieldFlags, x, y, ap);
1355 va_end(ap);
1356
1357 return error;
1358}
1359
1365static UINT rdpei_pen_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1366 INT32 x, INT32 y, ...)
1367{
1368 UINT error = 0;
1369 va_list ap;
1370
1371 va_start(ap, y);
1372 error = rdpei_pen_process(context, externalId,
1373 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1374 RDPINPUT_CONTACT_FLAG_INCONTACT,
1375 fieldFlags, x, y, ap);
1376 va_end(ap);
1377 return error;
1378}
1379
1385static UINT rdpei_pen_end(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, INT32 x,
1386 INT32 y, ...)
1387{
1388 UINT error = 0;
1389 va_list ap;
1390 va_start(ap, y);
1391 error = rdpei_pen_process(context, externalId,
1392 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_INRANGE, fieldFlags,
1393 x, y, ap);
1394 va_end(ap);
1395 return error;
1396}
1397
1403static UINT rdpei_pen_hover_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1404 INT32 x, INT32 y, ...)
1405{
1406 UINT error = 0;
1407 va_list ap;
1408
1409 va_start(ap, y);
1410 error = rdpei_pen_process(context, externalId,
1411 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1412 fieldFlags, x, y, ap);
1413 va_end(ap);
1414
1415 return error;
1416}
1417
1423static UINT rdpei_pen_hover_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1424 INT32 x, INT32 y, ...)
1425{
1426 UINT error = 0;
1427 va_list ap;
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_cancel(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1444 INT32 x, INT32 y, ...)
1445{
1446 UINT error = 0;
1447 va_list ap;
1448
1449 va_start(ap, y);
1450 error = rdpei_pen_process(context, externalId,
1451 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_CANCELED,
1452 fieldFlags, x, y, ap);
1453 va_end(ap);
1454
1455 return error;
1456}
1457
1458static UINT rdpei_pen_raw_event(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1459 UINT32 fieldFlags, INT32 x, INT32 y, ...)
1460{
1461 UINT error = 0;
1462 va_list ap;
1463
1464 va_start(ap, y);
1465 error = rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, ap);
1466 va_end(ap);
1467 return error;
1468}
1469
1470static UINT rdpei_pen_raw_event_va(RdpeiClientContext* context, INT32 externalId,
1471 UINT32 contactFlags, UINT32 fieldFlags, INT32 x, INT32 y,
1472 va_list args)
1473{
1474 return rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, args);
1475}
1476
1477static UINT init_plugin_cb(GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext, rdpSettings* settings)
1478{
1479 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1480
1481 WINPR_ASSERT(base);
1482 WINPR_UNUSED(settings);
1483
1484 rdpei->version = RDPINPUT_PROTOCOL_V300;
1485 rdpei->currentFrameTime = 0;
1486 rdpei->previousFrameTime = 0;
1487 rdpei->maxTouchContacts = MAX_CONTACTS;
1488 rdpei->maxPenContacts = MAX_PEN_CONTACTS;
1489 rdpei->rdpcontext = rcontext;
1490
1491 WINPR_ASSERT(rdpei->base.log);
1492
1493 InitializeCriticalSection(&rdpei->lock);
1494 rdpei->event = CreateEventA(NULL, TRUE, FALSE, NULL);
1495 if (!rdpei->event)
1496 {
1497 WLog_Print(rdpei->base.log, WLOG_ERROR, "calloc failed!");
1498 return CHANNEL_RC_NO_MEMORY;
1499 }
1500
1501 RdpeiClientContext* context = (RdpeiClientContext*)calloc(1, sizeof(RdpeiClientContext));
1502 if (!context)
1503 {
1504 WLog_Print(rdpei->base.log, WLOG_ERROR, "calloc failed!");
1505 return CHANNEL_RC_NO_MEMORY;
1506 }
1507
1508 context->clientFeaturesMask = UINT32_MAX;
1509 context->handle = (void*)rdpei;
1510 context->GetVersion = rdpei_get_version;
1511 context->GetFeatures = rdpei_get_features;
1512 context->AddContact = rdpei_add_contact;
1513 context->TouchBegin = rdpei_touch_begin;
1514 context->TouchUpdate = rdpei_touch_update;
1515 context->TouchEnd = rdpei_touch_end;
1516 context->TouchCancel = rdpei_touch_cancel;
1517 context->TouchRawEvent = rdpei_touch_raw_event;
1518 context->TouchRawEventVA = rdpei_touch_raw_event_va;
1519 context->AddPen = rdpei_add_pen;
1520 context->PenBegin = rdpei_pen_begin;
1521 context->PenUpdate = rdpei_pen_update;
1522 context->PenEnd = rdpei_pen_end;
1523 context->PenHoverBegin = rdpei_pen_hover_begin;
1524 context->PenHoverUpdate = rdpei_pen_hover_update;
1525 context->PenHoverCancel = rdpei_pen_hover_cancel;
1526 context->PenRawEvent = rdpei_pen_raw_event;
1527 context->PenRawEventVA = rdpei_pen_raw_event_va;
1528
1529 rdpei->context = context;
1530 rdpei->base.iface.pInterface = (void*)context;
1531
1532 rdpei->async =
1533 !freerdp_settings_get_bool(rdpei->rdpcontext->settings, FreeRDP_SynchronousDynamicChannels);
1534 if (rdpei->async)
1535 {
1536 rdpei->running = TRUE;
1537
1538 rdpei->thread = CreateThread(NULL, 0, rdpei_periodic_update, rdpei, 0, NULL);
1539 if (!rdpei->thread)
1540 {
1541 WLog_Print(rdpei->base.log, WLOG_ERROR, "calloc failed!");
1542 return CHANNEL_RC_NO_MEMORY;
1543 }
1544 }
1545 else
1546 {
1547 if (!freerdp_client_channel_register(rdpei->rdpcontext->channels, rdpei->event,
1548 rdpei_poll_run, rdpei))
1549 return ERROR_INTERNAL_ERROR;
1550 }
1551
1552 return CHANNEL_RC_OK;
1553}
1554
1555static void terminate_plugin_cb(GENERIC_DYNVC_PLUGIN* base)
1556{
1557 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1558 WINPR_ASSERT(rdpei);
1559
1560 rdpei->running = FALSE;
1561 if (rdpei->event)
1562 (void)SetEvent(rdpei->event);
1563
1564 if (rdpei->thread)
1565 {
1566 (void)WaitForSingleObject(rdpei->thread, INFINITE);
1567 (void)CloseHandle(rdpei->thread);
1568 }
1569
1570 if (rdpei->event && !rdpei->async)
1571 (void)freerdp_client_channel_unregister(rdpei->rdpcontext->channels, rdpei->event);
1572
1573 if (rdpei->event)
1574 (void)CloseHandle(rdpei->event);
1575
1576 DeleteCriticalSection(&rdpei->lock);
1577 free(rdpei->context);
1578}
1579
1580static const IWTSVirtualChannelCallback rdpei_callbacks = { rdpei_on_data_received, NULL, /* Open */
1581 rdpei_on_close, NULL };
1582
1588FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpei_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1589{
1590 return freerdp_generic_DVCPluginEntry(pEntryPoints, RDPEI_TAG, RDPEI_DVC_CHANNEL_NAME,
1591 sizeof(RDPEI_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
1592 &rdpei_callbacks, init_plugin_cb, terminate_plugin_cb);
1593}
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