watch.c revision 9ef0b394
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 51266e564dSmrg if ((newWatchProc = (_IceWatchProc *) malloc ( 52266e564dSmrg sizeof (_IceWatchProc))) == NULL) 53266e564dSmrg { 54266e564dSmrg return (0); 55266e564dSmrg } 56266e564dSmrg 57266e564dSmrg newWatchProc->watch_proc = watchProc; 58266e564dSmrg newWatchProc->client_data = clientData; 59266e564dSmrg newWatchProc->watched_connections = NULL; 60266e564dSmrg newWatchProc->next = NULL; 61266e564dSmrg 62266e564dSmrg while (ptr && ptr->next) 63266e564dSmrg ptr = ptr->next; 64266e564dSmrg 65266e564dSmrg if (ptr == NULL) 66266e564dSmrg _IceWatchProcs = newWatchProc; 67266e564dSmrg else 68266e564dSmrg ptr->next = newWatchProc; 69266e564dSmrg 70266e564dSmrg 71266e564dSmrg /* 72266e564dSmrg * Invoke the watch proc with any previously opened ICE connections. 73266e564dSmrg */ 749ef0b394Smrg 75266e564dSmrg for (i = 0; i < _IceConnectionCount; i++) 76266e564dSmrg { 77266e564dSmrg _IceWatchedConnection *newWatchedConn = (_IceWatchedConnection *) 78266e564dSmrg malloc (sizeof (_IceWatchedConnection)); 79266e564dSmrg 80266e564dSmrg newWatchedConn->iceConn = _IceConnectionObjs[i]; 81266e564dSmrg newWatchedConn->next = NULL; 82266e564dSmrg 83266e564dSmrg newWatchProc->watched_connections = newWatchedConn; 84266e564dSmrg 85266e564dSmrg (*newWatchProc->watch_proc) (_IceConnectionObjs[i], 86266e564dSmrg newWatchProc->client_data, True, &newWatchedConn->watch_data); 87266e564dSmrg } 88266e564dSmrg 89266e564dSmrg return (1); 90266e564dSmrg} 91266e564dSmrg 92266e564dSmrg 93266e564dSmrg 94266e564dSmrgvoid 95c5629e66SmrgIceRemoveConnectionWatch ( 96c5629e66Smrg IceWatchProc watchProc, 97c5629e66Smrg IcePointer clientData 98c5629e66Smrg) 99266e564dSmrg{ 100266e564dSmrg _IceWatchProc *currWatchProc = _IceWatchProcs; 101266e564dSmrg _IceWatchProc *prevWatchProc = NULL; 102266e564dSmrg 103266e564dSmrg while (currWatchProc && (currWatchProc->watch_proc != watchProc || 104266e564dSmrg currWatchProc->client_data != clientData)) 105266e564dSmrg { 106266e564dSmrg prevWatchProc = currWatchProc; 107266e564dSmrg currWatchProc = currWatchProc->next; 108266e564dSmrg } 109266e564dSmrg 110266e564dSmrg if (currWatchProc) 111266e564dSmrg { 112266e564dSmrg _IceWatchProc *nextWatchProc = currWatchProc->next; 113266e564dSmrg _IceWatchedConnection *watchedConn; 114266e564dSmrg 115266e564dSmrg watchedConn = currWatchProc->watched_connections; 116266e564dSmrg while (watchedConn) 117266e564dSmrg { 118266e564dSmrg _IceWatchedConnection *nextWatchedConn = watchedConn->next; 119266e564dSmrg free ((char *) watchedConn); 120266e564dSmrg watchedConn = nextWatchedConn; 121266e564dSmrg } 122266e564dSmrg 123266e564dSmrg if (prevWatchProc == NULL) 124266e564dSmrg _IceWatchProcs = nextWatchProc; 125266e564dSmrg else 126266e564dSmrg prevWatchProc->next = nextWatchProc; 127266e564dSmrg 128266e564dSmrg free ((char *) currWatchProc); 129266e564dSmrg } 130266e564dSmrg} 131266e564dSmrg 132266e564dSmrg 133266e564dSmrg 134266e564dSmrgvoid 135c5629e66Smrg_IceConnectionOpened ( 136c5629e66Smrg IceConn iceConn 137c5629e66Smrg) 138266e564dSmrg{ 139266e564dSmrg _IceWatchProc *watchProc = _IceWatchProcs; 140266e564dSmrg 141266e564dSmrg while (watchProc) 142266e564dSmrg { 143266e564dSmrg _IceWatchedConnection *newWatchedConn = (_IceWatchedConnection *) 144266e564dSmrg malloc (sizeof (_IceWatchedConnection)); 145266e564dSmrg _IceWatchedConnection *watchedConn; 146266e564dSmrg 147266e564dSmrg watchedConn = watchProc->watched_connections; 148266e564dSmrg while (watchedConn && watchedConn->next) 149266e564dSmrg watchedConn = watchedConn->next; 150266e564dSmrg 151266e564dSmrg newWatchedConn->iceConn = iceConn; 152266e564dSmrg newWatchedConn->next = NULL; 153266e564dSmrg 154266e564dSmrg if (watchedConn == NULL) 155266e564dSmrg watchProc->watched_connections = newWatchedConn; 156266e564dSmrg else 157266e564dSmrg watchedConn->next = newWatchedConn; 158266e564dSmrg 159266e564dSmrg (*watchProc->watch_proc) (iceConn, 160266e564dSmrg watchProc->client_data, True, &newWatchedConn->watch_data); 161266e564dSmrg 162266e564dSmrg watchProc = watchProc->next; 163266e564dSmrg } 164266e564dSmrg} 165266e564dSmrg 166266e564dSmrg 167266e564dSmrg 168266e564dSmrgvoid 169c5629e66Smrg_IceConnectionClosed ( 170c5629e66Smrg IceConn iceConn 171c5629e66Smrg) 172266e564dSmrg{ 173266e564dSmrg _IceWatchProc *watchProc = _IceWatchProcs; 174266e564dSmrg 175266e564dSmrg while (watchProc) 176266e564dSmrg { 177266e564dSmrg _IceWatchedConnection *watchedConn = watchProc->watched_connections; 178266e564dSmrg _IceWatchedConnection *prev = NULL; 179266e564dSmrg 180266e564dSmrg while (watchedConn && watchedConn->iceConn != iceConn) 181266e564dSmrg { 182266e564dSmrg prev = watchedConn; 183266e564dSmrg watchedConn = watchedConn->next; 184266e564dSmrg } 185266e564dSmrg 186266e564dSmrg if (watchedConn) 187266e564dSmrg { 188266e564dSmrg (*watchProc->watch_proc) (iceConn, 189266e564dSmrg watchProc->client_data, False, &watchedConn->watch_data); 190266e564dSmrg 191266e564dSmrg if (prev == NULL) 192266e564dSmrg watchProc->watched_connections = watchedConn->next; 193266e564dSmrg else 194266e564dSmrg prev->next = watchedConn->next; 195266e564dSmrg 196266e564dSmrg free ((char *) watchedConn); 197266e564dSmrg } 198266e564dSmrg 199266e564dSmrg watchProc = watchProc->next; 200266e564dSmrg } 201266e564dSmrg} 202