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