/*
 *	Demo program to investigate event handling
 *
 *  Graham R. Cobb			13 Dec 2001
 */

#include "rex/rex.h"
#include <stdlib.h> 

/* Function prototypes */
extern void trace (unsigned short s, unsigned short n1, unsigned short n2); // Defined in about.c
extern void debug (char *s, unsigned short n1, unsigned short n2); // Defined in about.c
extern void display_about (char *about_text); // Defined in about.c
void main();

extern const char *title, *about; // Defined in about.c

#define EVT_ENABLE 1
#define EVT_READ_MESSAGE_LEAVE 0
#define EVT_READ_MESSAGE_DELETE 1
#define EVT_BUTTON 0x50 // Non-repeating event
#define EVT_SCREEN 0x80 // Repeating event

void main() {
	MSG msg;
	char str1[10], str2[10], str3[10], *text_msg, *text_scode, *text_bcode;
	int flag, row_height, row;

	//debug("Hi",0,-1);
	//trace(1,2,3);
	display_about(about);
	//trace(4,5,6);
	
	DsClearScreen(); DsEventClear();

	DsPrintf(0,0,0x10,title); // x, y style (bold), text
	DsDisplayLine( 0,11,240,0,0xff );/*start x,y, end x offset, y offset, bitpattern*/

	DsEventAdd( 0 , 0 , 120, 120 , EVT_SCREEN , EVT_ENABLE);
	
	DsDialogTextButton(90,90,70,20,EVT_BUTTON,"Continue");
	DsDisplayLine( 0,0,240,120,0xaa );/*start x,y, end x offset, y offset, bitpattern*/

	row=12; row_height=12; flag=3;
	while(flag>0) {
		text_msg="";
		DsEventMessageGet(msg);
		
		// Initialise default text
		itoa(str1, msg.message);
		text_msg = str1;
		itoa(str2, msg.bCode);
		text_bcode = str2;
		itoa(str3, msg.sCode);
		text_scode = str3;

		switch (msg.message) {
		case MSG_DS_CLOSE:
			flag--;
			text_msg="CLOSE";
			break;
		case MSG_DS_PAINT:
			text_msg="PAINT";
			break;
		case MSG_DS_TIMER1:
			text_msg="TIMER1";
			break;
		case MSG_DS_TOUCH_DOWN:
			text_msg="TOUCH_D";
			switch (msg.bCode) {
			case MSG_DS_TOUCH_DOWN:
				text_bcode = "Down";
				break;
			case MSG_DS_TOUCH_MOVE:
				text_bcode = "Move";
				break;
			case MSG_DS_TOUCH_UP:
				text_bcode = "Up";
				break;
			}
			break;
			
		case MSG_DS_KEY_DOWN:
			text_msg="KEY_DOWN";
			switch (msg.sCode) {
			case KEY_TOP_A:
				text_scode="TOP key down (A)";
				break;
			case KEY_TOP_B:
				text_scode="TOP key held <1 second (B)";
				break;
			case KEY_TOP_C:
				text_scode="TOP key up <1 second (C)";
				break;
			case KEY_TOP_D:
				text_scode="TOP key held >1 second (D)";
				break;
			case KEY_TOP_E:
				text_scode="TOP key up >1 second (E)";
				break;
			case KEY_BACK_A:
				text_scode="BACK key down (A)";
				break;
			case KEY_BACK_B:
				text_scode="BACK key held <1 second (B)";
				break;
			case KEY_BACK_C:
				text_scode="BACK key up <1 second (C)";
				break;
			case KEY_BACK_D:
				text_scode="BACK key held >1 second (D)";
				break;
			case KEY_BACK_E:
				text_scode="BACK key up >1 second (E)";
				break;
			case KEY_ENTER_A:
				text_scode="ENTER key down (A)";
				break;
			case KEY_ENTER_B:
				text_scode="ENTER key held <1 second (B)";
				break;
			case KEY_ENTER_C:
				text_scode="ENTER key up <1 second (C)";
				break;
			case KEY_ENTER_D:
				text_scode="ENTER key held >1 second (D)";
				break;
			case KEY_ENTER_E:
				text_scode="ENTER key up >1 second (E)";
				break;
			case KEY_UP_A:
				text_scode="UP key down (A)";
				break;
			case KEY_UP_B:
				text_scode="UP key held <1 second (B)";
				break;
			case KEY_UP_C:
				text_scode="UP key up <1 second (C)";
				break;
			case KEY_UP_D:
				text_scode="UP key held >1 second (D)";
				break;
			case KEY_UP_E:
				text_scode="UP key up >1 second (E)";
				break;
			case KEY_DOWN_A:
				text_scode="DOWN key down (A)";
				break;
			case KEY_DOWN_B:
				text_scode="DOWN key held <1 second (B)";
				break;
			case KEY_DOWN_C:
				text_scode="DOWN key up <1 second (C)";
				break;
			case KEY_DOWN_D:
				text_scode="DOWN key held >1 second (D)";
				break;
			case KEY_DOWN_E:
				text_scode="DOWN key up >1 second (E)";
				break;
			}
			break;
			
		case MSG_DS_COMMAND:
			text_msg="COMMAND";
			switch (msg.bCode) {
			case EVT_BUTTON:
				text_bcode = "Button";
				break;
			case EVT_SCREEN:
				text_bcode = "Screen";
				break;
			}
			switch (msg.sCode) {
			case MSG_DS_COMMAND_DOWN:
				text_scode = "Down";
				break;
			case MSG_DS_COMMAND_REPEAT:
				text_scode = "Repeat";
				break;
			case MSG_DS_COMMAND_UP:
				text_scode = "Up";
				break;
			}
			break;
		}
		
		DsDisplayBlockClear(0,row,240,row_height); // x, y, width, height
		DsPrintf(0,row,0,text_msg); 
		DsPrintf(60,row,0,text_bcode); 
		DsPrintf(120,row,0,text_scode);
		
		row += row_height;
		
		if (row>(120-row_height)) row = 0;
	
	}
	DsClearScreen();
	DsAddinTerminate();
}

