diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 37 | ||||
-rw-r--r-- | hdaps-tux.c | 172 | ||||
-rw-r--r-- | hdaps-tux.h | 15 | ||||
-rw-r--r-- | tux.bmp | bin | 0 -> 36054 bytes |
5 files changed, 226 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8114b97 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +hdaps-tux diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0848fb5 --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +VERSION = 0.1 + +# Paths +prefix = $(HOME) +BINDIR = $(prefix)/bin +DESTDIR = + +CC := gcc +INSTALL := install + +INCLUDES := -I /usr/include/SDL -I /usr/include/X11 +LIBS := -lSDL + +CFLAGS := -g -Wall $(INCLUDES) -D_USE_SOURCE +LDFLAGS := $(LIBS) + +DEBUG = false + +ifeq ($(strip $(DEBUG)),true) + CFLAGS += -DDEBUG +endif + +PROGRAM := hdaps-tux + +all: $(PROGRAM) + +$(PROGRAM): $(PROGRAM).o $(PROGRAM).h + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +install: $(PROGRAM) + $(INSTALL) -m 775 $(PROGRAM) $(DESTDIR)$(BINDIR) + +clean: + rm -f *.o + rm -f $(PROGRAM) 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 <tklauser@distanz.ch> + * + * 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 <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <SDL.h> + +#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; +} + diff --git a/hdaps-tux.h b/hdaps-tux.h new file mode 100644 index 0000000..258e398 --- /dev/null +++ b/hdaps-tux.h @@ -0,0 +1,15 @@ +#ifndef _HDAPS_TUX_H_ +#define _HDAPS_TUX_H_ + +#define POSITION_FILE "/sys/devices/platform/hdaps/position" +#define TUX_FILE "tux.bmp" + +#define TIMEOUT_VAL 15 + +#ifdef DEBUG +#define dprintf(fmt, args...) fprintf(stderr, fmt, ##args) +#else +#define dprintf(fmt, args...) +#endif /* DEBUG */ + +#endif /* _HDAPS_TUX_H_ */ Binary files differ |