18108eb18Smrg/* $Xorg: xtwatch.c,v 1.4 2001/02/09 02:06:01 xorgcvs Exp $ */
28108eb18Smrg/******************************************************************************
38108eb18Smrg
48108eb18SmrgCopyright 1993, 1998  The Open Group
58108eb18Smrg
68108eb18SmrgPermission to use, copy, modify, distribute, and sell this software and its
78108eb18Smrgdocumentation for any purpose is hereby granted without fee, provided that
88108eb18Smrgthe above copyright notice appear in all copies and that both that
98108eb18Smrgcopyright notice and this permission notice appear in supporting
108108eb18Smrgdocumentation.
118108eb18Smrg
128108eb18SmrgThe above copyright notice and this permission notice shall be included in
138108eb18Smrgall copies or substantial portions of the Software.
148108eb18Smrg
158108eb18SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
168108eb18SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
178108eb18SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
188108eb18SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
198108eb18SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
208108eb18SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
218108eb18Smrg
228108eb18SmrgExcept as contained in this notice, the name of The Open Group shall not be
238108eb18Smrgused in advertising or otherwise to promote the sale, use or other dealings
248108eb18Smrgin this Software without prior written authorization from The Open Group.
258108eb18Smrg******************************************************************************/
268108eb18Smrg/* $XFree86: xc/programs/xsm/xtwatch.c,v 1.4 2001/01/17 23:46:31 dawes Exp $ */
278108eb18Smrg
288108eb18Smrg#include <X11/ICE/ICElib.h>
298108eb18Smrg#include <X11/Intrinsic.h>
308108eb18Smrg#include "xsm.h"
318108eb18Smrg#include "xtwatch.h"
328108eb18Smrg
338108eb18Smrgstatic void _XtIceWatchProc(IceConn ice_conn, IcePointer client_data,
348108eb18Smrg			    Bool opening, IcePointer *watch_data );
358108eb18Smrgstatic void _XtProcessIceMsgProc(XtPointer client_data, int *source,
368108eb18Smrg				 XtInputId *id);
378108eb18Smrg
388108eb18Smrg
398108eb18SmrgStatus
408108eb18SmrgInitWatchProcs(XtAppContext appContext)
418108eb18Smrg{
428108eb18Smrg
438108eb18Smrg    return (IceAddConnectionWatch (_XtIceWatchProc, (IcePointer) appContext));
448108eb18Smrg}
458108eb18Smrg
468108eb18Smrg
478108eb18Smrgstatic void
488108eb18Smrg_XtIceWatchProc(IceConn ice_conn, IcePointer client_data, Bool opening,
498108eb18Smrg		IcePointer *watch_data)
508108eb18Smrg{
518108eb18Smrg    if (opening)
528108eb18Smrg    {
538108eb18Smrg	XtAppContext appContext = (XtAppContext) client_data;
548108eb18Smrg
558108eb18Smrg	*watch_data = (IcePointer) XtAppAddInput (
568108eb18Smrg	    appContext,
578108eb18Smrg	    IceConnectionNumber (ice_conn),
588108eb18Smrg            (XtPointer) XtInputReadMask,
598108eb18Smrg	    _XtProcessIceMsgProc,
608108eb18Smrg	    (XtPointer) ice_conn);
618108eb18Smrg    }
628108eb18Smrg    else
638108eb18Smrg    {
648108eb18Smrg	XtRemoveInput ((XtInputId) *watch_data);
658108eb18Smrg    }
668108eb18Smrg}
678108eb18Smrg
688108eb18Smrg
698108eb18Smrgstatic void
708108eb18Smrg_XtProcessIceMsgProc(XtPointer client_data, int *source, XtInputId *id)
718108eb18Smrg{
728108eb18Smrg    IceConn			ice_conn = (IceConn) client_data;
738108eb18Smrg    IceProcessMessagesStatus	status;
748108eb18Smrg
758108eb18Smrg    status = IceProcessMessages (ice_conn, NULL, NULL);
768108eb18Smrg
778108eb18Smrg    if (status == IceProcessMessagesIOError)
788108eb18Smrg    {
798108eb18Smrg	List *cl;
808108eb18Smrg	int found = 0;
818108eb18Smrg
828108eb18Smrg	if (verbose)
838108eb18Smrg	{
848108eb18Smrg	    printf ("IO error on connection (fd = %d)\n",
858108eb18Smrg	        IceConnectionNumber (ice_conn));
868108eb18Smrg	    printf ("\n");
878108eb18Smrg	}
888108eb18Smrg
898108eb18Smrg	for (cl = ListFirst (RunningList); cl; cl = ListNext (cl))
908108eb18Smrg	{
918108eb18Smrg	    ClientRec *client = (ClientRec *) cl->thing;
928108eb18Smrg
938108eb18Smrg	    if (client->ice_conn == ice_conn)
948108eb18Smrg	    {
958108eb18Smrg		CloseDownClient (client);
968108eb18Smrg		found = 1;
978108eb18Smrg		break;
988108eb18Smrg	    }
998108eb18Smrg	}
1008108eb18Smrg
1018108eb18Smrg	if (!found)
1028108eb18Smrg	{
1038108eb18Smrg	    /*
1048108eb18Smrg	     * The client must have disconnected before it was added
1058108eb18Smrg	     * to the session manager's running list (i.e. before the
1068108eb18Smrg	     * NewClientProc callback was invoked).
1078108eb18Smrg	     */
1088108eb18Smrg
1098108eb18Smrg	    IceSetShutdownNegotiation (ice_conn, False);
1108108eb18Smrg	    IceCloseConnection (ice_conn);
1118108eb18Smrg	}
1128108eb18Smrg    }
1138108eb18Smrg}
114