Restructure how we look for Read files slightly.
[fvwm.git] / libs / XError.c
blob2ec0c13d5d84969af9b83f131c65189a21fd27f2
1 /* -*-c-*- */
2 /* This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * This function should be used by all modules and fvwm when a X error
19 * occurs and the module exits.
22 #include "config.h"
24 #include <stdio.h>
25 #include <X11/Xlib.h>
26 #include <fvwmlib.h>
27 #include "FRenderInit.h"
28 #include "XError.h"
29 #undef XSetErrorHandler
31 #define USE_GET_ERROR_TEXT 1
32 #ifndef USE_GET_ERROR_TEXT
33 static char *error_name(unsigned char code);
34 #endif
35 static char *request_name(unsigned char code);
36 static char unknown[32];
38 void do_coredump(void)
40 fprintf(stderr, " Leaving a core dump now\n");
42 char *nullp;
44 nullp = NULL;
46 *nullp = 99;
48 /* exit if this fails */
49 exit(99);
52 #define USE_GET_ERROR_TEXT 1
53 void PrintXErrorAndCoredump(Display *dpy, XErrorEvent *error, char *MyName)
55 char msg[256];
56 Bool suc = False;
58 msg[255] = 0;
59 #ifdef USE_GET_ERROR_TEXT
60 /* can't call this from within an error handler! */
61 /* DV (21-Nov-2000): Well, actually we *can* call it in an error
62 * handler since it does not trigger a protocol request. */
63 if (error->error_code >= FirstExtensionError)
65 suc = FRenderGetErrorText(error->error_code, msg);
67 if (!suc)
68 XGetErrorText(dpy, error->error_code, msg, sizeof(msg));
69 fprintf(stderr,"%s: Cause of next X Error.\n", MyName);
70 fprintf(stderr, " Error: %d (%s)\n", error->error_code, msg);
71 #else
72 fprintf(stderr,"%s: Cause of next X Error.\n", MyName);
73 if (error->error_code >= FirstExtensionError)
75 suc = FRenderGetErrorText(error->error_code, msg);
77 if (suc)
78 fprintf(stderr, " Error: %d (%s)\n",
79 error->error_code, msg);
80 else
81 fprintf(stderr, " Error: %d (%s)\n",
82 error->error_code, error_name(error->error_code));
83 #endif
84 fprintf(stderr, " Major opcode of failed request: %d (%s)\n",
85 error->request_code, request_name(error->request_code));
86 fprintf(stderr, " Minor opcode of failed request: %d \n",
87 error->minor_code);
88 /* error->resourceid may be uninitialised. This is no proble since we
89 * are dumping core anyway. */
90 fprintf(stderr, " Resource id of failed request: 0x%lx \n",
91 error->resourceid);
93 /* leave a coredump */
94 do_coredump();
97 #ifndef USE_GET_ERROR_TEXT
98 /* this comes out of X.h */
99 static char *error_names[] = {
100 "BadRequest",
101 "BadValue",
102 "BadWindow",
103 "BadPixmap",
104 "BadAtom",
105 "BadCursor",
106 "BadFont",
107 "BadMatch",
108 "BadDrawable",
109 "BadAccess",
110 "BadAlloc",
111 "BadColor",
112 "BadGC",
113 "BadIDChoice",
114 "BadName",
115 "BadLength",
116 "BadImplementation",
119 static char *error_name(unsigned char code)
121 if (code == 0 || code > (sizeof(error_names) / sizeof(char *)))
123 sprintf(unknown, "Unknown: %d", (int)code);
124 return unknown;
126 return error_names[code - 1];
128 #endif
130 /* this comes out of Xproto.h */
131 static char *code_names[] = {
132 "CreateWindow",
133 "ChangeWindowAttributes",
134 "GetWindowAttributes",
135 "DestroyWindow",
136 "DestroySubwindows",
137 "ChangeSaveSet",
138 "ReparentWindow",
139 "MapWindow",
140 "MapSubwindows",
141 "UnmapWindow",
142 "UnmapSubwindows",
143 "ConfigureWindow",
144 "CirculateWindow",
145 "GetGeometry",
146 "QueryTree",
147 "InternAtom",
148 "GetAtomName",
149 "ChangeProperty",
150 "DeleteProperty",
151 "GetProperty",
152 "ListProperties",
153 "SetSelectionOwner",
154 "GetSelectionOwner",
155 "ConvertSelection",
156 "SendEvent",
157 "GrabPointer",
158 "UngrabPointer",
159 "GrabButton",
160 "UngrabButton",
161 "ChangeActivePointerGrab",
162 "GrabKeyboard",
163 "UngrabKeyboard",
164 "GrabKey",
165 "UngrabKey",
166 "AllowEvents",
167 "GrabServer",
168 "UngrabServer",
169 "QueryPointer",
170 "GetMotionEvents",
171 "TranslateCoords",
172 "WarpPointer",
173 "SetInputFocus",
174 "GetInputFocus",
175 "QueryKeymap",
176 "OpenFont",
177 "CloseFont",
178 "QueryFont",
179 "QueryTextExtents",
180 "ListFonts",
181 "ListFontsWithInfo",
182 "SetFontPath",
183 "GetFontPath",
184 "CreatePixmap",
185 "FreePixmap",
186 "CreateGC",
187 "ChangeGC",
188 "CopyGC",
189 "SetDashes",
190 "SetClipRectangles",
191 "FreeGC",
192 "ClearArea",
193 "CopyArea",
194 "CopyPlane",
195 "PolyPoint",
196 "PolyLine",
197 "PolySegment",
198 "PolyRectangle",
199 "PolyArc",
200 "FillPoly",
201 "PolyFillRectangle",
202 "PolyFillArc",
203 "PutImage",
204 "GetImage",
205 "PolyText",
206 "PolyText1",
207 "ImageText",
208 "ImageText1",
209 "CreateColormap",
210 "FreeColormap",
211 "CopyColormapAndFree",
212 "InstallColormap",
213 "UninstallColormap",
214 "ListInstalledColormaps",
215 "AllocColor",
216 "AllocNamedColor",
217 "AllocColorCells",
218 "AllocColorPlanes",
219 "FreeColors",
220 "StoreColors",
221 "StoreNamedColor",
222 "QueryColors",
223 "LookupColor",
224 "CreateCursor",
225 "CreateGlyphCursor",
226 "FreeCursor",
227 "RecolorCursor",
228 "QueryBestSize",
229 "QueryExtension",
230 "ListExtensions",
231 "ChangeKeyboardMapping",
232 "GetKeyboardMapping",
233 "ChangeKeyboardControl",
234 "GetKeyboardControl",
235 "Bell",
236 "ChangePointerControl",
237 "GetPointerControl",
238 "SetScreenSaver",
239 "GetScreenSaver",
240 "ChangeHosts",
241 "ListHosts",
242 "SetAccessControl",
243 "SetCloseDownMode",
244 "KillClient",
245 "RotateProperties",
246 "ForceScreenSaver",
247 "SetPointerMapping",
248 "GetPointerMapping",
249 "SetModifierMapping",
250 "GetModifierMapping",
253 static char *request_name(unsigned char code)
255 if (code == 0 || code > (sizeof(code_names) / sizeof(char *)))
257 if (code == FRenderGetMajorOpCode())
259 sprintf(unknown, "XRender");
261 else
263 sprintf(unknown, "Unknown: %d", (int)code);
265 return unknown;
267 return code_names[code - 1];
270 /* -------------------------- error handler stack -------------------------- */
272 static ferror_handler_t old_handler = NULL;
274 void ferror_set_temp_error_handler(ferror_handler_t new_handler)
276 if (old_handler != NULL)
278 do_coredump();
280 old_handler = XSetErrorHandler(old_handler);
282 return;
285 void ferror_reset_temp_error_handler(void)
287 if (old_handler == NULL)
289 do_coredump();
291 XSetErrorHandler(old_handler);
292 old_handler = NULL;
294 return;
297 int ferror_call_next_error_handler(Display *dpy, XErrorEvent *error)
299 int rc;
301 if (old_handler == NULL)
303 do_coredump();
305 rc = old_handler(dpy, error);
307 return rc;