/* Header "phterm.h" for phterm Application */


struct CfgHdr {
	/*	Config file header */
	PtTerminalRowCol_t size, mins, maxs;
	short ncustfonts, fidx;
	unsigned short flags;
	};

struct XCfgHdr {
	short fontset;
	short scrlbk;
	char Reserved2[ 60 ];
	};

struct FontSet {
	char *prefix; /* NULL means "custom" (we might support multiple custom fontsets some day) */
	unsigned short nfonts, dflt;
	char **fullnames;		/* The actual font names (usually invisible) */
	char **dispnames;		/* Names displayed in the combo box (e.g. just the sizes; equal to dispnames for custom fonts) */
	char *csfile;			/* NULL or the charset file */
	PtTerminalCharsets_t *csets;
	PtTerminalCsXlatData_t *csdata;
	struct FontSet *saved;	/* Values to restore on a "Cancel..." */
	};

extern char **shargs, *title, *ptermrc, *cmdrc,
	devopt, *devfd, *device, *csfile;
extern unsigned short nfontsets;
extern struct FontSet
	*fontsets,			/* The array of font sets */
	*curfontset;		/* The font set chosen in the cfg dlg */

extern struct CfgHdr cfg;
extern struct XCfgHdr xcfg;

extern unsigned long xflags;

/*** FL_... are for cfg.flags ***/
#define FL_CNB	0x0001 /* Cursor never blinks */
#define FL_CAB	0x0002 /* Cursor always blinks */
#define FL_QNX	0x0004 /* QNX emulation */
//#define FL_FCD	0x0008 /* Font change changes DIM instead of SIZE */
//#define FL_LAT	0x0010 /* Use the 8859-1 character set */
//#define FL_CLK	0x0020 /* Display clock */
#define FL_VBL	0x0040 /* Visual bell */
#define FL_ALT	0x0080 /* Set Ph_WM_STATE_ISALTKEY on base window */
#define FL_MSE	0x0100 /* Set Pt_TERM_SELECTION_ALWAYS */


/*** XF_... are for xflags ***/

/* Command line options: */
#define XF_SIG		0x000001 /* Pass signals */
#define XF_PRIUP	0x000002 /* Increase priority */
#define XF_PRIDN	0x000004 /* Decrease priority */

#define XF_XST		0x000008 /* Exit with shell's exit status */
#define XF_NCFGF	0x000010 /* Don't read the config file */
#define XF_NFLST	0x000020 /* Don't read font list from config file */
#define XF_NXIT		0x000040 /* Don't exit when shell finished */
#define XF_NX0		0x000080 /* Don't exit unless status = 0 */
#define XF_NOCFG	0x000100 /* Disable the config dialog */
#define XF_EXIT		0x000200 /* Exit without asking */
#define XF_CMD		0x000400 /* A command was specified */

/* Some other flags: */
#define XF_CDLG		0x000800 /* The config dialog is active */
#define XF_FDLG		0x001000 /* The font list dialog is active */
#define XF_HDLG		0x002000 /* The title help dialog is active */
#define XF_TDEV		0x004000 /* The title contains dev info */
#define XF_TPRG		0x008000 /* The title contains process name */
#define XF_TCMD		0x010000 /* The title contains process args */
#define XF_TCLK		0x020000 /* The title contains current time */
#define XF_PAL		0x040000 /* A palette file has been read */
#define XF_BLINKS	0x080000 /* The cursor is blinking */
#define XF_DASHC	0x100000 /* A config file was given in the -C option */
#define XF_SPECF	0x200000 /* Use the "command-specific" cfg file */

#define TITLE_LEN 64
#define CMDPATH_LEN	( TITLE_LEN > PATH_MAX ? TITLE_LEN : PATH_MAX )

extern const char default_title[];

#define PALETTE_SIZE 16

extern PgColor_t palette[ PALETTE_SIZE ];

/*** Prototypes ***/

const char *afterslash( const char *path );
pid_t getprg( pid_t pid, char prg[ CMDPATH_LEN+1 ], char cmd[ TITLE_LEN+1 ], char args[ TITLE_LEN+1 ] );
void set_title( void );		/*	Set window title */
void check_title( void );	/*	Set xflags describing window title */
int cfgsave( void );		/*	Write config file (-1 if error) */
void cfg2wgt( void );		/*	Set tty's resources according to cfg */
void wgt2cfg( void );		/*	Get tty's resources that could have changed */

long getres( PtWidget_t *wgt, long res );
void newstr( char **var, const char *newval );

#define getptr( wgt, res )	( (void*) getres( wgt, res ) )

long getttyres( long res );
#define getttyptr( res )	( (void*) getttyres( res ) )

const char *getstring( PtWidget_t *wgt );

void setresl( PtWidget_t *wgt, long res, long val );
#define setres( wgt, res, val )	setresl( wgt, res, (long)(val) )

void setresl_len( PtWidget_t *wgt, long res, long val, long len );
#define setres_len( wgt, res, val, len ) setresl_len( wgt, res, (long)(val), len )

void but_able( int en, PtWidget_t *wgt ); /* Enable/disable a button */
void setstring( PtWidget_t *wgt, const char *str );
int setfont( PtWidget_t *wgt, const char *str );
int title_esc( PtWidget_t *, ApInfo_t *, PtCallbackInfo_t * );
int ask_for_stty( int proto, PtWidget_t *wgt, PtCallbackInfo_t *cb );
void setfontsizecombo( void );
void fontsets_apply( void );
void fontsets_cancel( void );
void setcharset( void );

void finish( void );
	#pragma aux finish aborts

/*****  Some stuff for debugging  *****/

#define CATCH_SEGV 0 /* 0 means don't catch SIGSEGV or SIGILL */
#define TRACE_ALLOC 0

#if TRACE_ALLOC
	extern void *myalloc(
		void *mem, unsigned len,
		const char *file, int line, const char *fun
		);
	#define malloc( len ) myalloc( NULL, len, __FILE__, __LINE__, "malloc" )
	#define calloc( a, b ) myalloc( NULL, (a) * (b), __FILE__, __LINE__, "calloc" )
	#define realloc( mem, len ) myalloc( mem, len, __FILE__, __LINE__, "realloc" )
	#define free( mem ) myalloc( mem, 0, __FILE__, __LINE__, "free" )
#endif

