Debian Codegraph @

libfreerdp/locale/keyboard_x11.c

raw symbols SCIP
1 /**
2 * FreeRDP: A Remote Desktop Protocol Implementation
3 * X11 Keyboard Mapping
4 *
5 * Copyright 2009-2012 Marc-Andre Moreau <[email protected]>
6 * Copyright 2023 Bernhard Miklautz <[email protected]>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 #include <string.h>
22
23 #include <X11/X.h>
24 #include <X11/Xatom.h>
25 #include <X11/Xlib.h>
26
27 #include "liblocale.h"cxx . . $ `<file>/libfreerdp/locale/liblocale.h`/
28 #include "keyboard_x11.h"cxx . . $ `<file>/libfreerdp/locale/keyboard_x11.h`/
29 #include "xkb_layout_ids.h"cxx . . $ `<file>/libfreerdp/locale/xkb_layout_ids.h`/
30
31 static BOOLcxx . . $ BOOL# parse_xkb_rule_namescxx . . $ parse_xkb_rule_names(20fe896bd243dded).(char* xkb_rule, unsigned long num_bytes, char** layout,
32 char** variant)
33 {
34 /* Sample output for "Canadian Multilingual Standard"
35 *
36 * _XKB_RULES_NAMES_BACKUP(STRING) = "xorg", "pc105", "ca", "multi", "magic"
37 *
38 * Format: "rules", "model", "layout", "variant", "options"
39 *
40 * Where "xorg" is the set of rules
41 * "pc105" the keyboard model
42 * "ca" the keyboard layout(s) (can also be something like 'us,uk')
43 * "multi" the keyboard layout variant(s) (in the examples, “,winkeys” - which means first
44 * layout uses some “default” variant and second uses “winkeys” variant)
45 * "magic" - configuration option (in the examples,
46 * “eurosign:e,lv3:ralt_switch,grp:rctrl_toggle”
47 * - three options)
48 */
49 for (size_t i = 0, index = 0; i < num_bytes; i++, index++)
50 {
51 char* ptr = xkb_rule + i;
52 i += strnlencxx . . $ strnlen(ac5a385650b4bde5).(ptr, num_bytes - i);
53
54 switch (index)
55 {
56 case 0: // rules
57 break;
58 case 1: // model
59 break;
60 case 2: // layout
61 {
62 /* If multiple languages are present we just take the first one */
63 char* delimiter = strchrcxx . . $ strchr(2050848944e00d54).(ptr, ',');
64 if (delimiter)
65 *delimiter = '\0';
66 *layout = ptr;
67 break;
68 }
69 case 3: // variant
70 {
71 /* If multiple variants are present we just take the first one */
72 char* delimiter = strchrcxx . . $ strchr(2050848944e00d54).(ptr, ',');
73 if (delimiter)
74 *delimiter = '\0';
75 *variant = ptr;
76 }
77 break;
78 case 4: // option
79 break;
80 default:
81 break;
82 }
83 }
84 return TRUEcxx . . $ `winpr/include/winpr/wtypes.h:126:9`!;
85 }
86
87 static DWORDcxx . . $ DWORD# kbd_layout_id_from_x_propertycxx . . $ kbd_layout_id_from_x_property(db57af38ba8c5117).(Displaycxx . . $ Display#* display, Windowcxx . . $ Window# root, char* property_name)
88 {
89 char* layout = nullptrcxx . . $ `winpr/include/winpr/platform.h:88:9`!;
90 char* variant = nullptrcxx . . $ `winpr/include/winpr/platform.h:88:9`!;
91 char* rule = nullptrcxx . . $ `winpr/include/winpr/platform.h:88:9`!;
92 Atomcxx . . $ Atom# type = Nonecxx . . $ `<external>/1208737181511191802/X.h:115:9`!;
93 int item_size = 0;
94 unsigned long items = 0;
95 unsigned long unread_items = 0;
96 DWORDcxx . . $ DWORD# layout_id = 0;
97
98 Atomcxx . . $ Atom# property = XInternAtomcxx . . $ XInternAtom(cce9092df7d5c4bb).(display, property_name, Falsecxx . . $ `<external>/7073035915403465041/Xlib.h:81:9`!);
99 if (property == Nonecxx . . $ `<external>/1208737181511191802/X.h:115:9`!)
100 return 0;
101
102 if (XGetWindowPropertycxx . . $ XGetWindowProperty(46eeb5a8c23bb2ad).(display, root, property, 0, 1024, Falsecxx . . $ `<external>/7073035915403465041/Xlib.h:81:9`!, XA_STRINGcxx . . $ `<external>/12705893824843406196/Xatom.h:39:9`!, &type, &item_size,
103 &items, &unread_items, (unsigned char**)&rule) != Successcxx . . $ `<external>/1208737181511191802/X.h:350:9`!)
104 return 0;
105
106 if (type != XA_STRINGcxx . . $ `<external>/12705893824843406196/Xatom.h:39:9`! || item_size != 8 || unread_items != 0)
107 {
108 XFreecxx . . $ XFree(dedf1e9b1c614a07).(rule);
109 return 0;
110 }
111
112 parse_xkb_rule_namescxx . . $ parse_xkb_rule_names(20fe896bd243dded).(rule, items, &layout, &variant);
113
114 DEBUG_KBDcxx . . $ `libfreerdp/locale/liblocale.h:31:9`!("%s layout: %s, variant: %s", property_name, layout, variant);
115 layout_id = find_keyboard_layout_in_xorg_rulescxx . . $ find_keyboard_layout_in_xorg_rules(d4414ff42889198).(layout, variant);
116
117 XFreecxx . . $ XFree(dedf1e9b1c614a07).(rule);
118
119 return layout_id;
120 }
121
122 int freerdp_detect_keyboard_layout_from_xkbcxx . . $ freerdp_detect_keyboard_layout_from_xkb(4ebf403dd87dd80).(DWORDcxx . . $ DWORD#* keyboardLayoutId)
123 {
124 Displaycxx . . $ Display#* display = XOpenDisplaycxx . . $ XOpenDisplay(f2c82aea04a05a19).(nullptrcxx . . $ `winpr/include/winpr/platform.h:88:9`!);
125
126 if (!display)
127 return 0;
128
129 Windowcxx . . $ Window# root = DefaultRootWindowcxx . . $ `<external>/7073035915403465041/Xlib.h:90:9`!(display);
130 if (!root)
131 return 0;
132
133 /* We start by looking for _XKB_RULES_NAMES_BACKUP which appears to be used by libxklavier */
134 DWORDcxx . . $ DWORD# id = kbd_layout_id_from_x_propertycxx . . $ kbd_layout_id_from_x_property(db57af38ba8c5117).(display, root, "_XKB_RULES_NAMES_BACKUP");
135
136 if (0 == id)
137 id = kbd_layout_id_from_x_propertycxx . . $ kbd_layout_id_from_x_property(db57af38ba8c5117).(display, root, "_XKB_RULES_NAMES");
138
139 if (0 != id)
140 *keyboardLayoutId = id;
141
142 XCloseDisplaycxx . . $ XCloseDisplay(62a385dbb0483707).(display);
143 return (int)id;
144 }