diff --git a/caca/caca.h b/caca/caca.h index 3f9d658..b2a6d5a 100644 --- a/caca/caca.h +++ b/caca/caca.h @@ -237,8 +237,7 @@ int caca_set_window_title(caca_t *kk, char const *); * clicks. * * @{ */ -int caca_get_event(caca_t *kk, unsigned int, struct caca_event *); -int caca_wait_event(caca_t *kk, unsigned int, struct caca_event *); +int caca_get_event(caca_t *kk, unsigned int, struct caca_event *, int); unsigned int caca_get_mouse_x(caca_t *kk); unsigned int caca_get_mouse_y(caca_t *kk); void caca_set_mouse(caca_t *kk, int); diff --git a/caca/event.c b/caca/event.c index cf5db59..80543d8 100644 --- a/caca/event.c +++ b/caca/event.c @@ -45,50 +45,59 @@ static int _lowlevel_event(caca_t *, struct caca_event *); * * This function polls the event queue for mouse or keyboard events matching * the event mask and returns the first matching event. Non-matching events - * are discarded. \c event_mask must have a non-zero value. This function is - * non-blocking and returns zero if no more events are pending in the queue. - * See also caca_wait_event() for a blocking version of this function. + * are discarded. \c event_mask must have a non-zero value. + * + * The timeout value tells how long this function needs to wait for an + * event. A value of zero returns immediately and the function returns zero + * if no more events are pending in the queue. A negative value causes the + * function to wait indefinitely until a matching event is received. * * \param event_mask Bitmask of requested events. + * \param timeout A timeout value in microseconds * \return The next matching event in the queue, or 0 if no event is pending. */ -int caca_get_event(caca_t *kk, unsigned int event_mask, struct caca_event *ev) +int caca_get_event(caca_t *kk, unsigned int event_mask, + struct caca_event *ev, int timeout) { - if(!event_mask) - return 0; - - for( ; ; ) - { - int ret = _get_next_event(kk, ev); - - if(!ret || ev->type & event_mask) - return ret; - } -} + struct caca_timer timer; + int usec = 0; -/** \brief Wait for the next mouse or keyboard input event. - * - * This function returns the first mouse or keyboard event in the queue - * that matches the event mask. If no event is pending, it blocks until a - * matching event is received. \c event_mask must have a non-zero value. - * See also caca_get_event() for a non-blocking version of this function. - * - * \param event_mask Bitmask of requested events. - * \return The next event in the queue. - */ -int caca_wait_event(caca_t *kk, unsigned int event_mask, struct caca_event *ev) -{ if(!event_mask) return 0; + if(timeout > 0) + _caca_getticks(&timer); + for( ; ; ) { int ret = _get_next_event(kk, ev); - if(ret && (ev->type & event_mask)) + /* If we got the event we wanted, return */ + if(ev->type & event_mask) return ret; - _caca_sleep(10000); + /* If there is no timeout, sleep and try again. */ + if(timeout < 0) + { + _caca_sleep(10000); + continue; + } + + /* If we timeouted, return an empty event */ + if(usec >= timeout) + { + ev->type = CACA_EVENT_NONE; + return 0; + } + + /* Otherwise sleep a bit. Our granularity is far too high for values + * below 10000 microseconds so we cheat a bit. */ + if(usec > 10000) + _caca_sleep(10000); + else + _caca_sleep(1000); + + usec += _caca_getticks(&timer); } } diff --git a/src/aafire.c b/src/aafire.c index 38850a6..8e02cc9 100644 --- a/src/aafire.c +++ b/src/aafire.c @@ -266,7 +266,7 @@ game (void) { #ifdef LIBCACA struct caca_event ev; - if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev)) + if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0)) { switch(ev.data.key.c) { diff --git a/src/cacaball.c b/src/cacaball.c index 128e840..bbdbeb5 100644 --- a/src/cacaball.c +++ b/src/cacaball.c @@ -93,7 +93,7 @@ int main(int argc, char **argv) for(;;) { struct caca_event ev; - if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev)) + if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0)) { switch(ev.data.key.c) { diff --git a/src/cacamoir.c b/src/cacamoir.c index ad8645e..c982bbc 100644 --- a/src/cacamoir.c +++ b/src/cacamoir.c @@ -69,7 +69,7 @@ int main (int argc, char **argv) for(;;) { struct caca_event ev; - if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev)) + if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0)) { switch(ev.data.key.c) { diff --git a/src/cacaplas.c b/src/cacaplas.c index 8668e16..f6679ea 100644 --- a/src/cacaplas.c +++ b/src/cacaplas.c @@ -85,7 +85,7 @@ int main (int argc, char **argv) for(;;) { struct caca_event ev; - if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev)) + if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0)) { switch(ev.data.key.c) { diff --git a/src/cacaplay.c b/src/cacaplay.c index 7613e45..9c79a3a 100644 --- a/src/cacaplay.c +++ b/src/cacaplay.c @@ -66,7 +66,7 @@ int main(int argc, char **argv) caca_display(kk); - while(caca_wait_event(kk, CACA_EVENT_KEY_PRESS, &ev)) + while(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, -1)) { if(ev.data.key.c == CACA_KEY_ESCAPE) break; diff --git a/src/cacaview.c b/src/cacaview.c index 37e5c7c..e96909a 100644 --- a/src/cacaview.c +++ b/src/cacaview.c @@ -151,9 +151,9 @@ int main(int argc, char **argv) int event; if(update) - event = caca_get_event(kk, event_mask, &ev); + event = caca_get_event(kk, event_mask, &ev, 0); else - event = caca_wait_event(kk, event_mask, &ev); + event = caca_get_event(kk, event_mask, &ev, -1); while(event) { @@ -304,7 +304,7 @@ int main(int argc, char **argv) if(help || new_help) help = new_help; - event = caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev); + event = caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0); } if(items && reload) diff --git a/test/colors.c b/test/colors.c index 0d50781..ca0622b 100644 --- a/test/colors.c +++ b/test/colors.c @@ -50,7 +50,7 @@ int main(int argc, char **argv) } caca_display(kk); - caca_wait_event(kk, CACA_EVENT_KEY_PRESS, &ev); + caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, -1); caca_detach(kk); cucul_free(qq); diff --git a/test/demo.c b/test/demo.c index b944726..6109691 100644 --- a/test/demo.c +++ b/test/demo.c @@ -74,7 +74,7 @@ int main(int argc, char **argv) struct caca_event ev; int menu = 0, mouse = 0, xmouse = 0, ymouse = 0; - while(caca_get_event(kk, CACA_EVENT_ANY, &ev)) + while(caca_get_event(kk, CACA_EVENT_ANY, &ev, 0)) { if(demo && (ev.type & CACA_EVENT_KEY_PRESS)) { @@ -148,6 +148,10 @@ int main(int argc, char **argv) xmouse = ev.data.mouse.x; ymouse = ev.data.mouse.y; } + else if(ev.type & CACA_EVENT_RESIZE) + { + mouse = 1; /* old hack */ + } } if(menu || (mouse && !demo)) @@ -156,7 +160,8 @@ int main(int argc, char **argv) if(mouse && !demo) { cucul_set_color(qq, CUCUL_COLOR_RED, CUCUL_COLOR_BLACK); - cucul_putstr(qq, xmouse, ymouse, "|\\"); + cucul_putstr(qq, xmouse, ymouse, "."); + cucul_putstr(qq, xmouse, ymouse + 1, "|\\"); } caca_display(kk); mouse = menu = 0; diff --git a/test/dithering.c b/test/dithering.c index 4372cdf..f1196f7 100644 --- a/test/dithering.c +++ b/test/dithering.c @@ -124,7 +124,7 @@ int main(void) caca_display(kk); - caca_wait_event(kk, CACA_EVENT_KEY_PRESS, &ev); + caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, -1); caca_detach(kk); cucul_free(qq); diff --git a/test/event.c b/test/event.c index e17d1bf..0ad14cd 100644 --- a/test/event.c +++ b/test/event.c @@ -54,7 +54,7 @@ int main(int argc, char **argv) { struct caca_event ev; static char const * quit_string[] = { "", "q", "qu", "qui", "quit" }; - int ret = caca_wait_event(kk, CACA_EVENT_ANY, &ev); + int ret = caca_get_event(kk, CACA_EVENT_ANY, &ev, -1); if(!ret) continue; @@ -77,7 +77,7 @@ int main(int argc, char **argv) memmove(events + 1, events, (h - 1) * sizeof(struct caca_event)); events[0] = ev; - ret = caca_get_event(kk, CACA_EVENT_ANY, &ev); + ret = caca_get_event(kk, CACA_EVENT_ANY, &ev, 0); } while(ret); diff --git a/test/gamma.c b/test/gamma.c index afc6412..2a96d49 100644 --- a/test/gamma.c +++ b/test/gamma.c @@ -62,7 +62,7 @@ int main(void) for(x = 0; ; x++) { - int ret = caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev); + int ret = caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0); if(ret) { diff --git a/test/hsv.c b/test/hsv.c index b4eba85..a3ea6e6 100644 --- a/test/hsv.c +++ b/test/hsv.c @@ -53,7 +53,7 @@ int main(void) caca_display(kk); - caca_wait_event(kk, CACA_EVENT_KEY_PRESS, &ev); + caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0); caca_detach(kk); cucul_free(qq); diff --git a/test/spritedit.c b/test/spritedit.c index cdb1770..6ff1dce 100644 --- a/test/spritedit.c +++ b/test/spritedit.c @@ -57,7 +57,7 @@ int main(int argc, char **argv) int xa, ya, xb, yb; char buf[BUFSIZ]; - while(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev)) + while(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0)) { switch(ev.data.key.c) { diff --git a/test/transform.c b/test/transform.c index b7eff64..c10fedd 100644 --- a/test/transform.c +++ b/test/transform.c @@ -108,7 +108,7 @@ int main(void) caca_display(kk); - caca_wait_event(kk, CACA_EVENT_KEY_PRESS, &ev); + caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, -1); caca_detach(kk); cucul_free(rotate); diff --git a/test/unicode.c b/test/unicode.c index 9df48ea..52dbedb 100644 --- a/test/unicode.c +++ b/test/unicode.c @@ -87,7 +87,7 @@ int main(void) caca_display(kk); - caca_wait_event(kk, CACA_EVENT_KEY_PRESS, &ev); + caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, -1); caca_detach(kk); cucul_free(qq);