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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-02-19 07:04:28 +04:00
|
|
|
#include <freerdp/types.h>
|
|
|
|
#include <freerdp/utils/file.h>
|
|
|
|
#include <freerdp/utils/memory.h>
|
|
|
|
#include <freerdp/locale/keyboard.h>
|
|
|
|
|
|
|
|
#include "liblocale.h"
|
|
|
|
|
2012-02-19 07:54:44 +04:00
|
|
|
#ifdef WITH_X11
|
|
|
|
#include "keyboard_x11.h"
|
|
|
|
#endif
|
|
|
|
|
2012-02-19 07:04:28 +04:00
|
|
|
#ifdef WITH_XKB
|
|
|
|
#include "keyboard_xkb.h"
|
2011-10-24 22:15:19 +04:00
|
|
|
#endif
|
|
|
|
|
2012-02-19 07:54:44 +04:00
|
|
|
#ifdef WITH_SUN
|
|
|
|
#include "keyboard_sun.h"
|
|
|
|
#endif
|
|
|
|
|
2012-02-19 08:08:17 +04:00
|
|
|
#include <freerdp/locale/locale.h>
|
|
|
|
#include <freerdp/locale/keyboard.h>
|
2011-06-30 23:55:03 +04:00
|
|
|
|
2012-02-20 05:24:06 +04:00
|
|
|
uint32 RDP_SCANCODE_TO_X11_KEYCODE[256][2];
|
|
|
|
RDP_SCANCODE X11_KEYCODE_TO_RDP_SCANCODE[256];
|
2011-06-30 23:55:03 +04:00
|
|
|
|
2012-02-20 05:24:06 +04:00
|
|
|
extern const VIRTUAL_KEY_CODE VIRTUAL_KEY_CODE_TABLE[256];
|
2012-02-22 05:56:34 +04:00
|
|
|
extern const uint32 VIRTUAL_KEY_CODE_TO_RDP_SCANCODE_TABLE[256];
|
|
|
|
extern const RDP_SCANCODE VIRTUAL_KEY_CODE_TO_DEFAULT_RDP_SCANCODE_TABLE[256];
|
2011-08-15 22:33:04 +04:00
|
|
|
|
2012-02-20 07:06:55 +04:00
|
|
|
int freerdp_keyboard_load_map(uint32 keycode_to_vkcode[256], char* name)
|
2011-06-30 23:55:03 +04:00
|
|
|
{
|
2012-02-19 07:54:44 +04:00
|
|
|
FILE* fp;
|
2011-06-30 23:55:03 +04:00
|
|
|
char* pch;
|
2012-02-19 07:04:28 +04:00
|
|
|
char* beg;
|
|
|
|
char* end;
|
2011-06-30 23:55:03 +04:00
|
|
|
int i = 0;
|
2012-02-22 06:28:52 +04:00
|
|
|
uint32 vkcode;
|
|
|
|
uint32 scancode;
|
2012-02-20 05:24:06 +04:00
|
|
|
int kbd_found = 0;
|
2012-02-19 07:54:44 +04:00
|
|
|
char* keymap_path;
|
|
|
|
uint32 keycode = 0;
|
|
|
|
char buffer[1024] = "";
|
|
|
|
char keymap_name[256] = "";
|
|
|
|
char keymap_include[256] = "";
|
|
|
|
char keymap_filename[256] = "";
|
2012-02-20 05:24:06 +04:00
|
|
|
char keycode_string[32] = "";
|
|
|
|
char vkcode_name[128] = "";
|
2012-02-22 02:10:10 +04:00
|
|
|
boolean found = false;
|
|
|
|
boolean extended = false;
|
2011-06-30 23:55:03 +04:00
|
|
|
|
2012-02-19 07:54:44 +04:00
|
|
|
beg = name;
|
2011-06-30 23:55:03 +04:00
|
|
|
|
|
|
|
/* Extract file name and keymap name */
|
2012-02-19 07:54:44 +04:00
|
|
|
if ((end = strrchr(name, '(')) != NULL)
|
2011-06-30 23:55:03 +04:00
|
|
|
{
|
2012-02-19 07:54:44 +04:00
|
|
|
strncpy(keymap_filename, &name[beg - name], end - beg);
|
2011-06-30 23:55:03 +04:00
|
|
|
|
|
|
|
beg = end + 1;
|
2012-02-19 07:54:44 +04:00
|
|
|
if ((end = strrchr(name, ')')) != NULL)
|
2011-06-30 23:55:03 +04:00
|
|
|
{
|
2012-02-19 07:54:44 +04:00
|
|
|
strncpy(keymap_name, &name[beg - name], end - beg);
|
|
|
|
keymap_name[end - beg] = '\0';
|
2011-06-30 23:55:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The keyboard name is the same as the file name */
|
2012-02-19 07:54:44 +04:00
|
|
|
strcpy(keymap_filename, name);
|
|
|
|
strcpy(keymap_name, name);
|
2011-06-30 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2012-02-19 07:54:44 +04:00
|
|
|
keymap_path = freerdp_construct_path(FREERDP_KEYMAP_PATH, keymap_filename);
|
2011-06-30 23:55:03 +04:00
|
|
|
|
2012-02-19 07:54:44 +04:00
|
|
|
DEBUG_KBD("Loading keymap %s, first trying %s", name, keymap_path);
|
2011-06-30 23:55:03 +04:00
|
|
|
|
2012-02-19 07:54:44 +04:00
|
|
|
if ((fp = fopen(keymap_path, "r")) == NULL)
|
2012-02-19 07:04:28 +04:00
|
|
|
{
|
|
|
|
return 0;
|
2011-06-30 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2012-02-19 07:04:28 +04:00
|
|
|
while (fgets(buffer, sizeof(buffer), fp) != NULL)
|
2011-06-30 23:55:03 +04:00
|
|
|
{
|
2012-01-09 05:32:50 +04:00
|
|
|
if (buffer[0] == '#')
|
2011-06-30 23:55:03 +04:00
|
|
|
{
|
|
|
|
continue; /* Skip comments */
|
|
|
|
}
|
|
|
|
|
2012-02-20 05:24:06 +04:00
|
|
|
if (kbd_found)
|
2011-06-30 23:55:03 +04:00
|
|
|
{
|
|
|
|
/* Closing curly bracket and semicolon */
|
2011-12-13 06:29:18 +04:00
|
|
|
if ((pch = strstr(buffer, "};")) != NULL)
|
2011-06-30 23:55:03 +04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-12-13 06:29:18 +04:00
|
|
|
else if ((pch = strstr(buffer, "VK_")) != NULL)
|
2011-06-30 23:55:03 +04:00
|
|
|
{
|
|
|
|
/* The end is delimited by the first white space */
|
|
|
|
end = strcspn(pch, " \t\n\0") + pch;
|
|
|
|
|
|
|
|
/* We copy the virtual key code name in a string */
|
|
|
|
beg = pch;
|
2012-02-20 05:24:06 +04:00
|
|
|
strncpy(vkcode_name, beg, end - beg);
|
|
|
|
vkcode_name[end - beg] = '\0';
|
2011-06-30 23:55:03 +04:00
|
|
|
|
|
|
|
/* Now we want to extract the virtual key code itself which is in between '<' and '>' */
|
2012-01-09 05:32:50 +04:00
|
|
|
if ((beg = strchr(pch + 3, '<')) == NULL)
|
2011-06-30 23:55:03 +04:00
|
|
|
break;
|
|
|
|
else
|
|
|
|
beg++;
|
|
|
|
|
2012-01-09 05:32:50 +04:00
|
|
|
if ((end = strchr(beg, '>')) == NULL)
|
2011-06-30 23:55:03 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* We copy the string representing the number in a string */
|
2012-02-20 05:24:06 +04:00
|
|
|
strncpy(keycode_string, beg, end - beg);
|
|
|
|
keycode_string[end - beg] = '\0';
|
2011-06-30 23:55:03 +04:00
|
|
|
|
|
|
|
/* Convert the string representing the code to an integer */
|
2012-02-20 05:24:06 +04:00
|
|
|
keycode = atoi(keycode_string);
|
2011-06-30 23:55:03 +04:00
|
|
|
|
|
|
|
/* Make sure it is a valid keycode */
|
2012-01-09 05:32:50 +04:00
|
|
|
if (keycode < 0 || keycode > 255)
|
2011-06-30 23:55:03 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Load this key mapping in the keyboard mapping */
|
2012-02-20 05:24:06 +04:00
|
|
|
|
|
|
|
i = 0;
|
2012-02-22 02:10:10 +04:00
|
|
|
found = false;
|
|
|
|
extended = false;
|
2012-02-20 05:24:06 +04:00
|
|
|
|
2012-02-22 06:28:52 +04:00
|
|
|
for (i = 0; i < 256; i++)
|
2011-06-30 23:55:03 +04:00
|
|
|
{
|
2012-02-22 06:28:52 +04:00
|
|
|
if (VIRTUAL_KEY_CODE_TABLE[i].name)
|
2012-02-22 02:10:10 +04:00
|
|
|
{
|
2012-02-22 06:28:52 +04:00
|
|
|
if (strcmp(vkcode_name, VIRTUAL_KEY_CODE_TABLE[i].name) == 0)
|
2012-02-22 02:10:10 +04:00
|
|
|
{
|
|
|
|
found = true;
|
2012-02-22 06:28:52 +04:00
|
|
|
vkcode = VIRTUAL_KEY_CODE_TABLE[i].code;
|
|
|
|
scancode = VIRTUAL_KEY_CODE_TO_RDP_SCANCODE_TABLE[vkcode];
|
|
|
|
extended = VIRTUAL_KEY_CODE_TO_DEFAULT_RDP_SCANCODE_TABLE[vkcode].extended;
|
|
|
|
keycode_to_vkcode[keycode] = vkcode;
|
2012-02-22 02:10:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-30 23:55:03 +04:00
|
|
|
}
|
2011-12-13 06:29:18 +04:00
|
|
|
else if ((pch = strstr(buffer, ": extends")) != NULL)
|
2011-06-30 23:55:03 +04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This map extends another keymap We extract its name
|
|
|
|
* and we recursively load the keymap we need to include.
|
|
|
|
*/
|
|
|
|
|
2011-12-13 06:29:18 +04:00
|
|
|
if ((beg = strchr(pch + sizeof(": extends"), '"')) == NULL)
|
2011-06-30 23:55:03 +04:00
|
|
|
break;
|
|
|
|
beg++;
|
|
|
|
|
2011-12-13 06:29:18 +04:00
|
|
|
if ((end = strchr(beg, '"')) == NULL)
|
2011-06-30 23:55:03 +04:00
|
|
|
break;
|
|
|
|
|
2012-02-19 07:54:44 +04:00
|
|
|
strncpy(keymap_include, beg, end - beg);
|
|
|
|
keymap_include[end - beg] = '\0';
|
2011-06-30 23:55:03 +04:00
|
|
|
|
2012-02-20 05:24:06 +04:00
|
|
|
freerdp_keyboard_load_map(keycode_to_vkcode, keymap_include); /* Load included keymap */
|
2011-06-30 23:55:03 +04:00
|
|
|
}
|
|
|
|
}
|
2011-12-13 06:29:18 +04:00
|
|
|
else if ((pch = strstr(buffer, "keyboard")) != NULL)
|
2011-06-30 23:55:03 +04:00
|
|
|
{
|
|
|
|
/* Keyboard map identifier */
|
2011-12-13 06:29:18 +04:00
|
|
|
if ((beg = strchr(pch + sizeof("keyboard"), '"')) == NULL)
|
2011-06-30 23:55:03 +04:00
|
|
|
break;
|
|
|
|
beg++;
|
|
|
|
|
2012-01-09 05:32:50 +04:00
|
|
|
if ((end = strchr(beg, '"')) == NULL)
|
2011-06-30 23:55:03 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
pch = beg;
|
|
|
|
buffer[end - beg] = '\0';
|
|
|
|
|
|
|
|
/* Does it match our keymap name? */
|
2012-02-19 07:54:44 +04:00
|
|
|
if (strncmp(keymap_name, pch, strlen(keymap_name)) == 0)
|
2012-02-20 05:24:06 +04:00
|
|
|
kbd_found = 1;
|
2011-06-30 23:55:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp); /* Don't forget to close file */
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-20 05:24:06 +04:00
|
|
|
void freerdp_keyboard_load_maps(uint32 keycode_to_vkcode[256], char* names)
|
2011-06-30 23:55:03 +04:00
|
|
|
{
|
|
|
|
char* kbd;
|
2012-02-20 05:24:06 +04:00
|
|
|
char* names_end;
|
|
|
|
int keymap_loaded = 0;
|
2011-06-30 23:55:03 +04:00
|
|
|
|
2012-02-20 05:24:06 +04:00
|
|
|
memset(keycode_to_vkcode, 0, sizeof(keycode_to_vkcode));
|
2011-06-30 23:55:03 +04:00
|
|
|
|
2012-02-20 02:36:39 +04:00
|
|
|
kbd = names;
|
2012-02-20 05:24:06 +04:00
|
|
|
names_end = names + strlen(names);
|
2011-06-30 23:55:03 +04:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2012-02-20 05:24:06 +04:00
|
|
|
/* multiple maps are separated by '+' */
|
|
|
|
int kbd_length = strcspn(kbd + 1, "+") + 1;
|
|
|
|
kbd[kbd_length] = '\0';
|
2011-06-30 23:55:03 +04:00
|
|
|
|
|
|
|
/* Load keyboard map */
|
2012-02-20 05:24:06 +04:00
|
|
|
keymap_loaded += freerdp_keyboard_load_map(keycode_to_vkcode, kbd);
|
2011-06-30 23:55:03 +04:00
|
|
|
|
2012-02-20 05:24:06 +04:00
|
|
|
kbd += kbd_length + 1;
|
2011-06-30 23:55:03 +04:00
|
|
|
}
|
2012-02-20 05:24:06 +04:00
|
|
|
while (kbd < names_end);
|
2011-06-30 23:55:03 +04:00
|
|
|
|
2012-02-20 05:24:06 +04:00
|
|
|
DEBUG_KBD("loaded %d keymaps", keymap_loaded);
|
2012-02-19 07:54:44 +04:00
|
|
|
|
2012-02-20 05:24:06 +04:00
|
|
|
if (keymap_loaded <= 0)
|
2012-02-19 07:54:44 +04:00
|
|
|
printf("error: no keyboard mapping available!\n");
|
2011-06-30 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2012-02-19 21:54:27 +04:00
|
|
|
uint32 freerdp_detect_keyboard(uint32 keyboardLayoutID)
|
|
|
|
{
|
|
|
|
if (keyboardLayoutID != 0)
|
|
|
|
DEBUG_KBD("keyboard layout configuration: %X", keyboardLayoutID);
|
|
|
|
|
|
|
|
if (keyboardLayoutID == 0)
|
|
|
|
{
|
2012-02-20 05:48:04 +04:00
|
|
|
keyboardLayoutID = freerdp_detect_keyboard_layout_from_system_locale();
|
2012-02-19 21:54:27 +04:00
|
|
|
DEBUG_KBD("detect_keyboard_layout_from_locale: %X", keyboardLayoutID);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keyboardLayoutID == 0)
|
|
|
|
{
|
|
|
|
keyboardLayoutID = 0x0409;
|
|
|
|
DEBUG_KBD("using default keyboard layout: %X", keyboardLayoutID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyboardLayoutID;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 freerdp_keyboard_init(uint32 keyboardLayoutId)
|
|
|
|
{
|
|
|
|
#ifdef WITH_XKB
|
|
|
|
keyboardLayoutId = freerdp_keyboard_init_xkb(keyboardLayoutId);
|
|
|
|
|
|
|
|
if (keyboardLayoutId == 0)
|
|
|
|
keyboardLayoutId = freerdp_keyboard_init_x11(keyboardLayoutId);
|
|
|
|
#else
|
|
|
|
|
|
|
|
#ifdef WITH_X11
|
|
|
|
keyboardLayoutId = freerdp_keyboard_init_x11(keyboardLayoutId);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
return keyboardLayoutId;
|
|
|
|
}
|
|
|
|
|
2012-02-20 05:24:06 +04:00
|
|
|
uint32 freerdp_keyboard_get_rdp_scancode_from_x11_keycode(uint32 keycode, boolean* extended)
|
2012-02-19 07:04:28 +04:00
|
|
|
{
|
2012-02-22 02:22:03 +04:00
|
|
|
DEBUG_KBD("x11 keycode: %02X -> rdp code: %02X%s", keycode,
|
|
|
|
X11_KEYCODE_TO_RDP_SCANCODE[keycode].code,
|
|
|
|
X11_KEYCODE_TO_RDP_SCANCODE[keycode].extended ? " extended" : "");
|
2012-02-24 03:42:54 +04:00
|
|
|
|
2012-02-20 05:24:06 +04:00
|
|
|
*extended = X11_KEYCODE_TO_RDP_SCANCODE[keycode].extended;
|
2012-02-24 03:42:54 +04:00
|
|
|
|
2012-02-20 05:24:06 +04:00
|
|
|
return X11_KEYCODE_TO_RDP_SCANCODE[keycode].code;
|
2012-02-19 07:04:28 +04:00
|
|
|
}
|
|
|
|
|
2012-02-20 05:24:06 +04:00
|
|
|
uint32 freerdp_keyboard_get_x11_keycode_from_rdp_scancode(uint32 scancode, boolean extended)
|
2012-02-19 07:04:28 +04:00
|
|
|
{
|
|
|
|
if (extended)
|
2012-02-20 05:24:06 +04:00
|
|
|
return RDP_SCANCODE_TO_X11_KEYCODE[scancode][1];
|
2012-02-19 07:04:28 +04:00
|
|
|
else
|
2012-02-20 05:24:06 +04:00
|
|
|
return RDP_SCANCODE_TO_X11_KEYCODE[scancode][0];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 freerdp_keyboard_get_rdp_scancode_from_virtual_key_code(uint32 vkcode, boolean* extended)
|
|
|
|
{
|
2012-02-22 05:56:34 +04:00
|
|
|
*extended = VIRTUAL_KEY_CODE_TO_DEFAULT_RDP_SCANCODE_TABLE[vkcode].extended;
|
|
|
|
return VIRTUAL_KEY_CODE_TO_RDP_SCANCODE_TABLE[vkcode];
|
2012-02-19 07:04:28 +04:00
|
|
|
}
|
|
|
|
|
2012-02-20 05:24:06 +04:00
|
|
|
char* freerdp_keyboard_get_virtual_key_code_name(uint32 vkcode)
|
2012-02-19 07:04:28 +04:00
|
|
|
{
|
2012-02-20 05:24:06 +04:00
|
|
|
return (char*) VIRTUAL_KEY_CODE_TABLE[vkcode].name;
|
2012-02-19 07:04:28 +04:00
|
|
|
}
|