10 #include <sys/select.h>
12 #include <sys/ioctl.h>
14 #define INP_BUF_SZ 256
30 unsigned int keystate;
32 struct termios saved_term;
35 int (*parse)(struct sball*, int, char*, int);
38 static int stty_sball(struct sball *sb);
39 static int stty_mag(struct sball *sb);
40 static void stty_save(struct sball *sb);
41 static void stty_restore(struct sball *sb);
43 static int proc_input(struct sball *sb);
45 static int mag_parsepkt(struct sball *sb, int id, char *data, int len);
46 static int sball_parsepkt(struct sball *sb, int id, char *data, int len);
48 static int guess_num_buttons(const char *verstr);
50 static void make_printable(char *buf, int len);
51 static int read_timeout(int fd, char *buf, int bufsz, long tm_usec);
53 static void print_state(struct sball *sb);
56 struct sball *sball_open(const char *dev)
62 if((fd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK)) == -1) {
63 fprintf(stderr, "sball_open: failed to open device: %s: %s\n", dev, strerror(errno));
67 if(!(sb = malloc(sizeof *sb))) {
68 fprintf(stderr, "sball_open: failed to allocate sball object\n");
77 if(stty_sball(sb) == -1) {
80 write(fd, "\r@RESET\r", 8);
82 if((sz = read_timeout(fd, buf, sizeof buf - 1, 2000000)) > 0 && strstr(buf, "\r@1")) {
83 /* we got a response, so it's a spaceball */
84 make_printable(buf, sz);
85 printf("Spaceball detected: %s\n", buf);
87 sb->nbuttons = guess_num_buttons(buf);
88 printf("%d buttons\n", sb->nbuttons);
90 /* set binary mode and enable automatic data packet sending */
91 write(fd, "\rCB\rMSSV\r", 9);
93 sb->parse = sball_parsepkt;
97 /* try as a magellan spacemouse */
98 if(stty_mag(sb) == -1) {
101 write(fd, "vQ\r", 3);
103 if((sz = read_timeout(fd, buf, sizeof buf - 1, 250000)) > 0 && buf[0] == 'v') {
104 make_printable(buf, sz);
105 printf("Magellan SpaceMouse detected:\n%s\n", buf);
107 sb->nbuttons = guess_num_buttons(buf);
108 printf("%d buttons\n", sb->nbuttons);
110 /* set 3D mode, not-dominant-axis, pass through motion and button packets */
111 write(fd, "m3\r", 3);
113 sb->parse = mag_parsepkt;
124 void sball_close(struct sball *sb)
132 int sball_fd(struct sball *sb)
137 int sball_read(struct sball *sb)
141 while((sz = read(sb->fd, sb->buf + sb->len, INP_BUF_SZ - sb->len - 1)) > 0) {
146 /* if we fill the input buffer, make a last attempt to parse it, and discard
147 * it so we can receive more
149 if(sb->len >= INP_BUF_SZ) {
157 int sball_axis(struct sball *sb, int axis)
159 return sb->mot[axis];
162 unsigned int sball_buttons(struct sball *sb)
167 int sball_num_buttons(struct sball *sb)
172 /* Labtec spaceball: 9600 8n1 XON/XOFF */
173 static int stty_sball(struct sball *sb)
178 term = sb->saved_term;
180 term.c_lflag = ICANON;
182 term.c_cc[VTIME] = 0;
184 term.c_cc[VEOL] = '\r';
185 term.c_cc[VEOL2] = 0;
186 term.c_cc[VERASE] = 0;
187 term.c_cc[VKILL] = 0;
189 term.c_cflag = CLOCAL | CREAD | CS8 | HUPCL;
190 term.c_iflag = IGNBRK | IGNPAR;
192 cfsetispeed(&term, B9600);
193 cfsetospeed(&term, B9600);
195 if(tcsetattr(sb->fd, TCSAFLUSH, &term) == -1) {
196 perror("sball_open: tcsetattr");
199 tcflush(sb->fd, TCIOFLUSH);
201 mstat = sb->saved_mstat | TIOCM_DTR | TIOCM_RTS;
202 ioctl(sb->fd, TIOCMGET, &mstat);
206 /* Logicad magellan spacemouse: 9600 8n2 CTS/RTS */
207 static int stty_mag(struct sball *sb)
212 term = sb->saved_term;
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(sb->fd, TCSAFLUSH, &term) == -1) {
230 perror("sball_open: tcsetattr");
233 tcflush(sb->fd, TCIOFLUSH);
235 mstat = sb->saved_mstat | TIOCM_DTR | TIOCM_RTS;
236 ioctl(sb->fd, TIOCMGET, &mstat);
240 static void stty_save(struct sball *sb)
242 tcgetattr(sb->fd, &sb->saved_term);
243 ioctl(sb->fd, TIOCMGET, &sb->saved_mstat);
246 static void stty_restore(struct sball *sb)
248 tcsetattr(sb->fd, TCSAFLUSH, &sb->saved_term);
249 tcflush(sb->fd, TCIOFLUSH);
250 ioctl(sb->fd, TIOCMSET, &sb->saved_mstat);
254 static int proc_input(struct sball *sb)
257 char *bptr = sb->buf;
258 char *start = sb->buf;
259 char *end = sb->buf + sb->len;
261 /* see if we have a CR in the buffer */
265 sb->parse(sb, *start, start + 1, bptr - start - 1);
272 sz = start - sb->buf;
274 memmove(sb->buf, start, sz);
280 static int mag_parsepkt(struct sball *sb, int id, char *data, int len)
284 /*printf("magellan packet: %c - %s (%d bytes)\n", (char)id, data, len);*/
289 fprintf(stderr, "magellan: invalid data packet, expected 24 bytes, got: %d\n", len);
293 #ifdef SBALL_BIG_ENDIAN
294 sb->mot[i] = ((((int)data[3] & 0xf) << 12) | (((int)data[2] & 0xf) << 8) |
295 (((int)data[1] & 0xf) << 4) | (data[0] & 0xf)) - 0x8000;
297 sb->mot[i] = ((((int)data[0] & 0xf) << 12) | (((int)data[1] & 0xf) << 8) |
298 (((int)data[2] & 0xf) << 4) | (data[3] & 0xf)) - 0x8000;
307 fprintf(stderr, "magellan: invalid keyboard pakcet, expected 3 bytes, got: %d\n", len);
310 sb->keystate = (data[0] & 0xf) | ((data[1] & 0xf) << 4) | (((unsigned int)data[2] & 0xf) << 8);
316 fprintf(stderr, "magellan error: illegal command: %c%c\n", data[1], data[2]);
317 } else if(data[0] == 2) {
318 fprintf(stderr, "magellan error: framing error\n");
320 fprintf(stderr, "magellan error: unknown device error\n");
330 static int sball_parsepkt(struct sball *sb, int id, char *data, int len)
335 /* decode data packet, replacing escaped values with the correct ones */
337 while(rd < data + len) {
338 if((c = *rd++) == '^') {
341 *wr++ = 0x11; /* XON */
344 *wr++ = 0x13; /* XOFF */
353 fprintf(stderr, "sball decode: ignoring invalid escape code: %xh\n", (unsigned int)c);
359 len = wr - data; /* update the decoded length */
364 fprintf(stderr, "sball: invalid data packet, expected 14 bytes, got: %d\n", len);
368 #ifndef SBALL_BIG_ENDIAN
374 sb->mot[i] = *(short*)data;
377 memcpy(sb->mot, data + 2, 12);
384 fprintf(stderr, "sball: invalid key packet, expected 2 bytes, got: %d\n", len);
387 if(sb->flags & SB4000) break; /* ignore K packets from spaceball 4000 devices */
389 /* data[1] bits 0-3 -> buttons 0,1,2,3
390 * data[1] bits 4,5 (3003 L/R) -> buttons 0, 1
391 * data[0] bits 0-2 -> buttons 4,5,6
392 * data[0] bit 4 is (2003 pick) -> button 7
394 sb->keystate = (data[1] & 0xf) | ((data[1] >> 4) & 3) | ((data[0] & 7) << 4) |
395 ((data[0] & 0x10) >> 1);
401 fprintf(stderr, "sball: invalid sb4k key packet, expected 2 bytes, got: %d\n", len);
404 /* spaceball 4000 key packet */
406 /* update orientation flag (actually don't bother) */
411 sb->flags &= ~FLIPXY;
415 /* data[1] bits 0-5 -> buttons 0,1,2,3,4,5
416 * data[1] bit 7 -> button 6
417 * data[0] bits 0-4 -> buttons 7,8,9,10,11
419 sb->keystate = (data[1] & 0x3f) | ((data[1] & 0x80) >> 1) | ((data[0] & 0x1f) << 7);
424 fprintf(stderr, "sball: error:");
425 for(i=0; i<len; i++) {
426 if(isprint(data[i])) {
427 fprintf(stderr, " %c", data[i]);
429 fprintf(stderr, " %02xh", (unsigned int)data[i]);
434 case 'M': /* ignore MSS responses */
439 fprintf(stderr, "sball: got '%c' packet:", (char)id);
440 for(i=0; i<len; i++) {
441 fprintf(stderr, " %02x", (unsigned int)data[i]);
448 static int guess_num_buttons(const char *verstr)
451 const char *s, *model;
453 if((s = strstr(verstr, "Firmware version"))) { /* spaceball */
455 /* if we got a model number, guess based on that */
456 if((model = strchr(s, '('))) {
457 if(memcmp(model, "(Model ", 7) == 0) {
462 switch(atoi(model)) {
473 /* try to guess based on firmware number */
474 if(sscanf(s + 17, "%d.%d", &major, &minor) == 2 && major == 2) {
475 if(minor == 35 || minor == 62 || minor == 63) {
476 return 2; /* spaceball 3003/3003C */
478 if(minor == 42 || minor == 43 || minor == 45) {
479 /* 2.42 is also used by spaceball 2003C, but this should be
480 * caught before we get here by the model number guess
482 return 12; /* spaceball 4000flx/5000flx-a */
484 if(minor == 2 || minor == 13 || minor == 15) {
485 return 8; /* spaceball 1003/2003/2003c */
490 if(strstr(verstr, "MAGELLAN")) {
491 return 9; /* magellan spacemouse */
494 if(strstr(verstr, "SPACEBALL")) {
495 return 12; /* spaceball 5000 */
498 if(strstr(verstr, "CadMan")) {
502 fprintf(stderr, "Can't guess number of buttons, default to 8, report this as a bug!\n");
506 static void make_printable(char *buf, int len)
511 for(i=0; i<len; i++) {
515 while(*buf == '\n' || *buf == '\r') buf++;
523 static int read_timeout(int fd, char *buf, int bufsz, long tm_usec)
527 struct timeval tv0, tv;
530 if(!buf || bufsz <= 0) return -1;
533 gettimeofday(&tv0, 0);
535 while(sz < bufsz && usec > 0) {
536 tv.tv_sec = usec / 1000000;
537 tv.tv_usec = usec % 1000000;
541 if((res = select(fd + 1, &rdset, 0, 0, &tv)) > 0 && FD_ISSET(fd, &rdset)) {
542 sz += read(fd, buf + sz, bufsz - sz);
544 tm_usec = usec = 128000; /* wait 128ms for the rest of the message to appear */
545 gettimeofday(&tv0, 0);
548 if(res == -1 && (errno == EWOULDBLOCK || errno == EAGAIN)) {
551 gettimeofday(&tv, 0);
552 usec = tm_usec - ((tv.tv_sec - tv0.tv_sec) * 1000000 + (tv.tv_usec - tv0.tv_usec));
555 return sz > 0 ? sz : -1;
558 static void print_motion(short *mot)
560 printf(" T[%+6d %+6d %+6d] R[%+6d %+6d %+6d]", mot[0], mot[1],
561 mot[2], mot[3], mot[4], mot[5]);
564 static void print_keystate(unsigned int keystate)
568 for(i=0; i<12; i++) {
570 int hex = b < 10 ? b + '0' : b - 10 + 'a';
571 putchar(keystate & (1 << b) ? hex : '-');
575 static void print_state(struct sball *sb)
577 print_motion(sb->mot);
579 print_keystate(sb->keystate);