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
1 changed files with 105 additions and 79 deletions

View File

@ -78,6 +78,7 @@ console_init(void)
{ {
mmask_t oldmask; mmask_t oldmask;
static unsigned char done; static unsigned char done;
if(done) { if(done) {
return; return;
} }
@ -87,13 +88,19 @@ console_init(void)
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++)
for(bg = 0; bg < 8; bg++)
init_pair(MKPAIR(bg, fg), fg, 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,8 +155,7 @@ 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,6 +242,7 @@ 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:
@ -243,7 +261,7 @@ console_cputc(char c)
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,20 +344,25 @@ 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;
} }
@ -355,6 +378,7 @@ console_readkey(int k)
case KEY_DOWN: case KEY_DOWN:
key = CH_CURS_DOWN; key = CH_CURS_DOWN;
break; break;
case KEY_F(9): /* Gnome uses F10 as menu trigger now... */
case KEY_F(10): case KEY_F(10):
key = CTK_CONF_MENU_KEY; key = CTK_CONF_MENU_KEY;
break; break;
@ -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;
} }