From 25e3f828aa2524d2ed21e16bbed6d770decb18dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Tue, 12 Apr 2011 21:45:27 +0200 Subject: [PATCH 01/25] Ncurses-based CTK backend for the native platform. --- examples/email/Makefile.native.defines | 1 + examples/irc/Makefile.native.defines | 1 + examples/webbrowser/Makefile.native.defines | 1 + platform/native/Makefile.native | 8 +- platform/native/contiki-conf.h | 64 +++ platform/native/contiki-main.c | 13 + platform/native/ctk/ctk-curses.c | 450 ++++++++++++++++++++ platform/native/ctk/ctk-curses.h | 68 +++ 8 files changed, 604 insertions(+), 2 deletions(-) create mode 100644 examples/email/Makefile.native.defines create mode 100644 examples/irc/Makefile.native.defines create mode 100644 examples/webbrowser/Makefile.native.defines create mode 100644 platform/native/ctk/ctk-curses.c create mode 100644 platform/native/ctk/ctk-curses.h diff --git a/examples/email/Makefile.native.defines b/examples/email/Makefile.native.defines new file mode 100644 index 000000000..1b5caf200 --- /dev/null +++ b/examples/email/Makefile.native.defines @@ -0,0 +1 @@ +DEFINES = WITH_GUI diff --git a/examples/irc/Makefile.native.defines b/examples/irc/Makefile.native.defines new file mode 100644 index 000000000..1b5caf200 --- /dev/null +++ b/examples/irc/Makefile.native.defines @@ -0,0 +1 @@ +DEFINES = WITH_GUI diff --git a/examples/webbrowser/Makefile.native.defines b/examples/webbrowser/Makefile.native.defines new file mode 100644 index 000000000..1b5caf200 --- /dev/null +++ b/examples/webbrowser/Makefile.native.defines @@ -0,0 +1 @@ +DEFINES = WITH_GUI diff --git a/platform/native/Makefile.native b/platform/native/Makefile.native index 60591c6a8..d74485149 100644 --- a/platform/native/Makefile.native +++ b/platform/native/Makefile.native @@ -10,12 +10,12 @@ ifeq ($(UIP_CONF_IPV6),1) CFLAGS += -DWITH_UIP6=1 endif -CONTIKI_TARGET_DIRS = . dev +CONTIKI_TARGET_DIRS = . dev ctk CONTIKI_TARGET_MAIN = ${addprefix $(OBJECTDIR)/,contiki-main.o} CONTIKI_TARGET_SOURCEFILES = contiki-main.c clock.c leds.c leds-arch.c \ button-sensor.c pir-sensor.c vib-sensor.c xmem.c \ - sensors.c irq.c cfs-posix.c cfs-posix-dir.c + sensors.c irq.c cfs-posix.c cfs-posix-dir.c ctk-curses.c ifeq ($(HOST_OS),Windows) CONTIKI_TARGET_SOURCEFILES += wpcap-drv.c wpcap.c @@ -30,6 +30,7 @@ CONTIKI_TARGET_SOURCEFILES += tapdev6.c endif endif +CONTIKI_SOURCEFILES += $(CTK) ctk-conio.c CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES) .SUFFIXES: @@ -37,3 +38,6 @@ CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES) ### Define the CPU directory CONTIKI_CPU=$(CONTIKI)/cpu/native include $(CONTIKI)/cpu/native/Makefile.native + +LDFLAGS += -lncurses + diff --git a/platform/native/contiki-conf.h b/platform/native/contiki-conf.h index d916350d5..54d338b45 100644 --- a/platform/native/contiki-conf.h +++ b/platform/native/contiki-conf.h @@ -165,6 +165,70 @@ typedef unsigned short uip_stats_t; #endif /* UIP_CONF_IPV6 */ +#include +#define ctk_arch_isprint isprint + +#include "ctk/ctk-curses.h" + +#define CH_ULCORNER -10 +#define CH_URCORNER -11 +#define CH_LLCORNER -12 +#define CH_LRCORNER -13 +#define CH_ENTER '\n' +#define CH_DEL '\b' +#define CH_CURS_UP -1 +#define CH_CURS_LEFT -2 +#define CH_CURS_RIGHT -3 +#define CH_CURS_DOWN -4 + +#define CTK_CONF_MENU_KEY -5 /* F10 */ +#define CTK_CONF_WINDOWSWITCH_KEY -6 /* Ctrl-Tab */ +#define CTK_CONF_WIDGETUP_KEY -7 /* Shift-Tab */ +#define CTK_CONF_WIDGETDOWN_KEY '\t' +#define CTK_CONF_WIDGET_FLAGS 0 +#define CTK_CONF_SCREENSAVER 0 + +#ifdef PLATFORM_BUILD +#define CTK_CONF_MOUSE_SUPPORT 1 +#define CTK_CONF_WINDOWS 1 +#define CTK_CONF_WINDOWMOVE 1 +#define CTK_CONF_WINDOWCLOSE 1 +#define CTK_CONF_ICONS 1 +#define CTK_CONF_ICON_BITMAPS 0 +#define CTK_CONF_ICON_TEXTMAPS 1 +#define CTK_CONF_MENUS 1 +#define CTK_CONF_MENUWIDTH 16 +#define CTK_CONF_MAXMENUITEMS 10 +#else /* PLATFORM_BUILD */ +#define CTK_CONF_MOUSE_SUPPORT 1 +#define CTK_CONF_WINDOWS 0 +#define CTK_CONF_WINDOWMOVE 0 +#define CTK_CONF_WINDOWCLOSE 0 +#define CTK_CONF_ICONS 0 +#define CTK_CONF_MENUS 0 +#endif /* PLATFORM_BUILD */ + +/* curses doesn't define this one */ +#define COLOR_GRAY COLOR_CYAN + +#define COLOR_BG COLOR_BLUE + +#define BORDERCOLOR COLOR_BLACK +#define SCREENCOLOR COLOR_BLACK +#define BACKGROUNDCOLOR COLOR_BLACK +#define WINDOWCOLOR_FOCUS COLOR_WHITE | COLOR_BG * 0x10 +#define WINDOWCOLOR COLOR_GRAY | COLOR_BG * 0x10 +#define DIALOGCOLOR COLOR_WHITE | COLOR_BG * 0x10 +#define WIDGETCOLOR_HLINK COLOR_CYAN | COLOR_BG * 0x10 +#define WIDGETCOLOR_FWIN COLOR_WHITE | COLOR_BG * 0x10 +#define WIDGETCOLOR COLOR_GRAY | COLOR_BG * 0x10 +#define WIDGETCOLOR_DIALOG COLOR_WHITE | COLOR_BG * 0x10 +#define WIDGETCOLOR_FOCUS COLOR_YELLOW | COLOR_BG * 0x10 +#define MENUCOLOR COLOR_WHITE | COLOR_BG * 0x10 +#define OPENMENUCOLOR COLOR_WHITE | COLOR_BG * 0x10 +#define ACTIVEMENUITEMCOLOR COLOR_YELLOW | COLOR_BG * 0x10 + + typedef unsigned long clock_time_t; #define CLOCK_CONF_SECOND 1000 diff --git a/platform/native/contiki-main.c b/platform/native/contiki-main.c index a870670cb..73dca7e47 100644 --- a/platform/native/contiki-main.c +++ b/platform/native/contiki-main.c @@ -43,6 +43,9 @@ #include "contiki.h" #include "net/netstack.h" +#include "ctk/ctk.h" +#include "ctk/ctk-curses.h" + #include "dev/serial-line.h" #include "net/uip.h" @@ -188,6 +191,10 @@ main(int argc, char **argv) process_start(&etimer_process, NULL); ctimer_init(); +#if WITH_GUI + process_start(&ctk_process, NULL); +#endif + set_rime_addr(); queuebuf_init(); @@ -263,6 +270,12 @@ main(int argc, char **argv) } etimer_request_poll(); + +#if WITH_GUI + if(console_resize()) { + ctk_restore(); + } +#endif /* WITH_GUI */ } return 0; diff --git a/platform/native/ctk/ctk-curses.c b/platform/native/ctk/ctk-curses.c new file mode 100644 index 000000000..1a91cbc5c --- /dev/null +++ b/platform/native/ctk/ctk-curses.c @@ -0,0 +1,450 @@ +/* + * Copyright (c) 2011, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * Author: François Revol + */ + +#include +#include +#include +#include +#include +#include + +#include "contiki.h" +#include "ctk/ctk.h" + +#include "ctk-curses.h" + +/* references: + * http://math.hws.edu/orr/s04/cpsc225/curses.html + */ + +#define MKPAIR(bg, fg) (bg << 3 | fg) + +static int stdinhandle; +static int stdouthandle; + +static unsigned char width; +static unsigned char height; + +static unsigned char color; +static unsigned char reversed; + +static ctk_arch_key_t keys[256]; +static unsigned char keys_in, keys_out; +static unsigned char available; + +static unsigned short xpos; +static unsigned short ypos; +static unsigned char button; + +/*-----------------------------------------------------------------------------------*/ +static void +ctrlhandler(int sig) +{ + exit(EXIT_SUCCESS); +} +/*-----------------------------------------------------------------------------------*/ +void +console_init(void) +{ + static unsigned char done; + if(done) { + return; + } + done = 1; + + stdinhandle = STDIN_FILENO; + stdouthandle = STDOUT_FILENO; + + /* will display an error and exit if the term can't be initialized */ + /*setupterm((char *)0, STDOUT_FILENO, (int *)0);*/ + + initscr(); + start_color(); + cbreak(); + noecho(); + /*nonl();*/ + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + + screensize(&width, &height); + + /* curses color handling is weird... */ + { + int bg, fg; + for (fg = 0; fg < 8; fg++) + for (bg = 0; bg < 8; bg++) + init_pair(MKPAIR(bg, fg), fg, bg); + } + + putp("\033]0;Contiki\a"); + + timeout(25); + + signal(SIGINT, ctrlhandler); + atexit(console_exit); + +} +/*-----------------------------------------------------------------------------------*/ +void +console_exit(void) +{ + static unsigned char done; + + if(done) { + return; + } + done = 1; + + revers(0); + clrscr(); + gotoxy(0, 0); + + + endwin(); +} +/*-----------------------------------------------------------------------------------*/ +unsigned char +console_resize(void) +{ + unsigned char new_width; + unsigned char new_height; + + screensize(&new_width, &new_height); + + if(new_width != width || + new_height != height) { + width = new_width; + height = new_height; + return 1; + } + + return 0; +} +/*-----------------------------------------------------------------------------------*/ +static void +setcolor(void) +{ + int bg, fg; + int attrs; + fg = (color & 0x0F); + bg = (color & 0xF0) >> 4; + + attrs = COLOR_PAIR(MKPAIR(bg, fg)); + if (reversed) + attrs |= WA_REVERSE; + attrset(attrs); +} +/*-----------------------------------------------------------------------------------*/ +unsigned char +wherex(void) +{ + int x, y; + getyx(stdscr, y, x); + return (unsigned char)x; +} +/*-----------------------------------------------------------------------------------*/ +unsigned char +wherey(void) +{ + int x, y; + getyx(stdscr, y, x); + return (unsigned char)y; +} +/*-----------------------------------------------------------------------------------*/ +void +clrscr(void) +{ + clear(); +} +/*-----------------------------------------------------------------------------------*/ +void +bgcolor(unsigned char c) +{ + /* Presume this to be one of the first calls. */ + console_init(); +} +/*-----------------------------------------------------------------------------------*/ +void +bordercolor(unsigned char c) +{ + /* Presume this to be one of the first calls. */ + console_init(); +} +/*-----------------------------------------------------------------------------------*/ +void +screensize(unsigned char *x, unsigned char *y) +{ + int mx, my; + getmaxyx(stdscr, my, mx); + *x = (unsigned char)mx; + *y = (unsigned char)my; +} +/*-----------------------------------------------------------------------------------*/ +void +revers(unsigned char c) +{ + reversed = c; + setcolor(); +} +/*-----------------------------------------------------------------------------------*/ +void +console_cputc(char c) +{ + int ch = c; + /* usually ACS_* don't fit in a char */ + switch (c) { + case CH_ULCORNER: + ch = ACS_ULCORNER; + break; + case CH_LLCORNER: + ch = ACS_LLCORNER; + break; + case CH_URCORNER: + ch = ACS_URCORNER; + break; + case CH_LRCORNER: + ch = ACS_LRCORNER; + break; + default: + break; + } + addch(ch); + /*refresh();*/ +} +/*-----------------------------------------------------------------------------------*/ +void +console_cputs(char *str) +{ + addstr(str); + refresh(); +} +/*-----------------------------------------------------------------------------------*/ +void +cclear(unsigned char length) +{ + hline(' ', length); + /*refresh();*/ +} +/*-----------------------------------------------------------------------------------*/ +void +chline(unsigned char length) +{ + hline(ACS_HLINE, length); + refresh(); +} +/*-----------------------------------------------------------------------------------*/ +void +cvline(unsigned char length) +{ + vline(ACS_VLINE, length); +} +/*-----------------------------------------------------------------------------------*/ +void +gotoxy(unsigned char x, unsigned char y) +{ + move(y, x); +} +/*-----------------------------------------------------------------------------------*/ +void +cclearxy(unsigned char x, unsigned char y, unsigned char length) +{ + gotoxy(x, y); + cclear(length); +} +/*-----------------------------------------------------------------------------------*/ +void +chlinexy(unsigned char x, unsigned char y, unsigned char length) +{ + gotoxy(x, y); + chline(length); +} +/*-----------------------------------------------------------------------------------*/ +void +cvlinexy(unsigned char x, unsigned char y, unsigned char length) +{ + gotoxy(x, y); + cvline(length); +} +/*-----------------------------------------------------------------------------------*/ +void +cputsxy(unsigned char x, unsigned char y, char *str) +{ + gotoxy(x, y); + console_cputs(str); +} +/*-----------------------------------------------------------------------------------*/ +void +cputcxy(unsigned char x, unsigned char y, char c) +{ + gotoxy(x, y); + console_cputc(c); +} +/*-----------------------------------------------------------------------------------*/ +void +textcolor(unsigned char c) +{ + color = c; + setcolor(); +} +/*-----------------------------------------------------------------------------------*/ +static void +console_readkey(int k) +{ + ctk_arch_key_t key; + + key = (ctk_arch_key_t)k; + /*fprintf(stderr, "key: %d\n", k);*/ + switch (k) { + case KEY_LEFT: + key = CH_CURS_LEFT; + break; + case KEY_UP: + key = CH_CURS_UP; + break; + case KEY_RIGHT: + key = CH_CURS_RIGHT; + break; + case KEY_DOWN: + key = CH_CURS_DOWN; + break; + case KEY_F(10): + key = CTK_CONF_MENU_KEY; + break; + case KEY_ENTER: + key = CH_ENTER; + break; + case 127: + case KEY_BACKSPACE: + case KEY_DC: + key = CH_DEL; + break; + case KEY_BTAB: + case KEY_CTAB: + case KEY_PPAGE: + case KEY_PREVIOUS: + key = CTK_CONF_WIDGETUP_KEY; + break; + case KEY_NPAGE: + case KEY_NEXT: + key = CTK_CONF_WIDGETDOWN_KEY; + break; + case KEY_STAB: + case KEY_HOME: + case KEY_END: + key = CTK_CONF_WINDOWSWITCH_KEY; + break; + default: + break; + } + if(key == 0) { + return; + } + + memset(keys + keys_in, key, sizeof(ctk_arch_key_t)); + keys_in++; + available++; +} +/*-----------------------------------------------------------------------------------*/ +static void +console_read(void) +{ + int k; + k = getch(); + if (k != ERR) + console_readkey(k); +} +/*-----------------------------------------------------------------------------------*/ +char +ctk_arch_getkey(void) +{ + console_read(); + char k = keys[keys_out++]; + available--; + return k; +} +/*-----------------------------------------------------------------------------------*/ +unsigned char +ctk_arch_keyavail(void) +{ + console_read(); + return available; +} +/*-----------------------------------------------------------------------------------*/ +void +ctk_mouse_init(void) +{ +} +/*-----------------------------------------------------------------------------------*/ +unsigned short +ctk_mouse_x(void) +{ + console_read(); + return xpos; +} +/*-----------------------------------------------------------------------------------*/ +unsigned short +ctk_mouse_y(void) +{ + console_read(); + return ypos; +} +/*-----------------------------------------------------------------------------------*/ +unsigned short +ctk_mouse_xtoc(unsigned short x) +{ + return x; +} +/*-----------------------------------------------------------------------------------*/ +unsigned short +ctk_mouse_ytoc(unsigned short y) +{ + return y; +} +/*-----------------------------------------------------------------------------------*/ +unsigned char +ctk_mouse_button(void) +{ + console_read(); + return button; +} +/*-----------------------------------------------------------------------------------*/ +void +ctk_mouse_hide(void) +{ +} +/*-----------------------------------------------------------------------------------*/ +void +ctk_mouse_show(void) +{ +} +/*-----------------------------------------------------------------------------------*/ diff --git a/platform/native/ctk/ctk-curses.h b/platform/native/ctk/ctk-curses.h new file mode 100644 index 000000000..2661f7f47 --- /dev/null +++ b/platform/native/ctk/ctk-curses.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2006, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * Author: François Revol + */ +#ifndef __CTK_CONSOLE_H__ +#define __CTK_CONSOLE_H__ + +#include + +#define cputc console_cputc +#define cputs console_cputs + +void console_init(void); +void console_exit(void); +unsigned char console_resize(void); + +unsigned char wherex(void); +unsigned char wherey(void); +void clrscr(void); +void bgcolor(unsigned char c); +void bordercolor(unsigned char c); +void screensize(unsigned char *x, unsigned char *y); +void revers(unsigned char c); +void console_cputc(char c); +void console_cputs(char *str); +void cclear(unsigned char length); +void chline(unsigned char length); +void cvline(unsigned char length); +void gotoxy(unsigned char x, unsigned char y); +void cclearxy(unsigned char x, unsigned char y, unsigned char length); +void chlinexy(unsigned char x, unsigned char y, unsigned char length); +void cvlinexy(unsigned char x, unsigned char y, unsigned char length); +void cputsxy(unsigned char x, unsigned char y, char *str); +void cputcxy(unsigned char x, unsigned char y, char c); +void textcolor(unsigned char c); + +char ctk_arch_getkey(void); +unsigned char ctk_arch_keyavail(void); + +#endif /* __CTK_CONSOLE_H__ */ From db982f7b8257e3d5c84c7ae76b27e0fce96ec7aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Thu, 14 Apr 2011 20:26:13 +0200 Subject: [PATCH 02/25] ctk-curses: Add mouse support to the ncurses CTK backend. --- platform/native/ctk/ctk-curses.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/platform/native/ctk/ctk-curses.c b/platform/native/ctk/ctk-curses.c index 1a91cbc5c..1898d617d 100644 --- a/platform/native/ctk/ctk-curses.c +++ b/platform/native/ctk/ctk-curses.c @@ -76,6 +76,7 @@ ctrlhandler(int sig) void console_init(void) { + mmask_t oldmask; static unsigned char done; if(done) { return; @@ -96,6 +97,9 @@ console_init(void) intrflush(stdscr, FALSE); keypad(stdscr, TRUE); + /* done here because ctk_mouse_init() is called before anyway */ + mousemask(ALL_MOUSE_EVENTS, &oldmask); + screensize(&width, &height); /* curses color handling is weird... */ @@ -325,6 +329,20 @@ console_readkey(int k) key = (ctk_arch_key_t)k; /*fprintf(stderr, "key: %d\n", k);*/ switch (k) { + case KEY_MOUSE: + { + MEVENT event; + if (getmouse(&event) == OK) { + xpos = event.x; + ypos = event.y; + button = event.bstate & BUTTON1_PRESSED + || event.bstate & BUTTON1_CLICKED + || event.bstate & BUTTON1_DOUBLE_CLICKED; + /*fprintf(stderr, "mevent: %d: %d, %d, %d, %x ; %d\n", + event.id, event.x, event.y, event.z, (int)event.bstate, button);*/ + } + return; + } case KEY_LEFT: key = CH_CURS_LEFT; break; From 1c5d6a9db709aaf2f1be9fad1c37d9612e0c9de1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Sat, 23 Jun 2012 13:07:19 +0200 Subject: [PATCH 03/25] 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 --- platform/native/ctk/ctk-curses.c | 184 ++++++++++++++++++------------- 1 file changed, 105 insertions(+), 79 deletions(-) diff --git a/platform/native/ctk/ctk-curses.c b/platform/native/ctk/ctk-curses.c index 1898d617d..d660e11cb 100644 --- a/platform/native/ctk/ctk-curses.c +++ b/platform/native/ctk/ctk-curses.c @@ -59,12 +59,12 @@ static unsigned char color; static unsigned char reversed; static ctk_arch_key_t keys[256]; -static unsigned char keys_in, keys_out; -static unsigned char available; +static unsigned char keys_in, keys_out; +static unsigned char available; static unsigned short xpos; static unsigned short ypos; -static unsigned char button; +static unsigned char button; /*-----------------------------------------------------------------------------------*/ static void @@ -78,22 +78,29 @@ console_init(void) { mmask_t oldmask; static unsigned char done; + if(done) { return; } done = 1; - stdinhandle = STDIN_FILENO; + stdinhandle = STDIN_FILENO; stdouthandle = STDOUT_FILENO; /* 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(); start_color(); cbreak(); + + /* don't echo typed characters */ noecho(); - /*nonl();*/ + /*nonl(); */ + + /* hide text cursor, CTK draws its own */ + curs_set(0); + intrflush(stdscr, FALSE); keypad(stdscr, TRUE); @@ -105,14 +112,17 @@ console_init(void) /* curses color handling is weird... */ { int bg, fg; - for (fg = 0; fg < 8; fg++) - for (bg = 0; bg < 8; bg++) - init_pair(MKPAIR(bg, fg), fg, bg); + + for(fg = 0; fg < 8; fg++) + for(bg = 0; bg < 8; bg++) + init_pair(MKPAIR(bg, fg), fg, bg); } + /* set window title */ putp("\033]0;Contiki\a"); - timeout(25); + /* don't block on read, just timeout 1ms */ + timeout(1); signal(SIGINT, ctrlhandler); atexit(console_exit); @@ -145,9 +155,8 @@ console_resize(void) screensize(&new_width, &new_height); - if(new_width != width || - new_height != height) { - width = new_width; + if(new_width != width || new_height != height) { + width = new_width; height = new_height; return 1; } @@ -160,11 +169,12 @@ setcolor(void) { int bg, fg; int attrs; + fg = (color & 0x0F); bg = (color & 0xF0) >> 4; attrs = COLOR_PAIR(MKPAIR(bg, fg)); - if (reversed) + if(reversed) attrs |= WA_REVERSE; attrset(attrs); } @@ -173,7 +183,9 @@ unsigned char wherex(void) { int x, y; + getyx(stdscr, y, x); + (void)y; return (unsigned char)x; } /*-----------------------------------------------------------------------------------*/ @@ -181,7 +193,9 @@ unsigned char wherey(void) { int x, y; + getyx(stdscr, y, x); + (void)x; return (unsigned char)y; } /*-----------------------------------------------------------------------------------*/ @@ -194,6 +208,8 @@ clrscr(void) void bgcolor(unsigned char c) { + color = ((c << 4) | (color & 0xF0)); + /* Presume this to be one of the first calls. */ console_init(); } @@ -209,6 +225,7 @@ void screensize(unsigned char *x, unsigned char *y) { int mx, my; + getmaxyx(stdscr, my, mx); *x = (unsigned char)mx; *y = (unsigned char)my; @@ -225,25 +242,26 @@ void console_cputc(char c) { int ch = c; + /* usually ACS_* don't fit in a char */ switch (c) { - case CH_ULCORNER: - ch = ACS_ULCORNER; - break; - case CH_LLCORNER: - ch = ACS_LLCORNER; - break; - case CH_URCORNER: - ch = ACS_URCORNER; - break; - case CH_LRCORNER: - ch = ACS_LRCORNER; - break; - default: - break; + case CH_ULCORNER: + ch = ACS_ULCORNER; + break; + case CH_LLCORNER: + ch = ACS_LLCORNER; + break; + case CH_URCORNER: + ch = ACS_URCORNER; + break; + case CH_LRCORNER: + ch = ACS_LRCORNER; + break; + default: + break; } addch(ch); - /*refresh();*/ + /*refresh(); */ } /*-----------------------------------------------------------------------------------*/ void @@ -257,7 +275,7 @@ void cclear(unsigned char length) { hline(' ', length); - /*refresh();*/ + /*refresh(); */ } /*-----------------------------------------------------------------------------------*/ void @@ -326,63 +344,69 @@ console_readkey(int k) { ctk_arch_key_t key; - key = (ctk_arch_key_t)k; - /*fprintf(stderr, "key: %d\n", k);*/ + key = (ctk_arch_key_t) k; + /*fprintf(stderr, "key: %d\n", k); */ switch (k) { - case KEY_MOUSE: + case KEY_MOUSE: { MEVENT event; - if (getmouse(&event) == OK) { + + if(getmouse(&event) == OK) { xpos = event.x; ypos = event.y; button = event.bstate & BUTTON1_PRESSED - || event.bstate & BUTTON1_CLICKED - || event.bstate & BUTTON1_DOUBLE_CLICKED; - /*fprintf(stderr, "mevent: %d: %d, %d, %d, %x ; %d\n", - event.id, event.x, event.y, event.z, (int)event.bstate, button);*/ + || event.bstate & BUTTON1_CLICKED + || event.bstate & BUTTON1_DOUBLE_CLICKED; + /*fprintf(stderr, "mevent: %d: %d, %d, %d, %lx ; %d\n", + event.id, event.x, event.y, event.z, event.bstate, button); */ + } + /* just in case */ + while(getmouse(&event) == OK) { + /*fprintf(stderr, "pumped mevent\n"); */ } return; } - case KEY_LEFT: - key = CH_CURS_LEFT; - break; - case KEY_UP: - key = CH_CURS_UP; - break; - case KEY_RIGHT: - key = CH_CURS_RIGHT; - break; - case KEY_DOWN: - key = CH_CURS_DOWN; - break; - case KEY_F(10): - key = CTK_CONF_MENU_KEY; - break; - case KEY_ENTER: - key = CH_ENTER; - break; - case 127: - case KEY_BACKSPACE: - case KEY_DC: - key = CH_DEL; - break; - case KEY_BTAB: - case KEY_CTAB: - case KEY_PPAGE: - case KEY_PREVIOUS: - key = CTK_CONF_WIDGETUP_KEY; - break; - case KEY_NPAGE: - case KEY_NEXT: - key = CTK_CONF_WIDGETDOWN_KEY; - break; - case KEY_STAB: - case KEY_HOME: - case KEY_END: - key = CTK_CONF_WINDOWSWITCH_KEY; - break; - default: - break; + case KEY_LEFT: + key = CH_CURS_LEFT; + break; + case KEY_UP: + key = CH_CURS_UP; + break; + case KEY_RIGHT: + key = CH_CURS_RIGHT; + break; + case KEY_DOWN: + key = CH_CURS_DOWN; + break; + case KEY_F(9): /* Gnome uses F10 as menu trigger now... */ + case KEY_F(10): + key = CTK_CONF_MENU_KEY; + break; + case KEY_ENTER: + key = CH_ENTER; + break; + case 127: + case KEY_BACKSPACE: + case KEY_DC: + key = CH_DEL; + break; + case KEY_BTAB: + case KEY_CTAB: + case KEY_PPAGE: + case KEY_PREVIOUS: + key = CTK_CONF_WIDGETUP_KEY; + break; + case KEY_NPAGE: + case KEY_NEXT: + key = CTK_CONF_WIDGETDOWN_KEY; + break; + case KEY_STAB: + case KEY_HOME: + case KEY_END: + key = CTK_CONF_WINDOWSWITCH_KEY; + break; + default: + break; } if(key == 0) { return; @@ -397,8 +421,9 @@ static void console_read(void) { int k; + k = getch(); - if (k != ERR) + if(k != ERR) console_readkey(k); } /*-----------------------------------------------------------------------------------*/ @@ -407,6 +432,7 @@ ctk_arch_getkey(void) { console_read(); char k = keys[keys_out++]; + available--; return k; } From 9ffaa964753f87e23c7f31d178e0d0639f39ab2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Wed, 20 Mar 2013 01:24:18 +0100 Subject: [PATCH 04/25] ctk-curses: fix build Including curses.h in contiki-conf.h (required for COLOR_* defines) induced a symbol name clash with neighbor-attr.c on "timeout". Renamed timeout to neighbor_timeout. --- core/net/neighbor-attr.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core/net/neighbor-attr.c b/core/net/neighbor-attr.c index 38b42839a..1fb4f8e48 100644 --- a/core/net/neighbor-attr.c +++ b/core/net/neighbor-attr.c @@ -48,7 +48,7 @@ #define PRINTF(...) #endif -static uint16_t timeout = 0; +static uint16_t neighbor_timeout = 0; MEMB(neighbor_addr_mem, struct neighbor_addr, NEIGHBOR_ATTR_MAX_NEIGHBORS); @@ -209,7 +209,7 @@ neighbor_attr_tick(const rimeaddr_t * addr) uint16_t neighbor_attr_get_timeout(void) { - return timeout; + return neighbor_timeout; } /*---------------------------------------------------------------------------*/ static struct ctimer ct; @@ -218,12 +218,12 @@ static struct ctimer ct; static void timeout_check(void *ptr) { - if(timeout > 0) { + if(neighbor_timeout > 0) { struct neighbor_addr *item = neighbor_attr_list_neighbors(); while(item != NULL) { item->time += TIMEOUT_SECONDS; - if(item->time >= timeout) { + if(item->time >= neighbor_timeout) { struct neighbor_addr *next_item = item->next; list_remove(neighbor_addrs, item); @@ -240,11 +240,11 @@ timeout_check(void *ptr) void neighbor_attr_set_timeout(uint16_t time) { - if(timeout == 0 && time > 0) { + if(neighbor_timeout == 0 && time > 0) { ctimer_set(&ct, TIMEOUT_SECONDS * CLOCK_SECOND, timeout_check, NULL); - } else if(timeout > 0 && time == 0) { + } else if(neighbor_timeout > 0 && time == 0) { ctimer_stop(&ct); } - timeout = time; + neighbor_timeout = time; } /*---------------------------------------------------------------------------*/ From 06d3c142130fa40adc70184a6b41f6d6f7de8a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Tue, 13 Nov 2012 00:36:46 +0100 Subject: [PATCH 05/25] ctk-curses: Enable screensaver --- platform/native/contiki-conf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/native/contiki-conf.h b/platform/native/contiki-conf.h index 54d338b45..3300d9c53 100644 --- a/platform/native/contiki-conf.h +++ b/platform/native/contiki-conf.h @@ -186,7 +186,7 @@ typedef unsigned short uip_stats_t; #define CTK_CONF_WIDGETUP_KEY -7 /* Shift-Tab */ #define CTK_CONF_WIDGETDOWN_KEY '\t' #define CTK_CONF_WIDGET_FLAGS 0 -#define CTK_CONF_SCREENSAVER 0 +#define CTK_CONF_SCREENSAVER 1 #ifdef PLATFORM_BUILD #define CTK_CONF_MOUSE_SUPPORT 1 From 1ce1232d0ff8f6a14ae175ffe0c2ff3ce4be4628 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Tue, 13 Nov 2012 00:38:45 +0100 Subject: [PATCH 06/25] ctk-curses: Add configuration for various apps --- platform/native/contiki-conf.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/platform/native/contiki-conf.h b/platform/native/contiki-conf.h index 3300d9c53..2c3cc17f2 100644 --- a/platform/native/contiki-conf.h +++ b/platform/native/contiki-conf.h @@ -235,6 +235,33 @@ typedef unsigned long clock_time_t; #define LOG_CONF_ENABLED 1 +#define PROGRAM_HANDLER_CONF_MAX_NUMDSCS 10 +#define PROGRAM_HANDLER_CONF_QUIT_MENU 1 + +#define EMAIL_CONF_WIDTH 78 +#define EMAIL_CONF_HEIGHT 17 +#ifndef PLATFORM_BUILD +#define EMAIL_CONF_ERASE 0 +#endif + +#define IRC_CONF_WIDTH 78 +#define IRC_CONF_HEIGHT 17 +#define IRC_CONF_SYSTEM_STRING "*nix" + +#define SHELL_CONF_WITH_PROGRAM_HANDLER 1 + +#define SHELL_GUI_CONF_XSIZE 78 +#define SHELL_GUI_CONF_YSIZE 17 + +#ifdef PLATFORM_BUILD +#define TELNETD_CONF_GUI 1 +#endif /* PLATFORM_BUILD */ + +#ifdef PLATFORM_BUILD +#define WWW_CONF_WEBPAGE_WIDTH 78 +#define WWW_CONF_WEBPAGE_HEIGHT 17 +#endif /* PLATFORM_BUILD */ + /* Not part of C99 but actually present */ int strcasecmp(const char*, const char*); From b7601c3acbea0878bea1c403d2f67dbfa833ffd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Wed, 20 Mar 2013 17:16:37 +0100 Subject: [PATCH 07/25] ctk-curses: Cleanup --- platform/native/Makefile.native | 3 +-- platform/native/contiki-conf.h | 8 +++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/platform/native/Makefile.native b/platform/native/Makefile.native index d74485149..369a1fd45 100644 --- a/platform/native/Makefile.native +++ b/platform/native/Makefile.native @@ -30,8 +30,7 @@ CONTIKI_TARGET_SOURCEFILES += tapdev6.c endif endif -CONTIKI_SOURCEFILES += $(CTK) ctk-conio.c -CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES) +CONTIKI_SOURCEFILES += $(CTK) ctk-conio.c $(CONTIKI_TARGET_SOURCEFILES) .SUFFIXES: diff --git a/platform/native/contiki-conf.h b/platform/native/contiki-conf.h index 2c3cc17f2..e3739e9be 100644 --- a/platform/native/contiki-conf.h +++ b/platform/native/contiki-conf.h @@ -208,20 +208,18 @@ typedef unsigned short uip_stats_t; #define CTK_CONF_MENUS 0 #endif /* PLATFORM_BUILD */ -/* curses doesn't define this one */ -#define COLOR_GRAY COLOR_CYAN - +/* base background color for widgets */ #define COLOR_BG COLOR_BLUE #define BORDERCOLOR COLOR_BLACK #define SCREENCOLOR COLOR_BLACK #define BACKGROUNDCOLOR COLOR_BLACK #define WINDOWCOLOR_FOCUS COLOR_WHITE | COLOR_BG * 0x10 -#define WINDOWCOLOR COLOR_GRAY | COLOR_BG * 0x10 +#define WINDOWCOLOR COLOR_CYAN | COLOR_BG * 0x10 #define DIALOGCOLOR COLOR_WHITE | COLOR_BG * 0x10 #define WIDGETCOLOR_HLINK COLOR_CYAN | COLOR_BG * 0x10 #define WIDGETCOLOR_FWIN COLOR_WHITE | COLOR_BG * 0x10 -#define WIDGETCOLOR COLOR_GRAY | COLOR_BG * 0x10 +#define WIDGETCOLOR COLOR_CYAN | COLOR_BG * 0x10 #define WIDGETCOLOR_DIALOG COLOR_WHITE | COLOR_BG * 0x10 #define WIDGETCOLOR_FOCUS COLOR_YELLOW | COLOR_BG * 0x10 #define MENUCOLOR COLOR_WHITE | COLOR_BG * 0x10 From 343a7643b7f90a1af995c83748867d32e520e50e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Tue, 26 Mar 2013 01:12:09 +0100 Subject: [PATCH 08/25] ctk-curses: Introduce CURSES_LIB makefile variable This should allow overriding it depending on $(HOST_OS). --- platform/native/Makefile.native | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/platform/native/Makefile.native b/platform/native/Makefile.native index 369a1fd45..1fad9d81f 100644 --- a/platform/native/Makefile.native +++ b/platform/native/Makefile.native @@ -38,5 +38,7 @@ CONTIKI_SOURCEFILES += $(CTK) ctk-conio.c $(CONTIKI_TARGET_SOURCEFILES) CONTIKI_CPU=$(CONTIKI)/cpu/native include $(CONTIKI)/cpu/native/Makefile.native -LDFLAGS += -lncurses +CURSES_LIBS ?= -lncurses + +LDFLAGS += $(CURSES_LIBS) From 86cb1769f6a11132810a99b0b066da1ac6568e04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Tue, 26 Mar 2013 01:49:05 +0100 Subject: [PATCH 09/25] ctk-curses: Remove extra mouse events pumping loop It was added to avoid getting garbage keyboard input in some cases, however it seems not to happen very often and might be the cause of hang in OSX. If garbage input happens again we can always try to pump a single event each time instead of looping anyway. --- platform/native/ctk/ctk-curses.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/platform/native/ctk/ctk-curses.c b/platform/native/ctk/ctk-curses.c index d660e11cb..4670ebffb 100644 --- a/platform/native/ctk/ctk-curses.c +++ b/platform/native/ctk/ctk-curses.c @@ -360,10 +360,6 @@ console_readkey(int k) /*fprintf(stderr, "mevent: %d: %d, %d, %d, %lx ; %d\n", event.id, event.x, event.y, event.z, event.bstate, button); */ } - /* just in case */ - while(getmouse(&event) == OK) { - /*fprintf(stderr, "pumped mevent\n"); */ - } return; } case KEY_LEFT: From 2bb9ced3d6de4efd5957b366fe9923c008a92d77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Tue, 26 Mar 2013 02:01:28 +0100 Subject: [PATCH 10/25] ctk-curses: Guard ncurses-specific mouse function calls The mouse support is ncurses-specific so guard it with proper #ifdef. --- platform/native/ctk/ctk-curses.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/platform/native/ctk/ctk-curses.c b/platform/native/ctk/ctk-curses.c index 4670ebffb..84e424422 100644 --- a/platform/native/ctk/ctk-curses.c +++ b/platform/native/ctk/ctk-curses.c @@ -76,7 +76,9 @@ ctrlhandler(int sig) void console_init(void) { +#ifdef NCURSES_MOUSE_VERSION mmask_t oldmask; +#endif static unsigned char done; if(done) { @@ -104,8 +106,10 @@ console_init(void) intrflush(stdscr, FALSE); keypad(stdscr, TRUE); +#ifdef NCURSES_MOUSE_VERSION /* done here because ctk_mouse_init() is called before anyway */ mousemask(ALL_MOUSE_EVENTS, &oldmask); +#endif screensize(&width, &height); @@ -347,6 +351,7 @@ console_readkey(int k) key = (ctk_arch_key_t) k; /*fprintf(stderr, "key: %d\n", k); */ switch (k) { +#ifdef NCURSES_MOUSE_VERSION case KEY_MOUSE: { MEVENT event; @@ -362,6 +367,7 @@ console_readkey(int k) } return; } +#endif case KEY_LEFT: key = CH_CURS_LEFT; break; From 54199d712146dbbadc4f2a513122ba0aa895f4d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Tue, 26 Mar 2013 08:10:59 +0100 Subject: [PATCH 11/25] ctk-curses: add CURSES_LIB to TARGET_LIBFILES, not LDFLAGS It seems TARGET_LIBFILES is used at the end of the link command, unlike LDFLAGS, which should help when only a static curses lib is available, like on Haiku. --- platform/native/Makefile.native | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/native/Makefile.native b/platform/native/Makefile.native index 1fad9d81f..8643a1fa4 100644 --- a/platform/native/Makefile.native +++ b/platform/native/Makefile.native @@ -40,5 +40,5 @@ include $(CONTIKI)/cpu/native/Makefile.native CURSES_LIBS ?= -lncurses -LDFLAGS += $(CURSES_LIBS) +TARGET_LIBFILES += $(CURSES_LIBS) From 56f11e7524b94954f588f5d9060cb36aabc0b55c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Tue, 26 Mar 2013 08:51:37 +0100 Subject: [PATCH 12/25] ctk-curses: C89 At least Haiku still uses gcc2 by default. --- platform/native/ctk/ctk-curses.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/platform/native/ctk/ctk-curses.c b/platform/native/ctk/ctk-curses.c index 84e424422..6cac2218e 100644 --- a/platform/native/ctk/ctk-curses.c +++ b/platform/native/ctk/ctk-curses.c @@ -432,8 +432,9 @@ console_read(void) char ctk_arch_getkey(void) { + char k; console_read(); - char k = keys[keys_out++]; + k = keys[keys_out++]; available--; return k; From ada305a3be5ec9180479c0f78446b645a15b94ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Tue, 26 Mar 2013 11:37:10 +0100 Subject: [PATCH 13/25] ctk-curses: Introduce CTK-specific color names Since ncurses also defines COLOR_BLACK and friends, and we want to avoid including curses.h in contiki-conf.h, define CTK_COLOR_* constants and map them to curses colors in ctk-curses.c. --- platform/native/contiki-conf.h | 42 ++++++++++++++++++++------------ platform/native/ctk/ctk-curses.c | 24 ++++++++++++++++-- platform/native/ctk/ctk-curses.h | 2 -- 3 files changed, 48 insertions(+), 20 deletions(-) diff --git a/platform/native/contiki-conf.h b/platform/native/contiki-conf.h index e3739e9be..71bda7a24 100644 --- a/platform/native/contiki-conf.h +++ b/platform/native/contiki-conf.h @@ -208,23 +208,33 @@ typedef unsigned short uip_stats_t; #define CTK_CONF_MENUS 0 #endif /* PLATFORM_BUILD */ -/* base background color for widgets */ -#define COLOR_BG COLOR_BLUE +/* CTK-specific color constants */ +#define CTK_COLOR_BLACK 0 +#define CTK_COLOR_RED 1 +#define CTK_COLOR_GREEN 2 +#define CTK_COLOR_YELLOW 3 +#define CTK_COLOR_BLUE 4 +#define CTK_COLOR_MAGENTA 5 +#define CTK_COLOR_CYAN 6 +#define CTK_COLOR_WHITE 7 -#define BORDERCOLOR COLOR_BLACK -#define SCREENCOLOR COLOR_BLACK -#define BACKGROUNDCOLOR COLOR_BLACK -#define WINDOWCOLOR_FOCUS COLOR_WHITE | COLOR_BG * 0x10 -#define WINDOWCOLOR COLOR_CYAN | COLOR_BG * 0x10 -#define DIALOGCOLOR COLOR_WHITE | COLOR_BG * 0x10 -#define WIDGETCOLOR_HLINK COLOR_CYAN | COLOR_BG * 0x10 -#define WIDGETCOLOR_FWIN COLOR_WHITE | COLOR_BG * 0x10 -#define WIDGETCOLOR COLOR_CYAN | COLOR_BG * 0x10 -#define WIDGETCOLOR_DIALOG COLOR_WHITE | COLOR_BG * 0x10 -#define WIDGETCOLOR_FOCUS COLOR_YELLOW | COLOR_BG * 0x10 -#define MENUCOLOR COLOR_WHITE | COLOR_BG * 0x10 -#define OPENMENUCOLOR COLOR_WHITE | COLOR_BG * 0x10 -#define ACTIVEMENUITEMCOLOR COLOR_YELLOW | COLOR_BG * 0x10 +/* base background color for widgets */ +#define COLOR_BG CTK_COLOR_BLUE + +#define BORDERCOLOR CTK_COLOR_BLACK +#define SCREENCOLOR CTK_COLOR_BLACK +#define BACKGROUNDCOLOR CTK_COLOR_BLACK +#define WINDOWCOLOR_FOCUS CTK_COLOR_WHITE | COLOR_BG * 0x10 +#define WINDOWCOLOR CTK_COLOR_CYAN | COLOR_BG * 0x10 +#define DIALOGCOLOR CTK_COLOR_WHITE | COLOR_BG * 0x10 +#define WIDGETCOLOR_HLINK CTK_COLOR_CYAN | COLOR_BG * 0x10 +#define WIDGETCOLOR_FWIN CTK_COLOR_WHITE | COLOR_BG * 0x10 +#define WIDGETCOLOR CTK_COLOR_CYAN | COLOR_BG * 0x10 +#define WIDGETCOLOR_DIALOG CTK_COLOR_WHITE | COLOR_BG * 0x10 +#define WIDGETCOLOR_FOCUS CTK_COLOR_YELLOW | COLOR_BG * 0x10 +#define MENUCOLOR CTK_COLOR_WHITE | COLOR_BG * 0x10 +#define OPENMENUCOLOR CTK_COLOR_WHITE | COLOR_BG * 0x10 +#define ACTIVEMENUITEMCOLOR CTK_COLOR_YELLOW | COLOR_BG * 0x10 typedef unsigned long clock_time_t; diff --git a/platform/native/ctk/ctk-curses.c b/platform/native/ctk/ctk-curses.c index 6cac2218e..df3572bde 100644 --- a/platform/native/ctk/ctk-curses.c +++ b/platform/native/ctk/ctk-curses.c @@ -66,6 +66,26 @@ static unsigned short xpos; static unsigned short ypos; static unsigned char button; +/* map CTK colors to curses colors */ +static unsigned char ctk_color_map[8] = { + COLOR_BLACK, + COLOR_RED, + COLOR_GREEN, + COLOR_YELLOW, + COLOR_BLUE, + COLOR_MAGENTA, + COLOR_CYAN, + COLOR_WHITE +}; + +static unsigned char map_color(unsigned char color) +{ + unsigned char c; + c = ctk_color_map[color & 0x0f]; + c |= ctk_color_map[(color >> 4) & 0x07] << 4; + return c; +} + /*-----------------------------------------------------------------------------------*/ static void ctrlhandler(int sig) @@ -212,8 +232,8 @@ clrscr(void) void bgcolor(unsigned char c) { + c = map_color(c); color = ((c << 4) | (color & 0xF0)); - /* Presume this to be one of the first calls. */ console_init(); } @@ -339,7 +359,7 @@ cputcxy(unsigned char x, unsigned char y, char c) void textcolor(unsigned char c) { - color = c; + color = map_color(c); setcolor(); } /*-----------------------------------------------------------------------------------*/ diff --git a/platform/native/ctk/ctk-curses.h b/platform/native/ctk/ctk-curses.h index 2661f7f47..98d432d5b 100644 --- a/platform/native/ctk/ctk-curses.h +++ b/platform/native/ctk/ctk-curses.h @@ -33,8 +33,6 @@ #ifndef __CTK_CONSOLE_H__ #define __CTK_CONSOLE_H__ -#include - #define cputc console_cputc #define cputs console_cputs From 5350f36d192c877b512811f84c9181bb47fb6da3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Tue, 26 Mar 2013 11:40:32 +0100 Subject: [PATCH 14/25] ctk-curses: Revert "ctk-curses: fix build" This reverts commit 9ffaa964753f87e23c7f31d178e0d0639f39ab2c. Since we do not include curses.h anymore there shouldn't be any name clash. --- core/net/neighbor-attr.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core/net/neighbor-attr.c b/core/net/neighbor-attr.c index 1fb4f8e48..38b42839a 100644 --- a/core/net/neighbor-attr.c +++ b/core/net/neighbor-attr.c @@ -48,7 +48,7 @@ #define PRINTF(...) #endif -static uint16_t neighbor_timeout = 0; +static uint16_t timeout = 0; MEMB(neighbor_addr_mem, struct neighbor_addr, NEIGHBOR_ATTR_MAX_NEIGHBORS); @@ -209,7 +209,7 @@ neighbor_attr_tick(const rimeaddr_t * addr) uint16_t neighbor_attr_get_timeout(void) { - return neighbor_timeout; + return timeout; } /*---------------------------------------------------------------------------*/ static struct ctimer ct; @@ -218,12 +218,12 @@ static struct ctimer ct; static void timeout_check(void *ptr) { - if(neighbor_timeout > 0) { + if(timeout > 0) { struct neighbor_addr *item = neighbor_attr_list_neighbors(); while(item != NULL) { item->time += TIMEOUT_SECONDS; - if(item->time >= neighbor_timeout) { + if(item->time >= timeout) { struct neighbor_addr *next_item = item->next; list_remove(neighbor_addrs, item); @@ -240,11 +240,11 @@ timeout_check(void *ptr) void neighbor_attr_set_timeout(uint16_t time) { - if(neighbor_timeout == 0 && time > 0) { + if(timeout == 0 && time > 0) { ctimer_set(&ct, TIMEOUT_SECONDS * CLOCK_SECOND, timeout_check, NULL); - } else if(neighbor_timeout > 0 && time == 0) { + } else if(timeout > 0 && time == 0) { ctimer_stop(&ct); } - neighbor_timeout = time; + timeout = time; } /*---------------------------------------------------------------------------*/ From 9372a153dd574a730f5c31d776683cdcabe8fa65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Tue, 26 Mar 2013 11:50:09 +0100 Subject: [PATCH 15/25] win32: Prefix color names with CTK_ For consistency with ctk-curses, prefix CTK colors with CTK_. Also introduce COLOR_BG to make it easier to change the widgets background at once. --- platform/win32/contiki-conf.h | 42 ++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/platform/win32/contiki-conf.h b/platform/win32/contiki-conf.h index 1d483945c..82ea56474 100644 --- a/platform/win32/contiki-conf.h +++ b/platform/win32/contiki-conf.h @@ -116,27 +116,29 @@ typedef unsigned short uip_stats_t; #define CTK_CONF_MENUS 0 #endif /* PLATFORM_BUILD */ -#define COLOR_BLACK (0) -#define COLOR_BLUE (1) -#define COLOR_GRAY (1 | 2 | 4) -#define COLOR_CYAN (1 | 2 | 8) -#define COLOR_YELLOW (2 | 4 | 8) -#define COLOR_WHITE (1 | 2 | 4 | 8) +#define CTK_COLOR_BLACK (0) +#define CTK_COLOR_BLUE (1) +#define CTK_COLOR_GRAY (1 | 2 | 4) +#define CTK_COLOR_CYAN (1 | 2 | 8) +#define CTK_COLOR_YELLOW (2 | 4 | 8) +#define CTK_COLOR_WHITE (1 | 2 | 4 | 8) -#define BORDERCOLOR COLOR_BLACK -#define SCREENCOLOR COLOR_BLACK -#define BACKGROUNDCOLOR COLOR_BLACK -#define WINDOWCOLOR_FOCUS COLOR_WHITE | COLOR_BLUE * 0x10 -#define WINDOWCOLOR COLOR_GRAY | COLOR_BLUE * 0x10 -#define DIALOGCOLOR COLOR_WHITE | COLOR_BLUE * 0x10 -#define WIDGETCOLOR_HLINK COLOR_CYAN | COLOR_BLUE * 0x10 -#define WIDGETCOLOR_FWIN COLOR_WHITE | COLOR_BLUE * 0x10 -#define WIDGETCOLOR COLOR_GRAY | COLOR_BLUE * 0x10 -#define WIDGETCOLOR_DIALOG COLOR_WHITE | COLOR_BLUE * 0x10 -#define WIDGETCOLOR_FOCUS COLOR_YELLOW | COLOR_BLUE * 0x10 -#define MENUCOLOR COLOR_WHITE | COLOR_BLUE * 0x10 -#define OPENMENUCOLOR COLOR_WHITE | COLOR_BLUE * 0x10 -#define ACTIVEMENUITEMCOLOR COLOR_YELLOW | COLOR_BLUE * 0x10 +#define COLOR_BG CTK_COLOR_BLUE + +#define BORDERCOLOR CTK_COLOR_BLACK +#define SCREENCOLOR CTK_COLOR_BLACK +#define BACKGROUNDCOLOR CTK_COLOR_BLACK +#define WINDOWCOLOR_FOCUS CTK_COLOR_WHITE | COLOR_BG * 0x10 +#define WINDOWCOLOR CTK_COLOR_GRAY | COLOR_BG * 0x10 +#define DIALOGCOLOR CTK_COLOR_WHITE | COLOR_BG * 0x10 +#define WIDGETCOLOR_HLINK CTK_COLOR_CYAN | COLOR_BG * 0x10 +#define WIDGETCOLOR_FWIN CTK_COLOR_WHITE | COLOR_BG * 0x10 +#define WIDGETCOLOR CTK_COLOR_GRAY | COLOR_BG * 0x10 +#define WIDGETCOLOR_DIALOG CTK_COLOR_WHITE | COLOR_BG * 0x10 +#define WIDGETCOLOR_FOCUS CTK_COLOR_YELLOW | COLOR_BG * 0x10 +#define MENUCOLOR CTK_COLOR_WHITE | COLOR_BG * 0x10 +#define OPENMENUCOLOR CTK_COLOR_WHITE | COLOR_BG * 0x10 +#define ACTIVEMENUITEMCOLOR CTK_COLOR_YELLOW | COLOR_BG * 0x10 #ifdef PLATFORM_BUILD From 45662ac1efc21bebc2fa9f1d7e7bc98bf70e5dd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Sat, 30 Mar 2013 17:57:01 +0100 Subject: [PATCH 16/25] ctk-curses: add some comments for clarity --- platform/native/ctk/ctk-curses.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/platform/native/ctk/ctk-curses.c b/platform/native/ctk/ctk-curses.c index df3572bde..2dddc22ab 100644 --- a/platform/native/ctk/ctk-curses.c +++ b/platform/native/ctk/ctk-curses.c @@ -78,6 +78,7 @@ static unsigned char ctk_color_map[8] = { COLOR_WHITE }; +/*-----------------------------------------------------------------------------------*/ static unsigned char map_color(unsigned char color) { unsigned char c; @@ -85,17 +86,18 @@ static unsigned char map_color(unsigned char color) c |= ctk_color_map[(color >> 4) & 0x07] << 4; return c; } - /*-----------------------------------------------------------------------------------*/ static void ctrlhandler(int sig) { + /* make sure we call console_exit() to leave the terminal in a clean state */ exit(EXIT_SUCCESS); } /*-----------------------------------------------------------------------------------*/ void console_init(void) { + /* mouse support is ncurses-specific */ #ifdef NCURSES_MOUSE_VERSION mmask_t oldmask; #endif @@ -112,6 +114,10 @@ console_init(void) /* will display an error and exit if the term can't be initialized */ /*setupterm((char *)0, STDOUT_FILENO, (int *)0); */ + /* references: + * http://linux.die.net/man/3/ncurses + * http://linux.die.net/HOWTO/NCURSES-Programming-HOWTO/index.html + */ initscr(); start_color(); cbreak(); @@ -148,6 +154,7 @@ console_init(void) /* don't block on read, just timeout 1ms */ timeout(1); + /* make sure we return the terminal in a clean state */ signal(SIGINT, ctrlhandler); atexit(console_exit); From f63aada8b6433b73f8de6515f1edc5b8cf416e4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Sat, 30 Mar 2013 18:11:17 +0100 Subject: [PATCH 17/25] ctk-curses: make sure gcc finds ncurses headers on cygwin They are located in /usr/include/ncurses --- platform/native/Makefile.native | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/platform/native/Makefile.native b/platform/native/Makefile.native index 8643a1fa4..8d4329130 100644 --- a/platform/native/Makefile.native +++ b/platform/native/Makefile.native @@ -38,6 +38,10 @@ CONTIKI_SOURCEFILES += $(CTK) ctk-conio.c $(CONTIKI_TARGET_SOURCEFILES) CONTIKI_CPU=$(CONTIKI)/cpu/native include $(CONTIKI)/cpu/native/Makefile.native +ifeq ($(HOST_OS),Windows) +CFLAGS += -I/usr/include/ncurses +endif + CURSES_LIBS ?= -lncurses TARGET_LIBFILES += $(CURSES_LIBS) From 065c25cfc572e08f25312dfd3999c84f2ab41726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Sat, 30 Mar 2013 23:00:44 +0100 Subject: [PATCH 18/25] ctk-curses: uncomment nonl() call and fix return key handling --- platform/native/ctk/ctk-curses.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/platform/native/ctk/ctk-curses.c b/platform/native/ctk/ctk-curses.c index 2dddc22ab..780fbefd1 100644 --- a/platform/native/ctk/ctk-curses.c +++ b/platform/native/ctk/ctk-curses.c @@ -124,7 +124,8 @@ console_init(void) /* don't echo typed characters */ noecho(); - /*nonl(); */ + /* disable return -> newline translation */ + nonl(); /* hide text cursor, CTK draws its own */ curs_set(0); @@ -411,6 +412,7 @@ console_readkey(int k) case KEY_F(10): key = CTK_CONF_MENU_KEY; break; + case '\r': case KEY_ENTER: key = CH_ENTER; break; From 5a4382a9965592860ed10c7d07b4cdccf9a1e118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Sat, 30 Mar 2013 23:03:04 +0100 Subject: [PATCH 19/25] ctk-curses: uncomment refresh() calls I'm not yet sure they are required there but it shouldn't hurt. --- platform/native/ctk/ctk-curses.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platform/native/ctk/ctk-curses.c b/platform/native/ctk/ctk-curses.c index 780fbefd1..192284435 100644 --- a/platform/native/ctk/ctk-curses.c +++ b/platform/native/ctk/ctk-curses.c @@ -293,7 +293,7 @@ console_cputc(char c) break; } addch(ch); - /*refresh(); */ + refresh(); } /*-----------------------------------------------------------------------------------*/ void @@ -307,7 +307,7 @@ void cclear(unsigned char length) { hline(' ', length); - /*refresh(); */ + refresh(); } /*-----------------------------------------------------------------------------------*/ void From 976746f97fb5f0cd0a987b592f4d81c18ab2587f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Sat, 30 Mar 2013 23:07:38 +0100 Subject: [PATCH 20/25] ctk-curses: cleanup --- platform/native/ctk/ctk-curses.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/platform/native/ctk/ctk-curses.c b/platform/native/ctk/ctk-curses.c index 192284435..c5a4b4d61 100644 --- a/platform/native/ctk/ctk-curses.c +++ b/platform/native/ctk/ctk-curses.c @@ -45,6 +45,8 @@ /* references: * http://math.hws.edu/orr/s04/cpsc225/curses.html + * http://linux.die.net/man/3/ncurses + * http://linux.die.net/HOWTO/NCURSES-Programming-HOWTO/index.html */ #define MKPAIR(bg, fg) (bg << 3 | fg) @@ -114,10 +116,6 @@ console_init(void) /* will display an error and exit if the term can't be initialized */ /*setupterm((char *)0, STDOUT_FILENO, (int *)0); */ - /* references: - * http://linux.die.net/man/3/ncurses - * http://linux.die.net/HOWTO/NCURSES-Programming-HOWTO/index.html - */ initscr(); start_color(); cbreak(); From 8daf2b6659c895021bd0af213e12298300dc33b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Sat, 30 Mar 2013 23:15:08 +0100 Subject: [PATCH 21/25] ctk-curses: remove unused code The setupterm() call is not needed when using curses. --- platform/native/ctk/ctk-curses.c | 9 --------- 1 file changed, 9 deletions(-) diff --git a/platform/native/ctk/ctk-curses.c b/platform/native/ctk/ctk-curses.c index c5a4b4d61..6de8b1f9b 100644 --- a/platform/native/ctk/ctk-curses.c +++ b/platform/native/ctk/ctk-curses.c @@ -51,9 +51,6 @@ #define MKPAIR(bg, fg) (bg << 3 | fg) -static int stdinhandle; -static int stdouthandle; - static unsigned char width; static unsigned char height; @@ -110,12 +107,6 @@ console_init(void) } done = 1; - stdinhandle = STDIN_FILENO; - stdouthandle = STDOUT_FILENO; - - /* will display an error and exit if the term can't be initialized */ - /*setupterm((char *)0, STDOUT_FILENO, (int *)0); */ - initscr(); start_color(); cbreak(); From a03334a59d3896a074ac9ac4f83ea62520444063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Sat, 30 Mar 2013 23:26:22 +0100 Subject: [PATCH 22/25] ctk-curses: style fixes --- platform/native/ctk/ctk-curses.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/platform/native/ctk/ctk-curses.c b/platform/native/ctk/ctk-curses.c index 6de8b1f9b..dce0e2a54 100644 --- a/platform/native/ctk/ctk-curses.c +++ b/platform/native/ctk/ctk-curses.c @@ -101,6 +101,7 @@ console_init(void) mmask_t oldmask; #endif static unsigned char done; + int bg, fg; if(done) { return; @@ -129,13 +130,11 @@ console_init(void) screensize(&width, &height); - /* curses color handling is weird... */ - { - int bg, fg; - - for(fg = 0; fg < 8; fg++) - for(bg = 0; bg < 8; bg++) - init_pair(MKPAIR(bg, fg), fg, bg); + /* we must declare all possible color pairs */ + for(fg = 0; fg < 8; fg++) { + for(bg = 0; bg < 8; bg++) { + init_pair(MKPAIR(bg, fg), fg, bg); + } } /* set window title */ @@ -195,8 +194,9 @@ setcolor(void) bg = (color & 0xF0) >> 4; attrs = COLOR_PAIR(MKPAIR(bg, fg)); - if(reversed) + if(reversed) { attrs |= WA_REVERSE; + } attrset(attrs); } /*-----------------------------------------------------------------------------------*/ @@ -443,8 +443,9 @@ console_read(void) int k; k = getch(); - if(k != ERR) + if(k != ERR) { console_readkey(k); + } } /*-----------------------------------------------------------------------------------*/ char From 3633c52bcb9d3b7a7e2323a1ef151214fde17fa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Sat, 30 Mar 2013 23:34:18 +0100 Subject: [PATCH 23/25] ctk-curses: whitespace fixes --- platform/native/ctk/ctk-curses.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/platform/native/ctk/ctk-curses.c b/platform/native/ctk/ctk-curses.c index dce0e2a54..0efb50745 100644 --- a/platform/native/ctk/ctk-curses.c +++ b/platform/native/ctk/ctk-curses.c @@ -146,7 +146,6 @@ console_init(void) /* make sure we return the terminal in a clean state */ signal(SIGINT, ctrlhandler); atexit(console_exit); - } /*-----------------------------------------------------------------------------------*/ void @@ -163,7 +162,6 @@ console_exit(void) clrscr(); gotoxy(0, 0); - endwin(); } /*-----------------------------------------------------------------------------------*/ From a43601b2fc8aa6fcf1242e8aa723bd5b3f0fccc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Sat, 30 Mar 2013 23:45:53 +0100 Subject: [PATCH 24/25] ctk-curses: WITH_GUI also on ftp example for native platform --- examples/ftp/Makefile.native.defines | 1 + 1 file changed, 1 insertion(+) create mode 100644 examples/ftp/Makefile.native.defines diff --git a/examples/ftp/Makefile.native.defines b/examples/ftp/Makefile.native.defines new file mode 100644 index 000000000..1b5caf200 --- /dev/null +++ b/examples/ftp/Makefile.native.defines @@ -0,0 +1 @@ +DEFINES = WITH_GUI From 476be8de7e218e808bd0ad59c977d4f53d133b69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Sun, 31 Mar 2013 04:07:03 +0200 Subject: [PATCH 25/25] ctk-curses: style fixes Thanks tools/contiki-indent --- platform/native/ctk/ctk-curses.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/platform/native/ctk/ctk-curses.c b/platform/native/ctk/ctk-curses.c index 0efb50745..19af14506 100644 --- a/platform/native/ctk/ctk-curses.c +++ b/platform/native/ctk/ctk-curses.c @@ -78,9 +78,11 @@ static unsigned char ctk_color_map[8] = { }; /*-----------------------------------------------------------------------------------*/ -static unsigned char map_color(unsigned char color) +static unsigned char +map_color(unsigned char color) { unsigned char c; + c = ctk_color_map[color & 0x0f]; c |= ctk_color_map[(color >> 4) & 0x07] << 4; return c; @@ -450,6 +452,7 @@ char ctk_arch_getkey(void) { char k; + console_read(); k = keys[keys_out++];