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