From 0017297a2437c5b334c979c9691bafc06389664f Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 3 Jul 2006 23:52:53 +0200 Subject: Initial import --- hdaps-tux.c | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 hdaps-tux.c (limited to 'hdaps-tux.c') diff --git a/hdaps-tux.c b/hdaps-tux.c new file mode 100644 index 0000000..5ca4751 --- /dev/null +++ b/hdaps-tux.c @@ -0,0 +1,172 @@ +/** + * hdaps_tux - Move tux around by using your IBM Thinkpad + * + * Copyright (c) 2006 Tobias Klauser + * + * This code is based on gtollina.c from [1] and the SDL example from [2] + * + * [1] http://www.gnome.org/~fherrera/gtollina.c + * [2] http://de.wikibooks.org/wiki/SDL + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include +#include +#include +#include +#include + +#include "hdaps-tux.h" + +#define BUF_SIZ 256 + +int get_pos(int *xpos, int *ypos) +{ + int fd; + size_t ret; + char buf[BUF_SIZ]; + + memset(&buf, 0, BUF_SIZ); + + /* Check wheter we have hdaps available */ + fd = open(POSITION_FILE, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "Could not open hdaps position file.\n"); + exit(1); + } + + ret = read(fd, &buf, BUF_SIZ); + close(fd); + if (ret < 0) + return -1; + + sscanf(buf, "(%d,%d)", xpos, ypos); + dprintf("(%d,%d)\n", *xpos, *ypos); + + return 0; +} + +int main(int argc, char *argv[]) +{ + SDL_Surface *screen, *image; + SDL_Rect dst, dstwhite; + SDL_Event event; + Uint8 *keys; + int tuxX = 270, tuxY = 170; + int hdapsX = 0, hdapsY = 0; + int hdapsXprev = 0, hdapsYprev = 0; + unsigned int timeout = TIMEOUT_VAL; + char done = 0; + + /* Initialize SDL */ + if (SDL_Init(SDL_INIT_VIDEO) == -1) { + fprintf(stderr, "Can't init SDL: %s\n", SDL_GetError()); + exit(1); + } + atexit(SDL_Quit); + + SDL_WM_SetCaption ("hdaps_tux - Move tux with your Thinkpad (press q to exit)", "hdaps_tux"); + + screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF); + SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255)); + if (!screen) { + printf("Can't set video mode: %s\n", SDL_GetError()); + exit(1); + } + + image = SDL_LoadBMP(TUX_FILE); + if (!image) { + printf("Can't load '%s': %s\n", TUX_FILE, SDL_GetError()); + exit(1); + } + + dst.w = image->w; + dst.h = image->h; + dstwhite.w = image->w + 1; + dstwhite.h = image->h + 1; + SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(image->format, 255, 255, 255)); + + get_pos(&hdapsX, &hdapsY); + + while (!done) { + while (SDL_PollEvent(&event)) { + switch(event.type) { + case SDL_QUIT: + done = 1; + break; + } + } + + dstwhite.x = tuxX; + dstwhite.y = tuxY; + + /* Hack! */ + if (--timeout == 0) { + int xdiff = 0, ydiff = 0; + + hdapsXprev = hdapsX; + hdapsYprev = hdapsY; + get_pos(&hdapsX, &hdapsY); + xdiff = hdapsX - hdapsXprev; + ydiff = hdapsY - hdapsYprev; + + dprintf("xdiff/ydiff: %d/%d\n", xdiff, ydiff); + + tuxX += xdiff * 3; + tuxY += ydiff * 3; + + timeout = TIMEOUT_VAL; + } + + keys = SDL_GetKeyState(NULL); +/* if (keys[SDLK_UP]) + if (tuxY > 5) + tuxY--; + if (keys[SDLK_DOWN]) + if (tuxY < 475 - image->h) + tuxY++; + if (keys[SDLK_RIGHT]) + if (tuxX < 635 - image->w) + tuxX++; + if (keys[SDLK_LEFT]) + if (tuxX > 5) + tuxX--; +*/ + if (keys[SDLK_q]) + done = 1; + + if (tuxX < 5) + tuxX = 5; + if (tuxX > 635 - image->w) + tuxX = 635 - image->w; + if (tuxY < 5) + tuxY = 5; + if (tuxY > 475 - image->h) + tuxY = 475 - image->h; + + dprintf("tuxX/tuxY: %d/%d\n", tuxX, tuxY); + + dst.x = tuxX; + dst.y = tuxY; + SDL_FillRect(screen, &dstwhite, SDL_MapRGB(screen->format, 255, 255, 255)); + SDL_BlitSurface(image, NULL, screen, &dst); + SDL_Flip(screen); + } + + SDL_FreeSurface(image); + + return 0; +} + -- cgit v1.2.3-54-g00ecf