22 #include <freerdp/config.h>
29 #include <winpr/crt.h>
30 #include <winpr/synch.h>
31 #include <winpr/thread.h>
32 #include <winpr/stream.h>
33 #include <winpr/sysinfo.h>
34 #include <winpr/cmdline.h>
35 #include <winpr/collections.h>
37 #include <freerdp/addin.h>
38 #include <freerdp/freerdp.h>
39 #include <freerdp/client/channels.h>
41 #include "rdpei_common.h"
43 #include "rdpei_main.h"
45 #define RDPEI_TAG CHANNELS_TAG("rdpei.client")
67 #define MAX_CONTACTS 64
68 #define MAX_PEN_CONTACTS 4
74 RdpeiClientContext* context;
78 UINT16 maxTouchContacts;
79 UINT64 currentFrameTime;
80 UINT64 previousFrameTime;
83 UINT64 currentPenFrameTime;
84 UINT64 previousPenFrameTime;
85 UINT16 maxPenContacts;
89 rdpContext* rdpcontext;
94 UINT64 lastPollEventTime;
106 #ifdef WITH_DEBUG_RDPEI
107 static const char* rdpei_eventid_string(UINT16 event)
111 case EVENTID_SC_READY:
112 return "EVENTID_SC_READY";
113 case EVENTID_CS_READY:
114 return "EVENTID_CS_READY";
116 return "EVENTID_TOUCH";
117 case EVENTID_SUSPEND_TOUCH:
118 return "EVENTID_SUSPEND_TOUCH";
119 case EVENTID_RESUME_TOUCH:
120 return "EVENTID_RESUME_TOUCH";
121 case EVENTID_DISMISS_HOVERING_CONTACT:
122 return "EVENTID_DISMISS_HOVERING_CONTACT";
124 return "EVENTID_PEN";
126 return "EVENTID_UNKNOWN";
133 for (UINT16 i = 0; i < rdpei->maxTouchContacts; i++)
137 if (!contactPoint->active && active)
139 else if (!contactPoint->active && !active)
141 contactPoint->contactId = i;
142 contactPoint->externalId = externalId;
143 contactPoint->active = TRUE;
146 else if (contactPoint->externalId == externalId)
159 static UINT rdpei_add_frame(RdpeiClientContext* context)
161 RDPEI_PLUGIN* rdpei = NULL;
165 if (!context || !context->handle)
166 return ERROR_INTERNAL_ERROR;
168 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;
225 if (!callback || !s || !callback->channel || !callback->channel->Write)
226 return ERROR_INTERNAL_ERROR;
228 if (pduLength > UINT32_MAX)
229 return ERROR_INVALID_PARAMETER;
231 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
233 return ERROR_INTERNAL_ERROR;
235 Stream_SetPosition(s, 0);
236 Stream_Write_UINT16(s, eventId);
237 Stream_Write_UINT32(s, (UINT32)pduLength);
238 Stream_SetPosition(s, Stream_Length(s));
239 status = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
241 #ifdef WITH_DEBUG_RDPEI
242 WLog_Print(rdpei->base.log, WLOG_DEBUG,
243 "rdpei_send_pdu: eventId: %" PRIu16
" (%s) length: %" PRIu32
" status: %" PRIu32
"",
244 eventId, rdpei_eventid_string(eventId), pduLength, status);
252 return ERROR_INTERNAL_ERROR;
254 if (!rdpei_write_2byte_unsigned(s, frame->contactCount))
255 return ERROR_OUTOFMEMORY;
256 if (!rdpei_write_8byte_unsigned(s, frame->frameOffset))
257 return ERROR_OUTOFMEMORY;
258 for (UINT16 x = 0; x < frame->contactCount; x++)
262 if (!Stream_EnsureRemainingCapacity(s, 1))
263 return ERROR_OUTOFMEMORY;
264 Stream_Write_UINT8(s, contact->deviceId);
265 if (!rdpei_write_2byte_unsigned(s, contact->fieldsPresent))
266 return ERROR_OUTOFMEMORY;
267 if (!rdpei_write_4byte_signed(s, contact->x))
268 return ERROR_OUTOFMEMORY;
269 if (!rdpei_write_4byte_signed(s, contact->y))
270 return ERROR_OUTOFMEMORY;
271 if (!rdpei_write_4byte_unsigned(s, contact->contactFlags))
272 return ERROR_OUTOFMEMORY;
273 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
275 if (!rdpei_write_4byte_unsigned(s, contact->penFlags))
276 return ERROR_OUTOFMEMORY;
278 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
280 if (!rdpei_write_4byte_unsigned(s, contact->pressure))
281 return ERROR_OUTOFMEMORY;
283 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
285 if (!rdpei_write_2byte_unsigned(s, contact->rotation))
286 return ERROR_OUTOFMEMORY;
288 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
290 if (!rdpei_write_2byte_signed(s, contact->tiltX))
291 return ERROR_OUTOFMEMORY;
293 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
295 if (!rdpei_write_2byte_signed(s, contact->tiltY))
296 return ERROR_OUTOFMEMORY;
299 return CHANNEL_RC_OK;
308 WINPR_ASSERT(callback);
310 if (frameOffset > UINT32_MAX)
311 return ERROR_INVALID_PARAMETER;
312 if (count > UINT16_MAX)
313 return ERROR_INVALID_PARAMETER;
315 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
317 return ERROR_INTERNAL_ERROR;
319 if (!frames || (count == 0))
320 return ERROR_INTERNAL_ERROR;
322 s = Stream_New(NULL, 64);
326 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Stream_New failed!");
327 return CHANNEL_RC_NO_MEMORY;
330 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
335 rdpei_write_4byte_unsigned(s,
336 (UINT32)frameOffset);
337 rdpei_write_2byte_unsigned(s, (UINT16)count);
339 for (
size_t x = 0; x < count; x++)
341 if ((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);
345 Stream_Free(s, TRUE);
349 Stream_SealLength(s);
351 status = rdpei_send_pdu(callback, s, EVENTID_PEN, Stream_Length(s));
352 Stream_Free(s, TRUE);
356 static UINT rdpei_send_pen_frame(RdpeiClientContext* context,
RDPINPUT_PEN_FRAME* frame)
358 const UINT64 currentTime = GetTickCount64();
359 RDPEI_PLUGIN* rdpei = NULL;
364 return ERROR_INTERNAL_ERROR;
365 rdpei = (RDPEI_PLUGIN*)context->handle;
366 if (!rdpei || !rdpei->base.listener_callback)
367 return ERROR_INTERNAL_ERROR;
368 if (!rdpei || !rdpei->rdpcontext)
369 return ERROR_INTERNAL_ERROR;
371 return CHANNEL_RC_OK;
373 callback = rdpei->base.listener_callback->channel_callback;
376 return CHANNEL_RC_OK;
378 if (!rdpei->previousPenFrameTime && !rdpei->currentPenFrameTime)
380 rdpei->currentPenFrameTime = currentTime;
381 frame->frameOffset = 0;
385 rdpei->currentPenFrameTime = currentTime;
386 frame->frameOffset = rdpei->currentPenFrameTime - rdpei->previousPenFrameTime;
389 if ((error = rdpei_send_pen_event_pdu(callback, frame->frameOffset, frame, 1)))
392 rdpei->previousPenFrameTime = rdpei->currentPenFrameTime;
396 static UINT rdpei_add_pen_frame(RdpeiClientContext* context)
398 RDPEI_PLUGIN* rdpei = NULL;
402 if (!context || !context->handle)
403 return ERROR_INTERNAL_ERROR;
405 rdpei = (RDPEI_PLUGIN*)context->handle;
407 penFrame.contacts = penContacts;
409 for (UINT16 i = 0; i < rdpei->maxPenContacts; i++)
415 penContacts[penFrame.contactCount++] = contact->data;
416 contact->dirty = FALSE;
418 else if (contact->active)
420 if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
422 contact->data.contactFlags = RDPINPUT_CONTACT_FLAG_UPDATE;
423 contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INRANGE;
424 contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INCONTACT;
427 penContacts[penFrame.contactCount++] = contact->data;
429 if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
431 contact->externalId = 0;
432 contact->active = FALSE;
436 if (penFrame.contactCount > 0)
437 return rdpei_send_pen_frame(context, &penFrame);
438 return CHANNEL_RC_OK;
441 static UINT rdpei_update(wLog* log, RdpeiClientContext* context)
443 UINT error = rdpei_add_frame(context);
444 if (error != CHANNEL_RC_OK)
446 WLog_Print(log, WLOG_ERROR,
"rdpei_add_frame failed with error %" PRIu32
"!", error);
450 return rdpei_add_pen_frame(context);
453 static BOOL rdpei_poll_run_unlocked(rdpContext* context,
void* userdata)
455 RDPEI_PLUGIN* rdpei = userdata;
457 WINPR_ASSERT(context);
459 const UINT64 now = GetTickCount64();
462 if ((now < rdpei->lastPollEventTime) || (now - rdpei->lastPollEventTime < 20ULL))
465 rdpei->lastPollEventTime = now;
467 const UINT error = rdpei_update(rdpei->base.log, rdpei->context);
469 (void)ResetEvent(rdpei->event);
471 if (error != CHANNEL_RC_OK)
473 WLog_Print(rdpei->base.log, WLOG_ERROR,
"rdpei_add_frame failed with error %" PRIu32
"!",
475 setChannelError(context, error,
"rdpei_add_frame reported an error");
482 static BOOL rdpei_poll_run(rdpContext* context,
void* userdata)
484 RDPEI_PLUGIN* rdpei = userdata;
487 EnterCriticalSection(&rdpei->lock);
488 BOOL rc = rdpei_poll_run_unlocked(context, userdata);
489 LeaveCriticalSection(&rdpei->lock);
493 static DWORD WINAPI rdpei_periodic_update(LPVOID arg)
496 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)arg;
497 UINT error = CHANNEL_RC_OK;
498 RdpeiClientContext* context = NULL;
502 error = ERROR_INVALID_PARAMETER;
506 context = rdpei->context;
510 error = ERROR_INVALID_PARAMETER;
514 while (rdpei->running)
516 status = WaitForSingleObject(rdpei->event, 20);
518 if (status == WAIT_FAILED)
520 error = GetLastError();
521 WLog_Print(rdpei->base.log, WLOG_ERROR,
522 "WaitForMultipleObjects failed with error %" PRIu32
"!", error);
526 error = rdpei_poll_run(rdpei->rdpcontext, rdpei);
531 if (error && rdpei && rdpei->rdpcontext)
532 setChannelError(rdpei->rdpcontext, error,
"rdpei_schedule_thread reported an error");
535 rdpei->running = FALSE;
551 UINT32 pduLength = 0;
552 RDPEI_PLUGIN* rdpei = NULL;
554 if (!callback || !callback->plugin)
555 return ERROR_INTERNAL_ERROR;
556 rdpei = (RDPEI_PLUGIN*)callback->plugin;
558 flags |= CS_READY_FLAGS_SHOW_TOUCH_VISUALS & rdpei->context->clientFeaturesMask;
559 if (rdpei->version > RDPINPUT_PROTOCOL_V10)
560 flags |= CS_READY_FLAGS_DISABLE_TIMESTAMP_INJECTION & rdpei->context->clientFeaturesMask;
561 if (rdpei->features & SC_READY_MULTIPEN_INJECTION_SUPPORTED)
562 flags |= CS_READY_FLAGS_ENABLE_MULTIPEN_INJECTION & rdpei->context->clientFeaturesMask;
564 pduLength = RDPINPUT_HEADER_LENGTH + 10;
565 s = Stream_New(NULL, pduLength);
569 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Stream_New failed!");
570 return CHANNEL_RC_NO_MEMORY;
573 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
574 Stream_Write_UINT32(s, flags);
575 Stream_Write_UINT32(s, rdpei->version);
576 Stream_Write_UINT16(s, rdpei->maxTouchContacts);
577 Stream_SealLength(s);
578 status = rdpei_send_pdu(callback, s, EVENTID_CS_READY, pduLength);
579 Stream_Free(s, TRUE);
583 #if defined(WITH_DEBUG_RDPEI)
584 static void rdpei_print_contact_flags(wLog* log, UINT32 contactFlags)
586 if (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
587 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_DOWN");
589 if (contactFlags & RDPINPUT_CONTACT_FLAG_UPDATE)
590 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_UPDATE");
592 if (contactFlags & RDPINPUT_CONTACT_FLAG_UP)
593 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_UP");
595 if (contactFlags & RDPINPUT_CONTACT_FLAG_INRANGE)
596 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_INRANGE");
598 if (contactFlags & RDPINPUT_CONTACT_FLAG_INCONTACT)
599 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_INCONTACT");
601 if (contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
602 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_CANCELED");
606 static INT16 bounded(INT32 val)
625 return ERROR_INTERNAL_ERROR;
626 #ifdef WITH_DEBUG_RDPEI
627 WLog_Print(log, WLOG_DEBUG,
"contactCount: %" PRIu32
"", frame->contactCount);
628 WLog_Print(log, WLOG_DEBUG,
"frameOffset: 0x%016" PRIX64
"", frame->frameOffset);
630 rdpei_write_2byte_unsigned(s,
631 frame->contactCount);
636 rdpei_write_8byte_unsigned(s, frame->frameOffset *
639 if (!Stream_EnsureRemainingCapacity(s, (
size_t)frame->contactCount * 64))
641 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
642 return CHANNEL_RC_NO_MEMORY;
645 for (UINT32 index = 0; index < frame->contactCount; index++)
647 contact = &frame->contacts[index];
648 contact->fieldsPresent |= CONTACT_DATA_CONTACTRECT_PRESENT;
649 contact->contactRectLeft = bounded(contact->x - rectSize);
650 contact->contactRectTop = bounded(contact->y - rectSize);
651 contact->contactRectRight = bounded(contact->x + rectSize);
652 contact->contactRectBottom = bounded(contact->y + rectSize);
653 #ifdef WITH_DEBUG_RDPEI
654 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].contactId: %" PRIu32
"", index,
656 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].fieldsPresent: %" PRIu32
"", index,
657 contact->fieldsPresent);
658 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].x: %" PRId32
"", index, contact->x);
659 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].y: %" PRId32
"", index, contact->y);
660 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].contactFlags: 0x%08" PRIX32
"", index,
661 contact->contactFlags);
662 rdpei_print_contact_flags(log, contact->contactFlags);
664 Stream_Write_UINT8(s, contact->contactId);
666 rdpei_write_2byte_unsigned(s, contact->fieldsPresent);
667 rdpei_write_4byte_signed(s, contact->x);
668 rdpei_write_4byte_signed(s, contact->y);
670 rdpei_write_4byte_unsigned(s, contact->contactFlags);
672 if (contact->fieldsPresent & CONTACT_DATA_CONTACTRECT_PRESENT)
675 rdpei_write_2byte_signed(s, contact->contactRectLeft);
677 rdpei_write_2byte_signed(s, contact->contactRectTop);
679 rdpei_write_2byte_signed(s, contact->contactRectRight);
681 rdpei_write_2byte_signed(s, contact->contactRectBottom);
684 if (contact->fieldsPresent & CONTACT_DATA_ORIENTATION_PRESENT)
687 rdpei_write_4byte_unsigned(s, contact->orientation);
690 if (contact->fieldsPresent & CONTACT_DATA_PRESSURE_PRESENT)
693 rdpei_write_4byte_unsigned(s, contact->pressure);
697 return CHANNEL_RC_OK;
710 WINPR_ASSERT(callback);
712 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
713 if (!rdpei || !rdpei->rdpcontext)
714 return ERROR_INTERNAL_ERROR;
716 return CHANNEL_RC_OK;
719 return ERROR_INTERNAL_ERROR;
721 size_t pduLength = 64ULL + (64ULL * frame->contactCount);
722 wStream* s = Stream_New(NULL, pduLength);
726 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Stream_New failed!");
727 return CHANNEL_RC_NO_MEMORY;
730 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
735 rdpei_write_4byte_unsigned(
736 s, (UINT32)frame->frameOffset);
737 rdpei_write_2byte_unsigned(s, 1);
739 status = rdpei_write_touch_frame(rdpei->base.log, s, frame);
742 WLog_Print(rdpei->base.log, WLOG_ERROR,
743 "rdpei_write_touch_frame failed with error %" PRIu32
"!", status);
744 Stream_Free(s, TRUE);
748 Stream_SealLength(s);
749 status = rdpei_send_pdu(callback, s, EVENTID_TOUCH, Stream_Length(s));
750 Stream_Free(s, TRUE);
762 UINT32 protocolVersion = 0;
764 if (!callback || !callback->plugin)
765 return ERROR_INTERNAL_ERROR;
767 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
769 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
770 return ERROR_INVALID_DATA;
771 Stream_Read_UINT32(s, protocolVersion);
773 if (protocolVersion >= RDPINPUT_PROTOCOL_V300)
775 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
776 return ERROR_INVALID_DATA;
779 if (Stream_GetRemainingLength(s) >= 4)
780 Stream_Read_UINT32(s, features);
782 if (rdpei->version > protocolVersion)
783 rdpei->version = protocolVersion;
784 rdpei->features = features;
787 if (protocolVersion != RDPINPUT_PROTOCOL_V10)
789 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Unknown [MS-RDPEI] protocolVersion: 0x%08"PRIX32
"", protocolVersion);
794 return CHANNEL_RC_OK;
804 UINT error = CHANNEL_RC_OK;
808 if (!callback || !callback->plugin)
809 return ERROR_INTERNAL_ERROR;
811 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
812 RdpeiClientContext* context = rdpei->context;
814 return ERROR_INTERNAL_ERROR;
816 IFCALLRET(context->SuspendTouch, error, context);
819 WLog_Print(rdpei->base.log, WLOG_ERROR,
820 "rdpei->SuspendTouch failed with error %" PRIu32
"!", error);
832 UINT error = CHANNEL_RC_OK;
834 return ERROR_INTERNAL_ERROR;
836 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
838 return ERROR_INTERNAL_ERROR;
840 RdpeiClientContext* context = (RdpeiClientContext*)callback->plugin->pInterface;
842 return ERROR_INTERNAL_ERROR;
844 IFCALLRET(context->ResumeTouch, error, context);
847 WLog_Print(rdpei->base.log, WLOG_ERROR,
"rdpei->ResumeTouch failed with error %" PRIu32
"!",
861 UINT32 pduLength = 0;
865 return ERROR_INTERNAL_ERROR;
867 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
869 return ERROR_INTERNAL_ERROR;
871 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 6))
872 return ERROR_INVALID_DATA;
874 Stream_Read_UINT16(s, eventId);
875 Stream_Read_UINT32(s, pduLength);
876 #ifdef WITH_DEBUG_RDPEI
877 WLog_Print(rdpei->base.log, WLOG_DEBUG,
878 "rdpei_recv_pdu: eventId: %" PRIu16
" (%s) length: %" PRIu32
"", eventId,
879 rdpei_eventid_string(eventId), pduLength);
882 if ((pduLength < 6) || !Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, pduLength - 6))
883 return ERROR_INVALID_DATA;
887 case EVENTID_SC_READY:
888 if ((error = rdpei_recv_sc_ready_pdu(callback, s)))
890 WLog_Print(rdpei->base.log, WLOG_ERROR,
891 "rdpei_recv_sc_ready_pdu failed with error %" PRIu32
"!", error);
895 if ((error = rdpei_send_cs_ready_pdu(callback)))
897 WLog_Print(rdpei->base.log, WLOG_ERROR,
898 "rdpei_send_cs_ready_pdu failed with error %" PRIu32
"!", error);
904 case EVENTID_SUSPEND_TOUCH:
905 if ((error = rdpei_recv_suspend_touch_pdu(callback, s)))
907 WLog_Print(rdpei->base.log, WLOG_ERROR,
908 "rdpei_recv_suspend_touch_pdu failed with error %" PRIu32
"!", error);
914 case EVENTID_RESUME_TOUCH:
915 if ((error = rdpei_recv_resume_touch_pdu(callback, s)))
917 WLog_Print(rdpei->base.log, WLOG_ERROR,
918 "rdpei_recv_resume_touch_pdu failed with error %" PRIu32
"!", error);
928 return CHANNEL_RC_OK;
936 static UINT rdpei_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
939 return rdpei_recv_pdu(callback, data);
947 static UINT rdpei_on_close(IWTSVirtualChannelCallback* pChannelCallback)
952 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
953 if (rdpei && rdpei->base.listener_callback)
955 if (rdpei->base.listener_callback->channel_callback == callback)
956 rdpei->base.listener_callback->channel_callback = NULL;
960 return CHANNEL_RC_OK;
967 static UINT32 rdpei_get_version(RdpeiClientContext* context)
969 RDPEI_PLUGIN* rdpei = NULL;
970 if (!context || !context->handle)
972 rdpei = (RDPEI_PLUGIN*)context->handle;
973 return rdpei->version;
976 static UINT32 rdpei_get_features(RdpeiClientContext* context)
978 RDPEI_PLUGIN* rdpei = NULL;
979 if (!context || !context->handle)
981 rdpei = (RDPEI_PLUGIN*)context->handle;
982 return rdpei->features;
992 UINT64 currentTime = GetTickCount64();
993 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
997 callback = rdpei->base.listener_callback->channel_callback;
1001 return CHANNEL_RC_OK;
1003 if (!rdpei->previousFrameTime && !rdpei->currentFrameTime)
1005 rdpei->currentFrameTime = currentTime;
1006 frame->frameOffset = 0;
1010 rdpei->currentFrameTime = currentTime;
1011 frame->frameOffset = rdpei->currentFrameTime - rdpei->previousFrameTime;
1014 if ((error = rdpei_send_touch_event_pdu(callback, frame)))
1016 WLog_Print(rdpei->base.log, WLOG_ERROR,
1017 "rdpei_send_touch_event_pdu failed with error %" PRIu32
"!", error);
1021 rdpei->previousFrameTime = rdpei->currentFrameTime;
1033 RDPEI_PLUGIN* rdpei = NULL;
1034 if (!context || !contact || !context->handle)
1035 return ERROR_INTERNAL_ERROR;
1037 rdpei = (RDPEI_PLUGIN*)context->handle;
1039 EnterCriticalSection(&rdpei->lock);
1040 contactPoint = &rdpei->contactPoints[contact->contactId];
1041 contactPoint->data = *contact;
1042 contactPoint->dirty = TRUE;
1043 (void)SetEvent(rdpei->event);
1044 LeaveCriticalSection(&rdpei->lock);
1046 return CHANNEL_RC_OK;
1049 static UINT rdpei_touch_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1050 INT32 x, INT32 y, INT32* contactId, UINT32 fieldFlags, va_list ap)
1052 INT64 contactIdlocal = -1;
1054 UINT error = CHANNEL_RC_OK;
1056 if (!context || !contactId || !context->handle)
1057 return ERROR_INTERNAL_ERROR;
1059 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1061 EnterCriticalSection(&rdpei->lock);
1062 const BOOL begin = (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN) != 0;
1063 contactPoint = rdpei_contact(rdpei, externalId, !begin);
1065 contactIdlocal = contactPoint->contactId;
1066 LeaveCriticalSection(&rdpei->lock);
1068 if (contactIdlocal > UINT32_MAX)
1069 return ERROR_INVALID_PARAMETER;
1071 if (contactIdlocal >= 0)
1076 contact.contactId = (UINT32)contactIdlocal;
1077 contact.contactFlags = contactFlags;
1078 contact.fieldsPresent = fieldFlags;
1080 if (fieldFlags & CONTACT_DATA_CONTACTRECT_PRESENT)
1082 contact.contactRectLeft = va_arg(ap, INT32);
1083 contact.contactRectTop = va_arg(ap, INT32);
1084 contact.contactRectRight = va_arg(ap, INT32);
1085 contact.contactRectBottom = va_arg(ap, INT32);
1087 if (fieldFlags & CONTACT_DATA_ORIENTATION_PRESENT)
1089 UINT32 p = va_arg(ap, UINT32);
1092 WLog_Print(rdpei->base.log, WLOG_WARN,
1093 "TouchContact %" PRId64
": Invalid orientation value %" PRIu32
1094 "degree, clamping to 359 degree",
1098 contact.orientation = p;
1100 if (fieldFlags & CONTACT_DATA_PRESSURE_PRESENT)
1102 UINT32 p = va_arg(ap, UINT32);
1105 WLog_Print(rdpei->base.log, WLOG_WARN,
1106 "TouchContact %" PRId64
": Invalid pressure value %" PRIu32
1107 ", clamping to 1024",
1111 contact.pressure = p;
1114 error = context->AddContact(context, &contact);
1118 *contactId = (INT32)contactIdlocal;
1127 static UINT rdpei_touch_begin(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1132 rc = rdpei_touch_process(context, externalId,
1133 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1134 RDPINPUT_CONTACT_FLAG_INCONTACT,
1135 x, y, contactId, 0, ap);
1144 static UINT rdpei_touch_update(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1149 rc = rdpei_touch_process(context, externalId,
1150 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1151 RDPINPUT_CONTACT_FLAG_INCONTACT,
1152 x, y, contactId, 0, ap);
1161 static UINT rdpei_touch_end(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1166 error = rdpei_touch_process(context, externalId,
1167 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1168 RDPINPUT_CONTACT_FLAG_INCONTACT,
1169 x, y, contactId, 0, ap);
1170 if (error != CHANNEL_RC_OK)
1173 rdpei_touch_process(context, externalId, RDPINPUT_CONTACT_FLAG_UP, x, y, contactId, 0, ap);
1182 static UINT rdpei_touch_cancel(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1187 rc = rdpei_touch_process(context, externalId,
1188 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_CANCELED, x, y,
1193 static UINT rdpei_touch_raw_event(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1194 INT32* contactId, UINT32 flags, UINT32 fieldFlags, ...)
1198 va_start(ap, fieldFlags);
1199 rc = rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, ap);
1204 static UINT rdpei_touch_raw_event_va(RdpeiClientContext* context, INT32 externalId, INT32 x,
1205 INT32 y, INT32* contactId, UINT32 flags, UINT32 fieldFlags,
1208 return rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, args);
1217 for (UINT32 x = 0; x < rdpei->maxPenContacts; x++)
1222 if (contact->active)
1224 if (contact->externalId == externalId)
1230 if (!contact->active)
1232 contact->externalId = externalId;
1233 contact->active = TRUE;
1241 static UINT rdpei_add_pen(RdpeiClientContext* context, INT32 externalId,
1244 RDPEI_PLUGIN* rdpei = NULL;
1247 if (!context || !contact || !context->handle)
1248 return ERROR_INTERNAL_ERROR;
1250 rdpei = (RDPEI_PLUGIN*)context->handle;
1252 EnterCriticalSection(&rdpei->lock);
1253 contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1256 contactPoint->data = *contact;
1257 contactPoint->dirty = TRUE;
1258 (void)SetEvent(rdpei->event);
1260 LeaveCriticalSection(&rdpei->lock);
1262 return CHANNEL_RC_OK;
1265 static UINT rdpei_pen_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1266 UINT32 fieldFlags, INT32 x, INT32 y, va_list ap)
1269 RDPEI_PLUGIN* rdpei = NULL;
1270 UINT error = CHANNEL_RC_OK;
1272 if (!context || !context->handle)
1273 return ERROR_INTERNAL_ERROR;
1275 rdpei = (RDPEI_PLUGIN*)context->handle;
1277 EnterCriticalSection(&rdpei->lock);
1279 contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1282 const UINT32 mask = RDPINPUT_CONTACT_FLAG_INRANGE;
1283 if ((contactFlags & mask) == mask)
1285 contactPoint = rdpei_pen_contact(rdpei, externalId, FALSE);
1288 LeaveCriticalSection(&rdpei->lock);
1289 if (contactPoint != NULL)
1295 contact.fieldsPresent = fieldFlags;
1297 contact.contactFlags = contactFlags;
1298 if (fieldFlags & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
1299 contact.penFlags = va_arg(ap, UINT32);
1300 if (fieldFlags & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
1301 contact.pressure = va_arg(ap, UINT32);
1302 if (fieldFlags & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
1303 contact.rotation = va_arg(ap, UINT32);
1304 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
1305 contact.tiltX = va_arg(ap, INT32);
1306 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
1307 contact.tiltY = va_arg(ap, INT32);
1309 error = context->AddPen(context, externalId, &contact);
1320 static UINT rdpei_pen_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1321 INT32 x, INT32 y, ...)
1327 error = rdpei_pen_process(context, externalId,
1328 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1329 RDPINPUT_CONTACT_FLAG_INCONTACT,
1330 fieldFlags, x, y, ap);
1341 static UINT rdpei_pen_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1342 INT32 x, INT32 y, ...)
1348 error = rdpei_pen_process(context, externalId,
1349 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1350 RDPINPUT_CONTACT_FLAG_INCONTACT,
1351 fieldFlags, x, y, ap);
1361 static UINT rdpei_pen_end(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, INT32 x,
1367 error = rdpei_pen_process(context, externalId,
1368 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_INRANGE, fieldFlags,
1379 static UINT rdpei_pen_hover_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1380 INT32 x, INT32 y, ...)
1386 error = rdpei_pen_process(context, externalId,
1387 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1388 fieldFlags, x, y, ap);
1399 static UINT rdpei_pen_hover_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1400 INT32 x, INT32 y, ...)
1406 error = rdpei_pen_process(context, externalId,
1407 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1408 fieldFlags, x, y, ap);
1419 static UINT rdpei_pen_hover_cancel(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1420 INT32 x, INT32 y, ...)
1426 error = rdpei_pen_process(context, externalId,
1427 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_CANCELED,
1428 fieldFlags, x, y, ap);
1434 static UINT rdpei_pen_raw_event(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1435 UINT32 fieldFlags, INT32 x, INT32 y, ...)
1441 error = rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, ap);
1446 static UINT rdpei_pen_raw_event_va(RdpeiClientContext* context, INT32 externalId,
1447 UINT32 contactFlags, UINT32 fieldFlags, INT32 x, INT32 y,
1450 return rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, args);
1453 static UINT init_plugin_cb(
GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext, rdpSettings* settings)
1455 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1458 WINPR_UNUSED(settings);
1460 rdpei->version = RDPINPUT_PROTOCOL_V300;
1461 rdpei->currentFrameTime = 0;
1462 rdpei->previousFrameTime = 0;
1463 rdpei->maxTouchContacts = MAX_CONTACTS;
1464 rdpei->maxPenContacts = MAX_PEN_CONTACTS;
1465 rdpei->rdpcontext = rcontext;
1467 WINPR_ASSERT(rdpei->base.log);
1469 InitializeCriticalSection(&rdpei->lock);
1470 rdpei->event = CreateEventA(NULL, TRUE, FALSE, NULL);
1473 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1474 return CHANNEL_RC_NO_MEMORY;
1477 RdpeiClientContext* context = (RdpeiClientContext*)calloc(1,
sizeof(RdpeiClientContext));
1480 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1481 return CHANNEL_RC_NO_MEMORY;
1484 context->clientFeaturesMask = UINT32_MAX;
1485 context->handle = (
void*)rdpei;
1486 context->GetVersion = rdpei_get_version;
1487 context->GetFeatures = rdpei_get_features;
1488 context->AddContact = rdpei_add_contact;
1489 context->TouchBegin = rdpei_touch_begin;
1490 context->TouchUpdate = rdpei_touch_update;
1491 context->TouchEnd = rdpei_touch_end;
1492 context->TouchCancel = rdpei_touch_cancel;
1493 context->TouchRawEvent = rdpei_touch_raw_event;
1494 context->TouchRawEventVA = rdpei_touch_raw_event_va;
1495 context->AddPen = rdpei_add_pen;
1496 context->PenBegin = rdpei_pen_begin;
1497 context->PenUpdate = rdpei_pen_update;
1498 context->PenEnd = rdpei_pen_end;
1499 context->PenHoverBegin = rdpei_pen_hover_begin;
1500 context->PenHoverUpdate = rdpei_pen_hover_update;
1501 context->PenHoverCancel = rdpei_pen_hover_cancel;
1502 context->PenRawEvent = rdpei_pen_raw_event;
1503 context->PenRawEventVA = rdpei_pen_raw_event_va;
1505 rdpei->context = context;
1506 rdpei->base.iface.pInterface = (
void*)context;
1512 rdpei->running = TRUE;
1514 rdpei->thread = CreateThread(NULL, 0, rdpei_periodic_update, rdpei, 0, NULL);
1517 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1518 return CHANNEL_RC_NO_MEMORY;
1523 if (!freerdp_client_channel_register(rdpei->rdpcontext->channels, rdpei->event,
1524 rdpei_poll_run, rdpei))
1525 return ERROR_INTERNAL_ERROR;
1528 return CHANNEL_RC_OK;
1533 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1534 WINPR_ASSERT(rdpei);
1536 rdpei->running = FALSE;
1538 (void)SetEvent(rdpei->event);
1542 (void)WaitForSingleObject(rdpei->thread, INFINITE);
1543 (void)CloseHandle(rdpei->thread);
1546 if (rdpei->event && !rdpei->async)
1547 (void)freerdp_client_channel_unregister(rdpei->rdpcontext->channels, rdpei->event);
1550 (void)CloseHandle(rdpei->event);
1552 DeleteCriticalSection(&rdpei->lock);
1553 free(rdpei->context);
1556 static const IWTSVirtualChannelCallback geometry_callbacks = { rdpei_on_data_received,
1558 rdpei_on_close, NULL };
1565 FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpei_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1567 return freerdp_generic_DVCPluginEntry(pEntryPoints, RDPEI_TAG, RDPEI_DVC_CHANNEL_NAME,
1569 &geometry_callbacks, init_plugin_cb, terminate_plugin_cb);
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.