Remove unused stuff from XKBcommonint.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 XkmKeyNamesIndex:
210         return "KeyNames";
211     default:
212         return "unknown";
213     }
214 }
215
216 static const char *actionTypeNames[XkbSA_NumActions]= {
217     "NoAction",         /* XkbSA_NoAction */
218     "SetMods",          /* XkbSA_SetMods */
219     "LatchMods",        /* XkbSA_LatchMods */
220     "LockMods",         /* XkbSA_LockMods */
221     "SetGroup",         /* XkbSA_SetGroup */
222     "LatchGroup",       /* XkbSA_LatchGroup */
223     "LockGroup",        /* XkbSA_LockGroup */
224     "MovePtr",          /* XkbSA_MovePtr */
225     "PtrBtn",           /* XkbSA_PtrBtn */
226     "LockPtrBtn",       /* XkbSA_LockPtrBtn */
227     "SetPtrDflt",       /* XkbSA_SetPtrDflt */
228     "ISOLock",          /* XkbSA_ISOLock */
229     "Terminate",        /* XkbSA_Terminate */
230     "SwitchScreen",     /* XkbSA_SwitchScreen */
231     "SetControls",      /* XkbSA_SetControls */
232     "LockControls",     /* XkbSA_LockControls */
233     "ActionMessage",    /* XkbSA_ActionMessage */
234     "RedirectKey",      /* XkbSA_RedirectKey */
235     "DeviceBtn",        /* XkbSA_DeviceBtn */
236     "LockDeviceBtn",    /* XkbSA_LockDeviceBtn */
237     "DeviceValuator"    /* XkbSA_DeviceValuator */
238 };
239
240 const char *
241 XkbcActionTypeText(unsigned type)
242 {
243     if (type <= XkbSA_LastAction)
244         return actionTypeNames[type];
245     return "Private";
246 }
247
248 const char *
249 XkbcKeysymText(xkb_keysym_t sym)
250 {
251     static char buffer[16];
252
253     xkb_keysym_to_string(sym, buffer, sizeof buffer);
254
255     return buffer;
256 }
257
258 const char *
259 XkbcKeyNameText(char *name)
260 {
261     char *buf;
262     int len;
263
264     buf = tbGetBuffer(7);
265     buf[0] = '<';
266     strncpy(&buf[1], name, 4);
267     buf[5] = '\0';
268     len = strlen(buf);
269     buf[len++] = '>';
270     buf[len] = '\0';
271
272     return buf;
273 }
274
275 static const char *siMatchText[5] = {
276     "NoneOf",       /* XkbSI_NoneOf */
277     "AnyOfOrNone",  /* XkbSI_AnyOfOrNone */
278     "AnyOf",        /* XkbSI_AnyOf */
279     "AllOf",        /* XkbSI_AllOf */
280     "Exactly"       /* XkbSI_Exactly */
281 };
282
283 const char *
284 XkbcSIMatchText(unsigned type)
285 {
286     char *buf;
287
288     switch (type & XkbSI_OpMask) {
289     case XkbSI_NoneOf:
290         return siMatchText[0];
291     case XkbSI_AnyOfOrNone:
292         return siMatchText[1];
293     case XkbSI_AnyOf:
294         return siMatchText[2];
295     case XkbSI_AllOf:
296         return siMatchText[3];
297     case XkbSI_Exactly:
298         return siMatchText[4];
299     default:
300         buf = tbGetBuffer(40);
301         snprintf(buf, 40, "0x%x", type & XkbSI_OpMask);
302         return buf;
303     }
304 }