1/* $XFree86: xc/lib/XTrap/XEStrMap.c,v 1.1 2001/11/02 23:29:27 dawes Exp $ */
2/*****************************************************************************
3Copyright 1987, 1988, 1989, 1990, 1991, 1992, 1994 by Digital Equipment Corp.,
4Maynard, MA
5
6Permission to use, copy, modify, and distribute this software and its
7documentation for any purpose and without fee is hereby granted,
8provided that the above copyright notice appear in all copies and that
9both that copyright notice and this permission notice appear in
10supporting documentation, and that the name of Digital not be
11used in advertising or publicity pertaining to distribution of the
12software without specific, written prior permission.
13
14DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
15ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
16DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
17ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
19ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20SOFTWARE.
21
22*****************************************************************************/
23#include <stdio.h>
24#include <X11/Xlib.h>
25#include <X11/extensions/xtraplib.h>
26#include <X11/extensions/xtraplibp.h>
27
28static char *eventName[] = {
29    "", "",
30    "KeyPress",         "KeyRelease",       "ButtonPress",
31    "ButtonRelease",    "MotionNotify",     "EnterNotify",
32    "LeaveNotify",      "FocusIn",          "FocusOut",
33    "KeymapNotify",     "Expose",           "GraphicsExpose",
34    "NoExpose",         "VisibilityNotify", "CreateNotify",
35    "DestroyNotify",    "UnmapNotify",      "MapNotify",
36    "MapRequest",       "ReparentNotify",   "ConfigureNotify",
37    "ConfigureRequest", "GravityNotify",    "ResizeRequest",
38    "CirculateNotify",  "CirculateRequest", "PropertyNotify",
39    "SelectionClear",   "SelectionRequest", "SelectionNotify",
40    "ColormapNotify",   "ClientMessage",    "MappingNotify",
41    "LASTEvent",
42};
43
44static char *requestName[] = {
45    "Invalid",
46    "X_CreateWindow",           "X_ChangeWindowAttributes",
47    "X_GetWindowAttributes",    "X_DestroyWindow",
48    "X_DestroySubwindows",      "X_ChangeSaveSet",
49    "X_ReparentWindow",         "X_MapWindow",
50    "X_MapSubwindows",          "X_UnmapWindow",
51    "X_UnmapSubwindows",        "X_ConfigureWindow",
52    "X_CirculateWindow",        "X_GetGeometry",
53    "X_QueryTree",              "X_InternAtom",
54    "X_GetAtomName",            "X_ChangeProperty",
55    "X_DeleteProperty",         "X_GetProperty",
56    "X_ListProperties",         "X_SetSelectionOwner",
57    "X_GetSelectionOwner",      "X_ConvertSelection",
58    "X_SendEvent",              "X_GrabPointer",
59    "X_UngrabPointer",          "X_GrabButton",
60    "X_UngrabButton",           "X_ChangeActivePointerGrab",
61    "X_GrabKeyboard",           "X_UngrabKeyboard",
62    "X_GrabKey",                "X_UngrabKey",
63    "X_AllowEvents",            "X_GrabServer",
64    "X_UngrabServer",           "X_QueryPointer",
65    "X_GetMotionEvents",        "X_TranslateCoords",
66    "X_WarpPointer",            "X_SetInputFocus",
67    "X_GetInputFocus",          "X_QueryKeymap",
68    "X_OpenFont",               "X_CloseFont",
69    "X_QueryFont",              "X_QueryTextExtents",
70    "X_ListFonts",              "X_ListFontsWithInfo",
71    "X_SetFontPath",            "X_GetFontPath",
72    "X_CreatePixmap",           "X_FreePixmap",
73    "X_CreateGC",               "X_ChangeGC",
74    "X_CopyGC",                 "X_SetDashes",
75    "X_SetClipRectangles",      "X_FreeGC",
76    "X_ClearArea",              "X_CopyArea",
77    "X_CopyPlane",              "X_PolyPoint",
78    "X_PolyLine",               "X_PolySegment",
79    "X_PolyRectangle",          "X_PolyArc",
80    "X_FillPoly",               "X_PolyFillRectangle",
81    "X_PolyFillArc",            "X_PutImage",
82    "X_GetImage",               "X_PolyText8",
83    "X_PolyText16",             "X_ImageText8",
84    "X_ImageText16",            "X_CreateColormap",
85    "X_FreeColormap",           "X_CopyColormapAndFree",
86    "X_InstallColormap",        "X_UninstallColormap",
87    "X_ListInstalledColormaps", "X_AllocColor",
88    "X_AllocNamedColor",        "X_AllocColorCells",
89    "X_AllocColorPlanes",       "X_FreeColors",
90    "X_StoreColors",            "X_StoreNamedColor",
91    "X_QueryColors",            "X_LookupColor",
92    "X_CreateCursor",           "X_CreateGlyphCursor",
93    "X_FreeCursor",             "X_RecolorCursorSize",
94    "X_QueryBestSize",          "X_QueryExtension",
95    "X_ListExtensions",         "X_ChangeKeyboardMapping",
96    "X_GetKeyboardMapping",     "X_ChangeKeyboardControl",
97    "X_GetKeyboardControl",     "X_Bell",
98    "X_ChangePointerControl",   "X_GetPointerControl",
99    "X_SetScreenSaver",         "X_GetScreenSaver",
100    "X_ChangeHosts",            "X_ListHosts",
101    "X_SetAccessControl",       "X_SetCloseDownMode",
102    "X_KillClient",             "X_RotateProperties",
103    "X_ForceScreenSaver",       "X_SetPointerMapping",
104    "X_GetPointerMapping",      "X_SetModifierMapping",
105    "X_GetModifierMapping",     "Invalid",
106    "Invalid",                  "Invalid",
107    "Invalid",                  "Invalid",
108    "Invalid",                  "Invalid",
109    "X_NoOperation",
110};
111
112typedef struct
113{
114    char *extName;
115    int  extEvent;
116}  _extensionData;
117static _extensionData *extensionData;
118static int numExtension = -1;
119
120static struct _pf_tbl {CARD32 id; char *str;} pf_tbl[] =
121{
122    {PF_Apollo,         "Apollo"},
123    {PF_ATT,            "ATT"},
124    {PF_Cray1,          "Cray1"},
125    {PF_Cray2,          "Cray2"},
126    {PF_DECUltrix,      "DECUltrix"},
127    {PF_DECVMS,         "DECVMS"},
128    {PF_DECELN,         "DECELN"},
129    {PF_DECOSF1,        "DECOSF1"},
130    {PF_DECVT1000,      "DECVT1000"},
131    {PF_DECXTerm,       "DECXTerm"},
132    {PF_HP9000s800,     "HP9000s800"},
133    {PF_HP9000s300,     "HP9000s300"},
134    {PF_IBMAT,          "IBMAT"},
135    {PF_IBMRT,          "IBMRT"},
136    {PF_IBMPS2,         "IBMPS2"},
137    {PF_IBMRS,          "IBMRS"},
138    {PF_MacII,          "MacII"},
139    {PF_Pegasus,        "Pegasus"},
140    {PF_SGI,            "SGI"},
141    {PF_Sony,           "Sony"},
142    {PF_Sun3,           "Sun3"},
143    {PF_Sun386i,        "Sun386i"},
144    {PF_SunSparc,       "SunSparc"},
145    {PF_Other,          "Other"}        /* always the last one! */
146};
147
148static char unknown[] = "unknown";
149
150#define ASize(array) (sizeof(array)/sizeof((array)[0]))
151
152static INT16 _StringToID(register char *match, register char **strings,
153    INT16 nstrings)
154{
155    register INT16 id = nstrings;
156
157    if (match && *match)
158    { while ((--id >= 0L) && (strcmp(match,strings[id]) != 0L)); }
159    else
160    { id = -1L; }
161
162    return(id);
163}
164static void loadExtStrings(XETC *tc)
165{
166    char **extensionName=XListExtensions(tc->dpy,&numExtension);
167    if (numExtension)
168    {
169        int i;
170        extensionData = (_extensionData *)XtCalloc(numExtension,
171                            sizeof(_extensionData));
172        for (i = 0; i < numExtension; i++)
173        {   /* Arrange extensions in opcode order */
174            int opcode,event,error;
175            if (XQueryExtension(tc->dpy,extensionName[i],&opcode,&event,
176                &error))
177            {
178                extensionData[opcode-128].extName = extensionName[i];
179                extensionData[opcode-128].extEvent = event;
180            }
181            else
182            {   /* This extension didn't load!  Error! */
183                extensionData[opcode-128].extName = "Invalid_Extension";
184            }
185        }
186        XFreeExtensionList(extensionName);
187    }
188}
189
190INT16 XEEventStringToID(register char *string)
191{
192    return(_StringToID(string,eventName,ASize(eventName)));
193}
194
195INT16 XERequestStringToID(register char *string)
196{
197    return(_StringToID(string,requestName,ASize(requestName)));
198}
199
200CARD32 XEPlatformStringToID(register char *string)
201{
202    struct _pf_tbl *ptr = &(pf_tbl[0]);
203    while(ptr->id != PF_Other)
204    {
205        if (!strncmp(ptr->str, string, strlen(ptr->str)))
206        {
207            return(ptr->id);
208        }
209        ptr++;
210    }
211    return((!strncmp(ptr->str,string,strlen(ptr->str))) ? ptr->id : -1L);
212}
213
214char *XEEventIDToString(register CARD8 id, XETC *tc)
215{
216    int i;
217    if (id < ASize(eventName))
218        return(eventName[id]);
219    /* either erroneous or an extension event */
220    if (numExtension < 0)
221    {   /*
222         * This is unfortunate, but necessary.  The client
223         * program has requested the string identifier for
224         * an extension request/event.  Since there's no Xlib
225         * equivalent for this, we have to query *all* the
226         * extensions looking for a match.  Chances are
227         * if a client wants one, it'll want them all,
228         * so just go through and initialize the extension
229         * list once.
230         */
231        loadExtStrings(tc);
232    }
233    /* Find id within extensionData */
234    for (i=0; i<numExtension; i++)
235    {
236        if (extensionData[i].extEvent == id)
237            return(extensionData[i].extName);
238    }
239    return(unknown);
240}
241
242char *XERequestIDToExtString(register CARD8 id, XETC *tc)
243{
244    int extid;
245
246    extid = id - ASize(requestName);
247
248    if (numExtension < 0)
249    {   /*
250         * This is unfortunate, but necessary.  The client
251         * program has requested the string identifier for
252         * an extension request/event.  Since there's no Xlib
253         * equivalent for this, we have to query *all* the
254         * extensions looking for a match.  Chances are
255         * if a client wants one, it'll want them all,
256         * so just go through and initialize the extension
257         * list once.
258         */
259        loadExtStrings(tc);
260    }
261    return((extid >=0 && extid < numExtension) ?
262        extensionData[extid].extName : unknown);
263}
264
265
266char *XERequestIDToString(register CARD8 id, XETC *tc)
267{
268    return((id < ASize(requestName)) ? requestName[id] :
269        XERequestIDToExtString(id,tc));
270}
271
272char *XEPlatformIDToString(register CARD32 id)
273{
274    struct _pf_tbl *ptr = &(pf_tbl[0]);
275    while((ptr->id != PF_Other) || (id == ptr->id))
276    {
277        if (id == ptr->id)
278        {
279            return(ptr->str);
280        }
281        ptr++;
282    }
283    return(unknown);
284}
285