/* Data and prototypes that are shared by both client and server */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <dos/dos.h>
#include <dos/dostags.h>
#include <dos/dosextens.h>

#include <exec/types.h>
#include <exec/io.h>
#include <exec/lists.h>
#include <exec/memory.h>

#include <clib/commodities_protos.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
#include <clib/icon_protos.h>
#include <clib/wb_protos.h>
#include <clib/intuition_protos.h>
#include <clib/diskfont_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/amarquee_protos.h>

#include <errno.h>
#include <time.h>

#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <intuition/gadgetclass.h>
#include <intuition/screens.h>
#include <libraries/gadtools.h>

#include <pragmas/amarquee_pragmas.h>

#include <libraries/diskfont.h>
#include <libraries/commodities.h>

#define UNLESS(x) if(!(x))

#define EVT_HOTKEY  1

/* Used internally by the event loops */
#define CODE_TIMER         0x0001 /* "The time went off." */
#define CODE_WINDOW_EVENT  0x0002 /* "Check for IDCMP stuff." */
#define CODE_RECONNECT	   0x0004 /* "Reconnect to another server/port" */
#define CODE_HIDE          0x0008 /* "Close your window." */
#define CODE_SHOW          0x0010 /* "Show your window." */
#define CODE_ENABLE        0x0020 /* "Resume pinging & updating" */
#define CODE_DISABLE       0x0040 /* "Stop automatic pinging & updating" */
#define CODE_AREXX         0x0080 /* "An ARexx message is ready." */
#define CODE_SENDSTATE     0x0100 /* "Send your current info to the server." */
#define CODE_QMESSAGE      0x0200 /* "A QMessage is ready." */
#define CODE_QUIT          0x0400 /* "Die! Die! Die!" */

/* Log event opcodes */
#define LOG_LOGON      0  /* A new host appeared on the list */
#define LOG_LOGOFF     1  /* A host dissapeared from the list */
#define LOG_UPDATE     2  /* A host changed its comment string */
#define LOG_CONNECT    3  /* We connected to the server */
#define LOG_DISCONNECT 4  /* We disconnected from the server */
#define LOG_START      5  /* QAmiTrack starts */
#define LOG_END        6  /* QAmiTrack finishes */
#define LOG_SYSMESSAGE 7  /* System message received */
#define LOG_DEFAULT    8  /* Rexx script to run if the real one isn't defined */
#define NUM_LOG_ENUMS  9  /* for array sizes, etc */

struct CxStuff {
    CxObj * broker;
    struct MsgPort * port;
    struct NewBroker nb;
    char * name;
};

struct WindowStuff {
	struct Screen * screen;		/* Pointer to screen we're on */
	struct Window * win;		/* Pointer to Intuition window structure */
	struct TextAttr font;		/* Gadget rendering font */
	struct TextFont * fontdata;	/* The font's bits */
	struct TextAttr fixedfont;	/* ListView rendering font */
	struct TextFont * fixedfontdata; /* This font's bits */
	void 	      * vi;              /* visual info for gadgets */
	struct Gadget * glist;		     /* list of all our gadgets */
	struct Gadget * CommentListGadget; /* ListView of all logins */
	struct Gadget * LogListGadget;   /* ListView of chat history */
	struct Gadget * ListGadget;	     /* ListView of all logins */
	struct Gadget * ServerString;	 /* Server display/chooser string */
	struct Gadget * PortString;	     /* Port display/chooser string */
	struct Gadget * ActionCycle;     /* Cycle to choose wdich action will be performed on double-click in the ListView */
	struct Gadget * CommentString;	 /* Comment display/chooser string */
};

struct TimerStuff {
	struct MsgPort     * TimerMP;
	struct timerequest * TimerIO;
	BOOL BDevOpen;
};


/* Shared functions */
void TrackWait(struct WindowStuff * Window, 
               struct QSession    * session, 
               struct RexxHost    * host,
               struct CxStuff     * cx,
               struct TimerStuff  * ts);

void SetTrackFlag(ULONG flag);
BOOL CheckTrackFlag(ULONG flag);
BOOL TrackFlagsSet(void);

struct TimerStuff * SetupTimer(struct TimerStuff * ts);
void SetTimer(struct TimerStuff * ts, int nSecs, int nMicros);

void ReplaceAllocedString(char ** szOldString, char * szNewString);
void TrackExit(char * szMessage, int nCode);

BOOL FillInfos(struct SocketStuff * sSocket, char **pSetName, ULONG * ulIPAddress, char **pSetComment);
void ClearTrackScreen(void);

int MakeReq(char *sTitle, char *sText, char *sGadgets);

char * PastSpaces(char * pcString);
char * RemoveUnprintableChars(char * pcString);
char * RemoveTrailingSpaces(char * pcString);
char * ToLower(char * pcString);

void SetLogViewLength(int numLines);
int ParseLogViewLevel(char * levelString);
void SetRexxScript(int which, char * filename);
BOOL SetLogViewLevel(ULONG ulItemCode);  /* returns TRUE iff a change was made */
int GetLogViewLevelPercent(ULONG ulItemCode);  /* item code -> % */