FreeRDP
client/rdpei_main.c
1 
22 #include <freerdp/config.h>
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdint.h>
28 
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>
37 
38 #include <freerdp/addin.h>
39 #include <freerdp/freerdp.h>
40 #include <freerdp/client/channels.h>
41 
42 #include "rdpei_common.h"
43 
44 #include "rdpei_main.h"
45 
46 #define RDPEI_TAG CHANNELS_TAG("rdpei.client")
47 
68 #define MAX_CONTACTS 64
69 #define MAX_PEN_CONTACTS 4
70 
71 typedef struct
72 {
74 
75  RdpeiClientContext* context;
76 
77  UINT32 version;
78  UINT32 features; /* SC_READY_MULTIPEN_INJECTION_SUPPORTED */
79  UINT16 maxTouchContacts;
80  UINT64 currentFrameTime;
81  UINT64 previousFrameTime;
82  RDPINPUT_CONTACT_POINT contactPoints[MAX_CONTACTS];
83 
84  UINT64 currentPenFrameTime;
85  UINT64 previousPenFrameTime;
86  UINT16 maxPenContacts;
87  RDPINPUT_PEN_CONTACT_POINT penContactPoints[MAX_PEN_CONTACTS];
88 
89  CRITICAL_SECTION lock;
90  rdpContext* rdpcontext;
91 
92  HANDLE thread;
93 
94  HANDLE event;
95  UINT64 lastPollEventTime;
96  BOOL running;
97  BOOL async;
98 } RDPEI_PLUGIN;
99 
105 static UINT rdpei_send_frame(RdpeiClientContext* context, RDPINPUT_TOUCH_FRAME* frame);
106 
107 #ifdef WITH_DEBUG_RDPEI
108 static const char* rdpei_eventid_string(UINT16 event)
109 {
110  switch (event)
111  {
112  case EVENTID_SC_READY:
113  return "EVENTID_SC_READY";
114  case EVENTID_CS_READY:
115  return "EVENTID_CS_READY";
116  case EVENTID_TOUCH:
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";
124  case EVENTID_PEN:
125  return "EVENTID_PEN";
126  default:
127  return "EVENTID_UNKNOWN";
128  }
129 }
130 #endif
131 
132 static RDPINPUT_CONTACT_POINT* rdpei_contact(RDPEI_PLUGIN* rdpei, INT32 externalId, BOOL active)
133 {
134  for (UINT16 i = 0; i < rdpei->maxTouchContacts; i++)
135  {
136  RDPINPUT_CONTACT_POINT* contactPoint = &rdpei->contactPoints[i];
137 
138  if (!contactPoint->active && active)
139  continue;
140  else if (!contactPoint->active && !active)
141  {
142  contactPoint->contactId = i;
143  contactPoint->externalId = externalId;
144  contactPoint->active = TRUE;
145  return contactPoint;
146  }
147  else if (contactPoint->externalId == externalId)
148  {
149  return contactPoint;
150  }
151  }
152  return NULL;
153 }
154 
160 static UINT rdpei_add_frame(RdpeiClientContext* context)
161 {
162  RDPEI_PLUGIN* rdpei = NULL;
163  RDPINPUT_TOUCH_FRAME frame = { 0 };
164  RDPINPUT_CONTACT_DATA contacts[MAX_CONTACTS] = { 0 };
165 
166  if (!context || !context->handle)
167  return ERROR_INTERNAL_ERROR;
168 
169  rdpei = (RDPEI_PLUGIN*)context->handle;
170  frame.contacts = contacts;
171 
172  for (UINT16 i = 0; i < rdpei->maxTouchContacts; i++)
173  {
174  RDPINPUT_CONTACT_POINT* contactPoint = &rdpei->contactPoints[i];
175  RDPINPUT_CONTACT_DATA* contact = &contactPoint->data;
176 
177  if (contactPoint->dirty)
178  {
179  contacts[frame.contactCount] = *contact;
180  rdpei->contactPoints[i].dirty = FALSE;
181  frame.contactCount++;
182  }
183  else if (contactPoint->active)
184  {
185  if (contact->contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
186  {
187  contact->contactFlags = RDPINPUT_CONTACT_FLAG_UPDATE;
188  contact->contactFlags |= RDPINPUT_CONTACT_FLAG_INRANGE;
189  contact->contactFlags |= RDPINPUT_CONTACT_FLAG_INCONTACT;
190  }
191 
192  contacts[frame.contactCount] = *contact;
193  frame.contactCount++;
194  }
195  if (contact->contactFlags & RDPINPUT_CONTACT_FLAG_UP)
196  {
197  contactPoint->active = FALSE;
198  contactPoint->externalId = 0;
199  contactPoint->contactId = 0;
200  }
201  }
202 
203  if (frame.contactCount > 0)
204  {
205  UINT error = rdpei_send_frame(context, &frame);
206  if (error != CHANNEL_RC_OK)
207  {
208  WLog_Print(rdpei->base.log, WLOG_ERROR,
209  "rdpei_send_frame failed with error %" PRIu32 "!", error);
210  return error;
211  }
212  }
213  return CHANNEL_RC_OK;
214 }
215 
221 static UINT rdpei_send_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s, UINT16 eventId,
222  size_t pduLength)
223 {
224  UINT status = 0;
225 
226  if (!callback || !s || !callback->channel || !callback->channel->Write)
227  return ERROR_INTERNAL_ERROR;
228 
229  if (pduLength > UINT32_MAX)
230  return ERROR_INVALID_PARAMETER;
231 
232  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
233  if (!rdpei)
234  return ERROR_INTERNAL_ERROR;
235 
236  Stream_SetPosition(s, 0);
237  Stream_Write_UINT16(s, eventId); /* eventId (2 bytes) */
238  Stream_Write_UINT32(s, (UINT32)pduLength); /* pduLength (4 bytes) */
239  Stream_SetPosition(s, Stream_Length(s));
240  status = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
241  NULL);
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);
246 #endif
247  return status;
248 }
249 
250 static UINT rdpei_write_pen_frame(wStream* s, const RDPINPUT_PEN_FRAME* frame)
251 {
252  if (!s || !frame)
253  return ERROR_INTERNAL_ERROR;
254 
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++)
260  {
261  const RDPINPUT_PEN_CONTACT* contact = &frame->contacts[x];
262 
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)
275  {
276  if (!rdpei_write_4byte_unsigned(s, contact->penFlags))
277  return ERROR_OUTOFMEMORY;
278  }
279  if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
280  {
281  if (!rdpei_write_4byte_unsigned(s, contact->pressure))
282  return ERROR_OUTOFMEMORY;
283  }
284  if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
285  {
286  if (!rdpei_write_2byte_unsigned(s, contact->rotation))
287  return ERROR_OUTOFMEMORY;
288  }
289  if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
290  {
291  if (!rdpei_write_2byte_signed(s, contact->tiltX))
292  return ERROR_OUTOFMEMORY;
293  }
294  if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
295  {
296  if (!rdpei_write_2byte_signed(s, contact->tiltY))
297  return ERROR_OUTOFMEMORY;
298  }
299  }
300  return CHANNEL_RC_OK;
301 }
302 
303 static UINT rdpei_send_pen_event_pdu(GENERIC_CHANNEL_CALLBACK* callback, size_t frameOffset,
304  const RDPINPUT_PEN_FRAME* frames, size_t count)
305 {
306  UINT status = 0;
307  wStream* s = NULL;
308 
309  WINPR_ASSERT(callback);
310 
311  if (frameOffset > UINT32_MAX)
312  return ERROR_INVALID_PARAMETER;
313  if (count > UINT16_MAX)
314  return ERROR_INVALID_PARAMETER;
315 
316  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
317  if (!rdpei)
318  return ERROR_INTERNAL_ERROR;
319 
320  if (!frames || (count == 0))
321  return ERROR_INTERNAL_ERROR;
322 
323  s = Stream_New(NULL, 64);
324 
325  if (!s)
326  {
327  WLog_Print(rdpei->base.log, WLOG_ERROR, "Stream_New failed!");
328  return CHANNEL_RC_NO_MEMORY;
329  }
330 
331  Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
336  rdpei_write_4byte_unsigned(s,
337  (UINT32)frameOffset); /* encodeTime (FOUR_BYTE_UNSIGNED_INTEGER) */
338  rdpei_write_2byte_unsigned(s, (UINT16)count); /* (frameCount) TWO_BYTE_UNSIGNED_INTEGER */
339 
340  for (size_t x = 0; x < count; x++)
341  {
342  if ((status = rdpei_write_pen_frame(s, &frames[x])))
343  {
344  WLog_Print(rdpei->base.log, WLOG_ERROR,
345  "rdpei_write_pen_frame failed with error %" PRIu32 "!", status);
346  Stream_Free(s, TRUE);
347  return status;
348  }
349  }
350  Stream_SealLength(s);
351 
352  status = rdpei_send_pdu(callback, s, EVENTID_PEN, Stream_Length(s));
353  Stream_Free(s, TRUE);
354  return status;
355 }
356 
357 static UINT rdpei_send_pen_frame(RdpeiClientContext* context, RDPINPUT_PEN_FRAME* frame)
358 {
359  const UINT64 currentTime = GetTickCount64();
360  RDPEI_PLUGIN* rdpei = NULL;
361  GENERIC_CHANNEL_CALLBACK* callback = NULL;
362  UINT error = 0;
363 
364  if (!context)
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;
371  if (freerdp_settings_get_bool(rdpei->rdpcontext->settings, FreeRDP_SuspendInput))
372  return CHANNEL_RC_OK;
373 
374  callback = rdpei->base.listener_callback->channel_callback;
375  /* Just ignore the event if the channel is not connected */
376  if (!callback)
377  return CHANNEL_RC_OK;
378 
379  if (!rdpei->previousPenFrameTime && !rdpei->currentPenFrameTime)
380  {
381  rdpei->currentPenFrameTime = currentTime;
382  frame->frameOffset = 0;
383  }
384  else
385  {
386  rdpei->currentPenFrameTime = currentTime;
387  frame->frameOffset = rdpei->currentPenFrameTime - rdpei->previousPenFrameTime;
388  }
389 
390  if ((error = rdpei_send_pen_event_pdu(callback, frame->frameOffset, frame, 1)))
391  return error;
392 
393  rdpei->previousPenFrameTime = rdpei->currentPenFrameTime;
394  return error;
395 }
396 
397 static UINT rdpei_add_pen_frame(RdpeiClientContext* context)
398 {
399  RDPEI_PLUGIN* rdpei = NULL;
400  RDPINPUT_PEN_FRAME penFrame = { 0 };
401  RDPINPUT_PEN_CONTACT penContacts[MAX_PEN_CONTACTS] = { 0 };
402 
403  if (!context || !context->handle)
404  return ERROR_INTERNAL_ERROR;
405 
406  rdpei = (RDPEI_PLUGIN*)context->handle;
407 
408  penFrame.contacts = penContacts;
409 
410  for (UINT16 i = 0; i < rdpei->maxPenContacts; i++)
411  {
412  RDPINPUT_PEN_CONTACT_POINT* contact = &(rdpei->penContactPoints[i]);
413 
414  if (contact->dirty)
415  {
416  penContacts[penFrame.contactCount++] = contact->data;
417  contact->dirty = FALSE;
418  }
419  else if (contact->active)
420  {
421  if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
422  {
423  contact->data.contactFlags = RDPINPUT_CONTACT_FLAG_UPDATE;
424  contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INRANGE;
425  contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INCONTACT;
426  }
427 
428  penContacts[penFrame.contactCount++] = contact->data;
429  }
430  if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
431  {
432  contact->externalId = 0;
433  contact->active = FALSE;
434  }
435  }
436 
437  if (penFrame.contactCount > 0)
438  return rdpei_send_pen_frame(context, &penFrame);
439  return CHANNEL_RC_OK;
440 }
441 
442 static UINT rdpei_update(wLog* log, RdpeiClientContext* context)
443 {
444  UINT error = rdpei_add_frame(context);
445  if (error != CHANNEL_RC_OK)
446  {
447  WLog_Print(log, WLOG_ERROR, "rdpei_add_frame failed with error %" PRIu32 "!", error);
448  return error;
449  }
450 
451  return rdpei_add_pen_frame(context);
452 }
453 
454 static BOOL rdpei_poll_run_unlocked(rdpContext* context, void* userdata)
455 {
456  RDPEI_PLUGIN* rdpei = userdata;
457  WINPR_ASSERT(rdpei);
458  WINPR_ASSERT(context);
459 
460  const UINT64 now = GetTickCount64();
461 
462  /* Send an event every ~20ms */
463  if ((now < rdpei->lastPollEventTime) || (now - rdpei->lastPollEventTime < 20ULL))
464  return TRUE;
465 
466  rdpei->lastPollEventTime = now;
467 
468  const UINT error = rdpei_update(rdpei->base.log, rdpei->context);
469 
470  (void)ResetEvent(rdpei->event);
471 
472  if (error != CHANNEL_RC_OK)
473  {
474  WLog_Print(rdpei->base.log, WLOG_ERROR, "rdpei_add_frame failed with error %" PRIu32 "!",
475  error);
476  setChannelError(context, error, "rdpei_add_frame reported an error");
477  return FALSE;
478  }
479 
480  return TRUE;
481 }
482 
483 static BOOL rdpei_poll_run(rdpContext* context, void* userdata)
484 {
485  RDPEI_PLUGIN* rdpei = userdata;
486  WINPR_ASSERT(rdpei);
487 
488  EnterCriticalSection(&rdpei->lock);
489  BOOL rc = rdpei_poll_run_unlocked(context, userdata);
490  LeaveCriticalSection(&rdpei->lock);
491  return rc;
492 }
493 
494 static DWORD WINAPI rdpei_periodic_update(LPVOID arg)
495 {
496  DWORD status = 0;
497  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)arg;
498  UINT error = CHANNEL_RC_OK;
499  RdpeiClientContext* context = NULL;
500 
501  if (!rdpei)
502  {
503  error = ERROR_INVALID_PARAMETER;
504  goto out;
505  }
506 
507  context = rdpei->context;
508 
509  if (!context)
510  {
511  error = ERROR_INVALID_PARAMETER;
512  goto out;
513  }
514 
515  while (rdpei->running)
516  {
517  status = WaitForSingleObject(rdpei->event, 20);
518 
519  if (status == WAIT_FAILED)
520  {
521  error = GetLastError();
522  WLog_Print(rdpei->base.log, WLOG_ERROR,
523  "WaitForMultipleObjects failed with error %" PRIu32 "!", error);
524  break;
525  }
526 
527  if (!rdpei_poll_run(rdpei->rdpcontext, rdpei))
528  error = ERROR_INTERNAL_ERROR;
529  }
530 
531 out:
532 
533  if (error && rdpei && rdpei->rdpcontext)
534  setChannelError(rdpei->rdpcontext, error, "rdpei_schedule_thread reported an error");
535 
536  if (rdpei)
537  rdpei->running = FALSE;
538 
539  ExitThread(error);
540  return error;
541 }
542 
548 static UINT rdpei_send_cs_ready_pdu(GENERIC_CHANNEL_CALLBACK* callback)
549 {
550  UINT status = 0;
551  wStream* s = NULL;
552  UINT32 flags = 0;
553  UINT32 pduLength = 0;
554  RDPEI_PLUGIN* rdpei = NULL;
555 
556  if (!callback || !callback->plugin)
557  return ERROR_INTERNAL_ERROR;
558  rdpei = (RDPEI_PLUGIN*)callback->plugin;
559 
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;
565 
566  pduLength = RDPINPUT_HEADER_LENGTH + 10;
567  s = Stream_New(NULL, pduLength);
568 
569  if (!s)
570  {
571  WLog_Print(rdpei->base.log, WLOG_ERROR, "Stream_New failed!");
572  return CHANNEL_RC_NO_MEMORY;
573  }
574 
575  Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
576  Stream_Write_UINT32(s, flags); /* flags (4 bytes) */
577  Stream_Write_UINT32(s, rdpei->version); /* protocolVersion (4 bytes) */
578  Stream_Write_UINT16(s, rdpei->maxTouchContacts); /* maxTouchContacts (2 bytes) */
579  Stream_SealLength(s);
580  status = rdpei_send_pdu(callback, s, EVENTID_CS_READY, pduLength);
581  Stream_Free(s, TRUE);
582  return status;
583 }
584 
585 #if defined(WITH_DEBUG_RDPEI)
586 static void rdpei_print_contact_flags(wLog* log, UINT32 contactFlags)
587 {
588  if (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
589  WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_DOWN");
590 
591  if (contactFlags & RDPINPUT_CONTACT_FLAG_UPDATE)
592  WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_UPDATE");
593 
594  if (contactFlags & RDPINPUT_CONTACT_FLAG_UP)
595  WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_UP");
596 
597  if (contactFlags & RDPINPUT_CONTACT_FLAG_INRANGE)
598  WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_INRANGE");
599 
600  if (contactFlags & RDPINPUT_CONTACT_FLAG_INCONTACT)
601  WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_INCONTACT");
602 
603  if (contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
604  WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_CANCELED");
605 }
606 #endif
607 
608 static INT16 bounded(INT32 val)
609 {
610  if (val < INT16_MIN)
611  return INT16_MIN;
612  if (val > INT16_MAX)
613  return INT16_MAX;
614  return (INT16)val;
615 }
616 
622 static UINT rdpei_write_touch_frame(wLog* log, wStream* s, RDPINPUT_TOUCH_FRAME* frame)
623 {
624  int rectSize = 2;
625  RDPINPUT_CONTACT_DATA* contact = NULL;
626  if (!s || !frame)
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);
631 #endif
632  rdpei_write_2byte_unsigned(s,
633  frame->contactCount); /* contactCount (TWO_BYTE_UNSIGNED_INTEGER) */
638  rdpei_write_8byte_unsigned(s, frame->frameOffset *
639  1000); /* frameOffset (EIGHT_BYTE_UNSIGNED_INTEGER) */
640 
641  if (!Stream_EnsureRemainingCapacity(s, (size_t)frame->contactCount * 64))
642  {
643  WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
644  return CHANNEL_RC_NO_MEMORY;
645  }
646 
647  for (UINT32 index = 0; index < frame->contactCount; index++)
648  {
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,
657  contact->contactId);
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);
665 #endif
666  Stream_Write_UINT8(
667  s, WINPR_ASSERTING_INT_CAST(uint8_t, contact->contactId)); /* contactId (1 byte) */
668  /* fieldsPresent (TWO_BYTE_UNSIGNED_INTEGER) */
669  rdpei_write_2byte_unsigned(s, contact->fieldsPresent);
670  rdpei_write_4byte_signed(s, contact->x); /* x (FOUR_BYTE_SIGNED_INTEGER) */
671  rdpei_write_4byte_signed(s, contact->y); /* y (FOUR_BYTE_SIGNED_INTEGER) */
672  /* contactFlags (FOUR_BYTE_UNSIGNED_INTEGER) */
673  rdpei_write_4byte_unsigned(s, contact->contactFlags);
674 
675  if (contact->fieldsPresent & CONTACT_DATA_CONTACTRECT_PRESENT)
676  {
677  /* contactRectLeft (TWO_BYTE_SIGNED_INTEGER) */
678  rdpei_write_2byte_signed(s, contact->contactRectLeft);
679  /* contactRectTop (TWO_BYTE_SIGNED_INTEGER) */
680  rdpei_write_2byte_signed(s, contact->contactRectTop);
681  /* contactRectRight (TWO_BYTE_SIGNED_INTEGER) */
682  rdpei_write_2byte_signed(s, contact->contactRectRight);
683  /* contactRectBottom (TWO_BYTE_SIGNED_INTEGER) */
684  rdpei_write_2byte_signed(s, contact->contactRectBottom);
685  }
686 
687  if (contact->fieldsPresent & CONTACT_DATA_ORIENTATION_PRESENT)
688  {
689  /* orientation (FOUR_BYTE_UNSIGNED_INTEGER) */
690  rdpei_write_4byte_unsigned(s, contact->orientation);
691  }
692 
693  if (contact->fieldsPresent & CONTACT_DATA_PRESSURE_PRESENT)
694  {
695  /* pressure (FOUR_BYTE_UNSIGNED_INTEGER) */
696  rdpei_write_4byte_unsigned(s, contact->pressure);
697  }
698  }
699 
700  return CHANNEL_RC_OK;
701 }
702 
708 static UINT rdpei_send_touch_event_pdu(GENERIC_CHANNEL_CALLBACK* callback,
709  RDPINPUT_TOUCH_FRAME* frame)
710 {
711  UINT status = 0;
712 
713  WINPR_ASSERT(callback);
714 
715  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
716  if (!rdpei || !rdpei->rdpcontext)
717  return ERROR_INTERNAL_ERROR;
718  if (freerdp_settings_get_bool(rdpei->rdpcontext->settings, FreeRDP_SuspendInput))
719  return CHANNEL_RC_OK;
720 
721  if (!frame)
722  return ERROR_INTERNAL_ERROR;
723 
724  size_t pduLength = 64ULL + (64ULL * frame->contactCount);
725  wStream* s = Stream_New(NULL, pduLength);
726 
727  if (!s)
728  {
729  WLog_Print(rdpei->base.log, WLOG_ERROR, "Stream_New failed!");
730  return CHANNEL_RC_NO_MEMORY;
731  }
732 
733  Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
738  rdpei_write_4byte_unsigned(
739  s, (UINT32)frame->frameOffset); /* encodeTime (FOUR_BYTE_UNSIGNED_INTEGER) */
740  rdpei_write_2byte_unsigned(s, 1); /* (frameCount) TWO_BYTE_UNSIGNED_INTEGER */
741 
742  status = rdpei_write_touch_frame(rdpei->base.log, s, frame);
743  if (status)
744  {
745  WLog_Print(rdpei->base.log, WLOG_ERROR,
746  "rdpei_write_touch_frame failed with error %" PRIu32 "!", status);
747  Stream_Free(s, TRUE);
748  return status;
749  }
750 
751  Stream_SealLength(s);
752  status = rdpei_send_pdu(callback, s, EVENTID_TOUCH, Stream_Length(s));
753  Stream_Free(s, TRUE);
754  return status;
755 }
756 
762 static UINT rdpei_recv_sc_ready_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
763 {
764  UINT32 features = 0;
765  UINT32 protocolVersion = 0;
766 
767  if (!callback || !callback->plugin)
768  return ERROR_INTERNAL_ERROR;
769 
770  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
771 
772  if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
773  return ERROR_INVALID_DATA;
774  Stream_Read_UINT32(s, protocolVersion); /* protocolVersion (4 bytes) */
775 
776  if (protocolVersion >= RDPINPUT_PROTOCOL_V300)
777  {
778  if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
779  return ERROR_INVALID_DATA;
780  }
781 
782  if (Stream_GetRemainingLength(s) >= 4)
783  Stream_Read_UINT32(s, features);
784 
785  if (rdpei->version > protocolVersion)
786  rdpei->version = protocolVersion;
787  rdpei->features = features;
788 #if 0
789 
790  if (protocolVersion != RDPINPUT_PROTOCOL_V10)
791  {
792  WLog_Print(rdpei->base.log, WLOG_ERROR, "Unknown [MS-RDPEI] protocolVersion: 0x%08"PRIX32"", protocolVersion);
793  return -1;
794  }
795 
796 #endif
797  return CHANNEL_RC_OK;
798 }
799 
805 static UINT rdpei_recv_suspend_touch_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
806 {
807  UINT error = CHANNEL_RC_OK;
808 
809  WINPR_UNUSED(s);
810 
811  if (!callback || !callback->plugin)
812  return ERROR_INTERNAL_ERROR;
813 
814  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
815  RdpeiClientContext* context = rdpei->context;
816  if (!rdpei)
817  return ERROR_INTERNAL_ERROR;
818 
819  IFCALLRET(context->SuspendTouch, error, context);
820 
821  if (error)
822  WLog_Print(rdpei->base.log, WLOG_ERROR,
823  "rdpei->SuspendTouch failed with error %" PRIu32 "!", error);
824 
825  return error;
826 }
827 
833 static UINT rdpei_recv_resume_touch_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
834 {
835  UINT error = CHANNEL_RC_OK;
836  if (!s || !callback)
837  return ERROR_INTERNAL_ERROR;
838 
839  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
840  if (!rdpei)
841  return ERROR_INTERNAL_ERROR;
842 
843  RdpeiClientContext* context = (RdpeiClientContext*)callback->plugin->pInterface;
844  if (!context)
845  return ERROR_INTERNAL_ERROR;
846 
847  IFCALLRET(context->ResumeTouch, error, context);
848 
849  if (error)
850  WLog_Print(rdpei->base.log, WLOG_ERROR, "rdpei->ResumeTouch failed with error %" PRIu32 "!",
851  error);
852 
853  return error;
854 }
855 
861 static UINT rdpei_recv_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
862 {
863  UINT16 eventId = 0;
864  UINT32 pduLength = 0;
865  UINT error = 0;
866 
867  if (!callback || !s)
868  return ERROR_INTERNAL_ERROR;
869 
870  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
871  if (!rdpei)
872  return ERROR_INTERNAL_ERROR;
873 
874  if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 6))
875  return ERROR_INVALID_DATA;
876 
877  Stream_Read_UINT16(s, eventId); /* eventId (2 bytes) */
878  Stream_Read_UINT32(s, pduLength); /* pduLength (4 bytes) */
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);
883 #endif
884 
885  if ((pduLength < 6) || !Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, pduLength - 6))
886  return ERROR_INVALID_DATA;
887 
888  switch (eventId)
889  {
890  case EVENTID_SC_READY:
891  if ((error = rdpei_recv_sc_ready_pdu(callback, s)))
892  {
893  WLog_Print(rdpei->base.log, WLOG_ERROR,
894  "rdpei_recv_sc_ready_pdu failed with error %" PRIu32 "!", error);
895  return error;
896  }
897 
898  if ((error = rdpei_send_cs_ready_pdu(callback)))
899  {
900  WLog_Print(rdpei->base.log, WLOG_ERROR,
901  "rdpei_send_cs_ready_pdu failed with error %" PRIu32 "!", error);
902  return error;
903  }
904 
905  break;
906 
907  case EVENTID_SUSPEND_TOUCH:
908  if ((error = rdpei_recv_suspend_touch_pdu(callback, s)))
909  {
910  WLog_Print(rdpei->base.log, WLOG_ERROR,
911  "rdpei_recv_suspend_touch_pdu failed with error %" PRIu32 "!", error);
912  return error;
913  }
914 
915  break;
916 
917  case EVENTID_RESUME_TOUCH:
918  if ((error = rdpei_recv_resume_touch_pdu(callback, s)))
919  {
920  WLog_Print(rdpei->base.log, WLOG_ERROR,
921  "rdpei_recv_resume_touch_pdu failed with error %" PRIu32 "!", error);
922  return error;
923  }
924 
925  break;
926 
927  default:
928  break;
929  }
930 
931  return CHANNEL_RC_OK;
932 }
933 
939 static UINT rdpei_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
940 {
941  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
942  return rdpei_recv_pdu(callback, data);
943 }
944 
950 static UINT rdpei_on_close(IWTSVirtualChannelCallback* pChannelCallback)
951 {
952  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
953  if (callback)
954  {
955  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
956  if (rdpei && rdpei->base.listener_callback)
957  {
958  if (rdpei->base.listener_callback->channel_callback == callback)
959  rdpei->base.listener_callback->channel_callback = NULL;
960  }
961  }
962  free(callback);
963  return CHANNEL_RC_OK;
964 }
965 
970 static UINT32 rdpei_get_version(RdpeiClientContext* context)
971 {
972  RDPEI_PLUGIN* rdpei = NULL;
973  if (!context || !context->handle)
974  return 0;
975  rdpei = (RDPEI_PLUGIN*)context->handle;
976  return rdpei->version;
977 }
978 
979 static UINT32 rdpei_get_features(RdpeiClientContext* context)
980 {
981  RDPEI_PLUGIN* rdpei = NULL;
982  if (!context || !context->handle)
983  return 0;
984  rdpei = (RDPEI_PLUGIN*)context->handle;
985  return rdpei->features;
986 }
987 
993 UINT rdpei_send_frame(RdpeiClientContext* context, RDPINPUT_TOUCH_FRAME* frame)
994 {
995  UINT64 currentTime = GetTickCount64();
996  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
997  GENERIC_CHANNEL_CALLBACK* callback = NULL;
998  UINT error = 0;
999 
1000  callback = rdpei->base.listener_callback->channel_callback;
1001 
1002  /* Just ignore the event if the channel is not connected */
1003  if (!callback)
1004  return CHANNEL_RC_OK;
1005 
1006  if (!rdpei->previousFrameTime && !rdpei->currentFrameTime)
1007  {
1008  rdpei->currentFrameTime = currentTime;
1009  frame->frameOffset = 0;
1010  }
1011  else
1012  {
1013  rdpei->currentFrameTime = currentTime;
1014  frame->frameOffset = rdpei->currentFrameTime - rdpei->previousFrameTime;
1015  }
1016 
1017  if ((error = rdpei_send_touch_event_pdu(callback, frame)))
1018  {
1019  WLog_Print(rdpei->base.log, WLOG_ERROR,
1020  "rdpei_send_touch_event_pdu failed with error %" PRIu32 "!", error);
1021  return error;
1022  }
1023 
1024  rdpei->previousFrameTime = rdpei->currentFrameTime;
1025  return error;
1026 }
1027 
1033 static UINT rdpei_add_contact(RdpeiClientContext* context, const RDPINPUT_CONTACT_DATA* contact)
1034 {
1035  RDPINPUT_CONTACT_POINT* contactPoint = NULL;
1036  RDPEI_PLUGIN* rdpei = NULL;
1037  if (!context || !contact || !context->handle)
1038  return ERROR_INTERNAL_ERROR;
1039 
1040  rdpei = (RDPEI_PLUGIN*)context->handle;
1041 
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);
1048 
1049  return CHANNEL_RC_OK;
1050 }
1051 
1052 static UINT rdpei_touch_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1053  INT32 x, INT32 y, INT32* contactId, UINT32 fieldFlags, va_list ap)
1054 {
1055  INT64 contactIdlocal = -1;
1056  RDPINPUT_CONTACT_POINT* contactPoint = NULL;
1057  UINT error = CHANNEL_RC_OK;
1058 
1059  if (!context || !contactId || !context->handle)
1060  return ERROR_INTERNAL_ERROR;
1061 
1062  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1063  /* Create a new contact point in an empty slot */
1064  EnterCriticalSection(&rdpei->lock);
1065  const BOOL begin = (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN) != 0;
1066  contactPoint = rdpei_contact(rdpei, externalId, !begin);
1067  if (contactPoint)
1068  contactIdlocal = contactPoint->contactId;
1069  LeaveCriticalSection(&rdpei->lock);
1070 
1071  if (contactIdlocal > UINT32_MAX)
1072  return ERROR_INVALID_PARAMETER;
1073 
1074  if (contactIdlocal >= 0)
1075  {
1076  RDPINPUT_CONTACT_DATA contact = { 0 };
1077  contact.x = x;
1078  contact.y = y;
1079  contact.contactId = (UINT32)contactIdlocal;
1080  contact.contactFlags = contactFlags;
1081  contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1082 
1083  if (fieldFlags & CONTACT_DATA_CONTACTRECT_PRESENT)
1084  {
1085  INT32 val = va_arg(ap, INT32);
1086  contact.contactRectLeft = WINPR_ASSERTING_INT_CAST(INT16, val);
1087 
1088  val = va_arg(ap, INT32);
1089  contact.contactRectTop = WINPR_ASSERTING_INT_CAST(INT16, val);
1090 
1091  val = va_arg(ap, INT32);
1092  contact.contactRectRight = WINPR_ASSERTING_INT_CAST(INT16, val);
1093 
1094  val = va_arg(ap, INT32);
1095  contact.contactRectBottom = WINPR_ASSERTING_INT_CAST(INT16, val);
1096  }
1097  if (fieldFlags & CONTACT_DATA_ORIENTATION_PRESENT)
1098  {
1099  UINT32 p = va_arg(ap, UINT32);
1100  if (p >= 360)
1101  {
1102  WLog_Print(rdpei->base.log, WLOG_WARN,
1103  "TouchContact %" PRId64 ": Invalid orientation value %" PRIu32
1104  "degree, clamping to 359 degree",
1105  contactIdlocal, p);
1106  p = 359;
1107  }
1108  contact.orientation = p;
1109  }
1110  if (fieldFlags & CONTACT_DATA_PRESSURE_PRESENT)
1111  {
1112  UINT32 p = va_arg(ap, UINT32);
1113  if (p > 1024)
1114  {
1115  WLog_Print(rdpei->base.log, WLOG_WARN,
1116  "TouchContact %" PRId64 ": Invalid pressure value %" PRIu32
1117  ", clamping to 1024",
1118  contactIdlocal, p);
1119  p = 1024;
1120  }
1121  contact.pressure = p;
1122  }
1123 
1124  error = context->AddContact(context, &contact);
1125  }
1126 
1127  if (contactId)
1128  *contactId = (INT32)contactIdlocal;
1129  return error;
1130 }
1131 
1137 static UINT rdpei_touch_begin(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1138  INT32* contactId)
1139 {
1140  UINT rc = 0;
1141  va_list ap = { 0 };
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);
1146  return rc;
1147 }
1148 
1154 static UINT rdpei_touch_update(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1155  INT32* contactId)
1156 {
1157  UINT rc = 0;
1158  va_list ap = { 0 };
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);
1163  return rc;
1164 }
1165 
1171 static UINT rdpei_touch_end(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1172  INT32* contactId)
1173 {
1174  UINT error = 0;
1175  va_list ap = { 0 };
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)
1181  return error;
1182  error =
1183  rdpei_touch_process(context, externalId, RDPINPUT_CONTACT_FLAG_UP, x, y, contactId, 0, ap);
1184  return error;
1185 }
1186 
1192 static UINT rdpei_touch_cancel(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1193  INT32* contactId)
1194 {
1195  UINT rc = 0;
1196  va_list ap = { 0 };
1197  rc = rdpei_touch_process(context, externalId,
1198  RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_CANCELED, x, y,
1199  contactId, 0, ap);
1200  return rc;
1201 }
1202 
1203 static UINT rdpei_touch_raw_event(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1204  INT32* contactId, UINT32 flags, UINT32 fieldFlags, ...)
1205 {
1206  UINT rc = 0;
1207  va_list ap;
1208  va_start(ap, fieldFlags);
1209  rc = rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, ap);
1210  va_end(ap);
1211  return rc;
1212 }
1213 
1214 static UINT rdpei_touch_raw_event_va(RdpeiClientContext* context, INT32 externalId, INT32 x,
1215  INT32 y, INT32* contactId, UINT32 flags, UINT32 fieldFlags,
1216  va_list args)
1217 {
1218  return rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, args);
1219 }
1220 
1221 static RDPINPUT_PEN_CONTACT_POINT* rdpei_pen_contact(RDPEI_PLUGIN* rdpei, INT32 externalId,
1222  BOOL active)
1223 {
1224  if (!rdpei)
1225  return NULL;
1226 
1227  for (UINT32 x = 0; x < rdpei->maxPenContacts; x++)
1228  {
1229  RDPINPUT_PEN_CONTACT_POINT* contact = &rdpei->penContactPoints[x];
1230  if (active)
1231  {
1232  if (contact->active)
1233  {
1234  if (contact->externalId == externalId)
1235  return contact;
1236  }
1237  }
1238  else
1239  {
1240  if (!contact->active)
1241  {
1242  contact->externalId = externalId;
1243  contact->active = TRUE;
1244  return contact;
1245  }
1246  }
1247  }
1248  return NULL;
1249 }
1250 
1251 static UINT rdpei_add_pen(RdpeiClientContext* context, INT32 externalId,
1252  const RDPINPUT_PEN_CONTACT* contact)
1253 {
1254  RDPEI_PLUGIN* rdpei = NULL;
1255  RDPINPUT_PEN_CONTACT_POINT* contactPoint = NULL;
1256 
1257  if (!context || !contact || !context->handle)
1258  return ERROR_INTERNAL_ERROR;
1259 
1260  rdpei = (RDPEI_PLUGIN*)context->handle;
1261 
1262  EnterCriticalSection(&rdpei->lock);
1263  contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1264  if (contactPoint)
1265  {
1266  contactPoint->data = *contact;
1267  contactPoint->dirty = TRUE;
1268  (void)SetEvent(rdpei->event);
1269  }
1270  LeaveCriticalSection(&rdpei->lock);
1271 
1272  return CHANNEL_RC_OK;
1273 }
1274 
1275 static UINT rdpei_pen_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1276  UINT32 fieldFlags, INT32 x, INT32 y, va_list ap)
1277 {
1278  RDPINPUT_PEN_CONTACT_POINT* contactPoint = NULL;
1279  RDPEI_PLUGIN* rdpei = NULL;
1280  UINT error = CHANNEL_RC_OK;
1281 
1282  if (!context || !context->handle)
1283  return ERROR_INTERNAL_ERROR;
1284 
1285  rdpei = (RDPEI_PLUGIN*)context->handle;
1286 
1287  EnterCriticalSection(&rdpei->lock);
1288  // Start a new contact only when it is not active.
1289  contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1290  if (!contactPoint)
1291  {
1292  const UINT32 mask = RDPINPUT_CONTACT_FLAG_INRANGE;
1293  if ((contactFlags & mask) == mask)
1294  {
1295  contactPoint = rdpei_pen_contact(rdpei, externalId, FALSE);
1296  }
1297  }
1298  LeaveCriticalSection(&rdpei->lock);
1299  if (contactPoint != NULL)
1300  {
1301  RDPINPUT_PEN_CONTACT contact = { 0 };
1302 
1303  contact.x = x;
1304  contact.y = y;
1305  contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1306 
1307  contact.contactFlags = contactFlags;
1308  if (fieldFlags & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
1309  {
1310  const UINT32 val = va_arg(ap, UINT32);
1311  contact.penFlags = WINPR_ASSERTING_INT_CAST(UINT16, val);
1312  }
1313  if (fieldFlags & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
1314  {
1315  const UINT32 val = va_arg(ap, UINT32);
1316  contact.pressure = WINPR_ASSERTING_INT_CAST(UINT16, val);
1317  }
1318  if (fieldFlags & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
1319  {
1320  const UINT32 val = va_arg(ap, UINT32);
1321  contact.rotation = WINPR_ASSERTING_INT_CAST(UINT16, val);
1322  }
1323  if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
1324  {
1325  const INT32 val = va_arg(ap, INT32);
1326  contact.tiltX = WINPR_ASSERTING_INT_CAST(INT16, val);
1327  }
1328  if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
1329  {
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);
1333  }
1334 
1335  error = context->AddPen(context, externalId, &contact);
1336  }
1337 
1338  return error;
1339 }
1340 
1346 static UINT rdpei_pen_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1347  INT32 x, INT32 y, ...)
1348 {
1349  UINT error = 0;
1350  va_list ap;
1351 
1352  va_start(ap, 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);
1357  va_end(ap);
1358 
1359  return error;
1360 }
1361 
1367 static UINT rdpei_pen_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1368  INT32 x, INT32 y, ...)
1369 {
1370  UINT error = 0;
1371  va_list ap;
1372 
1373  va_start(ap, 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);
1378  va_end(ap);
1379  return error;
1380 }
1381 
1387 static UINT rdpei_pen_end(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, INT32 x,
1388  INT32 y, ...)
1389 {
1390  UINT error = 0;
1391  va_list ap;
1392  va_start(ap, y);
1393  error = rdpei_pen_process(context, externalId,
1394  RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_INRANGE, fieldFlags,
1395  x, y, ap);
1396  va_end(ap);
1397  return error;
1398 }
1399 
1405 static UINT rdpei_pen_hover_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1406  INT32 x, INT32 y, ...)
1407 {
1408  UINT error = 0;
1409  va_list ap;
1410 
1411  va_start(ap, y);
1412  error = rdpei_pen_process(context, externalId,
1413  RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1414  fieldFlags, x, y, ap);
1415  va_end(ap);
1416 
1417  return error;
1418 }
1419 
1425 static UINT rdpei_pen_hover_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1426  INT32 x, INT32 y, ...)
1427 {
1428  UINT error = 0;
1429  va_list ap;
1430 
1431  va_start(ap, y);
1432  error = rdpei_pen_process(context, externalId,
1433  RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1434  fieldFlags, x, y, ap);
1435  va_end(ap);
1436 
1437  return error;
1438 }
1439 
1445 static UINT rdpei_pen_hover_cancel(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1446  INT32 x, INT32 y, ...)
1447 {
1448  UINT error = 0;
1449  va_list ap;
1450 
1451  va_start(ap, y);
1452  error = rdpei_pen_process(context, externalId,
1453  RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_CANCELED,
1454  fieldFlags, x, y, ap);
1455  va_end(ap);
1456 
1457  return error;
1458 }
1459 
1460 static UINT rdpei_pen_raw_event(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1461  UINT32 fieldFlags, INT32 x, INT32 y, ...)
1462 {
1463  UINT error = 0;
1464  va_list ap;
1465 
1466  va_start(ap, y);
1467  error = rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, ap);
1468  va_end(ap);
1469  return error;
1470 }
1471 
1472 static UINT rdpei_pen_raw_event_va(RdpeiClientContext* context, INT32 externalId,
1473  UINT32 contactFlags, UINT32 fieldFlags, INT32 x, INT32 y,
1474  va_list args)
1475 {
1476  return rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, args);
1477 }
1478 
1479 static UINT init_plugin_cb(GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext, rdpSettings* settings)
1480 {
1481  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1482 
1483  WINPR_ASSERT(base);
1484  WINPR_UNUSED(settings);
1485 
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;
1492 
1493  WINPR_ASSERT(rdpei->base.log);
1494 
1495  InitializeCriticalSection(&rdpei->lock);
1496  rdpei->event = CreateEventA(NULL, TRUE, FALSE, NULL);
1497  if (!rdpei->event)
1498  {
1499  WLog_Print(rdpei->base.log, WLOG_ERROR, "calloc failed!");
1500  return CHANNEL_RC_NO_MEMORY;
1501  }
1502 
1503  RdpeiClientContext* context = (RdpeiClientContext*)calloc(1, sizeof(RdpeiClientContext));
1504  if (!context)
1505  {
1506  WLog_Print(rdpei->base.log, WLOG_ERROR, "calloc failed!");
1507  return CHANNEL_RC_NO_MEMORY;
1508  }
1509 
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;
1530 
1531  rdpei->context = context;
1532  rdpei->base.iface.pInterface = (void*)context;
1533 
1534  rdpei->async =
1535  !freerdp_settings_get_bool(rdpei->rdpcontext->settings, FreeRDP_SynchronousDynamicChannels);
1536  if (rdpei->async)
1537  {
1538  rdpei->running = TRUE;
1539 
1540  rdpei->thread = CreateThread(NULL, 0, rdpei_periodic_update, rdpei, 0, NULL);
1541  if (!rdpei->thread)
1542  {
1543  WLog_Print(rdpei->base.log, WLOG_ERROR, "calloc failed!");
1544  return CHANNEL_RC_NO_MEMORY;
1545  }
1546  }
1547  else
1548  {
1549  if (!freerdp_client_channel_register(rdpei->rdpcontext->channels, rdpei->event,
1550  rdpei_poll_run, rdpei))
1551  return ERROR_INTERNAL_ERROR;
1552  }
1553 
1554  return CHANNEL_RC_OK;
1555 }
1556 
1557 static void terminate_plugin_cb(GENERIC_DYNVC_PLUGIN* base)
1558 {
1559  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1560  WINPR_ASSERT(rdpei);
1561 
1562  rdpei->running = FALSE;
1563  if (rdpei->event)
1564  (void)SetEvent(rdpei->event);
1565 
1566  if (rdpei->thread)
1567  {
1568  (void)WaitForSingleObject(rdpei->thread, INFINITE);
1569  (void)CloseHandle(rdpei->thread);
1570  }
1571 
1572  if (rdpei->event && !rdpei->async)
1573  (void)freerdp_client_channel_unregister(rdpei->rdpcontext->channels, rdpei->event);
1574 
1575  if (rdpei->event)
1576  (void)CloseHandle(rdpei->event);
1577 
1578  DeleteCriticalSection(&rdpei->lock);
1579  free(rdpei->context);
1580 }
1581 
1582 static const IWTSVirtualChannelCallback geometry_callbacks = { rdpei_on_data_received,
1583  NULL, /* Open */
1584  rdpei_on_close, NULL };
1585 
1591 FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpei_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1592 {
1593  return freerdp_generic_DVCPluginEntry(pEntryPoints, RDPEI_TAG, RDPEI_DVC_CHANNEL_NAME,
1594  sizeof(RDPEI_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
1595  &geometry_callbacks, init_plugin_cb, terminate_plugin_cb);
1596 }
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
a frame containing contact points