+ Check for gettimeofday(). * src/time.c: + Created _caca_time(). + Ported _caca_getticks() to the Win32 API. * src/caca.c: + Properly builds on Win32. * test/event.c: + Added <stdlib.h> because we use malloc().tags/v0.99.beta14
@@ -29,7 +29,7 @@ AC_ARG_ENABLE(x11, | |||
[ --enable-x11 X11 support (autodetected)]) | |||
AC_CHECK_HEADERS(inttypes.h endian.h) | |||
AC_CHECK_FUNCS(vsnprintf getenv putenv strcasecmp usleep Sleep) | |||
AC_CHECK_FUNCS(vsnprintf getenv putenv strcasecmp usleep Sleep gettimeofday) | |||
AC_CHECK_LIB(m, sin, MATH_LIBS="${MATH_LIBS} -lm") | |||
CACA_DRIVERS="" | |||
@@ -48,6 +48,9 @@ | |||
#if defined(USE_X11) | |||
# include <X11/Xlib.h> | |||
#endif | |||
#if defined(USE_WIN32) | |||
# include <windows.h> | |||
#endif | |||
#include <stdlib.h> | |||
#include <string.h> | |||
@@ -156,7 +159,19 @@ int caca_init(void) | |||
else | |||
#endif | |||
#if defined(USE_X11) | |||
/* Nothing to do */ | |||
if(_caca_driver == CACA_DRIVER_X11) | |||
{ | |||
/* Nothing to do */ | |||
} | |||
else | |||
#endif | |||
#if defined(USE_WIN32) | |||
if(_caca_driver == CACA_DRIVER_WIN32) | |||
{ | |||
/* Nothing to do */ | |||
printf("initialising win32 driver\n"); | |||
} | |||
else | |||
#endif | |||
{ | |||
/* Dummy */ | |||
@@ -372,6 +387,13 @@ void caca_end(void) | |||
/* Nothing to do */ | |||
} | |||
else | |||
#endif | |||
#if defined(USE_WIN32) | |||
if(_caca_driver == CACA_DRIVER_WIN32) | |||
{ | |||
/* Nothing to do */ | |||
} | |||
else | |||
#endif | |||
{ | |||
/* Dummy */ | |||
@@ -390,6 +412,11 @@ static void caca_init_driver(void) | |||
/* If the environment variable was set, use it */ | |||
if(var && *var) | |||
{ | |||
#if defined(USE_WIN32) | |||
if(!strcasecmp(var, "win32")) | |||
_caca_driver = CACA_DRIVER_WIN32; | |||
else | |||
#endif | |||
#if defined(USE_CONIO) | |||
if(!strcasecmp(var, "conio")) | |||
_caca_driver = CACA_DRIVER_CONIO; | |||
@@ -416,6 +443,10 @@ static void caca_init_driver(void) | |||
} | |||
#endif | |||
#if defined(USE_WIN32) | |||
_caca_driver = CACA_DRIVER_WIN32; | |||
return; | |||
#endif | |||
#if defined(USE_CONIO) | |||
_caca_driver = CACA_DRIVER_CONIO; | |||
return; | |||
@@ -487,8 +518,10 @@ static void caca_init_features(void) | |||
static void caca_init_terminal(void) | |||
{ | |||
#if defined(HAVE_GETENV) && defined(HAVE_PUTENV) | |||
#if defined(HAVE_GETENV) && defined(HAVE_PUTENV) && \ | |||
(defined(USE_SLANG) || defined(USE_NCURSES)) | |||
char *term, *colorterm, *other; | |||
#endif | |||
#if defined(USE_SLANG) | |||
if(_caca_driver != CACA_DRIVER_SLANG) | |||
@@ -498,6 +531,8 @@ static void caca_init_terminal(void) | |||
#endif | |||
return; | |||
#if defined(HAVE_GETENV) && defined(HAVE_PUTENV) && \ | |||
(defined(USE_SLANG) || defined(USE_NCURSES)) | |||
term = getenv("TERM"); | |||
colorterm = getenv("COLORTERM"); | |||
@@ -44,6 +44,9 @@ enum caca_driver | |||
#endif | |||
#if defined(USE_X11) | |||
CACA_DRIVER_X11 = 4, | |||
#endif | |||
#if defined(USE_WIN32) | |||
CACA_DRIVER_WIN32 = 5, | |||
#endif | |||
CACA_DRIVER_NONE = 0 | |||
}; | |||
@@ -64,6 +67,7 @@ extern int _caca_init_bitmap(void); | |||
extern int _caca_end_bitmap(void); | |||
/* Timer functions */ | |||
extern void _caca_sleep(unsigned int); | |||
extern unsigned int _caca_getticks(struct caca_timer *); | |||
/* Cached screen size */ | |||
@@ -61,7 +61,6 @@ typedef unsigned char uint8_t; | |||
#include <stdio.h> /* BUFSIZ */ | |||
#include <string.h> | |||
#include <stdlib.h> | |||
#include <unistd.h> | |||
#include <stdarg.h> | |||
#include "caca.h" | |||
@@ -935,13 +934,7 @@ void caca_refresh(void) | |||
ticks + IDLE_USEC < (int)_caca_delay; | |||
ticks += _caca_getticks(&timer)) | |||
{ | |||
#if defined(HAVE_USLEEP) | |||
usleep(IDLE_USEC); | |||
#elif defined(HAVE_SLEEP) | |||
Sleep(IDLE_USEC / 1000); | |||
#else | |||
SLEEP | |||
#endif | |||
_caca_sleep(IDLE_USEC); | |||
} | |||
/* Update the sliding mean of the render time */ | |||
@@ -47,8 +47,9 @@ | |||
# include <X11/Xutil.h> | |||
# include <X11/keysym.h> | |||
#endif | |||
#include <unistd.h> | |||
#if defined(USE_WIN32) | |||
# include <windows.h> | |||
#endif | |||
#include "caca.h" | |||
#include "caca_internals.h" | |||
@@ -121,13 +122,7 @@ unsigned int caca_wait_event(unsigned int event_mask) | |||
if(event & event_mask) | |||
return event; | |||
#if defined(HAVE_USLEEP) | |||
usleep(10000); | |||
#elif defined(HAVE_SLEEP) | |||
Sleep(10); | |||
#else | |||
SLEEP | |||
#endif | |||
_caca_sleep(10000); | |||
} | |||
} | |||
@@ -541,6 +536,13 @@ static unsigned int _lowlevel_event(void) | |||
return CACA_EVENT_KEY_PRESS | event; | |||
} | |||
else | |||
#endif | |||
#if defined(USE_WIN32) | |||
if(_caca_driver == CACA_DRIVER_WIN32) | |||
{ | |||
return CACA_EVENT_NONE; | |||
} | |||
else | |||
#endif | |||
{ | |||
/* Dummy */ | |||
@@ -33,32 +33,69 @@ | |||
#include <sys/time.h> | |||
#include <time.h> | |||
#if defined(USE_WIN32) | |||
# include <windows.h> | |||
#endif | |||
#include <unistd.h> | |||
#include "caca.h" | |||
#include "caca_internals.h" | |||
void _caca_sleep(unsigned int usec) | |||
{ | |||
#if defined(HAVE_USLEEP) | |||
usleep(usec); | |||
#elif defined(HAVE_SLEEP) | |||
Sleep(usec / 1000); | |||
#else | |||
SLEEP | |||
#endif | |||
} | |||
unsigned int _caca_getticks(struct caca_timer *timer) | |||
{ | |||
#if defined(HAVE_GETTIMEOFDAY) | |||
struct timeval tv; | |||
#elif defined(USE_WIN32) | |||
static long long int freq = -1LL; | |||
long long int usec; | |||
#endif | |||
unsigned int ticks = 0; | |||
int new_sec, new_usec; | |||
#if defined(HAVE_GETTIMEOFDAY) | |||
gettimeofday(&tv, NULL); | |||
new_sec = tv.tv_sec; | |||
new_usec = tv.tv_usec; | |||
#elif defined(USE_WIN32) | |||
if(freq == -1LL) | |||
{ | |||
if(!QueryPerformanceFrequency((LARGE_INTEGER *)&freq)) | |||
freq = 0; | |||
} | |||
QueryPerformanceCounter((LARGE_INTEGER *)&usec); | |||
new_sec = usec / freq; | |||
new_usec = usec % freq; | |||
#endif | |||
if(timer->last_sec != 0) | |||
{ | |||
/* If the delay was greater than 60 seconds, return 10 seconds | |||
* otherwise we may overflow our ticks counter. */ | |||
if(tv.tv_sec >= timer->last_sec + 60) | |||
if(new_sec >= timer->last_sec + 60) | |||
ticks = 60 * 1000000; | |||
else | |||
{ | |||
ticks = (tv.tv_sec - timer->last_sec) * 1000000; | |||
ticks += tv.tv_usec; | |||
ticks = (new_sec - timer->last_sec) * 1000000; | |||
ticks += new_usec; | |||
ticks -= timer->last_usec; | |||
} | |||
} | |||
timer->last_sec = tv.tv_sec; | |||
timer->last_usec = tv.tv_usec; | |||
timer->last_sec = new_sec; | |||
timer->last_usec = new_usec; | |||
return ticks; | |||
} | |||
@@ -25,6 +25,7 @@ | |||
#include <stdio.h> | |||
#include <string.h> | |||
#include <stdlib.h> | |||
#include "caca.h" | |||