10 #include <sys/select.h>
12 #include <sys/ioctl.h>
14 #if defined(__i386__) || defined(__ia64__) || defined(WIN32) || \
15 (defined(__alpha__) || defined(__alpha)) || \
17 (defined(__mips__) && defined(__MIPSEL__)) || \
18 defined(__SYMBIAN32__) || \
19 defined(__x86_64__) || \
20 defined(__LITTLE_ENDIAN__)
21 #define SBALL_LITTLE_ENDIAN
23 #define SBALL_BIG_ENDIAN
26 #define INP_BUF_SZ 256
42 unsigned int keystate;
44 struct termios saved_term;
47 int (*parse)(struct sball*, int, char*, int);
50 static int stty_sball(struct sball *sb);
51 static int stty_mag(struct sball *sb);
52 static void stty_save(struct sball *sb);
53 static void stty_restore(struct sball *sb);
55 static int proc_input(struct sball *sb);
57 static int mag_parsepkt(struct sball *sb, int id, char *data, int len);
58 static int sball_parsepkt(struct sball *sb, int id, char *data, int len);
60 static int guess_num_buttons(const char *verstr);
62 static void make_printable(char *buf, int len);
63 static int read_timeout(int fd, char *buf, int bufsz, long tm_usec);
65 static void print_state(struct sball *sb);
68 struct sball *sball_open(const char *dev)
74 if((fd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK)) == -1) {
75 fprintf(stderr, "sball_open: failed to open device: %s: %s\n", dev, strerror(errno));
79 if(!(sb = calloc(1, sizeof *sb))) {
80 fprintf(stderr, "sball_open: failed to allocate sball object\n");
87 if(stty_sball(sb) == -1) {
90 write(fd, "\r@RESET\r", 8);
92 if((sz = read_timeout(fd, buf, sizeof buf - 1, 2000000)) > 0 && strstr(buf, "\r@1")) {
93 /* we got a response, so it's a spaceball */
94 make_printable(buf, sz);
95 printf("Spaceball detected: %s\n", buf);
97 sb->nbuttons = guess_num_buttons(buf);
98 printf("%d buttons\n", sb->nbuttons);
100 /* set binary mode and enable automatic data packet sending */
101 write(fd, "\rCB\rMSSV\r", 9);
103 sb->parse = sball_parsepkt;
107 /* try as a magellan spacemouse */
108 if(stty_mag(sb) == -1) {
111 write(fd, "vQ\r", 3);
113 if((sz = read_timeout(fd, buf, sizeof buf - 1, 250000)) > 0 && buf[0] == 'v') {
114 make_printable(buf, sz);
115 printf("Magellan SpaceMouse detected:\n%s\n", buf);
117 sb->nbuttons = guess_num_buttons(buf);
118 printf("%d buttons\n", sb->nbuttons);
120 /* set 3D mode, not-dominant-axis, pass through motion and button packets */
121 write(fd, "m3\r", 3);
123 sb->parse = mag_parsepkt;
134 void sball_close(struct sball *sb)
142 int sball_fd(struct sball *sb)
147 int sball_read(struct sball *sb)
151 while((sz = read(sb->fd, sb->buf + sb->len, INP_BUF_SZ - sb->len - 1)) > 0) {
156 /* if we fill the input buffer, make a last attempt to parse it, and discard
157 * it so we can receive more
159 if(sb->len >= INP_BUF_SZ) {
167 int sball_axis(struct sball *sb, int axis)
169 return sb->mot[axis];
172 unsigned int sball_buttons(struct sball *sb)
177 int sball_num_buttons(struct sball *sb)
182 /* Labtec spaceball: 9600 8n1 XON/XOFF
183 * Can't use canonical mode to assemble input into lines for the spaceball,
184 * because binary data received for motion events can include newlines which
185 * would be eaten up by the line discipline. Therefore we'll rely on VTIME=1 to
186 * hopefully get more than 1 byte at a time. Alternatively we could request
187 * printable reports, but I don't feel like implementing that.
189 static int stty_sball(struct sball *sb)
194 term = sb->saved_term;
198 term.c_cc[VTIME] = 1;
200 term.c_cflag = CLOCAL | CREAD | CS8 | HUPCL;
201 term.c_iflag = IGNBRK | IGNPAR | IXON | IXOFF;
203 cfsetispeed(&term, B9600);
204 cfsetospeed(&term, B9600);
206 if(tcsetattr(sb->fd, TCSAFLUSH, &term) == -1) {
207 perror("sball_open: tcsetattr");
210 tcflush(sb->fd, TCIOFLUSH);
212 mstat = sb->saved_mstat | TIOCM_DTR | TIOCM_RTS;
213 ioctl(sb->fd, TIOCMGET, &mstat);
217 /* Logicad magellan spacemouse: 9600 8n2 CTS/RTS
218 * Since the magellan devices don't seem to send any newlines, we can rely on
219 * canonical mode to feed us nice whole lines at a time.
221 static int stty_mag(struct sball *sb)
226 term = sb->saved_term;
228 term.c_lflag = ICANON;
230 term.c_cc[VTIME] = 0;
232 term.c_cc[VEOL] = '\r';
233 term.c_cc[VEOL2] = 0;
234 term.c_cc[VERASE] = 0;
235 term.c_cc[VKILL] = 0;
237 term.c_cflag = CLOCAL | CREAD | CS8 | CSTOPB | HUPCL;
239 term.c_cflag |= CCTS_OFLOW;
240 #elif defined(CRTSCTS)
241 term.c_cflag |= CRTSCTS;
243 term.c_iflag = IGNBRK | IGNPAR;
245 cfsetispeed(&term, B9600);
246 cfsetospeed(&term, B9600);
248 if(tcsetattr(sb->fd, TCSAFLUSH, &term) == -1) {
249 perror("sball_open: tcsetattr");
252 tcflush(sb->fd, TCIOFLUSH);
254 mstat = sb->saved_mstat | TIOCM_DTR | TIOCM_RTS;
255 ioctl(sb->fd, TIOCMGET, &mstat);
259 static void stty_save(struct sball *sb)
261 tcgetattr(sb->fd, &sb->saved_term);
262 ioctl(sb->fd, TIOCMGET, &sb->saved_mstat);
265 static void stty_restore(struct sball *sb)
267 tcsetattr(sb->fd, TCSAFLUSH, &sb->saved_term);
268 tcflush(sb->fd, TCIOFLUSH);
269 ioctl(sb->fd, TIOCMSET, &sb->saved_mstat);
273 static int proc_input(struct sball *sb)
276 char *bptr = sb->buf;
277 char *start = sb->buf;
278 char *end = sb->buf + sb->len;
280 /* see if we have a CR in the buffer */
284 sb->parse(sb, *start, start + 1, bptr - start - 1);
291 sz = start - sb->buf;
293 memmove(sb->buf, start, sz);
299 static int mag_parsepkt(struct sball *sb, int id, char *data, int len)
303 /*printf("magellan packet: %c - %s (%d bytes)\n", (char)id, data, len);*/
308 fprintf(stderr, "magellan: invalid data packet, expected 24 bytes, got: %d\n", len);
312 sb->mot[i] = ((((int)data[0] & 0xf) << 12) | (((int)data[1] & 0xf) << 8) |
313 (((int)data[2] & 0xf) << 4) | (data[3] & 0xf)) - 0x8000;
321 fprintf(stderr, "magellan: invalid keyboard pakcet, expected 3 bytes, got: %d\n", len);
324 sb->keystate = (data[0] & 0xf) | ((data[1] & 0xf) << 4) | (((unsigned int)data[2] & 0xf) << 8);
330 fprintf(stderr, "magellan error: illegal command: %c%c\n", data[1], data[2]);
331 } else if(data[0] == 2) {
332 fprintf(stderr, "magellan error: framing error\n");
334 fprintf(stderr, "magellan error: unknown device error\n");
344 static int sball_parsepkt(struct sball *sb, int id, char *data, int len)
349 /* decode data packet, replacing escaped values with the correct ones */
351 while(rd < data + len) {
352 if((c = *rd++) == '^') {
355 *wr++ = 0x11; /* XON */
358 *wr++ = 0x13; /* XOFF */
367 fprintf(stderr, "sball decode: ignoring invalid escape code: %xh\n", (unsigned int)c);
373 len = wr - data; /* update the decoded length */
378 fprintf(stderr, "sball: invalid data packet, expected 14 bytes, got: %d\n", len);
382 #ifndef SBALL_BIG_ENDIAN
391 memcpy(sb->mot, data + 2, 12);
397 fprintf(stderr, "sball: invalid key packet, expected 2 bytes, got: %d\n", len);
400 if(sb->flags & SB4000) break; /* ignore K packets from spaceball 4000 devices */
402 /* data[1] bits 0-3 -> buttons 0,1,2,3
403 * data[1] bits 4,5 (3003 L/R) -> buttons 0, 1
404 * data[0] bits 0-2 -> buttons 4,5,6
405 * data[0] bit 4 is (2003 pick) -> button 7
407 sb->keystate = (data[1] & 0xf) | ((data[1] >> 4) & 3) | ((data[0] & 7) << 4) |
408 ((data[0] & 0x10) >> 1);
414 fprintf(stderr, "sball: invalid sb4k key packet, expected 2 bytes, got: %d\n", len);
417 /* spaceball 4000 key packet */
419 /* update orientation flag (actually don't bother) */
424 sb->flags &= ~FLIPXY;
428 /* data[1] bits 0-5 -> buttons 0,1,2,3,4,5
429 * data[1] bit 7 -> button 6
430 * data[0] bits 0-4 -> buttons 7,8,9,10,11
432 sb->keystate = (data[1] & 0x3f) | ((data[1] & 0x80) >> 1) | ((data[0] & 0x1f) << 7);
437 fprintf(stderr, "sball: error:");
438 for(i=0; i<len; i++) {
439 if(isprint((int)data[i])) {
440 fprintf(stderr, " %c", data[i]);
442 fprintf(stderr, " %02xh", (unsigned int)data[i]);
447 case 'M': /* ignore MSS responses */
452 fprintf(stderr, "sball: got '%c' packet:", (char)id);
453 for(i=0; i<len; i++) {
454 fprintf(stderr, " %02x", (unsigned int)data[i]);
461 static int guess_num_buttons(const char *verstr)
464 const char *s, *model;
466 if((s = strstr(verstr, "Firmware version"))) { /* spaceball */
468 /* if we got a model number, guess based on that */
469 if((model = strchr(s, '('))) {
470 if(memcmp(model, "(Model ", 7) == 0) {
475 switch(atoi(model)) {
486 /* try to guess based on firmware number */
487 if(sscanf(s + 17, "%d.%d", &major, &minor) == 2 && major == 2) {
488 if(minor == 35 || minor == 62 || minor == 63) {
489 return 2; /* spaceball 3003/3003C */
491 if(minor == 42 || minor == 43 || minor == 45) {
492 /* 2.42 is also used by spaceball 2003C, but this should be
493 * caught before we get here by the model number guess
495 return 12; /* spaceball 4000flx/5000flx-a */
497 if(minor == 2 || minor == 13 || minor == 15) {
498 return 8; /* spaceball 1003/2003/2003c */
503 if(strstr(verstr, "MAGELLAN")) {
504 return 9; /* magellan spacemouse */
507 if(strstr(verstr, "SPACEBALL")) {
508 return 12; /* spaceball 5000 */
511 if(strstr(verstr, "CadMan")) {
515 fprintf(stderr, "Can't guess number of buttons, default to 8, report this as a bug!\n");
519 static void make_printable(char *buf, int len)
524 for(i=0; i<len; i++) {
528 while(*buf == '\n' || *buf == '\r') buf++;
536 static int read_timeout(int fd, char *buf, int bufsz, long tm_usec)
540 struct timeval tv0, tv;
543 if(!buf || bufsz <= 0) return -1;
546 gettimeofday(&tv0, 0);
548 while(sz < bufsz && usec > 0) {
549 tv.tv_sec = usec / 1000000;
550 tv.tv_usec = usec % 1000000;
554 if((res = select(fd + 1, &rdset, 0, 0, &tv)) > 0 && FD_ISSET(fd, &rdset)) {
555 sz += read(fd, buf + sz, bufsz - sz);
557 tm_usec = usec = 128000; /* wait 128ms for the rest of the message to appear */
558 gettimeofday(&tv0, 0);
561 if(res == -1 && (errno == EWOULDBLOCK || errno == EAGAIN)) {
564 gettimeofday(&tv, 0);
565 usec = tm_usec - ((tv.tv_sec - tv0.tv_sec) * 1000000 + (tv.tv_usec - tv0.tv_usec));
568 return sz > 0 ? sz : -1;
571 static void print_motion(short *mot)
573 printf(" T[%+6d %+6d %+6d] R[%+6d %+6d %+6d]", mot[0], mot[1],
574 mot[2], mot[3], mot[4], mot[5]);
577 static void print_keystate(unsigned int keystate)
581 for(i=0; i<12; i++) {
583 int hex = b < 10 ? b + '0' : b - 10 + 'a';
584 putchar(keystate & (1 << b) ? hex : '-');
588 static void print_state(struct sball *sb)
590 print_motion(sb->mot);
592 print_keystate(sb->keystate);