summaryrefslogtreecommitdiff
path: root/hdaps-tux.c
blob: 28dac2ec85a8b2550adaf28ef20e1d0c0f5b5e52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
 * 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);

	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 = 0, tuxY = 0;
	int hdapsX = 0, hdapsY = 0;
	int hdapsXprev = 0, hdapsYprev = 0;
	int vel = 4;
	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(W_WIDTH, W_HEIGHT, 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));

	/* Calculate and set initial position of tux (centre of the window) */
	tuxX = (W_WIDTH / 2) - (image->w / 2);
	tuxY = (W_HEIGHT / 2) - (image->h / 2);

	/* Get the hdaps position so we have something to start from */
	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);

			if (abs(xdiff) > 2)
				tuxX += xdiff * vel;
			if (abs(ydiff) > 2)
				tuxY += ydiff * vel;

			timeout = TIMEOUT_VAL;
		}

		keys = SDL_GetKeyState(NULL);
		if (keys[SDLK_UP])	/* Manually move tux up */
			if (tuxY > 5)
				tuxY--;
		if (keys[SDLK_DOWN])	/* Manually move tux down */
			if (tuxY < W_HEIGHT - 5 - image->h)
				tuxY++;
		if (keys[SDLK_RIGHT])	/* Manually move tux right */
			if (tuxX < W_WIDTH - 5 - image->w)
				tuxX++;
		if (keys[SDLK_LEFT])	/* Manually move tux left */
			if (tuxX > 5)
				tuxX--;
		if (keys[SDLK_q])	/* Quit */
			done = 1;

		if (tuxX < 5)
			tuxX = 5;
		if (tuxX > W_WIDTH - image->w)
			tuxX = W_WIDTH - image->w;
		if (tuxY < 5)
			tuxY = 5;
		if (tuxY > W_HEIGHT - image->h)
			tuxY = W_HEIGHT - 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;
}