
#include "spec.h"

void CursesInit(Co co){
	initscr();
	cbreak();
	noecho();
    curs_set(0);
	keypad(stdscr,1);
	mousemask(ALL_MOUSE_EVENTS, NULL);
	nodelay(stdscr,1);
	if (has_colors() == 0 ){
		printw("no color");
        co->routine = coEnd; 
        return;
	}
	else{
		start_color();
		use_default_colors();
        short ofg=0, obg=0;
        pair_content(-1,&obg, &ofg);
        short palette[] = {-1, COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_BLUE,
                              COLOR_CYAN, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE }; 
        for(int c=0; c < 81; c++){
            short fg = c%9;
            short bg = c/9;
            init_pair(c,palette[bg],palette[fg]);
        }
	}
}

void coInput(Co co){
	MEVENT mouse;
	int k = getch();
	while (k != ERR){
        char tmp[] = {k,0};
        zmq_send(co->pub,tmp,1,ZMQ_NOBLOCK);
        switch(k){
            case KEY_MOUSE:
                if (getmouse(&mouse) == OK){
                }
                break;
            case 27 : 
                if (getch() == ERR){
                    co->routine = coEnd;
                }
                break;
            case KEY_UP:
                break;
            case KEY_DOWN:
                break;
            case KEY_LEFT:
                break;
            case KEY_RIGHT:
                break;
            case KEY_ENTER:
                break;
            case KEY_BACKSPACE:
                break;
        }
        // if the key repeat, just ignore until it pop out
        // no way to handle multiple held key
        k = getch();
	}
}
