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), NULL);
239#ifdef WITH_DEBUG_RDPEI
240 WLog_Print(rdpei->base.log, WLOG_DEBUG,
241 "rdpei_send_pdu: eventId: %" PRIu16
" (%s) length: %" PRIu32
" status: %" PRIu32
"",
242 eventId, rdpei_eventid_string(eventId), pduLength, status);
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 WINPR_ASSERT(callback);
305 if (frameOffset > UINT32_MAX)
306 return ERROR_INVALID_PARAMETER;
307 if (count > UINT16_MAX)
308 return ERROR_INVALID_PARAMETER;
310 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
312 return ERROR_INTERNAL_ERROR;
314 if (!frames || (count == 0))
315 return ERROR_INTERNAL_ERROR;
317 wStream* s = Stream_New(NULL, 64);
321 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Stream_New failed!");
322 return CHANNEL_RC_NO_MEMORY;
325 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
330 rdpei_write_4byte_unsigned(s,
331 (UINT32)frameOffset);
332 rdpei_write_2byte_unsigned(s, (UINT16)count);
334 for (
size_t x = 0; x < count; x++)
336 const UINT status = rdpei_write_pen_frame(s, &frames[x]);
339 WLog_Print(rdpei->base.log, WLOG_ERROR,
340 "rdpei_write_pen_frame failed with error %" PRIu32
"!", status);
341 Stream_Free(s, TRUE);
345 Stream_SealLength(s);
347 const UINT status = rdpei_send_pdu(callback, s, EVENTID_PEN, Stream_Length(s));
348 Stream_Free(s, TRUE);
352static UINT rdpei_send_pen_frame(RdpeiClientContext* context,
RDPINPUT_PEN_FRAME* frame)
354 const UINT64 currentTime = GetTickCount64();
357 return ERROR_INTERNAL_ERROR;
359 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
360 if (!rdpei || !rdpei->base.listener_callback)
361 return ERROR_INTERNAL_ERROR;
362 if (!rdpei || !rdpei->rdpcontext)
363 return ERROR_INTERNAL_ERROR;
365 return CHANNEL_RC_OK;
370 return CHANNEL_RC_OK;
372 if (!rdpei->previousPenFrameTime && !rdpei->currentPenFrameTime)
374 rdpei->currentPenFrameTime = currentTime;
375 frame->frameOffset = 0;
379 rdpei->currentPenFrameTime = currentTime;
380 frame->frameOffset = rdpei->currentPenFrameTime - rdpei->previousPenFrameTime;
383 const size_t off = WINPR_ASSERTING_INT_CAST(
size_t, frame->frameOffset);
384 const UINT error = rdpei_send_pen_event_pdu(callback, off, frame, 1);
388 rdpei->previousPenFrameTime = rdpei->currentPenFrameTime;
392static UINT rdpei_add_pen_frame(RdpeiClientContext* context)
397 if (!context || !context->handle)
398 return ERROR_INTERNAL_ERROR;
400 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
402 penFrame.contacts = penContacts;
404 for (UINT16 i = 0; i < rdpei->maxPenContacts; i++)
410 penContacts[penFrame.contactCount++] = contact->data;
411 contact->dirty = FALSE;
413 else if (contact->active)
415 if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
417 contact->data.contactFlags = RDPINPUT_CONTACT_FLAG_UPDATE;
418 contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INRANGE;
419 contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INCONTACT;
422 penContacts[penFrame.contactCount++] = contact->data;
424 if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
426 contact->externalId = 0;
427 contact->active = FALSE;
431 if (penFrame.contactCount > 0)
432 return rdpei_send_pen_frame(context, &penFrame);
433 return CHANNEL_RC_OK;
436static UINT rdpei_update(wLog* log, RdpeiClientContext* context)
438 UINT error = rdpei_add_frame(context);
439 if (error != CHANNEL_RC_OK)
441 WLog_Print(log, WLOG_ERROR,
"rdpei_add_frame failed with error %" PRIu32
"!", error);
445 return rdpei_add_pen_frame(context);
448static BOOL rdpei_poll_run_unlocked(rdpContext* context,
void* userdata)
450 RDPEI_PLUGIN* rdpei = userdata;
452 WINPR_ASSERT(context);
454 const UINT64 now = GetTickCount64();
457 if ((now < rdpei->lastPollEventTime) || (now - rdpei->lastPollEventTime < 20ULL))
460 rdpei->lastPollEventTime = now;
462 const UINT error = rdpei_update(rdpei->base.log, rdpei->context);
464 (void)ResetEvent(rdpei->event);
466 if (error != CHANNEL_RC_OK)
468 WLog_Print(rdpei->base.log, WLOG_ERROR,
"rdpei_add_frame failed with error %" PRIu32
"!",
470 setChannelError(context, error,
"rdpei_add_frame reported an error");
477static BOOL rdpei_poll_run(rdpContext* context,
void* userdata)
479 RDPEI_PLUGIN* rdpei = userdata;
482 EnterCriticalSection(&rdpei->lock);
483 BOOL rc = rdpei_poll_run_unlocked(context, userdata);
484 LeaveCriticalSection(&rdpei->lock);
488static DWORD WINAPI rdpei_periodic_update(LPVOID arg)
490 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)arg;
491 UINT error = CHANNEL_RC_OK;
495 error = ERROR_INVALID_PARAMETER;
501 error = ERROR_INVALID_PARAMETER;
505 while (rdpei->running)
507 const DWORD status = WaitForSingleObject(rdpei->event, 20);
509 if (status == WAIT_FAILED)
511 error = GetLastError();
512 WLog_Print(rdpei->base.log, WLOG_ERROR,
513 "WaitForMultipleObjects failed with error %" PRIu32
"!", error);
517 if (!rdpei_poll_run(rdpei->rdpcontext, rdpei))
518 error = ERROR_INTERNAL_ERROR;
523 if (error && rdpei && rdpei->rdpcontext)
524 setChannelError(rdpei->rdpcontext, error,
"rdpei_schedule_thread reported an error");
527 rdpei->running = FALSE;
540 if (!callback || !callback->plugin)
541 return ERROR_INTERNAL_ERROR;
543 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
545 UINT32 flags = CS_READY_FLAGS_SHOW_TOUCH_VISUALS & rdpei->context->clientFeaturesMask;
546 if (rdpei->version > RDPINPUT_PROTOCOL_V10)
547 flags |= CS_READY_FLAGS_DISABLE_TIMESTAMP_INJECTION & rdpei->context->clientFeaturesMask;
548 if (rdpei->features & SC_READY_MULTIPEN_INJECTION_SUPPORTED)
549 flags |= CS_READY_FLAGS_ENABLE_MULTIPEN_INJECTION & rdpei->context->clientFeaturesMask;
551 UINT32 pduLength = RDPINPUT_HEADER_LENGTH + 10;
552 wStream* s = Stream_New(NULL, pduLength);
556 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Stream_New failed!");
557 return CHANNEL_RC_NO_MEMORY;
560 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
561 Stream_Write_UINT32(s, flags);
562 Stream_Write_UINT32(s, rdpei->version);
563 Stream_Write_UINT16(s, rdpei->maxTouchContacts);
564 Stream_SealLength(s);
566 const UINT status = rdpei_send_pdu(callback, s, EVENTID_CS_READY, pduLength);
567 Stream_Free(s, TRUE);
571#if defined(WITH_DEBUG_RDPEI)
572static void rdpei_print_contact_flags(wLog* log, UINT32 contactFlags)
574 if (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
575 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_DOWN");
577 if (contactFlags & RDPINPUT_CONTACT_FLAG_UPDATE)
578 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_UPDATE");
580 if (contactFlags & RDPINPUT_CONTACT_FLAG_UP)
581 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_UP");
583 if (contactFlags & RDPINPUT_CONTACT_FLAG_INRANGE)
584 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_INRANGE");
586 if (contactFlags & RDPINPUT_CONTACT_FLAG_INCONTACT)
587 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_INCONTACT");
589 if (contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
590 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_CANCELED");
594static INT16 bounded(INT32 val)
612 return ERROR_INTERNAL_ERROR;
613#ifdef WITH_DEBUG_RDPEI
614 WLog_Print(log, WLOG_DEBUG,
"contactCount: %" PRIu32
"", frame->contactCount);
615 WLog_Print(log, WLOG_DEBUG,
"frameOffset: 0x%016" PRIX64
"", frame->frameOffset);
617 rdpei_write_2byte_unsigned(s,
618 frame->contactCount);
623 rdpei_write_8byte_unsigned(s, frame->frameOffset *
626 if (!Stream_EnsureRemainingCapacity(s, (
size_t)frame->contactCount * 64))
628 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
629 return CHANNEL_RC_NO_MEMORY;
632 for (UINT32 index = 0; index < frame->contactCount; index++)
636 contact->fieldsPresent |= CONTACT_DATA_CONTACTRECT_PRESENT;
637 contact->contactRectLeft = bounded(contact->x - rectSize);
638 contact->contactRectTop = bounded(contact->y - rectSize);
639 contact->contactRectRight = bounded(contact->x + rectSize);
640 contact->contactRectBottom = bounded(contact->y + rectSize);
641#ifdef WITH_DEBUG_RDPEI
642 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].contactId: %" PRIu32
"", index,
644 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].fieldsPresent: %" PRIu32
"", index,
645 contact->fieldsPresent);
646 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].x: %" PRId32
"", index, contact->x);
647 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].y: %" PRId32
"", index, contact->y);
648 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].contactFlags: 0x%08" PRIX32
"", index,
649 contact->contactFlags);
650 rdpei_print_contact_flags(log, contact->contactFlags);
653 s, WINPR_ASSERTING_INT_CAST(uint8_t, contact->contactId));
655 rdpei_write_2byte_unsigned(s, contact->fieldsPresent);
656 rdpei_write_4byte_signed(s, contact->x);
657 rdpei_write_4byte_signed(s, contact->y);
659 rdpei_write_4byte_unsigned(s, contact->contactFlags);
661 if (contact->fieldsPresent & CONTACT_DATA_CONTACTRECT_PRESENT)
664 rdpei_write_2byte_signed(s, contact->contactRectLeft);
666 rdpei_write_2byte_signed(s, contact->contactRectTop);
668 rdpei_write_2byte_signed(s, contact->contactRectRight);
670 rdpei_write_2byte_signed(s, contact->contactRectBottom);
673 if (contact->fieldsPresent & CONTACT_DATA_ORIENTATION_PRESENT)
676 rdpei_write_4byte_unsigned(s, contact->orientation);
679 if (contact->fieldsPresent & CONTACT_DATA_PRESSURE_PRESENT)
682 rdpei_write_4byte_unsigned(s, contact->pressure);
686 return CHANNEL_RC_OK;
697 WINPR_ASSERT(callback);
699 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
700 if (!rdpei || !rdpei->rdpcontext)
701 return ERROR_INTERNAL_ERROR;
703 return CHANNEL_RC_OK;
706 return ERROR_INTERNAL_ERROR;
708 size_t pduLength = 64ULL + (64ULL * frame->contactCount);
709 wStream* s = Stream_New(NULL, pduLength);
713 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Stream_New failed!");
714 return CHANNEL_RC_NO_MEMORY;
717 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
722 rdpei_write_4byte_unsigned(
723 s, (UINT32)frame->frameOffset);
724 rdpei_write_2byte_unsigned(s, 1);
726 const UINT rc = rdpei_write_touch_frame(rdpei->base.log, s, frame);
729 WLog_Print(rdpei->base.log, WLOG_ERROR,
730 "rdpei_write_touch_frame failed with error %" PRIu32
"!", rc);
731 Stream_Free(s, TRUE);
735 Stream_SealLength(s);
737 const UINT status = rdpei_send_pdu(callback, s, EVENTID_TOUCH, Stream_Length(s));
738 Stream_Free(s, TRUE);
750 UINT32 protocolVersion = 0;
752 if (!callback || !callback->plugin)
753 return ERROR_INTERNAL_ERROR;
755 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
757 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
758 return ERROR_INVALID_DATA;
759 Stream_Read_UINT32(s, protocolVersion);
761 if (protocolVersion >= RDPINPUT_PROTOCOL_V300)
763 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
764 return ERROR_INVALID_DATA;
767 if (Stream_GetRemainingLength(s) >= 4)
768 Stream_Read_UINT32(s, features);
770 if (rdpei->version > protocolVersion)
771 rdpei->version = protocolVersion;
772 rdpei->features = features;
774 if (protocolVersion > RDPINPUT_PROTOCOL_V300)
776 WLog_Print(rdpei->base.log, WLOG_WARN,
777 "Unknown [MS-RDPEI] protocolVersion: 0x%08" PRIX32
"", protocolVersion);
780 return CHANNEL_RC_OK;
790 UINT error = CHANNEL_RC_OK;
794 if (!callback || !callback->plugin)
795 return ERROR_INTERNAL_ERROR;
797 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
798 RdpeiClientContext* context = rdpei->context;
800 return ERROR_INTERNAL_ERROR;
802 IFCALLRET(context->SuspendTouch, error, context);
805 WLog_Print(rdpei->base.log, WLOG_ERROR,
806 "rdpei->SuspendTouch failed with error %" PRIu32
"!", error);
818 UINT error = CHANNEL_RC_OK;
820 return ERROR_INTERNAL_ERROR;
822 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
824 return ERROR_INTERNAL_ERROR;
826 RdpeiClientContext* context = (RdpeiClientContext*)callback->plugin->pInterface;
828 return ERROR_INTERNAL_ERROR;
830 IFCALLRET(context->ResumeTouch, error, context);
833 WLog_Print(rdpei->base.log, WLOG_ERROR,
"rdpei->ResumeTouch failed with error %" PRIu32
"!",
847 UINT32 pduLength = 0;
851 return ERROR_INTERNAL_ERROR;
853 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
855 return ERROR_INTERNAL_ERROR;
857 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 6))
858 return ERROR_INVALID_DATA;
860 Stream_Read_UINT16(s, eventId);
861 Stream_Read_UINT32(s, pduLength);
862#ifdef WITH_DEBUG_RDPEI
863 WLog_Print(rdpei->base.log, WLOG_DEBUG,
864 "rdpei_recv_pdu: eventId: %" PRIu16
" (%s) length: %" PRIu32
"", eventId,
865 rdpei_eventid_string(eventId), pduLength);
868 if ((pduLength < 6) || !Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, pduLength - 6))
869 return ERROR_INVALID_DATA;
873 case EVENTID_SC_READY:
874 if ((error = rdpei_recv_sc_ready_pdu(callback, s)))
876 WLog_Print(rdpei->base.log, WLOG_ERROR,
877 "rdpei_recv_sc_ready_pdu failed with error %" PRIu32
"!", error);
881 if ((error = rdpei_send_cs_ready_pdu(callback)))
883 WLog_Print(rdpei->base.log, WLOG_ERROR,
884 "rdpei_send_cs_ready_pdu failed with error %" PRIu32
"!", error);
890 case EVENTID_SUSPEND_TOUCH:
891 if ((error = rdpei_recv_suspend_touch_pdu(callback, s)))
893 WLog_Print(rdpei->base.log, WLOG_ERROR,
894 "rdpei_recv_suspend_touch_pdu failed with error %" PRIu32
"!", error);
900 case EVENTID_RESUME_TOUCH:
901 if ((error = rdpei_recv_resume_touch_pdu(callback, s)))
903 WLog_Print(rdpei->base.log, WLOG_ERROR,
904 "rdpei_recv_resume_touch_pdu failed with error %" PRIu32
"!", error);
914 return CHANNEL_RC_OK;
922static UINT rdpei_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
925 return rdpei_recv_pdu(callback, data);
933static UINT rdpei_on_close(IWTSVirtualChannelCallback* pChannelCallback)
938 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
939 if (rdpei && rdpei->base.listener_callback)
941 if (rdpei->base.listener_callback->channel_callback == callback)
942 rdpei->base.listener_callback->channel_callback = NULL;
946 return CHANNEL_RC_OK;
953static UINT32 rdpei_get_version(RdpeiClientContext* context)
955 if (!context || !context->handle)
958 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
959 return rdpei->version;
962static UINT32 rdpei_get_features(RdpeiClientContext* context)
964 if (!context || !context->handle)
967 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
968 return rdpei->features;
978 UINT64 currentTime = GetTickCount64();
979 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
985 return CHANNEL_RC_OK;
987 if (!rdpei->previousFrameTime && !rdpei->currentFrameTime)
989 rdpei->currentFrameTime = currentTime;
990 frame->frameOffset = 0;
994 rdpei->currentFrameTime = currentTime;
995 frame->frameOffset = rdpei->currentFrameTime - rdpei->previousFrameTime;
998 const UINT error = rdpei_send_touch_event_pdu(callback, frame);
1001 WLog_Print(rdpei->base.log, WLOG_ERROR,
1002 "rdpei_send_touch_event_pdu failed with error %" PRIu32
"!", error);
1006 rdpei->previousFrameTime = rdpei->currentFrameTime;
1017 UINT error = CHANNEL_RC_OK;
1018 if (!context || !contact || !context->handle)
1019 return ERROR_INTERNAL_ERROR;
1021 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1023 EnterCriticalSection(&rdpei->lock);
1026 if (contactPoint->dirty && contactPoint->data.contactFlags != contact->contactFlags)
1028 const INT32 externalId = contactPoint->externalId;
1029 error = rdpei_add_frame(context);
1030 if (!contactPoint->active)
1032 contactPoint->active = TRUE;
1033 contactPoint->externalId = externalId;
1034 contactPoint->contactId = contact->contactId;
1038 contactPoint->data = *contact;
1039 contactPoint->dirty = TRUE;
1040 (void)SetEvent(rdpei->event);
1041 LeaveCriticalSection(&rdpei->lock);
1046static UINT rdpei_touch_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1047 INT32 x, INT32 y, INT32* contactId, UINT32 fieldFlags, va_list ap)
1049 INT64 contactIdlocal = -1;
1050 UINT error = CHANNEL_RC_OK;
1052 if (!context || !contactId || !context->handle)
1053 return ERROR_INTERNAL_ERROR;
1055 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1057 EnterCriticalSection(&rdpei->lock);
1058 const BOOL begin = (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN) != 0;
1061 contactIdlocal = contactPoint->contactId;
1063 if (contactIdlocal > UINT32_MAX)
1065 error = ERROR_INVALID_PARAMETER;
1069 if (contactIdlocal >= 0)
1074 contact.contactId = (UINT32)contactIdlocal;
1075 contact.contactFlags = contactFlags;
1076 contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1078 if (fieldFlags & CONTACT_DATA_CONTACTRECT_PRESENT)
1080 INT32 val = va_arg(ap, INT32);
1081 contact.contactRectLeft = WINPR_ASSERTING_INT_CAST(INT16, val);
1083 val = va_arg(ap, INT32);
1084 contact.contactRectTop = WINPR_ASSERTING_INT_CAST(INT16, val);
1086 val = va_arg(ap, INT32);
1087 contact.contactRectRight = WINPR_ASSERTING_INT_CAST(INT16, val);
1089 val = va_arg(ap, INT32);
1090 contact.contactRectBottom = WINPR_ASSERTING_INT_CAST(INT16, val);
1092 if (fieldFlags & CONTACT_DATA_ORIENTATION_PRESENT)
1094 UINT32 p = va_arg(ap, UINT32);
1097 WLog_Print(rdpei->base.log, WLOG_WARN,
1098 "TouchContact %" PRId64
": Invalid orientation value %" PRIu32
1099 "degree, clamping to 359 degree",
1103 contact.orientation = p;
1105 if (fieldFlags & CONTACT_DATA_PRESSURE_PRESENT)
1107 UINT32 p = va_arg(ap, UINT32);
1110 WLog_Print(rdpei->base.log, WLOG_WARN,
1111 "TouchContact %" PRId64
": Invalid pressure value %" PRIu32
1112 ", clamping to 1024",
1116 contact.pressure = p;
1119 error = context->AddContact(context, &contact);
1124 *contactId = (INT32)contactIdlocal;
1126 LeaveCriticalSection(&rdpei->lock);
1135static UINT rdpei_touch_begin(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1140 rc = rdpei_touch_process(context, externalId,
1141 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1142 RDPINPUT_CONTACT_FLAG_INCONTACT,
1143 x, y, contactId, 0, ap);
1152static UINT rdpei_touch_update(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1157 rc = rdpei_touch_process(context, externalId,
1158 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1159 RDPINPUT_CONTACT_FLAG_INCONTACT,
1160 x, y, contactId, 0, ap);
1169static UINT rdpei_touch_end(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1174 error = rdpei_touch_process(context, externalId,
1175 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1176 RDPINPUT_CONTACT_FLAG_INCONTACT,
1177 x, y, contactId, 0, ap);
1178 if (error != CHANNEL_RC_OK)
1181 rdpei_touch_process(context, externalId, RDPINPUT_CONTACT_FLAG_UP, x, y, contactId, 0, ap);
1190static UINT rdpei_touch_cancel(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1195 rc = rdpei_touch_process(context, externalId,
1196 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_CANCELED, x, y,
1201static UINT rdpei_touch_raw_event(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1202 INT32* contactId, UINT32 flags, UINT32 fieldFlags, ...)
1206 va_start(ap, fieldFlags);
1207 rc = rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, ap);
1212static UINT rdpei_touch_raw_event_va(RdpeiClientContext* context, INT32 externalId, INT32 x,
1213 INT32 y, INT32* contactId, UINT32 flags, UINT32 fieldFlags,
1216 return rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, args);
1225 for (UINT32 x = 0; x < rdpei->maxPenContacts; x++)
1230 if (contact->active)
1232 if (contact->externalId == externalId)
1238 if (!contact->active)
1240 contact->externalId = externalId;
1241 contact->active = TRUE;
1249static UINT rdpei_add_pen(RdpeiClientContext* context, INT32 externalId,
1252 if (!context || !contact || !context->handle)
1253 return ERROR_INTERNAL_ERROR;
1255 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1257 EnterCriticalSection(&rdpei->lock);
1262 contactPoint->data = *contact;
1263 contactPoint->dirty = TRUE;
1264 (void)SetEvent(rdpei->event);
1267 LeaveCriticalSection(&rdpei->lock);
1269 return CHANNEL_RC_OK;
1272static UINT rdpei_pen_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1273 UINT32 fieldFlags, INT32 x, INT32 y, va_list ap)
1276 UINT error = CHANNEL_RC_OK;
1278 if (!context || !context->handle)
1279 return ERROR_INTERNAL_ERROR;
1281 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1283 EnterCriticalSection(&rdpei->lock);
1285 contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1288 const UINT32 mask = RDPINPUT_CONTACT_FLAG_INRANGE;
1289 if ((contactFlags & mask) == mask)
1291 contactPoint = rdpei_pen_contact(rdpei, externalId, FALSE);
1295 if (contactPoint != NULL)
1301 contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1303 contact.contactFlags = contactFlags;
1304 if (fieldFlags & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
1306 const UINT32 val = va_arg(ap, UINT32);
1307 contact.penFlags = WINPR_ASSERTING_INT_CAST(UINT16, val);
1309 if (fieldFlags & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
1311 const UINT32 val = va_arg(ap, UINT32);
1312 contact.pressure = WINPR_ASSERTING_INT_CAST(UINT16, val);
1314 if (fieldFlags & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
1316 const UINT32 val = va_arg(ap, UINT32);
1317 contact.rotation = WINPR_ASSERTING_INT_CAST(UINT16, val);
1319 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
1321 const INT32 val = va_arg(ap, INT32);
1322 contact.tiltX = WINPR_ASSERTING_INT_CAST(INT16, val);
1324 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
1326 const INT32 val = va_arg(ap, INT32);
1327 WINPR_ASSERT((val >= INT16_MIN) && (val <= INT16_MAX));
1328 contact.tiltY = WINPR_ASSERTING_INT_CAST(INT16, val);
1331 error = context->AddPen(context, externalId, &contact);
1334 LeaveCriticalSection(&rdpei->lock);
1344static UINT rdpei_pen_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1345 INT32 x, INT32 y, ...)
1351 error = rdpei_pen_process(context, externalId,
1352 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1353 RDPINPUT_CONTACT_FLAG_INCONTACT,
1354 fieldFlags, x, y, ap);
1365static UINT rdpei_pen_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1366 INT32 x, INT32 y, ...)
1372 error = rdpei_pen_process(context, externalId,
1373 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1374 RDPINPUT_CONTACT_FLAG_INCONTACT,
1375 fieldFlags, x, y, ap);
1385static UINT rdpei_pen_end(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, INT32 x,
1391 error = rdpei_pen_process(context, externalId,
1392 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_INRANGE, fieldFlags,
1403static UINT rdpei_pen_hover_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1404 INT32 x, INT32 y, ...)
1410 error = rdpei_pen_process(context, externalId,
1411 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1412 fieldFlags, x, y, ap);
1423static UINT rdpei_pen_hover_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1424 INT32 x, INT32 y, ...)
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_cancel(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1444 INT32 x, INT32 y, ...)
1450 error = rdpei_pen_process(context, externalId,
1451 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_CANCELED,
1452 fieldFlags, x, y, ap);
1458static UINT rdpei_pen_raw_event(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1459 UINT32 fieldFlags, INT32 x, INT32 y, ...)
1465 error = rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, ap);
1470static UINT rdpei_pen_raw_event_va(RdpeiClientContext* context, INT32 externalId,
1471 UINT32 contactFlags, UINT32 fieldFlags, INT32 x, INT32 y,
1474 return rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, args);
1477static UINT init_plugin_cb(
GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext, rdpSettings* settings)
1479 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1482 WINPR_UNUSED(settings);
1484 rdpei->version = RDPINPUT_PROTOCOL_V300;
1485 rdpei->currentFrameTime = 0;
1486 rdpei->previousFrameTime = 0;
1487 rdpei->maxTouchContacts = MAX_CONTACTS;
1488 rdpei->maxPenContacts = MAX_PEN_CONTACTS;
1489 rdpei->rdpcontext = rcontext;
1491 WINPR_ASSERT(rdpei->base.log);
1493 InitializeCriticalSection(&rdpei->lock);
1494 rdpei->event = CreateEventA(NULL, TRUE, FALSE, NULL);
1497 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1498 return CHANNEL_RC_NO_MEMORY;
1501 RdpeiClientContext* context = (RdpeiClientContext*)calloc(1,
sizeof(RdpeiClientContext));
1504 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1505 return CHANNEL_RC_NO_MEMORY;
1508 context->clientFeaturesMask = UINT32_MAX;
1509 context->handle = (
void*)rdpei;
1510 context->GetVersion = rdpei_get_version;
1511 context->GetFeatures = rdpei_get_features;
1512 context->AddContact = rdpei_add_contact;
1513 context->TouchBegin = rdpei_touch_begin;
1514 context->TouchUpdate = rdpei_touch_update;
1515 context->TouchEnd = rdpei_touch_end;
1516 context->TouchCancel = rdpei_touch_cancel;
1517 context->TouchRawEvent = rdpei_touch_raw_event;
1518 context->TouchRawEventVA = rdpei_touch_raw_event_va;
1519 context->AddPen = rdpei_add_pen;
1520 context->PenBegin = rdpei_pen_begin;
1521 context->PenUpdate = rdpei_pen_update;
1522 context->PenEnd = rdpei_pen_end;
1523 context->PenHoverBegin = rdpei_pen_hover_begin;
1524 context->PenHoverUpdate = rdpei_pen_hover_update;
1525 context->PenHoverCancel = rdpei_pen_hover_cancel;
1526 context->PenRawEvent = rdpei_pen_raw_event;
1527 context->PenRawEventVA = rdpei_pen_raw_event_va;
1529 rdpei->context = context;
1530 rdpei->base.iface.pInterface = (
void*)context;
1536 rdpei->running = TRUE;
1538 rdpei->thread = CreateThread(NULL, 0, rdpei_periodic_update, rdpei, 0, NULL);
1541 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1542 return CHANNEL_RC_NO_MEMORY;
1547 if (!freerdp_client_channel_register(rdpei->rdpcontext->channels, rdpei->event,
1548 rdpei_poll_run, rdpei))
1549 return ERROR_INTERNAL_ERROR;
1552 return CHANNEL_RC_OK;
1557 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1558 WINPR_ASSERT(rdpei);
1560 rdpei->running = FALSE;
1562 (void)SetEvent(rdpei->event);
1566 (void)WaitForSingleObject(rdpei->thread, INFINITE);
1567 (void)CloseHandle(rdpei->thread);
1570 if (rdpei->event && !rdpei->async)
1571 (void)freerdp_client_channel_unregister(rdpei->rdpcontext->channels, rdpei->event);
1574 (void)CloseHandle(rdpei->event);
1576 DeleteCriticalSection(&rdpei->lock);
1577 free(rdpei->context);
1580static const IWTSVirtualChannelCallback rdpei_callbacks = { rdpei_on_data_received, NULL,
1581 rdpei_on_close, NULL };
1588FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpei_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1590 return freerdp_generic_DVCPluginEntry(pEntryPoints, RDPEI_TAG, RDPEI_DVC_CHANNEL_NAME,
1592 &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.