c5e8c659d06ab72e5aed220b9b94e722a6d4b7cb
[sball] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <signal.h>
5 #include <errno.h>
6 #include <unistd.h>
7 #include <sys/select.h>
8 #include "sball.h"
9
10 #ifdef __FreeBSD__
11 #define DEFDEV  "/dev/ttyu0"
12 #else
13 #define DEFDEV  "/dev/ttyS0"
14 #endif
15
16 static void sighandler(int s);
17
18 static struct sball *sb;
19 static int quit;
20
21 int main(int argc, char **argv)
22 {
23         int fd;
24         fd_set rdset;
25         const char *dev = DEFDEV;
26
27         signal(SIGINT, sighandler);
28
29         if(argv[1]) {
30                 if(strcmp(argv[1], "btest") == 0) {
31                         sball_button_test();
32                         return 0;
33                 }
34                 dev = argv[1];
35         }
36
37         if(!(sb = sball_open(dev))) {
38                 fprintf(stderr, "Failed to open spaceball at %s\n", dev);
39                 return 1;
40         }
41         fd = sball_fd(sb);
42
43         printf("Monitoring device, ctrl-c to quit\n");
44
45         while(!quit) {
46                 FD_ZERO(&rdset);
47                 FD_SET(fd, &rdset);
48
49                 if(select(fd + 1, &rdset, 0, 0, 0) > 0) {
50                         if(FD_ISSET(fd, &rdset)) {
51                                 sball_read(sb);
52                         }
53                 }
54
55         }
56         putchar('\n');
57
58         sball_close(sb);
59         return 0;
60 }
61
62 static void sighandler(int s)
63 {
64         quit = 1;
65 }