magellan works
[sball] / src / sball.c
index 7e5101a..e51ee3e 100644 (file)
@@ -1,24 +1,43 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 #include <errno.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <termios.h>
+#include <sys/select.h>
+#include <sys/time.h>
+
+#define INP_BUF_SZ     256
 
 struct sball {
        int fd;
+
+       char buf[INP_BUF_SZ];
+       int len;
+
+       int (*proc_input)(struct sball *sb);
 };
 
 static int stty_sball(int fd);
 static int stty_mag(int fd);
 
+static int proc_sball(struct sball *sb);
+static int proc_mag(struct sball *sb);
+
+static int mag_parsepkt(int id, char *data, int len);
+
+static void make_printable(char *buf, int len);
+static int read_timeout(int fd, char *buf, int bufsz, long tm_usec);
+
 struct sball *sball_open(const char *dev)
 {
-       int fd;
+       int fd, sz;
+       char buf[128];
        struct sball *sb = 0;
 
-       if((fd = open(dev, O_RDWR | | O_NOCTTY | O_NONBLOCK)) == -1) {
+       if((fd = open(dev, O_RDWR | O_NOCTTY)) == -1) {
                fprintf(stderr, "sball_open: failed to open device: %s: %s\n", dev, strerror(errno));
                return 0;
        }
@@ -33,9 +52,33 @@ struct sball *sball_open(const char *dev)
                goto err;
        }
 
-       /* set detect receiver function pointer */
+       if((sz = read_timeout(fd, buf, sizeof buf - 1, 2000000)) > 0) {
+               /* we got a response, so it's a spaceball */
+               make_printable(buf, sz);
+               printf("Spaceball detected: %s\n", buf);
+               /* TODO: improve detection, verify it's a correct reset response */
+
+               sb->proc_input = proc_sball;
+               return sb;
+       }
+
+       /* try as a magellan spacemouse */
+       if(stty_mag(fd) == -1) {
+               goto err;
+       }
+       write(fd, "vQ\r", 3);
+
+       if((sz = read_timeout(fd, buf, sizeof buf - 1, 250000)) > 0) {
+               /* we got a response, it's a magellan spacemouse */
+               make_printable(buf, sz);
+               printf("Magellan SpaceMouse detected: %s\n", buf);
+
+               /* set 3D mode, not-dominant-axis, pass through motion and button packets */
+               write(fd, "m3\r", 3);
 
-       return sb;
+               sb->proc_input = proc_mag;
+               return sb;
+       }
 
 err:
        close(fd);
@@ -49,6 +92,30 @@ void sball_close(struct sball *sb)
        close(sb->fd);
 }
 
+int sball_fd(struct sball *sb)
+{
+       return sb->fd;
+}
+
+int sball_read(struct sball *sb)
+{
+       int sz;
+
+       while((sz = read(sb->fd, sb->buf + sb->len,  INP_BUF_SZ - sb->len - 1)) > 0) {
+               sb->len += sz;
+               sb->proc_input(sb);
+       }
+
+       /* if we fill the input buffer, make a last attempt to parse it, and discard
+        * it so we can receive more
+        */
+       if(sb->len >= INP_BUF_SZ) {
+               sb->proc_input(sb);
+               sb->len = 0;
+       }
+
+       return 0;
+}
 
 /* Labtec spaceball: 9600 8n1 XON/XOFF */
 static int stty_sball(int fd)
@@ -63,13 +130,13 @@ static int stty_sball(int fd)
        term.c_oflag = 0;
        term.c_lflag = 0;
        term.c_cc[VMIN] = 0;
-       term.c_cc[VTIME] = 0;
+       term.c_cc[VTIME] = 1;
 
        term.c_cflag = CLOCAL | CREAD | CS8 | HUPCL;
        term.c_iflag = IGNBRK | IGNPAR | IXON | IXOFF;
 
-       csetispeed(&term, B9600);
-       csetospeed(&term, B9600);
+       cfsetispeed(&term, B9600);
+       cfsetospeed(&term, B9600);
 
        if(tcsetattr(fd, TCSANOW, &term) == -1) {
                perror("sball_open: tcsetattr");
@@ -92,13 +159,13 @@ static int stty_mag(int fd)
        term.c_oflag = 0;
        term.c_lflag = 0;
        term.c_cc[VMIN] = 0;
-       term.c_cc[VTIME] = 0;
+       term.c_cc[VTIME] = 1;
 
        term.c_cflag = CLOCAL | CREAD | CS8 | CSTOPB | HUPCL | CRTSCTS;
-       term.c_iflag = IGNBRK | IGNPAR |;
+       term.c_iflag = IGNBRK | IGNPAR;
 
-       csetispeed(&term, B9600);
-       csetospeed(&term, B9600);
+       cfsetispeed(&term, B9600);
+       cfsetospeed(&term, B9600);
 
        if(tcsetattr(fd, TCSANOW, &term) == -1) {
                perror("sball_open: tcsetattr");
@@ -107,3 +174,140 @@ static int stty_mag(int fd)
 
        return 0;
 }
+
+
+static int proc_sball(struct sball *sb)
+{
+       return -1;
+}
+
+static int proc_mag(struct sball *sb)
+{
+       int sz;
+       char *bptr = sb->buf;
+       char *start = sb->buf;
+       char *end = sb->buf + sb->len;
+
+       /* see if we have a CR in the buffer */
+       while(bptr < end) {
+               if(*bptr == '\r') {
+                       *bptr = 0;
+                       mag_parsepkt(*start, start + 1, bptr - start - 1);
+                       start = ++bptr;
+               } else {
+                       bptr++;
+               }
+       }
+
+       sz = start - sb->buf;
+       if(sz > 0) {
+               memmove(sb->buf, start, sz);
+               sb->len -= sz;
+       }
+       return 0;
+}
+
+static int mag_parsepkt(int id, char *data, int len)
+{
+       int i, mot[6];
+       unsigned int keystate;
+
+       /*printf("magellan packet: %c - %s (%d bytes)\n", (char)id, data, len);*/
+
+       switch(id) {
+       case 'd':
+               if(len != 24) {
+                       fprintf(stderr, "magellan: invalid data packet, expected 24 bytes, got: %d\n", len);
+                       return -1;
+               }
+               for(i=0; i<6; i++) {
+                       mot[i] = ((((int)data[0] & 0xf) << 12) | (((int)data[1] & 0xf) << 8) |
+                                       (((int)data[2] & 0xf) << 4) | (data[3] & 0xf)) - 0x8000;
+                       data += 4;
+               }
+               printf("motion: T %+4d %+4d %+4d  R %+4d %+4d %+4d\n", mot[0], mot[1], mot[2], mot[3], mot[4], mot[5]);
+               break;
+
+       case 'k':
+               if(len != 3) {
+                       fprintf(stderr, "magellan: invalid keyboard pakcet, expected 3 bytes, got: %d\n", len);
+                       return -1;
+               }
+               keystate = (data[0] & 0xf) | ((data[1] & 0xf) << 4) | (((unsigned int)data[2] & 0xf) << 8);
+               printf("keystate: ");
+               for(i=0; i<12; i++) {
+                       int b = 11 - i;
+                       int hex = b < 10 ? b + '0' : b - 10 + 'a';
+                       putchar(keystate & (1 << b) ? hex : '-');
+               }
+               putchar('\n');
+               break;
+
+       case 'e':
+               if(data[0] == 1) {
+                       fprintf(stderr, "magellan error: illegal command: %c%c\n", data[1], data[2]);
+               } else if(data[0] == 2) {
+                       fprintf(stderr, "magellan error: framing error\n");
+               } else {
+                       fprintf(stderr, "magellan error: unknown device error\n");
+               }
+               return -1;
+
+       default:
+               break;
+       }
+       return 0;
+}
+
+static void make_printable(char *buf, int len)
+{
+       int i, c;
+       char *wr = buf;
+
+       for(i=0; i<len; i++) {
+               c = *buf++;
+               if(c == '\r') {
+                       *wr++ = '\n';
+                       if(*buf == '\n') buf++;
+               } else {
+                       *wr++ = c;
+               }
+       }
+       *wr = 0;
+}
+
+static int read_timeout(int fd, char *buf, int bufsz, long tm_usec)
+{
+       int res;
+       long usec, sz = 0;
+       struct timeval tv0, tv;
+       fd_set rdset;
+
+       if(!buf || bufsz <= 0) return -1;
+
+       usec = tm_usec;
+       gettimeofday(&tv0, 0);
+
+       while(sz < bufsz && usec > 0) {
+               tv.tv_sec = usec / 1000000;
+               tv.tv_usec = usec % 1000000;
+
+               FD_ZERO(&rdset);
+               FD_SET(fd, &rdset);
+               if((res = select(fd + 1, &rdset, 0, 0, &tv)) > 0 && FD_ISSET(fd, &rdset)) {
+                       sz += read(fd, buf + sz, bufsz - sz);
+                       buf[sz] = 0;
+                       tm_usec = usec = 128000;        /* wait 128ms for the rest of the message to appear */
+                       gettimeofday(&tv0, 0);
+                       continue;
+               }
+               if(res == -1 && (errno == EWOULDBLOCK || errno == EAGAIN)) {
+                       break;
+               }
+               gettimeofday(&tv, 0);
+               usec = tm_usec - ((tv.tv_sec - tv0.tv_sec) * 1000000 + (tv.tv_usec - tv0.tv_usec));
+       }
+
+       return sz > 0 ? sz : -1;
+}
+