11 #include <sys/select.h>
13 #include <sys/ioctl.h>
15 #define INP_BUF_SZ 256
31 unsigned int keystate;
33 int (*parse)(struct sball*, int, char*, int);
36 static int stty_sball(int fd);
37 static int stty_mag(int fd);
39 static int proc_input(struct sball *sb);
41 static int mag_parsepkt(struct sball *sb, int id, char *data, int len);
42 static int sball_parsepkt(struct sball *sb, int id, char *data, int len);
44 static int guess_num_buttons(const char *verstr);
46 static void make_printable(char *buf, int len);
47 static int read_timeout(int fd, char *buf, int bufsz, long tm_usec);
49 static void print_state(struct sball *sb);
52 struct sball *sball_open(const char *dev)
58 if((fd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK)) == -1) {
59 fprintf(stderr, "sball_open: failed to open device: %s: %s\n", dev, strerror(errno));
63 if(!(sb = malloc(sizeof *sb))) {
64 fprintf(stderr, "sball_open: failed to allocate sball object\n");
71 if(stty_sball(fd) == -1) {
74 write(fd, "\r@RESET\r", 8);
76 if((sz = read_timeout(fd, buf, sizeof buf - 1, 2000000)) > 0 && strstr(buf, "\r@1")) {
77 /* we got a response, so it's a spaceball */
78 make_printable(buf, sz);
79 printf("Spaceball detected: %s\n", buf);
81 sb->nbuttons = guess_num_buttons(buf);
82 printf("%d buttons\n", sb->nbuttons);
84 /* set binary mode and enable automatic data packet sending */
85 write(fd, "\rCB\rMSSV\r", 9);
87 sb->parse = sball_parsepkt;
91 /* try as a magellan spacemouse */
92 if(stty_mag(fd) == -1) {
97 if((sz = read_timeout(fd, buf, sizeof buf - 1, 250000)) > 0 && buf[0] == 'v') {
98 make_printable(buf, sz);
99 printf("Magellan SpaceMouse detected:\n%s\n", buf);
101 sb->nbuttons = guess_num_buttons(buf);
102 printf("%d buttons\n", sb->nbuttons);
104 /* set 3D mode, not-dominant-axis, pass through motion and button packets */
105 write(fd, "m3\r", 3);
107 sb->parse = mag_parsepkt;
117 void sball_close(struct sball *sb)
123 int sball_fd(struct sball *sb)
128 int sball_read(struct sball *sb)
132 while((sz = read(sb->fd, sb->buf + sb->len, INP_BUF_SZ - sb->len - 1)) > 0) {
137 /* if we fill the input buffer, make a last attempt to parse it, and discard
138 * it so we can receive more
140 if(sb->len >= INP_BUF_SZ) {
148 int sball_axis(struct sball *sb, int axis)
150 return sb->mot[axis];
153 unsigned int sball_buttons(struct sball *sb)
158 int sball_num_buttons(struct sball *sb)
163 /* Labtec spaceball: 9600 8n1 XON/XOFF */
164 static int stty_sball(int fd)
169 if(tcgetattr(fd, &term) == -1) {
170 perror("sball_open: tcgetattr");
175 term.c_lflag = ICANON;
177 term.c_cc[VTIME] = 0;
179 term.c_cc[VEOL] = '\r';
180 term.c_cc[VEOL2] = 0;
181 term.c_cc[VERASE] = 0;
182 term.c_cc[VKILL] = 0;
184 term.c_cflag = CLOCAL | CREAD | CS8 | HUPCL;
185 term.c_iflag = IGNBRK | IGNPAR;
187 cfsetispeed(&term, B9600);
188 cfsetospeed(&term, B9600);
190 if(tcsetattr(fd, TCSANOW, &term) == -1) {
191 perror("sball_open: tcsetattr");
195 if(ioctl(fd, TIOCMGET, &status) != -1) {
196 status |= TIOCM_DTR | TIOCM_RTS;
197 ioctl(fd, TIOCMSET, &status);
202 /* Logicad magellan spacemouse: 9600 8n2 CTS/RTS */
203 static int stty_mag(int fd)
208 if(tcgetattr(fd, &term) == -1) {
209 perror("sball_open: tcgetattr");
214 term.c_lflag = ICANON;
216 term.c_cc[VTIME] = 0;
218 term.c_cc[VEOL] = '\r';
219 term.c_cc[VEOL2] = 0;
220 term.c_cc[VERASE] = 0;
221 term.c_cc[VKILL] = 0;
223 term.c_cflag = CLOCAL | CREAD | CS8 | CSTOPB | HUPCL | CRTSCTS;
224 term.c_iflag = IGNBRK | IGNPAR;
226 cfsetispeed(&term, B9600);
227 cfsetospeed(&term, B9600);
229 if(tcsetattr(fd, TCSAFLUSH, &term) == -1) {
230 perror("sball_open: tcsetattr");
233 tcflush(fd, TCIOFLUSH);
235 if(ioctl(fd, TIOCMGET, &status) != -1) {
236 status |= TIOCM_DTR | TIOCM_RTS;
237 ioctl(fd, TIOCMGET, &status);
243 static int proc_input(struct sball *sb)
246 char *bptr = sb->buf;
247 char *start = sb->buf;
248 char *end = sb->buf + sb->len;
250 /* see if we have a CR in the buffer */
254 sb->parse(sb, *start, start + 1, bptr - start - 1);
261 sz = start - sb->buf;
263 memmove(sb->buf, start, sz);
269 static int mag_parsepkt(struct sball *sb, int id, char *data, int len)
273 /*printf("magellan packet: %c - %s (%d bytes)\n", (char)id, data, len);*/
278 fprintf(stderr, "magellan: invalid data packet, expected 24 bytes, got: %d\n", len);
282 #ifdef SBALL_BIG_ENDIAN
283 sb->mot[i] = ((((int)data[3] & 0xf) << 12) | (((int)data[2] & 0xf) << 8) |
284 (((int)data[1] & 0xf) << 4) | (data[0] & 0xf)) - 0x8000;
286 sb->mot[i] = ((((int)data[0] & 0xf) << 12) | (((int)data[1] & 0xf) << 8) |
287 (((int)data[2] & 0xf) << 4) | (data[3] & 0xf)) - 0x8000;
296 fprintf(stderr, "magellan: invalid keyboard pakcet, expected 3 bytes, got: %d\n", len);
299 sb->keystate = (data[0] & 0xf) | ((data[1] & 0xf) << 4) | (((unsigned int)data[2] & 0xf) << 8);
305 fprintf(stderr, "magellan error: illegal command: %c%c\n", data[1], data[2]);
306 } else if(data[0] == 2) {
307 fprintf(stderr, "magellan error: framing error\n");
309 fprintf(stderr, "magellan error: unknown device error\n");
319 static int sball_parsepkt(struct sball *sb, int id, char *data, int len)
324 /* decode data packet, replacing escaped values with the correct ones */
326 while(rd < data + len) {
327 if((c = *rd++) == '^') {
330 *wr++ = 0x11; /* XON */
333 *wr++ = 0x13; /* XOFF */
342 fprintf(stderr, "sball decode: ignoring invalid escape code: %xh\n", (unsigned int)c);
348 len = wr - data; /* update the decoded length */
353 fprintf(stderr, "sball: invalid data packet, expected 14 bytes, got: %d\n", len);
357 #ifndef SBALL_BIG_ENDIAN
363 sb->mot[i] = *(short*)data;
366 memcpy(sb->mot, data + 2, 12);
373 fprintf(stderr, "sball: invalid key packet, expected 2 bytes, got: %d\n", len);
376 if(sb->flags & SB4000) break; /* ignore K packets from spaceball 4000 devices */
378 /* data[1] bits 0-3 -> buttons 0,1,2,3
379 * data[1] bits 4,5 (3003 L/R) -> buttons 0, 1
380 * data[0] bits 0-2 -> buttons 4,5,6
381 * data[0] bit 4 is (2003 pick) -> button 7
383 sb->keystate = (data[1] & 0xf) | ((data[1] >> 4) & 3) | ((data[0] & 7) << 4) |
384 ((data[0] & 0x10) >> 1);
390 fprintf(stderr, "sball: invalid sb4k key packet, expected 2 bytes, got: %d\n", len);
393 /* spaceball 4000 key packet */
395 /* update orientation flag (actually don't bother) */
400 sb->flags &= ~FLIPXY;
404 /* data[1] bits 0-5 -> buttons 0,1,2,3,4,5
405 * data[1] bit 7 -> button 6
406 * data[0] bits 0-4 -> buttons 7,8,9,10,11
408 sb->keystate = (data[1] & 0x3f) | ((data[1] & 0x80) >> 1) | ((data[0] & 0x1f) << 7);
413 fprintf(stderr, "sball: error:");
414 for(i=0; i<len; i++) {
415 if(isprint(data[i])) {
416 fprintf(stderr, " %c", data[i]);
418 fprintf(stderr, " %02xh", (unsigned int)data[i]);
423 case 'M': /* ignore MSS responses */
428 fprintf(stderr, "sball: got '%c' packet:", (char)id);
429 for(i=0; i<len; i++) {
430 fprintf(stderr, " %02x", (unsigned int)data[i]);
437 static int guess_num_buttons(const char *verstr)
440 const char *s, *model;
442 if((s = strstr(verstr, "Firmware version"))) { /* spaceball */
444 /* if we got a model number, guess based on that */
445 if((model = strchr(s, '('))) {
446 if(memcmp(model, "(Model ", 7) == 0) {
451 switch(atoi(model)) {
462 /* try to guess based on firmware number */
463 if(sscanf(s + 17, "%d.%d", &major, &minor) == 2 && major == 2) {
464 if(minor == 35 || minor == 62 || minor == 63) {
465 return 2; /* spaceball 3003/3003C */
467 if(minor == 42 || minor == 43 || minor == 45) {
468 /* 2.42 is also used by spaceball 2003C, but this should be
469 * caught before we get here by the model number guess
471 return 12; /* spaceball 4000flx/5000flx-a */
473 if(minor == 2 || minor == 13 || minor == 15) {
474 return 8; /* spaceball 1003/2003/2003c */
479 if(strstr(verstr, "MAGELLAN")) {
480 return 9; /* magellan spacemouse */
483 if(strstr(verstr, "SPACEBALL")) {
484 return 12; /* spaceball 5000 */
487 if(strstr(verstr, "CadMan")) {
491 fprintf(stderr, "Can't guess number of buttons, default to 8, report this as a bug!\n");
495 static void make_printable(char *buf, int len)
500 for(i=0; i<len; i++) {
504 while(*buf == '\n' || *buf == '\r') buf++;
512 static int read_timeout(int fd, char *buf, int bufsz, long tm_usec)
516 struct timeval tv0, tv;
519 if(!buf || bufsz <= 0) return -1;
522 gettimeofday(&tv0, 0);
524 while(sz < bufsz && usec > 0) {
525 tv.tv_sec = usec / 1000000;
526 tv.tv_usec = usec % 1000000;
530 if((res = select(fd + 1, &rdset, 0, 0, &tv)) > 0 && FD_ISSET(fd, &rdset)) {
531 sz += read(fd, buf + sz, bufsz - sz);
533 tm_usec = usec = 128000; /* wait 128ms for the rest of the message to appear */
534 gettimeofday(&tv0, 0);
537 if(res == -1 && (errno == EWOULDBLOCK || errno == EAGAIN)) {
540 gettimeofday(&tv, 0);
541 usec = tm_usec - ((tv.tv_sec - tv0.tv_sec) * 1000000 + (tv.tv_usec - tv0.tv_usec));
544 return sz > 0 ? sz : -1;
547 static void print_motion(short *mot)
549 printf(" T[%+6d %+6d %+6d] R[%+6d %+6d %+6d]", mot[0], mot[1],
550 mot[2], mot[3], mot[4], mot[5]);
553 static void print_keystate(unsigned int keystate)
557 for(i=0; i<12; i++) {
559 int hex = b < 10 ? b + '0' : b - 10 + 'a';
560 putchar(keystate & (1 << b) ? hex : '-');
564 static void print_state(struct sball *sb)
566 print_motion(sb->mot);
568 print_keystate(sb->keystate);