ctk-curses: Cleanup + fixes

* Cleanup
* Fix warnings
* Fix indentation
* Only wait 1ms for keyboard timeout
* Hide text cursor
* Pump mouse events just in case
* Add F9 as menu key since F10 is used as menu trigger by Gnome
This commit is contained in:
François Revol 2012-06-23 13:07:19 +02:00
parent db982f7b82
commit 1c5d6a9db7

View file

@ -59,12 +59,12 @@ static unsigned char color;
static unsigned char reversed; static unsigned char reversed;
static ctk_arch_key_t keys[256]; static ctk_arch_key_t keys[256];
static unsigned char keys_in, keys_out; static unsigned char keys_in, keys_out;
static unsigned char available; static unsigned char available;
static unsigned short xpos; static unsigned short xpos;
static unsigned short ypos; static unsigned short ypos;
static unsigned char button; static unsigned char button;
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
static void static void
@ -78,22 +78,29 @@ console_init(void)
{ {
mmask_t oldmask; mmask_t oldmask;
static unsigned char done; static unsigned char done;
if(done) { if(done) {
return; return;
} }
done = 1; done = 1;
stdinhandle = STDIN_FILENO; stdinhandle = STDIN_FILENO;
stdouthandle = STDOUT_FILENO; stdouthandle = STDOUT_FILENO;
/* will display an error and exit if the term can't be initialized */ /* will display an error and exit if the term can't be initialized */
/*setupterm((char *)0, STDOUT_FILENO, (int *)0);*/ /*setupterm((char *)0, STDOUT_FILENO, (int *)0); */
initscr(); initscr();
start_color(); start_color();
cbreak(); cbreak();
/* don't echo typed characters */
noecho(); noecho();
/*nonl();*/ /*nonl(); */
/* hide text cursor, CTK draws its own */
curs_set(0);
intrflush(stdscr, FALSE); intrflush(stdscr, FALSE);
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
@ -105,14 +112,17 @@ console_init(void)
/* curses color handling is weird... */ /* curses color handling is weird... */
{ {
int bg, fg; int bg, fg;
for (fg = 0; fg < 8; fg++)
for (bg = 0; bg < 8; bg++) for(fg = 0; fg < 8; fg++)
init_pair(MKPAIR(bg, fg), fg, bg); for(bg = 0; bg < 8; bg++)
init_pair(MKPAIR(bg, fg), fg, bg);
} }
/* set window title */
putp("\033]0;Contiki\a"); putp("\033]0;Contiki\a");
timeout(25); /* don't block on read, just timeout 1ms */
timeout(1);
signal(SIGINT, ctrlhandler); signal(SIGINT, ctrlhandler);
atexit(console_exit); atexit(console_exit);
@ -145,9 +155,8 @@ console_resize(void)
screensize(&new_width, &new_height); screensize(&new_width, &new_height);
if(new_width != width || if(new_width != width || new_height != height) {
new_height != height) { width = new_width;
width = new_width;
height = new_height; height = new_height;
return 1; return 1;
} }
@ -160,11 +169,12 @@ setcolor(void)
{ {
int bg, fg; int bg, fg;
int attrs; int attrs;
fg = (color & 0x0F); fg = (color & 0x0F);
bg = (color & 0xF0) >> 4; bg = (color & 0xF0) >> 4;
attrs = COLOR_PAIR(MKPAIR(bg, fg)); attrs = COLOR_PAIR(MKPAIR(bg, fg));
if (reversed) if(reversed)
attrs |= WA_REVERSE; attrs |= WA_REVERSE;
attrset(attrs); attrset(attrs);
} }
@ -173,7 +183,9 @@ unsigned char
wherex(void) wherex(void)
{ {
int x, y; int x, y;
getyx(stdscr, y, x); getyx(stdscr, y, x);
(void)y;
return (unsigned char)x; return (unsigned char)x;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -181,7 +193,9 @@ unsigned char
wherey(void) wherey(void)
{ {
int x, y; int x, y;
getyx(stdscr, y, x); getyx(stdscr, y, x);
(void)x;
return (unsigned char)y; return (unsigned char)y;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -194,6 +208,8 @@ clrscr(void)
void void
bgcolor(unsigned char c) bgcolor(unsigned char c)
{ {
color = ((c << 4) | (color & 0xF0));
/* Presume this to be one of the first calls. */ /* Presume this to be one of the first calls. */
console_init(); console_init();
} }
@ -209,6 +225,7 @@ void
screensize(unsigned char *x, unsigned char *y) screensize(unsigned char *x, unsigned char *y)
{ {
int mx, my; int mx, my;
getmaxyx(stdscr, my, mx); getmaxyx(stdscr, my, mx);
*x = (unsigned char)mx; *x = (unsigned char)mx;
*y = (unsigned char)my; *y = (unsigned char)my;
@ -225,25 +242,26 @@ void
console_cputc(char c) console_cputc(char c)
{ {
int ch = c; int ch = c;
/* usually ACS_* don't fit in a char */ /* usually ACS_* don't fit in a char */
switch (c) { switch (c) {
case CH_ULCORNER: case CH_ULCORNER:
ch = ACS_ULCORNER; ch = ACS_ULCORNER;
break; break;
case CH_LLCORNER: case CH_LLCORNER:
ch = ACS_LLCORNER; ch = ACS_LLCORNER;
break; break;
case CH_URCORNER: case CH_URCORNER:
ch = ACS_URCORNER; ch = ACS_URCORNER;
break; break;
case CH_LRCORNER: case CH_LRCORNER:
ch = ACS_LRCORNER; ch = ACS_LRCORNER;
break; break;
default: default:
break; break;
} }
addch(ch); addch(ch);
/*refresh();*/ /*refresh(); */
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void void
@ -257,7 +275,7 @@ void
cclear(unsigned char length) cclear(unsigned char length)
{ {
hline(' ', length); hline(' ', length);
/*refresh();*/ /*refresh(); */
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void void
@ -326,63 +344,69 @@ console_readkey(int k)
{ {
ctk_arch_key_t key; ctk_arch_key_t key;
key = (ctk_arch_key_t)k; key = (ctk_arch_key_t) k;
/*fprintf(stderr, "key: %d\n", k);*/ /*fprintf(stderr, "key: %d\n", k); */
switch (k) { switch (k) {
case KEY_MOUSE: case KEY_MOUSE:
{ {
MEVENT event; MEVENT event;
if (getmouse(&event) == OK) {
if(getmouse(&event) == OK) {
xpos = event.x; xpos = event.x;
ypos = event.y; ypos = event.y;
button = event.bstate & BUTTON1_PRESSED button = event.bstate & BUTTON1_PRESSED
|| event.bstate & BUTTON1_CLICKED || event.bstate & BUTTON1_CLICKED
|| event.bstate & BUTTON1_DOUBLE_CLICKED; || event.bstate & BUTTON1_DOUBLE_CLICKED;
/*fprintf(stderr, "mevent: %d: %d, %d, %d, %x ; %d\n", /*fprintf(stderr, "mevent: %d: %d, %d, %d, %lx ; %d\n",
event.id, event.x, event.y, event.z, (int)event.bstate, button);*/ event.id, event.x, event.y, event.z, event.bstate, button); */
}
/* just in case */
while(getmouse(&event) == OK) {
/*fprintf(stderr, "pumped mevent\n"); */
} }
return; return;
} }
case KEY_LEFT: case KEY_LEFT:
key = CH_CURS_LEFT; key = CH_CURS_LEFT;
break; break;
case KEY_UP: case KEY_UP:
key = CH_CURS_UP; key = CH_CURS_UP;
break; break;
case KEY_RIGHT: case KEY_RIGHT:
key = CH_CURS_RIGHT; key = CH_CURS_RIGHT;
break; break;
case KEY_DOWN: case KEY_DOWN:
key = CH_CURS_DOWN; key = CH_CURS_DOWN;
break; break;
case KEY_F(10): case KEY_F(9): /* Gnome uses F10 as menu trigger now... */
key = CTK_CONF_MENU_KEY; case KEY_F(10):
break; key = CTK_CONF_MENU_KEY;
case KEY_ENTER: break;
key = CH_ENTER; case KEY_ENTER:
break; key = CH_ENTER;
case 127: break;
case KEY_BACKSPACE: case 127:
case KEY_DC: case KEY_BACKSPACE:
key = CH_DEL; case KEY_DC:
break; key = CH_DEL;
case KEY_BTAB: break;
case KEY_CTAB: case KEY_BTAB:
case KEY_PPAGE: case KEY_CTAB:
case KEY_PREVIOUS: case KEY_PPAGE:
key = CTK_CONF_WIDGETUP_KEY; case KEY_PREVIOUS:
break; key = CTK_CONF_WIDGETUP_KEY;
case KEY_NPAGE: break;
case KEY_NEXT: case KEY_NPAGE:
key = CTK_CONF_WIDGETDOWN_KEY; case KEY_NEXT:
break; key = CTK_CONF_WIDGETDOWN_KEY;
case KEY_STAB: break;
case KEY_HOME: case KEY_STAB:
case KEY_END: case KEY_HOME:
key = CTK_CONF_WINDOWSWITCH_KEY; case KEY_END:
break; key = CTK_CONF_WINDOWSWITCH_KEY;
default: break;
break; default:
break;
} }
if(key == 0) { if(key == 0) {
return; return;
@ -397,8 +421,9 @@ static void
console_read(void) console_read(void)
{ {
int k; int k;
k = getch(); k = getch();
if (k != ERR) if(k != ERR)
console_readkey(k); console_readkey(k);
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -407,6 +432,7 @@ ctk_arch_getkey(void)
{ {
console_read(); console_read();
char k = keys[keys_out++]; char k = keys[keys_out++];
available--; available--;
return k; return k;
} }