watch.c revision 266e564d
1/* $Xorg: watch.c,v 1.4 2001/02/09 02:03:26 xorgcvs Exp $ */
2/******************************************************************************
3
4
5Copyright 1993, 1998  The Open Group
6
7Permission to use, copy, modify, distribute, and sell this software and its
8documentation for any purpose is hereby granted without fee, provided that
9the above copyright notice appear in all copies and that both that
10copyright notice and this permission notice appear in supporting
11documentation.
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall not be
24used in advertising or otherwise to promote the sale, use or other dealings
25in this Software without prior written authorization from The Open Group.
26
27Author: Ralph Mor, X Consortium
28******************************************************************************/
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33#include <X11/ICE/ICElib.h>
34#include "ICElibint.h"
35
36
37Status
38IceAddConnectionWatch (watchProc, clientData)
39
40IceWatchProc	watchProc;
41IcePointer	clientData;
42
43{
44    /*
45     * watchProc will be called each time an ICE connection is
46     * created/destroyed by ICElib.
47     */
48
49    _IceWatchProc	*ptr = _IceWatchProcs;
50    _IceWatchProc	*newWatchProc;
51    int			i;
52
53    if ((newWatchProc = (_IceWatchProc *) malloc (
54	sizeof (_IceWatchProc))) == NULL)
55    {
56	return (0);
57    }
58
59    newWatchProc->watch_proc = watchProc;
60    newWatchProc->client_data = clientData;
61    newWatchProc->watched_connections = NULL;
62    newWatchProc->next = NULL;
63
64    while (ptr && ptr->next)
65	ptr = ptr->next;
66
67    if (ptr == NULL)
68	_IceWatchProcs = newWatchProc;
69    else
70	ptr->next = newWatchProc;
71
72
73    /*
74     * Invoke the watch proc with any previously opened ICE connections.
75     */
76
77    for (i = 0; i < _IceConnectionCount; i++)
78    {
79	_IceWatchedConnection *newWatchedConn = (_IceWatchedConnection *)
80	    malloc (sizeof (_IceWatchedConnection));
81
82	newWatchedConn->iceConn = _IceConnectionObjs[i];
83	newWatchedConn->next = NULL;
84
85	newWatchProc->watched_connections = newWatchedConn;
86
87	(*newWatchProc->watch_proc) (_IceConnectionObjs[i],
88	    newWatchProc->client_data, True, &newWatchedConn->watch_data);
89    }
90
91    return (1);
92}
93
94
95
96void
97IceRemoveConnectionWatch (watchProc, clientData)
98
99IceWatchProc	watchProc;
100IcePointer	clientData;
101
102{
103    _IceWatchProc	*currWatchProc = _IceWatchProcs;
104    _IceWatchProc	*prevWatchProc = NULL;
105
106    while (currWatchProc && (currWatchProc->watch_proc != watchProc ||
107        currWatchProc->client_data != clientData))
108    {
109	prevWatchProc = currWatchProc;
110	currWatchProc = currWatchProc->next;
111    }
112
113    if (currWatchProc)
114    {
115	_IceWatchProc		*nextWatchProc = currWatchProc->next;
116	_IceWatchedConnection 	*watchedConn;
117
118	watchedConn = currWatchProc->watched_connections;
119	while (watchedConn)
120	{
121	    _IceWatchedConnection *nextWatchedConn = watchedConn->next;
122	    free ((char *) watchedConn);
123	    watchedConn = nextWatchedConn;
124	}
125
126	if (prevWatchProc == NULL)
127	    _IceWatchProcs = nextWatchProc;
128	else
129	    prevWatchProc->next = nextWatchProc;
130
131	free ((char *) currWatchProc);
132    }
133}
134
135
136
137void
138_IceConnectionOpened (iceConn)
139
140IceConn	iceConn;
141
142{
143    _IceWatchProc *watchProc = _IceWatchProcs;
144
145    while (watchProc)
146    {
147	_IceWatchedConnection *newWatchedConn = (_IceWatchedConnection *)
148	    malloc (sizeof (_IceWatchedConnection));
149	_IceWatchedConnection *watchedConn;
150
151	watchedConn = watchProc->watched_connections;
152	while (watchedConn && watchedConn->next)
153	    watchedConn = watchedConn->next;
154
155	newWatchedConn->iceConn = iceConn;
156	newWatchedConn->next = NULL;
157
158	if (watchedConn == NULL)
159	    watchProc->watched_connections = newWatchedConn;
160	else
161	    watchedConn->next = newWatchedConn;
162
163	(*watchProc->watch_proc) (iceConn,
164	    watchProc->client_data, True, &newWatchedConn->watch_data);
165
166	watchProc = watchProc->next;
167    }
168}
169
170
171
172void
173_IceConnectionClosed (iceConn)
174
175IceConn	iceConn;
176
177{
178    _IceWatchProc *watchProc = _IceWatchProcs;
179
180    while (watchProc)
181    {
182	_IceWatchedConnection *watchedConn = watchProc->watched_connections;
183	_IceWatchedConnection *prev = NULL;
184
185	while (watchedConn && watchedConn->iceConn != iceConn)
186	{
187	    prev = watchedConn;
188	    watchedConn = watchedConn->next;
189	}
190
191	if (watchedConn)
192	{
193	    (*watchProc->watch_proc) (iceConn,
194	        watchProc->client_data, False, &watchedConn->watch_data);
195
196	    if (prev == NULL)
197		watchProc->watched_connections = watchedConn->next;
198	    else
199		prev->next = watchedConn->next;
200
201	    free ((char *) watchedConn);
202	}
203
204	watchProc = watchProc->next;
205    }
206}
207