Changeset 710


Ignore:
Timestamp:
Apr 1, 2006, 4:44:09 PM (15 years ago)
Author:
Sam Hocevar
Message:
  • Merged caca_wait_event() into caca_get_event() and added a timeout parameter to do what both functions did before, and even more.
Location:
libcaca/trunk
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca/caca.h

    r703 r710  
    238238 *
    239239 *  @{ */
    240 int caca_get_event(caca_t *kk, unsigned int, struct caca_event *);
    241 int caca_wait_event(caca_t *kk, unsigned int, struct caca_event *);
     240int caca_get_event(caca_t *kk, unsigned int, struct caca_event *, int);
    242241unsigned int caca_get_mouse_x(caca_t *kk);
    243242unsigned int caca_get_mouse_y(caca_t *kk);
  • libcaca/trunk/caca/event.c

    r681 r710  
    4646 *  This function polls the event queue for mouse or keyboard events matching
    4747 *  the event mask and returns the first matching event. Non-matching events
    48  *  are discarded. \c event_mask must have a non-zero value. This function is
    49  *  non-blocking and returns zero if no more events are pending in the queue.
    50  *  See also caca_wait_event() for a blocking version of this function.
     48 *  are discarded. \c event_mask must have a non-zero value.
     49 *
     50 *  The timeout value tells how long this function needs to wait for an
     51 *  event. A value of zero returns immediately and the function returns zero
     52 *  if no more events are pending in the queue. A negative value causes the
     53 *  function to wait indefinitely until a matching event is received.
    5154 *
    5255 * \param event_mask Bitmask of requested events.
     56 * \param timeout A timeout value in microseconds
    5357 * \return The next matching event in the queue, or 0 if no event is pending.
    5458 */
    55 int caca_get_event(caca_t *kk, unsigned int event_mask, struct caca_event *ev)
    56 {
     59int caca_get_event(caca_t *kk, unsigned int event_mask,
     60                   struct caca_event *ev, int timeout)
     61{
     62    struct caca_timer timer;
     63    int usec = 0;
     64
    5765    if(!event_mask)
    5866        return 0;
    5967
     68    if(timeout > 0)
     69        _caca_getticks(&timer);
     70
    6071    for( ; ; )
    6172    {
    6273        int ret = _get_next_event(kk, ev);
    6374
    64         if(!ret || ev->type & event_mask)
     75        /* If we got the event we wanted, return */
     76        if(ev->type & event_mask)
    6577            return ret;
    66     }
    67 }
    68 
    69 /** \brief Wait for the next mouse or keyboard input event.
    70  *
    71  *  This function returns the first mouse or keyboard event in the queue
    72  *  that matches the event mask. If no event is pending, it blocks until a
    73  *  matching event is received. \c event_mask must have a non-zero value.
    74  *  See also caca_get_event() for a non-blocking version of this function.
    75  *
    76  *  \param event_mask Bitmask of requested events.
    77  *  \return The next event in the queue.
    78  */
    79 int caca_wait_event(caca_t *kk, unsigned int event_mask, struct caca_event *ev)
    80 {
    81     if(!event_mask)
    82         return 0;
    83 
    84     for( ; ; )
    85     {
    86         int ret = _get_next_event(kk, ev);
    87 
    88         if(ret && (ev->type & event_mask))
    89             return ret;
    90 
    91         _caca_sleep(10000);
     78
     79        /* If there is no timeout, sleep and try again. */
     80        if(timeout < 0)
     81        {
     82            _caca_sleep(10000);
     83            continue;
     84        }
     85
     86        /* If we timeouted, return an empty event */
     87        if(usec >= timeout)
     88        {
     89            ev->type = CACA_EVENT_NONE;
     90            return 0;
     91        }
     92
     93        /* Otherwise sleep a bit. Our granularity is far too high for values
     94         * below 10000 microseconds so we cheat a bit. */
     95        if(usec > 10000)
     96            _caca_sleep(10000);
     97        else
     98            _caca_sleep(1000);
     99
     100        usec += _caca_getticks(&timer);
    92101    }
    93102}
  • libcaca/trunk/src/aafire.c

    r704 r710  
    267267#ifdef LIBCACA
    268268      struct caca_event ev;
    269       if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev))
     269      if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0))
    270270        {
    271271          switch(ev.data.key.c)
  • libcaca/trunk/src/cacaball.c

    r704 r710  
    9494    {
    9595        struct caca_event ev;
    96         if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev))
     96        if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0))
    9797        {
    9898            switch(ev.data.key.c)
  • libcaca/trunk/src/cacamoir.c

    r704 r710  
    7070    {
    7171        struct caca_event ev;
    72         if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev))
     72        if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0))
    7373        {
    7474            switch(ev.data.key.c)
  • libcaca/trunk/src/cacaplas.c

    r703 r710  
    8686    {
    8787        struct caca_event ev;
    88         if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev))
     88        if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0))
    8989        {
    9090            switch(ev.data.key.c)
  • libcaca/trunk/src/cacaplay.c

    r702 r710  
    6767    caca_display(kk);
    6868
    69     while(caca_wait_event(kk, CACA_EVENT_KEY_PRESS, &ev))
     69    while(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, -1))
    7070    {
    7171        if(ev.data.key.c == CACA_KEY_ESCAPE)
  • libcaca/trunk/src/cacaview.c

    r681 r710  
    152152
    153153        if(update)
    154             event = caca_get_event(kk, event_mask, &ev);
     154            event = caca_get_event(kk, event_mask, &ev, 0);
    155155        else
    156             event = caca_wait_event(kk, event_mask, &ev);
     156            event = caca_get_event(kk, event_mask, &ev, -1);
    157157
    158158        while(event)
     
    305305                help = new_help;
    306306
    307             event = caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev);
     307            event = caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0);
    308308        }
    309309
  • libcaca/trunk/test/colors.c

    r681 r710  
    5151
    5252    caca_display(kk);
    53     caca_wait_event(kk, CACA_EVENT_KEY_PRESS, &ev);
     53    caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, -1);
    5454
    5555    caca_detach(kk);
  • libcaca/trunk/test/demo.c

    r690 r710  
    7575        int menu = 0, mouse = 0, xmouse = 0, ymouse = 0;
    7676
    77         while(caca_get_event(kk, CACA_EVENT_ANY, &ev))
     77        while(caca_get_event(kk, CACA_EVENT_ANY, &ev, 0))
    7878        {
    7979            if(demo && (ev.type & CACA_EVENT_KEY_PRESS))
     
    149149                ymouse = ev.data.mouse.y;
    150150            }
     151            else if(ev.type & CACA_EVENT_RESIZE)
     152            {
     153                mouse = 1; /* old hack */
     154            }
    151155        }
    152156
     
    157161            {
    158162                cucul_set_color(qq, CUCUL_COLOR_RED, CUCUL_COLOR_BLACK);
    159                 cucul_putstr(qq, xmouse, ymouse, "|\\");
     163                cucul_putstr(qq, xmouse, ymouse,     ".");
     164                cucul_putstr(qq, xmouse, ymouse + 1, "|\\");
    160165            }
    161166            caca_display(kk);
  • libcaca/trunk/test/dithering.c

    r681 r710  
    125125    caca_display(kk);
    126126
    127     caca_wait_event(kk, CACA_EVENT_KEY_PRESS, &ev);
     127    caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, -1);
    128128
    129129    caca_detach(kk);
  • libcaca/trunk/test/event.c

    r681 r710  
    5555        struct caca_event ev;
    5656        static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
    57         int ret = caca_wait_event(kk, CACA_EVENT_ANY, &ev);
     57        int ret = caca_get_event(kk, CACA_EVENT_ANY, &ev, -1);
    5858
    5959        if(!ret)
     
    7878            events[0] = ev;
    7979
    80             ret = caca_get_event(kk, CACA_EVENT_ANY, &ev);
     80            ret = caca_get_event(kk, CACA_EVENT_ANY, &ev, 0);
    8181        }
    8282        while(ret);
  • libcaca/trunk/test/gamma.c

    r681 r710  
    6363    for(x = 0; ; x++)
    6464    {
    65         int ret = caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev);
     65        int ret = caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0);
    6666
    6767        if(ret)
  • libcaca/trunk/test/hsv.c

    r681 r710  
    5454    caca_display(kk);
    5555
    56     caca_wait_event(kk, CACA_EVENT_KEY_PRESS, &ev);
     56    caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0);
    5757
    5858    caca_detach(kk);
  • libcaca/trunk/test/spritedit.c

    r681 r710  
    5858        char buf[BUFSIZ];
    5959
    60         while(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev))
     60        while(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0))
    6161        {
    6262            switch(ev.data.key.c)
  • libcaca/trunk/test/transform.c

    r681 r710  
    109109    caca_display(kk);
    110110
    111     caca_wait_event(kk, CACA_EVENT_KEY_PRESS, &ev);
     111    caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, -1);
    112112
    113113    caca_detach(kk);
  • libcaca/trunk/test/unicode.c

    r681 r710  
    8888    caca_display(kk);
    8989
    90     caca_wait_event(kk, CACA_EVENT_KEY_PRESS, &ev);
     90    caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, -1);
    9191
    9292    caca_detach(kk);
Note: See TracChangeset for help on using the changeset viewer.