22 #include <freerdp/config.h>
29 #include <winpr/crt.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
108 static 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)
160 static UINT rdpei_add_frame(RdpeiClientContext* context)
162 RDPEI_PLUGIN* rdpei = NULL;
166 if (!context || !context->handle)
167 return ERROR_INTERNAL_ERROR;
169 rdpei = (RDPEI_PLUGIN*)context->handle;
170 frame.contacts = contacts;
172 for (UINT16 i = 0; i < rdpei->maxTouchContacts; i++)
177 if (contactPoint->dirty)
179 contacts[frame.contactCount] = *contact;
180 rdpei->contactPoints[i].dirty = FALSE;
181 frame.contactCount++;
183 else if (contactPoint->active)
185 if (contact->contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
187 contact->contactFlags = RDPINPUT_CONTACT_FLAG_UPDATE;
188 contact->contactFlags |= RDPINPUT_CONTACT_FLAG_INRANGE;
189 contact->contactFlags |= RDPINPUT_CONTACT_FLAG_INCONTACT;
192 contacts[frame.contactCount] = *contact;
193 frame.contactCount++;
195 if (contact->contactFlags & RDPINPUT_CONTACT_FLAG_UP)
197 contactPoint->active = FALSE;
198 contactPoint->externalId = 0;
199 contactPoint->contactId = 0;
203 if (frame.contactCount > 0)
205 UINT error = rdpei_send_frame(context, &frame);
206 if (error != CHANNEL_RC_OK)
208 WLog_Print(rdpei->base.log, WLOG_ERROR,
209 "rdpei_send_frame failed with error %" PRIu32
"!", error);
213 return CHANNEL_RC_OK;
226 if (!callback || !s || !callback->channel || !callback->channel->Write)
227 return ERROR_INTERNAL_ERROR;
229 if (pduLength > UINT32_MAX)
230 return ERROR_INVALID_PARAMETER;
232 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
234 return ERROR_INTERNAL_ERROR;
236 Stream_SetPosition(s, 0);
237 Stream_Write_UINT16(s, eventId);
238 Stream_Write_UINT32(s, (UINT32)pduLength);
239 Stream_SetPosition(s, Stream_Length(s));
240 status = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
242 #ifdef WITH_DEBUG_RDPEI
243 WLog_Print(rdpei->base.log, WLOG_DEBUG,
244 "rdpei_send_pdu: eventId: %" PRIu16
" (%s) length: %" PRIu32
" status: %" PRIu32
"",
245 eventId, rdpei_eventid_string(eventId), pduLength, status);
253 return ERROR_INTERNAL_ERROR;
255 if (!rdpei_write_2byte_unsigned(s, frame->contactCount))
256 return ERROR_OUTOFMEMORY;
257 if (!rdpei_write_8byte_unsigned(s, frame->frameOffset))
258 return ERROR_OUTOFMEMORY;
259 for (UINT16 x = 0; x < frame->contactCount; x++)
263 if (!Stream_EnsureRemainingCapacity(s, 1))
264 return ERROR_OUTOFMEMORY;
265 Stream_Write_UINT8(s, contact->deviceId);
266 if (!rdpei_write_2byte_unsigned(s, contact->fieldsPresent))
267 return ERROR_OUTOFMEMORY;
268 if (!rdpei_write_4byte_signed(s, contact->x))
269 return ERROR_OUTOFMEMORY;
270 if (!rdpei_write_4byte_signed(s, contact->y))
271 return ERROR_OUTOFMEMORY;
272 if (!rdpei_write_4byte_unsigned(s, contact->contactFlags))
273 return ERROR_OUTOFMEMORY;
274 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
276 if (!rdpei_write_4byte_unsigned(s, contact->penFlags))
277 return ERROR_OUTOFMEMORY;
279 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
281 if (!rdpei_write_4byte_unsigned(s, contact->pressure))
282 return ERROR_OUTOFMEMORY;
284 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
286 if (!rdpei_write_2byte_unsigned(s, contact->rotation))
287 return ERROR_OUTOFMEMORY;
289 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
291 if (!rdpei_write_2byte_signed(s, contact->tiltX))
292 return ERROR_OUTOFMEMORY;
294 if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
296 if (!rdpei_write_2byte_signed(s, contact->tiltY))
297 return ERROR_OUTOFMEMORY;
300 return CHANNEL_RC_OK;
309 WINPR_ASSERT(callback);
311 if (frameOffset > UINT32_MAX)
312 return ERROR_INVALID_PARAMETER;
313 if (count > UINT16_MAX)
314 return ERROR_INVALID_PARAMETER;
316 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
318 return ERROR_INTERNAL_ERROR;
320 if (!frames || (count == 0))
321 return ERROR_INTERNAL_ERROR;
323 s = Stream_New(NULL, 64);
327 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Stream_New failed!");
328 return CHANNEL_RC_NO_MEMORY;
331 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
336 rdpei_write_4byte_unsigned(s,
337 (UINT32)frameOffset);
338 rdpei_write_2byte_unsigned(s, (UINT16)count);
340 for (
size_t x = 0; x < count; x++)
342 if ((status = rdpei_write_pen_frame(s, &frames[x])))
344 WLog_Print(rdpei->base.log, WLOG_ERROR,
345 "rdpei_write_pen_frame failed with error %" PRIu32
"!", status);
346 Stream_Free(s, TRUE);
350 Stream_SealLength(s);
352 status = rdpei_send_pdu(callback, s, EVENTID_PEN, Stream_Length(s));
353 Stream_Free(s, TRUE);
357 static UINT rdpei_send_pen_frame(RdpeiClientContext* context,
RDPINPUT_PEN_FRAME* frame)
359 const UINT64 currentTime = GetTickCount64();
360 RDPEI_PLUGIN* rdpei = NULL;
365 return ERROR_INTERNAL_ERROR;
366 rdpei = (RDPEI_PLUGIN*)context->handle;
367 if (!rdpei || !rdpei->base.listener_callback)
368 return ERROR_INTERNAL_ERROR;
369 if (!rdpei || !rdpei->rdpcontext)
370 return ERROR_INTERNAL_ERROR;
372 return CHANNEL_RC_OK;
374 callback = rdpei->base.listener_callback->channel_callback;
377 return CHANNEL_RC_OK;
379 if (!rdpei->previousPenFrameTime && !rdpei->currentPenFrameTime)
381 rdpei->currentPenFrameTime = currentTime;
382 frame->frameOffset = 0;
386 rdpei->currentPenFrameTime = currentTime;
387 frame->frameOffset = rdpei->currentPenFrameTime - rdpei->previousPenFrameTime;
390 if ((error = rdpei_send_pen_event_pdu(callback, frame->frameOffset, frame, 1)))
393 rdpei->previousPenFrameTime = rdpei->currentPenFrameTime;
397 static UINT rdpei_add_pen_frame(RdpeiClientContext* context)
399 RDPEI_PLUGIN* rdpei = NULL;
403 if (!context || !context->handle)
404 return ERROR_INTERNAL_ERROR;
406 rdpei = (RDPEI_PLUGIN*)context->handle;
408 penFrame.contacts = penContacts;
410 for (UINT16 i = 0; i < rdpei->maxPenContacts; i++)
416 penContacts[penFrame.contactCount++] = contact->data;
417 contact->dirty = FALSE;
419 else if (contact->active)
421 if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
423 contact->data.contactFlags = RDPINPUT_CONTACT_FLAG_UPDATE;
424 contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INRANGE;
425 contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INCONTACT;
428 penContacts[penFrame.contactCount++] = contact->data;
430 if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
432 contact->externalId = 0;
433 contact->active = FALSE;
437 if (penFrame.contactCount > 0)
438 return rdpei_send_pen_frame(context, &penFrame);
439 return CHANNEL_RC_OK;
442 static UINT rdpei_update(wLog* log, RdpeiClientContext* context)
444 UINT error = rdpei_add_frame(context);
445 if (error != CHANNEL_RC_OK)
447 WLog_Print(log, WLOG_ERROR,
"rdpei_add_frame failed with error %" PRIu32
"!", error);
451 return rdpei_add_pen_frame(context);
454 static BOOL rdpei_poll_run_unlocked(rdpContext* context,
void* userdata)
456 RDPEI_PLUGIN* rdpei = userdata;
458 WINPR_ASSERT(context);
460 const UINT64 now = GetTickCount64();
463 if ((now < rdpei->lastPollEventTime) || (now - rdpei->lastPollEventTime < 20ULL))
466 rdpei->lastPollEventTime = now;
468 const UINT error = rdpei_update(rdpei->base.log, rdpei->context);
470 (void)ResetEvent(rdpei->event);
472 if (error != CHANNEL_RC_OK)
474 WLog_Print(rdpei->base.log, WLOG_ERROR,
"rdpei_add_frame failed with error %" PRIu32
"!",
476 setChannelError(context, error,
"rdpei_add_frame reported an error");
483 static BOOL rdpei_poll_run(rdpContext* context,
void* userdata)
485 RDPEI_PLUGIN* rdpei = userdata;
488 EnterCriticalSection(&rdpei->lock);
489 BOOL rc = rdpei_poll_run_unlocked(context, userdata);
490 LeaveCriticalSection(&rdpei->lock);
494 static DWORD WINAPI rdpei_periodic_update(LPVOID arg)
497 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)arg;
498 UINT error = CHANNEL_RC_OK;
499 RdpeiClientContext* context = NULL;
503 error = ERROR_INVALID_PARAMETER;
507 context = rdpei->context;
511 error = ERROR_INVALID_PARAMETER;
515 while (rdpei->running)
517 status = WaitForSingleObject(rdpei->event, 20);
519 if (status == WAIT_FAILED)
521 error = GetLastError();
522 WLog_Print(rdpei->base.log, WLOG_ERROR,
523 "WaitForMultipleObjects failed with error %" PRIu32
"!", error);
527 if (!rdpei_poll_run(rdpei->rdpcontext, rdpei))
528 error = ERROR_INTERNAL_ERROR;
533 if (error && rdpei && rdpei->rdpcontext)
534 setChannelError(rdpei->rdpcontext, error,
"rdpei_schedule_thread reported an error");
537 rdpei->running = FALSE;
553 UINT32 pduLength = 0;
554 RDPEI_PLUGIN* rdpei = NULL;
556 if (!callback || !callback->plugin)
557 return ERROR_INTERNAL_ERROR;
558 rdpei = (RDPEI_PLUGIN*)callback->plugin;
560 flags |= CS_READY_FLAGS_SHOW_TOUCH_VISUALS & rdpei->context->clientFeaturesMask;
561 if (rdpei->version > RDPINPUT_PROTOCOL_V10)
562 flags |= CS_READY_FLAGS_DISABLE_TIMESTAMP_INJECTION & rdpei->context->clientFeaturesMask;
563 if (rdpei->features & SC_READY_MULTIPEN_INJECTION_SUPPORTED)
564 flags |= CS_READY_FLAGS_ENABLE_MULTIPEN_INJECTION & rdpei->context->clientFeaturesMask;
566 pduLength = RDPINPUT_HEADER_LENGTH + 10;
567 s = Stream_New(NULL, pduLength);
571 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Stream_New failed!");
572 return CHANNEL_RC_NO_MEMORY;
575 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
576 Stream_Write_UINT32(s, flags);
577 Stream_Write_UINT32(s, rdpei->version);
578 Stream_Write_UINT16(s, rdpei->maxTouchContacts);
579 Stream_SealLength(s);
580 status = rdpei_send_pdu(callback, s, EVENTID_CS_READY, pduLength);
581 Stream_Free(s, TRUE);
585 #if defined(WITH_DEBUG_RDPEI)
586 static void rdpei_print_contact_flags(wLog* log, UINT32 contactFlags)
588 if (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
589 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_DOWN");
591 if (contactFlags & RDPINPUT_CONTACT_FLAG_UPDATE)
592 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_UPDATE");
594 if (contactFlags & RDPINPUT_CONTACT_FLAG_UP)
595 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_UP");
597 if (contactFlags & RDPINPUT_CONTACT_FLAG_INRANGE)
598 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_INRANGE");
600 if (contactFlags & RDPINPUT_CONTACT_FLAG_INCONTACT)
601 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_INCONTACT");
603 if (contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
604 WLog_Print(log, WLOG_DEBUG,
" RDPINPUT_CONTACT_FLAG_CANCELED");
608 static INT16 bounded(INT32 val)
627 return ERROR_INTERNAL_ERROR;
628 #ifdef WITH_DEBUG_RDPEI
629 WLog_Print(log, WLOG_DEBUG,
"contactCount: %" PRIu32
"", frame->contactCount);
630 WLog_Print(log, WLOG_DEBUG,
"frameOffset: 0x%016" PRIX64
"", frame->frameOffset);
632 rdpei_write_2byte_unsigned(s,
633 frame->contactCount);
638 rdpei_write_8byte_unsigned(s, frame->frameOffset *
641 if (!Stream_EnsureRemainingCapacity(s, (
size_t)frame->contactCount * 64))
643 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
644 return CHANNEL_RC_NO_MEMORY;
647 for (UINT32 index = 0; index < frame->contactCount; index++)
649 contact = &frame->contacts[index];
650 contact->fieldsPresent |= CONTACT_DATA_CONTACTRECT_PRESENT;
651 contact->contactRectLeft = bounded(contact->x - rectSize);
652 contact->contactRectTop = bounded(contact->y - rectSize);
653 contact->contactRectRight = bounded(contact->x + rectSize);
654 contact->contactRectBottom = bounded(contact->y + rectSize);
655 #ifdef WITH_DEBUG_RDPEI
656 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].contactId: %" PRIu32
"", index,
658 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].fieldsPresent: %" PRIu32
"", index,
659 contact->fieldsPresent);
660 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].x: %" PRId32
"", index, contact->x);
661 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].y: %" PRId32
"", index, contact->y);
662 WLog_Print(log, WLOG_DEBUG,
"contact[%" PRIu32
"].contactFlags: 0x%08" PRIX32
"", index,
663 contact->contactFlags);
664 rdpei_print_contact_flags(log, contact->contactFlags);
667 s, WINPR_ASSERTING_INT_CAST(uint8_t, contact->contactId));
669 rdpei_write_2byte_unsigned(s, contact->fieldsPresent);
670 rdpei_write_4byte_signed(s, contact->x);
671 rdpei_write_4byte_signed(s, contact->y);
673 rdpei_write_4byte_unsigned(s, contact->contactFlags);
675 if (contact->fieldsPresent & CONTACT_DATA_CONTACTRECT_PRESENT)
678 rdpei_write_2byte_signed(s, contact->contactRectLeft);
680 rdpei_write_2byte_signed(s, contact->contactRectTop);
682 rdpei_write_2byte_signed(s, contact->contactRectRight);
684 rdpei_write_2byte_signed(s, contact->contactRectBottom);
687 if (contact->fieldsPresent & CONTACT_DATA_ORIENTATION_PRESENT)
690 rdpei_write_4byte_unsigned(s, contact->orientation);
693 if (contact->fieldsPresent & CONTACT_DATA_PRESSURE_PRESENT)
696 rdpei_write_4byte_unsigned(s, contact->pressure);
700 return CHANNEL_RC_OK;
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(NULL, pduLength);
729 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Stream_New failed!");
730 return CHANNEL_RC_NO_MEMORY;
733 Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
738 rdpei_write_4byte_unsigned(
739 s, (UINT32)frame->frameOffset);
740 rdpei_write_2byte_unsigned(s, 1);
742 status = rdpei_write_touch_frame(rdpei->base.log, s, frame);
745 WLog_Print(rdpei->base.log, WLOG_ERROR,
746 "rdpei_write_touch_frame failed with error %" PRIu32
"!", status);
747 Stream_Free(s, TRUE);
751 Stream_SealLength(s);
752 status = rdpei_send_pdu(callback, s, EVENTID_TOUCH, Stream_Length(s));
753 Stream_Free(s, TRUE);
765 UINT32 protocolVersion = 0;
767 if (!callback || !callback->plugin)
768 return ERROR_INTERNAL_ERROR;
770 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
772 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
773 return ERROR_INVALID_DATA;
774 Stream_Read_UINT32(s, protocolVersion);
776 if (protocolVersion >= RDPINPUT_PROTOCOL_V300)
778 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
779 return ERROR_INVALID_DATA;
782 if (Stream_GetRemainingLength(s) >= 4)
783 Stream_Read_UINT32(s, features);
785 if (rdpei->version > protocolVersion)
786 rdpei->version = protocolVersion;
787 rdpei->features = features;
790 if (protocolVersion != RDPINPUT_PROTOCOL_V10)
792 WLog_Print(rdpei->base.log, WLOG_ERROR,
"Unknown [MS-RDPEI] protocolVersion: 0x%08"PRIX32
"", protocolVersion);
797 return CHANNEL_RC_OK;
807 UINT error = CHANNEL_RC_OK;
811 if (!callback || !callback->plugin)
812 return ERROR_INTERNAL_ERROR;
814 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
815 RdpeiClientContext* context = rdpei->context;
817 return ERROR_INTERNAL_ERROR;
819 IFCALLRET(context->SuspendTouch, error, context);
822 WLog_Print(rdpei->base.log, WLOG_ERROR,
823 "rdpei->SuspendTouch failed with error %" PRIu32
"!", error);
835 UINT error = CHANNEL_RC_OK;
837 return ERROR_INTERNAL_ERROR;
839 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
841 return ERROR_INTERNAL_ERROR;
843 RdpeiClientContext* context = (RdpeiClientContext*)callback->plugin->pInterface;
845 return ERROR_INTERNAL_ERROR;
847 IFCALLRET(context->ResumeTouch, error, context);
850 WLog_Print(rdpei->base.log, WLOG_ERROR,
"rdpei->ResumeTouch failed with error %" PRIu32
"!",
864 UINT32 pduLength = 0;
868 return ERROR_INTERNAL_ERROR;
870 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
872 return ERROR_INTERNAL_ERROR;
874 if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 6))
875 return ERROR_INVALID_DATA;
877 Stream_Read_UINT16(s, eventId);
878 Stream_Read_UINT32(s, pduLength);
879 #ifdef WITH_DEBUG_RDPEI
880 WLog_Print(rdpei->base.log, WLOG_DEBUG,
881 "rdpei_recv_pdu: eventId: %" PRIu16
" (%s) length: %" PRIu32
"", eventId,
882 rdpei_eventid_string(eventId), pduLength);
885 if ((pduLength < 6) || !Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, pduLength - 6))
886 return ERROR_INVALID_DATA;
890 case EVENTID_SC_READY:
891 if ((error = rdpei_recv_sc_ready_pdu(callback, s)))
893 WLog_Print(rdpei->base.log, WLOG_ERROR,
894 "rdpei_recv_sc_ready_pdu failed with error %" PRIu32
"!", error);
898 if ((error = rdpei_send_cs_ready_pdu(callback)))
900 WLog_Print(rdpei->base.log, WLOG_ERROR,
901 "rdpei_send_cs_ready_pdu failed with error %" PRIu32
"!", error);
907 case EVENTID_SUSPEND_TOUCH:
908 if ((error = rdpei_recv_suspend_touch_pdu(callback, s)))
910 WLog_Print(rdpei->base.log, WLOG_ERROR,
911 "rdpei_recv_suspend_touch_pdu failed with error %" PRIu32
"!", error);
917 case EVENTID_RESUME_TOUCH:
918 if ((error = rdpei_recv_resume_touch_pdu(callback, s)))
920 WLog_Print(rdpei->base.log, WLOG_ERROR,
921 "rdpei_recv_resume_touch_pdu failed with error %" PRIu32
"!", error);
931 return CHANNEL_RC_OK;
939 static UINT rdpei_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
942 return rdpei_recv_pdu(callback, data);
950 static UINT rdpei_on_close(IWTSVirtualChannelCallback* pChannelCallback)
955 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
956 if (rdpei && rdpei->base.listener_callback)
958 if (rdpei->base.listener_callback->channel_callback == callback)
959 rdpei->base.listener_callback->channel_callback = NULL;
963 return CHANNEL_RC_OK;
970 static UINT32 rdpei_get_version(RdpeiClientContext* context)
972 RDPEI_PLUGIN* rdpei = NULL;
973 if (!context || !context->handle)
975 rdpei = (RDPEI_PLUGIN*)context->handle;
976 return rdpei->version;
979 static UINT32 rdpei_get_features(RdpeiClientContext* context)
981 RDPEI_PLUGIN* rdpei = NULL;
982 if (!context || !context->handle)
984 rdpei = (RDPEI_PLUGIN*)context->handle;
985 return rdpei->features;
995 UINT64 currentTime = GetTickCount64();
996 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1000 callback = rdpei->base.listener_callback->channel_callback;
1004 return CHANNEL_RC_OK;
1006 if (!rdpei->previousFrameTime && !rdpei->currentFrameTime)
1008 rdpei->currentFrameTime = currentTime;
1009 frame->frameOffset = 0;
1013 rdpei->currentFrameTime = currentTime;
1014 frame->frameOffset = rdpei->currentFrameTime - rdpei->previousFrameTime;
1017 if ((error = rdpei_send_touch_event_pdu(callback, frame)))
1019 WLog_Print(rdpei->base.log, WLOG_ERROR,
1020 "rdpei_send_touch_event_pdu failed with error %" PRIu32
"!", error);
1024 rdpei->previousFrameTime = rdpei->currentFrameTime;
1036 RDPEI_PLUGIN* rdpei = NULL;
1037 if (!context || !contact || !context->handle)
1038 return ERROR_INTERNAL_ERROR;
1040 rdpei = (RDPEI_PLUGIN*)context->handle;
1042 EnterCriticalSection(&rdpei->lock);
1043 contactPoint = &rdpei->contactPoints[contact->contactId];
1044 contactPoint->data = *contact;
1045 contactPoint->dirty = TRUE;
1046 (void)SetEvent(rdpei->event);
1047 LeaveCriticalSection(&rdpei->lock);
1049 return CHANNEL_RC_OK;
1052 static UINT rdpei_touch_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1053 INT32 x, INT32 y, INT32* contactId, UINT32 fieldFlags, va_list ap)
1055 INT64 contactIdlocal = -1;
1057 UINT error = CHANNEL_RC_OK;
1059 if (!context || !contactId || !context->handle)
1060 return ERROR_INTERNAL_ERROR;
1062 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1064 EnterCriticalSection(&rdpei->lock);
1065 const BOOL begin = (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN) != 0;
1066 contactPoint = rdpei_contact(rdpei, externalId, !begin);
1068 contactIdlocal = contactPoint->contactId;
1069 LeaveCriticalSection(&rdpei->lock);
1071 if (contactIdlocal > UINT32_MAX)
1072 return ERROR_INVALID_PARAMETER;
1074 if (contactIdlocal >= 0)
1079 contact.contactId = (UINT32)contactIdlocal;
1080 contact.contactFlags = contactFlags;
1081 contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1083 if (fieldFlags & CONTACT_DATA_CONTACTRECT_PRESENT)
1085 INT32 val = va_arg(ap, INT32);
1086 contact.contactRectLeft = WINPR_ASSERTING_INT_CAST(INT16, val);
1088 val = va_arg(ap, INT32);
1089 contact.contactRectTop = WINPR_ASSERTING_INT_CAST(INT16, val);
1091 val = va_arg(ap, INT32);
1092 contact.contactRectRight = WINPR_ASSERTING_INT_CAST(INT16, val);
1094 val = va_arg(ap, INT32);
1095 contact.contactRectBottom = WINPR_ASSERTING_INT_CAST(INT16, val);
1097 if (fieldFlags & CONTACT_DATA_ORIENTATION_PRESENT)
1099 UINT32 p = va_arg(ap, UINT32);
1102 WLog_Print(rdpei->base.log, WLOG_WARN,
1103 "TouchContact %" PRId64
": Invalid orientation value %" PRIu32
1104 "degree, clamping to 359 degree",
1108 contact.orientation = p;
1110 if (fieldFlags & CONTACT_DATA_PRESSURE_PRESENT)
1112 UINT32 p = va_arg(ap, UINT32);
1115 WLog_Print(rdpei->base.log, WLOG_WARN,
1116 "TouchContact %" PRId64
": Invalid pressure value %" PRIu32
1117 ", clamping to 1024",
1121 contact.pressure = p;
1124 error = context->AddContact(context, &contact);
1128 *contactId = (INT32)contactIdlocal;
1137 static UINT rdpei_touch_begin(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1142 rc = rdpei_touch_process(context, externalId,
1143 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1144 RDPINPUT_CONTACT_FLAG_INCONTACT,
1145 x, y, contactId, 0, ap);
1154 static UINT rdpei_touch_update(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1159 rc = rdpei_touch_process(context, externalId,
1160 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1161 RDPINPUT_CONTACT_FLAG_INCONTACT,
1162 x, y, contactId, 0, ap);
1171 static UINT rdpei_touch_end(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1176 error = rdpei_touch_process(context, externalId,
1177 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1178 RDPINPUT_CONTACT_FLAG_INCONTACT,
1179 x, y, contactId, 0, ap);
1180 if (error != CHANNEL_RC_OK)
1183 rdpei_touch_process(context, externalId, RDPINPUT_CONTACT_FLAG_UP, x, y, contactId, 0, ap);
1192 static UINT rdpei_touch_cancel(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1197 rc = rdpei_touch_process(context, externalId,
1198 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_CANCELED, x, y,
1203 static UINT rdpei_touch_raw_event(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1204 INT32* contactId, UINT32 flags, UINT32 fieldFlags, ...)
1208 va_start(ap, fieldFlags);
1209 rc = rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, ap);
1214 static UINT rdpei_touch_raw_event_va(RdpeiClientContext* context, INT32 externalId, INT32 x,
1215 INT32 y, INT32* contactId, UINT32 flags, UINT32 fieldFlags,
1218 return rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, args);
1227 for (UINT32 x = 0; x < rdpei->maxPenContacts; x++)
1232 if (contact->active)
1234 if (contact->externalId == externalId)
1240 if (!contact->active)
1242 contact->externalId = externalId;
1243 contact->active = TRUE;
1251 static UINT rdpei_add_pen(RdpeiClientContext* context, INT32 externalId,
1254 RDPEI_PLUGIN* rdpei = NULL;
1257 if (!context || !contact || !context->handle)
1258 return ERROR_INTERNAL_ERROR;
1260 rdpei = (RDPEI_PLUGIN*)context->handle;
1262 EnterCriticalSection(&rdpei->lock);
1263 contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1266 contactPoint->data = *contact;
1267 contactPoint->dirty = TRUE;
1268 (void)SetEvent(rdpei->event);
1270 LeaveCriticalSection(&rdpei->lock);
1272 return CHANNEL_RC_OK;
1275 static UINT rdpei_pen_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1276 UINT32 fieldFlags, INT32 x, INT32 y, va_list ap)
1279 RDPEI_PLUGIN* rdpei = NULL;
1280 UINT error = CHANNEL_RC_OK;
1282 if (!context || !context->handle)
1283 return ERROR_INTERNAL_ERROR;
1285 rdpei = (RDPEI_PLUGIN*)context->handle;
1287 EnterCriticalSection(&rdpei->lock);
1289 contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1292 const UINT32 mask = RDPINPUT_CONTACT_FLAG_INRANGE;
1293 if ((contactFlags & mask) == mask)
1295 contactPoint = rdpei_pen_contact(rdpei, externalId, FALSE);
1298 LeaveCriticalSection(&rdpei->lock);
1299 if (contactPoint != NULL)
1305 contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1307 contact.contactFlags = contactFlags;
1308 if (fieldFlags & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
1310 const UINT32 val = va_arg(ap, UINT32);
1311 contact.penFlags = WINPR_ASSERTING_INT_CAST(UINT16, val);
1313 if (fieldFlags & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
1315 const UINT32 val = va_arg(ap, UINT32);
1316 contact.pressure = WINPR_ASSERTING_INT_CAST(UINT16, val);
1318 if (fieldFlags & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
1320 const UINT32 val = va_arg(ap, UINT32);
1321 contact.rotation = WINPR_ASSERTING_INT_CAST(UINT16, val);
1323 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
1325 const INT32 val = va_arg(ap, INT32);
1326 contact.tiltX = WINPR_ASSERTING_INT_CAST(INT16, val);
1328 if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
1330 const INT32 val = va_arg(ap, INT32);
1331 WINPR_ASSERT((val >= INT16_MIN) && (val <= INT16_MAX));
1332 contact.tiltY = WINPR_ASSERTING_INT_CAST(INT16, val);
1335 error = context->AddPen(context, externalId, &contact);
1346 static UINT rdpei_pen_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1347 INT32 x, INT32 y, ...)
1353 error = rdpei_pen_process(context, externalId,
1354 RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1355 RDPINPUT_CONTACT_FLAG_INCONTACT,
1356 fieldFlags, x, y, ap);
1367 static UINT rdpei_pen_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1368 INT32 x, INT32 y, ...)
1374 error = rdpei_pen_process(context, externalId,
1375 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1376 RDPINPUT_CONTACT_FLAG_INCONTACT,
1377 fieldFlags, x, y, ap);
1387 static UINT rdpei_pen_end(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, INT32 x,
1393 error = rdpei_pen_process(context, externalId,
1394 RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_INRANGE, fieldFlags,
1405 static UINT rdpei_pen_hover_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1406 INT32 x, INT32 y, ...)
1412 error = rdpei_pen_process(context, externalId,
1413 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1414 fieldFlags, x, y, ap);
1425 static UINT rdpei_pen_hover_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1426 INT32 x, INT32 y, ...)
1432 error = rdpei_pen_process(context, externalId,
1433 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1434 fieldFlags, x, y, ap);
1445 static UINT rdpei_pen_hover_cancel(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1446 INT32 x, INT32 y, ...)
1452 error = rdpei_pen_process(context, externalId,
1453 RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_CANCELED,
1454 fieldFlags, x, y, ap);
1460 static UINT rdpei_pen_raw_event(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1461 UINT32 fieldFlags, INT32 x, INT32 y, ...)
1467 error = rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, ap);
1472 static UINT rdpei_pen_raw_event_va(RdpeiClientContext* context, INT32 externalId,
1473 UINT32 contactFlags, UINT32 fieldFlags, INT32 x, INT32 y,
1476 return rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, args);
1479 static UINT init_plugin_cb(
GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext, rdpSettings* settings)
1481 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1484 WINPR_UNUSED(settings);
1486 rdpei->version = RDPINPUT_PROTOCOL_V300;
1487 rdpei->currentFrameTime = 0;
1488 rdpei->previousFrameTime = 0;
1489 rdpei->maxTouchContacts = MAX_CONTACTS;
1490 rdpei->maxPenContacts = MAX_PEN_CONTACTS;
1491 rdpei->rdpcontext = rcontext;
1493 WINPR_ASSERT(rdpei->base.log);
1495 InitializeCriticalSection(&rdpei->lock);
1496 rdpei->event = CreateEventA(NULL, TRUE, FALSE, NULL);
1499 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1500 return CHANNEL_RC_NO_MEMORY;
1503 RdpeiClientContext* context = (RdpeiClientContext*)calloc(1,
sizeof(RdpeiClientContext));
1506 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1507 return CHANNEL_RC_NO_MEMORY;
1510 context->clientFeaturesMask = UINT32_MAX;
1511 context->handle = (
void*)rdpei;
1512 context->GetVersion = rdpei_get_version;
1513 context->GetFeatures = rdpei_get_features;
1514 context->AddContact = rdpei_add_contact;
1515 context->TouchBegin = rdpei_touch_begin;
1516 context->TouchUpdate = rdpei_touch_update;
1517 context->TouchEnd = rdpei_touch_end;
1518 context->TouchCancel = rdpei_touch_cancel;
1519 context->TouchRawEvent = rdpei_touch_raw_event;
1520 context->TouchRawEventVA = rdpei_touch_raw_event_va;
1521 context->AddPen = rdpei_add_pen;
1522 context->PenBegin = rdpei_pen_begin;
1523 context->PenUpdate = rdpei_pen_update;
1524 context->PenEnd = rdpei_pen_end;
1525 context->PenHoverBegin = rdpei_pen_hover_begin;
1526 context->PenHoverUpdate = rdpei_pen_hover_update;
1527 context->PenHoverCancel = rdpei_pen_hover_cancel;
1528 context->PenRawEvent = rdpei_pen_raw_event;
1529 context->PenRawEventVA = rdpei_pen_raw_event_va;
1531 rdpei->context = context;
1532 rdpei->base.iface.pInterface = (
void*)context;
1538 rdpei->running = TRUE;
1540 rdpei->thread = CreateThread(NULL, 0, rdpei_periodic_update, rdpei, 0, NULL);
1543 WLog_Print(rdpei->base.log, WLOG_ERROR,
"calloc failed!");
1544 return CHANNEL_RC_NO_MEMORY;
1549 if (!freerdp_client_channel_register(rdpei->rdpcontext->channels, rdpei->event,
1550 rdpei_poll_run, rdpei))
1551 return ERROR_INTERNAL_ERROR;
1554 return CHANNEL_RC_OK;
1559 RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1560 WINPR_ASSERT(rdpei);
1562 rdpei->running = FALSE;
1564 (void)SetEvent(rdpei->event);
1568 (void)WaitForSingleObject(rdpei->thread, INFINITE);
1569 (void)CloseHandle(rdpei->thread);
1572 if (rdpei->event && !rdpei->async)
1573 (void)freerdp_client_channel_unregister(rdpei->rdpcontext->channels, rdpei->event);
1576 (void)CloseHandle(rdpei->event);
1578 DeleteCriticalSection(&rdpei->lock);
1579 free(rdpei->context);
1582 static const IWTSVirtualChannelCallback geometry_callbacks = { rdpei_on_data_received,
1584 rdpei_on_close, NULL };
1591 FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpei_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1593 return freerdp_generic_DVCPluginEntry(pEntryPoints, RDPEI_TAG, RDPEI_DVC_CHANNEL_NAME,
1595 &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.