Ignore:
Timestamp:
Mar 23, 2006, 7:36:59 PM (14 years ago)
Author:
Sam Hocevar
Message:
  • Massive rework of the event handling code, as per the TODO list.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca/event.c

    r672 r681  
    2020#include "config.h"
    2121
     22#if !defined(__KERNEL__)
     23#   include <stdio.h>
     24#endif
     25
    2226#include "cucul.h"
    2327#include "cucul_internals.h"
     
    2529#include "caca_internals.h"
    2630
    27 static unsigned int _get_next_event(caca_t *);
    28 static unsigned int _lowlevel_event(caca_t *);
     31static int _get_next_event(caca_t *, struct caca_event *);
     32static int _lowlevel_event(caca_t *, struct caca_event *);
    2933
    3034#if !defined(_DOXYGEN_SKIP_ME)
     
    4953 * \return The next matching event in the queue, or 0 if no event is pending.
    5054 */
    51 unsigned int caca_get_event(caca_t *kk, unsigned int event_mask)
     55int caca_get_event(caca_t *kk, unsigned int event_mask, struct caca_event *ev)
    5256{
    5357    if(!event_mask)
    54         return CACA_EVENT_NONE;
     58        return 0;
    5559
    5660    for( ; ; )
    5761    {
    58         unsigned int event = _get_next_event(kk);
    59 
    60         if(!event || event & event_mask)
    61             return event;
     62        int ret = _get_next_event(kk, ev);
     63
     64        if(!ret || ev->type & event_mask)
     65            return ret;
    6266    }
    6367}
     
    7377 *  \return The next event in the queue.
    7478 */
    75 unsigned int caca_wait_event(caca_t *kk, unsigned int event_mask)
     79int caca_wait_event(caca_t *kk, unsigned int event_mask, struct caca_event *ev)
    7680{
    7781    if(!event_mask)
    78         return CACA_EVENT_NONE;
     82        return 0;
    7983
    8084    for( ; ; )
    8185    {
    82         unsigned int event = _get_next_event(kk);
    83 
    84         if(event & event_mask)
    85             return event;
     86        int ret = _get_next_event(kk, ev);
     87
     88        if(ret && (ev->type & event_mask))
     89            return ret;
    8690
    8791        _caca_sleep(10000);
     
    127131 */
    128132
    129 static unsigned int _get_next_event(caca_t *kk)
     133static int _get_next_event(caca_t *kk, struct caca_event *ev)
    130134{
    131135#if defined(USE_SLANG) || defined(USE_NCURSES)
    132136    unsigned int ticks;
    133137#endif
    134     unsigned int event;
     138    int ret;
    135139
    136140    /* If we are about to return a resize event, acknowledge it */
     
    139143        kk->resize.resized = 0;
    140144        _caca_handle_resize(kk);
    141         return CACA_EVENT_RESIZE;
    142     }
    143 
    144     event = _lowlevel_event(kk);
     145        ev->type = CACA_EVENT_RESIZE;
     146        ev->data.resize.w = kk->qq->width;
     147        ev->data.resize.h = kk->qq->height;
     148        return 1;
     149    }
     150
     151    ret = _lowlevel_event(kk, ev);
    145152
    146153#if defined(USE_SLANG)
     
    150157    if(kk->drv.driver != CACA_DRIVER_NCURSES)
    151158#endif
    152     return event;
     159    return ret;
    153160
    154161#if defined(USE_SLANG) || defined(USE_NCURSES)
     
    159166
    160167    /* Handle autorepeat */
    161     if(kk->events.last_key
     168    if(kk->events.last_key_event.type
    162169           && kk->events.autorepeat_ticks > AUTOREPEAT_TRIGGER
    163170           && kk->events.autorepeat_ticks > AUTOREPEAT_THRESHOLD
    164171           && kk->events.autorepeat_ticks > AUTOREPEAT_RATE)
    165172    {
    166         _push_event(kk, event);
     173        _push_event(kk, ev);
    167174        kk->events.autorepeat_ticks -= AUTOREPEAT_RATE;
    168         return CACA_EVENT_KEY_PRESS | kk->events.last_key;
     175        *ev = kk->events.last_key_event;
     176        return 1;
    169177    }
    170178
    171179    /* We are in autorepeat mode and the same key was just pressed, ignore
    172180     * this event and return the next one by calling ourselves. */
    173     if(event == (CACA_EVENT_KEY_PRESS | kk->events.last_key))
     181    if(ev->type == CACA_EVENT_KEY_PRESS
     182        && kk->events.last_key_event.type
     183        && ev->data.key.c == kk->events.last_key_event.data.key.c
     184        && ev->data.key.ucs4 == kk->events.last_key_event.data.key.ucs4)
    174185    {
    175186        kk->events.last_key_ticks = 0;
    176         return _get_next_event(kk);
     187        return _get_next_event(kk, ev);
    177188    }
    178189
    179190    /* We are in autorepeat mode, but key has expired or a new key was
    180191     * pressed - store our event and return a key release event first */
    181     if(kk->events.last_key
     192    if(kk->events.last_key_event.type
    182193          && (kk->events.last_key_ticks > AUTOREPEAT_THRESHOLD
    183                || (event & CACA_EVENT_KEY_PRESS)))
    184     {
    185         _push_event(kk, event);
    186         event = CACA_EVENT_KEY_RELEASE | kk->events.last_key;
    187         kk->events.last_key = 0;
    188         return event;
     194               || (ev->type & CACA_EVENT_KEY_PRESS)))
     195    {
     196        _push_event(kk, ev);
     197        *ev = kk->events.last_key_event;
     198        ev->type = CACA_EVENT_KEY_RELEASE;
     199        kk->events.last_key_event.type = CACA_EVENT_NONE;
     200        return 1;
    189201    }
    190202
    191203    /* A new key was pressed, enter autorepeat mode */
    192     if(event & CACA_EVENT_KEY_PRESS)
     204    if(ev->type & CACA_EVENT_KEY_PRESS)
    193205    {
    194206        kk->events.last_key_ticks = 0;
    195207        kk->events.autorepeat_ticks = 0;
    196         kk->events.last_key = event & 0x00ffffff;
    197     }
    198 
    199     return event;
    200 #endif
    201 }
    202 
    203 static unsigned int _lowlevel_event(caca_t *kk)
     208        kk->events.last_key_event = *ev;
     209    }
     210
     211    return ev->type ? 1 : 0;
     212#endif
     213}
     214
     215static int _lowlevel_event(caca_t *kk, struct caca_event *ev)
    204216{
    205217#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
    206     unsigned int event = _pop_event(kk);
    207 
    208     if(event)
    209         return event;
    210 #endif
    211 
    212     return kk->drv.get_event(kk);
    213 }
    214 
    215 #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
    216 void _push_event(caca_t *kk, unsigned int event)
    217 {
    218     if(!event || kk->events.queue == EVENTBUF_LEN)
     218    int ret = _pop_event(kk, ev);
     219
     220    if(ret)
     221        return ret;
     222#endif
     223
     224    return kk->drv.get_event(kk, ev);
     225}
     226
     227#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
     228void _push_event(caca_t *kk, struct caca_event *ev)
     229{
     230    if(!ev->type || kk->events.queue == EVENTBUF_LEN)
    219231        return;
    220     kk->events.buf[kk->events.queue] = event;
     232    kk->events.buf[kk->events.queue] = *ev;
    221233    kk->events.queue++;
    222234}
    223235
    224 unsigned int _pop_event(caca_t *kk)
     236int _pop_event(caca_t *kk, struct caca_event *ev)
    225237{
    226238    int i;
    227     unsigned int event;
    228239
    229240    if(kk->events.queue == 0)
    230         return CACA_EVENT_NONE;
    231 
    232     event = kk->events.buf[0];
     241        return 0;
     242
     243    *ev = kk->events.buf[0];
    233244    for(i = 1; i < kk->events.queue; i++)
    234245        kk->events.buf[i - 1] = kk->events.buf[i];
    235246    kk->events.queue--;
    236247
    237     return event;
    238 }
    239 #endif
    240 
     248    return 1;
     249}
     250#endif
     251
Note: See TracChangeset for help on using the changeset viewer.