comparison touchTest/touchPong.c @ 4639:f5cd4b6231ba

Added Test Directory. Touch input works in touchSimp for wacom bamboo fun on Ubuntu linux. Not yet integrated into library. Should support other touch devices and other linux distros, but not tested on anything else.
author Jim Grandpre <jim.tla@gmail.com>
date Mon, 24 May 2010 23:44:24 -0400
parents
children
comparison
equal deleted inserted replaced
4464:fa77a6429698 4639:f5cd4b6231ba
1 #include <stdio.h>
2 #include <SDL.h>
3 #include <math.h>
4 #include <linux/input.h>
5 #include <fcntl.h>
6
7
8 #define PI 3.1415926535897
9 #define WIDTH 640
10 #define HEIGHT 480
11 #define BPP 4
12 #define DEPTH 32
13
14
15 #define MAX_FINGERS 2
16
17 int mousx,mousy;
18 int keystat[512];
19 int bstatus;
20
21
22
23 typedef struct {
24 int x,y;
25 } Point;
26
27 Point finger[MAX_FINGERS];
28
29 //Pong Code
30 Point ball,ballv;
31
32
33 void handler (int sig)
34 {
35 printf ("\nexiting...(%d)\n", sig);
36 exit (0);
37 }
38
39 void perror_exit (char *error)
40 {
41 perror (error);
42 handler (9);
43 }
44
45
46 void setpix(SDL_Surface *screen, int x, int y, int col)
47 {
48 Uint32 *pixmem32;
49 Uint32 colour;
50
51 if((unsigned)x > screen->w) return;
52 if((unsigned)y > screen->h) return;
53
54 colour = SDL_MapRGB( screen->format, (col>>16)&0xFF, (col>>8)&0xFF, col&0xFF);
55
56 pixmem32 = (Uint32*) screen->pixels + y*screen->pitch/BPP + x;
57 *pixmem32 = colour;
58 }
59
60 void drawCircle(SDL_Surface* screen,int x,int y,int r,int c)
61 {
62
63 float a;
64 for(a=0;a<2*PI;a+=1.f/(float)r)
65 {
66 setpix(screen,(int)(x+r*cos(a)),(int)(y+r*sin(a)),c);
67 }
68 }
69
70 void DrawScreen(SDL_Surface* screen, int h)
71 {
72 int x, y, xm,ym,c;
73 if(SDL_MUSTLOCK(screen))
74 {
75 if(SDL_LockSurface(screen) < 0) return;
76 }
77 for(y = 0; y < screen->h; y++ )
78 {
79 for( x = 0; x < screen->w; x++ )
80 {
81 //setpixel(screen, x, y, (x*x)/256+3*y+h, (y*y)/256+x+h, h);
82 //xm = (x+h)%screen->w;
83 //ym = (y+h)%screen->w;
84 //c = sin(h/256*2*PI)*x*y/screen->w/screen->h;
85 //setpix(screen,x,y,255*sin(xm/screen->w*2*PI),sin(h/255*2*PI)*255*y/screen->h,c);
86 setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
87 }
88 }
89 drawCircle(screen,mousx,mousy,30,0xFFFFFF);
90 int i;
91 for(i=0;i<MAX_FINGERS;i++)
92 drawCircle(screen,finger[i].x,finger[i].y,30,0xFFFFFF);
93
94 drawCircle(screen,ball.x,ball.y,30,0xFF00FF);
95
96 if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
97
98 SDL_Flip(screen);
99 }
100
101 SDL_Surface* initScreen(int width,int height)
102 {
103 return SDL_SetVideoMode(width, height, DEPTH,
104 SDL_HWSURFACE | SDL_RESIZABLE);
105 }
106
107 int main(int argc, char* argv[])
108 {
109 struct input_event ev[64];
110 int fd, rd, value, size = sizeof (struct input_event);
111 char name[256] = "Unknown";
112 char *device = NULL;
113
114
115
116 //Setup check
117 if (argv[1] == NULL){
118 printf("Please specify (on the command line) the path to the dev event interface device\n");
119 exit (0);
120 }
121
122 if ((getuid ()) != 0)
123 printf ("You are not root! This may not work...\n");
124
125 if (argc > 1)
126 device = argv[1];
127
128 //Open Device
129 if ((fd = open (device, O_RDONLY)) == -1)
130 printf ("%s is not a vaild device.\n", device);
131
132 //Print Device Name
133 ioctl (fd, EVIOCGNAME (sizeof (name)), name);
134 printf ("Reading From : %s (%s)\n", device, name);
135
136
137
138
139
140 SDL_Surface *screen;
141 SDL_Event event;
142
143 int keypress = 0;
144 int h=0,s=1,i,j;
145 int tx,ty,curf=0;
146
147
148
149 memset(keystat,0,512*sizeof(keystat[0]));
150 if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
151
152 if (!(screen = initScreen(WIDTH,HEIGHT)))
153 {
154 SDL_Quit();
155 return 1;
156 }
157 ball.x = screen->w/2;
158 ball.y = screen->h/2;
159 ballv.x = -3;
160 ballv.y = 1;
161 while(!keystat[27])
162 {
163 //Poll SDL
164 while(SDL_PollEvent(&event))
165 {
166 switch (event.type)
167 {
168 case SDL_QUIT:
169 keystat[27] = 1;
170 break;
171 case SDL_KEYDOWN:
172 //printf("%i\n",event.key.keysym.sym);
173 keystat[event.key.keysym.sym] = 1;
174 //keypress = 1;
175 break;
176 case SDL_KEYUP:
177 //printf("%i\n",event.key.keysym.sym);
178 keystat[event.key.keysym.sym] = 0;
179 //keypress = 1;
180 break;
181 case SDL_VIDEORESIZE:
182 if (!(screen = initScreen(event.resize.w,
183 event.resize.h)))
184 {
185 SDL_Quit();
186 return 1;
187 }
188 break;
189 case SDL_MOUSEMOTION:
190 mousx = event.motion.x;
191 mousy = event.motion.y;
192 break;
193 case SDL_MOUSEBUTTONDOWN:
194 bstatus |= (1<<(event.button.button-1));
195 break;
196 case SDL_MOUSEBUTTONUP:
197 bstatus &= ~(1<<(event.button.button-1));
198 break;
199 }
200 }
201
202 //poll for Touch <- Goal: make this a case:
203 if ((rd = read (fd, ev, size * 64)) < size)
204 perror_exit ("read()");
205 //printf("time: %i\n type: %X\n code: %X\n value: %i\n ",
206 // ev->time,ev->type,ev->code,ev->value);
207 for (i = 0; i < rd / sizeof(struct input_event); i++)
208 switch (ev[i].type)
209 {
210 case EV_ABS:
211 if(ev[i].code == ABS_X)
212 tx = ev[i].value;
213 else if (ev[i].code == ABS_Y)
214 ty = ev[i].value;
215 else if (ev[i].code == ABS_MISC)
216 {
217 //printf("Misc:type: %X\n code: %X\n value: %i\n ",
218 // ev[i].type,ev[i].code,ev[i].value);
219 }
220 break;
221 case EV_MSC:
222 if(ev[i].code == MSC_SERIAL)
223 curf = ev[i].value;
224 break;
225 case EV_SYN:
226 curf -= 1;
227 if(tx >= 0)
228 finger[curf].x = tx;
229 if(ty >= 0)
230 finger[curf].y = ty;
231
232 //printf("Synched: %i tx: %i, ty: %i\n",curf,finger[curf].x,finger[curf].y);
233 tx = -1;
234 ty = -1;
235 break;
236
237 }
238
239 //Pong code
240 ball.x += ballv.x;
241 ball.y += ballv.y;
242 for(i=0;i<MAX_FINGERS;i++)
243 {
244
245 if(finger[i].x > 0 || finger[i].y > 0)
246 {
247 int d2 = (finger[i].x-ball.x)*(finger[i].x-ball.x) +
248 (finger[i].y-ball.y)*(finger[i].y-ball.y);
249 ballv.x += (50*(ball.x-finger[i].x))/d2;
250 ballv.y += (30*(ball.y-finger[i].y))/d2;
251 }
252 }
253 if((unsigned int)ball.x > screen->w){
254 ball.x = screen->w/2;
255 ball.y = screen->h/2;
256 ballv.x = -3;
257 ballv.y = 1;
258 }
259 if((unsigned int)ball.y > screen->h) ballv.y *= -1;
260 printf("(%i,%i)\n",ball.x,ball.y);
261 //And draw
262 DrawScreen(screen,h);
263 /*
264 for(i=0;i<512;i++)
265 if(keystat[i]) printf("%i\n",i);
266 printf("Buttons:%i\n",bstatus);
267 */
268 }
269 SDL_Quit();
270
271 return 0;
272 }