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/caca
Files:
2 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}
Note: See TracChangeset for help on using the changeset viewer.