]> cvs.zerfleddert.de Git - micropolis/blob - src/tk/tkerror.c
Fixes for compilation with gcc 15
[micropolis] / src / tk / tkerror.c
1 /*
2 * tkError.c --
3 *
4 * This file provides a high-performance mechanism for
5 * selectively dealing with errors that occur in talking
6 * to the X server. This is useful, for example, when
7 * communicating with a window that may not exist.
8 *
9 * Copyright 1990 Regents of the University of California.
10 * Permission to use, copy, modify, and distribute this
11 * software and its documentation for any purpose and without
12 * fee is hereby granted, provided that the above copyright
13 * notice appear in all copies. The University of California
14 * makes no representations about the suitability of this
15 * software for any purpose. It is provided "as is" without
16 * express or implied warranty.
17 */
18
19 #ifndef lint
20 static char rcsid[] = "$Header: /user6/ouster/wish/RCS/tkError.c,v 1.10 92/04/12 17:02:08 ouster Exp $ SPRITE (Berkeley)";
21 #endif
22
23 #include "tkconfig.h"
24 #include "tkint.h"
25
26 static int initialized = 0;
27
28 /*
29 * Forward references to procedures declared later in this file:
30 */
31
32 static int ErrorProc _ANSI_ARGS_((Display *display,
33 XErrorEvent *errEventPtr));
34 \f
35 /*
36 *--------------------------------------------------------------
37 *
38 * Tk_CreateErrorHandler --
39 *
40 * Arrange for all a given procedure to be invoked whenever
41 * certain errors occur.
42 *
43 * Results:
44 * The return value is a token identifying the handler;
45 * it must be passed to Tk_DeleteErrorHandler to delete the
46 * handler.
47 *
48 * Side effects:
49 * If an X error occurs that matches the error, request,
50 * and minor arguments, then errorProc will be invoked.
51 * ErrorProc should have the following structure:
52 *
53 * int
54 * errorProc(clientData, errorEventPtr)
55 * caddr_t clientData;
56 * XErrorEvent *errorEventPtr;
57 * {
58 * }
59 *
60 * The clientData argument will be the same as the clientData
61 * argument to this procedure, and errorEvent will describe
62 * the error. If errorProc returns 0, it means that it
63 * completely "handled" the error: no further processing
64 * should be done. If errorProc returns 1, it means that it
65 * didn't know how to deal with the error, so we should look
66 * for other error handlers, or invoke the default error
67 * handler if no other handler returns zero. Handlers are
68 * invoked in order of age: youngest handler first.
69 *
70 * Note: errorProc will only be called for errors associated
71 * with X requests made AFTER this call, but BEFORE the handler
72 * is deleted by calling Tk_DeleteErrorHandler.
73 *
74 *--------------------------------------------------------------
75 */
76
77 Tk_ErrorHandler
78 Tk_CreateErrorHandler(
79 Display *display, /* Display for which to handle
80 * errors. */
81 int error, /* Consider only errors with this
82 * error_code (-1 means consider
83 * all errors). */
84 int request, /* Consider only errors with this
85 * major request code (-1 means
86 * consider all major codes). */
87 int minorCode, /* Consider only errors with this
88 * minor request code (-1 means
89 * consider all minor codes). */
90 Tk_ErrorProc *errorProc, /* Procedure to invoke when a
91 * matching error occurs. NULL means
92 * just ignore matching errors. */
93 ClientData clientData /* Arbitrary value to pass to
94 * errorProc. */
95 )
96 {
97 register TkErrorHandler *errorPtr;
98 register TkDisplay *dispPtr;
99
100 /*
101 * Make sure that X calls us whenever errors occur.
102 */
103
104 if (!initialized) {
105 XSetErrorHandler(ErrorProc);
106 initialized = 1;
107 }
108
109 /*
110 * Find the display. If Tk doesn't know about this display,
111 * it's an error: panic.
112 */
113
114 for (dispPtr = tkDisplayList; ; dispPtr = dispPtr->nextPtr) {
115 if (dispPtr->display == display) {
116 break;
117 }
118 if (dispPtr == NULL) {
119 panic("Unknown display passed to Tk_CreateErrorHandler");
120 }
121 }
122
123 /*
124 * Create the handler record.
125 */
126
127 errorPtr = (TkErrorHandler *) ckalloc(sizeof(TkErrorHandler));
128 errorPtr->dispPtr = dispPtr;
129 errorPtr->firstRequest = NextRequest(display);
130 errorPtr->lastRequest = -1;
131 errorPtr->error = error;
132 errorPtr->request = request;
133 errorPtr->minorCode = minorCode;
134 errorPtr->errorProc = errorProc;
135 errorPtr->clientData = clientData;
136 errorPtr->nextPtr = dispPtr->errorPtr;
137 dispPtr->errorPtr = errorPtr;
138
139 return (Tk_ErrorHandler) errorPtr;
140 }
141 \f
142 /*
143 *--------------------------------------------------------------
144 *
145 * Tk_DeleteErrorHandler --
146 *
147 * Do not use an error handler anymore.
148 *
149 * Results:
150 * None.
151 *
152 * Side effects:
153 * The handler denoted by the "handler" argument will not
154 * be invoked for any X errors associated with requests
155 * made after this call. However, if errors arrive later
156 * for requests made BEFORE this call, then the handler
157 * will still be invoked. Call XSync if you want to be
158 * sure that all outstanding errors have been received
159 * and processed.
160 *
161 *--------------------------------------------------------------
162 */
163
164 void
165 Tk_DeleteErrorHandler(
166 Tk_ErrorHandler handler /* Token for handler to delete;
167 * was previous return value from
168 * Tk_CreateErrorHandler. */
169 )
170 {
171 register TkErrorHandler *errorPtr = (TkErrorHandler *) handler;
172 register TkDisplay *dispPtr = errorPtr->dispPtr;
173
174 errorPtr->lastRequest = NextRequest(dispPtr->display) - 1;
175
176 /*
177 * Every once-in-a-while, cleanup handlers that are no longer
178 * active. We probably won't be able to free the handler that
179 * was just deleted (need to wait for any outstanding requests to
180 * be processed by server), but there may be previously-deleted
181 * handlers that are now ready for garbage collection. To reduce
182 * the cost of the cleanup, let a few dead handlers pile up, then
183 * clean them all at once. This adds a bit of overhead to errors
184 * that might occur while the dead handlers are hanging around,
185 * but reduces the overhead of scanning the list to clean up
186 * (particularly if there are many handlers that stay around
187 * forever).
188 */
189
190 dispPtr->deleteCount += 1;
191 if (dispPtr->deleteCount >= 10) {
192 register TkErrorHandler *prevPtr;
193 TkErrorHandler *nextPtr;
194 int lastSerial;
195
196 dispPtr->deleteCount = 0;
197 lastSerial = LastKnownRequestProcessed(dispPtr->display);
198 errorPtr = dispPtr->errorPtr;
199 for (errorPtr = dispPtr->errorPtr, prevPtr = NULL;
200 errorPtr != NULL; errorPtr = nextPtr) {
201 nextPtr = errorPtr->nextPtr;
202 if ((errorPtr->lastRequest != -1)
203 && (errorPtr->lastRequest <= lastSerial)) {
204 if (prevPtr == NULL) {
205 dispPtr->errorPtr = nextPtr;
206 } else {
207 prevPtr->nextPtr = nextPtr;
208 }
209 ckfree((char *) errorPtr);
210 continue;
211 }
212 prevPtr = errorPtr;
213 }
214 }
215 }
216 \f
217 /*
218 *--------------------------------------------------------------
219 *
220 * ErrorProc --
221 *
222 * This procedure is invoked by the X system when error
223 * events arrive.
224 *
225 * Results:
226 * If it returns, the return value is zero. However,
227 * it is possible that one of the error handlers may
228 * just exit.
229 *
230 * Side effects:
231 * This procedure does two things. First, it uses the
232 * serial # in the error event to eliminate handlers whose
233 * expiration serials are now in the past. Second, it
234 * invokes any handlers that want to deal with the error.
235 *
236 *--------------------------------------------------------------
237 */
238
239 static int
240 ErrorProc (
241 Display *display, /* Display for which error
242 * occurred. */
243 register XErrorEvent *errEventPtr /* Information about error. */
244 )
245 {
246 register TkDisplay *dispPtr;
247 register TkErrorHandler *errorPtr;
248 extern int _XDefaultError(Display *, XErrorEvent *);
249
250 /*
251 * See if we know anything about the display. If not, then
252 * invoke the default error handler.
253 */
254
255 for (dispPtr = tkDisplayList; ; dispPtr = dispPtr->nextPtr) {
256 if (dispPtr == NULL) {
257 goto couldntHandle;
258 }
259 if (dispPtr->display == display) {
260 break;
261 }
262 }
263
264 /*
265 * Otherwise invoke any relevant handlers for the error, in order.
266 */
267
268 for (errorPtr = dispPtr->errorPtr; errorPtr != NULL;
269 errorPtr = errorPtr->nextPtr) {
270 if ((errorPtr->firstRequest > errEventPtr->serial)
271 || ((errorPtr->error != -1)
272 && (errorPtr->error != errEventPtr->error_code))
273 || ((errorPtr->request != -1)
274 && (errorPtr->request != errEventPtr->request_code))
275 || ((errorPtr->minorCode != -1)
276 && (errorPtr->minorCode != errEventPtr->minor_code))
277 || ((errorPtr->lastRequest != -1)
278 && (errorPtr->lastRequest < errEventPtr->serial))) {
279 continue;
280 }
281 if (errorPtr->errorProc == NULL) {
282 return 0;
283 } else {
284 if ((*errorPtr->errorProc)(errorPtr->clientData,
285 errEventPtr) == 0) {
286 return 0;
287 }
288 }
289 }
290
291 /*
292 * We couldn't handle the error. Use the default handler.
293 */
294
295 couldntHandle:
296 return _XDefaultError(display, errEventPtr);
297 }
Impressum, Datenschutz