From 962f47b906fea252bede30ea7ac99b5827850ea8 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 31 Jul 2009 00:01:13 +0000 Subject: [PATCH] Implement conio.h's kbhit(). --- caca/caca_conio.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/caca/caca_conio.c b/caca/caca_conio.c index 85f31d9..59f66d7 100644 --- a/caca/caca_conio.c +++ b/caca/caca_conio.c @@ -32,6 +32,7 @@ static caca_canvas_t *cv; static caca_display_t *dp; static int unget_ch = -1; +static int kbhit_ch = -1; static char pass_buffer[BUFSIZ]; static char cgets_buffer[BUFSIZ]; @@ -126,7 +127,6 @@ void caca_conio_delline(void) int caca_conio_getch(void) { caca_event_t ev; - int ch; conio_init(); @@ -137,17 +137,15 @@ int caca_conio_getch(void) return tmp; } - caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1); - ch = caca_get_event_key_ch(&ev); - - switch(ch) + if(kbhit_ch >= 0) { - case CACA_KEY_LEFT: ch = 75; break; - case CACA_KEY_RIGHT: ch = 77; break; - default: break; + int tmp = kbhit_ch; + kbhit_ch = -1; + return tmp; } - return ch; + caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1); + return caca_get_event_key_ch(&ev); } /** \brief DOS conio.h getche() equivalent */ @@ -218,9 +216,18 @@ void caca_conio_insline(void) /** \brief DOS conio.h kbhit() equivalent */ int caca_conio_kbhit(void) { + caca_event_t ev; + conio_init(); - /* TODO: implement this function */ + if(kbhit_ch >= 0) + return 1; + + if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0)) + { + kbhit_ch = caca_get_event_key_ch(&ev); + return 1; + } return 0; }