1 #ifndef lint
2 static char *rcsid = "$Id: ioecall.c,v 1.2 1996/09/30 09:19:52 ishisone Exp $";
3 #endif
4 /*
5  * Copyright (C) 1994  Software Research Associates, Inc.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation for any purpose and without fee is hereby granted, provided
9  * that the above copyright notice appear in all copies and that both that
10  * copyright notice and this permission notice appear in supporting
11  * documentation, and that the name of Software Research Associates not be
12  * used in advertising or publicity pertaining to distribution of the
13  * software without specific, written prior permission.  Software Research
14  * Associates makes no representations about the suitability of this software
15  * for any purpose.  It is provided "as is" without express or implied
16  * warranty.
17  *
18  * Author:  Makoto Ishisone, Software Research Associates, Inc., Japan
19  */
20 
21 /*
22  *	X I/O error callback
23  */
24 
25 #include <stdio.h>
26 #include <X11/Xlib.h>
27 #include <X11/Xfuncproto.h>
28 #include "IOECall.h"
29 
30 #ifndef NULL
31 #define NULL	0
32 #endif
33 
34 typedef struct ioe_cb_desc_ {
35     void (*callback)();
36     XPointer client_data;
37     struct ioe_cb_desc_ *next;
38 } CBDesc;
39 
40 static CBDesc *cb_list;
41 
42 static int (*original_handler)();
43 
44 static int
XIOEHandler(dpy)45 XIOEHandler(dpy)
46 Display *dpy;
47 {
48     CBDesc *cb;
49 
50     cb = cb_list;
51     while (cb != NULL) {
52 	(*cb->callback)(cb->client_data);
53 	cb = cb->next;
54     }
55     return (*original_handler)(dpy);
56 }
57 
58 void
XIOEInit()59 XIOEInit()
60 {
61     int (*oldhandler)() = XSetIOErrorHandler(XIOEHandler);
62 
63     if (oldhandler != XIOEHandler) original_handler = oldhandler;
64 }
65 
66 XIOEHandle
67 XIOESet(handler, client_data)
68 void (*handler)();
69 XPointer client_data;
70 {
71     CBDesc *cb;
72 
73     cb = (CBDesc *)malloc(sizeof(CBDesc));
74     if (cb == NULL) return NULL;
75 
76     cb->callback = handler;
77     cb->client_data = client_data;
78     cb->next = NULL;
79 
80     if (cb_list == NULL) {
81 	cb_list = cb;
82     } else {
83 	CBDesc *p = cb_list;
84 
85 	while (p->next != NULL) p = p->next;
86 	p->next = cb;
87     }
88 
89     return cb;
90 }
91 
92 void
XIOEUnset(handle)93 XIOEUnset(handle)
94 XIOEHandle handle;
95 {
96     CBDesc *cb, *cb0;
97 
98     cb = cb_list;
99     cb0 = NULL;
100 
101     while (cb != NULL) {
102 	if (cb == handle) {
103 	    if (cb0 == NULL) {
104 		cb_list = cb->next;
105 	    } else {
106 		cb0->next = cb->next;
107 	    }
108 	    (void)free((char *)cb);
109 	    return;
110 	}
111 	cb = cb->next;
112     }
113 }
114