/*
 * clienthandle.h
 * Provide event loop and event processing services for the clients.
 *
 * Copyright (C) Leni Mayo 2001-2002
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License, version 2, as
 *	published by the Free Software Foundation.
 *	
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#ifndef _CLIENTHANDLE_H_
#define _CLIENTHANDLE_H_

#include <signal.h>
#include "command.h"

/*
 * MessageReply is created+set by the Handler stuff, and passed to
 * the callback function.
 * The callback function is:
 *   - registered during handleInitiate(), and
 *   - called by handleEventLoop()
 */

typedef struct
{
	int  retval;
} MessageReply;

typedef int (*HandleCallback)(const MessageReply *pReplyMessage, void *pUserData);

typedef struct
{
	HandleCallback callback;
	MessageReply   msgReply;
	void           *pUserData;
	int            fContinueEventLoop;
	sigset_t       ssOldMask;
	int            fdFifo;
} Handle;

extern int  handleInitialise(Handle *pHandle);
extern int  handleInitiate(Handle *pH, const Message *pM, HandleCallback fn, void *pUserData);
extern void handleEventLoop(Handle *pHandle);
extern void handleCleanup(Handle *pHandle);

/* Default callback function and structure for responding to handle events
*/
typedef struct
{
	Command *pCommand;    /* Command is passed in to the callback function */
	PathName fileChannel; /* set by onEventDefault()                       */
	int retval;           /* post-event, this has a meaningful value       */
} HandleEventDefaultUserData;

int onEventDefault(const MessageReply *pReplyMessage, void *pUserData);
int onEventDefaultBase(const MessageReply *pReplyMessage, void *pUserData);

/* all clients define one of these as a global
*/
extern Handle eventHandle;

int openChannel(char filename[], int len);

#endif /* _CLIENTHANDLE_H_ */
