File: dispatch.c

package info (click to toggle)
kinput2 3.1-10.2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,012 kB
  • ctags: 5,584
  • sloc: ansic: 49,959; makefile: 106; sh: 68
file content (268 lines) | stat: -rw-r--r-- 6,038 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
#ifndef lint
static char *rcsid = "$Id: dispatch.c,v 1.14 1994/05/31 07:48:42 ishisone Rel $";
#endif

/*
 * a very simple event dispatch library for non-widget windows
 *
 *	'non-widget window' means windows that have no associated widget,
 *	e.g. windows created by Xlib directly.
 */

/*
 * Copyright (c) 1990  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
 */

#include <X11/Intrinsic.h>
#include "MyDispatch.h"
#include "AsyncErr.h"

#define DEBUG_VAR debug_dispatch
#include "DebugPrint.h"

typedef struct _handler_ {
    int			type;		/* event type */
    unsigned long	mask;		/* event mask */
    void		(*handler)();
    XtPointer		data;
    struct _handler_	*next;
} HandlerRec;

typedef struct {
    Boolean		dispatching;	/* now dispatching */
    Boolean		toberemoved;	/* this list is to be removed later */
    unsigned long	mask;		/* event mask */
    HandlerRec		*handlers;
} WindowRec;

static int Initialized = 0;
static XContext Context;

static void
initialize()
{
    Context = XUniqueContext();
    Initialized = 1;
}

static void
resetEventMask(dpy, window, wp)
Display *dpy;
Window window;
WindowRec *wp;
{
    register HandlerRec *hp = wp->handlers;
    register unsigned long mask = 0L;

    while (hp != NULL) {
	mask |= hp->mask;
	hp = hp->next;
    }

    if (mask != wp->mask) {
	XAEHandle h = XAESetIgnoreErrors(dpy);	/* keep the operation safe */
	XSelectInput(dpy, window, mask);
	XAEUnset(h);
	wp->mask = mask;
    }
}

static void
removeAll(dpy, window, wp)
Display *dpy;
Window window;
WindowRec *wp;
{
    register HandlerRec *hp = wp->handlers;

    while (hp != NULL) {
	register HandlerRec *hp0 = hp;
	hp = hp->next;
	XtFree((char *)hp0);
    }

    if (wp->mask != 0L) {
	XAEHandle h = XAESetIgnoreErrors(dpy);

	/* keep it safe (because the window might not exist any more) */
	XSelectInput(dpy, window, 0L);
	XAEUnset(h);
    }

    XtFree((char *)wp);
    (void)XDeleteContext(dpy, window, Context);
}

static void
doDispatch(event, list)
XEvent *event;
register HandlerRec *list;
{
    void (*handler)();
    XtPointer data;
    register int type = event->type;

    /*
     * we must be careful here. the invoked handler might remove
     * itself, or remove other handler to be invoked next.
     * so we use this somewhat strange recursive call.
     */
    while (list != NULL) {
	if (list->type == type) {
	    handler = list->handler;
	    data = list->data;
	    doDispatch(event, list->next);
	    (*handler)(event, data);
	    return;
	}
	list = list->next;
    }
}

void
MyDispatchEvent(event)
XEvent *event;
{
    caddr_t data;

    if (!Initialized) initialize();

    if (!XFindContext(event->xany.display, event->xany.window,
		      Context, &data)) {
	WindowRec *wrec = (WindowRec *)data;

	wrec->dispatching = True;
	wrec->toberemoved = False;

	doDispatch(event, wrec->handlers);

	wrec->dispatching = False;
	if (wrec->toberemoved) {
	    removeAll(event->xany.display, event->xany.window, wrec);
	}
    }
}

void
MyAddEventHandler(dpy, window, type, mask, func, data)
Display *dpy;
Window window;
int type;
unsigned long mask;
void (*func)();
XtPointer data;
{
    WindowRec *wp;
    HandlerRec *hp;
    caddr_t cdata;

    TRACE(("MyAddEventHandler(window=%08lx,type=%d)\n", window, type));
    if (!Initialized) initialize();

    hp = XtNew(HandlerRec);
    hp->type = type;
    hp->mask = mask;
    hp->handler = func;
    hp->data = data;
    hp->next = NULL;

    if (!XFindContext(dpy, window, Context, &cdata)) {
	wp = (WindowRec *)cdata;
	hp->next = wp->handlers;
	wp->handlers = hp;
    } else {
	wp = XtNew(WindowRec);
	wp->mask = 0L;
	wp->dispatching = False;
	wp->handlers = hp;
	(void)XSaveContext(dpy, window, Context, (caddr_t)wp);
    }
    resetEventMask(dpy, window, wp);
}

void
MyRemoveEventHandler(dpy, window, type, func, data)
Display *dpy;
Window window;
int type;
void (*func)();
XtPointer data;
{
    caddr_t cdata;
    WindowRec *wp;
    HandlerRec *hp, *hp0;

    TRACE(("MyRemoveEventHandler(window=%08lx,type=%d)\n", window, type));
    if (!Initialized) initialize();
    if (XFindContext(dpy, window, Context, &cdata) || cdata == NULL) return;

    wp = (WindowRec *)cdata; 
    hp0 = NULL;
    hp = wp->handlers;

    while (hp != NULL) {
	if (hp->type == type && hp->handler == func && hp->data == data) {
	    HandlerRec *tmp = hp;

	    hp = hp->next;
	    if (hp0 == NULL) {
		wp->handlers = hp;
	    } else {
		hp0->next = hp;
	    }
	    XtFree((char *)tmp);
	} else {
	    hp0 = hp;
	    hp = hp->next;
	}
    }

    resetEventMask(dpy, window, wp);
    
    if (wp->handlers == NULL) {
	if (wp->dispatching) {
	    /* now dispatching. we just mark it to be removed later. */
	    wp->toberemoved = True;
	} else {
	    XtFree((char *)wp);
	    (void)XDeleteContext(dpy, window, Context);
	}
    }
}

void
MyRemoveAllEventHandler(dpy, window)
Display *dpy;
Window window;
{
    caddr_t cdata;
    WindowRec *wp;

    TRACE(("MyRemoveAllEventHandler(window=%08lx)\n", window));
    if (!Initialized) initialize();

    if (XFindContext(dpy, window, Context, &cdata) || cdata == NULL) return;

    wp = (WindowRec *)cdata;

    if (wp->dispatching) {
	/* now dispatching. we just mark it to be removed later. */
	wp->toberemoved = True;
	return;
    } else {
	removeAll(dpy, window, wp);
    }
}