Debian Codegraph @

src/text.c

raw
1 /*
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3 * SPDX-License-Identifier: HPND
4 */
5
6 #include "config.h"
7
8 #include <assert.h>
9
10 #include "keymap.h"
11 #include "keysym.h"
12 #include "text.h"
13
14 bool
15 LookupString(const LookupEntry tab[], const char *string,
16 unsigned int *value_rtrn)
17 {
18 if (!string)
19 return false;
20
21 for (const LookupEntry *entry = tab; entry->name; entry++) {
22 if (istreq(entry->name, string)) {
23 *value_rtrn = entry->value;
24 return true;
25 }
26 }
27
28 return false;
29 }
30
31 const char *
32 LookupValue(const LookupEntry tab[], unsigned int value)
33 {
34 for (const LookupEntry *entry = tab; entry->name; entry++)
35 if (entry->value == value)
36 return entry->name;
37
38 return NULL;
39 }
40
41 const LookupEntry ctrlMaskNames[] = {
42 { "RepeatKeys", CONTROL_REPEAT },
43 { "Repeat", CONTROL_REPEAT },
44 { "AutoRepeat", CONTROL_REPEAT },
45 { "SlowKeys", CONTROL_SLOW },
46 { "BounceKeys", CONTROL_DEBOUNCE },
47 { "StickyKeys", CONTROL_STICKY },
48 { "MouseKeys", CONTROL_MOUSEKEYS },
49 { "MouseKeysAccel", CONTROL_MOUSEKEYS_ACCEL },
50 { "AccessXKeys", CONTROL_AX },
51 { "AccessXTimeout", CONTROL_AX_TIMEOUT },
52 { "AccessXFeedback", CONTROL_AX_FEEDBACK },
53 { "AudibleBell", CONTROL_BELL },
54 { "IgnoreGroupLock", CONTROL_IGNORE_GROUP_LOCK },
55 { "all", CONTROL_ALL },
56 { "none", 0 },
57 { "Overlay1", 0 },
58 { "Overlay2", 0 },
59 { NULL, 0 }
60 };
61
62 const LookupEntry modComponentMaskNames[] = {
63 { "base", XKB_STATE_MODS_DEPRESSED },
64 { "latched", XKB_STATE_MODS_LATCHED },
65 { "locked", XKB_STATE_MODS_LOCKED },
66 { "effective", XKB_STATE_MODS_EFFECTIVE },
67 { "compat", XKB_STATE_MODS_EFFECTIVE },
68 { "any", XKB_STATE_MODS_EFFECTIVE },
69 { "none", 0 },
70 { NULL, 0 }
71 };
72
73 const LookupEntry groupComponentMaskNames[] = {
74 { "base", XKB_STATE_LAYOUT_DEPRESSED },
75 { "latched", XKB_STATE_LAYOUT_LATCHED },
76 { "locked", XKB_STATE_LAYOUT_LOCKED },
77 { "effective", XKB_STATE_LAYOUT_EFFECTIVE },
78 { "any", XKB_STATE_LAYOUT_EFFECTIVE },
79 { "none", 0 },
80 { NULL, 0 }
81 };
82
83 const LookupEntry groupMaskNames[] = {
84 { "none", 0x00 },
85 { "all", XKB_ALL_GROUPS },
86 { NULL, 0 }
87 };
88
89 const LookupEntry buttonNames[] = {
90 { "Button1", 1 },
91 { "Button2", 2 },
92 { "Button3", 3 },
93 { "Button4", 4 },
94 { "Button5", 5 },
95 { "default", 0 },
96 { NULL, 0 }
97 };
98
99 const LookupEntry useModMapValueNames[] = {
100 { "LevelOne", 1 },
101 { "Level1", 1 },
102 { "AnyLevel", 0 },
103 { "any", 0 },
104 { NULL, 0 }
105 };
106
107 const LookupEntry actionTypeNames[] = {
108 { "NoAction", ACTION_TYPE_NONE },
109 { "VoidAction", ACTION_TYPE_VOID },
110 { "SetMods", ACTION_TYPE_MOD_SET },
111 { "LatchMods", ACTION_TYPE_MOD_LATCH },
112 { "LockMods", ACTION_TYPE_MOD_LOCK },
113 { "SetGroup", ACTION_TYPE_GROUP_SET },
114 { "LatchGroup", ACTION_TYPE_GROUP_LATCH },
115 { "LockGroup", ACTION_TYPE_GROUP_LOCK },
116 { "MovePtr", ACTION_TYPE_PTR_MOVE },
117 { "MovePointer", ACTION_TYPE_PTR_MOVE },
118 { "PtrBtn", ACTION_TYPE_PTR_BUTTON },
119 { "PointerButton", ACTION_TYPE_PTR_BUTTON },
120 { "LockPtrBtn", ACTION_TYPE_PTR_LOCK },
121 { "LockPtrButton", ACTION_TYPE_PTR_LOCK },
122 { "LockPointerButton", ACTION_TYPE_PTR_LOCK },
123 { "LockPointerBtn", ACTION_TYPE_PTR_LOCK },
124 { "SetPtrDflt", ACTION_TYPE_PTR_DEFAULT },
125 { "SetPointerDefault", ACTION_TYPE_PTR_DEFAULT },
126 { "Terminate", ACTION_TYPE_TERMINATE },
127 { "TerminateServer", ACTION_TYPE_TERMINATE },
128 { "SwitchScreen", ACTION_TYPE_SWITCH_VT },
129 { "SetControls", ACTION_TYPE_CTRL_SET },
130 { "LockControls", ACTION_TYPE_CTRL_LOCK },
131 { "Private", ACTION_TYPE_PRIVATE },
132 /* deprecated actions below here - unused */
133 { "RedirectKey", ACTION_TYPE_UNSUPPORTED_LEGACY },
134 { "Redirect", ACTION_TYPE_UNSUPPORTED_LEGACY },
135 { "ISOLock", ACTION_TYPE_UNSUPPORTED_LEGACY },
136 { "ActionMessage", ACTION_TYPE_UNSUPPORTED_LEGACY },
137 { "MessageAction", ACTION_TYPE_UNSUPPORTED_LEGACY },
138 { "Message", ACTION_TYPE_UNSUPPORTED_LEGACY },
139 { "DeviceBtn", ACTION_TYPE_UNSUPPORTED_LEGACY },
140 { "DevBtn", ACTION_TYPE_UNSUPPORTED_LEGACY },
141 { "DevButton", ACTION_TYPE_UNSUPPORTED_LEGACY },
142 { "DeviceButton", ACTION_TYPE_UNSUPPORTED_LEGACY },
143 { "LockDeviceBtn", ACTION_TYPE_UNSUPPORTED_LEGACY },
144 { "LockDevBtn", ACTION_TYPE_UNSUPPORTED_LEGACY },
145 { "LockDevButton", ACTION_TYPE_UNSUPPORTED_LEGACY },
146 { "LockDeviceButton", ACTION_TYPE_UNSUPPORTED_LEGACY },
147 { "DeviceValuator", ACTION_TYPE_UNSUPPORTED_LEGACY },
148 { "DevVal", ACTION_TYPE_UNSUPPORTED_LEGACY },
149 { "DeviceVal", ACTION_TYPE_UNSUPPORTED_LEGACY },
150 { "DevValuator", ACTION_TYPE_UNSUPPORTED_LEGACY },
151 { NULL, 0 },
152 };
153
154 const LookupEntry symInterpretMatchMaskNames[] = {
155 { "NoneOf", MATCH_NONE },
156 { "AnyOfOrNone", MATCH_ANY_OR_NONE },
157 { "AnyOf", MATCH_ANY },
158 { "AllOf", MATCH_ALL },
159 { "Exactly", MATCH_EXACTLY },
160 { NULL, 0 },
161 };
162
163 const char *
164 ModIndexText(struct xkb_context *ctx, const struct xkb_mod_set *mods,
165 xkb_mod_index_t ndx)
166 {
167 if (ndx == XKB_MOD_INVALID)
168 return "none";
169
170 if (ndx == XKB_MOD_NONE)
171 return "None";
172
173 if (ndx >= mods->num_mods)
174 return NULL;
175
176 return xkb_atom_text(ctx, mods->mods[ndx].name);
177 }
178
179 const char *
180 ActionTypeText(enum xkb_action_type type)
181 {
182 const char *name = LookupValue(actionTypeNames, type);
183 return name ? name : "Private";
184 }
185
186 const char *
187 KeysymText(struct xkb_context *ctx, xkb_keysym_t sym)
188 {
189 char *buffer = xkb_context_get_buffer(ctx, XKB_KEYSYM_NAME_MAX_SIZE);
190 xkb_keysym_get_name(sym, buffer, XKB_KEYSYM_NAME_MAX_SIZE);
191 return buffer;
192 }
193
194 const char *
195 KeyNameText(struct xkb_context *ctx, xkb_atom_t name)
196 {
197 const char *sname = xkb_atom_text(ctx, name);
198 size_t len = strlen_safe(sname) + 3;
199 char *buf = xkb_context_get_buffer(ctx, len);
200 snprintf(buf, len, "<%s>", strempty(sname));
201 return buf;
202 }
203
204 const char *
205 SIMatchText(enum xkb_match_operation type)
206 {
207 return LookupValue(symInterpretMatchMaskNames, type);
208 }
209
210 const char *
211 ModMaskText(struct xkb_context *ctx, enum mod_type type,
212 const struct xkb_mod_set *mods, xkb_mod_mask_t mask)
213 {
214 char buf[1024] = {0};
215 size_t pos = 0;
216 const struct xkb_mod *mod;
217
218 /* We want to avoid boolean blindness, but we expected only 2 values */
219 assert(type == MOD_REAL || type == MOD_BOTH);
220
221 if (mask == 0)
222 return "none";
223
224 if (mask == MOD_REAL_MASK_ALL)
225 return "all";
226
227 if ((type == MOD_REAL && (mask & ~MOD_REAL_MASK_ALL)) ||
228 unlikely(mask & ~((UINT64_C(1) << mods->num_mods) - 1))) {
229 /* If we get a mask that cannot be expressed with the known modifiers
230 * of the given type, print it as hexadecimal */
231 const int ret = snprintf(buf, sizeof(buf), "0x%"PRIx32, mask);
232 static_assert(sizeof(mask) == 4 && sizeof(buf) >= sizeof("0xffffffff"),
233 "Buffer too small");
234 assert(ret >= 0 && (size_t) ret < sizeof(buf));
235 pos = (size_t) ret;
236 } else {
237 /* Print known mods */
238 xkb_mods_mask_foreach(mask, mod, mods) {
239 int ret = snprintf(buf + pos, sizeof(buf) - pos, "%s%s",
240 pos == 0 ? "" : "+",
241 xkb_atom_text(ctx, mod->name));
242 if (ret <= 0 || pos + ret >= sizeof(buf))
243 break;
244 else
245 pos += ret;
246 }
247 }
248 pos++;
249
250 return memcpy(xkb_context_get_buffer(ctx, pos), buf, pos);
251 }
252
253 const char *
254 LedStateMaskText(struct xkb_context *ctx, const LookupEntry *lookup,
255 enum xkb_state_component mask)
256 {
257 char buf[1024];
258 size_t pos = 0;
259
260 if (mask == 0)
261 return "0";
262
263 for (unsigned int i = 0; mask; i++) {
264 int ret;
265
266 if (!(mask & (1u << i)))
267 continue;
268
269 mask &= ~(1u << i);
270
271 const char* const maskText = LookupValue(lookup, 1u << i);
272 assert(maskText != NULL);
273 ret = snprintf(buf + pos, sizeof(buf) - pos, "%s%s",
274 pos == 0 ? "" : "+", maskText);
275 if (ret <= 0 || pos + ret >= sizeof(buf))
276 break;
277 else
278 pos += ret;
279 }
280 pos++;
281
282 return memcpy(xkb_context_get_buffer(ctx, pos), buf, pos);
283 }
284
285 const char *
286 ControlMaskText(struct xkb_context *ctx, enum xkb_action_controls mask)
287 {
288 char buf[1024];
289 size_t pos = 0;
290
291 if (mask == 0)
292 return "none";
293
294 if (mask == CONTROL_ALL)
295 return "all";
296
297 for (unsigned int i = 0; mask; i++) {
298 int ret;
299
300 if (!(mask & (1u << i)))
301 continue;
302
303 mask &= ~(1u << i);
304
305 const char* const maskText = LookupValue(ctrlMaskNames, 1u << i);
306 assert(maskText != NULL);
307 ret = snprintf(buf + pos, sizeof(buf) - pos, "%s%s",
308 pos == 0 ? "" : "+", maskText);
309 if (ret <= 0 || pos + ret >= sizeof(buf))
310 break;
311 else
312 pos += ret;
313 }
314 pos++;
315
316 return memcpy(xkb_context_get_buffer(ctx, pos), buf, pos);
317 }