#ident "@(#)chalk.h	1.5 91/09/14 SMI"
// This file is a product of Sun Microsystems, Inc. and is provided for
// unrestricted use provided that this legend is included on all tape
// media and as a part of the software program in whole or part.  Users
// may copy or modify this file without charge, but are not authorized to
// license or distribute it to anyone else except as part of a product
// or program developed by the user.
//
// THIS FILE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
// WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
// PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
//
// This file is provided with no support and without any obligation on the
// part of Sun Microsystems, Inc. to assist in its use, correction,
// modification or enhancement.
//
// SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
// INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS FILE
// OR ANY PART THEREOF.
//
// In no event will Sun Microsystems, Inc. be liable for any lost revenue
// or profits or other special, indirect and consequential damages, even
// if Sun has been advised of the possibility of such damages.
//
// Sun Microsystems, Inc.
// 2550 Garcia Avenue
// Mountain View, California  94043
//
//
// @(#)Chalk Talk 1.1 91/09/14
// written by Karl Jacob
//
// Copyright (c) 1991 by Sun Microsystems, Inc.
//

//
//	This file contains the class definitions.  If you wish to subclass
//	any of these classes use the hooks I have provided.  If you don't
//	I can't	guarantee what you will get.  An example might be don't
//	access the _prev variable use the prev method.

#include	<stream.h>
#include	<stdio.h>
#include	<string.h>
#include	<wire/wire.h>
#include	"chalk_cps.h"
#include	<ctype.h>
#include	<c_varieties.h>
#include	<malloc.h>
#include	<stdarg.h>
#include	<sys/time.h>
#include	<sysent.h>
#include	<stdlib.h>
#include	<generic.h>


#if defined (EXTERN)			/*This is necessary to avoid the redeclaration of global variables*/
#undef EXTERN
#endif
    
#if defined (DEFINE_GLOBALS)
#define EXTERN
#else
#define EXTERN extern
#endif

//
//	This class takes care of the callbacks the initcallback routine 
//	allows for an easy way to register callbacks.  It is really nothing
//	more than a structure with the ability to init some variables.
//

class Wire_Callbacks {
    friend class Wire;
 protected:
    int			*tag;
    wire_Handler	callback;
    int			*ps_token;
    char		*name;
 public:
    Wire_Callbacks() { tag=0;
		       callback=NULL;
		       ps_token=NULL;
		       name=NULL; }
};

class WireLink {
    friend class Wire;
private:
    WireLink	*_next;
    WireLink	*_prev;
    wire_Wire	_wire;
    int		_errno;
    char 	*_machine;
    char	*_user;
 public:
    WireLink() {
	_prev=0;
	_next=0;
	_wire=0;
	_errno=0;
	_machine=0;
	_user=0;
    }

    WireLink(WireLink& );
    virtual ~WireLink();
    WireLink& data(int& d);

//	Accessors for the various variables

    WireLink *next()
	{ return (WireLink *)  _next; }

    WireLink *prev()
	{ return (WireLink *)_prev; }

    const int& errno()
	{return _errno;}

    const wire_Wire& wire() 
 	{return _wire;};

    char *machine()
	{ return _machine;}

    char *user()
	{ return _user;}

    char *user_machine()
	{ char *s1=new char[strlen(_machine)+strlen(_user)+2];
	  strcpy(s1,_user);
	  strcat(s1,"@");
	  strcat(s1,_machine);
	  return s1;
	}


//	Mutators for the various variables

    WireLink& next(WireLink *l)
	{ _next=l;
	  return *this; }

    WireLink& prev(WireLink *l)
	{ _prev=l;
	  return *this; }

    WireLink& wire(wire_Wire& w)
	{_wire=w; return *this;}

    WireLink& errno(int& e)
	{_errno=e; return *this;}

    WireLink& machine(const char *m)
	{_machine=new char[strlen(m)+1];
	strcpy (_machine,m);
	 return *this; }

    WireLink& user(const char *u)
	{ _user= new char[ strlen(u)+1];
	strcpy (_user,u);
	 return *this; }
};

//
//	This class deals with the management of a list of wires
//


class Wire {
 protected:
    WireLink *_first;
    WireLink *_last;
    Wire_Callbacks	*wire_callbacks;
 public:

    Wire()
    { _first=_last=0;}

    virtual ~Wire();


//	Accessors

    WireLink *first()
    {return (WireLink *) _first;}

    WireLink *last()
    {return (WireLink *)_last;}

//	Mutators

    Wire& first(WireLink *l)
	{ _first=l;
	  return *this; }

    Wire& last(WireLink *l)
	{ _last=l;
	  return *this; }

    Wire& append(WireLink *,char *,char *,char *);
    Wire& remove(WireLink *);
    Wire& remove(wire_Wire );
    WireLink* link_from_wire(wire_Wire *);
    char*	name_from_wire(wire_Wire *);
    void 	initcallbacks(int items,...);
    void 	send_start(int,int,int);
    void 	send_motion(int,int,int,int,int);
    void 	send_stop(int,int,int,int,int);
    void	send_key	(int,int,int);
    void	send_delete	(int,int,int,int);
    void	send_access	(int, char *);
    void	send_quit	(wire_Wire *, char *);
    void	send_server	();
};



EXTERN Wire chalkwire;
EXTERN int canvastoken,windowtoken,noticetoken;
EXTERN int t_trackstart,t_quit,t_trackstop,t_trackmotion,t_key,t_access,
    	   t_delete,t_connect,t_user;
EXTERN     char *myname,*mymachine;
