Mercurial > sdl-ios-xcode
comparison touchTest/touchSimp.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 void handler (int sig) | |
30 { | |
31 printf ("\nexiting...(%d)\n", sig); | |
32 exit (0); | |
33 } | |
34 | |
35 void perror_exit (char *error) | |
36 { | |
37 perror (error); | |
38 handler (9); | |
39 } | |
40 | |
41 | |
42 void setpix(SDL_Surface *screen, int x, int y, int col) | |
43 { | |
44 Uint32 *pixmem32; | |
45 Uint32 colour; | |
46 | |
47 if((unsigned)x > screen->w) return; | |
48 if((unsigned)y > screen->h) return; | |
49 | |
50 colour = SDL_MapRGB( screen->format, (col>>16)&0xFF, (col>>8)&0xFF, col&0xFF); | |
51 | |
52 pixmem32 = (Uint32*) screen->pixels + y*screen->pitch/BPP + x; | |
53 *pixmem32 = colour; | |
54 } | |
55 | |
56 void drawCircle(SDL_Surface* screen,int x,int y,int r,int c) | |
57 { | |
58 | |
59 float a; | |
60 for(a=0;a<2*PI;a+=1.f/(float)r) | |
61 { | |
62 setpix(screen,(int)(x+r*cos(a)),(int)(y+r*sin(a)),c); | |
63 } | |
64 } | |
65 | |
66 void DrawScreen(SDL_Surface* screen, int h) | |
67 { | |
68 int x, y, xm,ym,c; | |
69 if(SDL_MUSTLOCK(screen)) | |
70 { | |
71 if(SDL_LockSurface(screen) < 0) return; | |
72 } | |
73 for(y = 0; y < screen->h; y++ ) | |
74 { | |
75 for( x = 0; x < screen->w; x++ ) | |
76 { | |
77 //setpixel(screen, x, y, (x*x)/256+3*y+h, (y*y)/256+x+h, h); | |
78 //xm = (x+h)%screen->w; | |
79 //ym = (y+h)%screen->w; | |
80 //c = sin(h/256*2*PI)*x*y/screen->w/screen->h; | |
81 //setpix(screen,x,y,255*sin(xm/screen->w*2*PI),sin(h/255*2*PI)*255*y/screen->h,c); | |
82 setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255); | |
83 } | |
84 } | |
85 drawCircle(screen,mousx,mousy,30,0xFFFFFF); | |
86 int i; | |
87 for(i=0;i<MAX_FINGERS;i++) | |
88 drawCircle(screen,finger[i].x,finger[i].y,30,0xFFFFFF); | |
89 | |
90 if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); | |
91 | |
92 SDL_Flip(screen); | |
93 } | |
94 | |
95 SDL_Surface* initScreen(int width,int height) | |
96 { | |
97 return SDL_SetVideoMode(width, height, DEPTH, | |
98 SDL_HWSURFACE | SDL_RESIZABLE); | |
99 } | |
100 | |
101 int main(int argc, char* argv[]) | |
102 { | |
103 struct input_event ev[64]; | |
104 int fd, rd, value, size = sizeof (struct input_event); | |
105 char name[256] = "Unknown"; | |
106 char *device = NULL; | |
107 | |
108 | |
109 | |
110 //Setup check | |
111 if (argv[1] == NULL){ | |
112 printf("Please specify (on the command line) the path to the dev event interface device\n"); | |
113 exit (0); | |
114 } | |
115 | |
116 if ((getuid ()) != 0) | |
117 printf ("You are not root! This may not work...\n"); | |
118 | |
119 if (argc > 1) | |
120 device = argv[1]; | |
121 | |
122 //Open Device | |
123 if ((fd = open (device, O_RDONLY)) == -1) | |
124 printf ("%s is not a vaild device.\n", device); | |
125 | |
126 //Print Device Name | |
127 ioctl (fd, EVIOCGNAME (sizeof (name)), name); | |
128 printf ("Reading From : %s (%s)\n", device, name); | |
129 | |
130 | |
131 | |
132 | |
133 | |
134 SDL_Surface *screen; | |
135 SDL_Event event; | |
136 | |
137 int keypress = 0; | |
138 int h=0,s=1,i,j; | |
139 int tx,ty,curf=0; | |
140 | |
141 Point ball,ballv; | |
142 | |
143 memset(keystat,0,512*sizeof(keystat[0])); | |
144 if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1; | |
145 | |
146 if (!(screen = initScreen(WIDTH,HEIGHT))) | |
147 { | |
148 SDL_Quit(); | |
149 return 1; | |
150 } | |
151 ball.x = screen->width()/2; | |
152 ball.y = screen->height()/2; | |
153 while(!keystat[27]) | |
154 { | |
155 //Poll SDL | |
156 while(SDL_PollEvent(&event)) | |
157 { | |
158 switch (event.type) | |
159 { | |
160 case SDL_QUIT: | |
161 keystat[27] = 1; | |
162 break; | |
163 case SDL_KEYDOWN: | |
164 //printf("%i\n",event.key.keysym.sym); | |
165 keystat[event.key.keysym.sym] = 1; | |
166 //keypress = 1; | |
167 break; | |
168 case SDL_KEYUP: | |
169 //printf("%i\n",event.key.keysym.sym); | |
170 keystat[event.key.keysym.sym] = 0; | |
171 //keypress = 1; | |
172 break; | |
173 case SDL_VIDEORESIZE: | |
174 if (!(screen = initScreen(event.resize.w, | |
175 event.resize.h))) | |
176 { | |
177 SDL_Quit(); | |
178 return 1; | |
179 } | |
180 break; | |
181 case SDL_MOUSEMOTION: | |
182 mousx = event.motion.x; | |
183 mousy = event.motion.y; | |
184 break; | |
185 case SDL_MOUSEBUTTONDOWN: | |
186 bstatus |= (1<<(event.button.button-1)); | |
187 break; | |
188 case SDL_MOUSEBUTTONUP: | |
189 bstatus &= ~(1<<(event.button.button-1)); | |
190 break; | |
191 } | |
192 } | |
193 | |
194 //poll for Touch <- Goal: make this a case: | |
195 if ((rd = read (fd, ev, size * 64)) < size) | |
196 perror_exit ("read()"); | |
197 //printf("time: %i\n type: %X\n code: %X\n value: %i\n ", | |
198 // ev->time,ev->type,ev->code,ev->value); | |
199 for (i = 0; i < rd / sizeof(struct input_event); i++) | |
200 switch (ev[i].type) | |
201 { | |
202 case EV_ABS: | |
203 if(ev[i].code == ABS_X) | |
204 tx = ev[i].value; | |
205 else if (ev[i].code == ABS_Y) | |
206 ty = ev[i].value; | |
207 else if (ev[i].code == ABS_MISC) | |
208 { | |
209 //printf("Misc:type: %X\n code: %X\n value: %i\n ", | |
210 // ev[i].type,ev[i].code,ev[i].value); | |
211 } | |
212 break; | |
213 case EV_MSC: | |
214 if(ev[i].code == MSC_SERIAL) | |
215 curf = ev[i].value; | |
216 break; | |
217 case EV_SYN: | |
218 curf -= 1; | |
219 if(tx >= 0) | |
220 finger[curf].x = tx; | |
221 if(ty >= 0) | |
222 finger[curf].y = ty; | |
223 | |
224 //printf("Synched: %i tx: %i, ty: %i\n",curf,finger[curf].x,finger[curf].y); | |
225 tx = -1; | |
226 ty = -1; | |
227 break; | |
228 | |
229 } | |
230 //And draw | |
231 DrawScreen(screen,h); | |
232 /* | |
233 for(i=0;i<512;i++) | |
234 if(keystat[i]) printf("%i\n",i); | |
235 printf("Buttons:%i\n",bstatus); | |
236 */ | |
237 } | |
238 SDL_Quit(); | |
239 | |
240 return 0; | |
241 } |