21#include <freerdp/config.h>
24#include <winpr/assert.h>
26#include <freerdp/input.h>
27#include <freerdp/log.h>
33#define TAG FREERDP_TAG("core")
36#define INPUT_EVENT_SYNC 0x0000
37#define INPUT_EVENT_SCANCODE 0x0004
38#define INPUT_EVENT_UNICODE 0x0005
39#define INPUT_EVENT_MOUSE 0x8001
40#define INPUT_EVENT_MOUSEX 0x8002
41#define INPUT_EVENT_MOUSEREL 0x8004
43static const char* SyncEventFlag2Str(uint32_t flag)
45 if (flag == KBD_SYNC_SCROLL_LOCK)
46 return "SYNC_SCROLL_LOCK";
47 if (flag == KBD_SYNC_NUM_LOCK)
48 return "SYNC_NUM_LOCK";
49 if (flag == KBD_SYNC_CAPS_LOCK)
50 return "SYNC_CAPS_LOCK";
51 if (flag == KBD_SYNC_KANA_LOCK)
52 return "SYNC_KANA_LOCK";
53 return "SYNC_UNKNOWN";
56static const char* SyncEventFlags2Str(
const char* prefix, uint32_t flags,
char* buffer,
size_t len)
58 const uint32_t tflags[] = { KBD_SYNC_SCROLL_LOCK, KBD_SYNC_NUM_LOCK, KBD_SYNC_CAPS_LOCK,
64 if (!winpr_str_append(
"{", buffer, len,
""))
68 for (
size_t x = 0; x < ARRAYSIZE(tflags); x++)
70 const uint32_t flag = tflags[x];
73 char ibuffer[64] = WINPR_C_ARRAY_INIT;
74 (void)_snprintf(ibuffer,
sizeof(ibuffer),
"%s%s", prefix, SyncEventFlag2Str(flag));
75 if (!winpr_str_append(ibuffer, &buffer[1], len - 2,
"|"))
79 if (!winpr_str_append(
"}", &buffer[1], len - 2,
""))
85const char* freerdp_input_keyboard_flags_string(uint32_t flags,
char* buffer,
size_t len)
87 return SyncEventFlags2Str(
"KBD_", flags, buffer, len);
90static void rdp_write_client_input_pdu_header(
wStream* s, UINT16 number)
93 WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 4);
94 Stream_Write_UINT16(s, number);
95 Stream_Write_UINT16(s, 0);
98static void rdp_write_input_event_header(
wStream* s, UINT32 time, UINT16 type)
101 WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 6);
102 Stream_Write_UINT32(s, time);
103 Stream_Write_UINT16(s, type);
106static wStream* rdp_client_input_pdu_init(rdpRdp* rdp, UINT16 type, UINT16* sec_flags)
108 wStream* s = rdp_data_pdu_init(rdp, sec_flags);
113 rdp_write_client_input_pdu_header(s, 1);
114 rdp_write_input_event_header(s, 0, type);
118static BOOL rdp_send_client_input_pdu(rdpRdp* rdp,
wStream* s, UINT16 sec_flags)
121 WINPR_ASSERT(rdp->mcs);
122 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_INPUT, rdp->mcs->userId, sec_flags);
125static void input_write_synchronize_event(
wStream* s, UINT32 flags)
128 WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 6);
129 Stream_Write_UINT16(s, 0);
130 Stream_Write_UINT32(s, flags);
133static BOOL input_ensure_client_running(rdpInput* input)
136 if (freerdp_shall_disconnect_context(input->context))
138 WLog_WARN(TAG,
"[APPLICATION BUG] input functions called after the session terminated");
144static BOOL input_send_synchronize_event(rdpInput* input, UINT32 flags)
146 UINT16 sec_flags = 0;
148 if (!input || !input->context)
151 rdpRdp* rdp = input->context->rdp;
153 if (!input_ensure_client_running(input))
156 wStream* s = rdp_client_input_pdu_init(rdp, INPUT_EVENT_SYNC, &sec_flags);
161 input_write_synchronize_event(s, flags);
162 return rdp_send_client_input_pdu(rdp, s, sec_flags);
165static void input_write_keyboard_event(
wStream* s, UINT16 flags, UINT16 code)
168 WINPR_ASSERT(code <= UINT8_MAX);
170 Stream_Write_UINT16(s, flags);
171 Stream_Write_UINT16(s, code);
172 Stream_Write_UINT16(s, 0);
175static BOOL input_send_keyboard_event(rdpInput* input, UINT16 flags, UINT8 code)
177 UINT16 sec_flags = 0;
179 rdpRdp* rdp =
nullptr;
181 if (!input || !input->context)
184 rdp = input->context->rdp;
186 if (!input_ensure_client_running(input))
189 s = rdp_client_input_pdu_init(rdp, INPUT_EVENT_SCANCODE, &sec_flags);
194 input_write_keyboard_event(s, flags, code);
195 return rdp_send_client_input_pdu(rdp, s, sec_flags);
198static void input_write_unicode_keyboard_event(
wStream* s, UINT16 flags, UINT16 code)
200 Stream_Write_UINT16(s, flags);
201 Stream_Write_UINT16(s, code);
202 Stream_Write_UINT16(s, 0);
205static BOOL input_send_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
207 UINT16 sec_flags = 0;
209 rdpRdp* rdp =
nullptr;
211 if (!input || !input->context)
214 if (!input_ensure_client_running(input))
219 WLog_WARN(TAG,
"Unicode input not supported by server.");
223 rdp = input->context->rdp;
224 s = rdp_client_input_pdu_init(rdp, INPUT_EVENT_UNICODE, &sec_flags);
229 input_write_unicode_keyboard_event(s, flags, code);
230 return rdp_send_client_input_pdu(rdp, s, sec_flags);
233static void input_write_mouse_event(
wStream* s, UINT16 flags, UINT16 x, UINT16 y)
235 Stream_Write_UINT16(s, flags);
236 Stream_Write_UINT16(s, x);
237 Stream_Write_UINT16(s, y);
240static BOOL input_send_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
242 UINT16 sec_flags = 0;
244 if (!input || !input->context || !input->context->settings)
247 rdpRdp* rdp = input->context->rdp;
249 if (!input_ensure_client_running(input))
254 if (flags & PTR_FLAGS_HWHEEL)
257 "skip mouse event %" PRIu16
"x%" PRIu16
" flags=0x%04" PRIX16
258 ", no horizontal mouse wheel supported",
264 wStream* s = rdp_client_input_pdu_init(rdp, INPUT_EVENT_MOUSE, &sec_flags);
269 input_write_mouse_event(s, flags, x, y);
270 return rdp_send_client_input_pdu(rdp, s, sec_flags);
273static BOOL input_send_relmouse_event(rdpInput* input, UINT16 flags, INT16 xDelta, INT16 yDelta)
275 UINT16 sec_flags = 0;
277 rdpRdp* rdp =
nullptr;
279 if (!input || !input->context || !input->context->settings)
282 rdp = input->context->rdp;
284 if (!input_ensure_client_running(input))
289 WLog_ERR(TAG,
"Sending relative mouse event, but no support for that");
293 s = rdp_client_input_pdu_init(rdp, INPUT_EVENT_MOUSEREL, &sec_flags);
298 Stream_Write_UINT16(s, flags);
299 Stream_Write_INT16(s, xDelta);
300 Stream_Write_INT16(s, yDelta);
302 return rdp_send_client_input_pdu(rdp, s, sec_flags);
305static void input_write_extended_mouse_event(
wStream* s, UINT16 flags, UINT16 x, UINT16 y)
307 Stream_Write_UINT16(s, flags);
308 Stream_Write_UINT16(s, x);
309 Stream_Write_UINT16(s, y);
312static BOOL input_send_extended_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
314 UINT16 sec_flags = 0;
317 WINPR_ASSERT(input->context);
318 WINPR_ASSERT(input->context->settings);
320 rdpRdp* rdp = input->context->rdp;
323 if (!input_ensure_client_running(input))
329 "skip extended mouse event %" PRIu16
"x%" PRIu16
" flags=0x%04" PRIX16
330 ", no extended mouse events supported",
335 wStream* s = rdp_client_input_pdu_init(rdp, INPUT_EVENT_MOUSEX, &sec_flags);
340 input_write_extended_mouse_event(s, flags, x, y);
341 return rdp_send_client_input_pdu(rdp, s, sec_flags);
344static BOOL input_send_focus_in_event(rdpInput* input, UINT16 toggleStates)
347 if (!input_send_keyboard_event(input, KBD_FLAGS_RELEASE, 0x0f))
351 if (!input_send_synchronize_event(input, (toggleStates & 0x1F)))
355 return input_send_keyboard_event(input, KBD_FLAGS_RELEASE, 0x0f);
358static BOOL input_send_keyboard_pause_event(rdpInput* input)
366 if (!input_send_keyboard_event(input, KBD_FLAGS_EXTENDED1,
367 RDP_SCANCODE_CODE(RDP_SCANCODE_LCONTROL)))
371 if (!input_send_keyboard_event(input, 0, RDP_SCANCODE_CODE(RDP_SCANCODE_NUMLOCK)))
375 if (!input_send_keyboard_event(input, KBD_FLAGS_RELEASE | KBD_FLAGS_EXTENDED1,
376 RDP_SCANCODE_CODE(RDP_SCANCODE_LCONTROL)))
380 return input_send_keyboard_event(input, KBD_FLAGS_RELEASE,
381 RDP_SCANCODE_CODE(RDP_SCANCODE_NUMLOCK));
384static BOOL input_send_fastpath_synchronize_event(rdpInput* input, UINT32 flags)
387 WINPR_ASSERT(input->context);
393 KBD_SYNC_SCROLL_LOCK | KBD_SYNC_NUM_LOCK | KBD_SYNC_CAPS_LOCK | KBD_SYNC_KANA_LOCK;
394 if ((flags & ~mask) != 0)
397 "Problematic [MS-RDPBCGR] 2.2.8.1.2.2.5 Fast-Path Synchronize Event "
398 "(TS_FP_SYNC_EVENT) flags=0x%08" PRIx32
", ignoring undefined bits.",
402 rdpRdp* rdp = input->context->rdp;
405 if (!input_ensure_client_running(input))
409 UINT16 sec_flags = 0;
411 fastpath_input_pdu_init(rdp->fastpath, (BYTE)flags, FASTPATH_INPUT_EVENT_SYNC, &sec_flags);
416 return fastpath_send_input_pdu(rdp->fastpath, s, sec_flags);
419static BOOL input_send_fastpath_keyboard_event(rdpInput* input, UINT16 flags, UINT8 code)
421 UINT16 sec_flags = 0;
424 rdpRdp* rdp =
nullptr;
427 WINPR_ASSERT(input->context);
429 rdp = input->context->rdp;
432 if (!input_ensure_client_running(input))
435 eventFlags |= (flags & KBD_FLAGS_RELEASE) ? FASTPATH_INPUT_KBDFLAGS_RELEASE : 0;
436 eventFlags |= (flags & KBD_FLAGS_EXTENDED) ? FASTPATH_INPUT_KBDFLAGS_EXTENDED : 0;
437 eventFlags |= (flags & KBD_FLAGS_EXTENDED1) ? FASTPATH_INPUT_KBDFLAGS_PREFIX_E1 : 0;
438 s = fastpath_input_pdu_init(rdp->fastpath, eventFlags, FASTPATH_INPUT_EVENT_SCANCODE,
444 WINPR_ASSERT(code <= UINT8_MAX);
445 Stream_Write_UINT8(s, code);
446 return fastpath_send_input_pdu(rdp->fastpath, s, sec_flags);
449static BOOL input_send_fastpath_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
451 UINT16 sec_flags = 0;
454 rdpRdp* rdp =
nullptr;
457 WINPR_ASSERT(input->context);
458 WINPR_ASSERT(input->context->settings);
460 rdp = input->context->rdp;
463 if (!input_ensure_client_running(input))
468 WLog_WARN(TAG,
"Unicode input not supported by server.");
472 eventFlags |= (flags & KBD_FLAGS_RELEASE) ? FASTPATH_INPUT_KBDFLAGS_RELEASE : 0;
473 s = fastpath_input_pdu_init(rdp->fastpath, eventFlags, FASTPATH_INPUT_EVENT_UNICODE,
479 Stream_Write_UINT16(s, code);
480 return fastpath_send_input_pdu(rdp->fastpath, s, sec_flags);
483static BOOL input_send_fastpath_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
485 UINT16 sec_flags = 0;
487 rdpRdp* rdp =
nullptr;
490 WINPR_ASSERT(input->context);
491 WINPR_ASSERT(input->context->settings);
493 rdp = input->context->rdp;
496 if (!input_ensure_client_running(input))
501 if (flags & PTR_FLAGS_HWHEEL)
504 "skip mouse event %" PRIu16
"x%" PRIu16
" flags=0x%04" PRIX16
505 ", no horizontal mouse wheel supported",
511 s = fastpath_input_pdu_init(rdp->fastpath, 0, FASTPATH_INPUT_EVENT_MOUSE, &sec_flags);
516 input_write_mouse_event(s, flags, x, y);
517 return fastpath_send_input_pdu(rdp->fastpath, s, sec_flags);
520static BOOL input_send_fastpath_extended_mouse_event(rdpInput* input, UINT16 flags, UINT16 x,
523 UINT16 sec_flags = 0;
525 rdpRdp* rdp =
nullptr;
528 WINPR_ASSERT(input->context);
530 rdp = input->context->rdp;
533 if (!input_ensure_client_running(input))
539 "skip extended mouse event %" PRIu16
"x%" PRIu16
" flags=0x%04" PRIX16
540 ", no extended mouse events supported",
545 s = fastpath_input_pdu_init(rdp->fastpath, 0, FASTPATH_INPUT_EVENT_MOUSEX, &sec_flags);
550 input_write_extended_mouse_event(s, flags, x, y);
551 return fastpath_send_input_pdu(rdp->fastpath, s, sec_flags);
554static BOOL input_send_fastpath_relmouse_event(rdpInput* input, UINT16 flags, INT16 xDelta,
557 UINT16 sec_flags = 0;
559 rdpRdp* rdp =
nullptr;
562 WINPR_ASSERT(input->context);
563 WINPR_ASSERT(input->context->settings);
565 rdp = input->context->rdp;
568 if (!input_ensure_client_running(input))
573 WLog_ERR(TAG,
"Sending relative fastpath mouse event, but no support for that announced");
577 s = fastpath_input_pdu_init(rdp->fastpath, 0, TS_FP_RELPOINTER_EVENT, &sec_flags);
582 Stream_Write_UINT16(s, flags);
583 Stream_Write_INT16(s, xDelta);
584 Stream_Write_INT16(s, yDelta);
585 return fastpath_send_input_pdu(rdp->fastpath, s, sec_flags);
588static BOOL input_send_fastpath_qoe_event(rdpInput* input, UINT32 timestampMS)
591 WINPR_ASSERT(input->context);
592 WINPR_ASSERT(input->context->settings);
594 rdpRdp* rdp = input->context->rdp;
597 if (!input_ensure_client_running(input))
602 WLog_ERR(TAG,
"Sending qoe event, but no support for that announced");
606 UINT16 sec_flags = 0;
607 wStream* s = fastpath_input_pdu_init(rdp->fastpath, 0, TS_FP_QOETIMESTAMP_EVENT, &sec_flags);
612 if (!Stream_EnsureRemainingCapacity(s, 4))
614 Stream_Free(s, TRUE);
618 Stream_Write_UINT32(s, timestampMS);
619 return fastpath_send_input_pdu(rdp->fastpath, s, sec_flags);
622static BOOL input_send_fastpath_focus_in_event(rdpInput* input, UINT16 toggleStates)
624 UINT16 sec_flags = 0;
627 rdpRdp* rdp =
nullptr;
630 WINPR_ASSERT(input->context);
632 rdp = input->context->rdp;
635 if (!input_ensure_client_running(input))
638 s = fastpath_input_pdu_init_header(rdp->fastpath, &sec_flags);
644 eventFlags = FASTPATH_INPUT_KBDFLAGS_RELEASE | FASTPATH_INPUT_EVENT_SCANCODE << 5;
645 Stream_Write_UINT8(s, eventFlags);
646 Stream_Write_UINT8(s, 0x0f);
648 eventFlags = (toggleStates & 0x1F) | FASTPATH_INPUT_EVENT_SYNC << 5;
649 Stream_Write_UINT8(s, eventFlags);
651 eventFlags = FASTPATH_INPUT_KBDFLAGS_RELEASE | FASTPATH_INPUT_EVENT_SCANCODE << 5;
652 Stream_Write_UINT8(s, eventFlags);
653 Stream_Write_UINT8(s, 0x0f);
654 return fastpath_send_multiple_input_pdu(rdp->fastpath, s, 3, sec_flags);
657static BOOL input_send_fastpath_keyboard_pause_event(rdpInput* input)
663 UINT16 sec_flags = 0;
665 const BYTE keyDownEvent = FASTPATH_INPUT_EVENT_SCANCODE << 5;
666 const BYTE keyUpEvent = (FASTPATH_INPUT_EVENT_SCANCODE << 5) | FASTPATH_INPUT_KBDFLAGS_RELEASE;
667 rdpRdp* rdp =
nullptr;
670 WINPR_ASSERT(input->context);
672 rdp = input->context->rdp;
675 if (!input_ensure_client_running(input))
678 s = fastpath_input_pdu_init_header(rdp->fastpath, &sec_flags);
684 Stream_Write_UINT8(s, keyDownEvent | FASTPATH_INPUT_KBDFLAGS_PREFIX_E1);
685 Stream_Write_UINT8(s, RDP_SCANCODE_CODE(RDP_SCANCODE_LCONTROL));
687 Stream_Write_UINT8(s, keyDownEvent);
688 Stream_Write_UINT8(s, RDP_SCANCODE_CODE(RDP_SCANCODE_NUMLOCK));
690 Stream_Write_UINT8(s, keyUpEvent | FASTPATH_INPUT_KBDFLAGS_PREFIX_E1);
691 Stream_Write_UINT8(s, RDP_SCANCODE_CODE(RDP_SCANCODE_LCONTROL));
693 Stream_Write_UINT8(s, keyUpEvent);
694 Stream_Write_UINT8(s, RDP_SCANCODE_CODE(RDP_SCANCODE_NUMLOCK));
695 return fastpath_send_multiple_input_pdu(rdp->fastpath, s, 4, sec_flags);
698static BOOL input_recv_sync_event(rdpInput* input,
wStream* s)
700 UINT32 toggleFlags = 0;
705 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
709 Stream_Read_UINT32(s, toggleFlags);
712 if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE))
714 return IFCALLRESULT(TRUE, input->SynchronizeEvent, input, toggleFlags);
717static BOOL input_recv_keyboard_event(rdpInput* input,
wStream* s)
719 UINT16 keyboardFlags = 0;
725 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
728 Stream_Read_UINT16(s, keyboardFlags);
729 Stream_Read_UINT16(s, keyCode);
732 if (keyboardFlags & KBD_FLAGS_RELEASE)
733 keyboardFlags &= ~KBD_FLAGS_DOWN;
735 if (keyCode & 0xFF00)
737 "Problematic [MS-RDPBCGR] 2.2.8.1.1.3.1.1.1 Keyboard Event (TS_KEYBOARD_EVENT) "
738 "keyCode=0x%04" PRIx16
739 ", high byte values should be sent in keyboardFlags field, ignoring.",
743 if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE))
745 return IFCALLRESULT(TRUE, input->KeyboardEvent, input, keyboardFlags, keyCode & 0xFF);
748static BOOL input_recv_unicode_keyboard_event(rdpInput* input,
wStream* s)
750 UINT16 keyboardFlags = 0;
751 UINT16 unicodeCode = 0;
756 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
759 Stream_Read_UINT16(s, keyboardFlags);
760 Stream_Read_UINT16(s, unicodeCode);
765 if (keyboardFlags & KBD_FLAGS_RELEASE)
766 keyboardFlags &= ~KBD_FLAGS_DOWN;
769 if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE))
771 return IFCALLRESULT(TRUE, input->UnicodeKeyboardEvent, input, keyboardFlags, unicodeCode);
774static BOOL input_recv_mouse_event(rdpInput* input,
wStream* s)
776 UINT16 pointerFlags = 0;
783 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
786 Stream_Read_UINT16(s, pointerFlags);
787 Stream_Read_UINT16(s, xPos);
788 Stream_Read_UINT16(s, yPos);
791 if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE))
793 return IFCALLRESULT(TRUE, input->MouseEvent, input, pointerFlags, xPos, yPos);
796static BOOL input_recv_relmouse_event(rdpInput* input,
wStream* s)
798 UINT16 pointerFlags = 0;
805 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
808 Stream_Read_UINT16(s, pointerFlags);
809 Stream_Read_INT16(s, xDelta);
810 Stream_Read_INT16(s, yDelta);
815 "Received relative mouse event(flags=0x%04" PRIx16
", xPos=%" PRId16
816 ", yPos=%" PRId16
"), but we did not announce support for that",
817 pointerFlags, xDelta, yDelta);
822 if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE))
825 return IFCALLRESULT(TRUE, input->RelMouseEvent, input, pointerFlags, xDelta, yDelta);
828static BOOL input_recv_extended_mouse_event(rdpInput* input,
wStream* s)
830 UINT16 pointerFlags = 0;
837 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
840 Stream_Read_UINT16(s, pointerFlags);
841 Stream_Read_UINT16(s, xPos);
842 Stream_Read_UINT16(s, yPos);
847 "Received extended mouse event(flags=0x%04" PRIx16
", xPos=%" PRIu16
848 ", yPos=%" PRIu16
"), but we did not announce support for that",
849 pointerFlags, xPos, yPos);
854 if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE))
856 return IFCALLRESULT(TRUE, input->ExtendedMouseEvent, input, pointerFlags, xPos, yPos);
859static BOOL input_recv_event(rdpInput* input,
wStream* s)
861 UINT16 messageType = 0;
866 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
870 Stream_Read_UINT16(s, messageType);
874 case INPUT_EVENT_SYNC:
875 if (!input_recv_sync_event(input, s))
880 case INPUT_EVENT_SCANCODE:
881 if (!input_recv_keyboard_event(input, s))
886 case INPUT_EVENT_UNICODE:
887 if (!input_recv_unicode_keyboard_event(input, s))
892 case INPUT_EVENT_MOUSE:
893 if (!input_recv_mouse_event(input, s))
898 case INPUT_EVENT_MOUSEX:
899 if (!input_recv_extended_mouse_event(input, s))
904 case INPUT_EVENT_MOUSEREL:
905 if (!input_recv_relmouse_event(input, s))
911 WLog_ERR(TAG,
"Unknown messageType %" PRIu16
"", messageType);
913 if (!Stream_SafeSeek(s, 6))
921BOOL input_recv(rdpInput* input,
wStream* s)
923 UINT16 numberEvents = 0;
928 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
931 Stream_Read_UINT16(s, numberEvents);
935 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, numberEvents, 6ull))
938 for (UINT16 i = 0; i < numberEvents; i++)
940 if (!input_recv_event(input, s))
947BOOL input_register_client_callbacks(rdpInput* input)
949 rdpSettings* settings =
nullptr;
954 settings = input->context->settings;
961 input->SynchronizeEvent = input_send_fastpath_synchronize_event;
962 input->KeyboardEvent = input_send_fastpath_keyboard_event;
963 input->KeyboardPauseEvent = input_send_fastpath_keyboard_pause_event;
964 input->UnicodeKeyboardEvent = input_send_fastpath_unicode_keyboard_event;
965 input->MouseEvent = input_send_fastpath_mouse_event;
966 input->RelMouseEvent = input_send_fastpath_relmouse_event;
967 input->ExtendedMouseEvent = input_send_fastpath_extended_mouse_event;
968 input->FocusInEvent = input_send_fastpath_focus_in_event;
969 input->QoEEvent = input_send_fastpath_qoe_event;
973 input->SynchronizeEvent = input_send_synchronize_event;
974 input->KeyboardEvent = input_send_keyboard_event;
975 input->KeyboardPauseEvent = input_send_keyboard_pause_event;
976 input->UnicodeKeyboardEvent = input_send_unicode_keyboard_event;
977 input->MouseEvent = input_send_mouse_event;
978 input->RelMouseEvent = input_send_relmouse_event;
979 input->ExtendedMouseEvent = input_send_extended_mouse_event;
980 input->FocusInEvent = input_send_focus_in_event;
987static BOOL input_update_last_event(rdpInput* input, BOOL mouse, UINT16 x, UINT16 y)
992 WINPR_ASSERT(input->context);
996 const time_t now = time(
nullptr);
997 in->lastInputTimestamp = WINPR_ASSERTING_INT_CAST(UINT64, now);
1009static const char* boolstr(
bool value)
1017WINPR_ATTR_FORMAT_ARG(2, 3)
1018static BOOL input_skip(const rdpInput* input, WINPR_FORMAT_ARG const
char* fmt, ...)
1020 WINPR_ASSERT(input);
1022 const rdpContext* context = input->context;
1023 WINPR_ASSERT(context);
1029 const BOOL connected = freerdp_is_active_state(context);
1030 const DWORD loglevel = WLOG_DEBUG;
1032 if (WLog_IsLevelActive(in->log, loglevel))
1034 char buffer[128] = WINPR_C_ARRAY_INIT;
1036 va_list args = WINPR_C_ARRAY_INIT;
1037 va_start(args, fmt);
1038 (void)vsnprintf(buffer,
sizeof(buffer), fmt, args);
1041 WLog_Print(in->log, loglevel,
"[connected=%s, suspended=%s] %s", boolstr(connected),
1042 boolstr(suspended), buffer);
1045 return suspended || !connected;
1048BOOL freerdp_input_send_synchronize_event(rdpInput* input, UINT32 flags)
1050 if (!input || !input->context)
1053 char buffer[128] = WINPR_C_ARRAY_INIT;
1054 if (input_skip(input,
"Keyboard {Sync} [%s]",
1055 freerdp_input_keyboard_flags_string(flags, buffer,
sizeof(buffer))))
1058 return IFCALLRESULT(TRUE, input->SynchronizeEvent, input, flags);
1061BOOL freerdp_input_send_keyboard_event(rdpInput* input, UINT16 flags, UINT8 code)
1063 if (!input || !input->context)
1066 char buffer[128] = WINPR_C_ARRAY_INIT;
1067 if (input_skip(input,
"Keyboard {Event} [%s]",
1068 freerdp_input_keypress_flags_string(flags, buffer,
sizeof(buffer))))
1071 input_update_last_event(input, FALSE, 0, 0);
1073 return IFCALLRESULT(TRUE, input->KeyboardEvent, input, flags, code);
1076BOOL freerdp_input_send_keyboard_event_ex(rdpInput* input, BOOL down, BOOL repeat,
1077 UINT32 rdp_scancode)
1079 UINT16 flags = (RDP_SCANCODE_EXTENDED(rdp_scancode) ? KBD_FLAGS_EXTENDED : 0);
1081 flags |= KBD_FLAGS_DOWN;
1083 flags |= KBD_FLAGS_RELEASE;
1085 return freerdp_input_send_keyboard_event(input, flags, RDP_SCANCODE_CODE(rdp_scancode));
1088BOOL freerdp_input_send_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
1090 if (!input || !input->context)
1093 char buffer[128] = WINPR_C_ARRAY_INIT;
1094 if (input_skip(input,
"Keyboard {Unicode} [%s]",
1095 freerdp_input_keypress_flags_string(flags, buffer, ARRAYSIZE(buffer))))
1098 input_update_last_event(input, FALSE, 0, 0);
1100 return IFCALLRESULT(TRUE, input->UnicodeKeyboardEvent, input, flags, code);
1103BOOL freerdp_input_send_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
1105 if (!input || !input->context)
1108 char buffer[128] = WINPR_C_ARRAY_INIT;
1109 if (input_skip(input,
"Mouse {Event} [%s]",
1110 freerdp_input_mouse_flags_string(flags,
false, buffer, ARRAYSIZE(buffer))))
1113 input_update_last_event(
1114 input, flags & (PTR_FLAGS_MOVE | PTR_FLAGS_BUTTON1 | PTR_FLAGS_BUTTON2 | PTR_FLAGS_BUTTON3),
1117 return IFCALLRESULT(TRUE, input->MouseEvent, input, flags, x, y);
1120BOOL freerdp_input_send_rel_mouse_event(rdpInput* input, UINT16 flags, INT16 xDelta, INT16 yDelta)
1122 if (!input || !input->context)
1125 char buffer[128] = WINPR_C_ARRAY_INIT;
1126 if (input_skip(input,
"Mouse {RelativeEvent} [%s]",
1127 freerdp_input_mouse_flags_string(flags,
true, buffer, ARRAYSIZE(buffer))))
1130 return IFCALLRESULT(TRUE, input->RelMouseEvent, input, flags, xDelta, yDelta);
1133BOOL freerdp_input_send_qoe_timestamp(rdpInput* input, UINT32 timestampMS)
1135 if (!input || !input->context)
1138 if (input_skip(input,
"QOE timestamp %" PRIu32, timestampMS))
1141 return IFCALLRESULT(TRUE, input->QoEEvent, input, timestampMS);
1144BOOL freerdp_input_send_extended_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
1146 if (!input || !input->context)
1149 char buffer[128] = WINPR_C_ARRAY_INIT;
1150 if (input_skip(input,
"Mouse {ExtendedEvent} %s",
1151 freerdp_input_mouse_flags_string(flags,
true, buffer, ARRAYSIZE(buffer))))
1154 input_update_last_event(input, TRUE, x, y);
1156 return IFCALLRESULT(TRUE, input->ExtendedMouseEvent, input, flags, x, y);
1159BOOL freerdp_input_send_focus_in_event(rdpInput* input, UINT16 toggleStates)
1161 if (!input || !input->context)
1164 char buffer[128] = WINPR_C_ARRAY_INIT;
1165 if (input_skip(input,
"Keyboard {FocusIn} [%s]",
1166 freerdp_input_keyboard_flags_string(toggleStates, buffer,
sizeof(buffer))))
1169 return IFCALLRESULT(TRUE, input->FocusInEvent, input, toggleStates);
1172BOOL freerdp_input_send_keyboard_pause_event(rdpInput* input)
1174 if (!input || !input->context)
1177 if (input_skip(input,
"Keyboard {Pause}"))
1180 return IFCALLRESULT(TRUE, input->KeyboardPauseEvent, input);
1183int input_process_events(rdpInput* input)
1188 return input_message_queue_process_pending_messages(input);
1191static void input_free_queued_message(
void* obj)
1193 wMessage* msg = (wMessage*)obj;
1194 input_message_queue_free_message(msg);
1197rdpInput* input_new(rdpRdp* rdp)
1199 const wObject cb = {
nullptr,
nullptr,
nullptr, input_free_queued_message,
nullptr };
1207 input->common.context = rdp->context;
1208 input->queue = MessageQueue_New(&cb);
1209 input->log = WLog_Get(TAG);
1217 return &input->common;
1220void input_free(rdpInput* input)
1222 if (input !=
nullptr)
1226 MessageQueue_Free(in->queue);
1231static const char* flag2str(uint32_t flag,
bool extended)
1241 EVCASE(PTR_XFLAGS_DOWN);
1242 EVCASE(PTR_XFLAGS_BUTTON1);
1243 EVCASE(PTR_XFLAGS_BUTTON2);
1245 return "PTR_XFLAGS_UNKNOWN";
1252 EVCASE(PTR_FLAGS_HWHEEL);
1253 EVCASE(PTR_FLAGS_WHEEL);
1254 EVCASE(PTR_FLAGS_WHEEL_NEGATIVE);
1255 EVCASE(PTR_FLAGS_MOVE);
1256 EVCASE(PTR_FLAGS_DOWN);
1257 EVCASE(PTR_FLAGS_BUTTON1);
1258 EVCASE(PTR_FLAGS_BUTTON2);
1259 EVCASE(PTR_FLAGS_BUTTON3);
1261 return "PTR_FLAGS_UNKNOWN";
1266const char* freerdp_input_mouse_flags_string(uint32_t flags,
bool extended,
char* buffer,
1272 if (!winpr_str_append(
"{", buffer, len,
""))
1276 for (
size_t x = 0; x < 32; x++)
1278 const uint32_t flag = 1u << x;
1281 char ibuffer[64] = WINPR_C_ARRAY_INIT;
1282 (void)_snprintf(ibuffer,
sizeof(ibuffer),
"%s", flag2str(flag, extended));
1283 if (!winpr_str_append(ibuffer, &buffer[1], len - 2,
"|"))
1287 if (!winpr_str_append(
"}", &buffer[1], len - 2,
""))
1293static const char* kbdflag2str(uint32_t flag)
1302 EVCASE(KBD_FLAGS_EXTENDED);
1303 EVCASE(KBD_FLAGS_EXTENDED1);
1304 EVCASE(KBD_FLAGS_DOWN);
1305 EVCASE(KBD_FLAGS_RELEASE);
1307 return "KBD_FLAGS_UNKNOWN";
1312const char* freerdp_input_keypress_flags_string(uint32_t flags,
char* buffer,
size_t len)
1317 if (!winpr_str_append(
"{", buffer, len,
""))
1321 for (
size_t x = 0; x < 32; x++)
1323 const uint32_t flag = 1u << x;
1326 char ibuffer[64] = WINPR_C_ARRAY_INIT;
1327 (void)_snprintf(ibuffer,
sizeof(ibuffer),
"%s", kbdflag2str(flag));
1328 if (!winpr_str_append(ibuffer, &buffer[1], len - 2,
"|"))
1332 if (!winpr_str_append(
"}", &buffer[1], len - 2,
""))
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
This struct contains function pointer to initialize/free objects.