Debian Codegraph @

KeyUtil.c

raw
1 /* X11::GUITest ($Id: KeyUtil.c 253 2026-08-19 22:37:51Z ctrondlp $)
2 *
3 * Copyright (c) 2003-2026 Dennis K. Paulsen, All Rights Reserved.
4 * Email: [email protected]
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see http://www.gnu.org/licenses.
18 *
19 */
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 #ifndef NOPERL
24 #include "EXTERN.h"
25 #include "perl.h"
26 #include "XSUB.h"
27 #else
28 #include <assert.h>
29 #endif
30 #ifdef __cplusplus
31 }
32 #endif
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <strings.h> /* strcasecmp() is POSIX, declared here rather than in string.h */
37 #include <unistd.h>
38 #include <X11/Xlib.h>
39 #include <X11/Xutil.h>
40 #include <X11/Intrinsic.h>
41 #include <X11/StringDefs.h>
42 #include <X11/keysym.h>
43 #include <X11/extensions/XTest.h>
44 #include "KeyUtil.h"
45
46 static const KeyNameSymTable kns_table[] = { /* {Name, Sym}, */
47 {"BAC", XK_BackSpace}, {"BS", XK_BackSpace}, {"BKS", XK_BackSpace},
48 {"BRE", XK_Break}, {"CAN", XK_Cancel}, {"CAP", XK_Caps_Lock},
49 {"DEL", XK_Delete}, {"DOWN", XK_Down}, {"END", XK_End},
50 {"ENT", XK_Return}, {"ESC", XK_Escape}, {"HEL", XK_Help},
51 {"HOM", XK_Home}, {"INS", XK_Insert}, {"LEF", XK_Left},
52 {"NUM", XK_Num_Lock}, {"PGD", XK_Next}, {"PGU", XK_Prior},
53 {"PRT", XK_Print}, {"RIG", XK_Right}, {"SCR", XK_Scroll_Lock},
54 {"TAB", XK_Tab}, {"UP", XK_Up}, {"F1", XK_F1},
55 {"F2", XK_F2}, {"F3", XK_F3}, {"F4", XK_F4},
56 {"F5", XK_F5}, {"F6", XK_F6}, {"F7", XK_F7},
57 {"F8", XK_F8}, {"F9", XK_F9}, {"F10", XK_F10},
58 {"F11", XK_F11}, {"F12", XK_F12}, {"SPC", XK_space},
59 {"SPA", XK_space}, {"LSK", XK_Super_L}, {"RSK", XK_Super_R},
60 {"MNU", XK_Menu}, {"~", XK_asciitilde}, {"_", XK_underscore},
61 {"[", XK_bracketleft}, {"]", XK_bracketright}, {"!", XK_exclam},
62 {"\"", XK_quotedbl}, {"#", XK_numbersign}, {"$", XK_dollar},
63 {"%", XK_percent}, {"&", XK_ampersand}, {"'", XK_quoteright},
64 {"*", XK_asterisk}, {"+", XK_plus}, {",", XK_comma},
65 {"-", XK_minus}, {".", XK_period}, {"?", XK_question},
66 {"<", XK_less}, {">", XK_greater}, {"=", XK_equal},
67 {"@", XK_at}, {":", XK_colon}, {";", XK_semicolon},
68 {"\\", XK_backslash}, {"`", XK_grave}, {"{", XK_braceleft},
69 {"}", XK_braceright}, {"|", XK_bar}, {"^", XK_asciicircum},
70 {"(", XK_parenleft}, {")", XK_parenright}, {" ", XK_space},
71 {"/", XK_slash}, {"\t", XK_Tab}, {"\n", XK_Return},
72 {"LSH", XK_Shift_L}, {"RSH", XK_Shift_R}, {"LCT", XK_Control_L},
73 {"RCT", XK_Control_R}, {"LAL", XK_Alt_L}, {"RAL", XK_Alt_R},
74 {"LMA", XK_Meta_L}, {"RMA", XK_Meta_R},
75 };
76
77 static const KeyNameSymTable kns_modcode_table[] = { /* {ModCodeName, Sym}, */
78 {"^", XK_Control_L},
79 {"%", XK_Alt_L},
80 {"+", XK_Shift_L},
81 {"#", XK_Meta_L},
82 {"&", XK_ISO_Level3_Shift},
83 /* The old table contained duplicate ^/+ entries for the right-hand
84 * modifiers. GetModifierCode() returns the first matching entry, so
85 * those duplicates were unreachable and provided no useful mapping. */
86 };
87
88 BOOL GetKeySym(const char *name, KeySym *sym)
89 {
90 size_t x = 0;
91
92 assert(name != NULL);
93 assert(sym != NULL);
94
95 /* See if we can obtain the KeySym without looking at table.
96 * Note: XStringToKeysym("space") would return KeySym
97 * XK_space... Case sensitive. */
98 *sym = XStringToKeysym(name);
99 if (*sym != NoSymbol) {
100 /* Got It */
101 return(TRUE);
102 }
103
104 /* Do case insensitive search for specified name to obtain the KeySym from table */
105 for (x = 0; x < (sizeof(kns_table) / sizeof(kns_table[0])); x++) {
106 if (strcasecmp(kns_table[x].Name, name) == 0) {
107 /* Found It */
108 *sym = kns_table[x].Sym;
109 return(TRUE);
110 }
111 }
112
113 /* Not Found */
114 *sym = NoSymbol;
115 return(FALSE);
116 }
117
118 const char *GetKeyName(KeySym sym)
119 {
120 size_t x = 0;
121
122 /* Look for KeySym in order to obtain name */
123 for (x = 0; x < (sizeof(kns_table) / sizeof(kns_table[0])); x++) {
124 if (sym == kns_table[x].Sym) {
125 /* Found It */
126 return kns_table[x].Name;
127 }
128 }
129
130 return XKeysymToString(sym);
131 }
132
133 const char *GetModifierCode(KeySym sym)
134 {
135 size_t x = 0;
136
137 for (x = 0; x < (sizeof(kns_modcode_table) / sizeof(kns_modcode_table[0])); x++) {
138 if (sym == kns_modcode_table[x].Sym) {
139 return kns_modcode_table[x].Name;
140 }
141 }
142
143 return NULL;
144 }