Use stdbool.h
[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 "xkbmisc.h"
28 #include "xkbcommon/xkbcommon.h"
29 #include "XKBcommonint.h"
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33
34 #define BUFFER_SIZE 1024
35 static char textBuffer[BUFFER_SIZE];
36 static unsigned int tbNext = 0;
37
38 static char *
39 tbGetBuffer(unsigned int size)
40 {
41     char *rtrn;
42
43     if (size >= BUFFER_SIZE)
44         return NULL;
45
46     if ((BUFFER_SIZE - tbNext) <= size)
47         tbNext = 0;
48
49     rtrn = &textBuffer[tbNext];
50     tbNext += size;
51
52     return rtrn;
53 }
54
55 static const char *
56 XkbcVModIndexText(struct xkb_keymap * xkb, unsigned ndx)
57 {
58     int len;
59     char *rtrn;
60     const char *tmp = NULL;
61     char buf[20];
62
63     if (ndx >= XkbNumVirtualMods)
64          tmp = "illegal";
65     else if (xkb && xkb->names)
66          tmp = xkb->names->vmods[ndx];
67
68     if (!tmp) {
69         snprintf(buf, sizeof(buf) - 1, "%d", ndx);
70         tmp = buf;
71     }
72
73     len = strlen(tmp) + 1;
74     if (len >= BUFFER_SIZE)
75         len = BUFFER_SIZE - 1;
76
77     rtrn = tbGetBuffer(len);
78     strncpy(rtrn, tmp, len);
79
80     return rtrn;
81 }
82
83 const char *
84 XkbcVModMaskText(struct xkb_keymap * xkb, unsigned modMask, unsigned mask)
85 {
86     int i, bit, len, rem;
87     const char *mm = NULL;
88     char *rtrn, *str;
89     char buf[BUFFER_SIZE];
90
91     if ((modMask == 0) && (mask == 0))
92         return "none";
93
94     if (modMask != 0)
95         mm = XkbcModMaskText(modMask, false);
96
97     str = buf;
98     buf[0]= '\0';
99     rem = BUFFER_SIZE;
100
101     if (mask) {
102         for (i = 0, bit = 1; i < XkbNumVirtualMods && rem > 1; i++, bit <<= 1)
103         {
104             if (!(mask & bit))
105                 continue;
106
107             len = snprintf(str, rem, "%s%s",
108                            (str != buf) ? "+" : "",
109                            XkbcVModIndexText(xkb, i));
110             rem -= len;
111             str += len;
112         }
113
114         str = buf;
115     }
116     else
117         str = NULL;
118
119     len = ((str) ? strlen(str) : 0) + ((mm) ? strlen(mm) : 0) +
120           ((str && mm) ? 1 : 0);
121     if (len >= BUFFER_SIZE)
122         len = BUFFER_SIZE - 1;
123
124     rtrn = tbGetBuffer(len + 1);
125     rtrn[0] = '\0';
126
127     snprintf(rtrn, len + 1, "%s%s%s", (mm) ? mm : "",
128              (mm && str) ? "+" : "", (str) ? str : "");
129
130     return rtrn;
131 }
132
133 static const char *modNames[XkbNumModifiers] = {
134     "Shift",
135     "Lock",
136     "Control",
137     "Mod1",
138     "Mod2",
139     "Mod3",
140     "Mod4",
141     "Mod5"
142 };
143
144 const char *
145 XkbcModIndexText(unsigned ndx)
146 {
147     char *buf;
148
149     if (ndx < XkbNumModifiers)
150         return modNames[ndx];
151     else if (ndx == XkbNoModifier)
152         return "none";
153
154     buf = tbGetBuffer(32);
155     snprintf(buf, 32, "ILLEGAL_%02x", ndx);
156
157     return buf;
158 }
159
160 const char *
161 XkbcModMaskText(unsigned mask, bool cFormat)
162 {
163     int i, rem, bit;
164     char *str, *buf;
165
166     if ((mask & 0xff) == 0xff)
167         return (cFormat ? "0xff" : "all");
168
169     if ((mask & 0xff) == 0)
170         return (cFormat ? "0" : "none");
171
172     rem = 64;
173     buf = tbGetBuffer(rem);
174     str = buf;
175     buf[0] = '\0';
176     for (i = 0, bit = 1; i < XkbNumModifiers && rem > 1; i++, bit <<= 1) {
177         int len;
178
179         if (!(mask & bit))
180             continue;
181
182         len = snprintf(str, rem, "%s%s%s",
183                        (str != buf) ? (cFormat ? "|" : "+") : "",
184                        modNames[i],
185                        cFormat ? "Mask" : "");
186         rem -= len;
187         str += len;
188     }
189
190     return buf;
191 }
192
193 const char *
194 XkbcConfigText(unsigned config)
195 {
196     switch (config) {
197     case XkmSemanticsFile:
198         return "Semantics";
199     case XkmLayoutFile:
200         return "Layout";
201     case XkmKeymapFile:
202         return "Keymap";
203     case XkmTypesIndex:
204         return "Types";
205     case XkmCompatMapIndex:
206         return "CompatMap";
207     case XkmSymbolsIndex:
208         return "Symbols";
209     case XkmIndicatorsIndex:
210         return "Indicators";
211     case XkmKeyNamesIndex:
212         return "KeyNames";
213     case XkmVirtualModsIndex:
214         return "VirtualMods";
215     default:
216         return "unknown";
217     }
218 }
219
220 static const char *actionTypeNames[XkbSA_NumActions]= {
221     "NoAction",         /* XkbSA_NoAction */
222     "SetMods",          /* XkbSA_SetMods */
223     "LatchMods",        /* XkbSA_LatchMods */
224     "LockMods",         /* XkbSA_LockMods */
225     "SetGroup",         /* XkbSA_SetGroup */
226     "LatchGroup",       /* XkbSA_LatchGroup */
227     "LockGroup",        /* XkbSA_LockGroup */
228     "MovePtr",          /* XkbSA_MovePtr */
229     "PtrBtn",           /* XkbSA_PtrBtn */
230     "LockPtrBtn",       /* XkbSA_LockPtrBtn */
231     "SetPtrDflt",       /* XkbSA_SetPtrDflt */
232     "ISOLock",          /* XkbSA_ISOLock */
233     "Terminate",        /* XkbSA_Terminate */
234     "SwitchScreen",     /* XkbSA_SwitchScreen */
235     "SetControls",      /* XkbSA_SetControls */
236     "LockControls",     /* XkbSA_LockControls */
237     "ActionMessage",    /* XkbSA_ActionMessage */
238     "RedirectKey",      /* XkbSA_RedirectKey */
239     "DeviceBtn",        /* XkbSA_DeviceBtn */
240     "LockDeviceBtn",    /* XkbSA_LockDeviceBtn */
241     "DeviceValuator"    /* XkbSA_DeviceValuator */
242 };
243
244 const char *
245 XkbcActionTypeText(unsigned type)
246 {
247     if (type <= XkbSA_LastAction)
248         return actionTypeNames[type];
249     return "Private";
250 }
251
252 const char *
253 XkbcKeysymText(xkb_keysym_t sym)
254 {
255     static char buffer[16];
256
257     xkb_keysym_to_string(sym, buffer, sizeof buffer);
258
259     return buffer;
260 }
261
262 const char *
263 XkbcKeyNameText(char *name)
264 {
265     char *buf;
266     int len;
267
268     buf = tbGetBuffer(7);
269     buf[0] = '<';
270     strncpy(&buf[1], name, 4);
271     buf[5] = '\0';
272     len = strlen(buf);
273     buf[len++] = '>';
274     buf[len] = '\0';
275
276     return buf;
277 }
278
279 static const char *siMatchText[5] = {
280     "NoneOf",       /* XkbSI_NoneOf */
281     "AnyOfOrNone",  /* XkbSI_AnyOfOrNone */
282     "AnyOf",        /* XkbSI_AnyOf */
283     "AllOf",        /* XkbSI_AllOf */
284     "Exactly"       /* XkbSI_Exactly */
285 };
286
287 const char *
288 XkbcSIMatchText(unsigned type)
289 {
290     char *buf;
291
292     switch (type & XkbSI_OpMask) {
293     case XkbSI_NoneOf:
294         return siMatchText[0];
295     case XkbSI_AnyOfOrNone:
296         return siMatchText[1];
297     case XkbSI_AnyOf:
298         return siMatchText[2];
299     case XkbSI_AllOf:
300         return siMatchText[3];
301     case XkbSI_Exactly:
302         return siMatchText[4];
303     default:
304         buf = tbGetBuffer(40);
305         snprintf(buf, 40, "0x%x", type & XkbSI_OpMask);
306         return buf;
307     }
308 }