Parcourir la source

* src/io.c:

+ caca_get_event() and caca_wait_event() now accept a mask as an argument
      in order to select events.
tags/v0.99.beta14
Sam Hocevar sam il y a 21 ans
Parent
révision
17f9a58aa6
8 fichiers modifiés avec 112 ajouts et 91 suppressions
  1. +1
    -1
      examples/aafire.c
  2. +36
    -34
      examples/cacaview.c
  3. +1
    -1
      examples/demo.c
  4. +17
    -18
      examples/spritedit.c
  5. +4
    -3
      src/caca.h
  6. +51
    -32
      src/io.c
  7. +1
    -1
      test/dithering.c
  8. +1
    -1
      test/hsv.c

+ 1
- 1
examples/aafire.c Voir le fichier

@@ -236,7 +236,7 @@ game (void)
int event; int event;
gentable (); gentable ();
#ifdef LIBCACA #ifdef LIBCACA
while (!(event = caca_get_event() & CACA_EVENT_KEY_PRESS))
while (!(event = caca_get_event(CACA_EVENT_KEY_PRESS)))
#else #else
while (!(event = aa_getevent (context, 0)) || event == AA_RESIZE) while (!(event = aa_getevent (context, 0)) || event == AA_RESIZE)
#endif #endif


+ 36
- 34
examples/cacaview.c Voir le fichier

@@ -108,112 +108,114 @@ int main(int argc, char **argv)
int ww = caca_get_width(); int ww = caca_get_width();
int wh = caca_get_height(); 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; if(items) current = (current + 1) % items;
reload = 1; reload = 1;
break; break;
case CACA_EVENT_KEY_PRESS | 'p':
case CACA_EVENT_KEY_PRESS | 'P':
case 'p':
case 'P':
if(items) current = (items + current - 1) % items; if(items) current = (items + current - 1) % items;
reload = 1; reload = 1;
break; break;
case CACA_EVENT_KEY_PRESS | 'f':
case CACA_EVENT_KEY_PRESS | 'F':
case 'f':
case 'F':
fullscreen = ~fullscreen; fullscreen = ~fullscreen;
update = 1; update = 1;
break; break;
case CACA_EVENT_KEY_PRESS | 'b':
case 'b':
i = 1 + caca_get_feature(CACA_BACKGROUND); i = 1 + caca_get_feature(CACA_BACKGROUND);
if(i > CACA_BACKGROUND_MAX) i = CACA_BACKGROUND_MIN; if(i > CACA_BACKGROUND_MAX) i = CACA_BACKGROUND_MIN;
caca_set_feature(i); caca_set_feature(i);
new_status = STATUS_BACKGROUND; new_status = STATUS_BACKGROUND;
update = 1; update = 1;
break; break;
case CACA_EVENT_KEY_PRESS | 'B':
case 'B':
i = -1 + caca_get_feature(CACA_BACKGROUND); i = -1 + caca_get_feature(CACA_BACKGROUND);
if(i < CACA_BACKGROUND_MIN) i = CACA_BACKGROUND_MAX; if(i < CACA_BACKGROUND_MIN) i = CACA_BACKGROUND_MAX;
caca_set_feature(i); caca_set_feature(i);
new_status = STATUS_BACKGROUND; new_status = STATUS_BACKGROUND;
update = 1; update = 1;
break; break;
case CACA_EVENT_KEY_PRESS | 'a':
case 'a':
i = 1 + caca_get_feature(CACA_ANTIALIASING); i = 1 + caca_get_feature(CACA_ANTIALIASING);
if(i > CACA_ANTIALIASING_MAX) i = CACA_ANTIALIASING_MIN; if(i > CACA_ANTIALIASING_MAX) i = CACA_ANTIALIASING_MIN;
caca_set_feature(i); caca_set_feature(i);
new_status = STATUS_ANTIALIASING; new_status = STATUS_ANTIALIASING;
update = 1; update = 1;
break; break;
case CACA_EVENT_KEY_PRESS | 'A':
case 'A':
i = -1 + caca_get_feature(CACA_ANTIALIASING); i = -1 + caca_get_feature(CACA_ANTIALIASING);
if(i < CACA_ANTIALIASING_MIN) i = CACA_ANTIALIASING_MAX; if(i < CACA_ANTIALIASING_MIN) i = CACA_ANTIALIASING_MAX;
caca_set_feature(i); caca_set_feature(i);
new_status = STATUS_ANTIALIASING; new_status = STATUS_ANTIALIASING;
update = 1; update = 1;
break; break;
case CACA_EVENT_KEY_PRESS | 'd':
case 'd':
i = 1 + caca_get_feature(CACA_DITHERING); i = 1 + caca_get_feature(CACA_DITHERING);
if(i > CACA_DITHERING_MAX) i = CACA_DITHERING_MIN; if(i > CACA_DITHERING_MAX) i = CACA_DITHERING_MIN;
caca_set_feature(i); caca_set_feature(i);
new_status = STATUS_DITHERING; new_status = STATUS_DITHERING;
update = 1; update = 1;
break; break;
case CACA_EVENT_KEY_PRESS | 'D':
case 'D':
i = -1 + caca_get_feature(CACA_DITHERING); i = -1 + caca_get_feature(CACA_DITHERING);
if(i < CACA_DITHERING_MIN) i = CACA_DITHERING_MAX; if(i < CACA_DITHERING_MIN) i = CACA_DITHERING_MAX;
caca_set_feature(i); caca_set_feature(i);
new_status = STATUS_DITHERING; new_status = STATUS_DITHERING;
update = 1; update = 1;
break; break;
case CACA_EVENT_KEY_PRESS | '+':
case '+':
zoom++; zoom++;
if(zoom > 48) zoom = 48; else update = 1; if(zoom > 48) zoom = 48; else update = 1;
break; break;
case CACA_EVENT_KEY_PRESS | '-':
case '-':
zoom--; zoom--;
if(zoom < -48) zoom = -48; else update = 1; if(zoom < -48) zoom = -48; else update = 1;
break; break;
case CACA_EVENT_KEY_PRESS | 'x':
case CACA_EVENT_KEY_PRESS | 'X':
case 'x':
case 'X':
zoom = 0; zoom = 0;
update = 1; update = 1;
break; 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; if(zoom > 0) y -= 1 + h / (2 + zoom) / 8;
update = 1; update = 1;
break; 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; if(zoom > 0) y += 1 + h / (2 + zoom) / 8;
update = 1; update = 1;
break; 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; if(zoom > 0) x -= 1 + w / (2 + zoom) / 8;
update = 1; update = 1;
break; 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; if(zoom > 0) x += 1 + w / (2 + zoom) / 8;
update = 1; update = 1;
break; break;
case CACA_EVENT_KEY_PRESS | '?':
case '?':
new_help = !help; new_help = !help;
update = 1; update = 1;
break; break;
case CACA_EVENT_KEY_PRESS | 'q':
case CACA_EVENT_KEY_PRESS | 'Q':
case 'q':
case 'Q':
quit = 1; quit = 1;
break; break;
} }


+ 1
- 1
examples/demo.c Voir le fichier

@@ -74,7 +74,7 @@ int main(int argc, char **argv)
int menu = 0, mouse = 0, xmouse = 0, ymouse = 0; int menu = 0, mouse = 0, xmouse = 0, ymouse = 0;
int event; int event;


while((event = caca_get_event()))
while((event = caca_get_event(CACA_EVENT_ANY)))
{ {
if(demo && (event & CACA_EVENT_KEY_PRESS)) if(demo && (event & CACA_EVENT_KEY_PRESS))
{ {


+ 17
- 18
examples/spritedit.c Voir le fichier

@@ -58,25 +58,24 @@ int main(int argc, char **argv)
char buf[BUFSIZ]; char buf[BUFSIZ];
int event; 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(); caca_clear();


+ 4
- 3
src/caca.h Voir le fichier

@@ -180,7 +180,8 @@ enum caca_event
CACA_EVENT_KEY_RELEASE = 0x02000000, /**< A key was released. */ CACA_EVENT_KEY_RELEASE = 0x02000000, /**< A key was released. */
CACA_EVENT_MOUSE_PRESS = 0x04000000, /**< A mouse button was pressed. */ CACA_EVENT_MOUSE_PRESS = 0x04000000, /**< A mouse button was pressed. */
CACA_EVENT_MOUSE_RELEASE = 0x08000000, /**< A mouse button was released. */ 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. /** \brief Special key values.
@@ -236,8 +237,8 @@ void caca_end(void);
* clicks. * 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 /** \defgroup char Character printing


+ 51
- 32
src/io.c Voir le fichier

@@ -53,6 +53,7 @@
#include "caca.h" #include "caca.h"
#include "caca_internals.h" #include "caca_internals.h"


static unsigned int _get_next_event(void);
static void _push_key(unsigned int); static void _push_key(unsigned int);
static unsigned int _pop_key(void); static unsigned int _pop_key(void);
static unsigned int _read_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. /** \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 * pending in the queue. See also caca_wait_event() for a blocking version
* of this function. * 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; unsigned int event = 0;


#if defined(USE_X11) #if defined(USE_X11)
/* The X11 event check routine */
if(_caca_driver == CACA_DRIVER_X11) if(_caca_driver == CACA_DRIVER_X11)
{ {
XEvent xevent; XEvent xevent;
@@ -88,6 +130,7 @@ unsigned int caca_get_event(void)
{ {
KeySym keysym; KeySym keysym;


/* Check for mouse motion events */
if(xevent.type == MotionNotify) if(xevent.type == MotionNotify)
{ {
unsigned int newx = xevent.xmotion.x / x11_font_width; 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); return CACA_EVENT_MOUSE_MOTION | (newx << 12) | (newy << 0);
} }


/* Check for mouse press and release events */
if(xevent.type == ButtonPress) if(xevent.type == ButtonPress)
return CACA_EVENT_MOUSE_PRESS | 1; return CACA_EVENT_MOUSE_PRESS | 1;


if(xevent.type == ButtonRelease) if(xevent.type == ButtonRelease)
return CACA_EVENT_MOUSE_RELEASE | 1; return CACA_EVENT_MOUSE_RELEASE | 1;


/* Check for key press and release events */
if(xevent.type == KeyPress) if(xevent.type == KeyPress)
event |= CACA_EVENT_KEY_PRESS; event |= CACA_EVENT_KEY_PRESS;
else if(xevent.type == KeyRelease) else if(xevent.type == KeyRelease)
event |= CACA_EVENT_KEY_RELEASE; event |= CACA_EVENT_KEY_RELEASE;
else else
return 0;
continue;


if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL)) if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL))
return event | key; return event | key;
@@ -175,8 +220,7 @@ unsigned int caca_get_event(void)
_pop_key(); _pop_key();
getmouse(&mevent); getmouse(&mevent);
_push_key(CACA_EVENT_MOUSE_PRESS | 1); _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]) switch(keybuf[0])
@@ -277,31 +321,6 @@ unsigned int caca_get_event(void)
return CACA_EVENT_KEY_PRESS | '\x1b'; 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) static void _push_key(unsigned int key)
{ {
if(keys == KEY_BUFLEN) if(keys == KEY_BUFLEN)


+ 1
- 1
test/dithering.c Voir le fichier

@@ -128,7 +128,7 @@ int main(void)


caca_refresh(); caca_refresh();


while((caca_get_event() & 0xff000000) != CACA_EVENT_KEY_PRESS);
while(!caca_get_event(CACA_EVENT_KEY_PRESS));


caca_end(); caca_end();
return 0; return 0;


+ 1
- 1
test/hsv.c Voir le fichier

@@ -56,7 +56,7 @@ int main(void)


caca_refresh(); caca_refresh();


while((caca_get_event() & 0xff000000) != CACA_EVENT_KEY_PRESS);
while(!caca_get_event(CACA_EVENT_KEY_PRESS));


caca_end(); caca_end();




Chargement…
Annuler
Enregistrer