projects
/
sball
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (from parent 1:
a763155
)
restore serial port settings on exit
author
John Tsiombikas
<nuclear@member.fsf.org>
Fri, 6 Nov 2020 23:21:29 +0000
(
01:21
+0200)
committer
John Tsiombikas
<nuclear@member.fsf.org>
Fri, 6 Nov 2020 23:21:29 +0000
(
01:21
+0200)
src/sball.c
patch
|
blob
|
history
diff --git
a/src/sball.c
b/src/sball.c
index
3273480
..
9dce2ae
100644
(file)
--- a/
src/sball.c
+++ b/
src/sball.c
@@
-4,7
+4,6
@@
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
-#include <alloca.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
@@
-30,11
+29,16
@@
struct sball {
short mot[6];
unsigned int keystate;
short mot[6];
unsigned int keystate;
+ struct termios saved_term;
+ int saved_mstat;
+
int (*parse)(struct sball*, int, char*, int);
};
int (*parse)(struct sball*, int, char*, int);
};
-static int stty_sball(int fd);
-static int stty_mag(int fd);
+static int stty_sball(struct sball *sb);
+static int stty_mag(struct sball *sb);
+static void stty_save(struct sball *sb);
+static void stty_restore(struct sball *sb);
static int proc_input(struct sball *sb);
static int proc_input(struct sball *sb);
@@
-68,7
+72,9
@@
struct sball *sball_open(const char *dev)
sb->flags = 0;
sb->len = 0;
sb->flags = 0;
sb->len = 0;
- if(stty_sball(fd) == -1) {
+ stty_save(sb);
+
+ if(stty_sball(sb) == -1) {
goto err;
}
write(fd, "\r@RESET\r", 8);
goto err;
}
write(fd, "\r@RESET\r", 8);
@@
-89,7
+95,7
@@
struct sball *sball_open(const char *dev)
}
/* try as a magellan spacemouse */
}
/* try as a magellan spacemouse */
- if(stty_mag(fd) == -1) {
+ if(stty_mag(sb) == -1) {
goto err;
}
write(fd, "vQ\r", 3);
goto err;
}
write(fd, "vQ\r", 3);
@@
-109,6
+115,7
@@
struct sball *sball_open(const char *dev)
}
err:
}
err:
+ stty_restore(sb);
close(fd);
free(sb);
return 0;
close(fd);
free(sb);
return 0;
@@
-117,6
+124,8
@@
err:
void sball_close(struct sball *sb)
{
if(!sb) return;
void sball_close(struct sball *sb)
{
if(!sb) return;
+
+ stty_restore(sb);
close(sb->fd);
}
close(sb->fd);
}
@@
-161,16
+170,12
@@
int sball_num_buttons(struct sball *sb)
}
/* Labtec spaceball: 9600 8n1 XON/XOFF */
}
/* Labtec spaceball: 9600 8n1 XON/XOFF */
-static int stty_sball(int fd)
+static int stty_sball(struct sball *sb)
{
{
- int status;
+ int mstat;
struct termios term;
struct termios term;
- if(tcgetattr(fd, &term) == -1) {
- perror("sball_open: tcgetattr");
- return -1;
- }
-
+ term = sb->saved_term;
term.c_oflag = 0;
term.c_lflag = ICANON;
term.c_cc[VMIN] = 0;
term.c_oflag = 0;
term.c_lflag = ICANON;
term.c_cc[VMIN] = 0;
@@
-187,29
+192,24
@@
static int stty_sball(int fd)
cfsetispeed(&term, B9600);
cfsetospeed(&term, B9600);
cfsetispeed(&term, B9600);
cfsetospeed(&term, B9600);
- if(tcsetattr(fd, TCSANOW, &term) == -1) {
+ if(tcsetattr(sb->fd, TCSAFLUSH, &term) == -1) {
perror("sball_open: tcsetattr");
return -1;
}
perror("sball_open: tcsetattr");
return -1;
}
+ tcflush(sb->fd, TCIOFLUSH);
- if(ioctl(fd, TIOCMGET, &status) != -1) {
- status |= TIOCM_DTR | TIOCM_RTS;
- ioctl(fd, TIOCMSET, &status);
- }
+ mstat = sb->saved_mstat | TIOCM_DTR | TIOCM_RTS;
+ ioctl(sb->fd, TIOCMGET, &mstat);
return 0;
}
/* Logicad magellan spacemouse: 9600 8n2 CTS/RTS */
return 0;
}
/* Logicad magellan spacemouse: 9600 8n2 CTS/RTS */
-static int stty_mag(int fd)
+static int stty_mag(struct sball *sb)
{
{
- int status;
+ int mstat;
struct termios term;
struct termios term;
- if(tcgetattr(fd, &term) == -1) {
- perror("sball_open: tcgetattr");
- return -1;
- }
-
+ term = sb->saved_term;
term.c_oflag = 0;
term.c_lflag = ICANON;
term.c_cc[VMIN] = 0;
term.c_oflag = 0;
term.c_lflag = ICANON;
term.c_cc[VMIN] = 0;
@@
-226,19
+226,30
@@
static int stty_mag(int fd)
cfsetispeed(&term, B9600);
cfsetospeed(&term, B9600);
cfsetispeed(&term, B9600);
cfsetospeed(&term, B9600);
- if(tcsetattr(fd, TCSAFLUSH, &term) == -1) {
+ if(tcsetattr(sb->fd, TCSAFLUSH, &term) == -1) {
perror("sball_open: tcsetattr");
return -1;
}
perror("sball_open: tcsetattr");
return -1;
}
- tcflush(fd, TCIOFLUSH);
+ tcflush(sb->fd, TCIOFLUSH);
- if(ioctl(fd, TIOCMGET, &status) != -1) {
- status |= TIOCM_DTR | TIOCM_RTS;
- ioctl(fd, TIOCMGET, &status);
- }
+ mstat = sb->saved_mstat | TIOCM_DTR | TIOCM_RTS;
+ ioctl(sb->fd, TIOCMGET, &mstat);
return 0;
}
return 0;
}
+static void stty_save(struct sball *sb)
+{
+ tcgetattr(sb->fd, &sb->saved_term);
+ ioctl(sb->fd, TIOCMGET, &sb->saved_mstat);
+}
+
+static void stty_restore(struct sball *sb)
+{
+ tcsetattr(sb->fd, TCSAFLUSH, &sb->saved_term);
+ tcflush(sb->fd, TCIOFLUSH);
+ ioctl(sb->fd, TIOCMSET, &sb->saved_mstat);
+}
+
static int proc_input(struct sball *sb)
{
static int proc_input(struct sball *sb)
{