Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
463 lines (387 loc) · 10.1 KB

SDL_fbelo.c

File metadata and controls

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