initial commit
authorJohn Tsiombikas <nuclear@member.fsf.org>
Thu, 29 Oct 2020 00:58:44 +0000 (20:58 -0400)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Thu, 29 Oct 2020 00:58:44 +0000 (20:58 -0400)
.gitignore [new file with mode: 0644]
Makefile [new file with mode: 0644]
src/main.c [new file with mode: 0644]
src/sball.c [new file with mode: 0644]
src/sball.h [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..0932ecf
--- /dev/null
@@ -0,0 +1,4 @@
+*.o
+*.d
+*.swp
+test
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..a41c55c
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,19 @@
+src = $(wildcard src/*.c)
+obj = $(src:.c=.o)
+dep = $(obj:.o=.d)
+bin = sball
+
+CFLAGS = -pedantic -Wall -g -MMD
+
+$(bin): $(obj)
+       $(CC) -o $@ $(obj) $(LDFLAGS)
+
+-include $(dep)
+
+.PHONY: clean
+clean:
+       rm -f $(obj) $(bin)
+
+.PHONY: cleandep
+cleandep:
+       rm -f $(dep)
diff --git a/src/main.c b/src/main.c
new file mode 100644 (file)
index 0000000..7fe32c5
--- /dev/null
@@ -0,0 +1,16 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "sball.h"
+
+static int fd;
+
+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");
+               return 1;
+       }
+
+       sball_close(fd);
+       return 0;
+}
diff --git a/src/sball.c b/src/sball.c
new file mode 100644 (file)
index 0000000..7e5101a
--- /dev/null
@@ -0,0 +1,109 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <termios.h>
+
+struct sball {
+       int fd;
+};
+
+static int stty_sball(int fd);
+static int stty_mag(int fd);
+
+struct sball *sball_open(const char *dev)
+{
+       int fd;
+       struct sball *sb = 0;
+
+       if((fd = open(dev, O_RDWR | | O_NOCTTY | O_NONBLOCK)) == -1) {
+               fprintf(stderr, "sball_open: failed to open device: %s: %s\n", dev, strerror(errno));
+               return 0;
+       }
+
+       if(!(sb = malloc(sizeof *sb))) {
+               fprintf(stderr, "sball_open: failed to allocate sball object\n");
+               goto err;
+       }
+       sb->fd = fd;
+
+       if(stty_sball(fd) == -1) {
+               goto err;
+       }
+
+       /* set detect receiver function pointer */
+
+       return sb;
+
+err:
+       close(fd);
+       free(sb);
+       return 0;
+}
+
+void sball_close(struct sball *sb)
+{
+       if(!sb) return;
+       close(sb->fd);
+}
+
+
+/* Labtec spaceball: 9600 8n1 XON/XOFF */
+static int stty_sball(int fd)
+{
+       struct termios term;
+
+       if(tcgetattr(fd, &term) == -1) {
+               perror("sball_open: tcgetattr");
+               return -1;
+       }
+
+       term.c_oflag = 0;
+       term.c_lflag = 0;
+       term.c_cc[VMIN] = 0;
+       term.c_cc[VTIME] = 0;
+
+       term.c_cflag = CLOCAL | CREAD | CS8 | HUPCL;
+       term.c_iflag = IGNBRK | IGNPAR | IXON | IXOFF;
+
+       csetispeed(&term, B9600);
+       csetospeed(&term, B9600);
+
+       if(tcsetattr(fd, TCSANOW, &term) == -1) {
+               perror("sball_open: tcsetattr");
+               return -1;
+       }
+
+       return 0;
+}
+
+/* Logicad magellan spacemouse: 9600 8n2 CTS/RTS */
+static int stty_mag(int fd)
+{
+       struct termios term;
+
+       if(tcgetattr(fd, &term) == -1) {
+               perror("sball_open: tcgetattr");
+               return -1;
+       }
+
+       term.c_oflag = 0;
+       term.c_lflag = 0;
+       term.c_cc[VMIN] = 0;
+       term.c_cc[VTIME] = 0;
+
+       term.c_cflag = CLOCAL | CREAD | CS8 | CSTOPB | HUPCL | CRTSCTS;
+       term.c_iflag = IGNBRK | IGNPAR |;
+
+       csetispeed(&term, B9600);
+       csetospeed(&term, B9600);
+
+       if(tcsetattr(fd, TCSANOW, &term) == -1) {
+               perror("sball_open: tcsetattr");
+               return -1;
+       }
+
+       return 0;
+}
diff --git a/src/sball.h b/src/sball.h
new file mode 100644 (file)
index 0000000..67ff1cf
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef SBALL_H_
+#define SBALL_H_
+
+struct sball;
+
+struct sball *sball_open(const char *dev);
+void sball_close(struct sball *sb);
+
+#endif /* SBALL_H_ */