Changeset 331


Ignore:
Timestamp:
Jan 9, 2004, 10:51:53 AM (17 years ago)
Author:
Sam Hocevar
Message:
  • test/event.c: + Do not refresh after each event, but only when there is no event

pending.

+ If the pressed key is a printable character, display it.

  • src/time.c: + Moved _caca_getticks() to this file.
  • src/caca.c: + Set the escape delay to a very low value in the ncurses driver,

because I don't want escape sequences to be entered manually.

  • src/io.c: + Autorepeat emulation in the ncurses and slang drivers: do not

immediately send the key release event.

  • configure.ac: + Check for usleep. + Improvements in the win32 platform detection.
Location:
libcaca/trunk
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/BUGS

    r297 r331  
    55   o  ncurses' hashmap scrolling optimisation code causes the screen to
    66      occasionally flicker because it tries to optimise the vertical
    7       scrolling.
    8 
    9    o  the X11 driver is very slow.
     7      scrolling. With S-Lang, SLtt_Term_Cannot_Scroll prevents this
     8      unwanted behaviour.
    109
    1110   o  some terminal emulators (Konsole, or the Linux VGA console) honour
  • libcaca/trunk/NOTES

    r281 r331  
    194194      work using <conio.h>. No need to kludge anything.
    195195
     196   o  Win32: we use GetConsoleScreenBufferInfo etc.
     197
  • libcaca/trunk/README

    r300 r331  
    1212     --disable-imlib2: remove Imlib2 support in cacaview
    1313
    14    o  Cross-compilation example:
     14   o  Cross-compilation examples:
    1515 
    1616     ./configure --disable-imlib2 --host=i386-pc-msdosdjgpp
     17
     18     ./configure --disable-imlib2 --host=i586-mingw32msvc
    1719
    1820
  • libcaca/trunk/configure.ac

    r330 r331  
    1616dnl AC_PROG_EGREP only exists in autoconf 2.54+, so we use AC_EGREP_CPP right
    1717dnl now otherwise it might be set in an obscure if statement.
    18 AC_EGREP_CPP(foo,foo)
     18AC_EGREP_CPP(foo, foo)
    1919
    2020AC_ARG_ENABLE(slang,
     
    2222AC_ARG_ENABLE(ncurses,
    2323  [  --enable-ncurses        ncurses graphics support (autodetected)])
     24AC_ARG_ENABLE(win32,
     25  [  --enable-win32          Windows console support (autodetected)])
    2426AC_ARG_ENABLE(conio,
    25   [  --enable-conio          DOS conio.h graphics support (default disabled)])
     27  [  --enable-conio          DOS conio.h graphics support (autodetected)])
    2628AC_ARG_ENABLE(x11,
    2729  [  --enable-x11            X11 support (autodetected)])
    2830
    2931AC_CHECK_HEADERS(inttypes.h endian.h)
    30 AC_CHECK_FUNCS(vsnprintf getenv putenv strcasecmp)
     32AC_CHECK_FUNCS(vsnprintf getenv putenv strcasecmp usleep Sleep)
    3133
    3234CACA_DRIVERS=""
     
    3638  AC_CHECK_HEADERS(conio.h,
    3739   [AC_MSG_CHECKING(for ScreenUpdate in pc.h)
    38     AC_EGREP_HEADER(ScreenUpdate,pc.h,
     40    AC_EGREP_HEADER(ScreenUpdate, pc.h,
    3941     [ac_cv_my_have_conio="yes"
    4042      AC_MSG_RESULT(yes)
     
    4648  if test "${ac_cv_my_have_conio}" = "no" -a "${enable_conio}" = "yes"; then
    4749    AC_MSG_ERROR([cannot find conio.h])
     50  fi
     51fi
     52
     53if test "${enable_win32}" != "no"; then
     54  ac_cv_my_have_win32="no"
     55  AC_CHECK_HEADERS(windows.h,
     56   [AC_MSG_CHECKING(for AllocConsole in windows.h)
     57    AC_EGREP_HEADER(AllocConsole, windows.h,
     58     [ac_cv_my_have_win32="yes"
     59      AC_MSG_RESULT(yes)
     60      AC_DEFINE(ALLOCCONSOLE_IN_WINDOWS_H, 1,
     61                Define if <windows.h> defines AllocConsole.)
     62      AC_DEFINE(USE_WIN32, 1, Define to activate the win32 backend driver)
     63      CACA_DRIVERS="${CACA_DRIVERS} win32"],
     64     [AC_MSG_RESULT(no)])])
     65  if test "${ac_cv_my_have_win32}" = "no" -a "${enable_win32}" = "yes"; then
     66    AC_MSG_ERROR([cannot find win32 console development files])
    4867  fi
    4968fi
  • libcaca/trunk/src/Makefile.am

    r247 r331  
    1919        sprite.c \
    2020        bitmap.c \
     21        time.c \
    2122        $(NULL)
    2223
  • libcaca/trunk/src/caca.c

    r324 r331  
    141141        mousemask(newmask, &oldmask);
    142142        mouseinterval(-1); /* No click emulation */
     143
     144        /* Set the escape delay to a ridiculously low value */
     145        ESCDELAY = 10;
    143146    }
    144147    else
  • libcaca/trunk/src/caca_internals.h

    r306 r331  
    4949};
    5050
     51/* Timer structure */
     52#define CACA_TIMER_INITIALIZER { 0, 0 }
     53struct caca_timer
     54{
     55    int last_sec, last_usec;
     56};
     57
    5158extern enum caca_driver _caca_driver;
    5259
     
    5663extern int _caca_init_bitmap(void);
    5764extern int _caca_end_bitmap(void);
     65
     66/* Timer functions */
     67extern unsigned int _caca_getticks(struct caca_timer *);
    5868
    5969/* Cached screen size */
  • libcaca/trunk/src/graphics.c

    r330 r331  
    6464#include <unistd.h>
    6565#include <stdarg.h>
    66 #include <sys/time.h>
    67 #include <time.h>
    6866
    6967#include "caca.h"
     
    188186#endif
    189187
    190 static unsigned int _caca_getticks(void);
    191 
    192188/** \brief Set the default colour pair.
    193189 *
     
    829825}
    830826
    831 static unsigned int _caca_getticks(void)
    832 {
    833     static int last_sec = 0, last_usec = 0;
    834 
    835     struct timeval tv;
    836     unsigned int ticks = 0;
    837 
    838     gettimeofday(&tv, NULL);
    839 
    840     if(last_sec != 0)
    841     {
    842         /* If the delay was greater than 60 seconds, return 10 seconds
    843          * otherwise we may overflow our ticks counter. */
    844         if(tv.tv_sec >= last_sec + 60)
    845             ticks = 60 * 1000000;
    846         else
    847         {
    848             ticks = (tv.tv_sec - last_sec) * 1000000;
    849             ticks += tv.tv_usec;
    850             ticks -= last_usec;
    851         }
    852     }
    853 
    854     last_sec = tv.tv_sec;
    855     last_usec = tv.tv_usec;
    856 
    857     return ticks;
    858 }
    859 
    860827/** \brief Flush pending changes and redraw the screen.
    861828 *
    862829 *  This function flushes all graphical operations and prints them to the
    863  *  screen. Nothing will show on the screen caca_refresh() is not called.
     830 *  screen. Nothing will show on the screen until caca_refresh() is
     831 *  called.
    864832 *
    865833 *  If caca_set_delay() was called with a non-zero value, caca_refresh()
     
    874842#define IDLE_USEC 10000
    875843#endif
     844    static struct caca_timer timer = CACA_TIMER_INITIALIZER;
    876845    static int lastticks = 0;
    877     int ticks = lastticks + _caca_getticks();
     846    int ticks = lastticks + _caca_getticks(&timer);
    878847
    879848#if defined(USE_SLANG)
     
    959928
    960929    /* Wait until _caca_delay + time of last call */
    961     ticks += _caca_getticks();
    962     for(; ticks + IDLE_USEC < (int)_caca_delay; ticks += _caca_getticks())
     930    ticks += _caca_getticks(&timer);
     931    for(ticks += _caca_getticks(&timer);
     932        ticks + IDLE_USEC < (int)_caca_delay;
     933        ticks += _caca_getticks(&timer))
     934    {
     935#if defined(HAVE_USLEEP)
    963936        usleep(IDLE_USEC);
     937#elif defined(HAVE_SLEEP)
     938        Sleep(IDLE_USEC / 1000);
     939#else
     940        SLEEP
     941#endif
     942    }
    964943
    965944    /* Update the sliding mean of the render time */
  • libcaca/trunk/src/io.c

    r329 r331  
    5555
    5656static unsigned int _get_next_event(void);
     57static unsigned int _lowlevel_event(void);
    5758static void _push_event(unsigned int);
    5859static unsigned int _pop_event(void);
     
    6364static unsigned int eventbuf[EVENTBUF_LEN];
    6465static int events = 0;
     66
     67#if !defined(_DOXYGEN_SKIP_ME)
     68/* If no new key was pressed after AUTOREPEAT_THRESHOLD usec, assume the
     69 * key was released */
     70#define AUTOREPEAT_THRESHOLD 200000
     71/* Start repeating key after AUTOREPEAT_TRIGGER usec and send keypress
     72 * events every AUTOREPEAT_RATE usec. */
     73#define AUTOREPEAT_TRIGGER 300000
     74#define AUTOREPEAT_RATE 100000
     75#endif
    6576
    6677/** \brief Get the next mouse or keyboard input event.
     
    105116            return event;
    106117
    107         usleep(1000);
     118#if defined(HAVE_USLEEP)
     119        usleep(10000);
     120#elif defined(HAVE_SLEEP)
     121        Sleep(10);
     122#else
     123        SLEEP
     124#endif
    108125    }
    109126}
     
    114131
    115132static unsigned int _get_next_event(void)
     133{
     134#if defined(USE_SLANG) || defined(USE_NCURSES)
     135    static struct caca_timer key_timer = CACA_TIMER_INITIALIZER;
     136    static unsigned int last_key_ticks = 0;
     137    static unsigned int autorepeat_ticks = 0;
     138    static unsigned int last_key = 0;
     139    unsigned int ticks;
     140#endif
     141    unsigned int event = _lowlevel_event();
     142
     143#if defined(USE_SLANG)
     144    if(_caca_driver != CACA_DRIVER_SLANG)
     145#endif
     146#if defined(USE_NCURSES)
     147    if(_caca_driver != CACA_DRIVER_NCURSES)
     148#endif
     149    return event;
     150
     151#if defined(USE_SLANG) || defined(USE_NCURSES)
     152    /* Simulate long keypresses using autorepeat features */
     153    ticks = _caca_getticks(&key_timer);
     154    last_key_ticks += ticks;
     155    autorepeat_ticks += ticks;
     156
     157    /* Handle autorepeat */
     158    if(last_key && autorepeat_ticks > AUTOREPEAT_TRIGGER
     159                && autorepeat_ticks > AUTOREPEAT_THRESHOLD
     160                && autorepeat_ticks > AUTOREPEAT_RATE)
     161    {
     162        _push_event(event);
     163        autorepeat_ticks -= AUTOREPEAT_RATE;
     164        return CACA_EVENT_KEY_PRESS | last_key;
     165    }
     166
     167    /* We are in autorepeat mode and the same key was just pressed, ignore
     168     * this event and return the next one by calling ourselves. */
     169    if(event == (CACA_EVENT_KEY_PRESS | last_key))
     170    {
     171        last_key_ticks = 0;
     172        return _get_next_event();
     173    }
     174
     175    /* We are in autorepeat mode, but key has expired or a new key was
     176     * pressed - store our event and return a key release event first */
     177    if(last_key && (last_key_ticks > AUTOREPEAT_THRESHOLD
     178                     || (event & CACA_EVENT_KEY_PRESS)))
     179    {
     180        _push_event(event);
     181        event = CACA_EVENT_KEY_RELEASE | last_key;
     182        last_key = 0;
     183        return event;
     184    }
     185
     186    /* A new key was pressed, enter autorepeat mode */
     187    if(event & CACA_EVENT_KEY_PRESS)
     188    {
     189        last_key_ticks = 0;
     190        autorepeat_ticks = 0;
     191        last_key = event & 0x00ffffff;
     192    }
     193
     194    return event;
     195#endif
     196}
     197
     198static unsigned int _lowlevel_event(void)
    116199{
    117200    unsigned int event = _pop_event();
     
    214297        if(intkey < 0x100)
    215298        {
    216             _push_event(CACA_EVENT_KEY_RELEASE | intkey);
    217299            return CACA_EVENT_KEY_PRESS | intkey;
    218300        }
     
    361443        }
    362444
    363         _push_event(CACA_EVENT_KEY_RELEASE | event);
    364445        return CACA_EVENT_KEY_PRESS | event;
    365446    }
     
    378459        if(intkey < 0x100)
    379460        {
    380             _push_event(CACA_EVENT_KEY_RELEASE | intkey);
    381461            return CACA_EVENT_KEY_PRESS | intkey;
    382462        }
     
    413493        }
    414494
    415         _push_event(CACA_EVENT_KEY_RELEASE | event);
    416495        return CACA_EVENT_KEY_PRESS | event;
    417496    }
  • libcaca/trunk/test/event.c

    r330 r331  
    3434{
    3535    int *events;
    36     int i, h;
     36    int i, h, quit;
    3737
    3838    if(caca_init())
     
    4949    memset(events, 0, h * sizeof(int));
    5050
    51     for( ; ; )
     51    for(quit = 0; !quit; )
    5252    {
    5353        unsigned int event = caca_wait_event(CACA_EVENT_ANY);
     
    5656            continue;
    5757
    58         memmove(events + 1, events, (h - 1) * sizeof(int));
    59         events[0] = event;
     58        do
     59        {
     60            /* q quits */
     61            if(event == (CACA_EVENT_KEY_PRESS | 'q'))
     62                quit = 1;
     63
     64            memmove(events + 1, events, (h - 1) * sizeof(int));
     65            events[0] = event;
     66
     67            event = caca_get_event(CACA_EVENT_ANY);
     68        }
     69        while(event);
    6070
    6171        caca_clear();
     
    6474        caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
    6575        caca_draw_line(0, 0, caca_get_width() - 1, 0, ' ');
    66         print_event(0, 0, event);
     76        print_event(0, 0, events[0]);
    6777
    6878        /* Print previous events */
     
    7080        for(i = 1; i < h && events[i]; i++)
    7181            print_event(0, i, events[i]);
    72 
    73         /* q quits */
    74         if(event == (CACA_EVENT_KEY_PRESS | 'q'))
    75             break;
    7682
    7783        caca_refresh();
     
    8692static void print_event(int x, int y, unsigned int event)
    8793{
     94    int character;
     95
    8896    switch(event & 0xff000000)
    8997    {
     
    92100        break;
    93101    case CACA_EVENT_KEY_PRESS:
    94         caca_printf(x, y, "CACA_EVENT_KEY_PRESS 0x%02x",
    95                     event & 0x00ffffff);
     102        character = event & 0x00ffffff;
     103        caca_printf(x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
     104                    (character > 0x20 && character < 0x80) ? character : '?');
    96105        break;
    97106    case CACA_EVENT_KEY_RELEASE:
    98         caca_printf(x, y, "CACA_EVENT_KEY_RELEASE 0x2%x",
    99                     event & 0x00ffffff);
     107        character = event & 0x00ffffff;
     108        caca_printf(x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
     109                    (character > 0x20 && character < 0x80) ? character : '?');
    100110        break;
    101111    case CACA_EVENT_MOUSE_MOTION:
Note: See TracChangeset for help on using the changeset viewer.