diff --git a/src/lib/input.c b/src/lib/input.c index 84cae7fec..e1a414f8b 100644 --- a/src/lib/input.c +++ b/src/lib/input.c @@ -321,9 +321,9 @@ handle_getc(ncinputlayer* nc, int kpress, ncinput* ni, int leftmargin, int topma // blocks up through ts (infinite with NULL ts), returning number of events // (0 on timeout) or -1 on error/interruption. static int -block_on_input(FILE* fp, const struct timespec* ts, sigset_t* sigmask){ +block_on_input(int fd, const struct timespec* ts, sigset_t* sigmask){ struct pollfd pfd = { - .fd = fileno(fp), + .fd = fd, .events = POLLIN, .revents = 0, }; @@ -380,17 +380,16 @@ handle_queued_input(ncinputlayer* nc, ncinput* ni, int leftmargin, int topmargin static char32_t handle_input(ncinputlayer* nc, ncinput* ni, int leftmargin, int topmargin, sigset_t* sigmask){ - int r; - // getc() returns unsigned chars cast to ints - while(!input_queue_full(nc) && (r = getc(nc->ttyinfp)) >= 0){ - nc->inputbuf[nc->inputbuf_write_at] = (unsigned char)r; + unsigned char c; + while(!input_queue_full(nc) && read(nc->ttyinfd, &c, 1) > 0){ + nc->inputbuf[nc->inputbuf_write_at] = c; //fprintf(stderr, "OCCUPY: %u@%u read: %d\n", nc->inputbuf_occupied, nc->inputbuf_write_at, nc->inputbuf[nc->inputbuf_write_at]); if(++nc->inputbuf_write_at == sizeof(nc->inputbuf) / sizeof(*nc->inputbuf)){ nc->inputbuf_write_at = 0; } ++nc->inputbuf_occupied; const struct timespec ts = {}; - if(block_on_input(nc->ttyinfp, &ts, sigmask) < 1){ + if(block_on_input(nc->ttyinfd, &ts, sigmask) < 1){ break; } } @@ -444,7 +443,7 @@ ncinputlayer_prestamp(ncinputlayer* nc, const struct timespec *ts, return handle_queued_input(nc, ni, leftmargin, topmargin); } errno = 0; - if(block_on_input(nc->ttyinfp, ts, sigmask) > 0){ + if(block_on_input(nc->ttyinfd, ts, sigmask) > 0){ //fprintf(stderr, "%d events from input!\n", events); return handle_ncinput(nc, ni, leftmargin, topmargin, sigmask); } diff --git a/src/lib/internal.h b/src/lib/internal.h index 2f2582bcb..98d92a1af 100644 --- a/src/lib/internal.h +++ b/src/lib/internal.h @@ -282,7 +282,7 @@ typedef struct tinfo { } tinfo; typedef struct ncinputlayer { - FILE* ttyinfp; // FILE* for processing input + int ttyinfd; // file descriptor for processing input unsigned char inputbuf[BUFSIZ]; // we keep a wee ringbuffer of input queued up for delivery. if // inputbuf_occupied == sizeof(inputbuf), there is no room. otherwise, data diff --git a/src/lib/notcurses.c b/src/lib/notcurses.c index 627b48ecc..f96712817 100644 --- a/src/lib/notcurses.c +++ b/src/lib/notcurses.c @@ -685,8 +685,7 @@ int ncplane_genocide(ncplane *ncp){ } static int -make_nonblocking(FILE* fp){ - int fd = fileno(fp); +make_nonblocking(int fd){ if(fd < 0){ return -1; } @@ -887,7 +886,7 @@ get_tty_fd(notcurses* nc, FILE* ttyfp){ int ncinputlayer_init(ncinputlayer* nilayer, FILE* infp){ setbuffer(infp, NULL, 0); nilayer->inputescapes = NULL; - nilayer->ttyinfp = infp; + nilayer->ttyinfd = fileno(infp); if(prep_special_keys(nilayer)){ return -1; } @@ -1035,7 +1034,7 @@ notcurses* notcurses_core_init(const notcurses_options* opts, FILE* outfp){ if(ncinputlayer_init(&ret->input, stdin)){ goto err; } - if(make_nonblocking(ret->input.ttyinfp)){ + if(make_nonblocking(ret->input.ttyinfd)){ goto err; } // Neither of these is supported on e.g. the "linux" virtual console. @@ -2415,11 +2414,11 @@ int notcurses_lex_margins(const char* op, notcurses_options* opts){ } int notcurses_inputready_fd(notcurses* n){ - return fileno(n->input.ttyinfp); + return n->input.ttyinfd; } int ncdirect_inputready_fd(ncdirect* n){ - return fileno(n->input.ttyinfp); + return n->input.ttyinfd; } uint32_t* ncplane_rgba(const ncplane* nc, ncblitter_e blit,