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