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