1 /* Serial spaceball and space-mouse example driver code for UNIX systems.
2 * Tested with Spaceball 4000FLX and Magellan SpaceMouse on GNU/Linux, FreeBSD,
3 * SGI/IRIX, and SunOS (Solaris/Illumos).
5 * Author: John Tsiombikas <nuclear@member.fsf.org>
6 * No copyright, public domain.
17 #include <sys/select.h>
19 #include <sys/ioctl.h>
21 #if defined(__i386__) || defined(__ia64__) || defined(WIN32) || \
22 (defined(__alpha__) || defined(__alpha)) || \
24 (defined(__mips__) && defined(__MIPSEL__)) || \
25 defined(__SYMBIAN32__) || \
26 defined(__x86_64__) || \
27 defined(__LITTLE_ENDIAN__)
28 #define SBALL_LITTLE_ENDIAN
30 #define SBALL_BIG_ENDIAN
33 #define INP_BUF_SZ 256
49 unsigned int keystate;
51 struct termios saved_term;
54 int (*parse)(struct sball*, int, char*, int);
57 static int stty_sball(struct sball *sb);
58 static int stty_mag(struct sball *sb);
59 static void stty_save(struct sball *sb);
60 static void stty_restore(struct sball *sb);
62 static int proc_input(struct sball *sb);
64 static int mag_parsepkt(struct sball *sb, int id, char *data, int len);
65 static int sball_parsepkt(struct sball *sb, int id, char *data, int len);
67 static int guess_num_buttons(const char *verstr);
69 static void make_printable(char *buf, int len);
70 static int read_timeout(int fd, char *buf, int bufsz, long tm_usec);
72 static void print_state(struct sball *sb);
75 struct sball *sball_open(const char *dev)
81 if((fd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK)) == -1) {
82 fprintf(stderr, "sball_open: failed to open device: %s: %s\n", dev, strerror(errno));
86 if(!(sb = calloc(1, sizeof *sb))) {
87 fprintf(stderr, "sball_open: failed to allocate sball object\n");
94 if(stty_sball(sb) == -1) {
97 write(fd, "\r@RESET\r", 8);
99 if((sz = read_timeout(fd, buf, sizeof buf - 1, 2000000)) > 0 && strstr(buf, "\r@1")) {
100 /* we got a response, so it's a spaceball */
101 make_printable(buf, sz);
102 printf("Spaceball detected: %s\n", buf);
104 sb->nbuttons = guess_num_buttons(buf);
105 printf("%d buttons\n", sb->nbuttons);
107 /* set binary mode and enable automatic data packet sending */
108 write(fd, "\rCB\rMSSV\r", 9);
110 sb->parse = sball_parsepkt;
114 /* try as a magellan spacemouse */
115 if(stty_mag(sb) == -1) {
118 write(fd, "vQ\r", 3);
120 if((sz = read_timeout(fd, buf, sizeof buf - 1, 250000)) > 0 && buf[0] == 'v') {
121 make_printable(buf, sz);
122 printf("Magellan SpaceMouse detected:\n%s\n", buf);
124 sb->nbuttons = guess_num_buttons(buf);
125 printf("%d buttons\n", sb->nbuttons);
127 /* set 3D mode, not-dominant-axis, pass through motion and button packets */
128 write(fd, "m3\r", 3);
130 sb->parse = mag_parsepkt;
141 void sball_close(struct sball *sb)
149 int sball_fd(struct sball *sb)
154 int sball_read(struct sball *sb)
158 while((sz = read(sb->fd, sb->buf + sb->len, INP_BUF_SZ - sb->len - 1)) > 0) {
163 /* if we fill the input buffer, make a last attempt to parse it, and discard
164 * it so we can receive more
166 if(sb->len >= INP_BUF_SZ) {
174 int sball_axis(struct sball *sb, int axis)
176 return sb->mot[axis];
179 unsigned int sball_buttons(struct sball *sb)
184 int sball_num_buttons(struct sball *sb)
189 /* Labtec spaceball: 9600 8n1 XON/XOFF
190 * Can't use canonical mode to assemble input into lines for the spaceball,
191 * because binary data received for motion events can include newlines which
192 * would be eaten up by the line discipline. Therefore we'll rely on VTIME=1 to
193 * hopefully get more than 1 byte at a time. Alternatively we could request
194 * printable reports, but I don't feel like implementing that.
196 static int stty_sball(struct sball *sb)
201 term = sb->saved_term;
205 term.c_cc[VTIME] = 1;
207 term.c_cflag = CLOCAL | CREAD | CS8 | HUPCL;
208 term.c_iflag = IGNBRK | IGNPAR | IXON | IXOFF;
210 cfsetispeed(&term, B9600);
211 cfsetospeed(&term, B9600);
213 if(tcsetattr(sb->fd, TCSAFLUSH, &term) == -1) {
214 perror("sball_open: tcsetattr");
217 tcflush(sb->fd, TCIOFLUSH);
219 mstat = sb->saved_mstat | TIOCM_DTR | TIOCM_RTS;
220 ioctl(sb->fd, TIOCMGET, &mstat);
224 /* Logicad magellan spacemouse: 9600 8n2 CTS/RTS
225 * Since the magellan devices don't seem to send any newlines, we can rely on
226 * canonical mode to feed us nice whole lines at a time.
228 static int stty_mag(struct sball *sb)
233 term = sb->saved_term;
235 term.c_lflag = ICANON;
237 term.c_cc[VTIME] = 0;
239 term.c_cc[VEOL] = '\r';
240 term.c_cc[VEOL2] = 0;
241 term.c_cc[VERASE] = 0;
242 term.c_cc[VKILL] = 0;
244 term.c_cflag = CLOCAL | CREAD | CS8 | CSTOPB | HUPCL;
246 term.c_cflag |= CCTS_OFLOW;
247 #elif defined(CRTSCTS)
248 term.c_cflag |= CRTSCTS;
250 term.c_iflag = IGNBRK | IGNPAR;
252 cfsetispeed(&term, B9600);
253 cfsetospeed(&term, B9600);
255 if(tcsetattr(sb->fd, TCSAFLUSH, &term) == -1) {
256 perror("sball_open: tcsetattr");
259 tcflush(sb->fd, TCIOFLUSH);
261 mstat = sb->saved_mstat | TIOCM_DTR | TIOCM_RTS;
262 ioctl(sb->fd, TIOCMGET, &mstat);
266 static void stty_save(struct sball *sb)
268 tcgetattr(sb->fd, &sb->saved_term);
269 ioctl(sb->fd, TIOCMGET, &sb->saved_mstat);
272 static void stty_restore(struct sball *sb)
274 tcsetattr(sb->fd, TCSAFLUSH, &sb->saved_term);
275 tcflush(sb->fd, TCIOFLUSH);
276 ioctl(sb->fd, TIOCMSET, &sb->saved_mstat);
280 static int proc_input(struct sball *sb)
283 char *bptr = sb->buf;
284 char *start = sb->buf;
285 char *end = sb->buf + sb->len;
287 /* see if we have a CR in the buffer */
291 sb->parse(sb, *start, start + 1, bptr - start - 1);
298 sz = start - sb->buf;
300 memmove(sb->buf, start, sz);
306 static int mag_parsepkt(struct sball *sb, int id, char *data, int len)
310 /*printf("magellan packet: %c - %s (%d bytes)\n", (char)id, data, len);*/
315 fprintf(stderr, "magellan: invalid data packet, expected 24 bytes, got: %d\n", len);
319 sb->mot[i] = ((((int)data[0] & 0xf) << 12) | (((int)data[1] & 0xf) << 8) |
320 (((int)data[2] & 0xf) << 4) | (data[3] & 0xf)) - 0x8000;
328 fprintf(stderr, "magellan: invalid keyboard pakcet, expected 3 bytes, got: %d\n", len);
331 sb->keystate = (data[0] & 0xf) | ((data[1] & 0xf) << 4) | (((unsigned int)data[2] & 0xf) << 8);
337 fprintf(stderr, "magellan error: illegal command: %c%c\n", data[1], data[2]);
338 } else if(data[0] == 2) {
339 fprintf(stderr, "magellan error: framing error\n");
341 fprintf(stderr, "magellan error: unknown device error\n");
351 static int sball_parsepkt(struct sball *sb, int id, char *data, int len)
356 /* decode data packet, replacing escaped values with the correct ones */
358 while(rd < data + len) {
359 if((c = *rd++) == '^') {
362 *wr++ = 0x11; /* XON */
365 *wr++ = 0x13; /* XOFF */
374 fprintf(stderr, "sball decode: ignoring invalid escape code: %xh\n", (unsigned int)c);
380 len = wr - data; /* update the decoded length */
385 fprintf(stderr, "sball: invalid data packet, expected 14 bytes, got: %d\n", len);
389 #ifndef SBALL_BIG_ENDIAN
398 memcpy(sb->mot, data + 2, 12);
404 fprintf(stderr, "sball: invalid key packet, expected 2 bytes, got: %d\n", len);
407 if(sb->flags & SB4000) break; /* ignore K packets from spaceball 4000 devices */
409 /* data[1] bits 0-3 -> buttons 0,1,2,3
410 * data[1] bits 4,5 (3003 L/R) -> buttons 0, 1
411 * data[0] bits 0-2 -> buttons 4,5,6
412 * data[0] bit 4 is (2003 pick) -> button 7
414 sb->keystate = (data[1] & 0xf) | ((data[1] >> 4) & 3) | ((data[0] & 7) << 4) |
415 ((data[0] & 0x10) >> 1);
421 fprintf(stderr, "sball: invalid sb4k key packet, expected 2 bytes, got: %d\n", len);
424 /* spaceball 4000 key packet */
426 /* update orientation flag (actually don't bother) */
431 sb->flags &= ~FLIPXY;
435 /* data[1] bits 0-5 -> buttons 0,1,2,3,4,5
436 * data[1] bit 7 -> button 6
437 * data[0] bits 0-4 -> buttons 7,8,9,10,11
439 sb->keystate = (data[1] & 0x3f) | ((data[1] & 0x80) >> 1) | ((data[0] & 0x1f) << 7);
444 fprintf(stderr, "sball: error:");
445 for(i=0; i<len; i++) {
446 if(isprint((int)data[i])) {
447 fprintf(stderr, " %c", data[i]);
449 fprintf(stderr, " %02xh", (unsigned int)data[i]);
454 case 'M': /* ignore MSS responses */
459 fprintf(stderr, "sball: got '%c' packet:", (char)id);
460 for(i=0; i<len; i++) {
461 fprintf(stderr, " %02x", (unsigned int)data[i]);
468 static int guess_num_buttons(const char *verstr)
471 const char *s, *model;
473 if((s = strstr(verstr, "Firmware version"))) { /* spaceball */
475 /* if we got a model number, guess based on that */
476 if((model = strchr(s, '('))) {
477 if(memcmp(model, "(Model ", 7) == 0) {
482 switch(atoi(model)) {
493 /* try to guess based on firmware number */
494 if(sscanf(s + 17, "%d.%d", &major, &minor) == 2 && major == 2) {
495 if(minor == 35 || minor == 62 || minor == 63) {
496 return 2; /* spaceball 3003/3003C */
498 if(minor == 42 || minor == 43 || minor == 45) {
499 /* 2.42 is also used by spaceball 2003C, but this should be
500 * caught before we get here by the model number guess
502 return 12; /* spaceball 4000flx/5000flx-a */
504 if(minor == 2 || minor == 13 || minor == 15) {
505 return 8; /* spaceball 1003/2003/2003c */
510 if(strstr(verstr, "MAGELLAN")) {
511 return 9; /* magellan spacemouse */
514 if(strstr(verstr, "SPACEBALL")) {
515 return 12; /* spaceball 5000 */
518 if(strstr(verstr, "CadMan")) {
522 fprintf(stderr, "Can't guess number of buttons, default to 8, report this as a bug!\n");
526 static void make_printable(char *buf, int len)
531 for(i=0; i<len; i++) {
535 while(*buf == '\n' || *buf == '\r') buf++;
543 static int read_timeout(int fd, char *buf, int bufsz, long tm_usec)
547 struct timeval tv0, tv;
550 if(!buf || bufsz <= 0) return -1;
553 gettimeofday(&tv0, 0);
555 while(sz < bufsz && usec > 0) {
556 tv.tv_sec = usec / 1000000;
557 tv.tv_usec = usec % 1000000;
561 if((res = select(fd + 1, &rdset, 0, 0, &tv)) > 0 && FD_ISSET(fd, &rdset)) {
562 sz += read(fd, buf + sz, bufsz - sz);
564 tm_usec = usec = 128000; /* wait 128ms for the rest of the message to appear */
565 gettimeofday(&tv0, 0);
568 if(res == -1 && (errno == EWOULDBLOCK || errno == EAGAIN)) {
571 gettimeofday(&tv, 0);
572 usec = tm_usec - ((tv.tv_sec - tv0.tv_sec) * 1000000 + (tv.tv_usec - tv0.tv_usec));
575 return sz > 0 ? sz : -1;
578 static void print_motion(short *mot)
580 printf(" T[%+6d %+6d %+6d] R[%+6d %+6d %+6d]", mot[0], mot[1],
581 mot[2], mot[3], mot[4], mot[5]);
584 static void print_keystate(unsigned int keystate)
588 for(i=0; i<12; i++) {
590 int hex = b < 10 ? b + '0' : b - 10 + 'a';
591 putchar(keystate & (1 << b) ? hex : '-');
595 static void print_state(struct sball *sb)
597 print_motion(sb->mot);
599 print_keystate(sb->keystate);