]>
cvs.zerfleddert.de Git - micropolis/blob - src/tk/tkerror.c
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.
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.
20 static char rcsid
[] = "$Header: /user6/ouster/wish/RCS/tkError.c,v 1.10 92/04/12 17:02:08 ouster Exp $ SPRITE (Berkeley)";
26 static int initialized
= 0;
29 * Forward references to procedures declared later in this file:
32 static int ErrorProc
_ANSI_ARGS_((Display
*display
,
33 XErrorEvent
*errEventPtr
));
36 *--------------------------------------------------------------
38 * Tk_CreateErrorHandler --
40 * Arrange for all a given procedure to be invoked whenever
41 * certain errors occur.
44 * The return value is a token identifying the handler;
45 * it must be passed to Tk_DeleteErrorHandler to delete the
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:
54 * errorProc(clientData, errorEventPtr)
56 * XErrorEvent *errorEventPtr;
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.
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.
74 *--------------------------------------------------------------
78 Tk_CreateErrorHandler(
79 Display
*display
, /* Display for which to handle
81 int error
, /* Consider only errors with this
82 * error_code (-1 means consider
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
97 register TkErrorHandler
*errorPtr
;
98 register TkDisplay
*dispPtr
;
101 * Make sure that X calls us whenever errors occur.
105 XSetErrorHandler(ErrorProc
);
110 * Find the display. If Tk doesn't know about this display,
111 * it's an error: panic.
114 for (dispPtr
= tkDisplayList
; ; dispPtr
= dispPtr
->nextPtr
) {
115 if (dispPtr
->display
== display
) {
118 if (dispPtr
== NULL
) {
119 panic("Unknown display passed to Tk_CreateErrorHandler");
124 * Create the handler record.
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
;
139 return (Tk_ErrorHandler
) errorPtr
;
143 *--------------------------------------------------------------
145 * Tk_DeleteErrorHandler --
147 * Do not use an error handler anymore.
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
161 *--------------------------------------------------------------
165 Tk_DeleteErrorHandler(
166 Tk_ErrorHandler handler
/* Token for handler to delete;
167 * was previous return value from
168 * Tk_CreateErrorHandler. */
171 register TkErrorHandler
*errorPtr
= (TkErrorHandler
*) handler
;
172 register TkDisplay
*dispPtr
= errorPtr
->dispPtr
;
174 errorPtr
->lastRequest
= NextRequest(dispPtr
->display
) - 1;
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
190 dispPtr
->deleteCount
+= 1;
191 if (dispPtr
->deleteCount
>= 10) {
192 register TkErrorHandler
*prevPtr
;
193 TkErrorHandler
*nextPtr
;
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
;
207 prevPtr
->nextPtr
= nextPtr
;
209 ckfree((char *) errorPtr
);
218 *--------------------------------------------------------------
222 * This procedure is invoked by the X system when error
226 * If it returns, the return value is zero. However,
227 * it is possible that one of the error handlers may
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.
236 *--------------------------------------------------------------
241 Display
*display
, /* Display for which error
243 register XErrorEvent
*errEventPtr
/* Information about error. */
246 register TkDisplay
*dispPtr
;
247 register TkErrorHandler
*errorPtr
;
248 extern int _XDefaultError(Display
*, XErrorEvent
*);
251 * See if we know anything about the display. If not, then
252 * invoke the default error handler.
255 for (dispPtr
= tkDisplayList
; ; dispPtr
= dispPtr
->nextPtr
) {
256 if (dispPtr
== NULL
) {
259 if (dispPtr
->display
== display
) {
265 * Otherwise invoke any relevant handlers for the error, in order.
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
))) {
281 if (errorPtr
->errorProc
== NULL
) {
284 if ((*errorPtr
->errorProc
)(errorPtr
->clientData
,
292 * We couldn't handle the error. Use the default handler.
296 return _XDefaultError(display
, errEventPtr
);