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 | f068a6dfc858 |
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 memset(keystat,0,512*sizeof(keystat[0])); | |
142 if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1; | |
143 | |
144 if (!(screen = initScreen(WIDTH,HEIGHT))) | |
145 { | |
146 SDL_Quit(); | |
147 return 1; | |
148 } | |
149 | |
150 while(!keystat[27]) | |
151 { | |
152 //Poll SDL | |
153 while(SDL_PollEvent(&event)) | |
154 { | |
155 switch (event.type) | |
156 { | |
157 case SDL_QUIT: | |
158 keystat[27] = 1; | |
159 break; | |
160 case SDL_KEYDOWN: | |
161 //printf("%i\n",event.key.keysym.sym); | |
162 keystat[event.key.keysym.sym] = 1; | |
163 //keypress = 1; | |
164 break; | |
165 case SDL_KEYUP: | |
166 //printf("%i\n",event.key.keysym.sym); | |
167 keystat[event.key.keysym.sym] = 0; | |
168 //keypress = 1; | |
169 break; | |
170 case SDL_VIDEORESIZE: | |
171 if (!(screen = initScreen(event.resize.w, | |
172 event.resize.h))) | |
173 { | |
174 SDL_Quit(); | |
175 return 1; | |
176 } | |
177 break; | |
178 case SDL_MOUSEMOTION: | |
179 mousx = event.motion.x; | |
180 mousy = event.motion.y; | |
181 break; | |
182 case SDL_MOUSEBUTTONDOWN: | |
183 bstatus |= (1<<(event.button.button-1)); | |
184 break; | |
185 case SDL_MOUSEBUTTONUP: | |
186 bstatus &= ~(1<<(event.button.button-1)); | |
187 break; | |
188 } | |
189 } | |
190 | |
191 //poll for Touch <- Goal: make this a case: | |
192 if ((rd = read (fd, ev, size * 64)) < size) | |
193 perror_exit ("read()"); | |
194 //printf("time: %i\n type: %X\n code: %X\n value: %i\n ", | |
195 // ev->time,ev->type,ev->code,ev->value); | |
196 for (i = 0; i < rd / sizeof(struct input_event); i++) | |
197 switch (ev[i].type) | |
198 { | |
199 case EV_ABS: | |
200 if(ev[i].code == ABS_X) | |
201 tx = ev[i].value; | |
202 else if (ev[i].code == ABS_Y) | |
203 ty = ev[i].value; | |
204 else if (ev[i].code == ABS_MISC) | |
205 { | |
206 //printf("Misc:type: %X\n code: %X\n value: %i\n ", | |
207 // ev[i].type,ev[i].code,ev[i].value); | |
208 } | |
209 break; | |
210 case EV_MSC: | |
211 if(ev[i].code == MSC_SERIAL) | |
212 curf = ev[i].value; | |
213 break; | |
214 case EV_SYN: | |
215 curf -= 1; | |
216 if(tx >= 0) | |
217 finger[curf].x = tx; | |
218 if(ty >= 0) | |
219 finger[curf].y = ty; | |
220 | |
221 //printf("Synched: %i tx: %i, ty: %i\n",curf,finger[curf].x,finger[curf].y); | |
222 tx = -1; | |
223 ty = -1; | |
224 break; | |
225 | |
226 } | |
227 //And draw | |
228 DrawScreen(screen,h); | |
229 /* | |
230 for(i=0;i<512;i++) | |
231 if(keystat[i]) printf("%i\n",i); | |
232 printf("Buttons:%i\n",bstatus); | |
233 */ | |
234 } | |
235 SDL_Quit(); | |
236 | |
237 return 0; | |
238 } |