Browse Source

* Merged caca_wait_event() into caca_get_event() and added a timeout

parameter to do what both functions did before, and even more.
tags/v0.99.beta14
Sam Hocevar sam 19 years ago
parent
commit
87604b8e1d
17 changed files with 63 additions and 50 deletions
  1. +1
    -2
      caca/caca.h
  2. +38
    -29
      caca/event.c
  3. +1
    -1
      src/aafire.c
  4. +1
    -1
      src/cacaball.c
  5. +1
    -1
      src/cacamoir.c
  6. +1
    -1
      src/cacaplas.c
  7. +1
    -1
      src/cacaplay.c
  8. +3
    -3
      src/cacaview.c
  9. +1
    -1
      test/colors.c
  10. +7
    -2
      test/demo.c
  11. +1
    -1
      test/dithering.c
  12. +2
    -2
      test/event.c
  13. +1
    -1
      test/gamma.c
  14. +1
    -1
      test/hsv.c
  15. +1
    -1
      test/spritedit.c
  16. +1
    -1
      test/transform.c
  17. +1
    -1
      test/unicode.c

+ 1
- 2
caca/caca.h View File

@@ -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);


+ 38
- 29
caca/event.c View File

@@ -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);
}
}



+ 1
- 1
src/aafire.c View File

@@ -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)
{


+ 1
- 1
src/cacaball.c View File

@@ -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)
{


+ 1
- 1
src/cacamoir.c View File

@@ -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)
{


+ 1
- 1
src/cacaplas.c View File

@@ -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)
{


+ 1
- 1
src/cacaplay.c View File

@@ -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;


+ 3
- 3
src/cacaview.c View File

@@ -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)


+ 1
- 1
test/colors.c View File

@@ -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);


+ 7
- 2
test/demo.c View File

@@ -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;


+ 1
- 1
test/dithering.c View File

@@ -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);


+ 2
- 2
test/event.c View File

@@ -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);



+ 1
- 1
test/gamma.c View File

@@ -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)
{


+ 1
- 1
test/hsv.c View File

@@ -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);


+ 1
- 1
test/spritedit.c View File

@@ -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)
{


+ 1
- 1
test/transform.c View File

@@ -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);


+ 1
- 1
test/unicode.c View File

@@ -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);


Loading…
Cancel
Save