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