Coverage Report

Created: 2024-09-08 06:20

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