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