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_SetPosition(s, 0);
234 Stream_Write_UINT16(s, eventId);
235 Stream_Write_UINT32(s, (UINT32)pduLength);
236 Stream_SetPosition(s, Stream_Length(s));
237 const UINT status = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s),
238 Stream_Buffer(s),
nullptr);
239#ifdef WITH_DEBUG_RDPEI
240 WLog_Print(rdpei->base.log, WLOG_DEBUG,
241 "rdpei_send_pdu: eventId: %" PRIu16
" (%s) length: %" PRIu32
" status: %" PRIu32
"",
242 eventId, rdpei_eventid_string(eventId), pduLength, status);
250 return ERROR_INTERNAL_ERROR;
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++)
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)
273 if (!rdpei_write_4byte_unsigned(s, contact->penFlags))
274 return ERROR_OUTOFMEMORY;
276 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
278 if (!rdpei_write_4byte_unsigned(s, contact->pressure))
279 return ERROR_OUTOFMEMORY;
281 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
283 if (!rdpei_write_2byte_unsigned(s, contact->rotation))
284 return ERROR_OUTOFMEMORY;
286 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
288 if (!rdpei_write_2byte_signed(s, contact->tiltX))
289 return ERROR_OUTOFMEMORY;
291 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
293 if (!rdpei_write_2byte_signed(s, contact->tiltY))
294 return ERROR_OUTOFMEMORY;
297 return CHANNEL_RC_OK;
303 UINT status = ERROR_OUTOFMEMORY;
304 WINPR_ASSERT(callback);
306 if (frameOffset > UINT32_MAX)
307 return ERROR_INVALID_PARAMETER;
308 if (count > UINT16_MAX)
309 return ERROR_INVALID_PARAMETER;
311 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
313 return ERROR_INTERNAL_ERROR;
315 if (!frames || (count == 0))
316 return ERROR_INTERNAL_ERROR;
318 wStream* s = Stream_New(
nullptr, 64);
322 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Stream_New failed!");
323 return CHANNEL_RC_NO_MEMORY;
326 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
331 if (!rdpei_write_4byte_unsigned(
332 s, (UINT32)frameOffset))
334 if (!rdpei_write_2byte_unsigned(s, (UINT16)count))
337 for (
size_t x = 0; x < count; x++)
339 status = rdpei_write_pen_frame(s, &frames[x]);
342 WLog_Print(rdpei->base.log, WLOG_ERROR,
343 "rdpei_write_pen_frame failed with error %" PRIu32
"!", status);
347 Stream_SealLength(s);
349 status = rdpei_send_pdu(callback, s, EVENTID_PEN, Stream_Length(s));
351 Stream_Free(s, TRUE);
355static UINT rdpei_send_pen_frame(RdpeiClientContext* context,
RDPINPUT_PEN_FRAME* frame)
357 const UINT64 currentTime = GetTickCount64();
360 return ERROR_INTERNAL_ERROR;
362 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
363 if (!rdpei || !rdpei->base.listener_callback)
364 return ERROR_INTERNAL_ERROR;
365 if (!rdpei || !rdpei->rdpcontext)
366 return ERROR_INTERNAL_ERROR;
368 return CHANNEL_RC_OK;
373 return CHANNEL_RC_OK;
375 if (!rdpei->previousPenFrameTime && !rdpei->currentPenFrameTime)
377 rdpei->currentPenFrameTime = currentTime;
378 frame->frameOffset = 0;
382 rdpei->currentPenFrameTime = currentTime;
383 frame->frameOffset = rdpei->currentPenFrameTime - rdpei->previousPenFrameTime;
386 const size_t off = WINPR_ASSERTING_INT_CAST(
size_t, frame->frameOffset);
387 const UINT error = rdpei_send_pen_event_pdu(callback, off, frame, 1);
391 rdpei->previousPenFrameTime = rdpei->currentPenFrameTime;
395static UINT rdpei_add_pen_frame(RdpeiClientContext* context)
400 if (!context || !context->handle)
401 return ERROR_INTERNAL_ERROR;
403 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
405 penFrame.contacts = penContacts;
407 for (UINT16 i = 0; i < rdpei->maxPenContacts; i++)
413 penContacts[penFrame.contactCount++] = contact->data;
414 contact->dirty = FALSE;
416 else if (contact->active)
418 if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
420 contact->data.contactFlags = RDPINPUT_CONTACT_FLAG_UPDATE;
421 contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INRANGE;
422 contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INCONTACT;
425 penContacts[penFrame.contactCount++] = contact->data;
427 if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
429 contact->externalId = 0;
430 contact->active = FALSE;
434 if (penFrame.contactCount > 0)
435 return rdpei_send_pen_frame(context, &penFrame);
436 return CHANNEL_RC_OK;
439static UINT rdpei_update(wLog* log, RdpeiClientContext* context)
441 UINT error = rdpei_add_frame(context);
442 if (error != CHANNEL_RC_OK)
444 WLog_Print(log, WLOG_ERROR,
"rdpei_add_frame failed with error %" PRIu32
"!", error);
448 return rdpei_add_pen_frame(context);
451static BOOL rdpei_poll_run_unlocked(rdpContext* context,
void* userdata)
453 RDPEI_PLUGIN* rdpei = userdata;
455 WINPR_ASSERT(context);
457 const UINT64 now = GetTickCount64();
460 if ((now < rdpei->lastPollEventTime) || (now - rdpei->lastPollEventTime < 20ULL))
463 rdpei->lastPollEventTime = now;
465 const UINT error = rdpei_update(rdpei->base.log, rdpei->context);
467 (void)ResetEvent(rdpei->event);
469 if (error != CHANNEL_RC_OK)
471 WLog_Print(rdpei->base.log, WLOG_ERROR,
"rdpei_add_frame failed with error %" PRIu32
"!",
473 setChannelError(context, error,
"rdpei_add_frame reported an error");
480static BOOL rdpei_poll_run(rdpContext* context,
void* userdata)
482 RDPEI_PLUGIN* rdpei = userdata;
485 EnterCriticalSection(&rdpei->lock);
486 BOOL rc = rdpei_poll_run_unlocked(context, userdata);
487 LeaveCriticalSection(&rdpei->lock);
491static DWORD WINAPI rdpei_periodic_update(LPVOID arg)
493 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)arg;
494 UINT error = CHANNEL_RC_OK;
498 error = ERROR_INVALID_PARAMETER;
504 error = ERROR_INVALID_PARAMETER;
508 while (rdpei->running)
510 const DWORD status = WaitForSingleObject(rdpei->event, 20);
512 if (status == WAIT_FAILED)
514 error = GetLastError();
515 WLog_Print(rdpei->base.log, WLOG_ERROR,
516 "WaitForMultipleObjects failed with error %" PRIu32
"!", error);
520 if (!rdpei_poll_run(rdpei->rdpcontext, rdpei))
521 error = ERROR_INTERNAL_ERROR;
526 if (error && rdpei && rdpei->rdpcontext)
527 setChannelError(rdpei->rdpcontext, error,
"rdpei_schedule_thread reported an error");
530 rdpei->running = FALSE;
543 if (!callback || !callback->plugin)
544 return ERROR_INTERNAL_ERROR;
546 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
548 UINT32 flags = CS_READY_FLAGS_SHOW_TOUCH_VISUALS & rdpei->context->clientFeaturesMask;
549 if (rdpei->version > RDPINPUT_PROTOCOL_V10)
550 flags |= CS_READY_FLAGS_DISABLE_TIMESTAMP_INJECTION & rdpei->context->clientFeaturesMask;
551 if (rdpei->features & SC_READY_MULTIPEN_INJECTION_SUPPORTED)
552 flags |= CS_READY_FLAGS_ENABLE_MULTIPEN_INJECTION & rdpei->context->clientFeaturesMask;
554 UINT32 pduLength = RDPINPUT_HEADER_LENGTH + 10;
555 wStream* s = Stream_New(
nullptr, pduLength);
559 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Stream_New failed!");
560 return CHANNEL_RC_NO_MEMORY;
563 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
564 Stream_Write_UINT32(s, flags);
565 Stream_Write_UINT32(s, rdpei->version);
566 Stream_Write_UINT16(s, rdpei->maxTouchContacts);
567 Stream_SealLength(s);
569 const UINT status = rdpei_send_pdu(callback, s, EVENTID_CS_READY, pduLength);
570 Stream_Free(s, TRUE);
574#if defined(WITH_DEBUG_RDPEI)
575static void rdpei_print_contact_flags(wLog* log, UINT32 contactFlags)
577 if (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
578 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_DOWN");
580 if (contactFlags & RDPINPUT_CONTACT_FLAG_UPDATE)
581 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_UPDATE");
583 if (contactFlags & RDPINPUT_CONTACT_FLAG_UP)
584 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_UP");
586 if (contactFlags & RDPINPUT_CONTACT_FLAG_INRANGE)
587 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_INRANGE");
589 if (contactFlags & RDPINPUT_CONTACT_FLAG_INCONTACT)
590 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_INCONTACT");
592 if (contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
593 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_CANCELED");
597static INT16 bounded(INT32 val)
615 return ERROR_INTERNAL_ERROR;
616#ifdef WITH_DEBUG_RDPEI
617 WLog_Print(log, WLOG_DEBUG,
"contactCount: %" PRIu32
"", frame->contactCount);
618 WLog_Print(log, WLOG_DEBUG,
"frameOffset: 0x%016" PRIX64
"", frame->frameOffset);
620 if (!rdpei_write_2byte_unsigned(
621 s, frame->contactCount))
622 return ERROR_OUTOFMEMORY;
627 if (!rdpei_write_8byte_unsigned(s, frame->frameOffset *
629 return ERROR_OUTOFMEMORY;
631 if (!Stream_EnsureRemainingCapacity(s, (
size_t)frame->contactCount * 64))
633 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
634 return CHANNEL_RC_NO_MEMORY;
637 for (UINT32 index = 0; index < frame->contactCount; index++)
641 contact->fieldsPresent |= CONTACT_DATA_CONTACTRECT_PRESENT;
642 contact->contactRectLeft = bounded(contact->x - rectSize);
643 contact->contactRectTop = bounded(contact->y - rectSize);
644 contact->contactRectRight = bounded(contact->x + rectSize);
645 contact->contactRectBottom = bounded(contact->y + rectSize);
646#ifdef WITH_DEBUG_RDPEI
647 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].contactId: %" PRIu32
"", index,
649 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].fieldsPresent: %" PRIu32
"", index,
650 contact->fieldsPresent);
651 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].x: %" PRId32
"", index, contact->x);
652 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].y: %" PRId32
"", index, contact->y);
653 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].contactFlags: 0x%08" PRIX32
"", index,
654 contact->contactFlags);
655 rdpei_print_contact_flags(log, contact->contactFlags);
658 s, WINPR_ASSERTING_INT_CAST(uint8_t, contact->contactId));
660 if (!rdpei_write_2byte_unsigned(s, contact->fieldsPresent))
661 return ERROR_OUTOFMEMORY;
662 if (!rdpei_write_4byte_signed(s, contact->x))
663 return ERROR_OUTOFMEMORY;
664 if (!rdpei_write_4byte_signed(s, contact->y))
665 return ERROR_OUTOFMEMORY;
667 if (!rdpei_write_4byte_unsigned(s, contact->contactFlags))
668 return ERROR_OUTOFMEMORY;
670 if (contact->fieldsPresent & CONTACT_DATA_CONTACTRECT_PRESENT)
673 if (!rdpei_write_2byte_signed(s, contact->contactRectLeft))
674 return ERROR_OUTOFMEMORY;
676 if (!rdpei_write_2byte_signed(s, contact->contactRectTop))
677 return ERROR_OUTOFMEMORY;
679 if (!rdpei_write_2byte_signed(s, contact->contactRectRight))
680 return ERROR_OUTOFMEMORY;
682 if (!rdpei_write_2byte_signed(s, contact->contactRectBottom))
683 return ERROR_OUTOFMEMORY;
686 if (contact->fieldsPresent & CONTACT_DATA_ORIENTATION_PRESENT)
689 if (!rdpei_write_4byte_unsigned(s, contact->orientation))
690 return ERROR_OUTOFMEMORY;
693 if (contact->fieldsPresent & CONTACT_DATA_PRESSURE_PRESENT)
696 if (!rdpei_write_4byte_unsigned(s, contact->pressure))
697 return ERROR_OUTOFMEMORY;
701 return CHANNEL_RC_OK;
712 UINT status = ERROR_OUTOFMEMORY;
713 WINPR_ASSERT(callback);
715 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
716 if (!rdpei || !rdpei->rdpcontext)
717 return ERROR_INTERNAL_ERROR;
719 return CHANNEL_RC_OK;
722 return ERROR_INTERNAL_ERROR;
724 size_t pduLength = 64ULL + (64ULL * frame->contactCount);
725 wStream* s = Stream_New(
nullptr, pduLength);
729 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Stream_New failed!");
730 return CHANNEL_RC_NO_MEMORY;
733 if (!Stream_SafeSeek(s, RDPINPUT_HEADER_LENGTH))
739 if (!rdpei_write_4byte_unsigned(
740 s, (UINT32)frame->frameOffset))
742 if (!rdpei_write_2byte_unsigned(s, 1))
745 const UINT rc = rdpei_write_touch_frame(rdpei->base.log, s, frame);
748 WLog_Print(rdpei->base.log, WLOG_ERROR,
749 "rdpei_write_touch_frame failed with error %" PRIu32
"!", rc);
754 Stream_SealLength(s);
756 status = rdpei_send_pdu(callback, s, EVENTID_TOUCH, Stream_Length(s));
758 Stream_Free(s, TRUE);
770 UINT32 protocolVersion = 0;
772 if (!callback || !callback->plugin)
773 return ERROR_INTERNAL_ERROR;
775 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
777 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
778 return ERROR_INVALID_DATA;
779 Stream_Read_UINT32(s, protocolVersion);
781 if (protocolVersion >= RDPINPUT_PROTOCOL_V300)
783 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
784 return ERROR_INVALID_DATA;
787 if (Stream_GetRemainingLength(s) >= 4)
788 Stream_Read_UINT32(s, features);
790 if (rdpei->version > protocolVersion)
791 rdpei->version = protocolVersion;
792 rdpei->features = features;
794 if (protocolVersion > RDPINPUT_PROTOCOL_V300)
796 WLog_Print(rdpei->base.log, WLOG_WARN,
797 "Unknown [MS-RDPEI] protocolVersion: 0x%08" PRIX32
"", protocolVersion);
800 return CHANNEL_RC_OK;
810 UINT error = CHANNEL_RC_OK;
814 if (!callback || !callback->plugin)
815 return ERROR_INTERNAL_ERROR;
817 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
818 RdpeiClientContext* context = rdpei->context;
820 return ERROR_INTERNAL_ERROR;
822 IFCALLRET(context->SuspendTouch, error, context);
825 WLog_Print(rdpei->base.log, WLOG_ERROR,
826 "rdpei->SuspendTouch failed with error %" PRIu32
"!", error);
838 UINT error = CHANNEL_RC_OK;
840 return ERROR_INTERNAL_ERROR;
842 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
844 return ERROR_INTERNAL_ERROR;
846 RdpeiClientContext* context = (RdpeiClientContext*)callback->plugin->pInterface;
848 return ERROR_INTERNAL_ERROR;
850 IFCALLRET(context->ResumeTouch, error, context);
853 WLog_Print(rdpei->base.log, WLOG_ERROR,
"rdpei->ResumeTouch failed with error %" PRIu32
"!",
867 UINT32 pduLength = 0;
871 return ERROR_INTERNAL_ERROR;
873 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
875 return ERROR_INTERNAL_ERROR;
877 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 6))
878 return ERROR_INVALID_DATA;
880 Stream_Read_UINT16(s, eventId);
881 Stream_Read_UINT32(s, pduLength);
882#ifdef WITH_DEBUG_RDPEI
883 WLog_Print(rdpei->base.log, WLOG_DEBUG,
884 "rdpei_recv_pdu: eventId: %" PRIu16
" (%s) length: %" PRIu32
"", eventId,
885 rdpei_eventid_string(eventId), pduLength);
888 if ((pduLength < 6) || !Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, pduLength - 6))
889 return ERROR_INVALID_DATA;
893 case EVENTID_SC_READY:
894 if ((error = rdpei_recv_sc_ready_pdu(callback, s)))
896 WLog_Print(rdpei->base.log, WLOG_ERROR,
897 "rdpei_recv_sc_ready_pdu failed with error %" PRIu32
"!", error);
901 if ((error = rdpei_send_cs_ready_pdu(callback)))
903 WLog_Print(rdpei->base.log, WLOG_ERROR,
904 "rdpei_send_cs_ready_pdu failed with error %" PRIu32
"!", error);
910 case EVENTID_SUSPEND_TOUCH:
911 if ((error = rdpei_recv_suspend_touch_pdu(callback, s)))
913 WLog_Print(rdpei->base.log, WLOG_ERROR,
914 "rdpei_recv_suspend_touch_pdu failed with error %" PRIu32
"!", error);
920 case EVENTID_RESUME_TOUCH:
921 if ((error = rdpei_recv_resume_touch_pdu(callback, s)))
923 WLog_Print(rdpei->base.log, WLOG_ERROR,
924 "rdpei_recv_resume_touch_pdu failed with error %" PRIu32
"!", error);
934 return CHANNEL_RC_OK;
942static UINT rdpei_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
945 return rdpei_recv_pdu(callback, data);
953static UINT rdpei_on_close(IWTSVirtualChannelCallback* pChannelCallback)
958 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
959 if (rdpei && rdpei->base.listener_callback)
961 if (rdpei->base.listener_callback->channel_callback == callback)
962 rdpei->base.listener_callback->channel_callback =
nullptr;
966 return CHANNEL_RC_OK;
973static UINT32 rdpei_get_version(RdpeiClientContext* context)
975 if (!context || !context->handle)
978 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
979 return rdpei->version;
982static UINT32 rdpei_get_features(RdpeiClientContext* context)
984 if (!context || !context->handle)
987 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
988 return rdpei->features;
998 UINT64 currentTime = GetTickCount64();
999 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1005 return CHANNEL_RC_OK;
1007 if (!rdpei->previousFrameTime && !rdpei->currentFrameTime)
1009 rdpei->currentFrameTime = currentTime;
1010 frame->frameOffset = 0;
1014 rdpei->currentFrameTime = currentTime;
1015 frame->frameOffset = rdpei->currentFrameTime - rdpei->previousFrameTime;
1018 const UINT error = rdpei_send_touch_event_pdu(callback, frame);
1021 WLog_Print(rdpei->base.log, WLOG_ERROR,
1022 "rdpei_send_touch_event_pdu failed with error %" PRIu32
"!", error);
1026 rdpei->previousFrameTime = rdpei->currentFrameTime;
1037 UINT error = CHANNEL_RC_OK;
1038 if (!context || !contact || !context->handle)
1039 return ERROR_INTERNAL_ERROR;
1041 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1043 EnterCriticalSection(&rdpei->lock);
1046 if (contactPoint->dirty && contactPoint->data.contactFlags != contact->contactFlags)
1048 const INT32 externalId = contactPoint->externalId;
1049 error = rdpei_add_frame(context);
1050 if (!contactPoint->active)
1052 contactPoint->active = TRUE;
1053 contactPoint->externalId = externalId;
1054 contactPoint->contactId = contact->contactId;
1058 contactPoint->data = *contact;
1059 contactPoint->dirty = TRUE;
1060 (void)SetEvent(rdpei->event);
1061 LeaveCriticalSection(&rdpei->lock);
1066static UINT rdpei_touch_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1067 INT32 x, INT32 y, INT32* contactId, UINT32 fieldFlags, va_list ap)
1069 INT64 contactIdlocal = -1;
1070 UINT error = CHANNEL_RC_OK;
1072 if (!context || !contactId || !context->handle)
1073 return ERROR_INTERNAL_ERROR;
1075 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1077 EnterCriticalSection(&rdpei->lock);
1078 const BOOL begin = (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN) != 0;
1081 contactIdlocal = contactPoint->contactId;
1083 if (contactIdlocal > UINT32_MAX)
1085 error = ERROR_INVALID_PARAMETER;
1089 if (contactIdlocal >= 0)
1094 contact.contactId = (UINT32)contactIdlocal;
1095 contact.contactFlags = contactFlags;
1096 contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1098 if (fieldFlags & CONTACT_DATA_CONTACTRECT_PRESENT)
1100 INT32 val = va_arg(ap, INT32);
1101 contact.contactRectLeft = WINPR_ASSERTING_INT_CAST(INT16, val);
1103 val = va_arg(ap, INT32);
1104 contact.contactRectTop = WINPR_ASSERTING_INT_CAST(INT16, val);
1106 val = va_arg(ap, INT32);
1107 contact.contactRectRight = WINPR_ASSERTING_INT_CAST(INT16, val);
1109 val = va_arg(ap, INT32);
1110 contact.contactRectBottom = WINPR_ASSERTING_INT_CAST(INT16, val);
1112 if (fieldFlags & CONTACT_DATA_ORIENTATION_PRESENT)
1114 UINT32 p = va_arg(ap, UINT32);
1117 WLog_Print(rdpei->base.log, WLOG_WARN,
1118 "TouchContact %" PRId64
": Invalid orientation value %" PRIu32
1119 "degree, clamping to 359 degree",
1123 contact.orientation = p;
1125 if (fieldFlags & CONTACT_DATA_PRESSURE_PRESENT)
1127 UINT32 p = va_arg(ap, UINT32);
1130 WLog_Print(rdpei->base.log, WLOG_WARN,
1131 "TouchContact %" PRId64
": Invalid pressure value %" PRIu32
1132 ", clamping to 1024",
1136 contact.pressure = p;
1139 error = context->AddContact(context, &contact);
1144 *contactId = (INT32)contactIdlocal;
1146 LeaveCriticalSection(&rdpei->lock);
1155static UINT rdpei_touch_begin(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1159 va_list ap = WINPR_C_ARRAY_INIT;
1160 rc = rdpei_touch_process(context, externalId,
1161 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1162 RDPINPUT_CONTACT_FLAG_INCONTACT,
1163 x, y, contactId, 0, ap);
1172static UINT rdpei_touch_update(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1176 va_list ap = WINPR_C_ARRAY_INIT;
1177 rc = rdpei_touch_process(context, externalId,
1178 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1179 RDPINPUT_CONTACT_FLAG_INCONTACT,
1180 x, y, contactId, 0, ap);
1189static UINT rdpei_touch_end(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1193 va_list ap = WINPR_C_ARRAY_INIT;
1194 error = rdpei_touch_process(context, externalId,
1195 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1196 RDPINPUT_CONTACT_FLAG_INCONTACT,
1197 x, y, contactId, 0, ap);
1198 if (error != CHANNEL_RC_OK)
1201 rdpei_touch_process(context, externalId, RDPINPUT_CONTACT_FLAG_UP, x, y, contactId, 0, ap);
1210static UINT rdpei_touch_cancel(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1214 va_list ap = WINPR_C_ARRAY_INIT;
1215 rc = rdpei_touch_process(context, externalId,
1216 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_CANCELED, x, y,
1221static UINT rdpei_touch_raw_event(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1222 INT32* contactId, UINT32 flags, UINT32 fieldFlags, ...)
1225 va_list ap = WINPR_C_ARRAY_INIT;
1226 va_start(ap, fieldFlags);
1227 rc = rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, ap);
1232static UINT rdpei_touch_raw_event_va(RdpeiClientContext* context, INT32 externalId, INT32 x,
1233 INT32 y, INT32* contactId, UINT32 flags, UINT32 fieldFlags,
1236 return rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, args);
1245 for (UINT32 x = 0; x < rdpei->maxPenContacts; x++)
1250 if (contact->active)
1252 if (contact->externalId == externalId)
1258 if (!contact->active)
1260 contact->externalId = externalId;
1261 contact->active = TRUE;
1269static UINT rdpei_add_pen(RdpeiClientContext* context, INT32 externalId,
1272 if (!context || !contact || !context->handle)
1273 return ERROR_INTERNAL_ERROR;
1275 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1277 EnterCriticalSection(&rdpei->lock);
1282 contactPoint->data = *contact;
1283 contactPoint->dirty = TRUE;
1284 (void)SetEvent(rdpei->event);
1287 LeaveCriticalSection(&rdpei->lock);
1289 return CHANNEL_RC_OK;
1292static UINT rdpei_pen_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1293 UINT32 fieldFlags, INT32 x, INT32 y, va_list ap)
1296 UINT error = CHANNEL_RC_OK;
1298 if (!context || !context->handle)
1299 return ERROR_INTERNAL_ERROR;
1301 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1303 EnterCriticalSection(&rdpei->lock);
1305 contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1308 const UINT32 mask = RDPINPUT_CONTACT_FLAG_INRANGE;
1309 if ((contactFlags & mask) == mask)
1311 contactPoint = rdpei_pen_contact(rdpei, externalId, FALSE);
1315 if (contactPoint !=
nullptr)
1321 contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1323 contact.contactFlags = contactFlags;
1324 if (fieldFlags & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
1326 const UINT32 val = va_arg(ap, UINT32);
1327 contact.penFlags = WINPR_ASSERTING_INT_CAST(UINT16, val);
1329 if (fieldFlags & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
1331 const UINT32 val = va_arg(ap, UINT32);
1332 contact.pressure = WINPR_ASSERTING_INT_CAST(UINT16, val);
1334 if (fieldFlags & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
1336 const UINT32 val = va_arg(ap, UINT32);
1337 contact.rotation = WINPR_ASSERTING_INT_CAST(UINT16, val);
1339 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
1341 const INT32 val = va_arg(ap, INT32);
1342 contact.tiltX = WINPR_ASSERTING_INT_CAST(INT16, val);
1344 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
1346 const INT32 val = va_arg(ap, INT32);
1347 WINPR_ASSERT((val >= INT16_MIN) && (val <= INT16_MAX));
1348 contact.tiltY = WINPR_ASSERTING_INT_CAST(INT16, val);
1351 error = context->AddPen(context, externalId, &contact);
1354 LeaveCriticalSection(&rdpei->lock);
1364static UINT rdpei_pen_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1365 INT32 x, INT32 y, ...)
1368 va_list ap = WINPR_C_ARRAY_INIT;
1371 error = rdpei_pen_process(context, externalId,
1372 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1373 RDPINPUT_CONTACT_FLAG_INCONTACT,
1374 fieldFlags, x, y, ap);
1385static UINT rdpei_pen_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1386 INT32 x, INT32 y, ...)
1389 va_list ap = WINPR_C_ARRAY_INIT;
1392 error = rdpei_pen_process(context, externalId,
1393 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1394 RDPINPUT_CONTACT_FLAG_INCONTACT,
1395 fieldFlags, x, y, ap);
1405static UINT rdpei_pen_end(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, INT32 x,
1409 va_list ap = WINPR_C_ARRAY_INIT;
1411 error = rdpei_pen_process(context, externalId,
1412 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_INRANGE, fieldFlags,
1423static UINT rdpei_pen_hover_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1424 INT32 x, INT32 y, ...)
1427 va_list ap = WINPR_C_ARRAY_INIT;
1430 error = rdpei_pen_process(context, externalId,
1431 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1432 fieldFlags, x, y, ap);
1443static UINT rdpei_pen_hover_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1444 INT32 x, INT32 y, ...)
1447 va_list ap = WINPR_C_ARRAY_INIT;
1450 error = rdpei_pen_process(context, externalId,
1451 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1452 fieldFlags, x, y, ap);
1463static UINT rdpei_pen_hover_cancel(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1464 INT32 x, INT32 y, ...)
1467 va_list ap = WINPR_C_ARRAY_INIT;
1470 error = rdpei_pen_process(context, externalId,
1471 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_CANCELED,
1472 fieldFlags, x, y, ap);
1478static UINT rdpei_pen_raw_event(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1479 UINT32 fieldFlags, INT32 x, INT32 y, ...)
1482 va_list ap = WINPR_C_ARRAY_INIT;
1485 error = rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, ap);
1490static UINT rdpei_pen_raw_event_va(RdpeiClientContext* context, INT32 externalId,
1491 UINT32 contactFlags, UINT32 fieldFlags, INT32 x, INT32 y,
1494 return rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, args);
1497static UINT init_plugin_cb(
GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext, rdpSettings* settings)
1499 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1502 WINPR_UNUSED(settings);
1504 rdpei->version = RDPINPUT_PROTOCOL_V300;
1505 rdpei->currentFrameTime = 0;
1506 rdpei->previousFrameTime = 0;
1507 rdpei->maxTouchContacts = MAX_CONTACTS;
1508 rdpei->maxPenContacts = MAX_PEN_CONTACTS;
1509 rdpei->rdpcontext = rcontext;
1511 WINPR_ASSERT(rdpei->base.log);
1513 InitializeCriticalSection(&rdpei->lock);
1514 rdpei->event = CreateEventA(
nullptr, TRUE, FALSE,
nullptr);
1517 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1518 return CHANNEL_RC_NO_MEMORY;
1521 RdpeiClientContext* context = (RdpeiClientContext*)calloc(1,
sizeof(RdpeiClientContext));
1524 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1525 return CHANNEL_RC_NO_MEMORY;
1528 context->clientFeaturesMask = UINT32_MAX;
1529 context->handle = (
void*)rdpei;
1530 context->GetVersion = rdpei_get_version;
1531 context->GetFeatures = rdpei_get_features;
1532 context->AddContact = rdpei_add_contact;
1533 context->TouchBegin = rdpei_touch_begin;
1534 context->TouchUpdate = rdpei_touch_update;
1535 context->TouchEnd = rdpei_touch_end;
1536 context->TouchCancel = rdpei_touch_cancel;
1537 context->TouchRawEvent = rdpei_touch_raw_event;
1538 context->TouchRawEventVA = rdpei_touch_raw_event_va;
1539 context->AddPen = rdpei_add_pen;
1540 context->PenBegin = rdpei_pen_begin;
1541 context->PenUpdate = rdpei_pen_update;
1542 context->PenEnd = rdpei_pen_end;
1543 context->PenHoverBegin = rdpei_pen_hover_begin;
1544 context->PenHoverUpdate = rdpei_pen_hover_update;
1545 context->PenHoverCancel = rdpei_pen_hover_cancel;
1546 context->PenRawEvent = rdpei_pen_raw_event;
1547 context->PenRawEventVA = rdpei_pen_raw_event_va;
1549 rdpei->context = context;
1550 rdpei->base.iface.pInterface = (
void*)context;
1556 rdpei->running = TRUE;
1558 rdpei->thread = CreateThread(
nullptr, 0, rdpei_periodic_update, rdpei, 0,
nullptr);
1561 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1562 return CHANNEL_RC_NO_MEMORY;
1567 if (!freerdp_client_channel_register(rdpei->rdpcontext->channels, rdpei->event,
1568 rdpei_poll_run, rdpei))
1569 return ERROR_INTERNAL_ERROR;
1572 return CHANNEL_RC_OK;
1577 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1578 WINPR_ASSERT(rdpei);
1580 rdpei->running = FALSE;
1582 (void)SetEvent(rdpei->event);
1586 (void)WaitForSingleObject(rdpei->thread, INFINITE);
1587 (void)CloseHandle(rdpei->thread);
1590 if (rdpei->event && !rdpei->async)
1591 freerdp_client_channel_unregister(rdpei->rdpcontext->channels, rdpei->event);
1594 (void)CloseHandle(rdpei->event);
1596 DeleteCriticalSection(&rdpei->lock);
1597 free(rdpei->context);
1600static const IWTSVirtualChannelCallback rdpei_callbacks = { rdpei_on_data_received,
1602 rdpei_on_close,
nullptr };
1609FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpei_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1611 return freerdp_generic_DVCPluginEntry(pEntryPoints, RDPEI_TAG, RDPEI_DVC_CHANNEL_NAME,
1613 &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.