Changeset 3601


Ignore:
Timestamp:
08/02/09 13:09:05 (4 years ago)
Author:
sam
Message:

Fix <conio.h> delay(), getch() and kbhit() to avoid busy loops and ensure
frequent screen refreshes.
Implement <conio.h> clreol() (incomplete) and <dos.h> sleep() functions.
Replace some defines with enums in <caca_conio.h>.

Location:
libcaca/trunk/caca
Files:
2 edited

Legend:

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

    r3596 r3601  
    3232static caca_display_t *dp; 
    3333 
     34static caca_timer_t refresh_timer = {0, 0}; 
     35static uint64_t refresh_ticks; 
     36 
    3437static int unget_ch = -1; 
    3538static int kbhit_ch = -1; 
     
    4144static void conio_fini(void); 
    4245 
     46int caca_conio_directvideo; 
     47int caca_conio__wscroll; 
     48 
    4349/** \brief DOS conio.h cgets() equivalent */ 
    4450char * caca_conio_cgets(char *str) 
     
    5763    conio_init(); 
    5864 
    59     /* TODO: implement this function */ 
     65    /* FIXME: must work within the currently active text window */ 
     66    caca_fill_box(cv, caca_wherex(cv), caca_wherey(cv), 
     67                  caca_get_width(cv), caca_wherey(cv), ' '); 
     68 
     69    conio_refresh(); 
    6070} 
    6171 
     
    110120} 
    111121 
    112 /** \brief DOS conio.h delay() equivalent */ 
    113 void caca_conio_delay(int i) 
    114 { 
    115     conio_init(); 
    116  
    117     _caca_sleep(i * 1000); 
     122/** \brief DOS dos.h delay() equivalent */ 
     123void caca_conio_delay(unsigned int milliseconds) 
     124{ 
     125    int64_t usec = (int64_t)milliseconds * 1000; 
     126    caca_timer_t timer = {0, 0}; 
     127 
     128    conio_init(); 
     129 
     130    _caca_getticks(&timer); 
     131 
     132    /* Refresh screen as long as we have enough time */ 
     133    while(usec > 5000) 
     134    { 
     135        conio_refresh(); 
     136        _caca_sleep(5000); 
     137        usec -= _caca_getticks(&timer); 
     138    } 
     139 
     140    if(usec > 0) 
     141        _caca_sleep(usec); 
     142 
     143    conio_refresh(); 
    118144} 
    119145 
     
    130156{ 
    131157    caca_event_t ev; 
     158    int ret; 
    132159 
    133160    conio_init(); 
     
    147174    } 
    148175 
    149     caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1); 
    150     return caca_get_event_key_ch(&ev); 
     176    while(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 1000) == 0) 
     177        conio_refresh(); 
     178 
     179    ret = caca_get_event_key_ch(&ev); 
     180 
     181    conio_refresh(); 
     182 
     183    return ret; 
    151184} 
    152185 
     
    220253int caca_conio_kbhit(void) 
    221254{ 
     255    static caca_timer_t timer = {0, 0}; 
     256    static int last_failed = 0; 
    222257    caca_event_t ev; 
    223258 
    224259    conio_init(); 
     260 
     261    /* If last call failed and this call is made less than 100µs 
     262     * afterwards, we assume the caller is in a busy loop and we 
     263     * delay it slightly to avoid resource leakage. */ 
     264    if(last_failed && _caca_getticks(&timer) < 100) 
     265    { 
     266        _caca_sleep(1000); 
     267        conio_refresh(); 
     268    } 
     269 
     270    last_failed = 0; 
    225271 
    226272    if(kbhit_ch >= 0) 
     
    232278        return 1; 
    233279    } 
     280 
     281    last_failed = 1; 
    234282 
    235283    return 0; 
     
    263311} 
    264312 
    265 /** \brief DOS conio.h nosound() equivalent */ 
     313/** \brief DOS dos.h nosound() equivalent */ 
    266314void caca_conio_nosound(void) 
    267315{ 
     
    318366} 
    319367 
    320 /** \brief DOS conio.h sound() equivalent */ 
    321 void caca_conio_sound(int i) 
     368/** \brief DOS dos.h sleep() equivalent */ 
     369void caca_conio_sleep(unsigned int seconds) 
     370{ 
     371    int64_t usec = (int64_t)seconds * 1000000; 
     372    caca_timer_t timer = {0, 0}; 
     373 
     374    conio_init(); 
     375 
     376    _caca_getticks(&timer); 
     377 
     378    /* Refresh screen as long as we have enough time */ 
     379    while(usec > 5000) 
     380    { 
     381        conio_refresh(); 
     382        _caca_sleep(5000); 
     383        usec -= _caca_getticks(&timer); 
     384    } 
     385 
     386    if(usec > 0) 
     387        _caca_sleep(usec); 
     388 
     389    conio_refresh(); 
     390} 
     391 
     392/** \brief DOS dos.h sound() equivalent */ 
     393void caca_conio_sound(unsigned int frequency) 
    322394{ 
    323395    conio_init(); 
     
    406478    { 
    407479        dp = caca_create_display(cv); 
     480        caca_refresh_display(dp); 
    408481        caca_set_cursor(dp, 1); 
     482        _caca_getticks(&refresh_timer); 
     483        refresh_ticks = 0; 
    409484#if defined HAVE_ATEXIT 
    410485        atexit(conio_fini); 
     
    415490static void conio_refresh(void) 
    416491{ 
    417     caca_refresh_display(dp); 
     492    refresh_ticks += _caca_getticks(&refresh_timer); 
     493    if(refresh_ticks > 10000) 
     494    { 
     495        refresh_ticks -= 10000; 
     496        caca_refresh_display(dp); 
     497    } 
    418498} 
    419499 
  • libcaca/trunk/caca/caca_conio.h

    r3592 r3601  
    3939#endif 
    4040 
    41 /* conio.h defines and global variables */ 
    42 #define CACA_CONIO_BLINK 128 
    43 #define CACA_CONIO_BLACK 0 
    44 #define CACA_CONIO_BLUE 1 
    45 #define CACA_CONIO_GREEN 2 
    46 #define CACA_CONIO_CYAN 3 
    47 #define CACA_CONIO_RED 4 
    48 #define CACA_CONIO_MAGENTA 5 
    49 #define CACA_CONIO_BROWN 6 
    50 #define CACA_CONIO_LIGHTGRAY 7 
    51 #define CACA_CONIO_DARKGRAY 8 
    52 #define CACA_CONIO_LIGHTBLUE 9 
    53 #define CACA_CONIO_LIGHTGREEN 10 
    54 #define CACA_CONIO_LIGHTCYAN 11 
    55 #define CACA_CONIO_LIGHTRED 12 
    56 #define CACA_CONIO_LIGHTMAGENTA 13 
    57 #define CACA_CONIO_YELLOW 14 
    58 #define CACA_CONIO_WHITE 15 
     41/* conio.h enums and global variables */ 
     42enum CACA_CONIO_COLORS 
     43{ 
     44    CACA_CONIO_BLINK = 128, 
     45    CACA_CONIO_BLACK = 0, 
     46    CACA_CONIO_BLUE = 1, 
     47    CACA_CONIO_GREEN = 2, 
     48    CACA_CONIO_CYAN = 3, 
     49    CACA_CONIO_RED = 4, 
     50    CACA_CONIO_MAGENTA = 5, 
     51    CACA_CONIO_BROWN = 6, 
     52    CACA_CONIO_LIGHTGRAY = 7, 
     53    CACA_CONIO_DARKGRAY = 8, 
     54    CACA_CONIO_LIGHTBLUE = 9, 
     55    CACA_CONIO_LIGHTGREEN = 10, 
     56    CACA_CONIO_LIGHTCYAN = 11, 
     57    CACA_CONIO_LIGHTRED = 12, 
     58    CACA_CONIO_LIGHTMAGENTA = 13, 
     59    CACA_CONIO_YELLOW = 14, 
     60    CACA_CONIO_WHITE = 15, 
     61}; 
    5962__extern int caca_conio_directvideo; 
    60 #define CACA_CONIO__NOCURSOR 0 
    61 #define CACA_CONIO__SOLIDCURSOR 1 
    62 #define CACA_CONIO__NORMALCURSOR 2 
     63enum CACA_CONIO_CURSOR 
     64{ 
     65    CACA_CONIO__NOCURSOR = 0, 
     66    CACA_CONIO__SOLIDCURSOR = 1, 
     67    CACA_CONIO__NORMALCURSOR = 2, 
     68}; 
    6369struct caca_conio_text_info 
    6470{ 
     
    7682    unsigned char cury;           /* y-coordinate in current window */ 
    7783}; 
    78 #define CACA_CONIO_LASTMODE -1 
    79 #define CACA_CONIO_BW40 0 
    80 #define CACA_CONIO_C40 1 
    81 #define CACA_CONIO_BW80 2 
    82 #define CACA_CONIO_C80 3 
    83 #define CACA_CONIO_MONO 7 
    84 #define CACA_CONIO_C4350 64 
     84enum CACA_CONIO_MODE 
     85{ 
     86    CACA_CONIO_LASTMODE = -1, 
     87    CACA_CONIO_BW40 = 0, 
     88    CACA_CONIO_C40 = 1, 
     89    CACA_CONIO_BW80 = 2, 
     90    CACA_CONIO_C80 = 3, 
     91    CACA_CONIO_MONO = 7, 
     92    CACA_CONIO_C4350 = 64, 
     93}; 
    8594__extern int caca_conio__wscroll; 
    8695 
     
    155164__extern int    caca_conio_cputs(const char *str); 
    156165__extern int    caca_conio_cscanf(char *format, ...); 
    157 __extern void   caca_conio_delay(int); 
     166__extern void   caca_conio_delay(unsigned int); 
    158167__extern void   caca_conio_delline(void); 
    159168__extern int    caca_conio_getch(void); 
     
    177186                                   void *destin); 
    178187__extern void   caca_conio__setcursortype(int cur_t); 
    179 __extern void   caca_conio_sound(int); 
     188__extern void   caca_conio_sleep(unsigned int); 
     189__extern void   caca_conio_sound(unsigned int); 
    180190__extern void   caca_conio_textattr(int newattr); 
    181191__extern void   caca_conio_textbackground(int newcolor); 
     
    238248#   undef _setcursortype 
    239249#   define _setcursortype caca_conio__setcursortype 
     250#   undef sleep 
     251#   define sleep caca_conio_sleep 
    240252#   undef sound 
    241253#   define sound caca_conio_sound 
Note: See TracChangeset for help on using the changeset viewer.