[locale,xkb] cleaned up variant detection

This commit is contained in:
Armin Novak 2023-07-03 09:57:56 +02:00 committed by akallabeth
parent 8822ad53c1
commit 382ef13b1d
3 changed files with 31 additions and 24 deletions

View File

@ -359,7 +359,7 @@ int freerdp_keyboard_init_xkbfile(DWORD* keyboardLayoutId, DWORD x11_keycode_to_
}
/* return substring starting after nth comma, ending at following comma */
static char* comma_substring(char* s, int n)
static char* comma_substring(char* s, size_t n)
{
char* p;
@ -382,8 +382,6 @@ static char* comma_substring(char* s, int n)
int detect_keyboard_layout_from_xkbfile(void* display, DWORD* keyboardLayoutId)
{
char* layout = NULL;
char* variant = NULL;
DWORD group = 0;
XkbStateRec state = { 0 };
XKeyboardState coreKbdState;
@ -391,7 +389,8 @@ int detect_keyboard_layout_from_xkbfile(void* display, DWORD* keyboardLayoutId)
DEBUG_KBD("display: %p", display);
if (display && XkbRF_GetNamesProp(display, NULL, &rules_names))
char* rules = NULL;
if (display && XkbRF_GetNamesProp(display, &rules, &rules_names))
{
DEBUG_KBD("layouts: %s", rules_names.layout ? rules_names.layout : "");
DEBUG_KBD("variants: %s", rules_names.variant ? rules_names.variant : "");
@ -403,8 +402,8 @@ int detect_keyboard_layout_from_xkbfile(void* display, DWORD* keyboardLayoutId)
DEBUG_KBD("group: %u", state.group);
layout = comma_substring(rules_names.layout, group);
variant = comma_substring(rules_names.variant, group);
const char* layout = comma_substring(rules_names.layout, group);
const char* variant = comma_substring(rules_names.variant, group);
DEBUG_KBD("layout: %s", layout ? layout : "");
DEBUG_KBD("variant: %s", variant ? variant : "");
@ -416,6 +415,7 @@ int detect_keyboard_layout_from_xkbfile(void* display, DWORD* keyboardLayoutId)
free(rules_names.variant);
free(rules_names.options);
}
free(rules);
return 0;
}

View File

@ -826,31 +826,38 @@ static const XKB_LAYOUT xkbLayouts[] = {
{ "tm", KBD_TURKISH_Q, tm_variants }, /* Turkmenistan */
};
UINT32 find_keyboard_layout_in_xorg_rules(char* layout, char* variant)
static UINT32 find_keyboard_layout_variant(const XKB_LAYOUT* layout, const char* variant)
{
size_t i, j;
WINPR_ASSERT(layout);
WINPR_ASSERT(variant);
const XKB_VARIANT* variants = layout->variants;
if (variants)
{
const XKB_VARIANT* var = variants;
while (var->variant && (strlen(var->variant) != 0))
{
if (strcmp(var->variant, variant) == 0)
return var->keyboardLayoutID;
var++;
}
}
return layout->keyboardLayoutID;
}
UINT32 find_keyboard_layout_in_xorg_rules(const char* layout, const char* variant)
{
if ((layout == NULL) || (variant == NULL))
return 0;
DEBUG_KBD("xkbLayout: %s\txkbVariant: %s", layout, variant);
for (i = 0; i < ARRAYSIZE(xkbLayouts); i++)
for (size_t i = 0; i < ARRAYSIZE(xkbLayouts); i++)
{
if (strcmp(xkbLayouts[i].layout, layout) == 0)
{
const XKB_VARIANT* variants = xkbLayouts[i].variants;
if (variants)
{
for (j = 0; variants[j].variant != NULL && strlen(variants[j].variant) > 0; j++)
{
if (strcmp(variants[j].variant, variant) == 0)
return variants[j].keyboardLayoutID;
}
}
return xkbLayouts[i].keyboardLayoutID;
}
const XKB_LAYOUT* cur = &xkbLayouts[i];
if (strcmp(cur->layout, layout) == 0)
return find_keyboard_layout_variant(cur, variant);
}
return 0;

View File

@ -23,6 +23,6 @@
#include <freerdp/types.h>
#include <freerdp/api.h>
FREERDP_LOCAL UINT32 find_keyboard_layout_in_xorg_rules(char* layout, char* variant);
FREERDP_LOCAL UINT32 find_keyboard_layout_in_xorg_rules(const char* layout, const char* variant);
#endif /* FREERDP_LIB_LOCALE_XKB_LAYOUT_IDS_H */