diff --git a/examples/aafire.c b/examples/aafire.c index cef24bf..fd87a96 100644 --- a/examples/aafire.c +++ b/examples/aafire.c @@ -236,7 +236,7 @@ game (void) int event; gentable (); #ifdef LIBCACA - while (!(event = caca_get_event() & CACA_EVENT_KEY_PRESS)) + while (!(event = caca_get_event(CACA_EVENT_KEY_PRESS))) #else while (!(event = aa_getevent (context, 0)) || event == AA_RESIZE) #endif diff --git a/examples/cacaview.c b/examples/cacaview.c index c786fd5..0e61d97 100644 --- a/examples/cacaview.c +++ b/examples/cacaview.c @@ -108,112 +108,114 @@ int main(int argc, char **argv) int ww = caca_get_width(); int wh = caca_get_height(); - int event, new_status = 0, new_help = 0; + unsigned int event, new_status = 0, new_help = 0; - while((event = caca_get_event())) + while((event = caca_get_event(CACA_EVENT_KEY_PRESS))) { - switch(event) + unsigned int key = event & 0x00ffffff; + + switch(key) { - case CACA_EVENT_KEY_PRESS | 'n': - case CACA_EVENT_KEY_PRESS | 'N': + case 'n': + case 'N': if(items) current = (current + 1) % items; reload = 1; break; - case CACA_EVENT_KEY_PRESS | 'p': - case CACA_EVENT_KEY_PRESS | 'P': + case 'p': + case 'P': if(items) current = (items + current - 1) % items; reload = 1; break; - case CACA_EVENT_KEY_PRESS | 'f': - case CACA_EVENT_KEY_PRESS | 'F': + case 'f': + case 'F': fullscreen = ~fullscreen; update = 1; break; - case CACA_EVENT_KEY_PRESS | 'b': + case 'b': i = 1 + caca_get_feature(CACA_BACKGROUND); if(i > CACA_BACKGROUND_MAX) i = CACA_BACKGROUND_MIN; caca_set_feature(i); new_status = STATUS_BACKGROUND; update = 1; break; - case CACA_EVENT_KEY_PRESS | 'B': + case 'B': i = -1 + caca_get_feature(CACA_BACKGROUND); if(i < CACA_BACKGROUND_MIN) i = CACA_BACKGROUND_MAX; caca_set_feature(i); new_status = STATUS_BACKGROUND; update = 1; break; - case CACA_EVENT_KEY_PRESS | 'a': + case 'a': i = 1 + caca_get_feature(CACA_ANTIALIASING); if(i > CACA_ANTIALIASING_MAX) i = CACA_ANTIALIASING_MIN; caca_set_feature(i); new_status = STATUS_ANTIALIASING; update = 1; break; - case CACA_EVENT_KEY_PRESS | 'A': + case 'A': i = -1 + caca_get_feature(CACA_ANTIALIASING); if(i < CACA_ANTIALIASING_MIN) i = CACA_ANTIALIASING_MAX; caca_set_feature(i); new_status = STATUS_ANTIALIASING; update = 1; break; - case CACA_EVENT_KEY_PRESS | 'd': + case 'd': i = 1 + caca_get_feature(CACA_DITHERING); if(i > CACA_DITHERING_MAX) i = CACA_DITHERING_MIN; caca_set_feature(i); new_status = STATUS_DITHERING; update = 1; break; - case CACA_EVENT_KEY_PRESS | 'D': + case 'D': i = -1 + caca_get_feature(CACA_DITHERING); if(i < CACA_DITHERING_MIN) i = CACA_DITHERING_MAX; caca_set_feature(i); new_status = STATUS_DITHERING; update = 1; break; - case CACA_EVENT_KEY_PRESS | '+': + case '+': zoom++; if(zoom > 48) zoom = 48; else update = 1; break; - case CACA_EVENT_KEY_PRESS | '-': + case '-': zoom--; if(zoom < -48) zoom = -48; else update = 1; break; - case CACA_EVENT_KEY_PRESS | 'x': - case CACA_EVENT_KEY_PRESS | 'X': + case 'x': + case 'X': zoom = 0; update = 1; break; - case CACA_EVENT_KEY_PRESS | 'k': - case CACA_EVENT_KEY_PRESS | 'K': - case CACA_EVENT_KEY_PRESS | CACA_KEY_UP: + case 'k': + case 'K': + case CACA_KEY_UP: if(zoom > 0) y -= 1 + h / (2 + zoom) / 8; update = 1; break; - case CACA_EVENT_KEY_PRESS | 'j': - case CACA_EVENT_KEY_PRESS | 'J': - case CACA_EVENT_KEY_PRESS | CACA_KEY_DOWN: + case 'j': + case 'J': + case CACA_KEY_DOWN: if(zoom > 0) y += 1 + h / (2 + zoom) / 8; update = 1; break; - case CACA_EVENT_KEY_PRESS | 'h': - case CACA_EVENT_KEY_PRESS | 'H': - case CACA_EVENT_KEY_PRESS | CACA_KEY_LEFT: + case 'h': + case 'H': + case CACA_KEY_LEFT: if(zoom > 0) x -= 1 + w / (2 + zoom) / 8; update = 1; break; - case CACA_EVENT_KEY_PRESS | 'l': - case CACA_EVENT_KEY_PRESS | 'L': - case CACA_EVENT_KEY_PRESS | CACA_KEY_RIGHT: + case 'l': + case 'L': + case CACA_KEY_RIGHT: if(zoom > 0) x += 1 + w / (2 + zoom) / 8; update = 1; break; - case CACA_EVENT_KEY_PRESS | '?': + case '?': new_help = !help; update = 1; break; - case CACA_EVENT_KEY_PRESS | 'q': - case CACA_EVENT_KEY_PRESS | 'Q': + case 'q': + case 'Q': quit = 1; break; } diff --git a/examples/demo.c b/examples/demo.c index dd362a5..1fbf2c4 100644 --- a/examples/demo.c +++ b/examples/demo.c @@ -74,7 +74,7 @@ int main(int argc, char **argv) int menu = 0, mouse = 0, xmouse = 0, ymouse = 0; int event; - while((event = caca_get_event())) + while((event = caca_get_event(CACA_EVENT_ANY))) { if(demo && (event & CACA_EVENT_KEY_PRESS)) { diff --git a/examples/spritedit.c b/examples/spritedit.c index d8f7a8e..66602e5 100644 --- a/examples/spritedit.c +++ b/examples/spritedit.c @@ -58,25 +58,24 @@ int main(int argc, char **argv) char buf[BUFSIZ]; int event; - while((event = caca_get_event())) + while((event = caca_get_event(CACA_EVENT_KEY_PRESS))) { - if(event & CACA_EVENT_KEY_PRESS) - switch(event & 0xff) - { - case 0: - break; - case 'q': - quit = 1; - break; - case '-': - if(frame > 0) - frame--; - break; - case '+': - if(frame < caca_get_sprite_frames(sprite) - 1) - frame++; - break; - } + switch(event & 0x00ffffff) + { + case 0: + break; + case 'q': + quit = 1; + break; + case '-': + if(frame > 0) + frame--; + break; + case '+': + if(frame < caca_get_sprite_frames(sprite) - 1) + frame++; + break; + } } caca_clear(); diff --git a/src/caca.h b/src/caca.h index d13f013..fd2f413 100644 --- a/src/caca.h +++ b/src/caca.h @@ -180,7 +180,8 @@ enum caca_event CACA_EVENT_KEY_RELEASE = 0x02000000, /**< A key was released. */ CACA_EVENT_MOUSE_PRESS = 0x04000000, /**< A mouse button was pressed. */ CACA_EVENT_MOUSE_RELEASE = 0x08000000, /**< A mouse button was released. */ - CACA_EVENT_MOUSE_MOTION = 0x10000000 /**< The mouse was moved. */ + CACA_EVENT_MOUSE_MOTION = 0x10000000, /**< The mouse was moved. */ + CACA_EVENT_ANY = 0xff000000 /**< Bitmask for any event. */ }; /** \brief Special key values. @@ -236,8 +237,8 @@ void caca_end(void); * clicks. * * @{ */ -unsigned int caca_get_event(void); -unsigned int caca_wait_event(void); +unsigned int caca_get_event(unsigned int); +unsigned int caca_wait_event(unsigned int); /* @} */ /** \defgroup char Character printing diff --git a/src/io.c b/src/io.c index 2921dd1..6fbfefd 100644 --- a/src/io.c +++ b/src/io.c @@ -53,6 +53,7 @@ #include "caca.h" #include "caca_internals.h" +static unsigned int _get_next_event(void); static void _push_key(unsigned int); static unsigned int _pop_key(void); static unsigned int _read_key(void); @@ -63,18 +64,59 @@ static int keys = 0; /** \brief Get the next mouse or keyboard input event. * - * This function polls the event queue for mouse or keyboard events and - * returns the event. It is non-blocking and returns zero if no event is + * 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. It 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. * - * \return The next event in the queue, or 0 if no event is pending. + * \param event_mask Bitmask of requested events. + * \return The next matching event in the queue, or 0 if no event is pending. */ -unsigned int caca_get_event(void) +unsigned int caca_get_event(unsigned int event_mask) +{ + for( ; ; ) + { + unsigned int event = _get_next_event(); + + if(!event || event & event_mask) + return event; + } +} + +/** \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. 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. + */ +unsigned int caca_wait_event(unsigned int event_mask) +{ + for( ; ; ) + { + unsigned int event = _get_next_event(); + + if(event & event_mask) + return event; + + usleep(1000); + } +} + +/* + * XXX: The following functions are local. + */ + +static unsigned int _get_next_event(void) { unsigned int event = 0; #if defined(USE_X11) + /* The X11 event check routine */ if(_caca_driver == CACA_DRIVER_X11) { XEvent xevent; @@ -88,6 +130,7 @@ unsigned int caca_get_event(void) { KeySym keysym; + /* Check for mouse motion events */ if(xevent.type == MotionNotify) { unsigned int newx = xevent.xmotion.x / x11_font_width; @@ -107,18 +150,20 @@ unsigned int caca_get_event(void) return CACA_EVENT_MOUSE_MOTION | (newx << 12) | (newy << 0); } + /* Check for mouse press and release events */ if(xevent.type == ButtonPress) return CACA_EVENT_MOUSE_PRESS | 1; if(xevent.type == ButtonRelease) return CACA_EVENT_MOUSE_RELEASE | 1; + /* Check for key press and release events */ if(xevent.type == KeyPress) event |= CACA_EVENT_KEY_PRESS; else if(xevent.type == KeyRelease) event |= CACA_EVENT_KEY_RELEASE; else - return 0; + continue; if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL)) return event | key; @@ -175,8 +220,7 @@ unsigned int caca_get_event(void) _pop_key(); getmouse(&mevent); _push_key(CACA_EVENT_MOUSE_PRESS | 1); - return CACA_EVENT_MOUSE_MOTION - | (mevent.x << 12) | (mevent.y << 0); + return CACA_EVENT_MOUSE_MOTION | (mevent.x << 12) | mevent.y; } switch(keybuf[0]) @@ -277,31 +321,6 @@ unsigned int caca_get_event(void) return CACA_EVENT_KEY_PRESS | '\x1b'; } -/** \brief Wait for the next mouse or keyboard input event. - * - * This function returns the first mouse or keyboard event in the queue. If - * no event is pending, it blocks until an event is received. See also - * caca_get_event() for a non-blocking version of this function. - * - * \return The next event in the queue. - */ -unsigned int caca_wait_event(void) -{ - for( ; ; ) - { - unsigned int event = caca_get_event(); - - if(event) - return event; - - usleep(1000); - } -} - -/* - * XXX: The following functions are local. - */ - static void _push_key(unsigned int key) { if(keys == KEY_BUFLEN) diff --git a/test/dithering.c b/test/dithering.c index a67ca7a..fc32c6a 100644 --- a/test/dithering.c +++ b/test/dithering.c @@ -128,7 +128,7 @@ int main(void) caca_refresh(); - while((caca_get_event() & 0xff000000) != CACA_EVENT_KEY_PRESS); + while(!caca_get_event(CACA_EVENT_KEY_PRESS)); caca_end(); return 0; diff --git a/test/hsv.c b/test/hsv.c index 3b44d61..2c55d70 100644 --- a/test/hsv.c +++ b/test/hsv.c @@ -56,7 +56,7 @@ int main(void) caca_refresh(); - while((caca_get_event() & 0xff000000) != CACA_EVENT_KEY_PRESS); + while(!caca_get_event(CACA_EVENT_KEY_PRESS)); caca_end();