#include <../arch/arch.h>
#include <../arch/common/glctx.h>

vboolean autowarp = false;
#include "dumpevt.c"

#define N 4

int main(void)
{
	int i, count = N;
	vboolean enable_kb_repeat = true;
	arch_window_p w, wins[N];
	ui_event_t ev;

	logStartConsoleLog();
	logUpperLevel(LOG_TRACE);

	for(i = 0;i < N;i++)
	{
		wins[i] = archWinOpenNP(NULL,NULL,"test",10 + 120 * i,10,100,100,true,false);
		assert(wins[i]);
		archWinShow(wins[i],true);
	}
	
	for(i = 0;count;i = ++i % N)
	{
		w = wins[i];
		if(!w)
			continue;
		while(!archWinWaitMessages(5000))
			puts("No messages ...");
		archWinPoll(w, false);
		
		while(w->msg_que)
		{
			DEQ_POP_BACK(event,w->msg_que,&ev);
			dump_event(i, &ev);
			if(ev.type != ui_key_event)
				continue;
			switch(ev.key.scan)
			{
				case sc_escape:
					if(!ev.key.press)
						goto close_win;
					break;
				case sc_s:
					archWinShow(w, false);
					archWinPoll(w, false);
					while(w->msg_que)
					{
						DEQ_POP_BACK(event,w->msg_que,&ev);
						dump_event(i, &ev);
					}
					timeDelay(3000);
					archWinShow(w, true);
					break;
				case sc_p:
					archWinShowPointer(w, true);
					break;
				case sc_o:
					archWinShowPointer(w, false);
					break;
				case sc_a:
					if(ev.key.press)
						archWinAutoRepeat(w,enable_kb_repeat = !enable_kb_repeat);
					break;
				case sc_w:
					archWinWarpPointer(w, 10, 10);
					/* archWinAutoWarp(w,autowrap = !autowrap); */
					break;
				default:
					break;
			}
		}
		continue;

close_win:;
		printf("Window %d: closing.\n", i);
		archWinClose(wins[i]);
		wins[i] = NULL;
		count--;

	}

	memPrintStats();
	return 0;
}

