1266e564dSmrg/******************************************************************************
2266e564dSmrg
3266e564dSmrg
4266e564dSmrgCopyright 1993, 1998  The Open Group
5266e564dSmrg
6266e564dSmrgPermission to use, copy, modify, distribute, and sell this software and its
7266e564dSmrgdocumentation for any purpose is hereby granted without fee, provided that
8266e564dSmrgthe above copyright notice appear in all copies and that both that
9266e564dSmrgcopyright notice and this permission notice appear in supporting
10266e564dSmrgdocumentation.
11266e564dSmrg
12266e564dSmrgThe above copyright notice and this permission notice shall be included in
13266e564dSmrgall copies or substantial portions of the Software.
14266e564dSmrg
15266e564dSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16266e564dSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17266e564dSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18266e564dSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19266e564dSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20266e564dSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21266e564dSmrg
22266e564dSmrgExcept as contained in this notice, the name of The Open Group shall not be
23266e564dSmrgused in advertising or otherwise to promote the sale, use or other dealings
24266e564dSmrgin this Software without prior written authorization from The Open Group.
25266e564dSmrg
26266e564dSmrgAuthor: Ralph Mor, X Consortium
27266e564dSmrg******************************************************************************/
28266e564dSmrg
29266e564dSmrg#ifdef HAVE_CONFIG_H
30266e564dSmrg#include <config.h>
31266e564dSmrg#endif
32266e564dSmrg#include <X11/ICE/ICElib.h>
33266e564dSmrg#include "ICElibint.h"
34266e564dSmrg
35266e564dSmrg
36266e564dSmrgStatus
37c5629e66SmrgIceAddConnectionWatch (
38c5629e66Smrg	IceWatchProc	watchProc,
39c5629e66Smrg	IcePointer	clientData
40c5629e66Smrg)
41266e564dSmrg{
42266e564dSmrg    /*
43266e564dSmrg     * watchProc will be called each time an ICE connection is
44266e564dSmrg     * created/destroyed by ICElib.
45266e564dSmrg     */
46266e564dSmrg
47266e564dSmrg    _IceWatchProc	*ptr = _IceWatchProcs;
48266e564dSmrg    _IceWatchProc	*newWatchProc;
49266e564dSmrg    int			i;
50266e564dSmrg
51fb5e8d76Smrg    if ((newWatchProc = malloc (sizeof (_IceWatchProc))) == NULL)
52266e564dSmrg    {
53266e564dSmrg	return (0);
54266e564dSmrg    }
55266e564dSmrg
56266e564dSmrg    newWatchProc->watch_proc = watchProc;
57266e564dSmrg    newWatchProc->client_data = clientData;
58266e564dSmrg    newWatchProc->watched_connections = NULL;
59266e564dSmrg    newWatchProc->next = NULL;
60266e564dSmrg
61266e564dSmrg    while (ptr && ptr->next)
62266e564dSmrg	ptr = ptr->next;
63266e564dSmrg
64266e564dSmrg    if (ptr == NULL)
65266e564dSmrg	_IceWatchProcs = newWatchProc;
66266e564dSmrg    else
67266e564dSmrg	ptr->next = newWatchProc;
68266e564dSmrg
69266e564dSmrg
70266e564dSmrg    /*
71266e564dSmrg     * Invoke the watch proc with any previously opened ICE connections.
72266e564dSmrg     */
739ef0b394Smrg
74266e564dSmrg    for (i = 0; i < _IceConnectionCount; i++)
75266e564dSmrg    {
76fb5e8d76Smrg	_IceWatchedConnection *newWatchedConn =
77266e564dSmrg	    malloc (sizeof (_IceWatchedConnection));
78266e564dSmrg
79266e564dSmrg	newWatchedConn->iceConn = _IceConnectionObjs[i];
80266e564dSmrg	newWatchedConn->next = NULL;
81266e564dSmrg
82266e564dSmrg	newWatchProc->watched_connections = newWatchedConn;
83266e564dSmrg
84266e564dSmrg	(*newWatchProc->watch_proc) (_IceConnectionObjs[i],
85266e564dSmrg	    newWatchProc->client_data, True, &newWatchedConn->watch_data);
86266e564dSmrg    }
87266e564dSmrg
88266e564dSmrg    return (1);
89266e564dSmrg}
90266e564dSmrg
91266e564dSmrg
92a3129944Smrg
93266e564dSmrgvoid
94c5629e66SmrgIceRemoveConnectionWatch (
95c5629e66Smrg	IceWatchProc	watchProc,
96c5629e66Smrg	IcePointer	clientData
97c5629e66Smrg)
98266e564dSmrg{
99266e564dSmrg    _IceWatchProc	*currWatchProc = _IceWatchProcs;
100266e564dSmrg    _IceWatchProc	*prevWatchProc = NULL;
101266e564dSmrg
102266e564dSmrg    while (currWatchProc && (currWatchProc->watch_proc != watchProc ||
103266e564dSmrg        currWatchProc->client_data != clientData))
104266e564dSmrg    {
105266e564dSmrg	prevWatchProc = currWatchProc;
106266e564dSmrg	currWatchProc = currWatchProc->next;
107266e564dSmrg    }
108266e564dSmrg
109266e564dSmrg    if (currWatchProc)
110266e564dSmrg    {
111266e564dSmrg	_IceWatchProc		*nextWatchProc = currWatchProc->next;
112266e564dSmrg	_IceWatchedConnection 	*watchedConn;
113266e564dSmrg
114266e564dSmrg	watchedConn = currWatchProc->watched_connections;
115266e564dSmrg	while (watchedConn)
116266e564dSmrg	{
117266e564dSmrg	    _IceWatchedConnection *nextWatchedConn = watchedConn->next;
118fb5e8d76Smrg	    free (watchedConn);
119266e564dSmrg	    watchedConn = nextWatchedConn;
120266e564dSmrg	}
121266e564dSmrg
122266e564dSmrg	if (prevWatchProc == NULL)
123266e564dSmrg	    _IceWatchProcs = nextWatchProc;
124266e564dSmrg	else
125266e564dSmrg	    prevWatchProc->next = nextWatchProc;
126266e564dSmrg
127fb5e8d76Smrg	free (currWatchProc);
128266e564dSmrg    }
129266e564dSmrg}
130266e564dSmrg
131266e564dSmrg
132a3129944Smrg
133266e564dSmrgvoid
134c5629e66Smrg_IceConnectionOpened (
135c5629e66Smrg	IceConn	iceConn
136c5629e66Smrg)
137266e564dSmrg{
138266e564dSmrg    _IceWatchProc *watchProc = _IceWatchProcs;
139266e564dSmrg
140266e564dSmrg    while (watchProc)
141266e564dSmrg    {
142fb5e8d76Smrg	_IceWatchedConnection *newWatchedConn =
143266e564dSmrg	    malloc (sizeof (_IceWatchedConnection));
144266e564dSmrg	_IceWatchedConnection *watchedConn;
145266e564dSmrg
146266e564dSmrg	watchedConn = watchProc->watched_connections;
147266e564dSmrg	while (watchedConn && watchedConn->next)
148266e564dSmrg	    watchedConn = watchedConn->next;
149266e564dSmrg
150266e564dSmrg	newWatchedConn->iceConn = iceConn;
151266e564dSmrg	newWatchedConn->next = NULL;
152266e564dSmrg
153266e564dSmrg	if (watchedConn == NULL)
154266e564dSmrg	    watchProc->watched_connections = newWatchedConn;
155266e564dSmrg	else
156266e564dSmrg	    watchedConn->next = newWatchedConn;
157266e564dSmrg
158266e564dSmrg	(*watchProc->watch_proc) (iceConn,
159266e564dSmrg	    watchProc->client_data, True, &newWatchedConn->watch_data);
160266e564dSmrg
161266e564dSmrg	watchProc = watchProc->next;
162266e564dSmrg    }
163266e564dSmrg}
164266e564dSmrg
165266e564dSmrg
166a3129944Smrg
167266e564dSmrgvoid
168c5629e66Smrg_IceConnectionClosed (
169c5629e66Smrg	IceConn	iceConn
170c5629e66Smrg)
171266e564dSmrg{
172266e564dSmrg    _IceWatchProc *watchProc = _IceWatchProcs;
173266e564dSmrg
174266e564dSmrg    while (watchProc)
175266e564dSmrg    {
176266e564dSmrg	_IceWatchedConnection *watchedConn = watchProc->watched_connections;
177266e564dSmrg	_IceWatchedConnection *prev = NULL;
178266e564dSmrg
179266e564dSmrg	while (watchedConn && watchedConn->iceConn != iceConn)
180266e564dSmrg	{
181266e564dSmrg	    prev = watchedConn;
182266e564dSmrg	    watchedConn = watchedConn->next;
183266e564dSmrg	}
184266e564dSmrg
185266e564dSmrg	if (watchedConn)
186266e564dSmrg	{
187266e564dSmrg	    (*watchProc->watch_proc) (iceConn,
188266e564dSmrg	        watchProc->client_data, False, &watchedConn->watch_data);
189266e564dSmrg
190266e564dSmrg	    if (prev == NULL)
191266e564dSmrg		watchProc->watched_connections = watchedConn->next;
192266e564dSmrg	    else
193266e564dSmrg		prev->next = watchedConn->next;
194266e564dSmrg
195fb5e8d76Smrg	    free (watchedConn);
196266e564dSmrg	}
197266e564dSmrg
198266e564dSmrg	watchProc = watchProc->next;
199266e564dSmrg    }
200266e564dSmrg}
201