From ea0d1101f7aedc8a41b57df8a11f5cc8bd29decc Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 17 Nov 2003 16:11:34 +0000 Subject: [PATCH] * src/io.c src/caca.h: + Interpret escape sequences for F1, F2, left, right, etc. * src/Makefile.am examples/Makefile.am: + Install caca.h in /usr/include. + Do not install example programs. * src/blit.c: + Minor improvements to grayscale colors. --- examples/Makefile.am | 2 +- src/Makefile.am | 2 + src/blit.c | 15 ++++--- src/caca.h | 29 +++++++++++- src/io.c | 105 ++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 140 insertions(+), 13 deletions(-) diff --git a/examples/Makefile.am b/examples/Makefile.am index 06c93cc..98cde77 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -13,7 +13,7 @@ if USE_NCURSES LDFLAGS_ncurses = -lncurses endif -bin_PROGRAMS = demo spritedit +noinst_PROGRAMS = demo spritedit demo_SOURCES = demo.c demo_LDADD = ../src/libcaca.a $(LDFLAGS_slang) $(LDFLAGS_ncurses) -lm diff --git a/src/Makefile.am b/src/Makefile.am index 988a03b..8e11eb7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -18,3 +18,5 @@ libcaca_a_SOURCES = \ blit.c \ $(NULL) +include_HEADERS = caca.h + diff --git a/src/blit.c b/src/blit.c index c2cff5e..26dcb28 100644 --- a/src/blit.c +++ b/src/blit.c @@ -85,6 +85,7 @@ void caca_set_dithering(enum caca_dithering dither) void caca_blit(int x1, int y1, int x2, int y2, void *pixels, int w, int h) { + static int white_colors[] = {CACA_COLOR_DARKGRAY, CACA_COLOR_LIGHTGRAY, CACA_COLOR_WHITE}; static int light_colors[] = {CACA_COLOR_LIGHTMAGENTA, CACA_COLOR_LIGHTRED, CACA_COLOR_YELLOW, CACA_COLOR_LIGHTGREEN, CACA_COLOR_LIGHTCYAN, CACA_COLOR_LIGHTBLUE, CACA_COLOR_LIGHTMAGENTA}; static int dark_colors[] = {CACA_COLOR_MAGENTA, CACA_COLOR_RED, CACA_COLOR_BROWN, CACA_COLOR_GREEN, CACA_COLOR_CYAN, CACA_COLOR_BLUE, CACA_COLOR_MAGENTA}; static char foo[] = { ' ', '.', ':', ';', '=', '%', '$', 'W', '#', '8', '@' }; @@ -100,7 +101,8 @@ void caca_blit(int x1, int y1, int x2, int y2, void *pixels, int w, int h) int tmp = y2; y2 = y1; y1 = tmp; } - pitch = (3 * w + 3) / 4 * 4; + //pitch = (3 * w + 3) / 4 * 4; + pitch = 4 * w; for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= (int)caca_get_height(); y++) { @@ -112,9 +114,12 @@ void caca_blit(int x1, int y1, int x2, int y2, void *pixels, int w, int h) { int fromx = w * (x - x1) / (x2 - x1 + 1); int fromy = h * (y - y1) / (y2 - y1 + 1); - int r = ((unsigned char *)pixels)[3 * fromx + pitch * fromy]; - int g = ((unsigned char *)pixels)[3 * fromx + 1 + pitch * fromy]; - int b = ((unsigned char *)pixels)[3 * fromx + 2 + pitch * fromy]; + //int r = ((unsigned char *)pixels)[3 * fromx + pitch * fromy]; + //int g = ((unsigned char *)pixels)[3 * fromx + 1 + pitch * fromy]; + //int b = ((unsigned char *)pixels)[3 * fromx + 2 + pitch * fromy]; + int b = ((unsigned char *)pixels)[4 * fromx + pitch * fromy]; + int g = ((unsigned char *)pixels)[4 * fromx + 1 + pitch * fromy]; + int r = ((unsigned char *)pixels)[4 * fromx + 2 + pitch * fromy]; int hue, sat, val; int min = r, max = r, delta; @@ -146,7 +151,7 @@ void caca_blit(int x1, int y1, int x2, int y2, void *pixels, int w, int h) } else { - caca_set_color(CACA_COLOR_LIGHTGRAY); + caca_set_color(white_colors[max * 3 / 256]); } caca_putchar(x, y, foo[(r + g + b + 2 * _get_dither()) / 3 / 25]); diff --git a/src/caca.h b/src/caca.h index 47225c0..1a73063 100644 --- a/src/caca.h +++ b/src/caca.h @@ -62,6 +62,33 @@ enum caca_dithering CACA_DITHER_RANDOM }; +/* + * Keys + */ +enum caca_key +{ + CACA_KEY_UP = 273, + CACA_KEY_DOWN = 274, + CACA_KEY_LEFT = 275, + CACA_KEY_RIGHT = 276, + + CACA_KEY_F1 = 282, + CACA_KEY_F2 = 283, + CACA_KEY_F3 = 284, + CACA_KEY_F4 = 285, + CACA_KEY_F5 = 286, + CACA_KEY_F6 = 287, + CACA_KEY_F7 = 288, + CACA_KEY_F8 = 289, + CACA_KEY_F9 = 290, + CACA_KEY_F10 = 291, + CACA_KEY_F11 = 292, + CACA_KEY_F12 = 293, + CACA_KEY_F13 = 294, + CACA_KEY_F14 = 295, + CACA_KEY_F15 = 296 +}; + /* * Types */ @@ -80,7 +107,7 @@ const char *caca_get_color_name(unsigned int); void caca_refresh(void); void caca_end(void); -char caca_get_key(void); +int caca_get_key(void); void caca_set_color(enum caca_color); enum caca_color caca_get_color(void); diff --git a/src/io.c b/src/io.c index 6be2eba..ff0be94 100644 --- a/src/io.c +++ b/src/io.c @@ -36,18 +36,111 @@ #include "caca.h" #include "caca_internals.h" -char caca_get_key(void) +static unsigned char back[5] = {0, 0, 0, 0, 0}; + +static void _push_key(unsigned char key) +{ + back[4] = back[3]; + back[3] = back[2]; + back[2] = back[1]; + back[1] = back[0]; + back[0] = key; +} + +static unsigned char _pop_key(void) +{ + unsigned char key = back[0]; + back[0] = back[1]; + back[1] = back[2]; + back[2] = back[3]; + back[3] = back[4]; + return key; +} + +static unsigned char _read_key(void) { #if defined(USE_SLANG) return SLang_input_pending(0) ? SLang_getkey() : 0; - #elif defined(USE_NCURSES) - char key = getch(); - return key != ERR ? key : 0; - + unsigned char key = getch(); + return (key == ERR) ? 0 : key; #elif defined(USE_CONIO) return _conio_kbhit() ? getch() : 0; - #endif } +int caca_get_key(void) +{ + unsigned char key[5]; + + /* If there were legacy keys, pop them */ + key[0] = _pop_key(); + if(key[0]) + return key[0]; + + key[0] = _read_key(); + if(key[0] != 0x1b) + return key[0]; + + key[1] = _read_key(); + if(!key[1]) + return key[0]; + + key[2] = _read_key(); + if(!key[2]) + { + _push_key(key[1]); + return key[0]; + } + + if(key[1] == 0x4f) + { + switch(key[2]) + { + case 0x50: return CACA_KEY_F1; + case 0x51: return CACA_KEY_F2; + case 0x52: return CACA_KEY_F3; + case 0x53: return CACA_KEY_F4; + } + } + else if(key[1] == 0x5b) + { + switch(key[2]) + { + case 0x41: return CACA_KEY_UP; + case 0x42: return CACA_KEY_DOWN; + case 0x43: return CACA_KEY_LEFT; + case 0x44: return CACA_KEY_RIGHT; + } + + key[3] = _read_key(); + key[4] = _read_key(); + + if(key[2] == 0x31 && key[4] == 0x7e) + switch(key[3]) + { + case 0x35: return CACA_KEY_F5; + case 0x37: return CACA_KEY_F6; + case 0x38: return CACA_KEY_F7; + case 0x39: return CACA_KEY_F8; + } + + if(key[2] == 0x32 && key[4] == 0x7e) + switch(key[3]) + { + case 0x30: return CACA_KEY_F9; + case 0x31: return CACA_KEY_F10; + case 0x33: return CACA_KEY_F11; + case 0x34: return CACA_KEY_F12; + } + + _push_key(key[4]); + _push_key(key[3]); + } + + /* Unknown escape sequence: return 0 */ + _push_key(key[2]); + _push_key(key[1]); + return key[0]; +} +