/*
 * this program demonstates one way to handle arrow escape sequences using
 * bsd curses.
 *
 *  to compile:  cc -o demo demo.c -lcurses -ltermcap
 *			
 */

#include <curses.h>
#include "arrows.h"

#include <stdio.h>

extern char *getenv();

/* array to hold arrow escape sequences */
char 	arrow_table[4][80];


/*
 * get arrow terminal capabilities for terminal from termcap file
 * curses uses the global variable ttytype to specify what it
 * thinks your terminal type is
 */
get_arrow_info()
{

	char buffer[1024];
	char *bp;

	tgetent(buffer,ttytype);

	bp = arrow_table[MY_KEY_RIGHT];
	tgetstr("kr",&bp);

	bp = arrow_table[MY_KEY_LEFT];
	tgetstr("kl",&bp);

	bp = arrow_table[MY_KEY_UP];
	tgetstr("ku",&bp);

	bp = arrow_table[MY_KEY_DOWN];
	tgetstr("kd",&bp);
}


/*
 * read in the escape sequence and compare it to what we got from
 * termcap entry.
 */
do_escape(c)
  int c;
{
	char buffer[8]; /* buffer to store entire escape sequence */
	int size;	/* length of the escape sequence */
	int  i;

	/* we assume that all arrow sequences are the same length */
	/* so we will just get the size of one of them.           */
	size=strlen(arrow_table[MY_KEY_RIGHT]);

	/* the first character was an escape */
	buffer[0]=ESCAPE;

	/* read in the rest of the escape sequence */
	for(i=1;i<size;i++) {

		buffer[i] = getch();

		/* this hack takes care of vt100s which termcap specifies
		 * as sending <esc>O[ABCD] but which sometimes send
		 *  <esc>[[ABCD]
		 */
		if(i == 1) 
		    if((buffer[i] == '[') && (arrow_table[MY_KEY_RIGHT][1] == 'O'))
				buffer[i] = 'O';

	}

	/* add a null terminator to our escape sequence */
	buffer[size] = '\0';

	/* compare what we read in to what termcap says */
	/* if we get a match, return the proper result  */
        if(!strcmp(buffer,arrow_table[MY_KEY_RIGHT]))
                return(MY_KEY_RIGHT);
        else if(!strcmp(buffer,arrow_table[MY_KEY_LEFT]))
                return(MY_KEY_LEFT);
        else if(!strcmp(buffer,arrow_table[MY_KEY_UP]))
                return(MY_KEY_UP);
        else if(!strcmp(buffer,arrow_table[MY_KEY_DOWN]))
                return(MY_KEY_DOWN);
        else
                return(ESCAPE);
}

#if 0
main()
{
	WINDOW *screen;
	int	x = 5, y = 5,  		/* location of our window */
		len = 30, width = 10;	/* dimensions of our window */
	char	c;

	/* initialize curses and enter cbreak mode */
	initscr();
	noecho();
	crmode();


	/* read in our arrow termcap entries */
	get_arrow_info();


	/* create our window and display it */
	screen = newwin(width,len,y,x);
	waddstr(screen,"\n\n\n\n\tHELLO WORLD\n");
	box(screen,'+','-'); 
	wrefresh(screen);

	/* read in characters and move window accordingly */
	while(c != 'x') {
		c = wgetch(screen);
		if ( c == ESCAPE ) {
			c = do_escape();
		}
		switch(c) {
			case MY_KEY_LEFT : if( x != 0 )
						mvwin(screen,y,--x);
					wrefresh(screen);
					break;
			case MY_KEY_DOWN :	if( y+width < LINES)
						mvwin(screen,++y,x);
					wrefresh(screen);
					break;
			case MY_KEY_UP :	if( y != 0 )
						mvwin(screen,--y,x);
					wrefresh(screen);
					break;
			case MY_KEY_RIGHT :if( x+len < COLS)
						mvwin(screen,y,++x);
					wrefresh(screen);
					break;
			case 'c' :	clear();
					refresh();
					touchwin(screen);
					wrefresh(screen);
					break;
		}
	}

	/* you should always call endwin before you exit a curses programs */
	/* so that the terminal is restored to its original state */
	clear();
	refresh();
	endwin();
	exit(0);
}
#endif
