comparison src/video/fbcon/SDL_fbelo.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
77 #define ELO_REPORT_DELAY 1 77 #define ELO_REPORT_DELAY 1
78 78
79 /* eloParsePacket 79 /* eloParsePacket
80 */ 80 */
81 int 81 int
82 eloParsePacket (unsigned char *mousebuf, int *dx, int *dy, int *button_state) 82 eloParsePacket(unsigned char *mousebuf, int *dx, int *dy, int *button_state)
83 { 83 {
84 static int elo_button = 0; 84 static int elo_button = 0;
85 static int last_x = 0; 85 static int last_x = 0;
86 static int last_y = 0; 86 static int last_y = 0;
87 int x, y; 87 int x, y;
92 } 92 }
93 93
94 x = ((mousebuf[4] << 8) | mousebuf[3]); 94 x = ((mousebuf[4] << 8) | mousebuf[3]);
95 y = ((mousebuf[6] << 8) | mousebuf[5]); 95 y = ((mousebuf[6] << 8) | mousebuf[5]);
96 96
97 if ((SDL_abs (x - last_x) > ELO_SNAP_SIZE) 97 if ((SDL_abs(x - last_x) > ELO_SNAP_SIZE)
98 || (SDL_abs (y - last_y) > ELO_SNAP_SIZE)) { 98 || (SDL_abs(y - last_y) > ELO_SNAP_SIZE)) {
99 *dx = ((mousebuf[4] << 8) | mousebuf[3]); 99 *dx = ((mousebuf[4] << 8) | mousebuf[3]);
100 *dy = ((mousebuf[6] << 8) | mousebuf[5]); 100 *dy = ((mousebuf[6] << 8) | mousebuf[5]);
101 } else { 101 } else {
102 *dx = last_x; 102 *dx = last_x;
103 *dy = last_y; 103 *dy = last_y;
119 119
120 /* Convert the raw coordinates from the ELO controller 120 /* Convert the raw coordinates from the ELO controller
121 to a screen position. 121 to a screen position.
122 */ 122 */
123 void 123 void
124 eloConvertXY (_THIS, int *dx, int *dy) 124 eloConvertXY(_THIS, int *dx, int *dy)
125 { 125 {
126 int input_x = *dx; 126 int input_x = *dx;
127 int input_y = *dy; 127 int input_y = *dy;
128 int width = ELO_MAX_X - ELO_MIN_X; 128 int width = ELO_MAX_X - ELO_MIN_X;
129 int height = ELO_MAX_Y - ELO_MIN_Y; 129 int height = ELO_MAX_Y - ELO_MIN_Y;
136 136
137 137
138 /* eloGetPacket 138 /* eloGetPacket
139 */ 139 */
140 int 140 int
141 eloGetPacket (unsigned char *buffer, int *buffer_p, int *checksum, int fd) 141 eloGetPacket(unsigned char *buffer, int *buffer_p, int *checksum, int fd)
142 { 142 {
143 int num_bytes; 143 int num_bytes;
144 int ok; 144 int ok;
145 145
146 if (fd == 0) { 146 if (fd == 0) {
147 num_bytes = ELO_PACKET_SIZE; 147 num_bytes = ELO_PACKET_SIZE;
148 } else { 148 } else {
149 num_bytes = read (fd, 149 num_bytes = read(fd,
150 (char *) (buffer + *buffer_p), 150 (char *) (buffer + *buffer_p),
151 ELO_PACKET_SIZE - *buffer_p); 151 ELO_PACKET_SIZE - *buffer_p);
152 } 152 }
153 153
154 if (num_bytes < 0) { 154 if (num_bytes < 0) {
155 #ifdef DEBUG_MOUSE 155 #ifdef DEBUG_MOUSE
156 fprintf (stderr, 156 fprintf(stderr,
157 "System error while reading from Elographics touchscreen.\n"); 157 "System error while reading from Elographics touchscreen.\n");
158 #endif 158 #endif
159 return 0; 159 return 0;
160 } 160 }
161 161
162 while (num_bytes) { 162 while (num_bytes) {
163 if ((*buffer_p == 0) && (buffer[0] != ELO_START_BYTE)) { 163 if ((*buffer_p == 0) && (buffer[0] != ELO_START_BYTE)) {
164 SDL_memcpy (&buffer[0], &buffer[1], num_bytes - 1); 164 SDL_memcpy(&buffer[0], &buffer[1], num_bytes - 1);
165 } else { 165 } else {
166 if (*buffer_p < ELO_PACKET_SIZE - 1) { 166 if (*buffer_p < ELO_PACKET_SIZE - 1) {
167 *checksum = *checksum + buffer[*buffer_p]; 167 *checksum = *checksum + buffer[*buffer_p];
168 *checksum = *checksum % 256; 168 *checksum = *checksum % 256;
169 } 169 }
189 189
190 /* eloSendPacket 190 /* eloSendPacket
191 */ 191 */
192 192
193 int 193 int
194 eloSendPacket (unsigned char *packet, int fd) 194 eloSendPacket(unsigned char *packet, int fd)
195 { 195 {
196 int i, result; 196 int i, result;
197 int sum = ELO_INIT_CHECKSUM; 197 int sum = ELO_INIT_CHECKSUM;
198 198
199 packet[0] = ELO_START_BYTE; 199 packet[0] = ELO_START_BYTE;
201 sum += packet[i]; 201 sum += packet[i];
202 sum &= 0xFF; 202 sum &= 0xFF;
203 } 203 }
204 packet[ELO_PACKET_SIZE - 1] = sum; 204 packet[ELO_PACKET_SIZE - 1] = sum;
205 205
206 result = write (fd, packet, ELO_PACKET_SIZE); 206 result = write(fd, packet, ELO_PACKET_SIZE);
207 207
208 if (result != ELO_PACKET_SIZE) { 208 if (result != ELO_PACKET_SIZE) {
209 #ifdef DEBUG_MOUSE 209 #ifdef DEBUG_MOUSE
210 printf ("System error while sending to Elographics touchscreen.\n"); 210 printf("System error while sending to Elographics touchscreen.\n");
211 #endif 211 #endif
212 return 0; 212 return 0;
213 } else { 213 } else {
214 return 1; 214 return 1;
215 } 215 }
217 217
218 218
219 /* eloWaitForInput 219 /* eloWaitForInput
220 */ 220 */
221 int 221 int
222 eloWaitForInput (int fd, int timeout) 222 eloWaitForInput(int fd, int timeout)
223 { 223 {
224 fd_set readfds; 224 fd_set readfds;
225 struct timeval to; 225 struct timeval to;
226 int r; 226 int r;
227 227
228 FD_ZERO (&readfds); 228 FD_ZERO(&readfds);
229 FD_SET (fd, &readfds); 229 FD_SET(fd, &readfds);
230 to.tv_sec = 0; 230 to.tv_sec = 0;
231 to.tv_usec = timeout; 231 to.tv_usec = timeout;
232 232
233 r = select (FD_SETSIZE, &readfds, NULL, NULL, &to); 233 r = select(FD_SETSIZE, &readfds, NULL, NULL, &to);
234 return r; 234 return r;
235 } 235 }
236 236
237 /* eloWaitReply 237 /* eloWaitReply
238 */ 238 */
239 int 239 int
240 eloWaitReply (unsigned char type, unsigned char *reply, int fd) 240 eloWaitReply(unsigned char type, unsigned char *reply, int fd)
241 { 241 {
242 int ok; 242 int ok;
243 int i, result; 243 int i, result;
244 int reply_p = 0; 244 int reply_p = 0;
245 int sum = ELO_INIT_CHECKSUM; 245 int sum = ELO_INIT_CHECKSUM;
246 246
247 i = ELO_MAX_TRIALS; 247 i = ELO_MAX_TRIALS;
248 do { 248 do {
249 ok = 0; 249 ok = 0;
250 250
251 result = eloWaitForInput (fd, ELO_MAX_WAIT); 251 result = eloWaitForInput(fd, ELO_MAX_WAIT);
252 252
253 if (result > 0) { 253 if (result > 0) {
254 ok = eloGetPacket (reply, &reply_p, &sum, fd); 254 ok = eloGetPacket(reply, &reply_p, &sum, fd);
255 255
256 if (ok && reply[1] != type && type != ELO_PARAMETER) { 256 if (ok && reply[1] != type && type != ELO_PARAMETER) {
257 #ifdef DEBUG_MOUSE 257 #ifdef DEBUG_MOUSE
258 fprintf (stderr, "Wrong reply received\n"); 258 fprintf(stderr, "Wrong reply received\n");
259 #endif 259 #endif
260 ok = 0; 260 ok = 0;
261 } 261 }
262 } else { 262 } else {
263 #ifdef DEBUG_MOUSE 263 #ifdef DEBUG_MOUSE
264 fprintf (stderr, "No input!\n"); 264 fprintf(stderr, "No input!\n");
265 #endif 265 #endif
266 } 266 }
267 267
268 if (result == 0) { 268 if (result == 0) {
269 i--; 269 i--;
277 277
278 /* eloWaitAck 278 /* eloWaitAck
279 */ 279 */
280 280
281 int 281 int
282 eloWaitAck (int fd) 282 eloWaitAck(int fd)
283 { 283 {
284 unsigned char packet[ELO_PACKET_SIZE]; 284 unsigned char packet[ELO_PACKET_SIZE];
285 int i, nb_errors; 285 int i, nb_errors;
286 286
287 if (eloWaitReply (ELO_ACK, packet, fd)) { 287 if (eloWaitReply(ELO_ACK, packet, fd)) {
288 for (i = 0, nb_errors = 0; i < 4; i++) { 288 for (i = 0, nb_errors = 0; i < 4; i++) {
289 if (packet[2 + i] != '0') { 289 if (packet[2 + i] != '0') {
290 nb_errors++; 290 nb_errors++;
291 } 291 }
292 } 292 }
293 293
294 if (nb_errors != 0) { 294 if (nb_errors != 0) {
295 #ifdef DEBUG_MOUSE 295 #ifdef DEBUG_MOUSE
296 fprintf (stderr, 296 fprintf(stderr,
297 "Elographics acknowledge packet reports %d errors\n", 297 "Elographics acknowledge packet reports %d errors\n",
298 nb_errors); 298 nb_errors);
299 #endif 299 #endif
300 } 300 }
301 return 1; 301 return 1;
302 } else { 302 } else {
303 return 0; 303 return 0;
306 306
307 307
308 /* eloSendQuery -- 308 /* eloSendQuery --
309 */ 309 */
310 int 310 int
311 eloSendQuery (unsigned char *request, unsigned char *reply, int fd) 311 eloSendQuery(unsigned char *request, unsigned char *reply, int fd)
312 { 312 {
313 int ok; 313 int ok;
314 314
315 if (eloSendPacket (request, fd)) { 315 if (eloSendPacket(request, fd)) {
316 ok = eloWaitReply (toupper (request[1]), reply, fd); 316 ok = eloWaitReply(toupper(request[1]), reply, fd);
317 if (ok) { 317 if (ok) {
318 ok = eloWaitAck (fd); 318 ok = eloWaitAck(fd);
319 } 319 }
320 return ok; 320 return ok;
321 } else { 321 } else {
322 return 0; 322 return 0;
323 } 323 }
325 325
326 326
327 /* eloSendControl 327 /* eloSendControl
328 */ 328 */
329 int 329 int
330 eloSendControl (unsigned char *control, int fd) 330 eloSendControl(unsigned char *control, int fd)
331 { 331 {
332 if (eloSendPacket (control, fd)) { 332 if (eloSendPacket(control, fd)) {
333 return eloWaitAck (fd); 333 return eloWaitAck(fd);
334 } else { 334 } else {
335 return 0; 335 return 0;
336 } 336 }
337 } 337 }
338 338
339 /* eloInitController 339 /* eloInitController
340 */ 340 */
341 int 341 int
342 eloInitController (int fd) 342 eloInitController(int fd)
343 { 343 {
344 unsigned char req[ELO_PACKET_SIZE]; 344 unsigned char req[ELO_PACKET_SIZE];
345 unsigned char reply[ELO_PACKET_SIZE]; 345 unsigned char reply[ELO_PACKET_SIZE];
346 const char *buffer = NULL; 346 const char *buffer = NULL;
347 int result = 0; 347 int result = 0;
348 348
349 struct termios mouse_termios; 349 struct termios mouse_termios;
350 350
351 /* try to read the calibration values */ 351 /* try to read the calibration values */
352 buffer = SDL_getenv ("SDL_ELO_MIN_X"); 352 buffer = SDL_getenv("SDL_ELO_MIN_X");
353 if (buffer) { 353 if (buffer) {
354 ELO_MIN_X = SDL_atoi (buffer); 354 ELO_MIN_X = SDL_atoi(buffer);
355 } 355 }
356 buffer = SDL_getenv ("SDL_ELO_MAX_X"); 356 buffer = SDL_getenv("SDL_ELO_MAX_X");
357 if (buffer) { 357 if (buffer) {
358 ELO_MAX_X = SDL_atoi (buffer); 358 ELO_MAX_X = SDL_atoi(buffer);
359 } 359 }
360 buffer = SDL_getenv ("SDL_ELO_MIN_Y"); 360 buffer = SDL_getenv("SDL_ELO_MIN_Y");
361 if (buffer) { 361 if (buffer) {
362 ELO_MIN_Y = SDL_atoi (buffer); 362 ELO_MIN_Y = SDL_atoi(buffer);
363 } 363 }
364 buffer = SDL_getenv ("SDL_ELO_MAX_Y"); 364 buffer = SDL_getenv("SDL_ELO_MAX_Y");
365 if (buffer) { 365 if (buffer) {
366 ELO_MAX_Y = SDL_atoi (buffer); 366 ELO_MAX_Y = SDL_atoi(buffer);
367 } 367 }
368 #ifdef DEBUG_MOUSE 368 #ifdef DEBUG_MOUSE
369 fprintf (stderr, 369 fprintf(stderr,
370 "ELO calibration values:\nmin_x: %i\nmax_x: %i\nmin_y: %i\nmax_y: %i\n", 370 "ELO calibration values:\nmin_x: %i\nmax_x: %i\nmin_y: %i\nmax_y: %i\n",
371 ELO_MIN_X, ELO_MAX_X, ELO_MIN_Y, ELO_MAX_Y); 371 ELO_MIN_X, ELO_MAX_X, ELO_MIN_Y, ELO_MAX_Y);
372 #endif 372 #endif
373 373
374 /* set comm params */ 374 /* set comm params */
375 SDL_memset (&mouse_termios, 0, sizeof (mouse_termios)); 375 SDL_memset(&mouse_termios, 0, sizeof(mouse_termios));
376 mouse_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL; 376 mouse_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
377 mouse_termios.c_cc[VMIN] = 1; 377 mouse_termios.c_cc[VMIN] = 1;
378 result = tcsetattr (fd, TCSANOW, &mouse_termios); 378 result = tcsetattr(fd, TCSANOW, &mouse_termios);
379 379
380 if (result < 0) { 380 if (result < 0) {
381 #ifdef DEBUG_MOUSE 381 #ifdef DEBUG_MOUSE
382 fprintf (stderr, 382 fprintf(stderr, "Unable to configure Elographics touchscreen port\n");
383 "Unable to configure Elographics touchscreen port\n"); 383 #endif
384 #endif 384 return 0;
385 return 0; 385 }
386 } 386
387 387 SDL_memset(req, 0, ELO_PACKET_SIZE);
388 SDL_memset (req, 0, ELO_PACKET_SIZE); 388 req[1] = tolower(ELO_PARAMETER);
389 req[1] = tolower (ELO_PARAMETER); 389 if (!eloSendQuery(req, reply, fd)) {
390 if (!eloSendQuery (req, reply, fd)) { 390 #ifdef DEBUG_MOUSE
391 #ifdef DEBUG_MOUSE 391 fprintf(stderr,
392 fprintf (stderr, 392 "Not at the specified rate or model 2310, will continue\n");
393 "Not at the specified rate or model 2310, will continue\n"); 393 #endif
394 #endif 394 }
395 } 395
396 396 SDL_memset(req, 0, ELO_PACKET_SIZE);
397 SDL_memset (req, 0, ELO_PACKET_SIZE); 397 req[1] = tolower(ELO_ID);
398 req[1] = tolower (ELO_ID); 398 if (eloSendQuery(req, reply, fd)) {
399 if (eloSendQuery (req, reply, fd)) { 399 #ifdef DEBUG_MOUSE
400 #ifdef DEBUG_MOUSE 400 fprintf(stderr, "Ok, controller configured!\n");
401 fprintf (stderr, "Ok, controller configured!\n"); 401 #endif
402 #endif 402 } else {
403 } else { 403 #ifdef DEBUG_MOUSE
404 #ifdef DEBUG_MOUSE 404 fprintf(stderr,
405 fprintf (stderr, 405 "Unable to ask Elographics touchscreen identification\n");
406 "Unable to ask Elographics touchscreen identification\n"); 406 #endif
407 #endif 407 return 0;
408 return 0; 408 }
409 } 409
410 410 SDL_memset(req, 0, ELO_PACKET_SIZE);
411 SDL_memset (req, 0, ELO_PACKET_SIZE);
412 req[1] = ELO_MODE; 411 req[1] = ELO_MODE;
413 req[3] = ELO_TOUCH_MODE | ELO_STREAM_MODE | ELO_UNTOUCH_MODE; 412 req[3] = ELO_TOUCH_MODE | ELO_STREAM_MODE | ELO_UNTOUCH_MODE;
414 req[4] = ELO_TRACKING_MODE; 413 req[4] = ELO_TRACKING_MODE;
415 if (!eloSendControl (req, fd)) { 414 if (!eloSendControl(req, fd)) {
416 #ifdef DEBUG_MOUSE 415 #ifdef DEBUG_MOUSE
417 fprintf (stderr, 416 fprintf(stderr,
418 "Unable to change Elographics touchscreen operating mode\n"); 417 "Unable to change Elographics touchscreen operating mode\n");
419 #endif 418 #endif
420 return 0; 419 return 0;
421 } 420 }
422 421
423 SDL_memset (req, 0, ELO_PACKET_SIZE); 422 SDL_memset(req, 0, ELO_PACKET_SIZE);
424 req[1] = ELO_REPORT; 423 req[1] = ELO_REPORT;
425 req[2] = ELO_UNTOUCH_DELAY; 424 req[2] = ELO_UNTOUCH_DELAY;
426 req[3] = ELO_REPORT_DELAY; 425 req[3] = ELO_REPORT_DELAY;
427 if (!eloSendControl (req, fd)) { 426 if (!eloSendControl(req, fd)) {
428 #ifdef DEBUG_MOUSE 427 #ifdef DEBUG_MOUSE
429 fprintf (stderr, 428 fprintf(stderr,
430 "Unable to change Elographics touchscreen reports timings\n"); 429 "Unable to change Elographics touchscreen reports timings\n");
431 #endif 430 #endif
432 return 0; 431 return 0;
433 } 432 }
434 433
435 return 1; 434 return 1;
436 } 435 }
437 436
438 int 437 int
439 eloReadPosition (_THIS, int fd, int *x, int *y, int *button_state, int *realx, 438 eloReadPosition(_THIS, int fd, int *x, int *y, int *button_state, int *realx,
440 int *realy) 439 int *realy)
441 { 440 {
442 unsigned char buffer[ELO_PACKET_SIZE]; 441 unsigned char buffer[ELO_PACKET_SIZE];
443 int pointer = 0; 442 int pointer = 0;
444 int checksum = ELO_INIT_CHECKSUM; 443 int checksum = ELO_INIT_CHECKSUM;
445 444
446 while (pointer < ELO_PACKET_SIZE) { 445 while (pointer < ELO_PACKET_SIZE) {
447 if (eloGetPacket (buffer, &pointer, &checksum, fd)) { 446 if (eloGetPacket(buffer, &pointer, &checksum, fd)) {
448 break; 447 break;
449 } 448 }
450 } 449 }
451 450
452 if (!eloParsePacket (buffer, realx, realy, button_state)) { 451 if (!eloParsePacket(buffer, realx, realy, button_state)) {
453 return 0; 452 return 0;
454 } 453 }
455 454
456 *x = *realx; 455 *x = *realx;
457 *y = *realy; 456 *y = *realy;
458 457
459 eloConvertXY (this, x, y); 458 eloConvertXY(this, x, y);
460 459
461 return 1; 460 return 1;
462 } 461 }
463 462
464 /* vi: set ts=4 sw=4 expandtab: */ 463 /* vi: set ts=4 sw=4 expandtab: */