libfreerdp-locale: cleanup layout detection
authorMarc-André Moreau <marcandre.moreau@gmail.com>
Wed, 6 Mar 2013 21:35:50 +0000 (16:35 -0500)
committerMarc-André Moreau <marcandre.moreau@gmail.com>
Wed, 6 Mar 2013 21:35:50 +0000 (16:35 -0500)
include/freerdp/locale/keyboard.h
include/freerdp/locale/locale.h
libfreerdp/locale/CMakeLists.txt
libfreerdp/locale/keyboard.c
libfreerdp/locale/keyboard_layout.c
libfreerdp/locale/keyboard_x11.c
libfreerdp/locale/keyboard_x11.h
libfreerdp/locale/keyboard_xkbfile.c
libfreerdp/locale/keyboard_xkbfile.h
libfreerdp/locale/locale.c

index 08b5eab..3e57e0b 100644 (file)
@@ -32,7 +32,7 @@
 
 struct _RDP_KEYBOARD_LAYOUT
 {
-       UINT32 code; /* Keyboard layout code */
+       DWORD code; /* Keyboard layout code */
        char* name; /* Keyboard layout name */
 };
 typedef struct _RDP_KEYBOARD_LAYOUT RDP_KEYBOARD_LAYOUT;
@@ -199,10 +199,10 @@ typedef struct _RDP_KEYBOARD_LAYOUT RDP_KEYBOARD_LAYOUT;
 #define KBD_TYPE_NOKIA_9140                                    0x00000006 /* Nokia 9140 and similar keyboards */
 #define KBD_TYPE_JAPANESE                                      0x00000007 /* Japanese keyboard */
 
-FREERDP_API UINT32 freerdp_keyboard_init(UINT32 keyboardLayoutId);
-FREERDP_API RDP_KEYBOARD_LAYOUT* freerdp_keyboard_get_layouts(UINT32 types);
-FREERDP_API const char* freerdp_keyboard_get_layout_name_from_id(UINT32 keyboardLayoutId);
-FREERDP_API DWORD freerdp_keyboard_get_rdp_scancode_from_x11_keycode(UINT32 keycode);
-FREERDP_API UINT32 freerdp_keyboard_get_x11_keycode_from_rdp_scancode(UINT32 scancode, BOOL extended);
+FREERDP_API DWORD freerdp_keyboard_init(DWORD keyboardLayoutId);
+FREERDP_API RDP_KEYBOARD_LAYOUT* freerdp_keyboard_get_layouts(DWORD types);
+FREERDP_API const char* freerdp_keyboard_get_layout_name_from_id(DWORD keyboardLayoutId);
+FREERDP_API DWORD freerdp_keyboard_get_rdp_scancode_from_x11_keycode(DWORD keycode);
+FREERDP_API DWORD freerdp_keyboard_get_x11_keycode_from_rdp_scancode(DWORD scancode, BOOL extended);
 
 #endif /* FREERDP_LOCALE_KEYBOARD_H */
index 54dfb39..1c00601 100644 (file)
 #define YORUBA                                         0x046A
 #define ZULU                                           0x0435
 
-FREERDP_API UINT32 freerdp_get_system_locale_id(void);
-FREERDP_API UINT32 freerdp_detect_keyboard_layout_from_system_locale(void);
-FREERDP_API const char* freerdp_get_system_locale_name_from_id(UINT32 localeId);
+FREERDP_API DWORD freerdp_get_system_locale_id(void);
+FREERDP_API DWORD freerdp_detect_keyboard_layout_from_system_locale(void);
+FREERDP_API const char* freerdp_get_system_locale_name_from_id(DWORD localeId);
 
 #endif /* FREERDP_LOCALE_H */
index e89dac3..a46a082 100644 (file)
@@ -26,13 +26,11 @@ set(${MODULE_PREFIX}_SRCS
        liblocale.h)
 
 set(${MODULE_PREFIX}_X11_SRCS
+       keyboard_x11.c
+       keyboard_x11.h
        xkb_layout_ids.c
        xkb_layout_ids.h)
 
-set(${MODULE_PREFIX}_X11_KEYMAP_SRCS
-       keyboard_x11.c
-       keyboard_x11.h)
-
 set(${MODULE_PREFIX}_XKBFILE_SRCS
        keyboard_xkbfile.c
        keyboard_xkbfile.h)
index 9bec2a9..294cf7c 100644 (file)
@@ -33,6 +33,7 @@
 #include "liblocale.h"
 
 #ifdef WITH_X11
+
 #include "keyboard_x11.h"
 
 #ifdef WITH_XKBFILE
 
 #endif
 
-UINT32 RDP_SCANCODE_TO_X11_KEYCODE[256][2];
+DWORD RDP_SCANCODE_TO_X11_KEYCODE[256][2];
 DWORD X11_KEYCODE_TO_RDP_SCANCODE[256];
 
-UINT32 freerdp_detect_keyboard(UINT32 keyboardLayoutID)
+int freerdp_detect_keyboard(DWORD* keyboardLayoutId)
 {
-       if (keyboardLayoutID != 0)
-               DEBUG_KBD("keyboard layout configuration: %X", keyboardLayoutID);
+       if (*keyboardLayoutId == 0)
+               freerdp_detect_keyboard_layout_from_xkb(keyboardLayoutId);
+
+       if (*keyboardLayoutId == 0)
+               *keyboardLayoutId = freerdp_detect_keyboard_layout_from_system_locale();
+
+       if (*keyboardLayoutId == 0)
+               *keyboardLayoutId = 0x0409;
+
+       return 0;
+}
 
-       if (keyboardLayoutID == 0)
+int freerdp_keyboard_init_apple(DWORD* keyboardLayoutId, DWORD x11_keycode_to_rdp_scancode[256])
+{
+       DWORD vkcode;
+       DWORD keycode;
+       DWORD keycode_to_vkcode[256];
+
+       ZeroMemory(keycode_to_vkcode, sizeof(keycode_to_vkcode));
+
+       for (keycode = 0; keycode < 256; keycode++)
        {
-               keyboardLayoutID = freerdp_detect_keyboard_layout_from_system_locale();
-               DEBUG_KBD("detect_keyboard_layout_from_locale: %X", keyboardLayoutID);
+               vkcode = keycode_to_vkcode[keycode] = GetVirtualKeyCodeFromKeycode(keycode, KEYCODE_TYPE_APPLE);
+               x11_keycode_to_rdp_scancode[keycode] = GetVirtualScanCodeFromVirtualKeyCode(vkcode, 4);
        }
 
-       if (keyboardLayoutID == 0)
+       return 0;
+}
+
+int freerdp_keyboard_init_x11_evdev(DWORD* keyboardLayoutId, DWORD x11_keycode_to_rdp_scancode[256])
+{
+       DWORD vkcode;
+       DWORD keycode;
+       DWORD keycode_to_vkcode[256];
+
+       ZeroMemory(keycode_to_vkcode, sizeof(keycode_to_vkcode));
+
+       for (keycode = 0; keycode < 256; keycode++)
        {
-               keyboardLayoutID = 0x0409;
-               DEBUG_KBD("using default keyboard layout: %X", keyboardLayoutID);
+               vkcode = keycode_to_vkcode[keycode] = GetVirtualKeyCodeFromKeycode(keycode, KEYCODE_TYPE_EVDEV);
+               x11_keycode_to_rdp_scancode[keycode] = GetVirtualScanCodeFromVirtualKeyCode(vkcode, 4);
        }
 
-       return keyboardLayoutID;
+       return 0;
 }
 
-UINT32 freerdp_keyboard_init(UINT32 keyboardLayoutId)
+DWORD freerdp_keyboard_init(DWORD keyboardLayoutId)
 {
-       UINT32 keycode;
+       DWORD keycode;
+       int status = -1;
+
+#ifdef __APPLE__
+       if (status < 0)
+               status = freerdp_keyboard_init_apple(&keyboardLayoutId, X11_KEYCODE_TO_RDP_SCANCODE);
+#endif
 
 #ifdef WITH_X11
 
 #ifdef WITH_XKBFILE
-       keyboardLayoutId = freerdp_keyboard_init_xkbfile(keyboardLayoutId, X11_KEYCODE_TO_RDP_SCANCODE);
-#else
-       keyboardLayoutId = freerdp_keyboard_init_x11(keyboardLayoutId, X11_KEYCODE_TO_RDP_SCANCODE);
+       if (status < 0)
+               status = freerdp_keyboard_init_xkbfile(&keyboardLayoutId, X11_KEYCODE_TO_RDP_SCANCODE);
 #endif
 
+       if (status < 0)
+               status = freerdp_keyboard_init_x11_evdev(&keyboardLayoutId, X11_KEYCODE_TO_RDP_SCANCODE);
+
 #endif
-       keyboardLayoutId = freerdp_detect_keyboard(keyboardLayoutId);
+
+       freerdp_detect_keyboard(&keyboardLayoutId);
 
        ZeroMemory(RDP_SCANCODE_TO_X11_KEYCODE, sizeof(RDP_SCANCODE_TO_X11_KEYCODE));
 
@@ -91,7 +129,7 @@ UINT32 freerdp_keyboard_init(UINT32 keyboardLayoutId)
        return keyboardLayoutId;
 }
 
-DWORD freerdp_keyboard_get_rdp_scancode_from_x11_keycode(UINT32 keycode)
+DWORD freerdp_keyboard_get_rdp_scancode_from_x11_keycode(DWORD keycode)
 {
        DEBUG_KBD("x11 keycode: %02X -> rdp code: %02X%s", keycode,
                RDP_SCANCODE_CODE(X11_KEYCODE_TO_RDP_SCANCODE[keycode]),
@@ -100,7 +138,7 @@ DWORD freerdp_keyboard_get_rdp_scancode_from_x11_keycode(UINT32 keycode)
        return X11_KEYCODE_TO_RDP_SCANCODE[keycode];
 }
 
-UINT32 freerdp_keyboard_get_x11_keycode_from_rdp_scancode(UINT32 scancode, BOOL extended)
+DWORD freerdp_keyboard_get_x11_keycode_from_rdp_scancode(DWORD scancode, BOOL extended)
 {
        if (extended)
                return RDP_SCANCODE_TO_X11_KEYCODE[scancode][1];
index f84f6fe..bbc5bbd 100644 (file)
@@ -128,8 +128,8 @@ static const RDP_KEYBOARD_LAYOUT RDP_KEYBOARD_LAYOUT_TABLE[] =
 
 struct _RDP_KEYBOARD_LAYOUT_VARIANT
 {
-       UINT32 code; /* Keyboard layout code */
-       UINT32 id; /* Keyboard variant ID */
+       DWORD code; /* Keyboard layout code */
+       DWORD id; /* Keyboard variant ID */
        const char* name; /* Keyboard layout variant name */
 };
 typedef struct _RDP_KEYBOARD_LAYOUT_VARIANT RDP_KEYBOARD_LAYOUT_VARIANT;
@@ -185,7 +185,7 @@ static const RDP_KEYBOARD_LAYOUT_VARIANT RDP_KEYBOARD_LAYOUT_VARIANT_TABLE[] =
 
 struct _RDP_KEYBOARD_IME
 {
-       UINT32 code; /* Keyboard layout code */
+       DWORD code; /* Keyboard layout code */
        const char* file; /* IME file */
        const char* name; /* Keyboard layout name */
 };
@@ -214,7 +214,7 @@ static const RDP_KEYBOARD_IME RDP_KEYBOARD_IME_TABLE[] =
        { KBD_CHINESE_TRADITIONAL_ALPHANUMERIC,                 "romanime.ime", "Chinese (Traditional) - Alphanumeric" }
 };
 
-RDP_KEYBOARD_LAYOUT* freerdp_keyboard_get_layouts(UINT32 types)
+RDP_KEYBOARD_LAYOUT* freerdp_keyboard_get_layouts(DWORD types)
 {
        int num, length, i;
        RDP_KEYBOARD_LAYOUT* layouts;
@@ -256,12 +256,12 @@ RDP_KEYBOARD_LAYOUT* freerdp_keyboard_get_layouts(UINT32 types)
                }
        }
 
-       memset(&layouts[num], 0, sizeof(RDP_KEYBOARD_LAYOUT));
+       ZeroMemory(&layouts[num], sizeof(RDP_KEYBOARD_LAYOUT));
 
        return layouts;
 }
 
-const char* freerdp_keyboard_get_layout_name_from_id(UINT32 keyboardLayoutID)
+const char* freerdp_keyboard_get_layout_name_from_id(DWORD keyboardLayoutID)
 {
        int i;
 
index b789225..0187de9 100644 (file)
@@ -36,7 +36,7 @@
 #include "keyboard_x11.h"
 #include "xkb_layout_ids.h"
 
-UINT32 freerdp_detect_keyboard_layout_from_xkb(char** xkb_layout, char** xkb_variant)
+int freerdp_detect_keyboard_layout_from_xkb(DWORD* keyboardLayoutId)
 {
        char* pch;
        char* beg;
@@ -45,7 +45,6 @@ UINT32 freerdp_detect_keyboard_layout_from_xkb(char** xkb_layout, char** xkb_var
        char buffer[1024];
        char* layout = NULL;
        char* variant = NULL;
-       UINT32 keyboardLayoutId = 0;
 
        /* We start by looking for _XKB_RULES_NAMES_BACKUP which appears to be used by libxklavier */
 
@@ -90,17 +89,14 @@ UINT32 freerdp_detect_keyboard_layout_from_xkb(char** xkb_layout, char** xkb_var
                        variant = beg;
                }
        }
+
        pclose(xprop);
 
        DEBUG_KBD("_XKB_RULES_NAMES_BACKUP layout: %s, variant: %s", layout, variant);
-       keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
+       *keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
 
-       if (keyboardLayoutId > 0)
-       {
-               *xkb_layout = _strdup(layout);
-               *xkb_variant = _strdup(variant);
-               return keyboardLayoutId;
-       }
+       if (*keyboardLayoutId > 0)
+               return 0;
 
        /* Check _XKB_RULES_NAMES if _XKB_RULES_NAMES_BACKUP fails */
 
@@ -140,14 +136,10 @@ UINT32 freerdp_detect_keyboard_layout_from_xkb(char** xkb_layout, char** xkb_var
        pclose(xprop);
 
        DEBUG_KBD("_XKB_RULES_NAMES layout: %s, variant: %s", layout, variant);
-       keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
+       *keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
 
-       if (keyboardLayoutId > 0)
-       {
-               *xkb_layout = _strdup(layout);
-               *xkb_variant = _strdup(variant);
-               return keyboardLayoutId;
-       }
+       if (*keyboardLayoutId > 0)
+               return *keyboardLayoutId;
 
        return 0;
 }
@@ -202,48 +194,3 @@ char* freerdp_detect_keymap_from_xkb()
 
        return keymap;
 }
-
-UINT32 freerdp_keyboard_init_x11(UINT32 keyboardLayoutId, DWORD x11_keycode_to_rdp_scancode[256])
-{
-       DWORD vkcode;
-       DWORD keycode;
-       DWORD keycode_to_vkcode[256];
-
-       ZeroMemory(keycode_to_vkcode, sizeof(keycode_to_vkcode));
-       ZeroMemory(x11_keycode_to_rdp_scancode, sizeof(DWORD) * 256);
-
-#ifdef __APPLE__
-       for (keycode = 0; keycode < 256; keycode++)
-       {
-               keycode_to_vkcode[keycode] = GetVirtualKeyCodeFromKeycode(keycode, KEYCODE_TYPE_APPLE);
-       }
-#else
-       {
-               char* xkb_layout = NULL;
-               char* xkb_variant = NULL;
-
-               if (keyboardLayoutId == 0)
-               {
-                       keyboardLayoutId = freerdp_detect_keyboard_layout_from_xkb(&xkb_layout, &xkb_variant);
-
-                       if (xkb_layout)
-                               free(xkb_layout);
-
-                       if (xkb_variant)
-                               free(xkb_variant);
-               }
-       }
-#endif
-
-       for (keycode = 0; keycode < 256; keycode++)
-       {
-               vkcode = keycode_to_vkcode[keycode];
-               
-               if (vkcode >= 0xFF)
-                       continue;
-               
-               x11_keycode_to_rdp_scancode[keycode] = GetVirtualScanCodeFromVirtualKeyCode(vkcode, 4);
-       }
-       
-       return keyboardLayoutId;
-}
index b9c81d0..7567f6b 100644 (file)
@@ -17,9 +17,9 @@
  * limitations under the License.
  */
 
-#ifndef __LOCALE_KEYBOARD_X11_H
-#define __LOCALE_KEYBOARD_X11_H
+#ifndef FREERDP_LOCALE_KEYBOARD_X11_H
+#define FREERDP_LOCALE_KEYBOARD_X11_H
 
-UINT32 freerdp_keyboard_init_x11(UINT32 keyboardLayoutId, DWORD x11_keycode_to_rdp_scancode[256]);
+int freerdp_detect_keyboard_layout_from_xkb(DWORD* keyboardLayoutId);
 
-#endif /* __LOCALE_KEYBOARD_X11_H */
+#endif /* FREERDP_LOCALE_KEYBOARD_X11_H */
index 1a44d7c..68e4a6f 100644 (file)
@@ -181,7 +181,7 @@ void* freerdp_keyboard_xkb_init()
        return (void*) display;
 }
 
-DWORD freerdp_keyboard_init_xkbfile(DWORD keyboardLayoutId, DWORD x11_keycode_to_rdp_scancode[256])
+int freerdp_keyboard_init_xkbfile(DWORD* keyboardLayoutId, DWORD x11_keycode_to_rdp_scancode[256])
 {
        void* display;
        
@@ -192,12 +192,12 @@ DWORD freerdp_keyboard_init_xkbfile(DWORD keyboardLayoutId, DWORD x11_keycode_to
        if (!display)
        {
                DEBUG_KBD("Error initializing xkb");
-               return 0;
+               return -1;
        }
 
-       if (keyboardLayoutId == 0)
+       if (*keyboardLayoutId == 0)
        {
-               keyboardLayoutId = detect_keyboard_layout_from_xkbfile(display);
+               *keyboardLayoutId = detect_keyboard_layout_from_xkbfile(display);
                DEBUG_KBD("detect_keyboard_layout_from_xkb: %X", keyboardLayoutId);
        }
 
@@ -205,7 +205,7 @@ DWORD freerdp_keyboard_init_xkbfile(DWORD keyboardLayoutId, DWORD x11_keycode_to
 
        XCloseDisplay(display);
 
-       return keyboardLayoutId;
+       return 0;
 }
 
 /* return substring starting after nth comma, ending at following comma */
index 06bdb96..a69209f 100644 (file)
@@ -23,7 +23,8 @@
 #include <freerdp/types.h>
 #include <freerdp/locale/keyboard.h>
 
-DWORD freerdp_keyboard_init_xkbfile(DWORD keyboardLayoutId, DWORD x11_keycode_to_rdp_scancode[256]);
+int freerdp_keyboard_init_xkbfile(DWORD* keyboardLayoutId, DWORD x11_keycode_to_rdp_scancode[256]);
+
 DWORD detect_keyboard_layout_from_xkbfile(void* display);
 int freerdp_keyboard_load_map_from_xkbfile(void* display, DWORD x11_keycode_to_rdp_scancode[256]);
 
index ba60498..6e530cb 100644 (file)
@@ -35,7 +35,7 @@ struct _SYSTEM_LOCALE
 {
        char language[4]; /* Two or three letter language code */
        char country[10]; /* Two or three letter country code (Sometimes with Cyrl_ prefix) */
-       UINT32 code; /* 32-bit unsigned integer corresponding to the locale */
+       DWORD code; /* 32-bit unsigned integer corresponding to the locale */
 };
 typedef struct _SYSTEM_LOCALE SYSTEM_LOCALE;
 
@@ -249,7 +249,7 @@ static const SYSTEM_LOCALE SYSTEM_LOCALE_TABLE[] =
 
 struct _LOCALE_NAME
 {
-       UINT32 localeId;
+       DWORD localeId;
        const char* name;
 };
 typedef struct _LOCALE_NAME LOCALE_NAME;
@@ -459,8 +459,8 @@ static const LOCALE_NAME LOCALE_NAME_TABLE[] =
 
 struct _LOCALE_KEYBOARD_LAYOUTS
 {
-       UINT32 locale; /* Locale ID */
-       UINT32 keyboardLayouts[5]; /* array of associated keyboard layouts */
+       DWORD locale; /* Locale ID */
+       DWORD keyboardLayouts[5]; /* array of associated keyboard layouts */
 
 };
 typedef struct _LOCALE_KEYBOARD_LAYOUTS LOCALE_KEYBOARD_LAYOUTS;
@@ -692,7 +692,7 @@ SYSTEM_LOCALE* freerdp_detect_system_locale()
        return locale;
 }
 
-UINT32 freerdp_get_system_locale_id()
+DWORD freerdp_get_system_locale_id()
 {
        SYSTEM_LOCALE* locale;
 
@@ -704,7 +704,7 @@ UINT32 freerdp_get_system_locale_id()
        return 0;
 }
 
-const char* freerdp_get_system_locale_name_from_id(UINT32 localeId)
+const char* freerdp_get_system_locale_name_from_id(DWORD localeId)
 {
        int index;
 
@@ -717,7 +717,7 @@ const char* freerdp_get_system_locale_name_from_id(UINT32 localeId)
        return NULL;
 }
 
-UINT32 freerdp_detect_keyboard_layout_from_system_locale()
+DWORD freerdp_detect_keyboard_layout_from_system_locale()
 {
        int i, j;
        char language[4];