2011-06-30 23:55:03 +04:00
|
|
|
/**
|
2012-02-20 05:24:06 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* X11 Keyboard Mapping
|
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>
|
2023-05-10 12:49:46 +03:00
|
|
|
* Copyright 2023 Bernhard Miklautz <bernhard.miklautz@thincast.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 <string.h>
|
2023-08-02 13:07:03 +03:00
|
|
|
|
|
|
|
#include <X11/X.h>
|
2023-05-10 12:49:46 +03:00
|
|
|
#include <X11/Xatom.h>
|
|
|
|
#include <X11/Xlib.h>
|
2012-10-09 07:42:01 +04:00
|
|
|
|
2012-02-19 07:04:28 +04:00
|
|
|
#include "liblocale.h"
|
|
|
|
#include "keyboard_x11.h"
|
2012-03-24 04:57:10 +04:00
|
|
|
#include "xkb_layout_ids.h"
|
2011-06-30 23:55:03 +04:00
|
|
|
|
2023-05-10 12:49:46 +03:00
|
|
|
static BOOL parse_xkb_rule_names(char* xkb_rule, unsigned long num_bytes, char** layout,
|
|
|
|
char** variant)
|
2012-02-22 06:28:52 +04:00
|
|
|
{
|
|
|
|
/* Sample output for "Canadian Multilingual Standard"
|
|
|
|
*
|
2023-05-10 12:49:46 +03:00
|
|
|
* _XKB_RULES_NAMES_BACKUP(STRING) = "xorg", "pc105", "ca", "multi", "magic"
|
|
|
|
*
|
|
|
|
* Format: "rules", "model", "layout", "variant", "options"
|
|
|
|
*
|
2012-02-22 06:28:52 +04:00
|
|
|
* Where "xorg" is the set of rules
|
2023-05-10 12:49:46 +03:00
|
|
|
* "pc105" the keyboard model
|
|
|
|
* "ca" the keyboard layout(s) (can also be something like 'us,uk')
|
|
|
|
* "multi" the keyboard layout variant(s) (in the examples, “,winkeys” - which means first
|
|
|
|
* layout uses some “default” variant and second uses “winkeys” variant)
|
|
|
|
* "magic" - configuration option (in the examples,
|
|
|
|
* “eurosign:e,lv3:ralt_switch,grp:rctrl_toggle”
|
|
|
|
* - three options)
|
2012-02-22 06:28:52 +04:00
|
|
|
*/
|
2024-01-30 12:25:38 +03:00
|
|
|
for (size_t i = 0, index = 0; i < num_bytes; i++, index++)
|
2012-03-24 05:01:12 +04:00
|
|
|
{
|
2023-05-10 12:49:46 +03:00
|
|
|
char* ptr = xkb_rule + i;
|
2012-02-22 06:28:52 +04:00
|
|
|
|
2023-05-10 12:49:46 +03:00
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case 0: // rules
|
|
|
|
break;
|
|
|
|
case 1: // model
|
|
|
|
break;
|
|
|
|
case 2: // layout
|
2023-09-28 16:15:16 +03:00
|
|
|
{
|
|
|
|
/* If multiple languages are present we just take the first one */
|
|
|
|
char* delimiter = strchr(ptr, ',');
|
|
|
|
if (delimiter)
|
|
|
|
*delimiter = '\0';
|
2023-05-10 12:49:46 +03:00
|
|
|
*layout = ptr;
|
|
|
|
break;
|
2023-09-28 16:15:16 +03:00
|
|
|
}
|
2023-05-10 12:49:46 +03:00
|
|
|
case 3: // variant
|
|
|
|
*variant = ptr;
|
|
|
|
break;
|
|
|
|
case 4: // option
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2012-02-22 06:28:52 +04:00
|
|
|
}
|
2023-05-10 12:49:46 +03:00
|
|
|
i += strlen(ptr);
|
2012-03-24 05:01:12 +04:00
|
|
|
}
|
2023-05-10 12:49:46 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
2013-03-07 01:35:50 +04:00
|
|
|
|
2023-05-10 12:49:46 +03:00
|
|
|
static DWORD kbd_layout_id_from_x_property(Display* display, Window root, char* property_name)
|
|
|
|
{
|
|
|
|
char* layout = NULL;
|
|
|
|
char* variant = NULL;
|
2024-01-23 18:49:54 +03:00
|
|
|
char* rule = NULL;
|
2023-05-10 12:49:46 +03:00
|
|
|
Atom type = None;
|
|
|
|
int item_size = 0;
|
|
|
|
unsigned long items = 0;
|
|
|
|
unsigned long unread_items = 0;
|
|
|
|
DWORD layout_id = 0;
|
|
|
|
|
2024-07-25 08:38:33 +03:00
|
|
|
Atom property = XInternAtom(display, property_name, False);
|
|
|
|
if (property == None)
|
|
|
|
return 0;
|
2023-05-10 12:49:46 +03:00
|
|
|
|
|
|
|
if (XGetWindowProperty(display, root, property, 0, 1024, False, XA_STRING, &type, &item_size,
|
|
|
|
&items, &unread_items, (unsigned char**)&rule) != Success)
|
2015-06-22 15:26:11 +03:00
|
|
|
return 0;
|
2012-02-22 06:28:52 +04:00
|
|
|
|
2023-05-10 12:49:46 +03:00
|
|
|
if (type != XA_STRING || item_size != 8 || unread_items != 0)
|
2012-03-24 05:01:12 +04:00
|
|
|
{
|
2023-05-10 12:49:46 +03:00
|
|
|
XFree(rule);
|
|
|
|
return 0;
|
2012-03-24 05:01:12 +04:00
|
|
|
}
|
2013-03-06 21:50:25 +04:00
|
|
|
|
2023-05-10 12:49:46 +03:00
|
|
|
parse_xkb_rule_names(rule, items, &layout, &variant);
|
2012-02-22 06:28:52 +04:00
|
|
|
|
2023-05-10 12:49:46 +03:00
|
|
|
DEBUG_KBD("%s layout: %s, variant: %s", property_name, layout, variant);
|
|
|
|
layout_id = find_keyboard_layout_in_xorg_rules(layout, variant);
|
2012-02-22 06:28:52 +04:00
|
|
|
|
2023-05-10 12:49:46 +03:00
|
|
|
XFree(rule);
|
2012-02-22 06:28:52 +04:00
|
|
|
|
2023-05-10 12:49:46 +03:00
|
|
|
return layout_id;
|
2012-02-22 06:28:52 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 12:49:46 +03:00
|
|
|
int freerdp_detect_keyboard_layout_from_xkb(DWORD* keyboardLayoutId)
|
2012-02-22 06:28:52 +04:00
|
|
|
{
|
2023-05-10 12:49:46 +03:00
|
|
|
Display* display = XOpenDisplay(NULL);
|
2015-06-22 15:26:11 +03:00
|
|
|
|
2023-05-10 12:49:46 +03:00
|
|
|
if (!display)
|
|
|
|
return 0;
|
2012-02-22 06:28:52 +04:00
|
|
|
|
2023-08-02 13:07:03 +03:00
|
|
|
Window root = DefaultRootWindow(display);
|
|
|
|
if (!root)
|
|
|
|
return 0;
|
2012-02-22 06:28:52 +04:00
|
|
|
|
2023-05-10 12:49:46 +03:00
|
|
|
/* We start by looking for _XKB_RULES_NAMES_BACKUP which appears to be used by libxklavier */
|
2023-08-02 13:07:03 +03:00
|
|
|
DWORD id = kbd_layout_id_from_x_property(display, root, "_XKB_RULES_NAMES_BACKUP");
|
|
|
|
|
|
|
|
if (0 == id)
|
|
|
|
id = kbd_layout_id_from_x_property(display, root, "_XKB_RULES_NAMES");
|
2012-02-22 06:28:52 +04:00
|
|
|
|
2023-08-02 13:07:03 +03:00
|
|
|
if (0 != id)
|
|
|
|
*keyboardLayoutId = id;
|
2012-02-22 06:28:52 +04:00
|
|
|
|
2023-05-10 12:49:46 +03:00
|
|
|
XCloseDisplay(display);
|
2023-08-02 13:07:03 +03:00
|
|
|
return (int)id;
|
2012-02-22 06:28:52 +04:00
|
|
|
}
|