

#define APPL_NAME "GTerm"


// Default Terminal Window:
#define WIDTH	800			// N*8
#define HEIGHT	600			// N*12


// Maximum number of streams:
// MAX_STREAMS muss ≤ 240 sein weil CTL control codes sind $F0++
#define	MAX_STREAMS 64
#define MAX_MEM (64 << 20)		// ram usage limit = 64 MB



enum IsaID
{
	IsaNothing		= 0,		// note: same as IDs for CREATE.TYPE:
	IsaTTYObject	= 1,		// == TTY
	IsaCRTObject	= 2,		// == CRT
	IsaGFXObject	= 3,		// == GFX
	IsaRectObject	= 4,		// == UNI
	IsaAudio		= 5,		// == AUDIO
	//IsaAlias		= 6,
	IsaTTYPixmap	= 7,
	IsaCRTPixmap,
	IsaGFXPixmap
};



// codes handled by escaped stream, not sent to painter:
enum
{
	// control prefix:
	CTL			= 0xF5,

	// 2nd byte:
	STREAM		= 0x00,		// 0x00 .. 0xF0-1	= stream number

	esc_CTL		= 0xF0,
	esc_XON		= 0xF1,
	esc_XOFF	= 0xF2,

	ACK			= 0xF3,		// EC transmission
	NAK			= 0xF4,		// EC transmission

	UNUSED_F5	= 0xF5,		// could actually be used. would be transmitted as CTL + CTL + esc_CTL

	RESET		= 0xF6,		// 0xF5, 0xF6, 0,0,0,0
	BREAK		= RESET,	// 0xF5, 0xF6, 9,9,9,9		EC transmission

	CONFIGURE	= 0xF7,		// 0xf5, 0xF7, WHAT

	CREATE		= 0xF8,		// 0xF5, 0xF8, TYPE, WW, HH, ARGB	create & select
	SELECT		= 0xF9,		// 0xF5, 0xF9, NN					and push old selection
	DISPOSE		= 0xFA,		// 0xF5, 0xFA, NN					remove from parent and from objects[]

	QUERY		= 0xFB,		// 0xF5, 0xFB, NN, WHAT		--> reply: 0xF5, 0xFB, NN, ..

	REORDER		= 0xFC,		// 0xF5, 0xFC, NN, HOW, ..
	GEOMETRY	= 0xFD,		// 0xF5, 0xFD, NN, WHAT, ..

	WAIT_FFB	= 0xFE,		// 0xF5, 0xFE				--> 0xfa, 0xff, flags, ..

	// REORDER.HOW:
	HIGHER		= 1,		// move object in it's parent's child list --> index -= 1
	LOWER		= 2,		// move object in it's parent's child list --> index += 1
	TOSTART		= 3,		// index = 0
	TOEND		= 4,		// index = last
	TOINDEX		= 5,		// index NN follows, inserts
	BEHIND		= 6,		// object_id NN follows
	BEFORE		= 7,		// object_id NN follows
	EXCHANGE	= 8,		// object_id NN follows
	REPLACE		= 9,		// object_id NN follows
	ATSTART		= 10,		// object_id of new parent NN follows
	ATEND		= 11,		// object_id of new parent NN follows
	ATINDEX		= 12,		// object_id of new parent NN  and index NN follow, inserts
	TRANSPOSE	= 13,		// object_id NN follows

	// GEOMETRY.FLAGS:
	// object mapping: zbox, qbox, Skalierung, Drehung, Scherung
	RESET_GEOMETRY	= 0x00,	// ZSIZE=QSIZE, QPOS=0,0, ROT=0
	SET_ZPOS		= 1,	// --> XX, YY follow
	SET_ZSIZE		= 2,	// --> WW, HH follow
	SET_QPOS		= 4,	// --> XX, YY follow
	SET_QSIZE		= 8,	// --> WW, HH follow
	//SET_SCALE
	//SET_ROTATION

	// CONFIGURE.WHAT:
	PTR_EVENTS_OFF	= 0,	// %00=off, %01=btns, %10=drag, %11=move
	PTR_EVENTS_BTN	= 1,
	PTR_EVENTS_DRAG	= 2,
	PTR_EVENTS_MOVE	= 3,
	RESIZE_EVENTS	= 4,	// bit0: on/off
	JOY_EVENTS		= 6,	// bit0: on/off
	KBD_EVENTS		= 8,	// bit0: on/off, default = ON: send control code and printable characters "as is"
	FFB_EVENTS		= 10,	// bit0: on/off, will be sent on PTR or JOY events too

	// WAIT_FFB.FLAGS:
	// event infos are attached to the FFB reply only if changed:
	MSG_PTR_EVENT	= 1,	// --> 1 byte button mask new state: %00000mrl
	MSG_PTR_MOVED	= 2,	// --> XX, YY
	MSG_INPUT_CHAR	= 4,	// --> 1 byte Latin-1 Char
	MSG_RESIZED		= 8,	// --> WW, HH
	MSG_JOY_EVENT	= 0x10,	// --> 2 bytes button mask new state: %xxxfudlr 1=active
	MSG_JOY2_EVENT	= 0x20,	//
	MSG_JOY3_EVENT	= 0x40,	//
	MSG_JOY4_EVENT	= 0x80,	//

	// CREATE.TYPE:
	TTY		= 1,
	CRT		= 2,
	GFX		= 3,
	UNI		= 4,
	AUDIO	= 5,			// AY sound chip TODO
	ALIAS	= 6,			// same type as current object, sharing the same pixmap. TTY and GFX only.
	AND_APPENTO	= 0x10,		// --> NN objID follows, else it is appended to offscreenobjects
//	AND_INSERTAT= 0x20,		// --> NN index follows
	AND_MOVETO  = 0x40,		// --> XX,YY follow
	AND_SELECT	= 0x80,		// implied if !AND_APPENDTO

	// QUERY.WHAT:
	GET_OBJECT_ID	= 0,	// --> NN		always sent: 2-byte obj_id or 1-byte NOTHING => message ends here
	GET_TYPE		= 1,	// --> TYPE
	GET_INDEX		= 2,	// --> NN		index in parent's child list,  -1 = no parent
	GET_CHILDCOUNT	= 4,	// --> NN
	GET_ZPOS		= 8,	// --> XX, YY	relative to parent
	GET_ZSIZE		= 16,	// --> WW, HH
	GET_QSIZE		= 32,	// --> WW, HH

	// Special values for object id NN:
	// these 1-byte codes can be sent in place of a 2-byte Object ID:
	OFFSCREENOBJECTS = 0xF5,// same as $0000
	FRAMEBUFFEROBJECT= 0xF6,// same as $0001
	PARENT		= 0xF7,		// parent of the currently selected object
	CURRENT		= 0xF8,		// the currently selected object
	PREVIOUS	= 0xF9,		// the previously selected object
	FIRSTCHILD	= 0xFA,		// first child
	LASTCHILD	= 0xFB,		// last child
	CHILDAT		= 0xFC,		// Child at index, 2-byte index follows
	NEXTSIBLING	= 0xFD,		// next neighbour / sibbling in parent's child list
	PREVSIBLING	= 0xFE,		// previous neighbour / sibbling in parent's child list
	NOTHING		= 0xFF,
};


enum	// Replies:
{
// all replies, except keyboard inputs, IDENTIFY and replies from the painters, start with CTL:
// if CTL (or XON or XOFF) occurs as data, it must be escaped.
	RPL_CTL		= CTL,
// 2nd byte:
	RPL_QUERY	= QUERY,		// response to query
	RPL_FFB		= WAIT_FFB,
	RPL_ERROR	= 0xF6,			// 1 byte error code follows. or string? or both? DENK..
};


// UNI (RectObject) control codes:
enum
{
	SET_COLOR	= 0x41		// 0x41, A, R, G, B
};


// CRT control codes:
// X and Y are in range 0 ≤ X|Y ≤ 255
// and scaled to min(width,height)
// and centered in the larger dimension
enum
{
	SET_FRAME_RATE	= 0x20,		// 0x20, N  					-> draw only every Nth frame
	SET_PAPER_COLOR	= 0x21,		// 0x21, A, R, G, B
	SET_LINE_COLOR 	= 0x22,		// 0x22, A, R, G, B
	DEFINE_CHAR		= 0x23,		// 0x23, CHAR, N, N * (x,y)		first and last point move, others draw

	MOVE			= 0x28,		// 0x28, X, Y
	DRAW			= 0x29,		// 0x29, X, Y
	DRAWCHAR		= 0x2A,		// 0x2A, CHAR

	DRAWLINES		= 0x40,		// 0x40+N, N * ( x.b, y.b)		== N * DRAW, 0 ≤ N ≤ 31
	DRAWTEXT		= 0x60,		// 0x60+N, N * char.b			== N * DRAWCHAR, 0 ≤ N ≤ 31
};


// TTY attributes:
enum
{
	BOLD				= 1,
	UNDERLINE			= 2,
	INVERTED			= 4,
	ITALIC				= 8,
	OVERPRINT			= 16,
	DOUBLE_WIDTH		= 32,
	DOUBLE_HEIGHT		= 64,
	GRAPHICS_CHARACTERS	= 128		// NIMP
};


// TTY control codes:
enum
{
	Reset				= 0x00,		// 0x00
	ClearScreen			= 0x01,		// 0x01
	GotoPosition		= 0x02,		// 0x02, <byte:row>, <byte:col>
	GotoColumn			= 0x03,		// 0x03, <byte:col>
	GetCursorPosition	= 0x04,		// 0x04  -->  0x04, <byte:row>, <byte:col>
	PushCursorPosition	= 0x05,		// 0x05
	PopCursorPosition	= 0x06,		// 0x06
	ShowCursor			= 0x07,		// 0x07
	CursorLeft			= 0x08,		// 0x08
	Tab					= 0x09,		// 0x09
	CursorDown			= 0x0A,		// 0x0A
	CursorUp			= 0x0B,		// 0x0B
	CursorRight			= 0x0C,		// 0x0C
	Return				= 0x0D,		// 0x0D
	ClearToEndOfLine	= 0x0E,		// 0x0E
	PrintBMP			= 0x0F,		// 0x0F, <byte[charheight]>
	//PrintImmediateGraphicsChar = 0x0F,	// old name
	SetAttributes		= 0x10,		// 0x10, <byte:attributes>
	XON					= 0x11,		// 0x11
	RepeatNextChar		= 0x12,		// 0x12, <byte:count>
	XOFF				= 0x13,		// 0x13
	//SetBackgroundColor = 0x14, 	// 0x14, <byte:color>	OLD, was actually not used, due to code size limit
	SetColors			= 0x14,		// <byte:which> <byte:a> <byte:r> <byte:g> <byte:b>
	ScrollScreen		= 0x15,		// 0x15, <byte:dir>
	ClearWindow			= 0x16,		// 0x16, <byte:rows>, <byte:cols>
	CopyWindow			= 0x17,		// 0x17, <byte:dest_row>, <byte:dest_col>, <byte:rows>, <byte:cols>
	EchoChar			= 0x18,		// 0x18, <byte:char>
	MoveWindow			= 0x19,		// 0x19, <byte:col>						NOT SUPPORTED
	SetFramebufferSize	= 0x1A,		// 0x1A, <byte:rows>, <byte:cols>		NOT SUPPORTED
	//SetLogicalScreenSize = 0x1A,	// old name
	Escape				= 0x1B,		// 0x1B, <byte:control>
	GetBmpOfCharMatrix	= 0x1C,		// 0x1C, <byte:char>					NOT SUPPORTED
	GetBmpAtCursor		= 0x1D,		// 0x1D  -->  0x1D, <byte[charheight]>	NOT SUPPORTED
	Identify			= 0x1E,		// 0x1E  -->  0x1E, "MyLCD,LM641541,ssz=40*80,fsz=42*128,csz=12*8,gs=2\n"
	Controlcode_0x1F	= 0x1F,		// 0x1F  NOP
};






// IDs for baud rates as used in ESC + <byte:setting>
// do not change!
enum Baudrate
{
	Baud2400	= 6,
	Baud3600	= 7,
	Baud4800	= 8,
	Baud7200	= 9,
	Baud9600	= 10,
	Baud14k4	= 11,
	Baud19k2	= 12,
	Baud28k8	= 13,
	Baud38k4	= 14,
	Baud57k6	= 15,
	Baud76k8	= 16,
	Baud115k2	= 17,
	Baud153k6	= 18,
	Baud230k4	= 19
};


















