From: John Tsiombikas Date: Thu, 29 Oct 2020 00:58:44 +0000 (-0400) Subject: initial commit X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=sball;a=commitdiff_plain;h=aeeaea66849f23d8dec42bb64c9a739846a835d9 initial commit --- aeeaea66849f23d8dec42bb64c9a739846a835d9 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0932ecf --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.o +*.d +*.swp +test diff --git a/Makefile b/Makefile new file mode 100644 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 index 0000000..7fe32c5 --- /dev/null +++ b/src/main.c @@ -0,0 +1,16 @@ +#include +#include +#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 index 0000000..7e5101a --- /dev/null +++ b/src/sball.c @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include +#include +#include + +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 index 0000000..67ff1cf --- /dev/null +++ b/src/sball.h @@ -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_ */