refactor ct_term
This commit is contained in:
parent
08e4376a35
commit
7f4f9941de
2 changed files with 27 additions and 14 deletions
37
src/term.c
37
src/term.c
|
|
@ -1,23 +1,36 @@
|
|||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "_ct_shared.h"
|
||||
#include "term.h"
|
||||
|
||||
int getterm(struct ct_term *const term) {
|
||||
if ((term->fd = open("/dev/tty", O_RDONLY)) == -1)
|
||||
return 1;
|
||||
if (tcgetattr(term->fd, &term->termios0) == -1)
|
||||
return 1;
|
||||
term->termios = term->termios0;
|
||||
return 0;
|
||||
int stashterm(const struct ct_term **const term) {
|
||||
struct ct_term *t = (struct ct_term *)malloc(sizeof(struct ct_term));
|
||||
|
||||
t->fd = open("/dev/tty", O_RDONLY);
|
||||
if (t->fd == -1)
|
||||
goto fail;
|
||||
else if (tcgetattr(t->fd, &t->termios0) == -1) {
|
||||
close(t->fd);
|
||||
goto fail;
|
||||
}
|
||||
t->termios = t->termios0;
|
||||
|
||||
*term = t;
|
||||
return OK;
|
||||
|
||||
fail:
|
||||
free(t);
|
||||
return ERR;
|
||||
}
|
||||
|
||||
void endterm(struct ct_term *const term) {
|
||||
void resetterm(struct ct_term *const term) {
|
||||
if (term->fd != -1) {
|
||||
close(term->fd);
|
||||
/* WARNING: should tcsetattr instead be called on term->fd before closing? */
|
||||
/* WARNING: should tcsetattr instead be called on term->fd before closing?
|
||||
*/
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &term->termios0);
|
||||
}
|
||||
free(term);
|
||||
|
|
@ -26,9 +39,9 @@ void endterm(struct ct_term *const term) {
|
|||
int termsize(struct ct_term *const term) {
|
||||
struct winsize argp;
|
||||
if (ioctl(term->fd, TIOCGWINSZ, &argp) == -1)
|
||||
return 1;
|
||||
return ERR;
|
||||
|
||||
term->rows = argp.ws_row;
|
||||
term->cols = argp.ws_col;
|
||||
return 0;
|
||||
return OK;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,8 +12,8 @@ struct ct_term {
|
|||
struct termios termios;
|
||||
};
|
||||
|
||||
int getterm(struct ct_term *const term);
|
||||
void endterm(struct ct_term *const term);
|
||||
int stashterm(const struct ct_term **const term);
|
||||
void resetterm(struct ct_term *const term);
|
||||
int termsize(struct ct_term *const term);
|
||||
|
||||
#endif /* _CURSETREE_TERM_H */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue