File: asyncerr.c

package info (click to toggle)
kinput2 3.1-14
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 3,284 kB
  • sloc: ansic: 49,965; makefile: 104; sh: 68
file content (275 lines) | stat: -rw-r--r-- 5,578 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#ifndef lint
static char *rcsid = "$Id: asyncerr.c,v 1.6 1994/08/08 01:20:52 ishisone Exp $";
#endif
/*
 * Copyright (C) 1992, 1994  Software Research Associates, Inc.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Software Research Associates not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  Software Research
 * Associates makes no representations about the suitability of this software
 * for any purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * Author:  Makoto Ishisone, Software Research Associates, Inc., Japan
 */

/*
 *	X asyncronous error handler
 */

#include <X11/Xlib.h>
#include <X11/Xfuncproto.h>
#include "AsyncErr.h"

#ifdef __STDC__
#include <stdlib.h>
#else
extern char *malloc();
#endif

#ifndef NULL
#define NULL 0
#endif

#undef XSetErrorHandler

typedef struct fe_errdesc_ {
    struct fe_errdesc_ *prev;
    struct fe_errdesc_ *next;
    Display *dpy;
    unsigned long from;		/* range of the sequence numbers */
    unsigned long to;		/* which this handler covers */
    int (*handler)();		/* async handler */
    void (*destroy)();
    XPointer client_data;
} ErrDesc;

static ErrDesc esentinel = {
    &esentinel,
    &esentinel,
};

#define EHEAD	(esentinel.next)
#define ETAIL	(esentinel.prev)
#define EEND(p)	((p) == &esentinel)

static int (*original_handler)();

/*
 * Some useful handlers
 */

/* ARGSUSED */
static int
ignoreErrors(dpy, eev, cldata)
Display *dpy;
XErrorEvent *eev;
XPointer cldata;
{
    /*
     * Just ignore any errors.
     */
    return 0;
}

/* ARGSUSED */
static int
recordErrors(dpy, eev, cldata)
Display *dpy;
XErrorEvent *eev;
XPointer cldata;
{
    /*
     * Record the type of the error.
     * It assumes that the client data is a pointer to
     * an unsigned long int.  If error occurs, it does:
     *   + if the error code is less than 32, set the 'error code'th bit.
     *   + otherwise (i.e. extension defined error) set the bit 0.
     */
    unsigned long *errorbitsp = (unsigned long *)cldata;

    if (eev->error_code < 32) {
	*errorbitsp |= (1 << eev->error_code);
    } else {
	/* other errors */
	*errorbitsp |= 1;
    }

    /* don't invoke global handler */
    return 0;
}

static ErrDesc *
newErrDesc()
{
    return (ErrDesc *)malloc(sizeof(ErrDesc));
}

static void
eremove(edp)
ErrDesc *edp;
{
    edp->prev->next = edp->next;
    edp->next->prev = edp->prev;
}

static void
eappend(edp)
ErrDesc *edp;
{
    edp->prev = esentinel.prev;
    edp->next = &esentinel;
    (esentinel.prev)->next = edp;
    esentinel.prev = edp;
}

static void
removeHandler(edp)
ErrDesc *edp;
{
    if (edp->destroy != NULL) (*edp->destroy)(edp->dpy, edp->client_data);
    eremove(edp);
    (void)free((char *)edp);
}

static int
callHandlers(dpy, eev)
Display *dpy;
XErrorEvent *eev;
{
    ErrDesc *edp = EHEAD;
    int found = 0;
    int call_original = 0;

    while (!EEND(edp)) {
	if (edp->dpy == dpy && edp->from <= eev->serial &&
	    (eev->serial < edp->to || edp->from == edp->to)) {
	    found = 1;
	    if ((*edp->handler)(dpy, eev, edp->client_data)) call_original = 1;
	}
	edp = edp->next;
    }
    return !found || call_original;
}

static void
removeHandlers(dpy)
Display *dpy;
{
    /*
     * Remove obsolete (out of date) handlers.
     */
    ErrDesc *edp = EHEAD;
    unsigned long last = LastKnownRequestProcessed(dpy);

    while (!EEND(edp)) {
	ErrDesc *next = edp->next;

	if (edp->dpy == dpy && edp->to <= last && edp->from != edp->to) {
	    removeHandler(edp);
	}
	edp = next;
    }
}

/*
 * public functions
 */

int
XAEHandler(dpy, eev)
Display *dpy;
XErrorEvent *eev;
{
    if (callHandlers(dpy, eev) && original_handler != NULL) {
	(void)original_handler(dpy, eev);
    }
    removeHandlers(dpy);
    return 0;	/* for lint */
}

void
XAEInit()
{
    int (*oldhandler)() = XSetErrorHandler(XAEHandler);

    if (oldhandler != XAEHandler) original_handler = oldhandler;
}

XErrorHandler
XAESetErrorHandler(handler)
XErrorHandler handler;
{
    int (*oldhandler)();

    oldhandler = original_handler;
    original_handler = handler;
    return oldhandler;
}

XAEHandle
XAESet(dpy, handler, destroy, client_data)
Display *dpy;
int (*handler)();
void (*destroy)();
XPointer client_data;
{
    ErrDesc *e;

    /*
     * First, remove out-of-date handlers.
     */
    removeHandlers(dpy);

    /*
     * Allocate new ErrDesc structure.
     */
    e = newErrDesc();
    if (e == NULL) return NULL;

    e->dpy = dpy;
    e->from = NextRequest(dpy);
    e->to = e->from;
    e->handler = handler;
    e->destroy = destroy;
    e->client_data = client_data;

    eappend(e);

    return e;
}

void
XAEUnset(handle)
XAEHandle handle;
{
    Display *dpy = handle->dpy;

    if (handle != NULL && handle->from == handle->to) {
	handle->to = NextRequest(dpy);
	if (handle->to <= handle->from) removeHandler(handle);
    }
    removeHandlers(dpy);
}

XAEHandle
XAESetIgnoreErrors(dpy)
Display *dpy;
{
    return XAESet(dpy, ignoreErrors, (void (*)())NULL, (XPointer)NULL);
}

XAEHandle
XAESetRecordErrors(dpy, errorbitsp)
Display *dpy;
unsigned long *errorbitsp;
{
    *errorbitsp = 0L;		/* clear all bits */
    return XAESet(dpy, recordErrors,
		  (void (*)())NULL, (XPointer)errorbitsp);
}