--- SDL-1.2.11/src/events/SDL_events.c 2006-05-07 05:25:24.000000000 +0200 +++ SDL-1.2.11.wii/src/events/SDL_events.c 2007-03-23 19:37:36.000000000 +0100 @@ -32,6 +32,10 @@ #include "../joystick/SDL_joystick_c.h" #endif +#include +#include +#include + /* Public data -- the event filter */ SDL_EventFilter SDL_EventOK = NULL; Uint8 SDL_ProcessEvents[SDL_NUMEVENTS]; @@ -386,10 +390,75 @@ } } +#define POSITION_FILE "/sys/devices/platform/applesmc/position" +#define CALIBRATION_FILE "/sys/devices/platform/applesmc/calibrate" +#define BUF_LEN 32 +#define DELAY 500 + +#define MOVE_MOUSE(x, y) \ + SDL_PrivateMouseMotion(0, 0, x, y); + +struct accelerometer_data { + int x, y, z; +}; + +static void read_accelerometer_data(struct accelerometer_data *d) +{ + char buf[BUF_LEN]; + int fd; + + fd = open(POSITION_FILE, O_RDONLY); + read(fd, buf, BUF_LEN); + sscanf(buf, "(%d,%d,%d)\n", &(d->x), &(d->y), &(d->z)); + close(fd); +} + /* Public functions */ int SDL_PollEvent (SDL_Event *event) { + static int acc_initialized = 0; + static int now = 0; + static signed short centerX, centerY; + static struct accelerometer_data data; + + struct timeval tv; + struct timezone tz; + int later = 0; + + if(!acc_initialized) { + char buf[BUF_LEN]; + int fd; + + fd = open(CALIBRATION_FILE, O_RDONLY); + read(fd, buf, BUF_LEN); + sscanf(buf, "(%d,%d)\n", ¢erX, ¢erY); + close(fd); + + acc_initialized = 1; + + gettimeofday(&tv, &tz); + now = tv.tv_usec; + + read_accelerometer_data(&data); + } + + gettimeofday(&tv, &tz); + later = tv.tv_usec; + + if((later - now > DELAY) || (now - later > DELAY)) { + read_accelerometer_data(&data); + now = later; + } + + int x = -(data.x - centerX); + int y = data.y - centerY; + + Sint16 x_real = ((x*(SDL_VideoSurface->w))>>9) + ((SDL_VideoSurface->w)>>1) ; + Sint16 y_real = ((y*(SDL_VideoSurface->h))>>9) + ((SDL_VideoSurface->h)>>1) ; + + MOVE_MOUSE(x_real, y_real); + SDL_PumpEvents(); /* We can't return -1, just return 0 (no event) on error */