0a3ea1bd6da4770698d0c6c0c12c229cd6f102e6
[oweals/cde.git] / cde / programs / dtsession / SmWatch.c
1 /*
2  * (c) Copyright 1995 Digital Equipment Corporation.
3  * (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
4  * (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
5  * (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
6  * (c) Copyright 1993, 1994, 1995 Novell, Inc. 
7  * (c) Copyright 1995 FUJITSU LIMITED.
8  * (c) Copyright 1995 Hitachi.
9  *
10  * $TOG: SmWatch.c /main/4 1997/03/19 12:21:06 barstow $
11  */
12 /******************************************************************************
13
14 Copyright (c) 1993  X Consortium
15
16 Permission is hereby granted, free of charge, to any person obtaining a copy
17 of this software and associated documentation files (the "Software"), to deal
18 in the Software without restriction, including without limitation the rights
19 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20 copies of the Software, and to permit persons to whom the Software is
21 furnished to do so, subject to the following conditions:
22
23 The above copyright notice and this permission notice shall be included in
24 all copies or substantial portions of the Software.
25
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
29 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
30 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32
33 Except as contained in this notice, the name of the X Consortium shall not be
34 used in advertising or otherwise to promote the sale, use or other dealings
35 in this Software without prior written authorization from the X Consortium.
36 ******************************************************************************/
37
38 #include <X11/Intrinsic.h>
39 #include <X11/ICE/ICElib.h>
40 #include "SmXSMP.h"
41
42 /*
43  * Forward declarations
44  */
45 void _XtIceWatchProc ();
46 void _XtProcessIceMsgProc ();
47
48 Status
49 InitWatchProcs (
50         XtAppContext            appContext)
51 {
52 #ifdef DEBUG
53     printf ("InitWatchProcs\n");
54 #endif /* DEBUG */
55
56     return (IceAddConnectionWatch (_XtIceWatchProc, (IcePointer) appContext));
57 }
58
59
60 void
61 _XtIceWatchProc (
62         IceConn                 ice_conn,
63         IcePointer              client_data,
64         Bool                    opening,
65         IcePointer              *watch_data)
66 {
67 #ifdef DEBUG
68     printf ("_XtIceWatchProc\n");
69 #endif /* DEBUG */
70
71     if (opening)
72     {
73         XtAppContext appContext = (XtAppContext) client_data;
74
75         *watch_data = (IcePointer) XtAppAddInput (
76             appContext,
77             IceConnectionNumber (ice_conn),
78             (XtPointer) XtInputReadMask,
79             _XtProcessIceMsgProc,
80             (XtPointer) ice_conn);
81     }
82     else
83     {
84         XtRemoveInput ((XtInputId) *watch_data);
85     }
86 }
87
88
89 void
90 _XtProcessIceMsgProc (
91         XtPointer               client_data,
92         int                     *source,
93         XtInputId               *id)
94 {
95     IceConn                     ice_conn = (IceConn) client_data;
96     IceProcessMessagesStatus    status;
97
98 #ifdef DEBUG
99     printf ("_XtProcessIceMsgProc\n");
100 #endif /* DEBUG */
101
102     status = IceProcessMessages (ice_conn, NULL, NULL);
103
104     if (status == IceProcessMessagesIOError)
105     {
106         ClientRecPtr            pClientRec;
107         int                     found = 0;
108
109 #ifdef DEBUG
110         printf ("IO error on connection (fd = %d)\n", 
111                 IceConnectionNumber (ice_conn));
112 #endif /* DEBUG */
113
114         for (pClientRec = connectedList; pClientRec != NULL; 
115                 pClientRec = pClientRec->next) 
116         {
117             if (pClientRec->iceConn == ice_conn) 
118             {
119                 CloseDownClient (pClientRec);
120                 found = 1;
121                 break;
122             }
123         }
124
125         if (!found) {
126             IceSetShutdownNegotiation (ice_conn, False);
127             IceCloseConnection (ice_conn);
128         }
129     }
130 }