2011-06-30 23:55:03 +04:00
|
|
|
/**
|
2012-02-20 05:24:06 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Keyboard Localization
|
2011-06-30 23:55:03 +04:00
|
|
|
*
|
2012-02-20 05:24:06 +04:00
|
|
|
* Copyright 2009-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2011-06-30 23:55:03 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-06-30 23:55:03 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-11-22 06:22:06 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2012-02-19 07:04:28 +04:00
|
|
|
#include <freerdp/types.h>
|
|
|
|
#include <freerdp/locale/keyboard.h>
|
2012-03-24 04:57:09 +04:00
|
|
|
#include <freerdp/locale/locale.h>
|
2012-02-19 07:04:28 +04:00
|
|
|
|
|
|
|
#include "liblocale.h"
|
|
|
|
|
2021-01-18 12:24:01 +03:00
|
|
|
#if defined(__MACOSX__)
|
|
|
|
#include "keyboard_apple.h"
|
|
|
|
#endif
|
|
|
|
|
2012-02-19 07:54:44 +04:00
|
|
|
#ifdef WITH_X11
|
2013-03-07 01:35:50 +04:00
|
|
|
|
2012-02-19 07:54:44 +04:00
|
|
|
#include "keyboard_x11.h"
|
|
|
|
|
2012-03-24 04:57:09 +04:00
|
|
|
#ifdef WITH_XKBFILE
|
|
|
|
#include "keyboard_xkbfile.h"
|
2011-10-24 22:15:19 +04:00
|
|
|
#endif
|
|
|
|
|
2012-03-24 04:57:09 +04:00
|
|
|
#endif
|
|
|
|
|
2013-03-07 22:56:00 +04:00
|
|
|
DWORD VIRTUAL_SCANCODE_TO_X11_KEYCODE[256][2];
|
|
|
|
DWORD X11_KEYCODE_TO_VIRTUAL_SCANCODE[256];
|
2011-06-30 23:55:03 +04:00
|
|
|
|
2013-03-07 01:35:50 +04:00
|
|
|
int freerdp_detect_keyboard(DWORD* keyboardLayoutId)
|
2012-02-19 21:54:27 +04:00
|
|
|
{
|
2020-12-11 14:58:51 +03:00
|
|
|
#if defined(_WIN32)
|
2020-12-15 11:59:52 +03:00
|
|
|
CHAR name[KL_NAMELENGTH + 1] = { 0 };
|
|
|
|
if (GetKeyboardLayoutNameA(name))
|
|
|
|
{
|
|
|
|
ULONG rc;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
rc = strtoul(name, NULL, 16);
|
|
|
|
if (errno == 0)
|
|
|
|
*keyboardLayoutId = rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*keyboardLayoutId == 0)
|
|
|
|
*keyboardLayoutId = ((DWORD)GetKeyboardLayout(0) >> 16) & 0x0000FFFF;
|
2020-12-11 14:58:51 +03:00
|
|
|
#endif
|
|
|
|
|
2021-01-18 12:24:01 +03:00
|
|
|
#if defined(__MACOSX__)
|
|
|
|
if (*keyboardLayoutId == 0)
|
|
|
|
freerdp_detect_keyboard_layout_from_cf(keyboardLayoutId);
|
|
|
|
#endif
|
|
|
|
|
2013-03-08 01:04:25 +04:00
|
|
|
#ifdef WITH_X11
|
2013-03-07 01:35:50 +04:00
|
|
|
if (*keyboardLayoutId == 0)
|
|
|
|
freerdp_detect_keyboard_layout_from_xkb(keyboardLayoutId);
|
2013-03-08 01:04:25 +04:00
|
|
|
#endif
|
2013-03-07 01:35:50 +04:00
|
|
|
|
|
|
|
if (*keyboardLayoutId == 0)
|
2013-03-07 02:43:41 +04:00
|
|
|
freerdp_detect_keyboard_layout_from_system_locale(keyboardLayoutId);
|
2013-03-07 01:35:50 +04:00
|
|
|
|
|
|
|
if (*keyboardLayoutId == 0)
|
2013-03-07 02:43:41 +04:00
|
|
|
*keyboardLayoutId = ENGLISH_UNITED_STATES;
|
2013-03-07 01:35:50 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-19 21:54:27 +04:00
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static int freerdp_keyboard_init_apple(DWORD* keyboardLayoutId,
|
|
|
|
DWORD x11_keycode_to_rdp_scancode[256])
|
2013-03-07 01:35:50 +04:00
|
|
|
{
|
|
|
|
DWORD vkcode;
|
|
|
|
DWORD keycode;
|
|
|
|
DWORD keycode_to_vkcode[256];
|
|
|
|
|
|
|
|
ZeroMemory(keycode_to_vkcode, sizeof(keycode_to_vkcode));
|
|
|
|
|
|
|
|
for (keycode = 0; keycode < 256; keycode++)
|
2012-02-19 21:54:27 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
vkcode = keycode_to_vkcode[keycode] =
|
|
|
|
GetVirtualKeyCodeFromKeycode(keycode, KEYCODE_TYPE_APPLE);
|
2013-03-07 01:35:50 +04:00
|
|
|
x11_keycode_to_rdp_scancode[keycode] = GetVirtualScanCodeFromVirtualKeyCode(vkcode, 4);
|
2012-02-19 21:54:27 +04:00
|
|
|
}
|
|
|
|
|
2013-03-07 01:35:50 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static int freerdp_keyboard_init_x11_evdev(DWORD* keyboardLayoutId,
|
|
|
|
DWORD x11_keycode_to_rdp_scancode[256])
|
2013-03-07 01:35:50 +04:00
|
|
|
{
|
|
|
|
DWORD vkcode;
|
|
|
|
DWORD keycode;
|
|
|
|
DWORD keycode_to_vkcode[256];
|
|
|
|
|
|
|
|
ZeroMemory(keycode_to_vkcode, sizeof(keycode_to_vkcode));
|
|
|
|
|
|
|
|
for (keycode = 0; keycode < 256; keycode++)
|
2012-02-19 21:54:27 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
vkcode = keycode_to_vkcode[keycode] =
|
|
|
|
GetVirtualKeyCodeFromKeycode(keycode, KEYCODE_TYPE_EVDEV);
|
2013-03-07 01:35:50 +04:00
|
|
|
x11_keycode_to_rdp_scancode[keycode] = GetVirtualScanCodeFromVirtualKeyCode(vkcode, 4);
|
2012-02-19 21:54:27 +04:00
|
|
|
}
|
|
|
|
|
2013-03-07 01:35:50 +04:00
|
|
|
return 0;
|
2012-02-19 21:54:27 +04:00
|
|
|
}
|
|
|
|
|
2013-03-07 01:35:50 +04:00
|
|
|
DWORD freerdp_keyboard_init(DWORD keyboardLayoutId)
|
2012-02-19 21:54:27 +04:00
|
|
|
{
|
2013-03-07 01:35:50 +04:00
|
|
|
DWORD keycode;
|
2017-07-03 20:45:59 +03:00
|
|
|
#if defined(__APPLE__) || defined(WITH_X11) || defined(WITH_WAYLAND)
|
2013-03-07 01:35:50 +04:00
|
|
|
int status = -1;
|
2015-06-10 19:57:05 +03:00
|
|
|
#endif
|
2013-03-07 01:35:50 +04:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
if (status < 0)
|
2013-03-07 22:56:00 +04:00
|
|
|
status = freerdp_keyboard_init_apple(&keyboardLayoutId, X11_KEYCODE_TO_VIRTUAL_SCANCODE);
|
2013-03-07 01:35:50 +04:00
|
|
|
#endif
|
2012-03-24 04:57:10 +04:00
|
|
|
|
2017-07-03 20:45:59 +03:00
|
|
|
#if defined(WITH_X11) || defined(WITH_WAYLAND)
|
2012-03-24 04:57:09 +04:00
|
|
|
|
2012-03-24 04:57:09 +04:00
|
|
|
#ifdef WITH_XKBFILE
|
2013-03-07 01:35:50 +04:00
|
|
|
if (status < 0)
|
2013-03-07 22:56:00 +04:00
|
|
|
status = freerdp_keyboard_init_xkbfile(&keyboardLayoutId, X11_KEYCODE_TO_VIRTUAL_SCANCODE);
|
2012-02-19 21:54:27 +04:00
|
|
|
#endif
|
|
|
|
|
2013-03-07 01:35:50 +04:00
|
|
|
if (status < 0)
|
2019-11-06 17:24:51 +03:00
|
|
|
status =
|
|
|
|
freerdp_keyboard_init_x11_evdev(&keyboardLayoutId, X11_KEYCODE_TO_VIRTUAL_SCANCODE);
|
2013-03-07 01:35:50 +04:00
|
|
|
|
2012-02-19 21:54:27 +04:00
|
|
|
#endif
|
2013-03-07 01:35:50 +04:00
|
|
|
|
|
|
|
freerdp_detect_keyboard(&keyboardLayoutId);
|
2012-03-24 04:57:10 +04:00
|
|
|
|
2013-03-07 22:56:00 +04:00
|
|
|
ZeroMemory(VIRTUAL_SCANCODE_TO_X11_KEYCODE, sizeof(VIRTUAL_SCANCODE_TO_X11_KEYCODE));
|
2013-03-04 01:30:31 +04:00
|
|
|
|
2013-03-07 22:56:00 +04:00
|
|
|
for (keycode = 0; keycode < ARRAYSIZE(VIRTUAL_SCANCODE_TO_X11_KEYCODE); keycode++)
|
2013-03-04 01:30:31 +04:00
|
|
|
{
|
2013-03-07 22:56:00 +04:00
|
|
|
VIRTUAL_SCANCODE_TO_X11_KEYCODE
|
2021-01-18 12:24:01 +03:00
|
|
|
[RDP_SCANCODE_CODE(X11_KEYCODE_TO_VIRTUAL_SCANCODE[keycode])]
|
2019-11-06 17:24:51 +03:00
|
|
|
[RDP_SCANCODE_EXTENDED(X11_KEYCODE_TO_VIRTUAL_SCANCODE[keycode]) ? 1 : 0] = keycode;
|
2013-03-04 01:30:31 +04:00
|
|
|
}
|
2012-03-24 04:57:10 +04:00
|
|
|
|
2012-02-19 21:54:27 +04:00
|
|
|
return keyboardLayoutId;
|
|
|
|
}
|
|
|
|
|
2013-03-07 01:35:50 +04:00
|
|
|
DWORD freerdp_keyboard_get_rdp_scancode_from_x11_keycode(DWORD keycode)
|
2012-02-19 07:04:28 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
DEBUG_KBD("x11 keycode: %02" PRIX32 " -> rdp code: %02" PRIX8 "%s", keycode,
|
|
|
|
RDP_SCANCODE_CODE(X11_KEYCODE_TO_VIRTUAL_SCANCODE[keycode]),
|
|
|
|
RDP_SCANCODE_EXTENDED(X11_KEYCODE_TO_VIRTUAL_SCANCODE[keycode]) ? " extended" : "");
|
2012-02-24 03:42:54 +04:00
|
|
|
|
2013-03-07 22:56:00 +04:00
|
|
|
return X11_KEYCODE_TO_VIRTUAL_SCANCODE[keycode];
|
2012-02-19 07:04:28 +04:00
|
|
|
}
|
|
|
|
|
2013-03-07 01:35:50 +04:00
|
|
|
DWORD freerdp_keyboard_get_x11_keycode_from_rdp_scancode(DWORD scancode, BOOL extended)
|
2012-02-19 07:04:28 +04:00
|
|
|
{
|
|
|
|
if (extended)
|
2013-03-07 22:56:00 +04:00
|
|
|
return VIRTUAL_SCANCODE_TO_X11_KEYCODE[scancode][1];
|
2012-02-19 07:04:28 +04:00
|
|
|
else
|
2013-03-07 22:56:00 +04:00
|
|
|
return VIRTUAL_SCANCODE_TO_X11_KEYCODE[scancode][0];
|
2012-02-20 05:24:06 +04:00
|
|
|
}
|