Coverage Report

Created: 2024-05-20 06:11

/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
/**
548
 * Function description
549
 *
550
 * @return 0 on success, otherwise a Win32 error code
551
 */
552
static UINT rdpei_write_touch_frame(wStream* s, RDPINPUT_TOUCH_FRAME* frame)
553
0
{
554
0
  int rectSize = 2;
555
0
  RDPINPUT_CONTACT_DATA* contact = NULL;
556
0
  if (!s || !frame)
557
0
    return ERROR_INTERNAL_ERROR;
558
#ifdef WITH_DEBUG_RDPEI
559
  WLog_DBG(TAG, "contactCount: %" PRIu32 "", frame->contactCount);
560
  WLog_DBG(TAG, "frameOffset: 0x%016" PRIX64 "", frame->frameOffset);
561
#endif
562
0
  rdpei_write_2byte_unsigned(s,
563
0
                             frame->contactCount); /* contactCount (TWO_BYTE_UNSIGNED_INTEGER) */
564
  /**
565
   * the time offset from the previous frame (in microseconds).
566
   * If this is the first frame being transmitted then this field MUST be set to zero.
567
   */
568
0
  rdpei_write_8byte_unsigned(s, frame->frameOffset *
569
0
                                    1000); /* frameOffset (EIGHT_BYTE_UNSIGNED_INTEGER) */
570
571
0
  if (!Stream_EnsureRemainingCapacity(s, (size_t)frame->contactCount * 64))
572
0
  {
573
0
    WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
574
0
    return CHANNEL_RC_NO_MEMORY;
575
0
  }
576
577
0
  for (UINT32 index = 0; index < frame->contactCount; index++)
578
0
  {
579
0
    contact = &frame->contacts[index];
580
0
    contact->fieldsPresent |= CONTACT_DATA_CONTACTRECT_PRESENT;
581
0
    contact->contactRectLeft = contact->x - rectSize;
582
0
    contact->contactRectTop = contact->y - rectSize;
583
0
    contact->contactRectRight = contact->x + rectSize;
584
0
    contact->contactRectBottom = contact->y + rectSize;
585
#ifdef WITH_DEBUG_RDPEI
586
    WLog_DBG(TAG, "contact[%" PRIu32 "].contactId: %" PRIu32 "", index, contact->contactId);
587
    WLog_DBG(TAG, "contact[%" PRIu32 "].fieldsPresent: %" PRIu32 "", index,
588
             contact->fieldsPresent);
589
    WLog_DBG(TAG, "contact[%" PRIu32 "].x: %" PRId32 "", index, contact->x);
590
    WLog_DBG(TAG, "contact[%" PRIu32 "].y: %" PRId32 "", index, contact->y);
591
    WLog_DBG(TAG, "contact[%" PRIu32 "].contactFlags: 0x%08" PRIX32 "", index,
592
             contact->contactFlags);
593
    rdpei_print_contact_flags(contact->contactFlags);
594
#endif
595
0
    Stream_Write_UINT8(s, contact->contactId); /* contactId (1 byte) */
596
    /* fieldsPresent (TWO_BYTE_UNSIGNED_INTEGER) */
597
0
    rdpei_write_2byte_unsigned(s, contact->fieldsPresent);
598
0
    rdpei_write_4byte_signed(s, contact->x); /* x (FOUR_BYTE_SIGNED_INTEGER) */
599
0
    rdpei_write_4byte_signed(s, contact->y); /* y (FOUR_BYTE_SIGNED_INTEGER) */
600
    /* contactFlags (FOUR_BYTE_UNSIGNED_INTEGER) */
601
0
    rdpei_write_4byte_unsigned(s, contact->contactFlags);
602
603
0
    if (contact->fieldsPresent & CONTACT_DATA_CONTACTRECT_PRESENT)
604
0
    {
605
      /* contactRectLeft (TWO_BYTE_SIGNED_INTEGER) */
606
0
      rdpei_write_2byte_signed(s, contact->contactRectLeft);
607
      /* contactRectTop (TWO_BYTE_SIGNED_INTEGER) */
608
0
      rdpei_write_2byte_signed(s, contact->contactRectTop);
609
      /* contactRectRight (TWO_BYTE_SIGNED_INTEGER) */
610
0
      rdpei_write_2byte_signed(s, contact->contactRectRight);
611
      /* contactRectBottom (TWO_BYTE_SIGNED_INTEGER) */
612
0
      rdpei_write_2byte_signed(s, contact->contactRectBottom);
613
0
    }
614
615
0
    if (contact->fieldsPresent & CONTACT_DATA_ORIENTATION_PRESENT)
616
0
    {
617
      /* orientation (FOUR_BYTE_UNSIGNED_INTEGER) */
618
0
      rdpei_write_4byte_unsigned(s, contact->orientation);
619
0
    }
620
621
0
    if (contact->fieldsPresent & CONTACT_DATA_PRESSURE_PRESENT)
622
0
    {
623
      /* pressure (FOUR_BYTE_UNSIGNED_INTEGER) */
624
0
      rdpei_write_4byte_unsigned(s, contact->pressure);
625
0
    }
626
0
  }
627
628
0
  return CHANNEL_RC_OK;
629
0
}
630
631
/**
632
 * Function description
633
 *
634
 * @return 0 on success, otherwise a Win32 error code
635
 */
636
static UINT rdpei_send_touch_event_pdu(GENERIC_CHANNEL_CALLBACK* callback,
637
                                       RDPINPUT_TOUCH_FRAME* frame)
638
0
{
639
0
  UINT status = 0;
640
0
  wStream* s = NULL;
641
0
  UINT32 pduLength = 0;
642
0
  RDPEI_PLUGIN* rdpei = NULL;
643
644
0
  WINPR_ASSERT(callback);
645
646
0
  rdpei = (RDPEI_PLUGIN*)callback->plugin;
647
0
  if (!rdpei || !rdpei->rdpcontext)
648
0
    return ERROR_INTERNAL_ERROR;
649
0
  if (freerdp_settings_get_bool(rdpei->rdpcontext->settings, FreeRDP_SuspendInput))
650
0
    return CHANNEL_RC_OK;
651
652
0
  if (!frame)
653
0
    return ERROR_INTERNAL_ERROR;
654
655
0
  pduLength = 64 + (frame->contactCount * 64);
656
0
  s = Stream_New(NULL, pduLength);
657
658
0
  if (!s)
659
0
  {
660
0
    WLog_ERR(TAG, "Stream_New failed!");
661
0
    return CHANNEL_RC_NO_MEMORY;
662
0
  }
663
664
0
  Stream_Seek(s, RDPINPUT_HEADER_LENGTH);
665
  /**
666
   * the time that has elapsed (in milliseconds) from when the oldest touch frame
667
   * was generated to when it was encoded for transmission by the client.
668
   */
669
0
  rdpei_write_4byte_unsigned(
670
0
      s, (UINT32)frame->frameOffset); /* encodeTime (FOUR_BYTE_UNSIGNED_INTEGER) */
671
0
  rdpei_write_2byte_unsigned(s, 1);   /* (frameCount) TWO_BYTE_UNSIGNED_INTEGER */
672
673
0
  if ((status = rdpei_write_touch_frame(s, frame)))
674
0
  {
675
0
    WLog_ERR(TAG, "rdpei_write_touch_frame failed with error %" PRIu32 "!", status);
676
0
    Stream_Free(s, TRUE);
677
0
    return status;
678
0
  }
679
680
0
  Stream_SealLength(s);
681
0
  pduLength = Stream_Length(s);
682
0
  status = rdpei_send_pdu(callback, s, EVENTID_TOUCH, pduLength);
683
0
  Stream_Free(s, TRUE);
684
0
  return status;
685
0
}
686
687
/**
688
 * Function description
689
 *
690
 * @return 0 on success, otherwise a Win32 error code
691
 */
692
static UINT rdpei_recv_sc_ready_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
693
0
{
694
0
  UINT32 features = 0;
695
0
  UINT32 protocolVersion = 0;
696
0
  RDPEI_PLUGIN* rdpei = NULL;
697
698
0
  if (!callback || !callback->plugin)
699
0
    return ERROR_INTERNAL_ERROR;
700
701
0
  rdpei = (RDPEI_PLUGIN*)callback->plugin;
702
703
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
704
0
    return ERROR_INVALID_DATA;
705
0
  Stream_Read_UINT32(s, protocolVersion); /* protocolVersion (4 bytes) */
706
707
0
  if (protocolVersion >= RDPINPUT_PROTOCOL_V300)
708
0
  {
709
0
    if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
710
0
      return ERROR_INVALID_DATA;
711
0
  }
712
713
0
  if (Stream_GetRemainingLength(s) >= 4)
714
0
    Stream_Read_UINT32(s, features);
715
716
0
  if (rdpei->version > protocolVersion)
717
0
    rdpei->version = protocolVersion;
718
0
  rdpei->features = features;
719
#if 0
720
721
  if (protocolVersion != RDPINPUT_PROTOCOL_V10)
722
  {
723
    WLog_ERR(TAG,  "Unknown [MS-RDPEI] protocolVersion: 0x%08"PRIX32"", protocolVersion);
724
    return -1;
725
  }
726
727
#endif
728
0
  return CHANNEL_RC_OK;
729
0
}
730
731
/**
732
 * Function description
733
 *
734
 * @return 0 on success, otherwise a Win32 error code
735
 */
736
static UINT rdpei_recv_suspend_touch_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
737
0
{
738
0
  UINT error = CHANNEL_RC_OK;
739
0
  RdpeiClientContext* rdpei = NULL;
740
741
0
  WINPR_UNUSED(s);
742
743
0
  if (!callback || !callback->plugin)
744
0
    return ERROR_INTERNAL_ERROR;
745
0
  rdpei = (RdpeiClientContext*)callback->plugin->pInterface;
746
0
  if (!rdpei)
747
0
    return ERROR_INTERNAL_ERROR;
748
749
0
  IFCALLRET(rdpei->SuspendTouch, error, rdpei);
750
751
0
  if (error)
752
0
    WLog_ERR(TAG, "rdpei->SuspendTouch failed with error %" PRIu32 "!", error);
753
754
0
  return error;
755
0
}
756
757
/**
758
 * Function description
759
 *
760
 * @return 0 on success, otherwise a Win32 error code
761
 */
762
static UINT rdpei_recv_resume_touch_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
763
0
{
764
0
  RdpeiClientContext* rdpei = NULL;
765
0
  UINT error = CHANNEL_RC_OK;
766
0
  if (!s || !callback || !callback->plugin)
767
0
    return ERROR_INTERNAL_ERROR;
768
0
  rdpei = (RdpeiClientContext*)callback->plugin->pInterface;
769
0
  if (!rdpei)
770
0
    return ERROR_INTERNAL_ERROR;
771
772
0
  IFCALLRET(rdpei->ResumeTouch, error, rdpei);
773
774
0
  if (error)
775
0
    WLog_ERR(TAG, "rdpei->ResumeTouch failed with error %" PRIu32 "!", error);
776
777
0
  return error;
778
0
}
779
780
/**
781
 * Function description
782
 *
783
 * @return 0 on success, otherwise a Win32 error code
784
 */
785
static UINT rdpei_recv_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
786
0
{
787
0
  UINT16 eventId = 0;
788
0
  UINT32 pduLength = 0;
789
0
  UINT error = 0;
790
791
0
  if (!s)
792
0
    return ERROR_INTERNAL_ERROR;
793
794
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
795
0
    return ERROR_INVALID_DATA;
796
797
0
  Stream_Read_UINT16(s, eventId);   /* eventId (2 bytes) */
798
0
  Stream_Read_UINT32(s, pduLength); /* pduLength (4 bytes) */
799
#ifdef WITH_DEBUG_RDPEI
800
  WLog_DBG(TAG, "rdpei_recv_pdu: eventId: %" PRIu16 " (%s) length: %" PRIu32 "", eventId,
801
           rdpei_eventid_string(eventId), pduLength);
802
#endif
803
804
0
  switch (eventId)
805
0
  {
806
0
    case EVENTID_SC_READY:
807
0
      if ((error = rdpei_recv_sc_ready_pdu(callback, s)))
808
0
      {
809
0
        WLog_ERR(TAG, "rdpei_recv_sc_ready_pdu failed with error %" PRIu32 "!", error);
810
0
        return error;
811
0
      }
812
813
0
      if ((error = rdpei_send_cs_ready_pdu(callback)))
814
0
      {
815
0
        WLog_ERR(TAG, "rdpei_send_cs_ready_pdu failed with error %" PRIu32 "!", error);
816
0
        return error;
817
0
      }
818
819
0
      break;
820
821
0
    case EVENTID_SUSPEND_TOUCH:
822
0
      if ((error = rdpei_recv_suspend_touch_pdu(callback, s)))
823
0
      {
824
0
        WLog_ERR(TAG, "rdpei_recv_suspend_touch_pdu failed with error %" PRIu32 "!", error);
825
0
        return error;
826
0
      }
827
828
0
      break;
829
830
0
    case EVENTID_RESUME_TOUCH:
831
0
      if ((error = rdpei_recv_resume_touch_pdu(callback, s)))
832
0
      {
833
0
        WLog_ERR(TAG, "rdpei_recv_resume_touch_pdu failed with error %" PRIu32 "!", error);
834
0
        return error;
835
0
      }
836
837
0
      break;
838
839
0
    default:
840
0
      break;
841
0
  }
842
843
0
  return CHANNEL_RC_OK;
844
0
}
845
846
/**
847
 * Function description
848
 *
849
 * @return 0 on success, otherwise a Win32 error code
850
 */
851
static UINT rdpei_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
852
0
{
853
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
854
0
  return rdpei_recv_pdu(callback, data);
855
0
}
856
857
/**
858
 * Function description
859
 *
860
 * @return 0 on success, otherwise a Win32 error code
861
 */
862
static UINT rdpei_on_close(IWTSVirtualChannelCallback* pChannelCallback)
863
0
{
864
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
865
0
  if (callback)
866
0
  {
867
0
    RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)callback->plugin;
868
0
    if (rdpei && rdpei->base.listener_callback)
869
0
    {
870
0
      if (rdpei->base.listener_callback->channel_callback == callback)
871
0
        rdpei->base.listener_callback->channel_callback = NULL;
872
0
    }
873
0
  }
874
0
  free(callback);
875
0
  return CHANNEL_RC_OK;
876
0
}
877
878
/**
879
 * Channel Client Interface
880
 */
881
882
static UINT32 rdpei_get_version(RdpeiClientContext* context)
883
0
{
884
0
  RDPEI_PLUGIN* rdpei = NULL;
885
0
  if (!context || !context->handle)
886
0
    return -1;
887
0
  rdpei = (RDPEI_PLUGIN*)context->handle;
888
0
  return rdpei->version;
889
0
}
890
891
static UINT32 rdpei_get_features(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->features;
898
0
}
899
900
/**
901
 * Function description
902
 *
903
 * @return 0 on success, otherwise a Win32 error code
904
 */
905
UINT rdpei_send_frame(RdpeiClientContext* context, RDPINPUT_TOUCH_FRAME* frame)
906
0
{
907
0
  UINT64 currentTime = GetTickCount64();
908
0
  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)context->handle;
909
0
  GENERIC_CHANNEL_CALLBACK* callback = NULL;
910
0
  UINT error = 0;
911
912
0
  callback = rdpei->base.listener_callback->channel_callback;
913
914
  /* Just ignore the event if the channel is not connected */
915
0
  if (!callback)
916
0
    return CHANNEL_RC_OK;
917
918
0
  if (!rdpei->previousFrameTime && !rdpei->currentFrameTime)
919
0
  {
920
0
    rdpei->currentFrameTime = currentTime;
921
0
    frame->frameOffset = 0;
922
0
  }
923
0
  else
924
0
  {
925
0
    rdpei->currentFrameTime = currentTime;
926
0
    frame->frameOffset = rdpei->currentFrameTime - rdpei->previousFrameTime;
927
0
  }
928
929
0
  if ((error = rdpei_send_touch_event_pdu(callback, frame)))
930
0
  {
931
0
    WLog_ERR(TAG, "rdpei_send_touch_event_pdu failed with error %" PRIu32 "!", error);
932
0
    return error;
933
0
  }
934
935
0
  rdpei->previousFrameTime = rdpei->currentFrameTime;
936
0
  return error;
937
0
}
938
939
/**
940
 * Function description
941
 *
942
 * @return 0 on success, otherwise a Win32 error code
943
 */
944
static UINT rdpei_add_contact(RdpeiClientContext* context, const RDPINPUT_CONTACT_DATA* contact)
945
0
{
946
0
  RDPINPUT_CONTACT_POINT* contactPoint = NULL;
947
0
  RDPEI_PLUGIN* rdpei = NULL;
948
0
  if (!context || !contact || !context->handle)
949
0
    return ERROR_INTERNAL_ERROR;
950
951
0
  rdpei = (RDPEI_PLUGIN*)context->handle;
952
953
0
  EnterCriticalSection(&rdpei->lock);
954
0
  contactPoint = &rdpei->contactPoints[contact->contactId];
955
0
  contactPoint->data = *contact;
956
0
  contactPoint->dirty = TRUE;
957
0
  SetEvent(rdpei->event);
958
0
  LeaveCriticalSection(&rdpei->lock);
959
960
0
  return CHANNEL_RC_OK;
961
0
}
962
963
static UINT rdpei_touch_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
964
                                INT32 x, INT32 y, INT32* contactId, UINT32 fieldFlags, va_list ap)
965
0
{
966
0
  INT64 contactIdlocal = -1;
967
0
  RDPINPUT_CONTACT_POINT* contactPoint = NULL;
968
0
  RDPEI_PLUGIN* rdpei = NULL;
969
0
  BOOL begin = 0;
970
0
  UINT error = CHANNEL_RC_OK;
971
972
0
  if (!context || !contactId || !context->handle)
973
0
    return ERROR_INTERNAL_ERROR;
974
975
0
  rdpei = (RDPEI_PLUGIN*)context->handle;
976
  /* Create a new contact point in an empty slot */
977
0
  EnterCriticalSection(&rdpei->lock);
978
0
  begin = contactFlags & RDPINPUT_CONTACT_FLAG_DOWN;
979
0
  contactPoint = rdpei_contact(rdpei, externalId, !begin);
980
0
  if (contactPoint)
981
0
    contactIdlocal = contactPoint->contactId;
982
0
  LeaveCriticalSection(&rdpei->lock);
983
984
0
  if (contactIdlocal >= 0)
985
0
  {
986
0
    RDPINPUT_CONTACT_DATA contact = { 0 };
987
0
    contact.x = x;
988
0
    contact.y = y;
989
0
    contact.contactId = contactIdlocal;
990
0
    contact.contactFlags = contactFlags;
991
0
    contact.fieldsPresent = fieldFlags;
992
993
0
    if (fieldFlags & CONTACT_DATA_CONTACTRECT_PRESENT)
994
0
    {
995
0
      contact.contactRectLeft = va_arg(ap, INT32);
996
0
      contact.contactRectTop = va_arg(ap, INT32);
997
0
      contact.contactRectRight = va_arg(ap, INT32);
998
0
      contact.contactRectBottom = va_arg(ap, INT32);
999
0
    }
1000
0
    if (fieldFlags & CONTACT_DATA_ORIENTATION_PRESENT)
1001
0
    {
1002
0
      UINT32 p = va_arg(ap, UINT32);
1003
0
      if (p >= 360)
1004
0
      {
1005
0
        WLog_WARN(TAG,
1006
0
                  "TouchContact %" PRIu32 ": Invalid orientation value %" PRIu32
1007
0
                  "degree, clamping to 359 degree",
1008
0
                  contactIdlocal, p);
1009
0
        p = 359;
1010
0
      }
1011
0
      contact.orientation = p;
1012
0
    }
1013
0
    if (fieldFlags & CONTACT_DATA_PRESSURE_PRESENT)
1014
0
    {
1015
0
      UINT32 p = va_arg(ap, UINT32);
1016
0
      if (p > 1024)
1017
0
      {
1018
0
        WLog_WARN(TAG,
1019
0
                  "TouchContact %" PRIu32 ": Invalid pressure value %" PRIu32
1020
0
                  ", clamping to 1024",
1021
0
                  contactIdlocal, p);
1022
0
        p = 1024;
1023
0
      }
1024
0
      contact.pressure = p;
1025
0
    }
1026
1027
0
    error = context->AddContact(context, &contact);
1028
0
  }
1029
1030
0
  if (contactId)
1031
0
    *contactId = contactIdlocal;
1032
0
  return error;
1033
0
}
1034
1035
/**
1036
 * Function description
1037
 *
1038
 * @return 0 on success, otherwise a Win32 error code
1039
 */
1040
static UINT rdpei_touch_begin(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1041
                              INT32* contactId)
1042
0
{
1043
0
  UINT rc = 0;
1044
0
  va_list ap = { 0 };
1045
0
  rc = rdpei_touch_process(context, externalId,
1046
0
                           RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1047
0
                               RDPINPUT_CONTACT_FLAG_INCONTACT,
1048
0
                           x, y, contactId, 0, ap);
1049
0
  return rc;
1050
0
}
1051
1052
/**
1053
 * Function description
1054
 *
1055
 * @return 0 on success, otherwise a Win32 error code
1056
 */
1057
static UINT rdpei_touch_update(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1058
                               INT32* contactId)
1059
0
{
1060
0
  UINT rc = 0;
1061
0
  va_list ap = { 0 };
1062
0
  rc = rdpei_touch_process(context, externalId,
1063
0
                           RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1064
0
                               RDPINPUT_CONTACT_FLAG_INCONTACT,
1065
0
                           x, y, contactId, 0, ap);
1066
0
  return rc;
1067
0
}
1068
1069
/**
1070
 * Function description
1071
 *
1072
 * @return 0 on success, otherwise a Win32 error code
1073
 */
1074
static UINT rdpei_touch_end(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1075
                            INT32* contactId)
1076
0
{
1077
0
  UINT error = 0;
1078
0
  va_list ap = { 0 };
1079
0
  error = rdpei_touch_process(context, externalId,
1080
0
                              RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1081
0
                                  RDPINPUT_CONTACT_FLAG_INCONTACT,
1082
0
                              x, y, contactId, 0, ap);
1083
0
  if (error != CHANNEL_RC_OK)
1084
0
    return error;
1085
0
  error =
1086
0
      rdpei_touch_process(context, externalId, RDPINPUT_CONTACT_FLAG_UP, x, y, contactId, 0, ap);
1087
0
  return error;
1088
0
}
1089
1090
/**
1091
 * Function description
1092
 *
1093
 * @return 0 on success, otherwise a Win32 error code
1094
 */
1095
static UINT rdpei_touch_cancel(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1096
                               INT32* contactId)
1097
0
{
1098
0
  UINT rc = 0;
1099
0
  va_list ap = { 0 };
1100
0
  rc = rdpei_touch_process(context, externalId,
1101
0
                           RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_CANCELED, x, y,
1102
0
                           contactId, 0, ap);
1103
0
  return rc;
1104
0
}
1105
1106
static UINT rdpei_touch_raw_event(RdpeiClientContext* context, INT32 externalId, INT32 x, INT32 y,
1107
                                  INT32* contactId, UINT32 flags, UINT32 fieldFlags, ...)
1108
0
{
1109
0
  UINT rc = 0;
1110
0
  va_list ap;
1111
0
  va_start(ap, fieldFlags);
1112
0
  rc = rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, ap);
1113
0
  va_end(ap);
1114
0
  return rc;
1115
0
}
1116
1117
static UINT rdpei_touch_raw_event_va(RdpeiClientContext* context, INT32 externalId, INT32 x,
1118
                                     INT32 y, INT32* contactId, UINT32 flags, UINT32 fieldFlags,
1119
                                     va_list args)
1120
0
{
1121
0
  return rdpei_touch_process(context, externalId, flags, x, y, contactId, fieldFlags, args);
1122
0
}
1123
1124
static RDPINPUT_PEN_CONTACT_POINT* rdpei_pen_contact(RDPEI_PLUGIN* rdpei, INT32 externalId,
1125
                                                     BOOL active)
1126
0
{
1127
0
  if (!rdpei)
1128
0
    return NULL;
1129
1130
0
  for (UINT32 x = 0; x < rdpei->maxPenContacts; x++)
1131
0
  {
1132
0
    RDPINPUT_PEN_CONTACT_POINT* contact = &rdpei->penContactPoints[x];
1133
0
    if (active)
1134
0
    {
1135
0
      if (contact->active)
1136
0
      {
1137
0
        if (contact->externalId == externalId)
1138
0
          return contact;
1139
0
      }
1140
0
    }
1141
0
    else
1142
0
    {
1143
0
      if (!contact->active)
1144
0
      {
1145
0
        contact->externalId = externalId;
1146
0
        contact->active = TRUE;
1147
0
        return contact;
1148
0
      }
1149
0
    }
1150
0
  }
1151
0
  return NULL;
1152
0
}
1153
1154
static UINT rdpei_add_pen(RdpeiClientContext* context, INT32 externalId,
1155
                          const RDPINPUT_PEN_CONTACT* contact)
1156
0
{
1157
0
  RDPEI_PLUGIN* rdpei = NULL;
1158
0
  RDPINPUT_PEN_CONTACT_POINT* contactPoint = NULL;
1159
1160
0
  if (!context || !contact || !context->handle)
1161
0
    return ERROR_INTERNAL_ERROR;
1162
1163
0
  rdpei = (RDPEI_PLUGIN*)context->handle;
1164
1165
0
  EnterCriticalSection(&rdpei->lock);
1166
0
  contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1167
0
  if (contactPoint)
1168
0
  {
1169
0
    contactPoint->data = *contact;
1170
0
    contactPoint->dirty = TRUE;
1171
0
    SetEvent(rdpei->event);
1172
0
  }
1173
0
  LeaveCriticalSection(&rdpei->lock);
1174
1175
0
  return CHANNEL_RC_OK;
1176
0
}
1177
1178
static UINT rdpei_pen_process(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1179
                              UINT32 fieldFlags, INT32 x, INT32 y, va_list ap)
1180
0
{
1181
0
  RDPINPUT_PEN_CONTACT_POINT* contactPoint = NULL;
1182
0
  RDPEI_PLUGIN* rdpei = NULL;
1183
0
  UINT error = CHANNEL_RC_OK;
1184
1185
0
  if (!context || !context->handle)
1186
0
    return ERROR_INTERNAL_ERROR;
1187
1188
0
  rdpei = (RDPEI_PLUGIN*)context->handle;
1189
1190
0
  EnterCriticalSection(&rdpei->lock);
1191
  // Start a new contact only when it is not active.
1192
0
  contactPoint = rdpei_pen_contact(rdpei, externalId, TRUE);
1193
0
  if (!contactPoint)
1194
0
  {
1195
0
    const UINT32 mask = RDPINPUT_CONTACT_FLAG_INRANGE;
1196
0
    if ((contactFlags & mask) == mask)
1197
0
    {
1198
0
      contactPoint = rdpei_pen_contact(rdpei, externalId, FALSE);
1199
0
    }
1200
0
  }
1201
0
  LeaveCriticalSection(&rdpei->lock);
1202
0
  if (contactPoint != NULL)
1203
0
  {
1204
0
    RDPINPUT_PEN_CONTACT contact = { 0 };
1205
1206
0
    contact.x = x;
1207
0
    contact.y = y;
1208
0
    contact.fieldsPresent = fieldFlags;
1209
1210
0
    contact.contactFlags = contactFlags;
1211
0
    if (fieldFlags & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
1212
0
      contact.penFlags = va_arg(ap, UINT32);
1213
0
    if (fieldFlags & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
1214
0
      contact.pressure = va_arg(ap, UINT32);
1215
0
    if (fieldFlags & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
1216
0
      contact.rotation = va_arg(ap, UINT32);
1217
0
    if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
1218
0
      contact.tiltX = va_arg(ap, INT32);
1219
0
    if (fieldFlags & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
1220
0
      contact.tiltY = va_arg(ap, INT32);
1221
1222
0
    error = context->AddPen(context, externalId, &contact);
1223
0
  }
1224
1225
0
  return error;
1226
0
}
1227
1228
/**
1229
 * Function description
1230
 *
1231
 * @return 0 on success, otherwise a Win32 error code
1232
 */
1233
static UINT rdpei_pen_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1234
                            INT32 x, INT32 y, ...)
1235
0
{
1236
0
  UINT error = 0;
1237
0
  va_list ap;
1238
1239
0
  va_start(ap, y);
1240
0
  error = rdpei_pen_process(context, externalId,
1241
0
                            RDPINPUT_CONTACT_FLAG_DOWN | RDPINPUT_CONTACT_FLAG_INRANGE |
1242
0
                                RDPINPUT_CONTACT_FLAG_INCONTACT,
1243
0
                            fieldFlags, x, y, ap);
1244
0
  va_end(ap);
1245
1246
0
  return error;
1247
0
}
1248
1249
/**
1250
 * Function description
1251
 *
1252
 * @return 0 on success, otherwise a Win32 error code
1253
 */
1254
static UINT rdpei_pen_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1255
                             INT32 x, INT32 y, ...)
1256
0
{
1257
0
  UINT error = 0;
1258
0
  va_list ap;
1259
1260
0
  va_start(ap, y);
1261
0
  error = rdpei_pen_process(context, externalId,
1262
0
                            RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE |
1263
0
                                RDPINPUT_CONTACT_FLAG_INCONTACT,
1264
0
                            fieldFlags, x, y, ap);
1265
0
  va_end(ap);
1266
0
  return error;
1267
0
}
1268
1269
/**
1270
 * Function description
1271
 *
1272
 * @return 0 on success, otherwise a Win32 error code
1273
 */
1274
static UINT rdpei_pen_end(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags, INT32 x,
1275
                          INT32 y, ...)
1276
0
{
1277
0
  UINT error = 0;
1278
0
  va_list ap;
1279
0
  va_start(ap, y);
1280
0
  error = rdpei_pen_process(context, externalId,
1281
0
                            RDPINPUT_CONTACT_FLAG_UP | RDPINPUT_CONTACT_FLAG_INRANGE, fieldFlags,
1282
0
                            x, y, ap);
1283
0
  va_end(ap);
1284
0
  return error;
1285
0
}
1286
1287
/**
1288
 * Function description
1289
 *
1290
 * @return 0 on success, otherwise a Win32 error code
1291
 */
1292
static UINT rdpei_pen_hover_begin(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1293
                                  INT32 x, INT32 y, ...)
1294
0
{
1295
0
  UINT error = 0;
1296
0
  va_list ap;
1297
1298
0
  va_start(ap, y);
1299
0
  error = rdpei_pen_process(context, externalId,
1300
0
                            RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1301
0
                            fieldFlags, x, y, ap);
1302
0
  va_end(ap);
1303
1304
0
  return error;
1305
0
}
1306
1307
/**
1308
 * Function description
1309
 *
1310
 * @return 0 on success, otherwise a Win32 error code
1311
 */
1312
static UINT rdpei_pen_hover_update(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1313
                                   INT32 x, INT32 y, ...)
1314
0
{
1315
0
  UINT error = 0;
1316
0
  va_list ap;
1317
1318
0
  va_start(ap, y);
1319
0
  error = rdpei_pen_process(context, externalId,
1320
0
                            RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_INRANGE,
1321
0
                            fieldFlags, x, y, ap);
1322
0
  va_end(ap);
1323
1324
0
  return error;
1325
0
}
1326
1327
/**
1328
 * Function description
1329
 *
1330
 * @return 0 on success, otherwise a Win32 error code
1331
 */
1332
static UINT rdpei_pen_hover_cancel(RdpeiClientContext* context, INT32 externalId, UINT32 fieldFlags,
1333
                                   INT32 x, INT32 y, ...)
1334
0
{
1335
0
  UINT error = 0;
1336
0
  va_list ap;
1337
1338
0
  va_start(ap, y);
1339
0
  error = rdpei_pen_process(context, externalId,
1340
0
                            RDPINPUT_CONTACT_FLAG_UPDATE | RDPINPUT_CONTACT_FLAG_CANCELED,
1341
0
                            fieldFlags, x, y, ap);
1342
0
  va_end(ap);
1343
1344
0
  return error;
1345
0
}
1346
1347
static UINT rdpei_pen_raw_event(RdpeiClientContext* context, INT32 externalId, UINT32 contactFlags,
1348
                                UINT32 fieldFlags, INT32 x, INT32 y, ...)
1349
0
{
1350
0
  UINT error = 0;
1351
0
  va_list ap;
1352
1353
0
  va_start(ap, y);
1354
0
  error = rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, ap);
1355
0
  va_end(ap);
1356
0
  return error;
1357
0
}
1358
1359
static UINT rdpei_pen_raw_event_va(RdpeiClientContext* context, INT32 externalId,
1360
                                   UINT32 contactFlags, UINT32 fieldFlags, INT32 x, INT32 y,
1361
                                   va_list args)
1362
0
{
1363
0
  return rdpei_pen_process(context, externalId, contactFlags, fieldFlags, x, y, args);
1364
0
}
1365
1366
static UINT init_plugin_cb(GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext, rdpSettings* settings)
1367
0
{
1368
0
  RdpeiClientContext* context = NULL;
1369
0
  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1370
1371
0
  WINPR_ASSERT(base);
1372
0
  WINPR_UNUSED(settings);
1373
1374
0
  rdpei->version = RDPINPUT_PROTOCOL_V300;
1375
0
  rdpei->currentFrameTime = 0;
1376
0
  rdpei->previousFrameTime = 0;
1377
0
  rdpei->maxTouchContacts = MAX_CONTACTS;
1378
0
  rdpei->maxPenContacts = MAX_PEN_CONTACTS;
1379
0
  rdpei->rdpcontext = rcontext;
1380
1381
0
  InitializeCriticalSection(&rdpei->lock);
1382
0
  rdpei->event = CreateEventA(NULL, TRUE, FALSE, NULL);
1383
0
  if (!rdpei->event)
1384
0
  {
1385
0
    WLog_ERR(TAG, "calloc failed!");
1386
0
    return CHANNEL_RC_NO_MEMORY;
1387
0
  }
1388
1389
0
  context = (RdpeiClientContext*)calloc(1, sizeof(*context));
1390
0
  if (!context)
1391
0
  {
1392
0
    WLog_ERR(TAG, "calloc failed!");
1393
0
    return CHANNEL_RC_NO_MEMORY;
1394
0
  }
1395
1396
0
  context->clientFeaturesMask = UINT32_MAX;
1397
0
  context->handle = (void*)rdpei;
1398
0
  context->GetVersion = rdpei_get_version;
1399
0
  context->GetFeatures = rdpei_get_features;
1400
0
  context->AddContact = rdpei_add_contact;
1401
0
  context->TouchBegin = rdpei_touch_begin;
1402
0
  context->TouchUpdate = rdpei_touch_update;
1403
0
  context->TouchEnd = rdpei_touch_end;
1404
0
  context->TouchCancel = rdpei_touch_cancel;
1405
0
  context->TouchRawEvent = rdpei_touch_raw_event;
1406
0
  context->TouchRawEventVA = rdpei_touch_raw_event_va;
1407
0
  context->AddPen = rdpei_add_pen;
1408
0
  context->PenBegin = rdpei_pen_begin;
1409
0
  context->PenUpdate = rdpei_pen_update;
1410
0
  context->PenEnd = rdpei_pen_end;
1411
0
  context->PenHoverBegin = rdpei_pen_hover_begin;
1412
0
  context->PenHoverUpdate = rdpei_pen_hover_update;
1413
0
  context->PenHoverCancel = rdpei_pen_hover_cancel;
1414
0
  context->PenRawEvent = rdpei_pen_raw_event;
1415
0
  context->PenRawEventVA = rdpei_pen_raw_event_va;
1416
1417
0
  rdpei->context = context;
1418
0
  rdpei->base.iface.pInterface = (void*)context;
1419
1420
0
  rdpei->running = TRUE;
1421
0
  rdpei->thread = CreateThread(NULL, 0, rdpei_periodic_update, rdpei, 0, NULL);
1422
0
  if (!rdpei->thread)
1423
0
  {
1424
0
    WLog_ERR(TAG, "calloc failed!");
1425
0
    return CHANNEL_RC_NO_MEMORY;
1426
0
  }
1427
1428
0
  return CHANNEL_RC_OK;
1429
0
}
1430
1431
static void terminate_plugin_cb(GENERIC_DYNVC_PLUGIN* base)
1432
0
{
1433
0
  RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*)base;
1434
0
  WINPR_ASSERT(rdpei);
1435
1436
0
  rdpei->running = FALSE;
1437
0
  if (rdpei->event)
1438
0
    SetEvent(rdpei->event);
1439
1440
0
  if (rdpei->thread)
1441
0
  {
1442
0
    WaitForSingleObject(rdpei->thread, INFINITE);
1443
0
    CloseHandle(rdpei->thread);
1444
0
  }
1445
1446
0
  if (rdpei->event)
1447
0
    CloseHandle(rdpei->event);
1448
1449
0
  DeleteCriticalSection(&rdpei->lock);
1450
0
  free(rdpei->context);
1451
0
}
1452
1453
static const IWTSVirtualChannelCallback geometry_callbacks = { rdpei_on_data_received,
1454
                                                             NULL, /* Open */
1455
                                                             rdpei_on_close, NULL };
1456
1457
/**
1458
 * Function description
1459
 *
1460
 * @return 0 on success, otherwise a Win32 error code
1461
 */
1462
FREERDP_ENTRY_POINT(UINT rdpei_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1463
0
{
1464
0
  return freerdp_generic_DVCPluginEntry(pEntryPoints, TAG, RDPEI_DVC_CHANNEL_NAME,
1465
0
                                        sizeof(RDPEI_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
1466
0
                                        &geometry_callbacks, init_plugin_cb, terminate_plugin_cb);
1467
0
}