/**********************************************************************
 * Nano-GUI -- a Nano-X environment for Linux                         *
 *                          By Darran D. Rimron <darran@rimron.co.uk> *
 *                                                                    *
 * Based on Nano-Term, a Nano-X demo                                  *
 *                                         By Al Riddoch & Greg Haerr *
 *                                                                    *
 * ---------+--------+---------+------------------------------------- *
 * Date     | Author | Version | Reason                               *
 * ---------+--------+---------+------------------------------------- *
 * 19991112 | Rimron | 0.0.1   | Start Application                    *
 * 19991226 | Rimron | 0.0.2   | Add clock, improve terminal          *
 * ---------+--------+---------+------------------------------------- *
 * See NanoGUI.CHANGES and CHANGELOG for full and graphic change info *
 * ------------------------------------------------------------------ *
 *                                                                    *
 *      Best viewed with console-vim & a tabstop of 3 (:set ts=3)     *
 **********************************************************************/

#define	DISABLED       0
#define  ANALOG         1
#define  DIGITAL        2
#define  CALANDER       3

time_t 	then;						/* when the clock was last plotted  */
int		CLOCK_TYPE;				/* what type of clock does the punter get? */

/* The clock, when in analog mode needs trig. *yuk* */
unsigned char trig[91] =
	{ 0, 4, 8, 13, 17, 22, 26, 31, 35, 40, 44, 48, 53, 57, 61, 66,
	70, 74, 79, 83, 87, 91, 95, 100, 104, 108, 112, 116, 120, 124, 128,
	131, 135, 139, 143, 146, 150, 154, 157, 161, 164, 167, 171, 174, 177,
	181, 184, 187, 190, 193, 196, 198, 201, 204, 207, 209, 212, 214, 217,
	219, 221, 223, 226, 228, 230, 232, 233, 235, 237, 238, 240, 242, 243,
	244, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 254, 255, 255,
	255, 255, 255, 255}; 

/*void update_clock();
void do_exposure();
void draw_time(int hour, int min, int sec*//*, GR_GC_ID gc *//*);*/

void update_clock(void)
{
	struct timeval tv;
	struct timezone tz;
	struct tm * tp;
	time_t now;
/*	static time_t then;*/
	int min, hour, sec;
	int xtimepos;

	poll_second_functions();

	gettimeofday(&tv, &tz);
	now = tv.tv_sec - (60 * tz.tz_minuteswest);
	if (now == then) {
		return;
	}
	then = now;
	tp = gmtime(&now);
	min = tp->tm_min;
	sec = tp->tm_sec;
/*	min = tp->tm_min * 6;
	sec = tp->tm_sec * 6;
*/	hour = tp->tm_hour;
	if (hour > 12) {
		hour -= 12;
	}
/*	switch(CLOCK_TYPE) {
		case DIGITAL:
		{	
*/			GrSetGCForeground(gc1, CLOCK_PEN_COLOR);
			GrSetGCBackground(gc1, CLOCK_PAPER_COLOR);
			xtimepos=1;
			if(hour<10)
			{
				GrText(clock_window, gc1, xtimepos, 9 + height /* + base */, "0", 1);
				xtimepos += width;
			}
			if(hour>9)
			{
				GrText(clock_window, gc1, xtimepos, 9 + height /* + base */, "1", 1);
				xtimepos += width;
				hour -=10;
			}
//			GrText(clock_window, gc1, xtimepos, 9 + height /* + base , hour*/, "H", 1);
			GrText(clock_window, gc1, xtimepos, 9 + height /* + base */, "H", 1);
			xtimepos +=width;
			GrText(clock_window, gc1, xtimepos, 9 + height /* + base */, ":", 1);
/*		}
		case ANALOG:
		{
			hour *= 30;
			do_exposure();
			draw_time(hour, min, sec, gc2);
		}
		case CALANDER:
		{
		}
	}

*/}

int bsin(int angle)
{
	if(angle < 91) {
		return trig[angle];
	} else if (angle < 181) {
		return trig[180 - angle];
	} else if (angle < 271) {
		return -trig[angle - 180];
	} else if (angle < 361) {
		return -trig[360 - angle];
	} else {
		return 0;
	}
}

int bcos(int angle)
{
	if(angle < 91) {
		return trig[90 - angle];
	} else if (angle < 181) {
		return -trig[angle - 90];
	} else if (angle < 271) {
		return -trig[270 - angle];
	} else if (angle < 361) {
		return trig[angle - 270];
	} else {
		return 0;
	}
}

void
do_exposure(ep)
	GR_EVENT_EXPOSURE	*ep;
{
	GR_COORD	midx = 33;
	GR_COORD	midy = 33;
	int i, l;

	GrEllipse(clock_window, gc2, midx, midy, midx - 1, midy - 1);
	for(i = 0; i < 60; i++) {
		if (i%5 == 0) {
			l = 5;
		} else {
			l = 0;
		}
		GrLine(clock_window, gc2,
			midx + (((midx - 3) * bsin(i * 6)) >> 8), 
			midy + (((midy - 3) * bcos(i * 6)) >> 8), 
			midx + (((midx - 3 - l) * bsin(i * 6)) >> 8), 
			midy + (((midy - 3 - l) * bcos(i * 6)) >> 8));
	}
	
	
}

void draw_time(int hour, int min, int sec/*, GR_GC_ID gc*/ )
{
	GR_COORD	midx = 33;
	GR_COORD	midy = 33;
	static int lh = -1, lm = -1, ls = -1;

	GrLine(clock_window, gc1,
		midx + (((midx - 8 - midx / 10) * bsin(ls)) >> 8), 
		midy - (((midy - 8 - midy / 10) * bcos(ls)) >> 8), 
		midx + (((midx - 8 - midx / 4) * bsin(ls)) >> 8), 
		midy - (((midy - 8 - midx / 4) * bcos(ls)) >> 8));
	GrLine(clock_window, gc2,
		midx + (((midx - 8 - midx / 10) * bsin(sec)) >> 8), 
		midy - (((midy - 8 - midy / 10) * bcos(sec)) >> 8), 
		midx + (((midx - 8 - midx / 4) * bsin(sec)) >> 8), 
		midy - (((midy - 8 - midx / 4) * bcos(sec)) >> 8));
	if ((lm != min) || (ls == min)) {
		GrLine(clock_window, gc1,
			midx + (((midx - 8 - midx / 10) * bsin(lm)) >> 8), 
			midy - (((midy - 8 - midy / 10) * bcos(lm)) >> 8), 
			midx + (((midx / 5) * bsin(lm)) >> 8), 
			midy - (((midy / 5) * bcos(lm)) >> 8));
		GrLine(clock_window, gc2,
			midx + (((midx - 8 - midx / 10) * bsin(min)) >> 8), 
			midy - (((midy - 8 - midy / 10) * bcos(min)) >> 8), 
			midx + (((midx / 5) * bsin(min)) >> 8), 
			midy - (((midy / 5) * bcos(min)) >> 8));
		GrLine(clock_window, gc1,
			midx + (((midx - 8 - midx / 2) * bsin(lh)) >> 8), 
			midy - (((midy - 8 - midy / 2) * bcos(lh)) >> 8), 
			midx + (((midx / 5) * bsin(lh)) >> 8), 
			midy - (((midy / 5) * bcos(lh)) >> 8));
		GrLine(clock_window, gc2,
			midx + (((midx - 8 - midx / 2) * bsin(hour)) >> 8), 
			midy - (((midy - 8 - midy / 2) * bcos(hour)) >> 8), 
			midx + (((midx / 5) * bsin(hour)) >> 8), 
			midy - (((midy / 5) * bcos(hour)) >> 8));
	}
	lh = hour;
	lm = min;
	ls = sec;
}
