118820b60ed5d4a4e1b08245cf3b6d4ca76b7d33
[platform/upstream/libxkbcommon.git] / src / text.c
1 /************************************************************
2  * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3  *
4  * Permission to use, copy, modify, and distribute this
5  * software and its documentation for any purpose and without
6  * fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting
9  * documentation, and that the name of Silicon Graphics not be
10  * used in advertising or publicity pertaining to distribution
11  * of the software without specific prior written permission.
12  * Silicon Graphics makes no representation about the suitability
13  * of this software for any purpose. It is provided "as is"
14  * without any express or implied warranty.
15  *
16  * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19  * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
24  *
25  ********************************************************/
26
27 #include "keymap.h"
28 #include "text.h"
29
30 bool
31 LookupString(const LookupEntry tab[], const char *string,
32               unsigned int *value_rtrn)
33 {
34     const LookupEntry *entry;
35
36     if (!string)
37         return false;
38
39     for (entry = tab; entry->name; entry++) {
40         if (istreq(entry->name, string)) {
41             *value_rtrn = entry->value;
42             return true;
43         }
44     }
45
46     return false;
47 }
48
49 const char *
50 LookupValue(const LookupEntry tab[], unsigned int value)
51 {
52     const LookupEntry *entry;
53
54     for (entry = tab; entry->name; entry++)
55         if (entry->value == value)
56             return entry->name;
57
58     return NULL;
59 }
60
61 const LookupEntry ctrlMaskNames[] = {
62     { "RepeatKeys", CONTROL_REPEAT },
63     { "Repeat", CONTROL_REPEAT },
64     { "AutoRepeat", CONTROL_REPEAT },
65     { "SlowKeys", CONTROL_SLOW },
66     { "BounceKeys", CONTROL_DEBOUNCE },
67     { "StickyKeys", CONTROL_STICKY },
68     { "MouseKeys", CONTROL_MOUSEKEYS },
69     { "MouseKeysAccel", CONTROL_MOUSEKEYS_ACCEL },
70     { "AccessXKeys", CONTROL_AX },
71     { "AccessXTimeout", CONTROL_AX_TIMEOUT },
72     { "AccessXFeedback", CONTROL_AX_FEEDBACK },
73     { "AudibleBell", CONTROL_BELL },
74     { "IgnoreGroupLock", CONTROL_IGNORE_GROUP_LOCK },
75     { "all", CONTROL_ALL },
76     { "none", 0 },
77     { "Overlay1", 0 },
78     { "Overlay2", 0 },
79     { NULL, 0 }
80 };
81
82 const LookupEntry modComponentMaskNames[] = {
83     {"base", XKB_STATE_DEPRESSED},
84     {"latched", XKB_STATE_LATCHED},
85     {"locked", XKB_STATE_LOCKED},
86     {"effective", XKB_STATE_EFFECTIVE},
87     {"compat", XKB_STATE_EFFECTIVE},
88     {"any", XKB_STATE_EFFECTIVE},
89     {"none", 0},
90     {NULL, 0}
91 };
92
93 const LookupEntry groupComponentMaskNames[] = {
94     {"base", XKB_STATE_DEPRESSED},
95     {"latched", XKB_STATE_LATCHED},
96     {"locked", XKB_STATE_LOCKED},
97     {"effective", XKB_STATE_EFFECTIVE},
98     {"any", XKB_STATE_EFFECTIVE},
99     {"none", 0},
100     {NULL, 0}
101 };
102
103 const LookupEntry groupMaskNames[] = {
104     {"group1", 0x01},
105     {"group2", 0x02},
106     {"group3", 0x04},
107     {"group4", 0x08},
108     {"group5", 0x10},
109     {"group6", 0x20},
110     {"group7", 0x40},
111     {"group8", 0x80},
112     {"none", 0x00},
113     {"all", 0xff},
114     {NULL, 0}
115 };
116
117 const LookupEntry groupNames[] = {
118     {"group1", 1},
119     {"group2", 2},
120     {"group3", 3},
121     {"group4", 4},
122     {"group5", 5},
123     {"group6", 6},
124     {"group7", 7},
125     {"group8", 8},
126     {NULL, 0}
127 };
128
129 const LookupEntry levelNames[] = {
130     { "level1", 1 },
131     { "level2", 2 },
132     { "level3", 3 },
133     { "level4", 4 },
134     { "level5", 5 },
135     { "level6", 6 },
136     { "level7", 7 },
137     { "level8", 8 },
138     { NULL, 0 }
139 };
140
141 const LookupEntry buttonNames[] = {
142     { "button1", 1 },
143     { "button2", 2 },
144     { "button3", 3 },
145     { "button4", 4 },
146     { "button5", 5 },
147     { "default", 0 },
148     { NULL, 0 }
149 };
150
151 const LookupEntry useModMapValueNames[] = {
152     { "levelone", 1 },
153     { "level1", 1 },
154     { "anylevel", 0 },
155     { "any", 0 },
156     { NULL, 0 }
157 };
158
159 const LookupEntry actionTypeNames[] = {
160     { "NoAction", ACTION_TYPE_NONE },
161     { "SetMods", ACTION_TYPE_MOD_SET },
162     { "LatchMods", ACTION_TYPE_MOD_LATCH },
163     { "LockMods", ACTION_TYPE_MOD_LOCK },
164     { "SetGroup", ACTION_TYPE_GROUP_SET },
165     { "LatchGroup", ACTION_TYPE_GROUP_LATCH },
166     { "LockGroup", ACTION_TYPE_GROUP_LOCK },
167     { "MovePtr", ACTION_TYPE_PTR_MOVE },
168     { "MovePointer", ACTION_TYPE_PTR_MOVE },
169     { "PtrBtn", ACTION_TYPE_PTR_BUTTON },
170     { "PointerButton", ACTION_TYPE_PTR_BUTTON },
171     { "LockPtrBtn", ACTION_TYPE_PTR_LOCK },
172     { "LockPtrButton", ACTION_TYPE_PTR_LOCK },
173     { "LockPointerButton", ACTION_TYPE_PTR_LOCK },
174     { "LockPointerBtn", ACTION_TYPE_PTR_LOCK },
175     { "SetPtrDflt", ACTION_TYPE_PTR_DEFAULT },
176     { "SetPointerDefault", ACTION_TYPE_PTR_DEFAULT },
177     { "Terminate", ACTION_TYPE_TERMINATE },
178     { "TerminateServer", ACTION_TYPE_TERMINATE },
179     { "SwitchScreen", ACTION_TYPE_SWITCH_VT },
180     { "SetControls", ACTION_TYPE_CTRL_SET },
181     { "LockControls", ACTION_TYPE_CTRL_LOCK },
182     { "RedirectKey", ACTION_TYPE_KEY_REDIRECT },
183     { "Redirect", ACTION_TYPE_KEY_REDIRECT },
184     { "Private", ACTION_TYPE_PRIVATE },
185     /* deprecated actions below here - unused */
186     { "ISOLock", ACTION_TYPE_NONE },
187     { "ActionMessage", ACTION_TYPE_NONE },
188     { "MessageAction", ACTION_TYPE_NONE },
189     { "Message", ACTION_TYPE_NONE },
190     { "DeviceBtn", ACTION_TYPE_NONE },
191     { "DevBtn", ACTION_TYPE_NONE },
192     { "DevButton", ACTION_TYPE_NONE },
193     { "DeviceButton", ACTION_TYPE_NONE },
194     { "LockDeviceBtn", ACTION_TYPE_NONE },
195     { "LockDevBtn", ACTION_TYPE_NONE },
196     { "LockDevButton", ACTION_TYPE_NONE },
197     { "LockDeviceButton", ACTION_TYPE_NONE },
198     { "DeviceValuator", ACTION_TYPE_NONE },
199     { "DevVal", ACTION_TYPE_NONE },
200     { "DeviceVal", ACTION_TYPE_NONE },
201     { "DevValuator", ACTION_TYPE_NONE },
202     { NULL, 0 },
203 };
204
205 const LookupEntry symInterpretMatchMaskNames[] = {
206     { "NoneOf", MATCH_NONE },
207     { "AnyOfOrNone", MATCH_ANY_OR_NONE },
208     { "AnyOf", MATCH_ANY },
209     { "AllOf", MATCH_ALL },
210     { "Exactly", MATCH_EXACTLY },
211 };
212
213 #define BUFFER_SIZE 1024
214
215 static char *
216 GetBuffer(size_t size)
217 {
218     static char buffer[BUFFER_SIZE];
219     static size_t next;
220     char *rtrn;
221
222     if (size >= BUFFER_SIZE)
223         return NULL;
224
225     if (BUFFER_SIZE - next <= size)
226         next = 0;
227
228     rtrn = &buffer[next];
229     next += size;
230
231     return rtrn;
232 }
233
234 /* Get a vmod name's text, where the vmod index is zero based. */
235 static const char *
236 VModIndexText(struct xkb_keymap *keymap, xkb_mod_index_t ndx)
237 {
238     if (ndx >= darray_size(keymap->vmods))
239         return "illegal";
240     return xkb_atom_text(keymap->ctx,
241                          darray_item(keymap->vmods, ndx).name);
242 }
243
244 /* Get a mod mask's text, where the mask is in rmods+vmods format. */
245 const char *
246 VModMaskText(struct xkb_keymap *keymap, xkb_mod_mask_t cmask)
247 {
248     xkb_mod_index_t i;
249     xkb_mod_mask_t rmask, vmask;
250     int len, rem;
251     const char *mm = NULL;
252     char *rtrn, *str;
253     char buf[BUFFER_SIZE];
254
255     rmask = cmask & 0xff;
256     vmask = cmask >> XKB_NUM_CORE_MODS;
257
258     if (rmask == 0 && vmask == 0)
259         return "none";
260
261     if (rmask != 0)
262         mm = ModMaskText(rmask);
263
264     str = buf;
265     buf[0] = '\0';
266     rem = BUFFER_SIZE;
267
268     if (vmask != 0) {
269         for (i = 0; i < darray_size(keymap->vmods) && rem > 1; i++) {
270             if (!(vmask & (1 << i)))
271                 continue;
272
273             len = snprintf(str, rem, "%s%s",
274                            (str != buf) ? "+" : "",
275                            VModIndexText(keymap, i));
276             rem -= len;
277             str += len;
278         }
279
280         str = buf;
281     }
282     else
283         str = NULL;
284
285     len = (str ? strlen(str) : 0) + (mm ? strlen(mm) : 0) +
286           (str && mm ? 1 : 0);
287     if (len >= BUFFER_SIZE)
288         len = BUFFER_SIZE - 1;
289
290     rtrn = GetBuffer(len + 1);
291     rtrn[0] = '\0';
292
293     snprintf(rtrn, len + 1, "%s%s%s", (mm ? mm : ""),
294              (mm && str ? "+" : ""), (str ? str : ""));
295
296     return rtrn;
297 }
298
299 /*
300  * IMPORTATNT
301  * The indices used for the legacy core modifiers is derived from
302  * the order of the names in this table. It matches the values
303  * ShiftMapIndex, LockMapIndex, etc. from X11/X.h. Take note before
304  * changing.
305  */
306 static const char *modNames[XKB_NUM_CORE_MODS] = {
307     "Shift",
308     "Lock",
309     "Control",
310     "Mod1",
311     "Mod2",
312     "Mod3",
313     "Mod4",
314     "Mod5",
315 };
316
317 xkb_mod_index_t
318 ModNameToIndex(const char *name)
319 {
320     xkb_mod_index_t i;
321
322     for (i = 0; i < XKB_NUM_CORE_MODS; i++)
323         if (istreq(name, modNames[i]))
324             return i;
325
326     return XKB_MOD_INVALID;
327 }
328
329 const char *
330 ModIndexToName(xkb_mod_index_t ndx)
331 {
332     if (ndx < XKB_NUM_CORE_MODS)
333         return modNames[ndx];
334     return NULL;
335 }
336
337 const char *
338 ModIndexText(xkb_mod_index_t ndx)
339 {
340     const char *name;
341     char *buf;
342
343     name = ModIndexToName(ndx);
344     if (name)
345         return name;
346
347     if (ndx == XKB_MOD_INVALID)
348         return "none";
349
350     buf = GetBuffer(32);
351     snprintf(buf, 32, "ILLEGAL_%02x", ndx);
352
353     return buf;
354 }
355
356 /* Gets the text for the real modifiers only. */
357 const char *
358 ModMaskText(xkb_mod_mask_t mask)
359 {
360     int i, rem;
361     xkb_mod_index_t bit;
362     char *str, *buf;
363
364     if ((mask & 0xff) == 0xff)
365         return "all";
366
367     if ((mask & 0xff) == 0)
368         return "none";
369
370     rem = 64;
371     buf = GetBuffer(rem);
372     str = buf;
373     buf[0] = '\0';
374     for (i = 0, bit = 1; i < XKB_NUM_CORE_MODS && rem > 1; i++, bit <<= 1) {
375         int len;
376
377         if (!(mask & bit))
378             continue;
379
380         len = snprintf(str, rem, "%s%s",
381                        (str != buf ?  "+" : ""), modNames[i]);
382         rem -= len;
383         str += len;
384     }
385
386     return buf;
387 }
388
389 const char *
390 ActionTypeText(unsigned type)
391 {
392     const char *name = LookupValue(actionTypeNames, type);
393     return name ? name : "Private";
394 }
395
396 const char *
397 KeysymText(xkb_keysym_t sym)
398 {
399     static char buffer[64];
400
401     xkb_keysym_get_name(sym, buffer, sizeof buffer);
402
403     return buffer;
404 }
405
406 const char *
407 KeyNameText(struct xkb_context *ctx, xkb_atom_t name)
408 {
409     const char *sname = xkb_atom_text(ctx, name);
410     size_t len = strlen(sname) + 3;
411     char *buf = GetBuffer(len);
412     snprintf(buf, len, "<%s>", sname);
413     return buf;
414 }
415
416 const char *
417 SIMatchText(enum xkb_match_operation type)
418 {
419     const char *name;
420     char *buf;
421
422     type &= MATCH_OP_MASK;
423
424     name = LookupValue(symInterpretMatchMaskNames, type);
425     if (name)
426         return name;
427
428     buf = GetBuffer(40);
429     snprintf(buf, 40, "0x%x", type);
430     return buf;
431 }