11 #include <sys/select.h>
14 #define INP_BUF_SZ 256
30 unsigned int keystate;
32 int (*parse)(struct sball*, int, char*, int);
35 static int stty_sball(int fd);
36 static int stty_mag(int fd);
38 static int proc_input(struct sball *sb);
40 static int mag_parsepkt(struct sball *sb, int id, char *data, int len);
41 static int sball_parsepkt(struct sball *sb, int id, char *data, int len);
43 static int guess_num_buttons(const char *verstr);
45 static void make_printable(char *buf, int len);
46 static int read_timeout(int fd, char *buf, int bufsz, long tm_usec);
48 static void print_state(struct sball *sb);
51 struct sball *sball_open(const char *dev)
57 if((fd = open(dev, O_RDWR | O_NOCTTY)) == -1) {
58 fprintf(stderr, "sball_open: failed to open device: %s: %s\n", dev, strerror(errno));
62 if(!(sb = malloc(sizeof *sb))) {
63 fprintf(stderr, "sball_open: failed to allocate sball object\n");
70 if(stty_sball(fd) == -1) {
74 if((sz = read_timeout(fd, buf, sizeof buf - 1, 2000000)) > 0 && memcmp(buf, "\r@1", 3) == 0) {
75 /* we got a response, so it's a spaceball */
76 make_printable(buf, sz);
77 printf("Spaceball detected: %s\n", buf);
79 sb->nbuttons = guess_num_buttons(buf);
80 printf("%d buttons\n", sb->nbuttons);
82 /* set binary mode and enable automatic data packet sending */
83 write(fd, "\rCB\rMSS\r", 8);
85 sb->parse = sball_parsepkt;
89 /* try as a magellan spacemouse */
90 if(stty_mag(fd) == -1) {
95 if((sz = read_timeout(fd, buf, sizeof buf - 1, 250000)) > 0 && buf[0] == 'v') {
96 make_printable(buf, sz);
97 printf("Magellan SpaceMouse detected:\n%s\n", buf);
99 sb->nbuttons = guess_num_buttons(buf);
100 printf("%d buttons\n", sb->nbuttons);
102 /* set 3D mode, not-dominant-axis, pass through motion and button packets */
103 write(fd, "m3\r", 3);
105 sb->parse = mag_parsepkt;
115 void sball_close(struct sball *sb)
121 int sball_fd(struct sball *sb)
126 int sball_read(struct sball *sb)
130 while((sz = read(sb->fd, sb->buf + sb->len, INP_BUF_SZ - sb->len - 1)) > 0) {
135 /* if we fill the input buffer, make a last attempt to parse it, and discard
136 * it so we can receive more
138 if(sb->len >= INP_BUF_SZ) {
146 int sball_axis(struct sball *sb, int axis)
148 return sb->mot[axis];
151 unsigned int sball_buttons(struct sball *sb)
156 int sball_num_buttons(struct sball *sb)
161 /* Labtec spaceball: 9600 8n1 XON/XOFF */
162 static int stty_sball(int fd)
166 if(tcgetattr(fd, &term) == -1) {
167 perror("sball_open: tcgetattr");
174 term.c_cc[VTIME] = 1;
176 term.c_cflag = CLOCAL | CREAD | CS8 | HUPCL;
177 term.c_iflag = IGNBRK | IGNPAR | IXON | IXOFF;
179 cfsetispeed(&term, B9600);
180 cfsetospeed(&term, B9600);
182 if(tcsetattr(fd, TCSANOW, &term) == -1) {
183 perror("sball_open: tcsetattr");
190 /* Logicad magellan spacemouse: 9600 8n2 CTS/RTS */
191 static int stty_mag(int fd)
195 if(tcgetattr(fd, &term) == -1) {
196 perror("sball_open: tcgetattr");
203 term.c_cc[VTIME] = 1;
205 term.c_cflag = CLOCAL | CREAD | CS8 | CSTOPB | HUPCL | CRTSCTS;
206 term.c_iflag = IGNBRK | IGNPAR;
208 cfsetispeed(&term, B9600);
209 cfsetospeed(&term, B9600);
211 if(tcsetattr(fd, TCSANOW, &term) == -1) {
212 perror("sball_open: tcsetattr");
220 static int proc_input(struct sball *sb)
223 char *bptr = sb->buf;
224 char *start = sb->buf;
225 char *end = sb->buf + sb->len;
227 /* see if we have a CR in the buffer */
231 sb->parse(sb, *start, start + 1, bptr - start - 1);
238 sz = start - sb->buf;
240 memmove(sb->buf, start, sz);
246 static int mag_parsepkt(struct sball *sb, int id, char *data, int len)
250 /*printf("magellan packet: %c - %s (%d bytes)\n", (char)id, data, len);*/
255 fprintf(stderr, "magellan: invalid data packet, expected 24 bytes, got: %d\n", len);
259 #ifdef SBALL_BIG_ENDIAN
260 sb->mot[i] = ((((int)data[3] & 0xf) << 12) | (((int)data[2] & 0xf) << 8) |
261 (((int)data[1] & 0xf) << 4) | (data[0] & 0xf)) - 0x8000;
263 sb->mot[i] = ((((int)data[0] & 0xf) << 12) | (((int)data[1] & 0xf) << 8) |
264 (((int)data[2] & 0xf) << 4) | (data[3] & 0xf)) - 0x8000;
273 fprintf(stderr, "magellan: invalid keyboard pakcet, expected 3 bytes, got: %d\n", len);
276 sb->keystate = (data[0] & 0xf) | ((data[1] & 0xf) << 4) | (((unsigned int)data[2] & 0xf) << 8);
282 fprintf(stderr, "magellan error: illegal command: %c%c\n", data[1], data[2]);
283 } else if(data[0] == 2) {
284 fprintf(stderr, "magellan error: framing error\n");
286 fprintf(stderr, "magellan error: unknown device error\n");
296 static int sball_parsepkt(struct sball *sb, int id, char *data, int len)
301 /* decode data packet, replacing escaped values with the correct ones */
303 while(rd < data + len) {
304 if((c = *rd++) == '^') {
307 *wr++ = 0x11; /* XON */
310 *wr++ = 0x13; /* XOFF */
319 fprintf(stderr, "sball decode: ignoring invalid escape code: %xh\n", (unsigned int)c);
325 len = wr - data; /* update the decoded length */
330 fprintf(stderr, "sball: invalid data packet, expected 14 bytes, got: %d\n", len);
334 #ifndef SBALL_BIG_ENDIAN
340 sb->mot[i] = *(short*)data;
343 memcpy(sb->mot, data + 2, 12);
350 fprintf(stderr, "sball: invalid key packet, expected 2 bytes, got: %d\n", len);
353 if(sb->flags & SB4000) break; /* ignore K packets from spaceball 4000 devices */
355 /* data[1] bits 0-3 -> buttons 0,1,2,3
356 * data[1] bits 4,5 (3003 L/R) -> buttons 0, 1
357 * data[0] bits 0-2 -> buttons 4,5,6
358 * data[0] bit 4 is (2003 pick) -> button 7
360 sb->keystate = (data[1] & 0xf) | ((data[1] >> 4) & 3) | ((data[0] & 7) << 4) |
361 ((data[0] & 0x10) >> 1);
367 fprintf(stderr, "sball: invalid sb4k key packet, expected 2 bytes, got: %d\n", len);
370 /* spaceball 4000 key packet */
372 /* update orientation flag (actually don't bother) */
377 sb->flags &= ~FLIPXY;
381 /* data[1] bits 0-5 -> buttons 0,1,2,3,4,5
382 * data[1] bit 7 -> button 6
383 * data[0] bits 0-4 -> buttons 7,8,9,10,11
385 sb->keystate = (data[1] & 0x3f) | ((data[1] & 0x80) >> 1) | ((data[0] & 0x1f) << 7);
390 fprintf(stderr, "sball: error:");
391 for(i=0; i<len; i++) {
392 if(isprint(data[i])) {
393 fprintf(stderr, " %c", data[i]);
395 fprintf(stderr, " %02xh", (unsigned int)data[i]);
400 case 'M': /* ignore MSS responses */
405 fprintf(stderr, "sball: got '%c' packet:", (char)id);
406 for(i=0; i<len; i++) {
407 fprintf(stderr, " %02x", (unsigned int)data[i]);
414 static int guess_num_buttons(const char *verstr)
417 const char *s, *model;
419 if((s = strstr(verstr, "Firmware version"))) { /* spaceball */
421 /* if we got a model number, guess based on that */
422 if((model = strchr(s, '('))) {
423 if(memcmp(model, "(Model ", 7) == 0) {
428 switch(atoi(model)) {
439 /* try to guess based on firmware number */
440 if(sscanf(s + 17, "%d.%d", &major, &minor) == 2 && major == 2) {
441 if(minor == 35 || minor == 62 || minor == 63) {
442 return 2; /* spaceball 3003/3003C */
444 if(minor == 42 || minor == 43 || minor == 45) {
445 /* 2.42 is also used by spaceball 2003C, but this should be
446 * caught before we get here by the model number guess
448 return 12; /* spaceball 4000flx/5000flx-a */
450 if(minor == 2 || minor == 13 || minor == 15) {
451 return 8; /* spaceball 1003/2003/2003c */
456 if(strstr(verstr, "MAGELLAN")) {
457 return 9; /* magellan spacemouse */
460 if(strstr(verstr, "SPACEBALL")) {
461 return 12; /* spaceball 5000 */
464 if(strstr(verstr, "CadMan")) {
468 fprintf(stderr, "Can't guess number of buttons, default to 8, report this as a bug!\n");
472 static void make_printable(char *buf, int len)
477 for(i=0; i<len; i++) {
481 while(*buf == '\n' || *buf == '\r') buf++;
489 static int read_timeout(int fd, char *buf, int bufsz, long tm_usec)
493 struct timeval tv0, tv;
496 if(!buf || bufsz <= 0) return -1;
499 gettimeofday(&tv0, 0);
501 while(sz < bufsz && usec > 0) {
502 tv.tv_sec = usec / 1000000;
503 tv.tv_usec = usec % 1000000;
507 if((res = select(fd + 1, &rdset, 0, 0, &tv)) > 0 && FD_ISSET(fd, &rdset)) {
508 sz += read(fd, buf + sz, bufsz - sz);
510 tm_usec = usec = 128000; /* wait 128ms for the rest of the message to appear */
511 gettimeofday(&tv0, 0);
514 if(res == -1 && (errno == EWOULDBLOCK || errno == EAGAIN)) {
517 gettimeofday(&tv, 0);
518 usec = tm_usec - ((tv.tv_sec - tv0.tv_sec) * 1000000 + (tv.tv_usec - tv0.tv_usec));
521 return sz > 0 ? sz : -1;
524 static void print_motion(short *mot)
526 printf(" T[%+6d %+6d %+6d] R[%+6d %+6d %+6d]", mot[0], mot[1],
527 mot[2], mot[3], mot[4], mot[5]);
530 static void print_keystate(unsigned int keystate)
534 for(i=0; i<12; i++) {
536 int hex = b < 10 ? b + '0' : b - 10 + 'a';
537 putchar(keystate & (1 << b) ? hex : '-');
541 static void print_state(struct sball *sb)
543 print_motion(sb->mot);
545 print_keystate(sb->keystate);