source: libcaca/trunk/caca/event.c @ 710

Last change on this file since 710 was 710, checked in by Sam Hocevar, 15 years ago
  • Merged caca_wait_event() into caca_get_event() and added a timeout parameter to do what both functions did before, and even more.
  • Property svn:keywords set to Id
File size: 7.3 KB
RevLine 
[35]1/*
[672]2 *  libcaca       Colour ASCII-Art library
[527]3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
[268]4 *                All Rights Reserved
[35]5 *
[268]6 *  This library is free software; you can redistribute it and/or
[522]7 *  modify it under the terms of the Do What The Fuck You Want To
8 *  Public License, Version 2, as published by Sam Hocevar. See
9 *  http://sam.zoy.org/wtfpl/COPYING for more details.
[35]10 */
[17]11
[345]12/** \file event.c
[268]13 *  \version \$Id: event.c 710 2006-04-01 14:44:09Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
[298]15 *  \brief Event handling
[205]16 *
[268]17 *  This file contains event handling functions for keyboard and mouse input.
[205]18 */
19
[63]20#include "config.h"
21
[681]22#if !defined(__KERNEL__)
23#   include <stdio.h>
24#endif
25
[524]26#include "cucul.h"
27#include "cucul_internals.h"
[185]28#include "caca.h"
29#include "caca_internals.h"
[17]30
[681]31static int _get_next_event(caca_t *, struct caca_event *);
32static int _lowlevel_event(caca_t *, struct caca_event *);
[199]33
[322]34#if !defined(_DOXYGEN_SKIP_ME)
[331]35/* If no new key was pressed after AUTOREPEAT_THRESHOLD usec, assume the
36 * key was released */
37#define AUTOREPEAT_THRESHOLD 200000
38/* Start repeating key after AUTOREPEAT_TRIGGER usec and send keypress
39 * events every AUTOREPEAT_RATE usec. */
40#define AUTOREPEAT_TRIGGER 300000
41#define AUTOREPEAT_RATE 100000
42#endif
43
[268]44/** \brief Get the next mouse or keyboard input event.
[257]45 *
[316]46 *  This function polls the event queue for mouse or keyboard events matching
47 *  the event mask and returns the first matching event. Non-matching events
[710]48 *  are discarded. \c event_mask must have a non-zero value.
[268]49 *
[710]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.
54 *
[316]55 * \param event_mask Bitmask of requested events.
[710]56 * \param timeout A timeout value in microseconds
[316]57 * \return The next matching event in the queue, or 0 if no event is pending.
[257]58 */
[710]59int caca_get_event(caca_t *kk, unsigned int event_mask,
60                   struct caca_event *ev, int timeout)
[79]61{
[710]62    struct caca_timer timer;
63    int usec = 0;
64
[332]65    if(!event_mask)
[681]66        return 0;
[332]67
[710]68    if(timeout > 0)
69        _caca_getticks(&timer);
70
[316]71    for( ; ; )
72    {
[681]73        int ret = _get_next_event(kk, ev);
[316]74
[710]75        /* If we got the event we wanted, return */
76        if(ev->type & event_mask)
[681]77            return ret;
[316]78
[710]79        /* If there is no timeout, sleep and try again. */
80        if(timeout < 0)
81        {
82            _caca_sleep(10000);
83            continue;
84        }
[332]85
[710]86        /* If we timeouted, return an empty event */
87        if(usec >= timeout)
88        {
89            ev->type = CACA_EVENT_NONE;
90            return 0;
91        }
[316]92
[710]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);
[316]99
[710]100        usec += _caca_getticks(&timer);
[316]101    }
102}
103
[347]104/** \brief Return the X mouse coordinate.
105 *
106 *  This function returns the X coordinate of the mouse position last time
107 *  it was detected. This function is not reliable if the ncurses or S-Lang
108 *  drivers are being used, because mouse position is only detected when
109 *  the mouse is clicked. Other drivers such as X11 work well.
110 *
111 *  \return The X mouse coordinate.
112 */
[524]113unsigned int caca_get_mouse_x(caca_t *kk)
[347]114{
[551]115    if(kk->mouse.x >= kk->qq->width)
116        kk->mouse.x = kk->qq->width - 1;
[351]117
[551]118    return kk->mouse.x;
[347]119}
120
121/** \brief Return the Y mouse coordinate.
122 *
123 *  This function returns the Y coordinate of the mouse position last time
124 *  it was detected. This function is not reliable if the ncurses or S-Lang
125 *  drivers are being used, because mouse position is only detected when
126 *  the mouse is clicked. Other drivers such as X11 work well.
127 *
128 *  \return The Y mouse coordinate.
129 */
[524]130unsigned int caca_get_mouse_y(caca_t *kk)
[347]131{
[551]132    if(kk->mouse.y >= kk->qq->height)
133        kk->mouse.y = kk->qq->height - 1;
[351]134
[551]135    return kk->mouse.y;
[347]136}
137
[316]138/*
139 * XXX: The following functions are local.
140 */
141
[681]142static int _get_next_event(caca_t *kk, struct caca_event *ev)
[316]143{
[331]144#if defined(USE_SLANG) || defined(USE_NCURSES)
145    unsigned int ticks;
[498]146#endif
[681]147    int ret;
[487]148
[553]149    /* If we are about to return a resize event, acknowledge it */
150    if(kk->resize.resized)
151    {
152        kk->resize.resized = 0;
153        _caca_handle_resize(kk);
[681]154        ev->type = CACA_EVENT_RESIZE;
155        ev->data.resize.w = kk->qq->width;
156        ev->data.resize.h = kk->qq->height;
157        return 1;
[553]158    }
159
[681]160    ret = _lowlevel_event(kk, ev);
[331]161
162#if defined(USE_SLANG)
[550]163    if(kk->drv.driver != CACA_DRIVER_SLANG)
[331]164#endif
165#if defined(USE_NCURSES)
[550]166    if(kk->drv.driver != CACA_DRIVER_NCURSES)
[331]167#endif
[681]168    return ret;
[331]169
170#if defined(USE_SLANG) || defined(USE_NCURSES)
171    /* Simulate long keypresses using autorepeat features */
[527]172    ticks = _caca_getticks(&kk->events.key_timer);
173    kk->events.last_key_ticks += ticks;
174    kk->events.autorepeat_ticks += ticks;
[331]175
176    /* Handle autorepeat */
[681]177    if(kk->events.last_key_event.type
[527]178           && kk->events.autorepeat_ticks > AUTOREPEAT_TRIGGER
179           && kk->events.autorepeat_ticks > AUTOREPEAT_THRESHOLD
180           && kk->events.autorepeat_ticks > AUTOREPEAT_RATE)
[331]181    {
[681]182        _push_event(kk, ev);
[527]183        kk->events.autorepeat_ticks -= AUTOREPEAT_RATE;
[681]184        *ev = kk->events.last_key_event;
185        return 1;
[331]186    }
187
188    /* We are in autorepeat mode and the same key was just pressed, ignore
189     * this event and return the next one by calling ourselves. */
[681]190    if(ev->type == CACA_EVENT_KEY_PRESS
191        && kk->events.last_key_event.type
192        && ev->data.key.c == kk->events.last_key_event.data.key.c
193        && ev->data.key.ucs4 == kk->events.last_key_event.data.key.ucs4)
[331]194    {
[527]195        kk->events.last_key_ticks = 0;
[681]196        return _get_next_event(kk, ev);
[331]197    }
198
199    /* We are in autorepeat mode, but key has expired or a new key was
200     * pressed - store our event and return a key release event first */
[681]201    if(kk->events.last_key_event.type
[527]202          && (kk->events.last_key_ticks > AUTOREPEAT_THRESHOLD
[681]203               || (ev->type & CACA_EVENT_KEY_PRESS)))
[331]204    {
[681]205        _push_event(kk, ev);
206        *ev = kk->events.last_key_event;
207        ev->type = CACA_EVENT_KEY_RELEASE;
208        kk->events.last_key_event.type = CACA_EVENT_NONE;
209        return 1;
[331]210    }
211
212    /* A new key was pressed, enter autorepeat mode */
[681]213    if(ev->type & CACA_EVENT_KEY_PRESS)
[331]214    {
[527]215        kk->events.last_key_ticks = 0;
216        kk->events.autorepeat_ticks = 0;
[681]217        kk->events.last_key_event = *ev;
[331]218    }
219
[681]220    return ev->type ? 1 : 0;
[331]221#endif
222}
223
[681]224static int _lowlevel_event(caca_t *kk, struct caca_event *ev)
[331]225{
[336]226#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
[681]227    int ret = _pop_event(kk, ev);
[336]228
[681]229    if(ret)
230        return ret;
[336]231#endif
[329]232
[681]233    return kk->drv.get_event(kk, ev);
[198]234}
235
[681]236#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
237void _push_event(caca_t *kk, struct caca_event *ev)
[199]238{
[681]239    if(!ev->type || kk->events.queue == EVENTBUF_LEN)
[213]240        return;
[681]241    kk->events.buf[kk->events.queue] = *ev;
[541]242    kk->events.queue++;
[199]243}
244
[681]245int _pop_event(caca_t *kk, struct caca_event *ev)
[199]246{
[213]247    int i;
248
[541]249    if(kk->events.queue == 0)
[681]250        return 0;
[199]251
[681]252    *ev = kk->events.buf[0];
[541]253    for(i = 1; i < kk->events.queue; i++)
254        kk->events.buf[i - 1] = kk->events.buf[i];
255    kk->events.queue--;
[251]256
[681]257    return 1;
[199]258}
[336]259#endif
[199]260
Note: See TracBrowser for help on using the repository browser.