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, (
size_t)frame->contactCount * 64))
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_SafeSeek(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)
962 if (rdpei->base.listener_callback->channel_callback == callback)
963 rdpei->base.listener_callback->channel_callback =
nullptr;
967 return CHANNEL_RC_OK;
974static UINT32 rdpei_get_version(RdpeiClientContext* context)
976 if (!context || !context->handle)
979 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
980 return rdpei->version;
983static UINT32 rdpei_get_features(RdpeiClientContext* context)
985 if (!context || !context->handle)
988 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
989 return rdpei->features;
999 UINT64 currentTime = GetTickCount64();
1000 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1006 return CHANNEL_RC_OK;
1008 if (!rdpei->previousFrameTime && !rdpei->currentFrameTime)
1010 rdpei->currentFrameTime = currentTime;
1011 frame->frameOffset = 0;
1015 rdpei->currentFrameTime = currentTime;
1016 frame->frameOffset = rdpei->currentFrameTime - rdpei->previousFrameTime;
1019 const UINT error = rdpei_send_touch_event_pdu(callback, frame);
1022 WLog_Print(rdpei->base.log, WLOG_ERROR,
1023 "rdpei_send_touch_event_pdu failed with error %" PRIu32
"!", error);
1027 rdpei->previousFrameTime = rdpei->currentFrameTime;
1038 UINT error = CHANNEL_RC_OK;
1039 if (!context || !contact || !context->handle)
1040 return ERROR_INTERNAL_ERROR;
1042 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1044 EnterCriticalSection(&rdpei->lock);
1047 if (contactPoint->dirty && contactPoint->data.contactFlags != contact->contactFlags)
1049 const INT32 externalId = contactPoint->externalId;
1050 error = rdpei_add_frame(context);
1051 if (!contactPoint->active)
1053 contactPoint->active = TRUE;
1054 contactPoint->externalId = externalId;
1055 contactPoint->contactId = contact->contactId;
1059 contactPoint->data = *contact;
1060 contactPoint->dirty = TRUE;
1061 (void)SetEvent(rdpei->event);
1062 LeaveCriticalSection(&rdpei->lock);
1067static UINT rdpei_touch_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1068 INT32 x, INT32 y, INT32* contactId, UINT32 fieldFlags, va_list ap)
1070 INT64 contactIdlocal = -1;
1071 UINT error = CHANNEL_RC_OK;
1073 if (!context || !contactId || !context->handle)
1074 return ERROR_INTERNAL_ERROR;
1076 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1078 EnterCriticalSection(&rdpei->lock);
1079 const BOOL begin = (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN) != 0;
1082 contactIdlocal = contactPoint->contactId;
1084 if (contactIdlocal > UINT32_MAX)
1086 error = ERROR_INVALID_PARAMETER;
1090 if (contactIdlocal >= 0)
1095 contact.contactId = (UINT32)contactIdlocal;
1096 contact.contactFlags = contactFlags;
1097 contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1099 if (fieldFlags & CONTACT_DATA_CONTACTRECT_PRESENT)
1101 INT32 val = va_arg(ap, INT32);
1102 contact.contactRectLeft = WINPR_ASSERTING_INT_CAST(INT16, val);
1104 val = va_arg(ap, INT32);
1105 contact.contactRectTop = WINPR_ASSERTING_INT_CAST(INT16, val);
1107 val = va_arg(ap, INT32);
1108 contact.contactRectRight = WINPR_ASSERTING_INT_CAST(INT16, val);
1110 val = va_arg(ap, INT32);
1111 contact.contactRectBottom = WINPR_ASSERTING_INT_CAST(INT16, val);
1113 if (fieldFlags & CONTACT_DATA_ORIENTATION_PRESENT)
1115 UINT32 p = va_arg(ap, UINT32);
1118 WLog_Print(rdpei->base.log, WLOG_WARN,
1119 "TouchContact %" PRId64
": Invalid orientation value %" PRIu32
1120 "degree, clamping to 359 degree",
1124 contact.orientation = p;
1126 if (fieldFlags & CONTACT_DATA_PRESSURE_PRESENT)
1128 UINT32 p = va_arg(ap, UINT32);
1131 WLog_Print(rdpei->base.log, WLOG_WARN,
1132 "TouchContact %" PRId64
": Invalid pressure value %" PRIu32
1133 ", clamping to 1024",
1137 contact.pressure = p;
1140 error = context->AddContact(context, &contact);
1145 *contactId = (INT32)contactIdlocal;
1147 LeaveCriticalSection(&rdpei->lock);
1156static UINT rdpei_touch_begin(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1160 va_list ap = WINPR_C_ARRAY_INIT;
1161 rc = rdpei_touch_process(context, externalId,
1162 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1163 RDPINPUT_CONTACT_FLAG_INCONTACT,
1164 x, y, contactId, 0, ap);
1173static UINT rdpei_touch_update(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1177 va_list ap = WINPR_C_ARRAY_INIT;
1178 rc = rdpei_touch_process(context, externalId,
1179 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1180 RDPINPUT_CONTACT_FLAG_INCONTACT,
1181 x, y, contactId, 0, ap);
1190static UINT rdpei_touch_end(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1194 va_list ap = WINPR_C_ARRAY_INIT;
1195 error = rdpei_touch_process(context, externalId,
1196 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1197 RDPINPUT_CONTACT_FLAG_INCONTACT,
1198 x, y, contactId, 0, ap);
1199 if (error != CHANNEL_RC_OK)
1202 rdpei_touch_process(context, externalId, RDPINPUT_CONTACT_FLAG_UP, x, y, contactId, 0, ap);
1211static UINT rdpei_touch_cancel(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1215 va_list ap = WINPR_C_ARRAY_INIT;
1216 rc = rdpei_touch_process(context, externalId,
1217 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_CANCELED, x, y,
1222static UINT rdpei_touch_raw_event(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1223 INT32* contactId, UINT32 flags, UINT32 fieldFlags, ...)
1226 va_list ap = WINPR_C_ARRAY_INIT;
1227 va_start(ap, fieldFlags);
1228 rc = rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, ap);
1233static UINT rdpei_touch_raw_event_va(RdpeiClientContext* context, INT32 externalId, INT32 x,
1234 INT32 y, INT32* contactId, UINT32 flags, UINT32 fieldFlags,
1237 return rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, args);
1246 for (UINT32 x = 0; x < rdpei->maxPenContacts; x++)
1251 if (contact->active)
1253 if (contact->externalId == externalId)
1259 if (!contact->active)
1261 contact->externalId = externalId;
1262 contact->active = TRUE;
1270static UINT rdpei_add_pen(RdpeiClientContext* context, INT32 externalId,
1273 if (!context || !contact || !context->handle)
1274 return ERROR_INTERNAL_ERROR;
1276 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1278 EnterCriticalSection(&rdpei->lock);
1283 contactPoint->data = *contact;
1284 contactPoint->dirty = TRUE;
1285 (void)SetEvent(rdpei->event);
1288 LeaveCriticalSection(&rdpei->lock);
1290 return CHANNEL_RC_OK;
1293static UINT rdpei_pen_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1294 UINT32 fieldFlags, INT32 x, INT32 y, va_list ap)
1297 UINT error = CHANNEL_RC_OK;
1299 if (!context || !context->handle)
1300 return ERROR_INTERNAL_ERROR;
1302 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1304 EnterCriticalSection(&rdpei->lock);
1306 contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1309 const UINT32 mask = RDPINPUT_CONTACT_FLAG_INRANGE;
1310 if ((contactFlags & mask) == mask)
1312 contactPoint = rdpei_pen_contact(rdpei, externalId, FALSE);
1316 if (contactPoint !=
nullptr)
1322 contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1324 contact.contactFlags = contactFlags;
1325 if (fieldFlags & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
1327 const UINT32 val = va_arg(ap, UINT32);
1328 contact.penFlags = WINPR_ASSERTING_INT_CAST(UINT16, val);
1330 if (fieldFlags & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
1332 const UINT32 val = va_arg(ap, UINT32);
1333 contact.pressure = WINPR_ASSERTING_INT_CAST(UINT16, val);
1335 if (fieldFlags & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
1337 const UINT32 val = va_arg(ap, UINT32);
1338 contact.rotation = WINPR_ASSERTING_INT_CAST(UINT16, val);
1340 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
1342 const INT32 val = va_arg(ap, INT32);
1343 contact.tiltX = WINPR_ASSERTING_INT_CAST(INT16, val);
1345 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
1347 const INT32 val = va_arg(ap, INT32);
1348 WINPR_ASSERT((val >= INT16_MIN) && (val <= INT16_MAX));
1349 contact.tiltY = WINPR_ASSERTING_INT_CAST(INT16, val);
1352 error = context->AddPen(context, externalId, &contact);
1355 LeaveCriticalSection(&rdpei->lock);
1365static UINT rdpei_pen_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1366 INT32 x, INT32 y, ...)
1369 va_list ap = WINPR_C_ARRAY_INIT;
1372 error = rdpei_pen_process(context, externalId,
1373 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1374 RDPINPUT_CONTACT_FLAG_INCONTACT,
1375 fieldFlags, x, y, ap);
1386static UINT rdpei_pen_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1387 INT32 x, INT32 y, ...)
1390 va_list ap = WINPR_C_ARRAY_INIT;
1393 error = rdpei_pen_process(context, externalId,
1394 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1395 RDPINPUT_CONTACT_FLAG_INCONTACT,
1396 fieldFlags, x, y, ap);
1406static UINT rdpei_pen_end(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, INT32 x,
1410 va_list ap = WINPR_C_ARRAY_INIT;
1412 error = rdpei_pen_process(context, externalId,
1413 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_INRANGE, fieldFlags,
1424static UINT rdpei_pen_hover_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1425 INT32 x, INT32 y, ...)
1428 va_list ap = WINPR_C_ARRAY_INIT;
1431 error = rdpei_pen_process(context, externalId,
1432 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1433 fieldFlags, x, y, ap);
1444static UINT rdpei_pen_hover_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1445 INT32 x, INT32 y, ...)
1448 va_list ap = WINPR_C_ARRAY_INIT;
1451 error = rdpei_pen_process(context, externalId,
1452 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1453 fieldFlags, x, y, ap);
1464static UINT rdpei_pen_hover_cancel(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1465 INT32 x, INT32 y, ...)
1468 va_list ap = WINPR_C_ARRAY_INIT;
1471 error = rdpei_pen_process(context, externalId,
1472 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_CANCELED,
1473 fieldFlags, x, y, ap);
1479static UINT rdpei_pen_raw_event(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1480 UINT32 fieldFlags, INT32 x, INT32 y, ...)
1483 va_list ap = WINPR_C_ARRAY_INIT;
1486 error = rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, ap);
1491static UINT rdpei_pen_raw_event_va(RdpeiClientContext* context, INT32 externalId,
1492 UINT32 contactFlags, UINT32 fieldFlags, INT32 x, INT32 y,
1495 return rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, args);
1498static UINT init_plugin_cb(
GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext, rdpSettings* settings)
1500 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1503 WINPR_UNUSED(settings);
1505 rdpei->version = RDPINPUT_PROTOCOL_V300;
1506 rdpei->currentFrameTime = 0;
1507 rdpei->previousFrameTime = 0;
1508 rdpei->maxTouchContacts = MAX_CONTACTS;
1509 rdpei->maxPenContacts = MAX_PEN_CONTACTS;
1510 rdpei->rdpcontext = rcontext;
1512 WINPR_ASSERT(rdpei->base.log);
1514 InitializeCriticalSection(&rdpei->lock);
1515 rdpei->event = CreateEventA(
nullptr, TRUE, FALSE,
nullptr);
1518 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1519 return CHANNEL_RC_NO_MEMORY;
1522 RdpeiClientContext* context = (RdpeiClientContext*)calloc(1,
sizeof(RdpeiClientContext));
1525 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1526 return CHANNEL_RC_NO_MEMORY;
1529 context->clientFeaturesMask = UINT32_MAX;
1530 context->handle = (
void*)rdpei;
1531 context->GetVersion = rdpei_get_version;
1532 context->GetFeatures = rdpei_get_features;
1533 context->AddContact = rdpei_add_contact;
1534 context->TouchBegin = rdpei_touch_begin;
1535 context->TouchUpdate = rdpei_touch_update;
1536 context->TouchEnd = rdpei_touch_end;
1537 context->TouchCancel = rdpei_touch_cancel;
1538 context->TouchRawEvent = rdpei_touch_raw_event;
1539 context->TouchRawEventVA = rdpei_touch_raw_event_va;
1540 context->AddPen = rdpei_add_pen;
1541 context->PenBegin = rdpei_pen_begin;
1542 context->PenUpdate = rdpei_pen_update;
1543 context->PenEnd = rdpei_pen_end;
1544 context->PenHoverBegin = rdpei_pen_hover_begin;
1545 context->PenHoverUpdate = rdpei_pen_hover_update;
1546 context->PenHoverCancel = rdpei_pen_hover_cancel;
1547 context->PenRawEvent = rdpei_pen_raw_event;
1548 context->PenRawEventVA = rdpei_pen_raw_event_va;
1550 rdpei->context = context;
1551 rdpei->base.iface.pInterface = (
void*)context;
1557 rdpei->running = TRUE;
1559 rdpei->thread = CreateThread(
nullptr, 0, rdpei_periodic_update, rdpei, 0,
nullptr);
1562 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1563 return CHANNEL_RC_NO_MEMORY;
1568 if (!freerdp_client_channel_register(rdpei->rdpcontext->channels, rdpei->event,
1569 rdpei_poll_run, rdpei))
1570 return ERROR_INTERNAL_ERROR;
1573 return CHANNEL_RC_OK;
1578 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1579 WINPR_ASSERT(rdpei);
1581 rdpei->running = FALSE;
1583 (void)SetEvent(rdpei->event);
1587 (void)WaitForSingleObject(rdpei->thread, INFINITE);
1588 (void)CloseHandle(rdpei->thread);
1591 if (rdpei->event && !rdpei->async)
1592 freerdp_client_channel_unregister(rdpei->rdpcontext->channels, rdpei->event);
1595 (void)CloseHandle(rdpei->event);
1597 DeleteCriticalSection(&rdpei->lock);
1598 free(rdpei->context);
1601static const IWTSVirtualChannelCallback rdpei_callbacks = { rdpei_on_data_received,
1603 rdpei_on_close,
nullptr };
1610FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpei_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1612 return freerdp_generic_DVCPluginEntry(pEntryPoints, RDPEI_TAG, RDPEI_DVC_CHANNEL_NAME,
1614 &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.