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