Coverage Report

Created: 2025-07-01 06:46

/src/FreeRDP/channels/rdpei/client/rdpei_main.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Input Virtual Channel Extension
4
 *
5
 * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2015 Thincast Technologies GmbH
7
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21
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
0
#define RDPEI_TAG CHANNELS_TAG("rdpei.client")
47
48
/**
49
 * Touch Input
50
 * http://msdn.microsoft.com/en-us/library/windows/desktop/dd562197/
51
 *
52
 * Windows Touch Input
53
 * http://msdn.microsoft.com/en-us/library/windows/desktop/dd317321/
54
 *
55
 * Input: Touch injection sample
56
 * http://code.msdn.microsoft.com/windowsdesktop/Touch-Injection-Sample-444d9bf7
57
 *
58
 * Pointer Input Message Reference
59
 * http://msdn.microsoft.com/en-us/library/hh454916/
60
 *
61
 * POINTER_INFO Structure
62
 * http://msdn.microsoft.com/en-us/library/hh454907/
63
 *
64
 * POINTER_TOUCH_INFO Structure
65
 * http://msdn.microsoft.com/en-us/library/hh454910/
66
 */
67
68
0
#define MAX_CONTACTS 64
69
0
#define MAX_PEN_CONTACTS 4
70
71
typedef struct
72
{
73
  GENERIC_DYNVC_PLUGIN base;
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
100
/**
101
 * Function description
102
 *
103
 * @return 0 on success, otherwise a Win32 error code
104
 */
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
0
{
134
0
  for (UINT16 i = 0; i < rdpei->maxTouchContacts; i++)
135
0
  {
136
0
    RDPINPUT_CONTACT_POINT* contactPoint = &rdpei->contactPoints[i];
137
138
0
    if (!contactPoint->active && active)
139
0
      continue;
140
0
    else if (!contactPoint->active && !active)
141
0
    {
142
0
      contactPoint->contactId = i;
143
0
      contactPoint->externalId = externalId;
144
0
      contactPoint->active = TRUE;
145
0
      return contactPoint;
146
0
    }
147
0
    else if (contactPoint->externalId == externalId)
148
0
    {
149
0
      return contactPoint;
150
0
    }
151
0
  }
152
0
  return NULL;
153
0
}
154
155
/**
156
 * Function description
157
 *
158
 * @return 0 on success, otherwise a Win32 error code
159
 */
160
static UINT rdpei_add_frame(RdpeiClientContext* context)
161
0
{
162
0
  RDPEI_PLUGIN* rdpei = NULL;
163
0
  RDPINPUT_TOUCH_FRAME frame = { 0 };
164
0
  RDPINPUT_CONTACT_DATA contacts[MAX_CONTACTS] = { 0 };
165
166
0
  if (!context || !context->handle)
167
0
    return ERROR_INTERNAL_ERROR;
168
169
0
  rdpei = (RDPEI_PLUGIN*)context->handle;
170
0
  frame.contacts = contacts;
171
172
0
  for (UINT16 i = 0; i < rdpei->maxTouchContacts; i++)
173
0
  {
174
0
    RDPINPUT_CONTACT_POINT* contactPoint = &rdpei->contactPoints[i];
175
0
    RDPINPUT_CONTACT_DATA* contact = &contactPoint->data;
176
177
0
    if (contactPoint->dirty)
178
0
    {
179
0
      contacts[frame.contactCount] = *contact;
180
0
      rdpei->contactPoints[i].dirty = FALSE;
181
0
      frame.contactCount++;
182
0
    }
183
0
    else if (contactPoint->active)
184
0
    {
185
0
      if (contact->contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
186
0
      {
187
0
        contact->contactFlags = RDPINPUT_CONTACT_FLAG_UPDATE;
188
0
        contact->contactFlags |= RDPINPUT_CONTACT_FLAG_INRANGE;
189
0
        contact->contactFlags |= RDPINPUT_CONTACT_FLAG_INCONTACT;
190
0
      }
191
192
0
      contacts[frame.contactCount] = *contact;
193
0
      frame.contactCount++;
194
0
    }
195
0
    if (contact->contactFlags & RDPINPUT_CONTACT_FLAG_UP)
196
0
    {
197
0
      contactPoint->active = FALSE;
198
0
      contactPoint->externalId = 0;
199
0
      contactPoint->contactId = 0;
200
0
    }
201
0
  }
202
203
0
  if (frame.contactCount > 0)
204
0
  {
205
0
    UINT error = rdpei_send_frame(context, &frame);
206
0
    if (error != CHANNEL_RC_OK)
207
0
    {
208
0
      WLog_Print(rdpei->base.log, WLOG_ERROR,
209
0
                 "rdpei_send_frame failed with error %" PRIu32 "!", error);
210
0
      return error;
211
0
    }
212
0
  }
213
0
  return CHANNEL_RC_OK;
214
0
}
215
216
/**
217
 * Function description
218
 *
219
 * @return 0 on success, otherwise a Win32 error code
220
 */
221
static UINT rdpei_send_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s, UINT16 eventId,
222
                           size_t pduLength)
223
0
{
224
0
  UINT status = 0;
225
226
0
  if (!callback || !s || !callback->channel || !callback->channel->Write)
227
0
    return ERROR_INTERNAL_ERROR;
228
229
0
  if (pduLength > UINT32_MAX)
230
0
    return ERROR_INVALID_PARAMETER;
231
232
0
  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
233
0
  if (!rdpei)
234
0
    return ERROR_INTERNAL_ERROR;
235
236
0
  Stream_SetPosition(s, 0);
237
0
  Stream_Write_UINT16(s, eventId);   /* eventId (2 bytes) */
238
0
  Stream_Write_UINT32(s, (UINT32)pduLength); /* pduLength (4 bytes) */
239
0
  Stream_SetPosition(s, Stream_Length(s));
240
0
  status = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
241
0
                                    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
0
  return status;
248
0
}
249
250
static UINT rdpei_write_pen_frame(wStream* s, const RDPINPUT_PEN_FRAME* frame)
251
0
{
252
0
  if (!s || !frame)
253
0
    return ERROR_INTERNAL_ERROR;
254
255
0
  if (!rdpei_write_2byte_unsigned(s, frame->contactCount))
256
0
    return ERROR_OUTOFMEMORY;
257
0
  if (!rdpei_write_8byte_unsigned(s, frame->frameOffset))
258
0
    return ERROR_OUTOFMEMORY;
259
0
  for (UINT16 x = 0; x < frame->contactCount; x++)
260
0
  {
261
0
    const RDPINPUT_PEN_CONTACT* contact = &frame->contacts[x];
262
263
0
    if (!Stream_EnsureRemainingCapacity(s, 1))
264
0
      return ERROR_OUTOFMEMORY;
265
0
    Stream_Write_UINT8(s, contact->deviceId);
266
0
    if (!rdpei_write_2byte_unsigned(s, contact->fieldsPresent))
267
0
      return ERROR_OUTOFMEMORY;
268
0
    if (!rdpei_write_4byte_signed(s, contact->x))
269
0
      return ERROR_OUTOFMEMORY;
270
0
    if (!rdpei_write_4byte_signed(s, contact->y))
271
0
      return ERROR_OUTOFMEMORY;
272
0
    if (!rdpei_write_4byte_unsigned(s, contact->contactFlags))
273
0
      return ERROR_OUTOFMEMORY;
274
0
    if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
275
0
    {
276
0
      if (!rdpei_write_4byte_unsigned(s, contact->penFlags))
277
0
        return ERROR_OUTOFMEMORY;
278
0
    }
279
0
    if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
280
0
    {
281
0
      if (!rdpei_write_4byte_unsigned(s, contact->pressure))
282
0
        return ERROR_OUTOFMEMORY;
283
0
    }
284
0
    if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
285
0
    {
286
0
      if (!rdpei_write_2byte_unsigned(s, contact->rotation))
287
0
        return ERROR_OUTOFMEMORY;
288
0
    }
289
0
    if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
290
0
    {
291
0
      if (!rdpei_write_2byte_signed(s, contact->tiltX))
292
0
        return ERROR_OUTOFMEMORY;
293
0
    }
294
0
    if (contact->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
295
0
    {
296
0
      if (!rdpei_write_2byte_signed(s, contact->tiltY))
297
0
        return ERROR_OUTOFMEMORY;
298
0
    }
299
0
  }
300
0
  return CHANNEL_RC_OK;
301
0
}
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
0
{
306
0
  UINT status = 0;
307
0
  wStream* s = NULL;
308
309
0
  WINPR_ASSERT(callback);
310
311
0
  if (frameOffset > UINT32_MAX)
312
0
    return ERROR_INVALID_PARAMETER;
313
0
  if (count > UINT16_MAX)
314
0
    return ERROR_INVALID_PARAMETER;
315
316
0
  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
317
0
  if (!rdpei)
318
0
    return ERROR_INTERNAL_ERROR;
319
320
0
  if (!frames || (count == 0))
321
0
    return ERROR_INTERNAL_ERROR;
322
323
0
  s = Stream_New(NULL, 64);
324
325
0
  if (!s)
326
0
  {
327
0
    WLog_Print(rdpei->base.log, WLOG_ERROR, "Stream_New failed!");
328
0
    return CHANNEL_RC_NO_MEMORY;
329
0
  }
330
331
0
  Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
332
  /**
333
   * the time that has elapsed (in milliseconds) from when the oldest touch frame
334
   * was generated to when it was encoded for transmission by the client.
335
   */
336
0
  rdpei_write_4byte_unsigned(s,
337
0
                             (UINT32)frameOffset); /* encodeTime (FOUR_BYTE_UNSIGNED_INTEGER) */
338
0
  rdpei_write_2byte_unsigned(s, (UINT16)count);    /* (frameCount) TWO_BYTE_UNSIGNED_INTEGER */
339
340
0
  for (size_t x = 0; x < count; x++)
341
0
  {
342
0
    if ((status = rdpei_write_pen_frame(s, &frames[x])))
343
0
    {
344
0
      WLog_Print(rdpei->base.log, WLOG_ERROR,
345
0
                 "rdpei_write_pen_frame failed with error %" PRIu32 "!", status);
346
0
      Stream_Free(s, TRUE);
347
0
      return status;
348
0
    }
349
0
  }
350
0
  Stream_SealLength(s);
351
352
0
  status = rdpei_send_pdu(callback, s, EVENTID_PEN, Stream_Length(s));
353
0
  Stream_Free(s, TRUE);
354
0
  return status;
355
0
}
356
357
static UINT rdpei_send_pen_frame(RdpeiClientContext* context, RDPINPUT_PEN_FRAME* frame)
358
0
{
359
0
  const UINT64 currentTime = GetTickCount64();
360
0
  RDPEI_PLUGIN* rdpei = NULL;
361
0
  GENERIC_CHANNEL_CALLBACK* callback = NULL;
362
0
  UINT error = 0;
363
364
0
  if (!context)
365
0
    return ERROR_INTERNAL_ERROR;
366
0
  rdpei = (RDPEI_PLUGIN*)context->handle;
367
0
  if (!rdpei || !rdpei->base.listener_callback)
368
0
    return ERROR_INTERNAL_ERROR;
369
0
  if (!rdpei || !rdpei->rdpcontext)
370
0
    return ERROR_INTERNAL_ERROR;
371
0
  if (freerdp_settings_get_bool(rdpei->rdpcontext->settings, FreeRDP_SuspendInput))
372
0
    return CHANNEL_RC_OK;
373
374
0
  callback = rdpei->base.listener_callback->channel_callback;
375
  /* Just ignore the event if the channel is not connected */
376
0
  if (!callback)
377
0
    return CHANNEL_RC_OK;
378
379
0
  if (!rdpei->previousPenFrameTime && !rdpei->currentPenFrameTime)
380
0
  {
381
0
    rdpei->currentPenFrameTime = currentTime;
382
0
    frame->frameOffset = 0;
383
0
  }
384
0
  else
385
0
  {
386
0
    rdpei->currentPenFrameTime = currentTime;
387
0
    frame->frameOffset = rdpei->currentPenFrameTime - rdpei->previousPenFrameTime;
388
0
  }
389
390
0
  const size_t off = WINPR_ASSERTING_INT_CAST(size_t, frame->frameOffset);
391
0
  error = rdpei_send_pen_event_pdu(callback, off, frame, 1);
392
0
  if (error)
393
0
    return error;
394
395
0
  rdpei->previousPenFrameTime = rdpei->currentPenFrameTime;
396
0
  return error;
397
0
}
398
399
static UINT rdpei_add_pen_frame(RdpeiClientContext* context)
400
0
{
401
0
  RDPEI_PLUGIN* rdpei = NULL;
402
0
  RDPINPUT_PEN_FRAME penFrame = { 0 };
403
0
  RDPINPUT_PEN_CONTACT penContacts[MAX_PEN_CONTACTS] = { 0 };
404
405
0
  if (!context || !context->handle)
406
0
    return ERROR_INTERNAL_ERROR;
407
408
0
  rdpei = (RDPEI_PLUGIN*)context->handle;
409
410
0
  penFrame.contacts = penContacts;
411
412
0
  for (UINT16 i = 0; i < rdpei->maxPenContacts; i++)
413
0
  {
414
0
    RDPINPUT_PEN_CONTACT_POINT* contact = &(rdpei->penContactPoints[i]);
415
416
0
    if (contact->dirty)
417
0
    {
418
0
      penContacts[penFrame.contactCount++] = contact->data;
419
0
      contact->dirty = FALSE;
420
0
    }
421
0
    else if (contact->active)
422
0
    {
423
0
      if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
424
0
      {
425
0
        contact->data.contactFlags = RDPINPUT_CONTACT_FLAG_UPDATE;
426
0
        contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INRANGE;
427
0
        contact->data.contactFlags |= RDPINPUT_CONTACT_FLAG_INCONTACT;
428
0
      }
429
430
0
      penContacts[penFrame.contactCount++] = contact->data;
431
0
    }
432
0
    if (contact->data.contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
433
0
    {
434
0
      contact->externalId = 0;
435
0
      contact->active = FALSE;
436
0
    }
437
0
  }
438
439
0
  if (penFrame.contactCount > 0)
440
0
    return rdpei_send_pen_frame(context, &penFrame);
441
0
  return CHANNEL_RC_OK;
442
0
}
443
444
static UINT rdpei_update(wLog* log, RdpeiClientContext* context)
445
0
{
446
0
  UINT error = rdpei_add_frame(context);
447
0
  if (error != CHANNEL_RC_OK)
448
0
  {
449
0
    WLog_Print(log, WLOG_ERROR, "rdpei_add_frame failed with error %" PRIu32 "!", error);
450
0
    return error;
451
0
  }
452
453
0
  return rdpei_add_pen_frame(context);
454
0
}
455
456
static BOOL rdpei_poll_run_unlocked(rdpContext* context, void* userdata)
457
0
{
458
0
  RDPEI_PLUGIN* rdpei = userdata;
459
0
  WINPR_ASSERT(rdpei);
460
0
  WINPR_ASSERT(context);
461
462
0
  const UINT64 now = GetTickCount64();
463
464
  /* Send an event every ~20ms */
465
0
  if ((now < rdpei->lastPollEventTime) || (now - rdpei->lastPollEventTime < 20ULL))
466
0
    return TRUE;
467
468
0
  rdpei->lastPollEventTime = now;
469
470
0
  const UINT error = rdpei_update(rdpei->base.log, rdpei->context);
471
472
0
  (void)ResetEvent(rdpei->event);
473
474
0
  if (error != CHANNEL_RC_OK)
475
0
  {
476
0
    WLog_Print(rdpei->base.log, WLOG_ERROR, "rdpei_add_frame failed with error %" PRIu32 "!",
477
0
               error);
478
0
    setChannelError(context, error, "rdpei_add_frame reported an error");
479
0
    return FALSE;
480
0
  }
481
482
0
  return TRUE;
483
0
}
484
485
static BOOL rdpei_poll_run(rdpContext* context, void* userdata)
486
0
{
487
0
  RDPEI_PLUGIN* rdpei = userdata;
488
0
  WINPR_ASSERT(rdpei);
489
490
0
  EnterCriticalSection(&rdpei->lock);
491
0
  BOOL rc = rdpei_poll_run_unlocked(context, userdata);
492
0
  LeaveCriticalSection(&rdpei->lock);
493
0
  return rc;
494
0
}
495
496
static DWORD WINAPI rdpei_periodic_update(LPVOID arg)
497
0
{
498
0
  DWORD status = 0;
499
0
  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)arg;
500
0
  UINT error = CHANNEL_RC_OK;
501
0
  RdpeiClientContext* context = NULL;
502
503
0
  if (!rdpei)
504
0
  {
505
0
    error = ERROR_INVALID_PARAMETER;
506
0
    goto out;
507
0
  }
508
509
0
  context = rdpei->context;
510
511
0
  if (!context)
512
0
  {
513
0
    error = ERROR_INVALID_PARAMETER;
514
0
    goto out;
515
0
  }
516
517
0
  while (rdpei->running)
518
0
  {
519
0
    status = WaitForSingleObject(rdpei->event, 20);
520
521
0
    if (status == WAIT_FAILED)
522
0
    {
523
0
      error = GetLastError();
524
0
      WLog_Print(rdpei->base.log, WLOG_ERROR,
525
0
                 "WaitForMultipleObjects failed with error %" PRIu32 "!", error);
526
0
      break;
527
0
    }
528
529
0
    if (!rdpei_poll_run(rdpei->rdpcontext, rdpei))
530
0
      error = ERROR_INTERNAL_ERROR;
531
0
  }
532
533
0
out:
534
535
0
  if (error && rdpei && rdpei->rdpcontext)
536
0
    setChannelError(rdpei->rdpcontext, error, "rdpei_schedule_thread reported an error");
537
538
0
  if (rdpei)
539
0
    rdpei->running = FALSE;
540
541
0
  ExitThread(error);
542
0
  return error;
543
0
}
544
545
/**
546
 * Function description
547
 *
548
 * @return 0 on success, otherwise a Win32 error code
549
 */
550
static UINT rdpei_send_cs_ready_pdu(GENERIC_CHANNEL_CALLBACK* callback)
551
0
{
552
0
  UINT status = 0;
553
0
  wStream* s = NULL;
554
0
  UINT32 flags = 0;
555
0
  UINT32 pduLength = 0;
556
0
  RDPEI_PLUGIN* rdpei = NULL;
557
558
0
  if (!callback || !callback->plugin)
559
0
    return ERROR_INTERNAL_ERROR;
560
0
  rdpei = (RDPEI_PLUGIN*)callback->plugin;
561
562
0
  flags |= CS_READY_FLAGS_SHOW_TOUCH_VISUALS & rdpei->context->clientFeaturesMask;
563
0
  if (rdpei->version > RDPINPUT_PROTOCOL_V10)
564
0
    flags |= CS_READY_FLAGS_DISABLE_TIMESTAMP_INJECTION & rdpei->context->clientFeaturesMask;
565
0
  if (rdpei->features & SC_READY_MULTIPEN_INJECTION_SUPPORTED)
566
0
    flags |= CS_READY_FLAGS_ENABLE_MULTIPEN_INJECTION & rdpei->context->clientFeaturesMask;
567
568
0
  pduLength = RDPINPUT_HEADER_LENGTH + 10;
569
0
  s = Stream_New(NULL, pduLength);
570
571
0
  if (!s)
572
0
  {
573
0
    WLog_Print(rdpei->base.log, WLOG_ERROR, "Stream_New failed!");
574
0
    return CHANNEL_RC_NO_MEMORY;
575
0
  }
576
577
0
  Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
578
0
  Stream_Write_UINT32(s, flags);                   /* flags (4 bytes) */
579
0
  Stream_Write_UINT32(s, rdpei->version);          /* protocolVersion (4 bytes) */
580
0
  Stream_Write_UINT16(s, rdpei->maxTouchContacts); /* maxTouchContacts (2 bytes) */
581
0
  Stream_SealLength(s);
582
0
  status = rdpei_send_pdu(callback, s, EVENTID_CS_READY, pduLength);
583
0
  Stream_Free(s, TRUE);
584
0
  return status;
585
0
}
586
587
#if defined(WITH_DEBUG_RDPEI)
588
static void rdpei_print_contact_flags(wLog* log, UINT32 contactFlags)
589
{
590
  if (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN)
591
    WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_DOWN");
592
593
  if (contactFlags & RDPINPUT_CONTACT_FLAG_UPDATE)
594
    WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_UPDATE");
595
596
  if (contactFlags & RDPINPUT_CONTACT_FLAG_UP)
597
    WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_UP");
598
599
  if (contactFlags & RDPINPUT_CONTACT_FLAG_INRANGE)
600
    WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_INRANGE");
601
602
  if (contactFlags & RDPINPUT_CONTACT_FLAG_INCONTACT)
603
    WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_INCONTACT");
604
605
  if (contactFlags & RDPINPUT_CONTACT_FLAG_CANCELED)
606
    WLog_Print(log, WLOG_DEBUG, " RDPINPUT_CONTACT_FLAG_CANCELED");
607
}
608
#endif
609
610
static INT16 bounded(INT32 val)
611
0
{
612
0
  if (val < INT16_MIN)
613
0
    return INT16_MIN;
614
0
  if (val > INT16_MAX)
615
0
    return INT16_MAX;
616
0
  return (INT16)val;
617
0
}
618
619
/**
620
 * Function description
621
 *
622
 * @return 0 on success, otherwise a Win32 error code
623
 */
624
static UINT rdpei_write_touch_frame(wLog* log, wStream* s, RDPINPUT_TOUCH_FRAME* frame)
625
0
{
626
0
  int rectSize = 2;
627
0
  RDPINPUT_CONTACT_DATA* contact = NULL;
628
0
  if (!s || !frame)
629
0
    return ERROR_INTERNAL_ERROR;
630
#ifdef WITH_DEBUG_RDPEI
631
  WLog_Print(log, WLOG_DEBUG, "contactCount: %" PRIu32 "", frame->contactCount);
632
  WLog_Print(log, WLOG_DEBUG, "frameOffset: 0x%016" PRIX64 "", frame->frameOffset);
633
#endif
634
0
  rdpei_write_2byte_unsigned(s,
635
0
                             frame->contactCount); /* contactCount (TWO_BYTE_UNSIGNED_INTEGER) */
636
  /**
637
   * the time offset from the previous frame (in microseconds).
638
   * If this is the first frame being transmitted then this field MUST be set to zero.
639
   */
640
0
  rdpei_write_8byte_unsigned(s, frame->frameOffset *
641
0
                                    1000); /* frameOffset (EIGHT_BYTE_UNSIGNED_INTEGER) */
642
643
0
  if (!Stream_EnsureRemainingCapacity(s, (size_t)frame->contactCount * 64))
644
0
  {
645
0
    WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
646
0
    return CHANNEL_RC_NO_MEMORY;
647
0
  }
648
649
0
  for (UINT32 index = 0; index < frame->contactCount; index++)
650
0
  {
651
0
    contact = &frame->contacts[index];
652
0
    contact->fieldsPresent |= CONTACT_DATA_CONTACTRECT_PRESENT;
653
0
    contact->contactRectLeft = bounded(contact->x - rectSize);
654
0
    contact->contactRectTop = bounded(contact->y - rectSize);
655
0
    contact->contactRectRight = bounded(contact->x + rectSize);
656
0
    contact->contactRectBottom = bounded(contact->y + rectSize);
657
#ifdef WITH_DEBUG_RDPEI
658
    WLog_Print(log, WLOG_DEBUG, "contact[%" PRIu32 "].contactId: %" PRIu32 "", index,
659
               contact->contactId);
660
    WLog_Print(log, WLOG_DEBUG, "contact[%" PRIu32 "].fieldsPresent: %" PRIu32 "", index,
661
               contact->fieldsPresent);
662
    WLog_Print(log, WLOG_DEBUG, "contact[%" PRIu32 "].x: %" PRId32 "", index, contact->x);
663
    WLog_Print(log, WLOG_DEBUG, "contact[%" PRIu32 "].y: %" PRId32 "", index, contact->y);
664
    WLog_Print(log, WLOG_DEBUG, "contact[%" PRIu32 "].contactFlags: 0x%08" PRIX32 "", index,
665
               contact->contactFlags);
666
    rdpei_print_contact_flags(log, contact->contactFlags);
667
#endif
668
0
    Stream_Write_UINT8(
669
0
        s, WINPR_ASSERTING_INT_CAST(uint8_t, contact->contactId)); /* contactId (1 byte) */
670
    /* fieldsPresent (TWO_BYTE_UNSIGNED_INTEGER) */
671
0
    rdpei_write_2byte_unsigned(s, contact->fieldsPresent);
672
0
    rdpei_write_4byte_signed(s, contact->x); /* x (FOUR_BYTE_SIGNED_INTEGER) */
673
0
    rdpei_write_4byte_signed(s, contact->y); /* y (FOUR_BYTE_SIGNED_INTEGER) */
674
    /* contactFlags (FOUR_BYTE_UNSIGNED_INTEGER) */
675
0
    rdpei_write_4byte_unsigned(s, contact->contactFlags);
676
677
0
    if (contact->fieldsPresent & CONTACT_DATA_CONTACTRECT_PRESENT)
678
0
    {
679
      /* contactRectLeft (TWO_BYTE_SIGNED_INTEGER) */
680
0
      rdpei_write_2byte_signed(s, contact->contactRectLeft);
681
      /* contactRectTop (TWO_BYTE_SIGNED_INTEGER) */
682
0
      rdpei_write_2byte_signed(s, contact->contactRectTop);
683
      /* contactRectRight (TWO_BYTE_SIGNED_INTEGER) */
684
0
      rdpei_write_2byte_signed(s, contact->contactRectRight);
685
      /* contactRectBottom (TWO_BYTE_SIGNED_INTEGER) */
686
0
      rdpei_write_2byte_signed(s, contact->contactRectBottom);
687
0
    }
688
689
0
    if (contact->fieldsPresent & CONTACT_DATA_ORIENTATION_PRESENT)
690
0
    {
691
      /* orientation (FOUR_BYTE_UNSIGNED_INTEGER) */
692
0
      rdpei_write_4byte_unsigned(s, contact->orientation);
693
0
    }
694
695
0
    if (contact->fieldsPresent & CONTACT_DATA_PRESSURE_PRESENT)
696
0
    {
697
      /* pressure (FOUR_BYTE_UNSIGNED_INTEGER) */
698
0
      rdpei_write_4byte_unsigned(s, contact->pressure);
699
0
    }
700
0
  }
701
702
0
  return CHANNEL_RC_OK;
703
0
}
704
705
/**
706
 * Function description
707
 *
708
 * @return 0 on success, otherwise a Win32 error code
709
 */
710
static UINT rdpei_send_touch_event_pdu(GENERIC_CHANNEL_CALLBACK* callback,
711
                                       RDPINPUT_TOUCH_FRAME* frame)
712
0
{
713
0
  UINT status = 0;
714
715
0
  WINPR_ASSERT(callback);
716
717
0
  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
718
0
  if (!rdpei || !rdpei->rdpcontext)
719
0
    return ERROR_INTERNAL_ERROR;
720
0
  if (freerdp_settings_get_bool(rdpei->rdpcontext->settings, FreeRDP_SuspendInput))
721
0
    return CHANNEL_RC_OK;
722
723
0
  if (!frame)
724
0
    return ERROR_INTERNAL_ERROR;
725
726
0
  size_t pduLength = 64ULL + (64ULL * frame->contactCount);
727
0
  wStream* s = Stream_New(NULL, pduLength);
728
729
0
  if (!s)
730
0
  {
731
0
    WLog_Print(rdpei->base.log, WLOG_ERROR, "Stream_New failed!");
732
0
    return CHANNEL_RC_NO_MEMORY;
733
0
  }
734
735
0
  Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
736
  /**
737
   * the time that has elapsed (in milliseconds) from when the oldest touch frame
738
   * was generated to when it was encoded for transmission by the client.
739
   */
740
0
  rdpei_write_4byte_unsigned(
741
0
      s, (UINT32)frame->frameOffset); /* encodeTime (FOUR_BYTE_UNSIGNED_INTEGER) */
742
0
  rdpei_write_2byte_unsigned(s, 1);   /* (frameCount) TWO_BYTE_UNSIGNED_INTEGER */
743
744
0
  status = rdpei_write_touch_frame(rdpei->base.log, s, frame);
745
0
  if (status)
746
0
  {
747
0
    WLog_Print(rdpei->base.log, WLOG_ERROR,
748
0
               "rdpei_write_touch_frame failed with error %" PRIu32 "!", status);
749
0
    Stream_Free(s, TRUE);
750
0
    return status;
751
0
  }
752
753
0
  Stream_SealLength(s);
754
0
  status = rdpei_send_pdu(callback, s, EVENTID_TOUCH, Stream_Length(s));
755
0
  Stream_Free(s, TRUE);
756
0
  return status;
757
0
}
758
759
/**
760
 * Function description
761
 *
762
 * @return 0 on success, otherwise a Win32 error code
763
 */
764
static UINT rdpei_recv_sc_ready_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
765
0
{
766
0
  UINT32 features = 0;
767
0
  UINT32 protocolVersion = 0;
768
769
0
  if (!callback || !callback->plugin)
770
0
    return ERROR_INTERNAL_ERROR;
771
772
0
  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
773
774
0
  if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
775
0
    return ERROR_INVALID_DATA;
776
0
  Stream_Read_UINT32(s, protocolVersion); /* protocolVersion (4 bytes) */
777
778
0
  if (protocolVersion >= RDPINPUT_PROTOCOL_V300)
779
0
  {
780
0
    if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 4))
781
0
      return ERROR_INVALID_DATA;
782
0
  }
783
784
0
  if (Stream_GetRemainingLength(s) >= 4)
785
0
    Stream_Read_UINT32(s, features);
786
787
0
  if (rdpei->version > protocolVersion)
788
0
    rdpei->version = protocolVersion;
789
0
  rdpei->features = features;
790
791
0
  if (protocolVersion > RDPINPUT_PROTOCOL_V300)
792
0
  {
793
0
    WLog_Print(rdpei->base.log, WLOG_WARN,
794
0
               "Unknown [MS-RDPEI] protocolVersion: 0x%08" PRIX32 "", protocolVersion);
795
0
  }
796
797
0
  return CHANNEL_RC_OK;
798
0
}
799
800
/**
801
 * Function description
802
 *
803
 * @return 0 on success, otherwise a Win32 error code
804
 */
805
static UINT rdpei_recv_suspend_touch_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
806
0
{
807
0
  UINT error = CHANNEL_RC_OK;
808
809
0
  WINPR_UNUSED(s);
810
811
0
  if (!callback || !callback->plugin)
812
0
    return ERROR_INTERNAL_ERROR;
813
814
0
  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
815
0
  RdpeiClientContext* context = rdpei->context;
816
0
  if (!rdpei)
817
0
    return ERROR_INTERNAL_ERROR;
818
819
0
  IFCALLRET(context->SuspendTouch, error, context);
820
821
0
  if (error)
822
0
    WLog_Print(rdpei->base.log, WLOG_ERROR,
823
0
               "rdpei->SuspendTouch failed with error %" PRIu32 "!", error);
824
825
0
  return error;
826
0
}
827
828
/**
829
 * Function description
830
 *
831
 * @return 0 on success, otherwise a Win32 error code
832
 */
833
static UINT rdpei_recv_resume_touch_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
834
0
{
835
0
  UINT error = CHANNEL_RC_OK;
836
0
  if (!s || !callback)
837
0
    return ERROR_INTERNAL_ERROR;
838
839
0
  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
840
0
  if (!rdpei)
841
0
    return ERROR_INTERNAL_ERROR;
842
843
0
  RdpeiClientContext* context = (RdpeiClientContext*)callback->plugin->pInterface;
844
0
  if (!context)
845
0
    return ERROR_INTERNAL_ERROR;
846
847
0
  IFCALLRET(context->ResumeTouch, error, context);
848
849
0
  if (error)
850
0
    WLog_Print(rdpei->base.log, WLOG_ERROR, "rdpei->ResumeTouch failed with error %" PRIu32 "!",
851
0
               error);
852
853
0
  return error;
854
0
}
855
856
/**
857
 * Function description
858
 *
859
 * @return 0 on success, otherwise a Win32 error code
860
 */
861
static UINT rdpei_recv_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
862
0
{
863
0
  UINT16 eventId = 0;
864
0
  UINT32 pduLength = 0;
865
0
  UINT error = 0;
866
867
0
  if (!callback || !s)
868
0
    return ERROR_INTERNAL_ERROR;
869
870
0
  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
871
0
  if (!rdpei)
872
0
    return ERROR_INTERNAL_ERROR;
873
874
0
  if (!Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, 6))
875
0
    return ERROR_INVALID_DATA;
876
877
0
  Stream_Read_UINT16(s, eventId);   /* eventId (2 bytes) */
878
0
  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
0
  if ((pduLength < 6) || !Stream_CheckAndLogRequiredLengthWLog(rdpei->base.log, s, pduLength - 6))
886
0
    return ERROR_INVALID_DATA;
887
888
0
  switch (eventId)
889
0
  {
890
0
    case EVENTID_SC_READY:
891
0
      if ((error = rdpei_recv_sc_ready_pdu(callback, s)))
892
0
      {
893
0
        WLog_Print(rdpei->base.log, WLOG_ERROR,
894
0
                   "rdpei_recv_sc_ready_pdu failed with error %" PRIu32 "!", error);
895
0
        return error;
896
0
      }
897
898
0
      if ((error = rdpei_send_cs_ready_pdu(callback)))
899
0
      {
900
0
        WLog_Print(rdpei->base.log, WLOG_ERROR,
901
0
                   "rdpei_send_cs_ready_pdu failed with error %" PRIu32 "!", error);
902
0
        return error;
903
0
      }
904
905
0
      break;
906
907
0
    case EVENTID_SUSPEND_TOUCH:
908
0
      if ((error = rdpei_recv_suspend_touch_pdu(callback, s)))
909
0
      {
910
0
        WLog_Print(rdpei->base.log, WLOG_ERROR,
911
0
                   "rdpei_recv_suspend_touch_pdu failed with error %" PRIu32 "!", error);
912
0
        return error;
913
0
      }
914
915
0
      break;
916
917
0
    case EVENTID_RESUME_TOUCH:
918
0
      if ((error = rdpei_recv_resume_touch_pdu(callback, s)))
919
0
      {
920
0
        WLog_Print(rdpei->base.log, WLOG_ERROR,
921
0
                   "rdpei_recv_resume_touch_pdu failed with error %" PRIu32 "!", error);
922
0
        return error;
923
0
      }
924
925
0
      break;
926
927
0
    default:
928
0
      break;
929
0
  }
930
931
0
  return CHANNEL_RC_OK;
932
0
}
933
934
/**
935
 * Function description
936
 *
937
 * @return 0 on success, otherwise a Win32 error code
938
 */
939
static UINT rdpei_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
940
0
{
941
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
942
0
  return rdpei_recv_pdu(callback, data);
943
0
}
944
945
/**
946
 * Function description
947
 *
948
 * @return 0 on success, otherwise a Win32 error code
949
 */
950
static UINT rdpei_on_close(IWTSVirtualChannelCallback* pChannelCallback)
951
0
{
952
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
953
0
  if (callback)
954
0
  {
955
0
    RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
956
0
    if (rdpei && rdpei->base.listener_callback)
957
0
    {
958
0
      if (rdpei->base.listener_callback->channel_callback == callback)
959
0
        rdpei->base.listener_callback->channel_callback = NULL;
960
0
    }
961
0
  }
962
0
  free(callback);
963
0
  return CHANNEL_RC_OK;
964
0
}
965
966
/**
967
 * Channel Client Interface
968
 */
969
970
static UINT32 rdpei_get_version(RdpeiClientContext* context)
971
0
{
972
0
  RDPEI_PLUGIN* rdpei = NULL;
973
0
  if (!context || !context->handle)
974
0
    return 0;
975
0
  rdpei = (RDPEI_PLUGIN*)context->handle;
976
0
  return rdpei->version;
977
0
}
978
979
static UINT32 rdpei_get_features(RdpeiClientContext* context)
980
0
{
981
0
  RDPEI_PLUGIN* rdpei = NULL;
982
0
  if (!context || !context->handle)
983
0
    return 0;
984
0
  rdpei = (RDPEI_PLUGIN*)context->handle;
985
0
  return rdpei->features;
986
0
}
987
988
/**
989
 * Function description
990
 *
991
 * @return 0 on success, otherwise a Win32 error code
992
 */
993
UINT rdpei_send_frame(RdpeiClientContext* context, RDPINPUT_TOUCH_FRAME* frame)
994
0
{
995
0
  UINT64 currentTime = GetTickCount64();
996
0
  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
997
0
  GENERIC_CHANNEL_CALLBACK* callback = NULL;
998
0
  UINT error = 0;
999
1000
0
  callback = rdpei->base.listener_callback->channel_callback;
1001
1002
  /* Just ignore the event if the channel is not connected */
1003
0
  if (!callback)
1004
0
    return CHANNEL_RC_OK;
1005
1006
0
  if (!rdpei->previousFrameTime && !rdpei->currentFrameTime)
1007
0
  {
1008
0
    rdpei->currentFrameTime = currentTime;
1009
0
    frame->frameOffset = 0;
1010
0
  }
1011
0
  else
1012
0
  {
1013
0
    rdpei->currentFrameTime = currentTime;
1014
0
    frame->frameOffset = rdpei->currentFrameTime - rdpei->previousFrameTime;
1015
0
  }
1016
1017
0
  if ((error = rdpei_send_touch_event_pdu(callback, frame)))
1018
0
  {
1019
0
    WLog_Print(rdpei->base.log, WLOG_ERROR,
1020
0
               "rdpei_send_touch_event_pdu failed with error %" PRIu32 "!", error);
1021
0
    return error;
1022
0
  }
1023
1024
0
  rdpei->previousFrameTime = rdpei->currentFrameTime;
1025
0
  return error;
1026
0
}
1027
1028
/**
1029
 * Function description
1030
 *
1031
 * @return 0 on success, otherwise a Win32 error code
1032
 */
1033
static UINT rdpei_add_contact(RdpeiClientContext* context, const RDPINPUT_CONTACT_DATA* contact)
1034
0
{
1035
0
  RDPINPUT_CONTACT_POINT* contactPoint = NULL;
1036
0
  RDPEI_PLUGIN* rdpei = NULL;
1037
0
  if (!context || !contact || !context->handle)
1038
0
    return ERROR_INTERNAL_ERROR;
1039
1040
0
  rdpei = (RDPEI_PLUGIN*)context->handle;
1041
1042
0
  EnterCriticalSection(&rdpei->lock);
1043
0
  contactPoint = &rdpei->contactPoints[contact->contactId];
1044
0
  contactPoint->data = *contact;
1045
0
  contactPoint->dirty = TRUE;
1046
0
  (void)SetEvent(rdpei->event);
1047
0
  LeaveCriticalSection(&rdpei->lock);
1048
1049
0
  return CHANNEL_RC_OK;
1050
0
}
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
0
{
1055
0
  INT64 contactIdlocal = -1;
1056
0
  RDPINPUT_CONTACT_POINT* contactPoint = NULL;
1057
0
  UINT error = CHANNEL_RC_OK;
1058
1059
0
  if (!context || !contactId || !context->handle)
1060
0
    return ERROR_INTERNAL_ERROR;
1061
1062
0
  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
1063
  /* Create a new contact point in an empty slot */
1064
0
  EnterCriticalSection(&rdpei->lock);
1065
0
  const BOOL begin = (contactFlags & RDPINPUT_CONTACT_FLAG_DOWN) != 0;
1066
0
  contactPoint = rdpei_contact(rdpei, externalId, !begin);
1067
0
  if (contactPoint)
1068
0
    contactIdlocal = contactPoint->contactId;
1069
0
  LeaveCriticalSection(&rdpei->lock);
1070
1071
0
  if (contactIdlocal > UINT32_MAX)
1072
0
    return ERROR_INVALID_PARAMETER;
1073
1074
0
  if (contactIdlocal >= 0)
1075
0
  {
1076
0
    RDPINPUT_CONTACT_DATA contact = { 0 };
1077
0
    contact.x = x;
1078
0
    contact.y = y;
1079
0
    contact.contactId = (UINT32)contactIdlocal;
1080
0
    contact.contactFlags = contactFlags;
1081
0
    contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1082
1083
0
    if (fieldFlags & CONTACT_DATA_CONTACTRECT_PRESENT)
1084
0
    {
1085
0
      INT32 val = va_arg(ap, INT32);
1086
0
      contact.contactRectLeft = WINPR_ASSERTING_INT_CAST(INT16, val);
1087
1088
0
      val = va_arg(ap, INT32);
1089
0
      contact.contactRectTop = WINPR_ASSERTING_INT_CAST(INT16, val);
1090
1091
0
      val = va_arg(ap, INT32);
1092
0
      contact.contactRectRight = WINPR_ASSERTING_INT_CAST(INT16, val);
1093
1094
0
      val = va_arg(ap, INT32);
1095
0
      contact.contactRectBottom = WINPR_ASSERTING_INT_CAST(INT16, val);
1096
0
    }
1097
0
    if (fieldFlags & CONTACT_DATA_ORIENTATION_PRESENT)
1098
0
    {
1099
0
      UINT32 p = va_arg(ap, UINT32);
1100
0
      if (p >= 360)
1101
0
      {
1102
0
        WLog_Print(rdpei->base.log, WLOG_WARN,
1103
0
                   "TouchContact %" PRId64 ": Invalid orientation value %" PRIu32
1104
0
                   "degree, clamping to 359 degree",
1105
0
                   contactIdlocal, p);
1106
0
        p = 359;
1107
0
      }
1108
0
      contact.orientation = p;
1109
0
    }
1110
0
    if (fieldFlags & CONTACT_DATA_PRESSURE_PRESENT)
1111
0
    {
1112
0
      UINT32 p = va_arg(ap, UINT32);
1113
0
      if (p > 1024)
1114
0
      {
1115
0
        WLog_Print(rdpei->base.log, WLOG_WARN,
1116
0
                   "TouchContact %" PRId64 ": Invalid pressure value %" PRIu32
1117
0
                   ", clamping to 1024",
1118
0
                   contactIdlocal, p);
1119
0
        p = 1024;
1120
0
      }
1121
0
      contact.pressure = p;
1122
0
    }
1123
1124
0
    error = context->AddContact(context, &contact);
1125
0
  }
1126
1127
0
  if (contactId)
1128
0
    *contactId = (INT32)contactIdlocal;
1129
0
  return error;
1130
0
}
1131
1132
/**
1133
 * Function description
1134
 *
1135
 * @return 0 on success, otherwise a Win32 error code
1136
 */
1137
static UINT rdpei_touch_begin(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1138
                              INT32* contactId)
1139
0
{
1140
0
  UINT rc = 0;
1141
0
  va_list ap = { 0 };
1142
0
  rc = rdpei_touch_process(context, externalId,
1143
0
                           RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1144
0
                               RDPINPUT_CONTACT_FLAG_INCONTACT,
1145
0
                           x, y, contactId, 0, ap);
1146
0
  return rc;
1147
0
}
1148
1149
/**
1150
 * Function description
1151
 *
1152
 * @return 0 on success, otherwise a Win32 error code
1153
 */
1154
static UINT rdpei_touch_update(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1155
                               INT32* contactId)
1156
0
{
1157
0
  UINT rc = 0;
1158
0
  va_list ap = { 0 };
1159
0
  rc = rdpei_touch_process(context, externalId,
1160
0
                           RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1161
0
                               RDPINPUT_CONTACT_FLAG_INCONTACT,
1162
0
                           x, y, contactId, 0, ap);
1163
0
  return rc;
1164
0
}
1165
1166
/**
1167
 * Function description
1168
 *
1169
 * @return 0 on success, otherwise a Win32 error code
1170
 */
1171
static UINT rdpei_touch_end(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1172
                            INT32* contactId)
1173
0
{
1174
0
  UINT error = 0;
1175
0
  va_list ap = { 0 };
1176
0
  error = rdpei_touch_process(context, externalId,
1177
0
                              RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1178
0
                                  RDPINPUT_CONTACT_FLAG_INCONTACT,
1179
0
                              x, y, contactId, 0, ap);
1180
0
  if (error != CHANNEL_RC_OK)
1181
0
    return error;
1182
0
  error =
1183
0
      rdpei_touch_process(context, externalId, RDPINPUT_CONTACT_FLAG_UP, x, y, contactId, 0, ap);
1184
0
  return error;
1185
0
}
1186
1187
/**
1188
 * Function description
1189
 *
1190
 * @return 0 on success, otherwise a Win32 error code
1191
 */
1192
static UINT rdpei_touch_cancel(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1193
                               INT32* contactId)
1194
0
{
1195
0
  UINT rc = 0;
1196
0
  va_list ap = { 0 };
1197
0
  rc = rdpei_touch_process(context, externalId,
1198
0
                           RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_CANCELED, x, y,
1199
0
                           contactId, 0, ap);
1200
0
  return rc;
1201
0
}
1202
1203
static UINT rdpei_touch_raw_event(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1204
                                  INT32* contactId, UINT32 flags, UINT32 fieldFlags, ...)
1205
0
{
1206
0
  UINT rc = 0;
1207
0
  va_list ap;
1208
0
  va_start(ap, fieldFlags);
1209
0
  rc = rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, ap);
1210
0
  va_end(ap);
1211
0
  return rc;
1212
0
}
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
0
{
1218
0
  return rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, args);
1219
0
}
1220
1221
static RDPINPUT_PEN_CONTACT_POINT* rdpei_pen_contact(RDPEI_PLUGIN* rdpei, INT32 externalId,
1222
                                                     BOOL active)
1223
0
{
1224
0
  if (!rdpei)
1225
0
    return NULL;
1226
1227
0
  for (UINT32 x = 0; x < rdpei->maxPenContacts; x++)
1228
0
  {
1229
0
    RDPINPUT_PEN_CONTACT_POINT* contact = &rdpei->penContactPoints[x];
1230
0
    if (active)
1231
0
    {
1232
0
      if (contact->active)
1233
0
      {
1234
0
        if (contact->externalId == externalId)
1235
0
          return contact;
1236
0
      }
1237
0
    }
1238
0
    else
1239
0
    {
1240
0
      if (!contact->active)
1241
0
      {
1242
0
        contact->externalId = externalId;
1243
0
        contact->active = TRUE;
1244
0
        return contact;
1245
0
      }
1246
0
    }
1247
0
  }
1248
0
  return NULL;
1249
0
}
1250
1251
static UINT rdpei_add_pen(RdpeiClientContext* context, INT32 externalId,
1252
                          const RDPINPUT_PEN_CONTACT* contact)
1253
0
{
1254
0
  RDPEI_PLUGIN* rdpei = NULL;
1255
0
  RDPINPUT_PEN_CONTACT_POINT* contactPoint = NULL;
1256
1257
0
  if (!context || !contact || !context->handle)
1258
0
    return ERROR_INTERNAL_ERROR;
1259
1260
0
  rdpei = (RDPEI_PLUGIN*)context->handle;
1261
1262
0
  EnterCriticalSection(&rdpei->lock);
1263
0
  contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1264
0
  if (contactPoint)
1265
0
  {
1266
0
    contactPoint->data = *contact;
1267
0
    contactPoint->dirty = TRUE;
1268
0
    (void)SetEvent(rdpei->event);
1269
0
  }
1270
0
  LeaveCriticalSection(&rdpei->lock);
1271
1272
0
  return CHANNEL_RC_OK;
1273
0
}
1274
1275
static UINT rdpei_pen_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1276
                              UINT32 fieldFlags, INT32 x, INT32 y, va_list ap)
1277
0
{
1278
0
  RDPINPUT_PEN_CONTACT_POINT* contactPoint = NULL;
1279
0
  RDPEI_PLUGIN* rdpei = NULL;
1280
0
  UINT error = CHANNEL_RC_OK;
1281
1282
0
  if (!context || !context->handle)
1283
0
    return ERROR_INTERNAL_ERROR;
1284
1285
0
  rdpei = (RDPEI_PLUGIN*)context->handle;
1286
1287
0
  EnterCriticalSection(&rdpei->lock);
1288
  // Start a new contact only when it is not active.
1289
0
  contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1290
0
  if (!contactPoint)
1291
0
  {
1292
0
    const UINT32 mask = RDPINPUT_CONTACT_FLAG_INRANGE;
1293
0
    if ((contactFlags & mask) == mask)
1294
0
    {
1295
0
      contactPoint = rdpei_pen_contact(rdpei, externalId, FALSE);
1296
0
    }
1297
0
  }
1298
0
  LeaveCriticalSection(&rdpei->lock);
1299
0
  if (contactPoint != NULL)
1300
0
  {
1301
0
    RDPINPUT_PEN_CONTACT contact = { 0 };
1302
1303
0
    contact.x = x;
1304
0
    contact.y = y;
1305
0
    contact.fieldsPresent = WINPR_ASSERTING_INT_CAST(UINT16, fieldFlags);
1306
1307
0
    contact.contactFlags = contactFlags;
1308
0
    if (fieldFlags & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
1309
0
    {
1310
0
      const UINT32 val = va_arg(ap, UINT32);
1311
0
      contact.penFlags = WINPR_ASSERTING_INT_CAST(UINT16, val);
1312
0
    }
1313
0
    if (fieldFlags & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
1314
0
    {
1315
0
      const UINT32 val = va_arg(ap, UINT32);
1316
0
      contact.pressure = WINPR_ASSERTING_INT_CAST(UINT16, val);
1317
0
    }
1318
0
    if (fieldFlags & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
1319
0
    {
1320
0
      const UINT32 val = va_arg(ap, UINT32);
1321
0
      contact.rotation = WINPR_ASSERTING_INT_CAST(UINT16, val);
1322
0
    }
1323
0
    if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
1324
0
    {
1325
0
      const INT32 val = va_arg(ap, INT32);
1326
0
      contact.tiltX = WINPR_ASSERTING_INT_CAST(INT16, val);
1327
0
    }
1328
0
    if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
1329
0
    {
1330
0
      const INT32 val = va_arg(ap, INT32);
1331
0
      WINPR_ASSERT((val >= INT16_MIN) && (val <= INT16_MAX));
1332
0
      contact.tiltY = WINPR_ASSERTING_INT_CAST(INT16, val);
1333
0
    }
1334
1335
0
    error = context->AddPen(context, externalId, &contact);
1336
0
  }
1337
1338
0
  return error;
1339
0
}
1340
1341
/**
1342
 * Function description
1343
 *
1344
 * @return 0 on success, otherwise a Win32 error code
1345
 */
1346
static UINT rdpei_pen_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1347
                            INT32 x, INT32 y, ...)
1348
0
{
1349
0
  UINT error = 0;
1350
0
  va_list ap;
1351
1352
0
  va_start(ap, y);
1353
0
  error = rdpei_pen_process(context, externalId,
1354
0
                            RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1355
0
                                RDPINPUT_CONTACT_FLAG_INCONTACT,
1356
0
                            fieldFlags, x, y, ap);
1357
0
  va_end(ap);
1358
1359
0
  return error;
1360
0
}
1361
1362
/**
1363
 * Function description
1364
 *
1365
 * @return 0 on success, otherwise a Win32 error code
1366
 */
1367
static UINT rdpei_pen_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1368
                             INT32 x, INT32 y, ...)
1369
0
{
1370
0
  UINT error = 0;
1371
0
  va_list ap;
1372
1373
0
  va_start(ap, y);
1374
0
  error = rdpei_pen_process(context, externalId,
1375
0
                            RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1376
0
                                RDPINPUT_CONTACT_FLAG_INCONTACT,
1377
0
                            fieldFlags, x, y, ap);
1378
0
  va_end(ap);
1379
0
  return error;
1380
0
}
1381
1382
/**
1383
 * Function description
1384
 *
1385
 * @return 0 on success, otherwise a Win32 error code
1386
 */
1387
static UINT rdpei_pen_end(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, INT32 x,
1388
                          INT32 y, ...)
1389
0
{
1390
0
  UINT error = 0;
1391
0
  va_list ap;
1392
0
  va_start(ap, y);
1393
0
  error = rdpei_pen_process(context, externalId,
1394
0
                            RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_INRANGE, fieldFlags,
1395
0
                            x, y, ap);
1396
0
  va_end(ap);
1397
0
  return error;
1398
0
}
1399
1400
/**
1401
 * Function description
1402
 *
1403
 * @return 0 on success, otherwise a Win32 error code
1404
 */
1405
static UINT rdpei_pen_hover_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1406
                                  INT32 x, INT32 y, ...)
1407
0
{
1408
0
  UINT error = 0;
1409
0
  va_list ap;
1410
1411
0
  va_start(ap, y);
1412
0
  error = rdpei_pen_process(context, externalId,
1413
0
                            RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1414
0
                            fieldFlags, x, y, ap);
1415
0
  va_end(ap);
1416
1417
0
  return error;
1418
0
}
1419
1420
/**
1421
 * Function description
1422
 *
1423
 * @return 0 on success, otherwise a Win32 error code
1424
 */
1425
static UINT rdpei_pen_hover_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1426
                                   INT32 x, INT32 y, ...)
1427
0
{
1428
0
  UINT error = 0;
1429
0
  va_list ap;
1430
1431
0
  va_start(ap, y);
1432
0
  error = rdpei_pen_process(context, externalId,
1433
0
                            RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1434
0
                            fieldFlags, x, y, ap);
1435
0
  va_end(ap);
1436
1437
0
  return error;
1438
0
}
1439
1440
/**
1441
 * Function description
1442
 *
1443
 * @return 0 on success, otherwise a Win32 error code
1444
 */
1445
static UINT rdpei_pen_hover_cancel(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1446
                                   INT32 x, INT32 y, ...)
1447
0
{
1448
0
  UINT error = 0;
1449
0
  va_list ap;
1450
1451
0
  va_start(ap, y);
1452
0
  error = rdpei_pen_process(context, externalId,
1453
0
                            RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_CANCELED,
1454
0
                            fieldFlags, x, y, ap);
1455
0
  va_end(ap);
1456
1457
0
  return error;
1458
0
}
1459
1460
static UINT rdpei_pen_raw_event(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1461
                                UINT32 fieldFlags, INT32 x, INT32 y, ...)
1462
0
{
1463
0
  UINT error = 0;
1464
0
  va_list ap;
1465
1466
0
  va_start(ap, y);
1467
0
  error = rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, ap);
1468
0
  va_end(ap);
1469
0
  return error;
1470
0
}
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
0
{
1476
0
  return rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, args);
1477
0
}
1478
1479
static UINT init_plugin_cb(GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext, rdpSettings* settings)
1480
0
{
1481
0
  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1482
1483
0
  WINPR_ASSERT(base);
1484
0
  WINPR_UNUSED(settings);
1485
1486
0
  rdpei->version = RDPINPUT_PROTOCOL_V300;
1487
0
  rdpei->currentFrameTime = 0;
1488
0
  rdpei->previousFrameTime = 0;
1489
0
  rdpei->maxTouchContacts = MAX_CONTACTS;
1490
0
  rdpei->maxPenContacts = MAX_PEN_CONTACTS;
1491
0
  rdpei->rdpcontext = rcontext;
1492
1493
0
  WINPR_ASSERT(rdpei->base.log);
1494
1495
0
  InitializeCriticalSection(&rdpei->lock);
1496
0
  rdpei->event = CreateEventA(NULL, TRUE, FALSE, NULL);
1497
0
  if (!rdpei->event)
1498
0
  {
1499
0
    WLog_Print(rdpei->base.log, WLOG_ERROR, "calloc failed!");
1500
0
    return CHANNEL_RC_NO_MEMORY;
1501
0
  }
1502
1503
0
  RdpeiClientContext* context = (RdpeiClientContext*)calloc(1, sizeof(RdpeiClientContext));
1504
0
  if (!context)
1505
0
  {
1506
0
    WLog_Print(rdpei->base.log, WLOG_ERROR, "calloc failed!");
1507
0
    return CHANNEL_RC_NO_MEMORY;
1508
0
  }
1509
1510
0
  context->clientFeaturesMask = UINT32_MAX;
1511
0
  context->handle = (void*)rdpei;
1512
0
  context->GetVersion = rdpei_get_version;
1513
0
  context->GetFeatures = rdpei_get_features;
1514
0
  context->AddContact = rdpei_add_contact;
1515
0
  context->TouchBegin = rdpei_touch_begin;
1516
0
  context->TouchUpdate = rdpei_touch_update;
1517
0
  context->TouchEnd = rdpei_touch_end;
1518
0
  context->TouchCancel = rdpei_touch_cancel;
1519
0
  context->TouchRawEvent = rdpei_touch_raw_event;
1520
0
  context->TouchRawEventVA = rdpei_touch_raw_event_va;
1521
0
  context->AddPen = rdpei_add_pen;
1522
0
  context->PenBegin = rdpei_pen_begin;
1523
0
  context->PenUpdate = rdpei_pen_update;
1524
0
  context->PenEnd = rdpei_pen_end;
1525
0
  context->PenHoverBegin = rdpei_pen_hover_begin;
1526
0
  context->PenHoverUpdate = rdpei_pen_hover_update;
1527
0
  context->PenHoverCancel = rdpei_pen_hover_cancel;
1528
0
  context->PenRawEvent = rdpei_pen_raw_event;
1529
0
  context->PenRawEventVA = rdpei_pen_raw_event_va;
1530
1531
0
  rdpei->context = context;
1532
0
  rdpei->base.iface.pInterface = (void*)context;
1533
1534
0
  rdpei->async =
1535
0
      !freerdp_settings_get_bool(rdpei->rdpcontext->settings, FreeRDP_SynchronousDynamicChannels);
1536
0
  if (rdpei->async)
1537
0
  {
1538
0
    rdpei->running = TRUE;
1539
1540
0
    rdpei->thread = CreateThread(NULL, 0, rdpei_periodic_update, rdpei, 0, NULL);
1541
0
    if (!rdpei->thread)
1542
0
    {
1543
0
      WLog_Print(rdpei->base.log, WLOG_ERROR, "calloc failed!");
1544
0
      return CHANNEL_RC_NO_MEMORY;
1545
0
    }
1546
0
  }
1547
0
  else
1548
0
  {
1549
0
    if (!freerdp_client_channel_register(rdpei->rdpcontext->channels, rdpei->event,
1550
0
                                         rdpei_poll_run, rdpei))
1551
0
      return ERROR_INTERNAL_ERROR;
1552
0
  }
1553
1554
0
  return CHANNEL_RC_OK;
1555
0
}
1556
1557
static void terminate_plugin_cb(GENERIC_DYNVC_PLUGIN* base)
1558
0
{
1559
0
  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1560
0
  WINPR_ASSERT(rdpei);
1561
1562
0
  rdpei->running = FALSE;
1563
0
  if (rdpei->event)
1564
0
    (void)SetEvent(rdpei->event);
1565
1566
0
  if (rdpei->thread)
1567
0
  {
1568
0
    (void)WaitForSingleObject(rdpei->thread, INFINITE);
1569
0
    (void)CloseHandle(rdpei->thread);
1570
0
  }
1571
1572
0
  if (rdpei->event && !rdpei->async)
1573
0
    (void)freerdp_client_channel_unregister(rdpei->rdpcontext->channels, rdpei->event);
1574
1575
0
  if (rdpei->event)
1576
0
    (void)CloseHandle(rdpei->event);
1577
1578
0
  DeleteCriticalSection(&rdpei->lock);
1579
0
  free(rdpei->context);
1580
0
}
1581
1582
static const IWTSVirtualChannelCallback geometry_callbacks = { rdpei_on_data_received,
1583
                                                             NULL, /* Open */
1584
                                                             rdpei_on_close, NULL };
1585
1586
/**
1587
 * Function description
1588
 *
1589
 * @return 0 on success, otherwise a Win32 error code
1590
 */
1591
FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpei_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1592
0
{
1593
0
  return freerdp_generic_DVCPluginEntry(pEntryPoints, RDPEI_TAG, RDPEI_DVC_CHANNEL_NAME,
1594
0
                                        sizeof(RDPEI_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
1595
0
                                        &geometry_callbacks, init_plugin_cb, terminate_plugin_cb);
1596
0
}