Coverage Report

Created: 2026-11-10 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/core/input.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Input PDUs
4
 *
5
 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
20
#include <time.h>
21
#include <freerdp/config.h>
22
23
#include <winpr/crt.h>
24
#include <winpr/assert.h>
25
26
#include <freerdp/input.h>
27
#include <freerdp/log.h>
28
29
#include "message.h"
30
31
#include "input.h"
32
33
24.7k
#define TAG FREERDP_TAG("core")
34
35
/* Input Events */
36
0
#define INPUT_EVENT_SYNC 0x0000
37
0
#define INPUT_EVENT_SCANCODE 0x0004
38
0
#define INPUT_EVENT_UNICODE 0x0005
39
0
#define INPUT_EVENT_MOUSE 0x8001
40
0
#define INPUT_EVENT_MOUSEX 0x8002
41
0
#define INPUT_EVENT_MOUSEREL 0x8004
42
43
static const char* SyncEventFlag2Str(uint32_t flag)
44
0
{
45
0
  if (flag == KBD_SYNC_SCROLL_LOCK)
46
0
    return "SYNC_SCROLL_LOCK";
47
0
  if (flag == KBD_SYNC_NUM_LOCK)
48
0
    return "SYNC_NUM_LOCK";
49
0
  if (flag == KBD_SYNC_CAPS_LOCK)
50
0
    return "SYNC_CAPS_LOCK";
51
0
  if (flag == KBD_SYNC_KANA_LOCK)
52
0
    return "SYNC_KANA_LOCK";
53
0
  return "SYNC_UNKNOWN";
54
0
}
55
56
static const char* SyncEventFlags2Str(const char* prefix, uint32_t flags, char* buffer, size_t len)
57
0
{
58
0
  const uint32_t tflags[] = { KBD_SYNC_SCROLL_LOCK, KBD_SYNC_NUM_LOCK, KBD_SYNC_CAPS_LOCK,
59
0
                            KBD_SYNC_KANA_LOCK };
60
61
0
  if (len <= 2)
62
0
    return nullptr;
63
64
0
  if (!winpr_str_append("{", buffer, len, ""))
65
0
    return nullptr;
66
67
  /* Strip initial symbol so we do not get duplicate separators */
68
0
  for (size_t x = 0; x < ARRAYSIZE(tflags); x++)
69
0
  {
70
0
    const uint32_t flag = tflags[x];
71
0
    if (flags & flag)
72
0
    {
73
0
      char ibuffer[64] = WINPR_C_ARRAY_INIT;
74
0
      (void)_snprintf(ibuffer, sizeof(ibuffer), "%s%s", prefix, SyncEventFlag2Str(flag));
75
0
      if (!winpr_str_append(ibuffer, &buffer[1], len - 2, "|"))
76
0
        return nullptr;
77
0
    }
78
0
  }
79
0
  if (!winpr_str_append("}", &buffer[1], len - 2, ""))
80
0
    return nullptr;
81
82
0
  return buffer;
83
0
}
84
85
const char* freerdp_input_keyboard_flags_string(uint32_t flags, char* buffer, size_t len)
86
0
{
87
0
  return SyncEventFlags2Str("KBD_", flags, buffer, len);
88
0
}
89
90
static void rdp_write_client_input_pdu_header(wStream* s, UINT16 number)
91
0
{
92
0
  WINPR_ASSERT(s);
93
0
  WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 4);
94
0
  Stream_Write_UINT16(s, number); /* numberEvents (2 bytes) */
95
0
  Stream_Write_UINT16(s, 0);      /* pad2Octets (2 bytes) */
96
0
}
97
98
static void rdp_write_input_event_header(wStream* s, UINT32 time, UINT16 type)
99
0
{
100
0
  WINPR_ASSERT(s);
101
0
  WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 6);
102
0
  Stream_Write_UINT32(s, time); /* eventTime (4 bytes) */
103
0
  Stream_Write_UINT16(s, type); /* messageType (2 bytes) */
104
0
}
105
106
static wStream* rdp_client_input_pdu_init(rdpRdp* rdp, UINT16 type, UINT16* sec_flags)
107
0
{
108
0
  wStream* s = rdp_data_pdu_init(rdp, sec_flags);
109
110
0
  if (!s)
111
0
    return nullptr;
112
113
0
  rdp_write_client_input_pdu_header(s, 1);
114
0
  rdp_write_input_event_header(s, 0, type);
115
0
  return s;
116
0
}
117
118
static BOOL rdp_send_client_input_pdu(rdpRdp* rdp, wStream* s, UINT16 sec_flags)
119
0
{
120
0
  WINPR_ASSERT(rdp);
121
0
  WINPR_ASSERT(rdp->mcs);
122
0
  return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_INPUT, rdp->mcs->userId, sec_flags);
123
0
}
124
125
static void input_write_synchronize_event(wStream* s, UINT32 flags)
126
0
{
127
0
  WINPR_ASSERT(s);
128
0
  WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 6);
129
0
  Stream_Write_UINT16(s, 0);     /* pad2Octets (2 bytes) */
130
0
  Stream_Write_UINT32(s, flags); /* toggleFlags (4 bytes) */
131
0
}
132
133
static BOOL input_ensure_client_running(rdpInput* input)
134
0
{
135
0
  WINPR_ASSERT(input);
136
0
  if (freerdp_shall_disconnect_context(input->context))
137
0
  {
138
0
    WLog_WARN(TAG, "[APPLICATION BUG] input functions called after the session terminated");
139
0
    return FALSE;
140
0
  }
141
0
  return TRUE;
142
0
}
143
144
static BOOL input_send_synchronize_event(rdpInput* input, UINT32 flags)
145
0
{
146
0
  UINT16 sec_flags = 0;
147
148
0
  if (!input || !input->context)
149
0
    return FALSE;
150
151
0
  rdpRdp* rdp = input->context->rdp;
152
153
0
  if (!input_ensure_client_running(input))
154
0
    return FALSE;
155
156
0
  wStream* s = rdp_client_input_pdu_init(rdp, INPUT_EVENT_SYNC, &sec_flags);
157
158
0
  if (!s)
159
0
    return FALSE;
160
161
0
  input_write_synchronize_event(s, flags);
162
0
  return rdp_send_client_input_pdu(rdp, s, sec_flags);
163
0
}
164
165
static void input_write_keyboard_event(wStream* s, UINT16 flags, UINT16 code)
166
0
{
167
0
  WINPR_ASSERT(s);
168
0
  WINPR_ASSERT(code <= UINT8_MAX);
169
170
0
  Stream_Write_UINT16(s, flags); /* keyboardFlags (2 bytes) */
171
0
  Stream_Write_UINT16(s, code);  /* keyCode (2 bytes) */
172
0
  Stream_Write_UINT16(s, 0);     /* pad2Octets (2 bytes) */
173
0
}
174
175
static BOOL input_send_keyboard_event(rdpInput* input, UINT16 flags, UINT8 code)
176
0
{
177
0
  UINT16 sec_flags = 0;
178
0
  wStream* s = nullptr;
179
0
  rdpRdp* rdp = nullptr;
180
181
0
  if (!input || !input->context)
182
0
    return FALSE;
183
184
0
  rdp = input->context->rdp;
185
186
0
  if (!input_ensure_client_running(input))
187
0
    return FALSE;
188
189
0
  s = rdp_client_input_pdu_init(rdp, INPUT_EVENT_SCANCODE, &sec_flags);
190
191
0
  if (!s)
192
0
    return FALSE;
193
194
0
  input_write_keyboard_event(s, flags, code);
195
0
  return rdp_send_client_input_pdu(rdp, s, sec_flags);
196
0
}
197
198
static void input_write_unicode_keyboard_event(wStream* s, UINT16 flags, UINT16 code)
199
0
{
200
0
  Stream_Write_UINT16(s, flags); /* keyboardFlags (2 bytes) */
201
0
  Stream_Write_UINT16(s, code);  /* unicodeCode (2 bytes) */
202
0
  Stream_Write_UINT16(s, 0);     /* pad2Octets (2 bytes) */
203
0
}
204
205
static BOOL input_send_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
206
0
{
207
0
  UINT16 sec_flags = 0;
208
0
  wStream* s = nullptr;
209
0
  rdpRdp* rdp = nullptr;
210
211
0
  if (!input || !input->context)
212
0
    return FALSE;
213
214
0
  if (!input_ensure_client_running(input))
215
0
    return FALSE;
216
217
0
  if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_UnicodeInput))
218
0
  {
219
0
    WLog_WARN(TAG, "Unicode input not supported by server.");
220
0
    return FALSE;
221
0
  }
222
223
0
  rdp = input->context->rdp;
224
0
  s = rdp_client_input_pdu_init(rdp, INPUT_EVENT_UNICODE, &sec_flags);
225
226
0
  if (!s)
227
0
    return FALSE;
228
229
0
  input_write_unicode_keyboard_event(s, flags, code);
230
0
  return rdp_send_client_input_pdu(rdp, s, sec_flags);
231
0
}
232
233
static void input_write_mouse_event(wStream* s, UINT16 flags, UINT16 x, UINT16 y)
234
0
{
235
0
  Stream_Write_UINT16(s, flags); /* pointerFlags (2 bytes) */
236
0
  Stream_Write_UINT16(s, x);     /* xPos (2 bytes) */
237
0
  Stream_Write_UINT16(s, y);     /* yPos (2 bytes) */
238
0
}
239
240
static BOOL input_send_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
241
0
{
242
0
  UINT16 sec_flags = 0;
243
244
0
  if (!input || !input->context || !input->context->settings)
245
0
    return FALSE;
246
247
0
  rdpRdp* rdp = input->context->rdp;
248
249
0
  if (!input_ensure_client_running(input))
250
0
    return FALSE;
251
252
0
  if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_HasHorizontalWheel))
253
0
  {
254
0
    if (flags & PTR_FLAGS_HWHEEL)
255
0
    {
256
0
      WLog_WARN(TAG,
257
0
                "skip mouse event %" PRIu16 "x%" PRIu16 " flags=0x%04" PRIX16
258
0
                ", no horizontal mouse wheel supported",
259
0
                x, y, flags);
260
0
      return TRUE;
261
0
    }
262
0
  }
263
264
0
  wStream* s = rdp_client_input_pdu_init(rdp, INPUT_EVENT_MOUSE, &sec_flags);
265
266
0
  if (!s)
267
0
    return FALSE;
268
269
0
  input_write_mouse_event(s, flags, x, y);
270
0
  return rdp_send_client_input_pdu(rdp, s, sec_flags);
271
0
}
272
273
static BOOL input_send_relmouse_event(rdpInput* input, UINT16 flags, INT16 xDelta, INT16 yDelta)
274
0
{
275
0
  UINT16 sec_flags = 0;
276
0
  wStream* s = nullptr;
277
0
  rdpRdp* rdp = nullptr;
278
279
0
  if (!input || !input->context || !input->context->settings)
280
0
    return FALSE;
281
282
0
  rdp = input->context->rdp;
283
284
0
  if (!input_ensure_client_running(input))
285
0
    return FALSE;
286
287
0
  if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_HasRelativeMouseEvent))
288
0
  {
289
0
    WLog_ERR(TAG, "Sending relative mouse event, but no support for that");
290
0
    return FALSE;
291
0
  }
292
293
0
  s = rdp_client_input_pdu_init(rdp, INPUT_EVENT_MOUSEREL, &sec_flags);
294
295
0
  if (!s)
296
0
    return FALSE;
297
298
0
  Stream_Write_UINT16(s, flags); /* pointerFlags (2 bytes) */
299
0
  Stream_Write_INT16(s, xDelta); /* xDelta (2 bytes) */
300
0
  Stream_Write_INT16(s, yDelta); /* yDelta (2 bytes) */
301
302
0
  return rdp_send_client_input_pdu(rdp, s, sec_flags);
303
0
}
304
305
static void input_write_extended_mouse_event(wStream* s, UINT16 flags, UINT16 x, UINT16 y)
306
0
{
307
0
  Stream_Write_UINT16(s, flags); /* pointerFlags (2 bytes) */
308
0
  Stream_Write_UINT16(s, x);     /* xPos (2 bytes) */
309
0
  Stream_Write_UINT16(s, y);     /* yPos (2 bytes) */
310
0
}
311
312
static BOOL input_send_extended_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
313
0
{
314
0
  UINT16 sec_flags = 0;
315
316
0
  WINPR_ASSERT(input);
317
0
  WINPR_ASSERT(input->context);
318
0
  WINPR_ASSERT(input->context->settings);
319
320
0
  rdpRdp* rdp = input->context->rdp;
321
0
  WINPR_ASSERT(rdp);
322
323
0
  if (!input_ensure_client_running(input))
324
0
    return FALSE;
325
326
0
  if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_HasExtendedMouseEvent))
327
0
  {
328
0
    WLog_WARN(TAG,
329
0
              "skip extended mouse event %" PRIu16 "x%" PRIu16 " flags=0x%04" PRIX16
330
0
              ", no extended mouse events supported",
331
0
              x, y, flags);
332
0
    return TRUE;
333
0
  }
334
335
0
  wStream* s = rdp_client_input_pdu_init(rdp, INPUT_EVENT_MOUSEX, &sec_flags);
336
337
0
  if (!s)
338
0
    return FALSE;
339
340
0
  input_write_extended_mouse_event(s, flags, x, y);
341
0
  return rdp_send_client_input_pdu(rdp, s, sec_flags);
342
0
}
343
344
static BOOL input_send_focus_in_event(rdpInput* input, UINT16 toggleStates)
345
0
{
346
  /* send a tab up like mstsc.exe */
347
0
  if (!input_send_keyboard_event(input, KBD_FLAGS_RELEASE, 0x0f))
348
0
    return FALSE;
349
350
  /* send the toggle key states */
351
0
  if (!input_send_synchronize_event(input, (toggleStates & 0x1F)))
352
0
    return FALSE;
353
354
  /* send another tab up like mstsc.exe */
355
0
  return input_send_keyboard_event(input, KBD_FLAGS_RELEASE, 0x0f);
356
0
}
357
358
static BOOL input_send_keyboard_pause_event(rdpInput* input)
359
0
{
360
  /* In ancient days, pause-down without control sent E1 1D 45 E1 9D C5,
361
   * and pause-up sent nothing.  However, reverse engineering mstsc shows
362
   * it sending the following sequence:
363
   */
364
365
  /* Control down (0x1D) */
366
0
  if (!input_send_keyboard_event(input, KBD_FLAGS_EXTENDED1,
367
0
                                 RDP_SCANCODE_CODE(RDP_SCANCODE_LCONTROL)))
368
0
    return FALSE;
369
370
  /* Numlock down (0x45) */
371
0
  if (!input_send_keyboard_event(input, 0, RDP_SCANCODE_CODE(RDP_SCANCODE_NUMLOCK)))
372
0
    return FALSE;
373
374
  /* Control up (0x1D) */
375
0
  if (!input_send_keyboard_event(input, KBD_FLAGS_RELEASE | KBD_FLAGS_EXTENDED1,
376
0
                                 RDP_SCANCODE_CODE(RDP_SCANCODE_LCONTROL)))
377
0
    return FALSE;
378
379
  /* Numlock up (0x45) */
380
0
  return input_send_keyboard_event(input, KBD_FLAGS_RELEASE,
381
0
                                   RDP_SCANCODE_CODE(RDP_SCANCODE_NUMLOCK));
382
0
}
383
384
static BOOL input_send_fastpath_synchronize_event(rdpInput* input, UINT32 flags)
385
0
{
386
0
  WINPR_ASSERT(input);
387
0
  WINPR_ASSERT(input->context);
388
389
  /* The FastPath Synchronization eventFlags has identical values as SlowPath,
390
   * but the field is only 5 bits wide. Drop everything else, a value with
391
   * undefined bits set may have been received from a peer (proxy) */
392
0
  const UINT32 mask =
393
0
      KBD_SYNC_SCROLL_LOCK | KBD_SYNC_NUM_LOCK | KBD_SYNC_CAPS_LOCK | KBD_SYNC_KANA_LOCK;
394
0
  if (flags & ~mask)
395
0
    WLog_WARN(TAG,
396
0
              "Problematic [MS-RDPBCGR] 2.2.8.1.2.2.5 Fast-Path Synchronize Event "
397
0
              "(TS_FP_SYNC_EVENT) flags=0x%08" PRIx32 ", ignoring undefined bits.",
398
0
              flags);
399
0
  rdpRdp* rdp = input->context->rdp;
400
0
  WINPR_ASSERT(rdp);
401
402
0
  if (!input_ensure_client_running(input))
403
0
    return FALSE;
404
405
  /* The FastPath Synchronization eventFlags has identical values as SlowPath */
406
0
  UINT16 sec_flags = 0;
407
0
  wStream* s =
408
0
      fastpath_input_pdu_init(rdp->fastpath, (BYTE)flags, FASTPATH_INPUT_EVENT_SYNC, &sec_flags);
409
410
0
  if (!s)
411
0
    return FALSE;
412
413
0
  return fastpath_send_input_pdu(rdp->fastpath, s, sec_flags);
414
0
}
415
416
static BOOL input_send_fastpath_keyboard_event(rdpInput* input, UINT16 flags, UINT8 code)
417
0
{
418
0
  UINT16 sec_flags = 0;
419
0
  wStream* s = nullptr;
420
0
  BYTE eventFlags = 0;
421
0
  rdpRdp* rdp = nullptr;
422
423
0
  WINPR_ASSERT(input);
424
0
  WINPR_ASSERT(input->context);
425
426
0
  rdp = input->context->rdp;
427
0
  WINPR_ASSERT(rdp);
428
429
0
  if (!input_ensure_client_running(input))
430
0
    return FALSE;
431
432
0
  eventFlags |= (flags & KBD_FLAGS_RELEASE) ? FASTPATH_INPUT_KBDFLAGS_RELEASE : 0;
433
0
  eventFlags |= (flags & KBD_FLAGS_EXTENDED) ? FASTPATH_INPUT_KBDFLAGS_EXTENDED : 0;
434
0
  eventFlags |= (flags & KBD_FLAGS_EXTENDED1) ? FASTPATH_INPUT_KBDFLAGS_PREFIX_E1 : 0;
435
0
  s = fastpath_input_pdu_init(rdp->fastpath, eventFlags, FASTPATH_INPUT_EVENT_SCANCODE,
436
0
                              &sec_flags);
437
438
0
  if (!s)
439
0
    return FALSE;
440
441
0
  WINPR_ASSERT(code <= UINT8_MAX);
442
0
  Stream_Write_UINT8(s, code); /* keyCode (1 byte) */
443
0
  return fastpath_send_input_pdu(rdp->fastpath, s, sec_flags);
444
0
}
445
446
static BOOL input_send_fastpath_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
447
0
{
448
0
  UINT16 sec_flags = 0;
449
0
  wStream* s = nullptr;
450
0
  BYTE eventFlags = 0;
451
0
  rdpRdp* rdp = nullptr;
452
453
0
  WINPR_ASSERT(input);
454
0
  WINPR_ASSERT(input->context);
455
0
  WINPR_ASSERT(input->context->settings);
456
457
0
  rdp = input->context->rdp;
458
0
  WINPR_ASSERT(rdp);
459
460
0
  if (!input_ensure_client_running(input))
461
0
    return FALSE;
462
463
0
  if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_UnicodeInput))
464
0
  {
465
0
    WLog_WARN(TAG, "Unicode input not supported by server.");
466
0
    return FALSE;
467
0
  }
468
469
0
  eventFlags |= (flags & KBD_FLAGS_RELEASE) ? FASTPATH_INPUT_KBDFLAGS_RELEASE : 0;
470
0
  s = fastpath_input_pdu_init(rdp->fastpath, eventFlags, FASTPATH_INPUT_EVENT_UNICODE,
471
0
                              &sec_flags);
472
473
0
  if (!s)
474
0
    return FALSE;
475
476
0
  Stream_Write_UINT16(s, code); /* unicodeCode (2 bytes) */
477
0
  return fastpath_send_input_pdu(rdp->fastpath, s, sec_flags);
478
0
}
479
480
static BOOL input_send_fastpath_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
481
0
{
482
0
  UINT16 sec_flags = 0;
483
0
  wStream* s = nullptr;
484
0
  rdpRdp* rdp = nullptr;
485
486
0
  WINPR_ASSERT(input);
487
0
  WINPR_ASSERT(input->context);
488
0
  WINPR_ASSERT(input->context->settings);
489
490
0
  rdp = input->context->rdp;
491
0
  WINPR_ASSERT(rdp);
492
493
0
  if (!input_ensure_client_running(input))
494
0
    return FALSE;
495
496
0
  if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_HasHorizontalWheel))
497
0
  {
498
0
    if (flags & PTR_FLAGS_HWHEEL)
499
0
    {
500
0
      WLog_WARN(TAG,
501
0
                "skip mouse event %" PRIu16 "x%" PRIu16 " flags=0x%04" PRIX16
502
0
                ", no horizontal mouse wheel supported",
503
0
                x, y, flags);
504
0
      return TRUE;
505
0
    }
506
0
  }
507
508
0
  s = fastpath_input_pdu_init(rdp->fastpath, 0, FASTPATH_INPUT_EVENT_MOUSE, &sec_flags);
509
510
0
  if (!s)
511
0
    return FALSE;
512
513
0
  input_write_mouse_event(s, flags, x, y);
514
0
  return fastpath_send_input_pdu(rdp->fastpath, s, sec_flags);
515
0
}
516
517
static BOOL input_send_fastpath_extended_mouse_event(rdpInput* input, UINT16 flags, UINT16 x,
518
                                                     UINT16 y)
519
0
{
520
0
  UINT16 sec_flags = 0;
521
0
  wStream* s = nullptr;
522
0
  rdpRdp* rdp = nullptr;
523
524
0
  WINPR_ASSERT(input);
525
0
  WINPR_ASSERT(input->context);
526
527
0
  rdp = input->context->rdp;
528
0
  WINPR_ASSERT(rdp);
529
530
0
  if (!input_ensure_client_running(input))
531
0
    return FALSE;
532
533
0
  if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_HasExtendedMouseEvent))
534
0
  {
535
0
    WLog_WARN(TAG,
536
0
              "skip extended mouse event %" PRIu16 "x%" PRIu16 " flags=0x%04" PRIX16
537
0
              ", no extended mouse events supported",
538
0
              x, y, flags);
539
0
    return TRUE;
540
0
  }
541
542
0
  s = fastpath_input_pdu_init(rdp->fastpath, 0, FASTPATH_INPUT_EVENT_MOUSEX, &sec_flags);
543
544
0
  if (!s)
545
0
    return FALSE;
546
547
0
  input_write_extended_mouse_event(s, flags, x, y);
548
0
  return fastpath_send_input_pdu(rdp->fastpath, s, sec_flags);
549
0
}
550
551
static BOOL input_send_fastpath_relmouse_event(rdpInput* input, UINT16 flags, INT16 xDelta,
552
                                               INT16 yDelta)
553
0
{
554
0
  UINT16 sec_flags = 0;
555
0
  wStream* s = nullptr;
556
0
  rdpRdp* rdp = nullptr;
557
558
0
  WINPR_ASSERT(input);
559
0
  WINPR_ASSERT(input->context);
560
0
  WINPR_ASSERT(input->context->settings);
561
562
0
  rdp = input->context->rdp;
563
0
  WINPR_ASSERT(rdp);
564
565
0
  if (!input_ensure_client_running(input))
566
0
    return FALSE;
567
568
0
  if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_HasRelativeMouseEvent))
569
0
  {
570
0
    WLog_ERR(TAG, "Sending relative fastpath mouse event, but no support for that announced");
571
0
    return FALSE;
572
0
  }
573
574
0
  s = fastpath_input_pdu_init(rdp->fastpath, 0, TS_FP_RELPOINTER_EVENT, &sec_flags);
575
576
0
  if (!s)
577
0
    return FALSE;
578
579
0
  Stream_Write_UINT16(s, flags); /* pointerFlags (2 bytes) */
580
0
  Stream_Write_INT16(s, xDelta); /* xDelta (2 bytes) */
581
0
  Stream_Write_INT16(s, yDelta); /* yDelta (2 bytes) */
582
0
  return fastpath_send_input_pdu(rdp->fastpath, s, sec_flags);
583
0
}
584
585
static BOOL input_send_fastpath_qoe_event(rdpInput* input, UINT32 timestampMS)
586
0
{
587
0
  WINPR_ASSERT(input);
588
0
  WINPR_ASSERT(input->context);
589
0
  WINPR_ASSERT(input->context->settings);
590
591
0
  rdpRdp* rdp = input->context->rdp;
592
0
  WINPR_ASSERT(rdp);
593
594
0
  if (!input_ensure_client_running(input))
595
0
    return FALSE;
596
597
0
  if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_HasQoeEvent))
598
0
  {
599
0
    WLog_ERR(TAG, "Sending qoe event, but no support for that announced");
600
0
    return FALSE;
601
0
  }
602
603
0
  UINT16 sec_flags = 0;
604
0
  wStream* s = fastpath_input_pdu_init(rdp->fastpath, 0, TS_FP_QOETIMESTAMP_EVENT, &sec_flags);
605
606
0
  if (!s)
607
0
    return FALSE;
608
609
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
610
0
  {
611
0
    Stream_Free(s, TRUE);
612
0
    return FALSE;
613
0
  }
614
615
0
  Stream_Write_UINT32(s, timestampMS);
616
0
  return fastpath_send_input_pdu(rdp->fastpath, s, sec_flags);
617
0
}
618
619
static BOOL input_send_fastpath_focus_in_event(rdpInput* input, UINT16 toggleStates)
620
0
{
621
0
  UINT16 sec_flags = 0;
622
0
  wStream* s = nullptr;
623
0
  BYTE eventFlags = 0;
624
0
  rdpRdp* rdp = nullptr;
625
626
0
  WINPR_ASSERT(input);
627
0
  WINPR_ASSERT(input->context);
628
629
0
  rdp = input->context->rdp;
630
0
  WINPR_ASSERT(rdp);
631
632
0
  if (!input_ensure_client_running(input))
633
0
    return FALSE;
634
635
0
  s = fastpath_input_pdu_init_header(rdp->fastpath, &sec_flags);
636
637
0
  if (!s)
638
0
    return FALSE;
639
640
  /* send a tab up like mstsc.exe */
641
0
  eventFlags = FASTPATH_INPUT_KBDFLAGS_RELEASE | FASTPATH_INPUT_EVENT_SCANCODE << 5;
642
0
  Stream_Write_UINT8(s, eventFlags); /* Key Release event (1 byte) */
643
0
  Stream_Write_UINT8(s, 0x0f);       /* keyCode (1 byte) */
644
  /* send the toggle key states */
645
0
  eventFlags = (toggleStates & 0x1F) | FASTPATH_INPUT_EVENT_SYNC << 5;
646
0
  Stream_Write_UINT8(s, eventFlags); /* toggle state (1 byte) */
647
  /* send another tab up like mstsc.exe */
648
0
  eventFlags = FASTPATH_INPUT_KBDFLAGS_RELEASE | FASTPATH_INPUT_EVENT_SCANCODE << 5;
649
0
  Stream_Write_UINT8(s, eventFlags); /* Key Release event (1 byte) */
650
0
  Stream_Write_UINT8(s, 0x0f);       /* keyCode (1 byte) */
651
0
  return fastpath_send_multiple_input_pdu(rdp->fastpath, s, 3, sec_flags);
652
0
}
653
654
static BOOL input_send_fastpath_keyboard_pause_event(rdpInput* input)
655
0
{
656
  /* In ancient days, pause-down without control sent E1 1D 45 E1 9D C5,
657
   * and pause-up sent nothing.  However, reverse engineering mstsc shows
658
   * it sending the following sequence:
659
   */
660
0
  UINT16 sec_flags = 0;
661
0
  wStream* s = nullptr;
662
0
  const BYTE keyDownEvent = FASTPATH_INPUT_EVENT_SCANCODE << 5;
663
0
  const BYTE keyUpEvent = (FASTPATH_INPUT_EVENT_SCANCODE << 5) | FASTPATH_INPUT_KBDFLAGS_RELEASE;
664
0
  rdpRdp* rdp = nullptr;
665
666
0
  WINPR_ASSERT(input);
667
0
  WINPR_ASSERT(input->context);
668
669
0
  rdp = input->context->rdp;
670
0
  WINPR_ASSERT(rdp);
671
672
0
  if (!input_ensure_client_running(input))
673
0
    return FALSE;
674
675
0
  s = fastpath_input_pdu_init_header(rdp->fastpath, &sec_flags);
676
677
0
  if (!s)
678
0
    return FALSE;
679
680
  /* Control down (0x1D) */
681
0
  Stream_Write_UINT8(s, keyDownEvent | FASTPATH_INPUT_KBDFLAGS_PREFIX_E1);
682
0
  Stream_Write_UINT8(s, RDP_SCANCODE_CODE(RDP_SCANCODE_LCONTROL));
683
  /* Numlock down (0x45) */
684
0
  Stream_Write_UINT8(s, keyDownEvent);
685
0
  Stream_Write_UINT8(s, RDP_SCANCODE_CODE(RDP_SCANCODE_NUMLOCK));
686
  /* Control up (0x1D) */
687
0
  Stream_Write_UINT8(s, keyUpEvent | FASTPATH_INPUT_KBDFLAGS_PREFIX_E1);
688
0
  Stream_Write_UINT8(s, RDP_SCANCODE_CODE(RDP_SCANCODE_LCONTROL));
689
  /* Numlock down (0x45) */
690
0
  Stream_Write_UINT8(s, keyUpEvent);
691
0
  Stream_Write_UINT8(s, RDP_SCANCODE_CODE(RDP_SCANCODE_NUMLOCK));
692
0
  return fastpath_send_multiple_input_pdu(rdp->fastpath, s, 4, sec_flags);
693
0
}
694
695
static BOOL input_recv_sync_event(rdpInput* input, wStream* s)
696
0
{
697
0
  UINT32 toggleFlags = 0;
698
699
0
  WINPR_ASSERT(input);
700
0
  WINPR_ASSERT(s);
701
702
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
703
0
    return FALSE;
704
705
0
  Stream_Seek(s, 2);                  /* pad2Octets (2 bytes) */
706
0
  Stream_Read_UINT32(s, toggleFlags); /* toggleFlags (4 bytes) */
707
708
  /* only pass input if we are actually connected */
709
0
  if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE))
710
0
    return TRUE;
711
0
  return IFCALLRESULT(TRUE, input->SynchronizeEvent, input, toggleFlags);
712
0
}
713
714
static BOOL input_recv_keyboard_event(rdpInput* input, wStream* s)
715
0
{
716
0
  UINT16 keyboardFlags = 0;
717
0
  UINT16 keyCode = 0;
718
719
0
  WINPR_ASSERT(input);
720
0
  WINPR_ASSERT(s);
721
722
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
723
0
    return FALSE;
724
725
0
  Stream_Read_UINT16(s, keyboardFlags); /* keyboardFlags (2 bytes) */
726
0
  Stream_Read_UINT16(s, keyCode);       /* keyCode (2 bytes) */
727
0
  Stream_Seek(s, 2);                    /* pad2Octets (2 bytes) */
728
729
0
  if (keyboardFlags & KBD_FLAGS_RELEASE)
730
0
    keyboardFlags &= ~KBD_FLAGS_DOWN;
731
732
0
  if (keyCode & 0xFF00)
733
0
    WLog_WARN(TAG,
734
0
              "Problematic [MS-RDPBCGR] 2.2.8.1.1.3.1.1.1 Keyboard Event (TS_KEYBOARD_EVENT) "
735
0
              "keyCode=0x%04" PRIx16
736
0
              ", high byte values should be sent in keyboardFlags field, ignoring.",
737
0
              keyCode);
738
739
  /* only pass input if we are actually connected */
740
0
  if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE))
741
0
    return TRUE;
742
0
  return IFCALLRESULT(TRUE, input->KeyboardEvent, input, keyboardFlags, keyCode & 0xFF);
743
0
}
744
745
static BOOL input_recv_unicode_keyboard_event(rdpInput* input, wStream* s)
746
0
{
747
0
  UINT16 keyboardFlags = 0;
748
0
  UINT16 unicodeCode = 0;
749
750
0
  WINPR_ASSERT(input);
751
0
  WINPR_ASSERT(s);
752
753
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
754
0
    return FALSE;
755
756
0
  Stream_Read_UINT16(s, keyboardFlags); /* keyboardFlags (2 bytes) */
757
0
  Stream_Read_UINT16(s, unicodeCode);   /* unicodeCode (2 bytes) */
758
0
  Stream_Seek(s, 2);                    /* pad2Octets (2 bytes) */
759
760
  /* "fix" keyboardFlags - see comment in input_recv_keyboard_event() */
761
762
0
  if (keyboardFlags & KBD_FLAGS_RELEASE)
763
0
    keyboardFlags &= ~KBD_FLAGS_DOWN;
764
765
  /* only pass input if we are actually connected */
766
0
  if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE))
767
0
    return TRUE;
768
0
  return IFCALLRESULT(TRUE, input->UnicodeKeyboardEvent, input, keyboardFlags, unicodeCode);
769
0
}
770
771
static BOOL input_recv_mouse_event(rdpInput* input, wStream* s)
772
0
{
773
0
  UINT16 pointerFlags = 0;
774
0
  UINT16 xPos = 0;
775
0
  UINT16 yPos = 0;
776
777
0
  WINPR_ASSERT(input);
778
0
  WINPR_ASSERT(s);
779
780
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
781
0
    return FALSE;
782
783
0
  Stream_Read_UINT16(s, pointerFlags); /* pointerFlags (2 bytes) */
784
0
  Stream_Read_UINT16(s, xPos);         /* xPos (2 bytes) */
785
0
  Stream_Read_UINT16(s, yPos);         /* yPos (2 bytes) */
786
787
  /* only pass input if we are actually connected */
788
0
  if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE))
789
0
    return TRUE;
790
0
  return IFCALLRESULT(TRUE, input->MouseEvent, input, pointerFlags, xPos, yPos);
791
0
}
792
793
static BOOL input_recv_relmouse_event(rdpInput* input, wStream* s)
794
0
{
795
0
  UINT16 pointerFlags = 0;
796
0
  INT16 xDelta = 0;
797
0
  INT16 yDelta = 0;
798
799
0
  WINPR_ASSERT(input);
800
0
  WINPR_ASSERT(s);
801
802
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
803
0
    return FALSE;
804
805
0
  Stream_Read_UINT16(s, pointerFlags); /* pointerFlags (2 bytes) */
806
0
  Stream_Read_INT16(s, xDelta);        /* xPos (2 bytes) */
807
0
  Stream_Read_INT16(s, yDelta);        /* yPos (2 bytes) */
808
809
0
  if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_HasRelativeMouseEvent))
810
0
  {
811
0
    WLog_ERR(TAG,
812
0
             "Received relative mouse event(flags=0x%04" PRIx16 ", xPos=%" PRId16
813
0
             ", yPos=%" PRId16 "), but we did not announce support for that",
814
0
             pointerFlags, xDelta, yDelta);
815
0
    return FALSE;
816
0
  }
817
818
  /* only pass input if we are actually connected */
819
0
  if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE))
820
0
    return TRUE;
821
822
0
  return IFCALLRESULT(TRUE, input->RelMouseEvent, input, pointerFlags, xDelta, yDelta);
823
0
}
824
825
static BOOL input_recv_extended_mouse_event(rdpInput* input, wStream* s)
826
0
{
827
0
  UINT16 pointerFlags = 0;
828
0
  UINT16 xPos = 0;
829
0
  UINT16 yPos = 0;
830
831
0
  WINPR_ASSERT(input);
832
0
  WINPR_ASSERT(s);
833
834
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
835
0
    return FALSE;
836
837
0
  Stream_Read_UINT16(s, pointerFlags); /* pointerFlags (2 bytes) */
838
0
  Stream_Read_UINT16(s, xPos);         /* xPos (2 bytes) */
839
0
  Stream_Read_UINT16(s, yPos);         /* yPos (2 bytes) */
840
841
0
  if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_HasExtendedMouseEvent))
842
0
  {
843
0
    WLog_ERR(TAG,
844
0
             "Received extended mouse event(flags=0x%04" PRIx16 ", xPos=%" PRIu16
845
0
             ", yPos=%" PRIu16 "), but we did not announce support for that",
846
0
             pointerFlags, xPos, yPos);
847
0
    return FALSE;
848
0
  }
849
850
  /* only pass input if we are actually connected */
851
0
  if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE))
852
0
    return TRUE;
853
0
  return IFCALLRESULT(TRUE, input->ExtendedMouseEvent, input, pointerFlags, xPos, yPos);
854
0
}
855
856
static BOOL input_recv_event(rdpInput* input, wStream* s)
857
0
{
858
0
  UINT16 messageType = 0;
859
860
0
  WINPR_ASSERT(input);
861
0
  WINPR_ASSERT(s);
862
863
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
864
0
    return FALSE;
865
866
0
  Stream_Seek(s, 4);                  /* eventTime (4 bytes), ignored by the server */
867
0
  Stream_Read_UINT16(s, messageType); /* messageType (2 bytes) */
868
869
0
  switch (messageType)
870
0
  {
871
0
    case INPUT_EVENT_SYNC:
872
0
      if (!input_recv_sync_event(input, s))
873
0
        return FALSE;
874
875
0
      break;
876
877
0
    case INPUT_EVENT_SCANCODE:
878
0
      if (!input_recv_keyboard_event(input, s))
879
0
        return FALSE;
880
881
0
      break;
882
883
0
    case INPUT_EVENT_UNICODE:
884
0
      if (!input_recv_unicode_keyboard_event(input, s))
885
0
        return FALSE;
886
887
0
      break;
888
889
0
    case INPUT_EVENT_MOUSE:
890
0
      if (!input_recv_mouse_event(input, s))
891
0
        return FALSE;
892
893
0
      break;
894
895
0
    case INPUT_EVENT_MOUSEX:
896
0
      if (!input_recv_extended_mouse_event(input, s))
897
0
        return FALSE;
898
899
0
      break;
900
901
0
    case INPUT_EVENT_MOUSEREL:
902
0
      if (!input_recv_relmouse_event(input, s))
903
0
        return FALSE;
904
905
0
      break;
906
907
0
    default:
908
0
      WLog_ERR(TAG, "Unknown messageType %" PRIu16 "", messageType);
909
      /* Each input event uses 6 bytes. */
910
0
      if (!Stream_SafeSeek(s, 6))
911
0
        return FALSE;
912
0
      break;
913
0
  }
914
915
0
  return TRUE;
916
0
}
917
918
BOOL input_recv(rdpInput* input, wStream* s)
919
0
{
920
0
  UINT16 numberEvents = 0;
921
922
0
  WINPR_ASSERT(input);
923
0
  WINPR_ASSERT(s);
924
925
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
926
0
    return FALSE;
927
928
0
  Stream_Read_UINT16(s, numberEvents); /* numberEvents (2 bytes) */
929
0
  Stream_Seek(s, 2);                   /* pad2Octets (2 bytes) */
930
931
  /* Each input event uses 6 exactly bytes. */
932
0
  if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, numberEvents, 6ull))
933
0
    return FALSE;
934
935
0
  for (UINT16 i = 0; i < numberEvents; i++)
936
0
  {
937
0
    if (!input_recv_event(input, s))
938
0
      return FALSE;
939
0
  }
940
941
0
  return TRUE;
942
0
}
943
944
BOOL input_register_client_callbacks(rdpInput* input)
945
0
{
946
0
  rdpSettings* settings = nullptr;
947
948
0
  if (!input->context)
949
0
    return FALSE;
950
951
0
  settings = input->context->settings;
952
953
0
  if (!settings)
954
0
    return FALSE;
955
956
0
  if (freerdp_settings_get_bool(settings, FreeRDP_FastPathInput))
957
0
  {
958
0
    input->SynchronizeEvent = input_send_fastpath_synchronize_event;
959
0
    input->KeyboardEvent = input_send_fastpath_keyboard_event;
960
0
    input->KeyboardPauseEvent = input_send_fastpath_keyboard_pause_event;
961
0
    input->UnicodeKeyboardEvent = input_send_fastpath_unicode_keyboard_event;
962
0
    input->MouseEvent = input_send_fastpath_mouse_event;
963
0
    input->RelMouseEvent = input_send_fastpath_relmouse_event;
964
0
    input->ExtendedMouseEvent = input_send_fastpath_extended_mouse_event;
965
0
    input->FocusInEvent = input_send_fastpath_focus_in_event;
966
0
    input->QoEEvent = input_send_fastpath_qoe_event;
967
0
  }
968
0
  else
969
0
  {
970
0
    input->SynchronizeEvent = input_send_synchronize_event;
971
0
    input->KeyboardEvent = input_send_keyboard_event;
972
0
    input->KeyboardPauseEvent = input_send_keyboard_pause_event;
973
0
    input->UnicodeKeyboardEvent = input_send_unicode_keyboard_event;
974
0
    input->MouseEvent = input_send_mouse_event;
975
0
    input->RelMouseEvent = input_send_relmouse_event;
976
0
    input->ExtendedMouseEvent = input_send_extended_mouse_event;
977
0
    input->FocusInEvent = input_send_focus_in_event;
978
0
  }
979
980
0
  return TRUE;
981
0
}
982
983
/* Save last input timestamp and/or mouse position in prevent-session-lock mode */
984
static BOOL input_update_last_event(rdpInput* input, BOOL mouse, UINT16 x, UINT16 y)
985
0
{
986
0
  rdp_input_internal* in = input_cast(input);
987
988
0
  WINPR_ASSERT(input);
989
0
  WINPR_ASSERT(input->context);
990
991
0
  if (freerdp_settings_get_uint32(input->context->settings, FreeRDP_FakeMouseMotionInterval) > 0)
992
0
  {
993
0
    const time_t now = time(nullptr);
994
0
    in->lastInputTimestamp = WINPR_ASSERTING_INT_CAST(UINT64, now);
995
996
0
    if (mouse)
997
0
    {
998
0
      in->lastX = x;
999
0
      in->lastY = y;
1000
0
    }
1001
0
  }
1002
0
  return TRUE;
1003
0
}
1004
1005
WINPR_ATTR_NODISCARD
1006
static const char* boolstr(bool value)
1007
0
{
1008
0
  if (value)
1009
0
    return "true";
1010
0
  return "false";
1011
0
}
1012
1013
WINPR_ATTR_NODISCARD
1014
WINPR_ATTR_FORMAT_ARG(2, 3)
1015
static BOOL input_skip(const rdpInput* input, WINPR_FORMAT_ARG const char* fmt, ...)
1016
0
{
1017
0
  WINPR_ASSERT(input);
1018
1019
0
  const rdpContext* context = input->context;
1020
0
  WINPR_ASSERT(context);
1021
1022
0
  const rdp_input_internal* in = (const rdp_input_internal*)input;
1023
0
  WINPR_ASSERT(in);
1024
1025
0
  const BOOL suspended = freerdp_settings_get_bool(context->settings, FreeRDP_SuspendInput);
1026
0
  const BOOL connected = freerdp_is_active_state(context);
1027
0
  const DWORD loglevel = WLOG_DEBUG;
1028
1029
0
  if (WLog_IsLevelActive(in->log, loglevel))
1030
0
  {
1031
0
    char buffer[128] = WINPR_C_ARRAY_INIT;
1032
1033
0
    va_list args = WINPR_C_ARRAY_INIT;
1034
0
    va_start(args, fmt);
1035
0
    (void)vsnprintf(buffer, sizeof(buffer), fmt, args);
1036
0
    va_end(args);
1037
1038
0
    WLog_Print(in->log, loglevel, "[connected=%s, suspended=%s] %s", boolstr(connected),
1039
0
               boolstr(suspended), buffer);
1040
0
  }
1041
1042
0
  return suspended || !connected;
1043
0
}
1044
1045
BOOL freerdp_input_send_synchronize_event(rdpInput* input, UINT32 flags)
1046
0
{
1047
0
  if (!input || !input->context)
1048
0
    return FALSE;
1049
1050
0
  char buffer[128] = WINPR_C_ARRAY_INIT;
1051
0
  if (input_skip(input, "Keyboard {Sync} [%s]",
1052
0
                 freerdp_input_keyboard_flags_string(flags, buffer, sizeof(buffer))))
1053
0
    return TRUE;
1054
1055
0
  return IFCALLRESULT(TRUE, input->SynchronizeEvent, input, flags);
1056
0
}
1057
1058
BOOL freerdp_input_send_keyboard_event(rdpInput* input, UINT16 flags, UINT8 code)
1059
0
{
1060
0
  if (!input || !input->context)
1061
0
    return FALSE;
1062
1063
0
  char buffer[128] = WINPR_C_ARRAY_INIT;
1064
0
  if (input_skip(input, "Keyboard {Event} [%s]",
1065
0
                 freerdp_input_keypress_flags_string(flags, buffer, sizeof(buffer))))
1066
0
    return TRUE;
1067
1068
0
  input_update_last_event(input, FALSE, 0, 0);
1069
1070
0
  return IFCALLRESULT(TRUE, input->KeyboardEvent, input, flags, code);
1071
0
}
1072
1073
BOOL freerdp_input_send_keyboard_event_ex(rdpInput* input, BOOL down, BOOL repeat,
1074
                                          UINT32 rdp_scancode)
1075
0
{
1076
0
  UINT16 flags = (RDP_SCANCODE_EXTENDED(rdp_scancode) ? KBD_FLAGS_EXTENDED : 0);
1077
0
  if (down && repeat)
1078
0
    flags |= KBD_FLAGS_DOWN;
1079
0
  else if (!down)
1080
0
    flags |= KBD_FLAGS_RELEASE;
1081
1082
0
  return freerdp_input_send_keyboard_event(input, flags, RDP_SCANCODE_CODE(rdp_scancode));
1083
0
}
1084
1085
BOOL freerdp_input_send_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
1086
0
{
1087
0
  if (!input || !input->context)
1088
0
    return FALSE;
1089
1090
0
  char buffer[128] = WINPR_C_ARRAY_INIT;
1091
0
  if (input_skip(input, "Keyboard {Unicode} [%s]",
1092
0
                 freerdp_input_keypress_flags_string(flags, buffer, ARRAYSIZE(buffer))))
1093
0
    return TRUE;
1094
1095
0
  input_update_last_event(input, FALSE, 0, 0);
1096
1097
0
  return IFCALLRESULT(TRUE, input->UnicodeKeyboardEvent, input, flags, code);
1098
0
}
1099
1100
BOOL freerdp_input_send_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
1101
0
{
1102
0
  if (!input || !input->context)
1103
0
    return FALSE;
1104
1105
0
  char buffer[128] = WINPR_C_ARRAY_INIT;
1106
0
  if (input_skip(input, "Mouse {Event} [%s]",
1107
0
                 freerdp_input_mouse_flags_string(flags, false, buffer, ARRAYSIZE(buffer))))
1108
0
    return TRUE;
1109
1110
0
  input_update_last_event(
1111
0
      input, flags & (PTR_FLAGS_MOVE | PTR_FLAGS_BUTTON1 | PTR_FLAGS_BUTTON2 | PTR_FLAGS_BUTTON3),
1112
0
      x, y);
1113
1114
0
  return IFCALLRESULT(TRUE, input->MouseEvent, input, flags, x, y);
1115
0
}
1116
1117
BOOL freerdp_input_send_rel_mouse_event(rdpInput* input, UINT16 flags, INT16 xDelta, INT16 yDelta)
1118
0
{
1119
0
  if (!input || !input->context)
1120
0
    return FALSE;
1121
1122
0
  char buffer[128] = WINPR_C_ARRAY_INIT;
1123
0
  if (input_skip(input, "Mouse {RelativeEvent} [%s]",
1124
0
                 freerdp_input_mouse_flags_string(flags, true, buffer, ARRAYSIZE(buffer))))
1125
0
    return TRUE;
1126
1127
0
  return IFCALLRESULT(TRUE, input->RelMouseEvent, input, flags, xDelta, yDelta);
1128
0
}
1129
1130
BOOL freerdp_input_send_qoe_timestamp(rdpInput* input, UINT32 timestampMS)
1131
0
{
1132
0
  if (!input || !input->context)
1133
0
    return FALSE;
1134
1135
0
  if (input_skip(input, "QOE timestamp %" PRIu32, timestampMS))
1136
0
    return TRUE;
1137
1138
0
  return IFCALLRESULT(TRUE, input->QoEEvent, input, timestampMS);
1139
0
}
1140
1141
BOOL freerdp_input_send_extended_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
1142
0
{
1143
0
  if (!input || !input->context)
1144
0
    return FALSE;
1145
1146
0
  char buffer[128] = WINPR_C_ARRAY_INIT;
1147
0
  if (input_skip(input, "Mouse {ExtendedEvent} %s",
1148
0
                 freerdp_input_mouse_flags_string(flags, true, buffer, ARRAYSIZE(buffer))))
1149
0
    return TRUE;
1150
1151
0
  input_update_last_event(input, TRUE, x, y);
1152
1153
0
  return IFCALLRESULT(TRUE, input->ExtendedMouseEvent, input, flags, x, y);
1154
0
}
1155
1156
BOOL freerdp_input_send_focus_in_event(rdpInput* input, UINT16 toggleStates)
1157
0
{
1158
0
  if (!input || !input->context)
1159
0
    return FALSE;
1160
1161
0
  char buffer[128] = WINPR_C_ARRAY_INIT;
1162
0
  if (input_skip(input, "Keyboard {FocusIn} [%s]",
1163
0
                 freerdp_input_keyboard_flags_string(toggleStates, buffer, sizeof(buffer))))
1164
0
    return TRUE;
1165
1166
0
  return IFCALLRESULT(TRUE, input->FocusInEvent, input, toggleStates);
1167
0
}
1168
1169
BOOL freerdp_input_send_keyboard_pause_event(rdpInput* input)
1170
0
{
1171
0
  if (!input || !input->context)
1172
0
    return FALSE;
1173
1174
0
  if (input_skip(input, "Keyboard {Pause}"))
1175
0
    return TRUE;
1176
1177
0
  return IFCALLRESULT(TRUE, input->KeyboardPauseEvent, input);
1178
0
}
1179
1180
int input_process_events(rdpInput* input)
1181
0
{
1182
0
  if (!input)
1183
0
    return FALSE;
1184
1185
0
  return input_message_queue_process_pending_messages(input);
1186
0
}
1187
1188
static void input_free_queued_message(void* obj)
1189
0
{
1190
0
  wMessage* msg = (wMessage*)obj;
1191
0
  input_message_queue_free_message(msg);
1192
0
}
1193
1194
rdpInput* input_new(rdpRdp* rdp)
1195
24.7k
{
1196
24.7k
  const wObject cb = { nullptr, nullptr, nullptr, input_free_queued_message, nullptr };
1197
24.7k
  rdp_input_internal* input = (rdp_input_internal*)calloc(1, sizeof(rdp_input_internal));
1198
1199
24.7k
  WINPR_UNUSED(rdp);
1200
1201
24.7k
  if (!input)
1202
0
    return nullptr;
1203
1204
24.7k
  input->common.context = rdp->context;
1205
24.7k
  input->queue = MessageQueue_New(&cb);
1206
24.7k
  input->log = WLog_Get(TAG);
1207
1208
24.7k
  if (!input->queue)
1209
0
  {
1210
0
    free(input);
1211
0
    return nullptr;
1212
0
  }
1213
1214
24.7k
  return &input->common;
1215
24.7k
}
1216
1217
void input_free(rdpInput* input)
1218
24.7k
{
1219
24.7k
  if (input != nullptr)
1220
24.7k
  {
1221
24.7k
    rdp_input_internal* in = input_cast(input);
1222
1223
24.7k
    MessageQueue_Free(in->queue);
1224
24.7k
    free(in);
1225
24.7k
  }
1226
24.7k
}
1227
1228
static const char* flag2str(uint32_t flag, bool extended)
1229
0
{
1230
0
#define EVCASE(x) \
1231
0
  case x:       \
1232
0
    return #x
1233
0
  if (extended)
1234
0
  {
1235
0
    switch (flag)
1236
0
    {
1237
      /* Extended Pointer Flags */
1238
0
      EVCASE(PTR_XFLAGS_DOWN);
1239
0
      EVCASE(PTR_XFLAGS_BUTTON1);
1240
0
      EVCASE(PTR_XFLAGS_BUTTON2);
1241
0
      default:
1242
0
        return "PTR_XFLAGS_UNKNOWN";
1243
0
    }
1244
0
  }
1245
1246
0
  switch (flag)
1247
0
  {
1248
    /* Pointer Flags */
1249
0
    EVCASE(PTR_FLAGS_HWHEEL);
1250
0
    EVCASE(PTR_FLAGS_WHEEL);
1251
0
    EVCASE(PTR_FLAGS_WHEEL_NEGATIVE);
1252
0
    EVCASE(PTR_FLAGS_MOVE);
1253
0
    EVCASE(PTR_FLAGS_DOWN);
1254
0
    EVCASE(PTR_FLAGS_BUTTON1);
1255
0
    EVCASE(PTR_FLAGS_BUTTON2);
1256
0
    EVCASE(PTR_FLAGS_BUTTON3);
1257
0
    default:
1258
0
      return "PTR_FLAGS_UNKNOWN";
1259
0
  }
1260
0
#undef EVCASE
1261
0
}
1262
1263
const char* freerdp_input_mouse_flags_string(uint32_t flags, bool extended, char* buffer,
1264
                                             size_t len)
1265
0
{
1266
0
  if (len <= 2)
1267
0
    return nullptr;
1268
1269
0
  if (!winpr_str_append("{", buffer, len, ""))
1270
0
    return nullptr;
1271
1272
  /* Strip initial symbol so we do not get duplicate separators */
1273
0
  for (size_t x = 0; x < 32; x++)
1274
0
  {
1275
0
    const uint32_t flag = 1u << x;
1276
0
    if (flags & flag)
1277
0
    {
1278
0
      char ibuffer[64] = WINPR_C_ARRAY_INIT;
1279
0
      (void)_snprintf(ibuffer, sizeof(ibuffer), "%s", flag2str(flag, extended));
1280
0
      if (!winpr_str_append(ibuffer, &buffer[1], len - 2, "|"))
1281
0
        return nullptr;
1282
0
    }
1283
0
  }
1284
0
  if (!winpr_str_append("}", &buffer[1], len - 2, ""))
1285
0
    return nullptr;
1286
1287
0
  return buffer;
1288
0
}
1289
1290
static const char* kbdflag2str(uint32_t flag)
1291
0
{
1292
0
#define EVCASE(x) \
1293
0
  case x:       \
1294
0
    return #x
1295
1296
0
  switch (flag)
1297
0
  {
1298
    /* Pointer Flags */
1299
0
    EVCASE(KBD_FLAGS_EXTENDED);
1300
0
    EVCASE(KBD_FLAGS_EXTENDED1);
1301
0
    EVCASE(KBD_FLAGS_DOWN);
1302
0
    EVCASE(KBD_FLAGS_RELEASE);
1303
0
    default:
1304
0
      return "KBD_FLAGS_UNKNOWN";
1305
0
  }
1306
0
#undef EVCASE
1307
0
}
1308
1309
const char* freerdp_input_keypress_flags_string(uint32_t flags, char* buffer, size_t len)
1310
0
{
1311
0
  if (len <= 2)
1312
0
    return nullptr;
1313
1314
0
  if (!winpr_str_append("{", buffer, len, ""))
1315
0
    return nullptr;
1316
1317
  /* Strip initial symbol so we do not get duplicate separators */
1318
0
  for (size_t x = 0; x < 32; x++)
1319
0
  {
1320
0
    const uint32_t flag = 1u << x;
1321
0
    if (flags & flag)
1322
0
    {
1323
0
      char ibuffer[64] = WINPR_C_ARRAY_INIT;
1324
0
      (void)_snprintf(ibuffer, sizeof(ibuffer), "%s", kbdflag2str(flag));
1325
0
      if (!winpr_str_append(ibuffer, &buffer[1], len - 2, "|"))
1326
0
        return nullptr;
1327
0
    }
1328
0
  }
1329
0
  if (!winpr_str_append("}", &buffer[1], len - 2, ""))
1330
0
    return nullptr;
1331
1332
0
  return buffer;
1333
0
}