Mercurial > sdl-ios-xcode
annotate src/video/fbcon/SDL_fbelo.c @ 1338:604d73db6802
Removed uses of stdlib.h and string.h
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 07 Feb 2006 09:29:18 +0000 |
parents | 3692456e7b0f |
children | d02b552e5304 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1259
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1259
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1259
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1259
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1259
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1259
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1259
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
152
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 #include <unistd.h> | |
24 #include <sys/time.h> | |
25 #include <ctype.h> | |
26 | |
1338
604d73db6802
Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents:
1336
diff
changeset
|
27 #include "SDL_stdlib.h" |
604d73db6802
Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents:
1336
diff
changeset
|
28 #include "SDL_string.h" |
0 | 29 #include "SDL_fbvideo.h" |
30 #include "SDL_fbelo.h" | |
31 | |
32 /* | |
33 calibration default values | |
34 values are read from the following environment variables: | |
35 | |
36 SDL_ELO_MIN_X | |
37 SDL_ELO_MAX_X | |
38 SDL_ELO_MIN_Y | |
39 SDL_ELO_MAX_Y | |
40 */ | |
41 | |
42 static int ELO_MIN_X = 400; | |
43 static int ELO_MAX_X = 3670; | |
44 static int ELO_MIN_Y = 500; | |
45 static int ELO_MAX_Y = 3540; | |
46 | |
47 #define ELO_SNAP_SIZE 6 | |
48 #define ELO_TOUCH_BYTE 'T' | |
49 #define ELO_ID 'I' | |
50 #define ELO_MODE 'M' | |
51 #define ELO_PARAMETER 'P' | |
52 #define ELO_REPORT 'B' | |
53 #define ELO_ACK 'A' | |
54 | |
55 #define ELO_INIT_CHECKSUM 0xAA | |
56 | |
57 #define ELO_BTN_PRESS 0x01 | |
58 #define ELO_STREAM 0x02 | |
59 #define ELO_BTN_RELEASE 0x04 | |
60 | |
61 #define ELO_TOUCH_MODE 0x01 | |
62 #define ELO_STREAM_MODE 0x02 | |
63 #define ELO_UNTOUCH_MODE 0x04 | |
64 #define ELO_RANGE_CHECK_MODE 0x40 | |
65 #define ELO_TRIM_MODE 0x02 | |
66 #define ELO_CALIB_MODE 0x04 | |
67 #define ELO_SCALING_MODE 0x08 | |
68 #define ELO_TRACKING_MODE 0x40 | |
69 | |
70 #define ELO_SERIAL_MASK 0xF8 | |
71 | |
72 #define ELO_SERIAL_IO '0' | |
73 | |
74 #define ELO_MAX_TRIALS 3 | |
75 #define ELO_MAX_WAIT 100000 | |
76 #define ELO_UNTOUCH_DELAY 5 | |
77 #define ELO_REPORT_DELAY 1 | |
78 | |
79 /* eloParsePacket | |
80 */ | |
81 int eloParsePacket(unsigned char* mousebuf, int* dx, int* dy, int* button_state) { | |
82 static int elo_button = 0; | |
83 static int last_x = 0; | |
84 static int last_y = 0; | |
85 int x,y; | |
86 | |
87 /* Check if we have a touch packet */ | |
88 if (mousebuf[1] != ELO_TOUCH_BYTE) { | |
89 return 0; | |
90 } | |
91 | |
92 x = ((mousebuf[4] << 8) | mousebuf[3]); | |
93 y = ((mousebuf[6] << 8) | mousebuf[5]); | |
94 | |
95 if((abs(x - last_x) > ELO_SNAP_SIZE) || (abs(y - last_y) > ELO_SNAP_SIZE)) { | |
96 *dx = ((mousebuf[4] << 8) | mousebuf[3]); | |
97 *dy = ((mousebuf[6] << 8) | mousebuf[5]); | |
98 } | |
99 else { | |
100 *dx = last_x; | |
101 *dy = last_y; | |
102 } | |
103 | |
104 last_x = *dx; | |
105 last_y = *dy; | |
106 | |
107 if ( (mousebuf[2] & 0x07) == ELO_BTN_PRESS ) { | |
108 elo_button = 1; | |
109 } | |
110 if ( (mousebuf[2] & 0x07) == ELO_BTN_RELEASE ) { | |
111 elo_button = 0; | |
112 } | |
113 | |
114 *button_state = elo_button; | |
115 return 1; | |
116 } | |
117 | |
118 /* Convert the raw coordinates from the ELO controller | |
119 to a screen position. | |
120 */ | |
121 void eloConvertXY(_THIS, int *dx, int *dy) { | |
122 int input_x = *dx; | |
123 int input_y = *dy; | |
124 int width = ELO_MAX_X - ELO_MIN_X; | |
125 int height = ELO_MAX_Y - ELO_MIN_Y; | |
126 | |
1259
36f24cdfcda7
te: Thu, 17 Apr 2003 11:25:26 -0700
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
127 *dx = ((int)cache_vinfo.xres - ((int)cache_vinfo.xres * (input_x - ELO_MIN_X)) / width); |
0 | 128 *dy = (cache_vinfo.yres * (input_y - ELO_MIN_Y)) / height; |
129 } | |
130 | |
131 | |
132 /* eloGetPacket | |
133 */ | |
134 int eloGetPacket(unsigned char* buffer, int* buffer_p, int* checksum, int fd) { | |
135 int num_bytes; | |
136 int ok; | |
137 | |
138 if(fd == 0) { | |
139 num_bytes = ELO_PACKET_SIZE; | |
140 } | |
141 else { | |
142 num_bytes = read(fd, | |
143 (char *) (buffer + *buffer_p), | |
144 ELO_PACKET_SIZE - *buffer_p); | |
145 } | |
146 | |
147 if (num_bytes < 0) { | |
148 #ifdef DEBUG_MOUSE | |
149 fprintf(stderr, "System error while reading from Elographics touchscreen.\n"); | |
150 #endif | |
151 return 0; | |
152 } | |
153 | |
154 while (num_bytes) { | |
155 if ((*buffer_p == 0) && (buffer[0] != ELO_START_BYTE)) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
156 SDL_memcpy(&buffer[0], &buffer[1], num_bytes-1); |
0 | 157 } |
158 else { | |
159 if (*buffer_p < ELO_PACKET_SIZE-1) { | |
160 *checksum = *checksum + buffer[*buffer_p]; | |
161 *checksum = *checksum % 256; | |
162 } | |
163 (*buffer_p)++; | |
164 } | |
165 num_bytes--; | |
166 } | |
167 | |
168 if (*buffer_p == ELO_PACKET_SIZE) { | |
169 ok = (*checksum == buffer[ELO_PACKET_SIZE-1]); | |
170 *checksum = ELO_INIT_CHECKSUM; | |
171 *buffer_p = 0; | |
172 | |
173 if (!ok) { | |
174 return 0; | |
175 } | |
176 | |
177 return 1; | |
178 } | |
179 else { | |
180 return 0; | |
181 } | |
182 } | |
183 | |
184 /* eloSendPacket | |
185 */ | |
186 | |
187 int eloSendPacket(unsigned char* packet, int fd) | |
188 { | |
189 int i, result; | |
190 int sum = ELO_INIT_CHECKSUM; | |
191 | |
192 packet[0] = ELO_START_BYTE; | |
193 for (i = 0; i < ELO_PACKET_SIZE-1; i++) { | |
194 sum += packet[i]; | |
195 sum &= 0xFF; | |
196 } | |
197 packet[ELO_PACKET_SIZE-1] = sum; | |
198 | |
199 result = write(fd, packet, ELO_PACKET_SIZE); | |
200 | |
201 if (result != ELO_PACKET_SIZE) { | |
202 #ifdef DEBUG_MOUSE | |
203 printf("System error while sending to Elographics touchscreen.\n"); | |
204 #endif | |
205 return 0; | |
206 } | |
207 else { | |
208 return 1; | |
209 } | |
210 } | |
211 | |
212 | |
213 /* eloWaitForInput | |
214 */ | |
215 int eloWaitForInput(int fd, int timeout) | |
216 { | |
217 fd_set readfds; | |
218 struct timeval to; | |
219 int r; | |
220 | |
221 FD_ZERO(&readfds); | |
222 FD_SET(fd, &readfds); | |
223 to.tv_sec = 0; | |
224 to.tv_usec = timeout; | |
225 | |
226 r = select(FD_SETSIZE, &readfds, NULL, NULL, &to); | |
227 return r; | |
228 } | |
229 | |
230 /* eloWaitReply | |
231 */ | |
232 int eloWaitReply(unsigned char type, unsigned char *reply, int fd) { | |
233 int ok; | |
234 int i, result; | |
235 int reply_p = 0; | |
236 int sum = ELO_INIT_CHECKSUM; | |
237 | |
238 i = ELO_MAX_TRIALS; | |
239 do { | |
240 ok = 0; | |
241 | |
242 result = eloWaitForInput(fd, ELO_MAX_WAIT); | |
243 | |
244 if (result > 0) { | |
245 ok = eloGetPacket(reply, &reply_p, &sum, fd); | |
246 | |
247 if (ok && reply[1] != type && type != ELO_PARAMETER) { | |
248 #ifdef DEBUG_MOUSE | |
249 fprintf(stderr, "Wrong reply received\n"); | |
250 #endif | |
251 ok = 0; | |
252 } | |
253 } | |
254 else { | |
255 #ifdef DEBUG_MOUSE | |
256 fprintf(stderr, "No input!\n"); | |
257 #endif | |
258 } | |
259 | |
260 if (result == 0) { | |
261 i--; | |
262 } | |
263 } while(!ok && (i>0)); | |
264 | |
265 return ok; | |
266 } | |
267 | |
268 | |
269 /* eloWaitAck | |
270 */ | |
271 | |
272 int eloWaitAck(int fd) { | |
273 unsigned char packet[ELO_PACKET_SIZE]; | |
274 int i, nb_errors; | |
275 | |
276 if (eloWaitReply(ELO_ACK, packet, fd)) { | |
277 for (i = 0, nb_errors = 0; i < 4; i++) { | |
278 if (packet[2 + i] != '0') { | |
279 nb_errors++; | |
280 } | |
281 } | |
282 | |
283 if (nb_errors != 0) { | |
284 #ifdef DEBUG_MOUSE | |
285 fprintf(stderr, "Elographics acknowledge packet reports %d errors\n", nb_errors); | |
286 #endif | |
287 } | |
288 return 1; | |
289 } | |
290 else { | |
291 return 0; | |
292 } | |
293 } | |
294 | |
295 | |
296 /* eloSendQuery -- | |
297 */ | |
298 int eloSendQuery(unsigned char *request, unsigned char* reply, int fd) { | |
299 int ok; | |
300 | |
301 if (eloSendPacket(request, fd)) { | |
302 ok = eloWaitReply(toupper(request[1]), reply, fd); | |
303 if (ok) { | |
304 ok = eloWaitAck(fd); | |
305 } | |
306 return ok; | |
307 } | |
308 else { | |
309 return 0; | |
310 } | |
311 } | |
312 | |
313 | |
314 /* eloSendControl | |
315 */ | |
316 int eloSendControl(unsigned char* control, int fd) { | |
317 if (eloSendPacket(control, fd)) { | |
318 return eloWaitAck(fd); | |
319 } | |
320 else { | |
321 return 0; | |
322 } | |
323 } | |
324 | |
325 /* eloInitController | |
326 */ | |
327 int eloInitController(int fd) { | |
328 unsigned char req[ELO_PACKET_SIZE]; | |
329 unsigned char reply[ELO_PACKET_SIZE]; | |
330 const char *buffer = NULL; | |
331 int result = 0; | |
332 | |
333 struct termios mouse_termios; | |
334 | |
335 /* try to read the calibration values */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
336 buffer = SDL_getenv("SDL_ELO_MIN_X"); |
0 | 337 if(buffer) { |
338 ELO_MIN_X = atoi(buffer); | |
339 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
340 buffer = SDL_getenv("SDL_ELO_MAX_X"); |
0 | 341 if(buffer) { |
342 ELO_MAX_X = atoi(buffer); | |
343 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
344 buffer = SDL_getenv("SDL_ELO_MIN_Y"); |
0 | 345 if(buffer) { |
346 ELO_MIN_Y = atoi(buffer); | |
347 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
348 buffer = SDL_getenv("SDL_ELO_MAX_Y"); |
0 | 349 if(buffer) { |
350 ELO_MAX_Y = atoi(buffer); | |
351 } | |
352 | |
353 #ifdef DEBUG_MOUSE | |
354 fprintf( stderr, "ELO calibration values:\nmin_x: %i\nmax_x: %i\nmin_y: %i\nmax_y: %i\n", | |
355 ELO_MIN_X, | |
356 ELO_MAX_X, | |
357 ELO_MIN_Y, | |
358 ELO_MAX_Y); | |
359 #endif | |
360 | |
361 /* set comm params */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
362 SDL_memset(&mouse_termios, 0, sizeof(mouse_termios)); |
0 | 363 mouse_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL; |
364 mouse_termios.c_cc[VMIN] = 1; | |
365 result = tcsetattr(fd, TCSANOW, &mouse_termios); | |
366 | |
367 if (result < 0) { | |
368 #ifdef DEBUG_MOUSE | |
369 fprintf( stderr, "Unable to configure Elographics touchscreen port\n"); | |
370 #endif | |
371 return 0; | |
372 } | |
373 | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
374 SDL_memset(req, 0, ELO_PACKET_SIZE); |
0 | 375 req[1] = tolower(ELO_PARAMETER); |
376 if (!eloSendQuery(req, reply, fd)) { | |
377 #ifdef DEBUG_MOUSE | |
378 fprintf( stderr, "Not at the specified rate or model 2310, will continue\n"); | |
379 #endif | |
380 } | |
381 | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
382 SDL_memset(req, 0, ELO_PACKET_SIZE); |
0 | 383 req[1] = tolower(ELO_ID); |
384 if (eloSendQuery(req, reply, fd)) { | |
385 #ifdef DEBUG_MOUSE | |
386 fprintf(stderr, "Ok, controller configured!\n"); | |
387 #endif | |
388 } | |
389 else { | |
390 #ifdef DEBUG_MOUSE | |
391 fprintf( stderr, "Unable to ask Elographics touchscreen identification\n"); | |
392 #endif | |
393 return 0; | |
394 } | |
395 | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
396 SDL_memset(req, 0, ELO_PACKET_SIZE); |
0 | 397 req[1] = ELO_MODE; |
398 req[3] = ELO_TOUCH_MODE | ELO_STREAM_MODE | ELO_UNTOUCH_MODE; | |
399 req[4] = ELO_TRACKING_MODE; | |
400 if (!eloSendControl(req, fd)) { | |
401 #ifdef DEBUG_MOUSE | |
402 fprintf( stderr, "Unable to change Elographics touchscreen operating mode\n"); | |
403 #endif | |
404 return 0; | |
405 } | |
406 | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
407 SDL_memset(req, 0, ELO_PACKET_SIZE); |
0 | 408 req[1] = ELO_REPORT; |
409 req[2] = ELO_UNTOUCH_DELAY; | |
410 req[3] = ELO_REPORT_DELAY; | |
411 if (!eloSendControl(req, fd)) { | |
412 #ifdef DEBUG_MOUSE | |
413 fprintf( stderr, "Unable to change Elographics touchscreen reports timings\n"); | |
414 #endif | |
415 return 0; | |
416 } | |
417 | |
418 return 1; | |
419 } | |
4
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
420 |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
421 int eloReadPosition(_THIS, int fd, int* x, int* y, int* button_state, int* realx, int* realy) { |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
422 unsigned char buffer[ELO_PACKET_SIZE]; |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
423 int pointer = 0; |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
424 int checksum = ELO_INIT_CHECKSUM; |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
425 |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
426 while(pointer < ELO_PACKET_SIZE) { |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
427 if(eloGetPacket(buffer, &pointer, &checksum, fd)) { |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
428 break; |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
429 } |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
430 } |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
431 |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
432 if(!eloParsePacket(buffer, realx, realy, button_state)) { |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
433 return 0; |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
434 } |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
435 |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
436 *x = *realx; |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
437 *y = *realy; |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
438 |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
439 eloConvertXY(this, x, y); |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
440 |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
441 return 1; |
4f6c5f021323
Date: Thu, 26 Apr 2001 10:46:23 +0200
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
442 } |