22#include <freerdp/config.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>
38#include <freerdp/addin.h>
39#include <freerdp/freerdp.h>
40#include <freerdp/client/channels.h>
42#include "rdpei_common.h"
44#include "rdpei_main.h"
46#define RDPEI_TAG CHANNELS_TAG("rdpei.client")
68#define MAX_CONTACTS 64
69#define MAX_PEN_CONTACTS 4
75 RdpeiClientContext* context;
79 UINT16 maxTouchContacts;
80 UINT64 currentFrameTime;
81 UINT64 previousFrameTime;
84 UINT64 currentPenFrameTime;
85 UINT64 previousPenFrameTime;
86 UINT16 maxPenContacts;
90 rdpContext* rdpcontext;
95 UINT64 lastPollEventTime;
107#ifdef WITH_DEBUG_RDPEI
108static const char* rdpei_eventid_string(UINT16 event)
112 case EVENTID_SC_READY:
113 return "EVENTID_SC_READY";
114 case EVENTID_CS_READY:
115 return "EVENTID_CS_READY";
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";
125 return "EVENTID_PEN";
127 return "EVENTID_UNKNOWN";
134 for (UINT16 i = 0; i < rdpei->maxTouchContacts; i++)
138 if (!contactPoint->active && active)
140 else if (!contactPoint->active && !active)
142 contactPoint->contactId = i;
143 contactPoint->externalId = externalId;
144 contactPoint->active = TRUE;
147 else if (contactPoint->externalId == externalId)
160static UINT rdpei_add_frame(RdpeiClientContext* context)
165 if (!context || !context->handle)
166 return ERROR_INTERNAL_ERROR;
168 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
169 frame.contacts = contacts;
171 for (UINT16 i = 0; i < rdpei->maxTouchContacts; i++)
176 if (contactPoint->dirty)
178 contacts[frame.contactCount] = *contact;
179 rdpei->contactPoints[i].dirty = FALSE;
180 frame.contactCount++;
182 else if (contactPoint->active)
184 if (contact->contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
186 contact->contactFlags = RDPINPUT_CONTACT_FLAG_UPDATE;
187 contact->contactFlags |= RDPINPUT_CONTACT_FLAG_INRANGE;
188 contact->contactFlags |= RDPINPUT_CONTACT_FLAG_INCONTACT;
191 contacts[frame.contactCount] = *contact;
192 frame.contactCount++;
194 if (contact->contactFlags & RDPINPUT_CONTACT_FLAG_UP)
196 contactPoint->active = FALSE;
197 contactPoint->externalId = 0;
198 contactPoint->contactId = 0;
202 if (frame.contactCount > 0)
204 UINT error = rdpei_send_frame(context, &frame);
205 if (error != CHANNEL_RC_OK)
207 WLog_Print(rdpei->base.log, WLOG_ERROR,
208 "rdpei_send_frame failed with error %" PRIu32
"!", error);
212 return CHANNEL_RC_OK;
223 if (!callback || !s || !callback->channel || !callback->channel->Write)
224 return ERROR_INTERNAL_ERROR;
226 if (pduLength > UINT32_MAX)
227 return ERROR_INVALID_PARAMETER;
229 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
231 return ERROR_INTERNAL_ERROR;
233 Stream_ResetPosition(s);
234 Stream_Write_UINT16(s, eventId);
235 Stream_Write_UINT32(s, (UINT32)pduLength);
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);
251 return ERROR_INTERNAL_ERROR;
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++)
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)
274 if (!rdpei_write_4byte_unsigned(s, contact->penFlags))
275 return ERROR_OUTOFMEMORY;
277 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
279 if (!rdpei_write_4byte_unsigned(s, contact->pressure))
280 return ERROR_OUTOFMEMORY;
282 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
284 if (!rdpei_write_2byte_unsigned(s, contact->rotation))
285 return ERROR_OUTOFMEMORY;
287 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
289 if (!rdpei_write_2byte_signed(s, contact->tiltX))
290 return ERROR_OUTOFMEMORY;
292 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
294 if (!rdpei_write_2byte_signed(s, contact->tiltY))
295 return ERROR_OUTOFMEMORY;
298 return CHANNEL_RC_OK;
304 UINT status = ERROR_OUTOFMEMORY;
305 WINPR_ASSERT(callback);
307 if (frameOffset > UINT32_MAX)
308 return ERROR_INVALID_PARAMETER;
309 if (count > UINT16_MAX)
310 return ERROR_INVALID_PARAMETER;
312 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
314 return ERROR_INTERNAL_ERROR;
316 if (!frames || (count == 0))
317 return ERROR_INTERNAL_ERROR;
319 wStream* s = Stream_New(
nullptr, 64);
323 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Stream_New failed!");
324 return CHANNEL_RC_NO_MEMORY;
327 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
332 if (!rdpei_write_4byte_unsigned(
333 s, (UINT32)frameOffset))
335 if (!rdpei_write_2byte_unsigned(s, (UINT16)count))
338 for (
size_t x = 0; x < count; x++)
340 status = rdpei_write_pen_frame(s, &frames[x]);
343 WLog_Print(rdpei->base.log, WLOG_ERROR,
344 "rdpei_write_pen_frame failed with error %" PRIu32
"!", status);
348 Stream_SealLength(s);
350 status = rdpei_send_pdu(callback, s, EVENTID_PEN, Stream_Length(s));
352 Stream_Free(s, TRUE);
356static UINT rdpei_send_pen_frame(RdpeiClientContext* context,
RDPINPUT_PEN_FRAME* frame)
358 const UINT64 currentTime = GetTickCount64();
361 return ERROR_INTERNAL_ERROR;
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;
369 return CHANNEL_RC_OK;
374 return CHANNEL_RC_OK;
376 if (!rdpei->previousPenFrameTime && !rdpei->currentPenFrameTime)
378 rdpei->currentPenFrameTime = currentTime;
379 frame->frameOffset = 0;
383 rdpei->currentPenFrameTime = currentTime;
384 frame->frameOffset = rdpei->currentPenFrameTime - rdpei->previousPenFrameTime;
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);
392 rdpei->previousPenFrameTime = rdpei->currentPenFrameTime;
396static UINT rdpei_add_pen_frame(RdpeiClientContext* context)
401 if (!context || !context->handle)
402 return ERROR_INTERNAL_ERROR;
404 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
406 penFrame.contacts = penContacts;
408 for (UINT16 i = 0; i < rdpei->maxPenContacts; i++)
414 penContacts[penFrame.contactCount++] = contact->data;
415 contact->dirty = FALSE;
417 else if (contact->active)
419 if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
421 contact->data.contactFlags = RDPINPUT_CONTACT_FLAG_UPDATE;
422 contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INRANGE;
423 contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INCONTACT;
426 penContacts[penFrame.contactCount++] = contact->data;
428 if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
430 contact->externalId = 0;
431 contact->active = FALSE;
435 if (penFrame.contactCount > 0)
436 return rdpei_send_pen_frame(context, &penFrame);
437 return CHANNEL_RC_OK;
440static UINT rdpei_update(wLog* log, RdpeiClientContext* context)
442 UINT error = rdpei_add_frame(context);
443 if (error != CHANNEL_RC_OK)
445 WLog_Print(log, WLOG_ERROR,
"rdpei_add_frame failed with error %" PRIu32
"!", error);
449 return rdpei_add_pen_frame(context);
452static BOOL rdpei_poll_run_unlocked(rdpContext* context,
void* userdata)
454 RDPEI_PLUGIN* rdpei = userdata;
456 WINPR_ASSERT(context);
458 const UINT64 now = GetTickCount64();
461 if ((now < rdpei->lastPollEventTime) || (now - rdpei->lastPollEventTime < 20ULL))
464 rdpei->lastPollEventTime = now;
466 const UINT error = rdpei_update(rdpei->base.log, rdpei->context);
468 (void)ResetEvent(rdpei->event);
470 if (error != CHANNEL_RC_OK)
472 WLog_Print(rdpei->base.log, WLOG_ERROR,
"rdpei_add_frame failed with error %" PRIu32
"!",
474 setChannelError(context, error,
"rdpei_add_frame reported an error");
481static BOOL rdpei_poll_run(rdpContext* context,
void* userdata)
483 RDPEI_PLUGIN* rdpei = userdata;
486 EnterCriticalSection(&rdpei->lock);
487 BOOL rc = rdpei_poll_run_unlocked(context, userdata);
488 LeaveCriticalSection(&rdpei->lock);
492static DWORD WINAPI rdpei_periodic_update(LPVOID arg)
494 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)arg;
495 UINT error = CHANNEL_RC_OK;
499 error = ERROR_INVALID_PARAMETER;
505 error = ERROR_INVALID_PARAMETER;
509 while (rdpei->running)
511 const DWORD status = WaitForSingleObject(rdpei->event, 20);
513 if (status == WAIT_FAILED)
515 error = GetLastError();
516 WLog_Print(rdpei->base.log, WLOG_ERROR,
517 "WaitForMultipleObjects failed with error %" PRIu32
"!", error);
521 if (!rdpei_poll_run(rdpei->rdpcontext, rdpei))
522 error = ERROR_INTERNAL_ERROR;
527 if (error && rdpei && rdpei->rdpcontext)
528 setChannelError(rdpei->rdpcontext, error,
"rdpei_schedule_thread reported an error");
531 rdpei->running = FALSE;
544 if (!callback || !callback->plugin)
545 return ERROR_INTERNAL_ERROR;
547 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
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;
555 UINT32 pduLength = RDPINPUT_HEADER_LENGTH + 10;
556 wStream* s = Stream_New(
nullptr, pduLength);
560 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Stream_New failed!");
561 return CHANNEL_RC_NO_MEMORY;
564 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
565 Stream_Write_UINT32(s, flags);
566 Stream_Write_UINT32(s, rdpei->version);
567 Stream_Write_UINT16(s, rdpei->maxTouchContacts);
568 Stream_SealLength(s);
570 const UINT status = rdpei_send_pdu(callback, s, EVENTID_CS_READY, pduLength);
571 Stream_Free(s, TRUE);
575#if defined(WITH_DEBUG_RDPEI)
576static void rdpei_print_contact_flags(wLog* log, UINT32 contactFlags)
578 if (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
579 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_DOWN");
581 if (contactFlags & RDPINPUT_CONTACT_FLAG_UPDATE)
582 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_UPDATE");
584 if (contactFlags & RDPINPUT_CONTACT_FLAG_UP)
585 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_UP");
587 if (contactFlags & RDPINPUT_CONTACT_FLAG_INRANGE)
588 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_INRANGE");
590 if (contactFlags & RDPINPUT_CONTACT_FLAG_INCONTACT)
591 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_INCONTACT");
593 if (contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
594 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_CANCELED");
598static INT16 bounded(INT32 val)
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);
621 if (!rdpei_write_2byte_unsigned(
622 s, frame->contactCount))
623 return ERROR_OUTOFMEMORY;
628 if (!rdpei_write_8byte_unsigned(s, frame->frameOffset *
630 return ERROR_OUTOFMEMORY;
632 if (!Stream_EnsureRemainingCapacity(s, frame->contactCount * 64ull))
634 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
635 return CHANNEL_RC_NO_MEMORY;
638 for (UINT32 index = 0; index < frame->contactCount; index++)
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,
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);
659 s, WINPR_ASSERTING_INT_CAST(uint8_t, contact->contactId));
661 if (!rdpei_write_2byte_unsigned(s, contact->fieldsPresent))
662 return ERROR_OUTOFMEMORY;
663 if (!rdpei_write_4byte_signed(s, contact->x))
664 return ERROR_OUTOFMEMORY;
665 if (!rdpei_write_4byte_signed(s, contact->y))
666 return ERROR_OUTOFMEMORY;
668 if (!rdpei_write_4byte_unsigned(s, contact->contactFlags))
669 return ERROR_OUTOFMEMORY;
671 if (contact->fieldsPresent & CONTACT_DATA_CONTACTRECT_PRESENT)
674 if (!rdpei_write_2byte_signed(s, contact->contactRectLeft))
675 return ERROR_OUTOFMEMORY;
677 if (!rdpei_write_2byte_signed(s, contact->contactRectTop))
678 return ERROR_OUTOFMEMORY;
680 if (!rdpei_write_2byte_signed(s, contact->contactRectRight))
681 return ERROR_OUTOFMEMORY;
683 if (!rdpei_write_2byte_signed(s, contact->contactRectBottom))
684 return ERROR_OUTOFMEMORY;
687 if (contact->fieldsPresent & CONTACT_DATA_ORIENTATION_PRESENT)
690 if (!rdpei_write_4byte_unsigned(s, contact->orientation))
691 return ERROR_OUTOFMEMORY;
694 if (contact->fieldsPresent & CONTACT_DATA_PRESSURE_PRESENT)
697 if (!rdpei_write_4byte_unsigned(s, contact->pressure))
698 return ERROR_OUTOFMEMORY;
702 return CHANNEL_RC_OK;
713 UINT status = ERROR_OUTOFMEMORY;
714 WINPR_ASSERT(callback);
716 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
717 if (!rdpei || !rdpei->rdpcontext)
718 return ERROR_INTERNAL_ERROR;
720 return CHANNEL_RC_OK;
723 return ERROR_INTERNAL_ERROR;
725 size_t pduLength = 64ULL + (64ULL * frame->contactCount);
726 wStream* s = Stream_New(
nullptr, pduLength);
730 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Stream_New failed!");
731 return CHANNEL_RC_NO_MEMORY;
734 if (!Stream_SafeZero(s, RDPINPUT_HEADER_LENGTH))
740 if (!rdpei_write_4byte_unsigned(
741 s, (UINT32)frame->frameOffset))
743 if (!rdpei_write_2byte_unsigned(s, 1))
746 const UINT rc = rdpei_write_touch_frame(rdpei->base.log, s, frame);
749 WLog_Print(rdpei->base.log, WLOG_ERROR,
750 "rdpei_write_touch_frame failed with error %" PRIu32
"!", rc);
755 Stream_SealLength(s);
757 status = rdpei_send_pdu(callback, s, EVENTID_TOUCH, Stream_Length(s));
759 Stream_Free(s, TRUE);
771 UINT32 protocolVersion = 0;
773 if (!callback || !callback->plugin)
774 return ERROR_INTERNAL_ERROR;
776 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
778 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
779 return ERROR_INVALID_DATA;
780 Stream_Read_UINT32(s, protocolVersion);
782 if (protocolVersion >= RDPINPUT_PROTOCOL_V300)
784 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
785 return ERROR_INVALID_DATA;
788 if (Stream_GetRemainingLength(s) >= 4)
789 Stream_Read_UINT32(s, features);
791 if (rdpei->version > protocolVersion)
792 rdpei->version = protocolVersion;
793 rdpei->features = features;
795 if (protocolVersion > RDPINPUT_PROTOCOL_V300)
797 WLog_Print(rdpei->base.log, WLOG_WARN,
798 "Unknown [MS-RDPEI] protocolVersion: 0x%08" PRIX32
"", protocolVersion);
801 return CHANNEL_RC_OK;
811 UINT error = CHANNEL_RC_OK;
815 if (!callback || !callback->plugin)
816 return ERROR_INTERNAL_ERROR;
818 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
819 RdpeiClientContext* context = rdpei->context;
821 return ERROR_INTERNAL_ERROR;
823 IFCALLRET(context->SuspendTouch, error, context);
826 WLog_Print(rdpei->base.log, WLOG_ERROR,
827 "rdpei->SuspendTouch failed with error %" PRIu32
"!", error);
839 UINT error = CHANNEL_RC_OK;
841 return ERROR_INTERNAL_ERROR;
843 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
845 return ERROR_INTERNAL_ERROR;
847 RdpeiClientContext* context = (RdpeiClientContext*)callback->plugin->pInterface;
849 return ERROR_INTERNAL_ERROR;
851 IFCALLRET(context->ResumeTouch, error, context);
854 WLog_Print(rdpei->base.log, WLOG_ERROR,
"rdpei->ResumeTouch failed with error %" PRIu32
"!",
868 UINT32 pduLength = 0;
872 return ERROR_INTERNAL_ERROR;
874 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
876 return ERROR_INTERNAL_ERROR;
878 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 6))
879 return ERROR_INVALID_DATA;
881 Stream_Read_UINT16(s, eventId);
882 Stream_Read_UINT32(s, pduLength);
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);
889 if ((pduLength < 6) || !Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, pduLength - 6))
890 return ERROR_INVALID_DATA;
894 case EVENTID_SC_READY:
895 if ((error = rdpei_recv_sc_ready_pdu(callback, s)))
897 WLog_Print(rdpei->base.log, WLOG_ERROR,
898 "rdpei_recv_sc_ready_pdu failed with error %" PRIu32
"!", error);
902 if ((error = rdpei_send_cs_ready_pdu(callback)))
904 WLog_Print(rdpei->base.log, WLOG_ERROR,
905 "rdpei_send_cs_ready_pdu failed with error %" PRIu32
"!", error);
911 case EVENTID_SUSPEND_TOUCH:
912 if ((error = rdpei_recv_suspend_touch_pdu(callback, s)))
914 WLog_Print(rdpei->base.log, WLOG_ERROR,
915 "rdpei_recv_suspend_touch_pdu failed with error %" PRIu32
"!", error);
921 case EVENTID_RESUME_TOUCH:
922 if ((error = rdpei_recv_resume_touch_pdu(callback, s)))
924 WLog_Print(rdpei->base.log, WLOG_ERROR,
925 "rdpei_recv_resume_touch_pdu failed with error %" PRIu32
"!", error);
935 return CHANNEL_RC_OK;
943static UINT rdpei_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
946 return rdpei_recv_pdu(callback, data);
954static UINT rdpei_on_close(IWTSVirtualChannelCallback* pChannelCallback)
959 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
960 if (rdpei && rdpei->base.listener_callback)
963 EnterCriticalSection(&rdpei->lock);
964 if (rdpei->base.listener_callback->channel_callback == callback)
965 rdpei->base.listener_callback->channel_callback =
nullptr;
966 LeaveCriticalSection(&rdpei->lock);
970 return CHANNEL_RC_OK;
977static UINT32 rdpei_get_version(RdpeiClientContext* context)
979 if (!context || !context->handle)
982 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
983 return rdpei->version;
986static UINT32 rdpei_get_features(RdpeiClientContext* context)
988 if (!context || !context->handle)
991 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
992 return rdpei->features;
1002 UINT64 currentTime = GetTickCount64();
1003 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1009 return CHANNEL_RC_OK;
1011 if (!rdpei->previousFrameTime && !rdpei->currentFrameTime)
1013 rdpei->currentFrameTime = currentTime;
1014 frame->frameOffset = 0;
1018 rdpei->currentFrameTime = currentTime;
1019 frame->frameOffset = rdpei->currentFrameTime - rdpei->previousFrameTime;
1022 const UINT error = rdpei_send_touch_event_pdu(callback, frame);
1025 WLog_Print(rdpei->base.log, WLOG_ERROR,
1026 "rdpei_send_touch_event_pdu failed with error %" PRIu32
"!", error);
1030 rdpei->previousFrameTime = rdpei->currentFrameTime;
1041 UINT error = CHANNEL_RC_OK;
1042 if (!context || !contact || !context->handle)
1043 return ERROR_INTERNAL_ERROR;
1045 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1047 EnterCriticalSection(&rdpei->lock);
1050 if (contactPoint->dirty && contactPoint->data.contactFlags != contact->contactFlags)
1052 const INT32 externalId = contactPoint->externalId;
1053 error = rdpei_add_frame(context);
1054 if (!contactPoint->active)
1056 contactPoint->active = TRUE;
1057 contactPoint->externalId = externalId;
1058 contactPoint->contactId = contact->contactId;
1062 contactPoint->data = *contact;
1063 contactPoint->dirty = TRUE;
1064 (void)SetEvent(rdpei->event);
1065 LeaveCriticalSection(&rdpei->lock);
1070static UINT rdpei_touch_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1071 INT32 x, INT32 y, INT32* contactId, UINT32 fieldFlags, va_list ap)
1073 INT64 contactIdlocal = -1;
1074 UINT error = CHANNEL_RC_OK;
1076 if (!context || !contactId || !context->handle)
1077 return ERROR_INTERNAL_ERROR;
1079 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1081 EnterCriticalSection(&rdpei->lock);
1082 const BOOL begin = (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN) != 0;
1085 contactIdlocal = contactPoint->contactId;
1087 if (contactIdlocal > UINT32_MAX)
1089 error = ERROR_INVALID_PARAMETER;
1093 if (contactIdlocal >= 0)
1098 contact.contactId = (UINT32)contactIdlocal;
1099 contact.contactFlags = contactFlags;
1100 contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1102 if (fieldFlags & CONTACT_DATA_CONTACTRECT_PRESENT)
1104 INT32 val = va_arg(ap, INT32);
1105 contact.contactRectLeft = WINPR_ASSERTING_INT_CAST(INT16, val);
1107 val = va_arg(ap, INT32);
1108 contact.contactRectTop = WINPR_ASSERTING_INT_CAST(INT16, val);
1110 val = va_arg(ap, INT32);
1111 contact.contactRectRight = WINPR_ASSERTING_INT_CAST(INT16, val);
1113 val = va_arg(ap, INT32);
1114 contact.contactRectBottom = WINPR_ASSERTING_INT_CAST(INT16, val);
1116 if (fieldFlags & CONTACT_DATA_ORIENTATION_PRESENT)
1118 UINT32 p = va_arg(ap, UINT32);
1121 WLog_Print(rdpei->base.log, WLOG_WARN,
1122 "TouchContact %" PRId64
": Invalid orientation value %" PRIu32
1123 "degree, clamping to 359 degree",
1127 contact.orientation = p;
1129 if (fieldFlags & CONTACT_DATA_PRESSURE_PRESENT)
1131 UINT32 p = va_arg(ap, UINT32);
1134 WLog_Print(rdpei->base.log, WLOG_WARN,
1135 "TouchContact %" PRId64
": Invalid pressure value %" PRIu32
1136 ", clamping to 1024",
1140 contact.pressure = p;
1143 error = context->AddContact(context, &contact);
1148 *contactId = (INT32)contactIdlocal;
1150 LeaveCriticalSection(&rdpei->lock);
1159static UINT rdpei_touch_begin(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1163 va_list ap = WINPR_C_ARRAY_INIT;
1164 rc = rdpei_touch_process(context, externalId,
1165 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1166 RDPINPUT_CONTACT_FLAG_INCONTACT,
1167 x, y, contactId, 0, ap);
1176static UINT rdpei_touch_update(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1180 va_list ap = WINPR_C_ARRAY_INIT;
1181 rc = rdpei_touch_process(context, externalId,
1182 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1183 RDPINPUT_CONTACT_FLAG_INCONTACT,
1184 x, y, contactId, 0, ap);
1193static UINT rdpei_touch_end(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1197 va_list ap = WINPR_C_ARRAY_INIT;
1198 error = rdpei_touch_process(context, externalId,
1199 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1200 RDPINPUT_CONTACT_FLAG_INCONTACT,
1201 x, y, contactId, 0, ap);
1202 if (error != CHANNEL_RC_OK)
1205 rdpei_touch_process(context, externalId, RDPINPUT_CONTACT_FLAG_UP, x, y, contactId, 0, ap);
1214static UINT rdpei_touch_cancel(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1218 va_list ap = WINPR_C_ARRAY_INIT;
1219 rc = rdpei_touch_process(context, externalId,
1220 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_CANCELED, x, y,
1225static UINT rdpei_touch_raw_event(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1226 INT32* contactId, UINT32 flags, UINT32 fieldFlags, ...)
1229 va_list ap = WINPR_C_ARRAY_INIT;
1230 va_start(ap, fieldFlags);
1231 rc = rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, ap);
1236static UINT rdpei_touch_raw_event_va(RdpeiClientContext* context, INT32 externalId, INT32 x,
1237 INT32 y, INT32* contactId, UINT32 flags, UINT32 fieldFlags,
1240 return rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, args);
1249 for (UINT32 x = 0; x < rdpei->maxPenContacts; x++)
1254 if (contact->active)
1256 if (contact->externalId == externalId)
1262 if (!contact->active)
1264 contact->externalId = externalId;
1265 contact->active = TRUE;
1273static UINT rdpei_add_pen(RdpeiClientContext* context, INT32 externalId,
1276 if (!context || !contact || !context->handle)
1277 return ERROR_INTERNAL_ERROR;
1279 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1281 EnterCriticalSection(&rdpei->lock);
1286 contactPoint->data = *contact;
1287 contactPoint->dirty = TRUE;
1288 (void)SetEvent(rdpei->event);
1291 LeaveCriticalSection(&rdpei->lock);
1293 return CHANNEL_RC_OK;
1296static UINT rdpei_pen_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1297 UINT32 fieldFlags, INT32 x, INT32 y, va_list ap)
1300 UINT error = CHANNEL_RC_OK;
1302 if (!context || !context->handle)
1303 return ERROR_INTERNAL_ERROR;
1305 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1307 EnterCriticalSection(&rdpei->lock);
1309 contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1312 const UINT32 mask = RDPINPUT_CONTACT_FLAG_INRANGE;
1313 if ((contactFlags & mask) == mask)
1315 contactPoint = rdpei_pen_contact(rdpei, externalId, FALSE);
1319 if (contactPoint !=
nullptr)
1325 contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1327 contact.contactFlags = contactFlags;
1328 if (fieldFlags & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
1330 const UINT32 val = va_arg(ap, UINT32);
1331 contact.penFlags = WINPR_ASSERTING_INT_CAST(UINT16, val);
1333 if (fieldFlags & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
1335 const UINT32 val = va_arg(ap, UINT32);
1336 contact.pressure = WINPR_ASSERTING_INT_CAST(UINT16, val);
1338 if (fieldFlags & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
1340 const UINT32 val = va_arg(ap, UINT32);
1341 contact.rotation = WINPR_ASSERTING_INT_CAST(UINT16, val);
1343 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
1345 const INT32 val = va_arg(ap, INT32);
1346 contact.tiltX = WINPR_ASSERTING_INT_CAST(INT16, val);
1348 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
1350 const INT32 val = va_arg(ap, INT32);
1351 WINPR_ASSERT((val >= INT16_MIN) && (val <= INT16_MAX));
1352 contact.tiltY = WINPR_ASSERTING_INT_CAST(INT16, val);
1355 error = context->AddPen(context, externalId, &contact);
1358 LeaveCriticalSection(&rdpei->lock);
1368static UINT rdpei_pen_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1369 INT32 x, INT32 y, ...)
1372 va_list ap = WINPR_C_ARRAY_INIT;
1375 error = rdpei_pen_process(context, externalId,
1376 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1377 RDPINPUT_CONTACT_FLAG_INCONTACT,
1378 fieldFlags, x, y, ap);
1389static UINT rdpei_pen_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1390 INT32 x, INT32 y, ...)
1393 va_list ap = WINPR_C_ARRAY_INIT;
1396 error = rdpei_pen_process(context, externalId,
1397 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1398 RDPINPUT_CONTACT_FLAG_INCONTACT,
1399 fieldFlags, x, y, ap);
1409static UINT rdpei_pen_end(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, INT32 x,
1413 va_list ap = WINPR_C_ARRAY_INIT;
1415 error = rdpei_pen_process(context, externalId,
1416 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_INRANGE, fieldFlags,
1427static UINT rdpei_pen_hover_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1428 INT32 x, INT32 y, ...)
1431 va_list ap = WINPR_C_ARRAY_INIT;
1434 error = rdpei_pen_process(context, externalId,
1435 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1436 fieldFlags, x, y, ap);
1447static UINT rdpei_pen_hover_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1448 INT32 x, INT32 y, ...)
1451 va_list ap = WINPR_C_ARRAY_INIT;
1454 error = rdpei_pen_process(context, externalId,
1455 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1456 fieldFlags, x, y, ap);
1467static UINT rdpei_pen_hover_cancel(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1468 INT32 x, INT32 y, ...)
1471 va_list ap = WINPR_C_ARRAY_INIT;
1474 error = rdpei_pen_process(context, externalId,
1475 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_CANCELED,
1476 fieldFlags, x, y, ap);
1482static UINT rdpei_pen_raw_event(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1483 UINT32 fieldFlags, INT32 x, INT32 y, ...)
1486 va_list ap = WINPR_C_ARRAY_INIT;
1489 error = rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, ap);
1494static UINT rdpei_pen_raw_event_va(RdpeiClientContext* context, INT32 externalId,
1495 UINT32 contactFlags, UINT32 fieldFlags, INT32 x, INT32 y,
1498 return rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, args);
1501static UINT init_plugin_cb(
GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext, rdpSettings* settings)
1503 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1506 WINPR_UNUSED(settings);
1508 rdpei->version = RDPINPUT_PROTOCOL_V300;
1509 rdpei->currentFrameTime = 0;
1510 rdpei->previousFrameTime = 0;
1511 rdpei->maxTouchContacts = MAX_CONTACTS;
1512 rdpei->maxPenContacts = MAX_PEN_CONTACTS;
1513 rdpei->rdpcontext = rcontext;
1515 WINPR_ASSERT(rdpei->base.log);
1517 InitializeCriticalSection(&rdpei->lock);
1518 rdpei->event = CreateEventA(
nullptr, TRUE, FALSE,
nullptr);
1521 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1522 return CHANNEL_RC_NO_MEMORY;
1525 RdpeiClientContext* context = (RdpeiClientContext*)calloc(1,
sizeof(RdpeiClientContext));
1528 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1529 return CHANNEL_RC_NO_MEMORY;
1532 context->clientFeaturesMask = UINT32_MAX;
1533 context->handle = (
void*)rdpei;
1534 context->GetVersion = rdpei_get_version;
1535 context->GetFeatures = rdpei_get_features;
1536 context->AddContact = rdpei_add_contact;
1537 context->TouchBegin = rdpei_touch_begin;
1538 context->TouchUpdate = rdpei_touch_update;
1539 context->TouchEnd = rdpei_touch_end;
1540 context->TouchCancel = rdpei_touch_cancel;
1541 context->TouchRawEvent = rdpei_touch_raw_event;
1542 context->TouchRawEventVA = rdpei_touch_raw_event_va;
1543 context->AddPen = rdpei_add_pen;
1544 context->PenBegin = rdpei_pen_begin;
1545 context->PenUpdate = rdpei_pen_update;
1546 context->PenEnd = rdpei_pen_end;
1547 context->PenHoverBegin = rdpei_pen_hover_begin;
1548 context->PenHoverUpdate = rdpei_pen_hover_update;
1549 context->PenHoverCancel = rdpei_pen_hover_cancel;
1550 context->PenRawEvent = rdpei_pen_raw_event;
1551 context->PenRawEventVA = rdpei_pen_raw_event_va;
1553 rdpei->context = context;
1554 rdpei->base.iface.pInterface = (
void*)context;
1560 rdpei->running = TRUE;
1562 rdpei->thread = CreateThread(
nullptr, 0, rdpei_periodic_update, rdpei, 0,
nullptr);
1565 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1566 return CHANNEL_RC_NO_MEMORY;
1571 if (!freerdp_client_channel_register(rdpei->rdpcontext->channels, rdpei->event,
1572 rdpei_poll_run, rdpei))
1573 return ERROR_INTERNAL_ERROR;
1576 return CHANNEL_RC_OK;
1581 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1582 WINPR_ASSERT(rdpei);
1584 rdpei->running = FALSE;
1586 (void)SetEvent(rdpei->event);
1590 (void)WaitForSingleObject(rdpei->thread, INFINITE);
1591 (void)CloseHandle(rdpei->thread);
1594 if (rdpei->event && !rdpei->async)
1595 freerdp_client_channel_unregister(rdpei->rdpcontext->channels, rdpei->event);
1598 (void)CloseHandle(rdpei->event);
1600 DeleteCriticalSection(&rdpei->lock);
1601 free(rdpei->context);
1604static const IWTSVirtualChannelCallback rdpei_callbacks = { rdpei_on_data_received,
1606 rdpei_on_close,
nullptr };
1613FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpei_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1615 return freerdp_generic_DVCPluginEntry(pEntryPoints, RDPEI_TAG, RDPEI_DVC_CHANNEL_NAME,
1617 &rdpei_callbacks, init_plugin_cb, terminate_plugin_cb);
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.