FreeRDP
Loading...
Searching...
No Matches
sdlInput Class Reference

#include <sdl_kbd.hpp>

Public Member Functions

 sdlInput (SdlContext *sdl)
 sdlInput (const sdlInput &other)=delete
 sdlInput (sdlInput &&other)=delete
sdlInput & operator= (const sdlInput &other)=delete
sdlInput & operator= (sdlInput &&other)=delete
BOOL keyboard_sync_state ()
BOOL keyboard_focus_in ()
BOOL keyboard_handle_event (const SDL_KeyboardEvent *ev)
BOOL keyboard_grab (Uint32 windowID, bool enable)
BOOL mouse_focus (Uint32 windowID)
BOOL mouse_grab (Uint32 windowID, SDL_bool enable)
 sdlInput (SdlContext *sdl)
 sdlInput (const sdlInput &other)=delete
 sdlInput (sdlInput &&other)=delete
sdlInput & operator= (const sdlInput &other)=delete
sdlInput & operator= (sdlInput &&other)=delete
bool initialize ()
bool keyboard_sync_state ()
bool keyboard_focus_in ()
bool handleEvent (const SDL_KeyboardEvent &ev)
bool keyboard_grab (Uint32 windowID, bool enable)
bool mouse_focus (Uint32 windowID)
bool mouse_grab (Uint32 windowID, bool enable)
bool prefToEnabled ()
uint32_t prefToMask ()

Static Public Member Functions

static BOOL keyboard_set_indicators (rdpContext *context, UINT16 led_flags)
static BOOL keyboard_set_ime_status (rdpContext *context, UINT16 imeId, UINT32 imeState, UINT32 imeConvMode)
static bool prefToEnabled ()
static uint32_t prefToMask ()
static uint32_t prefKeyValue (const std::string &key, uint32_t fallback=SDL_SCANCODE_UNKNOWN)
static BOOL keyboard_set_indicators (rdpContext *context, UINT16 led_flags)
static BOOL keyboard_set_ime_status (rdpContext *context, UINT16 imeId, UINT32 imeState, UINT32 imeConvMode)
static uint32_t prefKeyValue (const std::string &key, uint32_t fallback=SDL_SCANCODE_UNKNOWN)

Detailed Description

FreeRDP: A Remote Desktop Protocol Implementation SDL Client keyboard helper

Copyright 2022 Armin Novak armin.nosp@m..nov.nosp@m.ak@th.nosp@m.inca.nosp@m.st.co.nosp@m.m

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Definition at line 34 of file sdl_kbd.hpp.

Constructor & Destructor Documentation

◆ sdlInput()

sdlInput::sdlInput ( SdlContext * sdl)
explicit

Definition at line 598 of file sdl_kbd.cpp.

599 : _sdl(sdl), _lastWindowID(UINT32_MAX), _hotkeysEnabled(prefToEnabled()),
600 _hotkeyModmask(prefToMask())
601{
602 auto list =
603 freerdp_settings_get_string(_sdl->context()->settings, FreeRDP_KeyboardRemappingList);
604 _remapTable = freerdp_keyboard_remap_string_to_list(list);
605 assert(_remapTable);
606 _hotkeyFullscreen = prefKeyValue("SDL_Fullscreen", SDL_SCANCODE_RETURN);
607 _hotkeyResizable = prefKeyValue("SDL_Resizeable", SDL_SCANCODE_R);
608 _hotkeyGrab = prefKeyValue("SDL_Grab", SDL_SCANCODE_G);
609 _hotkeyDisconnect = prefKeyValue("SDL_Disconnect", SDL_SCANCODE_D);
610 _hotkeyMinimize = prefKeyValue("SDL_Minimize", SDL_SCANCODE_M);
611}
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.

◆ ~sdlInput()

sdlInput::~sdlInput ( )

Definition at line 613 of file sdl_kbd.cpp.

614{
615 freerdp_keyboard_remap_free(_remapTable);
616}

Member Function Documentation

◆ handleEvent()

bool sdlInput::handleEvent ( const SDL_KeyboardEvent & ev)
nodiscard

Definition at line 581 of file SDL3/sdl_input.cpp.

582{
583 UINT32 rdp_scancode = scancode_to_rdp(ev.scancode);
584#if defined(_WIN32)
585 /* CJK IMEs on Windows may set the extended bit for Right Shift, producing
586 * the invalid scan code E0 36. SDL maps that event to SDL_SCANCODE_UNKNOWN,
587 * so normalize it in the same way as the native Windows client. */
588 if ((rdp_scancode == RDP_SCANCODE_UNKNOWN) && (ev.raw == 0xE036))
589 rdp_scancode = RDP_SCANCODE_RSHIFT;
590#endif
591 /* The modifiers held when this key was pressed: SDL_GetModState() is the state after every
592 * event already queued, so a hotkey could be missed, or fire on a plain key typed just
593 * before a modifier. */
594 const SDL_Keymod mods = ev.mod;
595
596 if (_hotkeysEnabled && (mods & _hotkeyModmask) == _hotkeyModmask)
597 {
598 if (ev.type == SDL_EVENT_KEY_DOWN)
599 {
600 if (ev.scancode == _hotkeyFullscreen)
601 {
602 WLog_Print(_sdl->getWLog(), WLOG_INFO, "%s+<%s> pressed, toggling fullscreen state",
603 masktostr(_hotkeyModmask).c_str(), sdl_scancode_name(_hotkeyFullscreen));
604 if (!keyboard_sync_state())
605 return false;
606 return _sdl->toggleFullscreen();
607 }
608 if (ev.scancode == _hotkeyResizable)
609 {
610 WLog_Print(_sdl->getWLog(), WLOG_INFO, "%s+<%s> pressed, toggling resizeable state",
611 masktostr(_hotkeyModmask).c_str(), sdl_scancode_name(_hotkeyResizable));
612 if (!keyboard_sync_state())
613 return false;
614 return _sdl->toggleResizeable();
615 }
616
617 if (ev.scancode == _hotkeyGrab)
618 {
619 WLog_Print(_sdl->getWLog(), WLOG_INFO, "%s+<%s> pressed, toggling grab state",
620 masktostr(_hotkeyModmask).c_str(), sdl_scancode_name(_hotkeyGrab));
621 if (!keyboard_sync_state())
622 return false;
623 return keyboard_grab(ev.windowID, !_sdl->grabKeyboard());
624 }
625 if (ev.scancode == _hotkeyDisconnect)
626 {
627 WLog_Print(_sdl->getWLog(), WLOG_INFO, "%s+<%s> pressed, disconnecting RDP session",
628 masktostr(_hotkeyModmask).c_str(), sdl_scancode_name(_hotkeyDisconnect));
629 if (!keyboard_sync_state())
630 return false;
631 std::ignore = _sdl->abortConnect();
632 return true;
633 }
634 if (ev.scancode == _hotkeyInfo)
635 {
636 WLog_Print(_sdl->getWLog(), WLOG_INFO, "%s+<%s> pressed, toggling the overlay",
637 masktostr(_hotkeyModmask).c_str(), sdl_scancode_name(_hotkeyInfo));
638 if (!keyboard_sync_state())
639 return false;
640 return _sdl->toggleOverlay();
641 }
642 if (ev.scancode == _hotkeyMinimize)
643 {
644 WLog_Print(_sdl->getWLog(), WLOG_INFO, "%s+<%s> pressed, minimizing client",
645 masktostr(_hotkeyModmask).c_str(), sdl_scancode_name(_hotkeyMinimize));
646 if (!keyboard_sync_state())
647 return false;
648 return _sdl->setMinimized();
649 }
650 }
651 }
652
653#if defined(WITH_DEBUG_KBD)
654 {
655 const BOOL ex = RDP_SCANCODE_EXTENDED(rdp_scancode);
656 const DWORD sc = RDP_SCANCODE_CODE(rdp_scancode);
657 WLog_Print(_sdl->getWLog(), WLOG_DEBUG,
658 "SDL keycode: %02" PRIX32 " -> rdp code: [%04" PRIx16 "] %02" PRIX8 "%s",
659 ev.scancode, rdp_scancode, sc, ex ? " extended" : "");
660 }
661#endif
662
663 auto scancode = freerdp_keyboard_remap_key(_remapTable, rdp_scancode);
664#if defined(WITH_DEBUG_KBD)
665 {
666 const BOOL ex = RDP_SCANCODE_EXTENDED(scancode);
667 const DWORD sc = RDP_SCANCODE_CODE(scancode);
668 WLog_Print(_sdl->getWLog(), WLOG_DEBUG,
669 "SDL keycode: %02" PRIX32 " -> remapped rdp code: [%04" PRIx16 "] %02" PRIX8
670 "%s",
671 ev.scancode, scancode, sc, ex ? " extended" : "");
672 }
673#endif
674 if (freerdp_settings_get_bool(_sdl->context()->settings, FreeRDP_UnicodeInput))
675 {
676 /* With key_event false the keycode follows the layout, also for non-Latin ones, where key
677 * events carry the QWERTY letter (SDL_HINT_KEYCODE_OPTIONS). */
678 const auto key = SDL_GetKeyFromScancode(ev.scancode, mods, false);
679 std::array<char, 5> str = {};
680 if ((key >= 0x20) && (key != SDLK_DELETE) && (key <= 0x10FFFF))
681 std::ignore = SDL_UCS4ToUTF8(key, str.data());
682 return sdl::utils::inputSent(_sdl->context(), _sdl->getWLog(),
683 freerdp_client_send_key_as_unicode(
684 _sdl->common(),
685 (ev.type == SDL_EVENT_KEY_DOWN) ? 0 : KBD_FLAGS_RELEASE,
686 str.data(), scancode, sdl_get_kbd_modmask(mods)),
687 "key event");
688 }
689 return sdl::utils::inputSent(_sdl->context(), _sdl->getWLog(),
690 freerdp_input_send_keyboard_event_ex(_sdl->context()->input,
691 ev.type == SDL_EVENT_KEY_DOWN,
692 ev.repeat, scancode),
693 "key event");
694}
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.

◆ initialize()

bool sdlInput::initialize ( )
nodiscard

Definition at line 759 of file SDL3/sdl_input.cpp.

760{
761 auto settings = _sdl->context()->settings;
762 WINPR_ASSERT(settings);
763 WINPR_ASSERT(!_remapTable);
764
765 {
766 auto list = freerdp_settings_get_string(settings, FreeRDP_KeyboardRemappingList);
767 _remapTable = freerdp_keyboard_remap_string_to_list(list);
768 if (!_remapTable)
769 return false;
770 }
771
772 if (freerdp_settings_get_uint32(settings, FreeRDP_KeyboardLayout) == 0)
773 {
774 DWORD KeyboardLayout = 0;
775
776 freerdp_detect_keyboard_layout_from_system_locale(&KeyboardLayout);
777 if (KeyboardLayout == 0)
778 KeyboardLayout = ENGLISH_UNITED_STATES;
779
780 if (!freerdp_settings_set_uint32(settings, FreeRDP_KeyboardLayout, KeyboardLayout))
781 return false;
782 }
783 return true;
784}
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.

◆ keyboard_focus_in()

bool sdlInput::keyboard_focus_in ( )

Definition at line 323 of file sdl_kbd.cpp.

324{
325 auto input = _sdl->context()->input;
326 WINPR_ASSERT(input);
327
328 auto syncFlags = sdl_get_kbd_flags();
329 freerdp_input_send_focus_in_event(input, WINPR_ASSERTING_INT_CAST(UINT16, syncFlags));
330
331 /* finish with a mouse pointer position like mstsc.exe if required */
332 // TODO: fullscreen/remote app
333 int x = 0;
334 int y = 0;
335 if (_sdl->fullscreen)
336 {
337 SDL_GetGlobalMouseState(&x, &y);
338 }
339 else
340 {
341 SDL_GetMouseState(&x, &y);
342 }
343 auto w = SDL_GetMouseFocus();
344 if (w)
345 {
346 auto id = SDL_GetWindowID(w);
347 sdl_scale_coordinates(_sdl, id, &x, &y, TRUE, TRUE);
348 }
349 return freerdp_client_send_button_event(_sdl->common(), FALSE, PTR_FLAGS_MOVE, x, y);
350}

◆ keyboard_grab()

bool sdlInput::keyboard_grab ( Uint32 windowID,
bool enable )

Definition at line 564 of file sdl_kbd.cpp.

565{
566 auto it = _sdl->windows.find(windowID);
567 if (it == _sdl->windows.end())
568 return FALSE;
569
570 auto status = enable && _sdl->grab_kbd_enabled;
571 _sdl->grab_kbd = status;
572 return it->second.grabKeyboard(status);
573}

◆ keyboard_handle_event()

BOOL sdlInput::keyboard_handle_event ( const SDL_KeyboardEvent * ev)

Definition at line 517 of file sdl_kbd.cpp.

518{
519 WINPR_ASSERT(ev);
520 const UINT32 rdp_scancode = sdl_scancode_to_rdp(ev->keysym.scancode);
521 /* The modifiers held when this key was pressed: SDL_GetModState() is the state after every
522 * event already queued, so a hotkey could be missed, or fire on a plain key typed just
523 * before a modifier. */
524 const auto mods = static_cast<SDL_Keymod>(ev->keysym.mod);
525
526 if (_hotkeysEnabled && (mods & _hotkeyModmask) == _hotkeyModmask)
527 {
528 if (ev->type == SDL_KEYDOWN)
529 {
530 if (ev->keysym.scancode == _hotkeyFullscreen)
531 {
532 _sdl->update_fullscreen(!_sdl->fullscreen);
533 return TRUE;
534 }
535 if (ev->keysym.scancode == _hotkeyResizable)
536 {
537 _sdl->update_resizeable(!_sdl->resizeable);
538 return TRUE;
539 }
540
541 if (ev->keysym.scancode == _hotkeyGrab)
542 {
543 keyboard_grab(ev->windowID, !_sdl->grab_kbd);
544 return TRUE;
545 }
546 if (ev->keysym.scancode == _hotkeyDisconnect)
547 {
548 freerdp_abort_connect_context(_sdl->context());
549 return TRUE;
550 }
551 if (ev->keysym.scancode == _hotkeyMinimize)
552 {
553 _sdl->update_minimize();
554 return TRUE;
555 }
556 }
557 }
558
559 auto scancode = freerdp_keyboard_remap_key(_remapTable, rdp_scancode);
560 return freerdp_input_send_keyboard_event_ex(_sdl->context()->input, ev->type == SDL_KEYDOWN,
561 ev->repeat, scancode);
562}

◆ keyboard_set_ime_status()

BOOL sdlInput::keyboard_set_ime_status ( rdpContext * context,
UINT16 imeId,
UINT32 imeState,
UINT32 imeConvMode )
static

Definition at line 377 of file sdl_kbd.cpp.

379{
380 if (!context)
381 return FALSE;
382
383 WLog_WARN(TAG,
384 "KeyboardSetImeStatus(unitId=%04" PRIx16 ", imeState=%08" PRIx32
385 ", imeConvMode=%08" PRIx32 ") ignored",
386 imeId, imeState, imeConvMode);
387 return TRUE;
388}
#define TAG

◆ keyboard_set_indicators()

BOOL sdlInput::keyboard_set_indicators ( rdpContext * context,
UINT16 led_flags )
static

Definition at line 353 of file sdl_kbd.cpp.

354{
355 WINPR_UNUSED(context);
356
357 int state = KMOD_NONE;
358
359 if ((led_flags & KBD_SYNC_NUM_LOCK) != 0)
360 state |= KMOD_NUM;
361 if ((led_flags & KBD_SYNC_CAPS_LOCK) != 0)
362 state |= KMOD_CAPS;
363#if SDL_VERSION_ATLEAST(2, 0, 18)
364 if ((led_flags & KBD_SYNC_SCROLL_LOCK) != 0)
365 state |= KMOD_SCROLL;
366#endif
367
368 // TODO: KBD_SYNC_KANA_LOCK
369
370 // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
371 SDL_SetModState(static_cast<SDL_Keymod>(state));
372
373 return TRUE;
374}

◆ keyboard_sync_state()

bool sdlInput::keyboard_sync_state ( )

Definition at line 317 of file sdl_kbd.cpp.

318{
319 const auto syncFlags = sdl_get_kbd_flags();
320 return freerdp_input_send_synchronize_event(_sdl->context()->input, syncFlags);
321}

◆ mouse_focus()

bool sdlInput::mouse_focus ( Uint32 windowID)

Definition at line 575 of file sdl_kbd.cpp.

576{
577 if (_lastWindowID != windowID)
578 {
579 _lastWindowID = windowID;
580 auto it = _sdl->windows.find(windowID);
581 if (it == _sdl->windows.end())
582 return FALSE;
583
584 it->second.raise();
585 }
586 return TRUE;
587}

◆ mouse_grab() [1/2]

bool sdlInput::mouse_grab ( Uint32 windowID,
bool enable )
nodiscard

Definition at line 727 of file SDL3/sdl_input.cpp.

728{
729 auto cfg_enabled = freerdp_settings_get_bool(_sdl->context()->settings, FreeRDP_GrabMouse);
730 auto status = enable && cfg_enabled;
731
732 auto window = _sdl->getWindowForId(windowID);
733 if (!window)
734 {
735 WLog_Print(_sdl->getWLog(), WLOG_WARN, "Failed to grab mouse for window %" PRIu32,
736 windowID);
737 return false;
738 }
739 return _sdl->setGrabMouse(status);
740}

◆ mouse_grab() [2/2]

BOOL sdlInput::mouse_grab ( Uint32 windowID,
SDL_bool enable )

Definition at line 589 of file sdl_kbd.cpp.

590{
591 auto it = _sdl->windows.find(windowID);
592 if (it == _sdl->windows.end())
593 return FALSE;
594 _sdl->grab_mouse = enable;
595 return it->second.grabMouse(enable);
596}

◆ prefKeyValue()

uint32_t sdlInput::prefKeyValue ( const std::string & key,
uint32_t fallback = SDL_SCANCODE_UNKNOWN )
static

Definition at line 485 of file sdl_kbd.cpp.

486{
487 auto item = SdlPref::instance()->get_string(key);
488 if (item.empty())
489 return fallback;
490 auto val = sdl_scancode_val(item.c_str());
491 if (val == SDL_SCANCODE_UNKNOWN)
492 return fallback;
493 return val;
494}

◆ prefToEnabled()

bool sdlInput::prefToEnabled ( )
static

Definition at line 418 of file sdl_kbd.cpp.

419{
420 bool enable = true;
421 const auto& m = getSdlMap();
422 for (const auto& val : SdlPref::instance()->get_array("SDL_KeyModMask", { "KMOD_RSHIFT" }))
423 {
424 auto it = m.find(val);
425 if (it != m.end())
426 {
427 if (it->second == KMOD_NONE)
428 enable = false;
429 }
430 else
431 {
432 WLog_WARN(TAG, "Invalid config::SDL_KeyModMask entry value '%s', disabling hotkeys",
433 val.c_str());
434 enable = false;
435 }
436 }
437 return enable;
438}

◆ prefToMask()

uint32_t sdlInput::prefToMask ( )
static

Definition at line 440 of file sdl_kbd.cpp.

441{
442 const auto& mapping = getSdlMap();
443 uint32_t mod = KMOD_NONE;
444 for (const auto& val : SdlPref::instance()->get_array("SDL_KeyModMask", { "KMOD_RSHIFT" }))
445 {
446 auto it = mapping.find(val);
447 if (it != mapping.end())
448 mod |= it->second;
449 }
450 return mod;
451}

The documentation for this class was generated from the following files: