diff --git a/src/term.c b/src/term.c index c3d232b..0c4188f 100644 --- a/src/term.c +++ b/src/term.c @@ -1,5 +1,6 @@ #include #include +#include #include #include "ct_alloc.h" @@ -16,29 +17,36 @@ end: return fd; } +static inline void ct_resettermios(struct ct_term *restrict const term) { + term->termios = term->termios0; +} + +int ct_applyterm(struct ct_term *restrict const term) { + return tcsetattr(term->fd, TCSANOW, &term->termios); +} + int ct_stashterm(struct ct_term **restrict term) { - struct ct_term *t = (struct ct_term *)malloc(sizeof(struct ct_term)); + // struct ct_term *t = (struct ct_term *)malloc(sizeof(struct ct_term)); + *term = (struct ct_term *)malloc(sizeof(struct ct_term)); + memset(*term, 0, sizeof(struct ct_term)); // cast away the const to initialise these fields - *(int *)&(*term)->fd = ct_tty((struct termios *)&(*term)->termios0); + *(int *)&(*term)->fd = ct_tty((struct termios*)&(*term)->termios0); if ((*term)->fd < 0) { free(*term); return NERR; } // duplicate as termios0 will now be readonly - (*term)->termios = (*term)->termios0; - ct_termsize(*term); - - return OK; + ct_resettermios(*term); + return ct_termsize(*term); } void ct_resetterm(struct ct_term *const term) { if (term->fd != -1) { + ct_resettermios(term); + ct_applyterm(term); close(term->fd); - /* WARNING: should tcsetattr instead be called on term->fd before closing? - */ - tcsetattr(STDIN_FILENO, TCSANOW, &term->termios0); } free(term); } diff --git a/src/term.h b/src/term.h index f66089d..f8fee6c 100644 --- a/src/term.h +++ b/src/term.h @@ -26,6 +26,7 @@ static inline int ct_termsize(struct ct_term *const term) { } int ct_stashterm(struct ct_term **const term); +int ct_applyterm(struct ct_term *const term); void ct_resetterm(struct ct_term *const term); #endif /* _CURSETREE_TERM_H */