#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/select.h>
#include "sball.h"
-static int fd;
+#ifdef __FreeBSD__
+#define DEFDEV "/dev/ttyu0"
+#elif defined(__sgi__)
+#define DEFDEV "/dev/ttyd1"
+#else
+#define DEFDEV "/dev/ttyS0"
+#endif
+
+static void sighandler(int s);
+
+static struct sball *sb;
+static int quit;
int main(int argc, char **argv)
{
- if(!(fd = sball_open(argv[1] ? argv[1] : "/dev/ttyS0"))) {
- fprintf(stderr, "Failed to open spaceball at %s\n", argv[1] ? argv[1] : "/dev/ttyS0");
+ int fd;
+ fd_set rdset;
+ const char *dev = DEFDEV;
+
+ signal(SIGINT, sighandler);
+
+ if(argv[1]) {
+ if(strcmp(argv[1], "btest") == 0) {
+ sball_button_test();
+ return 0;
+ }
+ dev = argv[1];
+ }
+
+ if(!(sb = sball_open(dev))) {
+ fprintf(stderr, "Failed to open spaceball at %s\n", dev);
return 1;
}
+ fd = sball_fd(sb);
+
+ printf("Monitoring device, ctrl-c to quit\n");
+
+ while(!quit) {
+ FD_ZERO(&rdset);
+ FD_SET(fd, &rdset);
+
+ if(select(fd + 1, &rdset, 0, 0, 0) > 0) {
+ if(FD_ISSET(fd, &rdset)) {
+ sball_read(sb);
+ }
+ }
+
+ }
+ putchar('\n');
- sball_close(fd);
+ sball_close(sb);
return 0;
}
+
+static void sighandler(int s)
+{
+ quit = 1;
+}