Skip to content

Commit

Permalink
Move STDIN association to termio module
Browse files Browse the repository at this point in the history
	modified:   Makefile
	modified:   src/nmseffect.c
	modified:   src/nmstermio.c
	modified:   src/nmstermio_ncurses.c
  • Loading branch information
bartobri committed Oct 25, 2018
1 parent a771cf3 commit 1440337
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ all: nms sneakers

all-ncurses: nms-ncurses sneakers-ncurses

nms-ncurses: $(OBJ)/nmscharset.o $(OBJ)/nmstermio_ncurses.o $(OBJ)/nmseffect.o $(OBJ)/nms.o | $(BIN)
nms-ncurses: $(OBJ)/input.o $(OBJ)/error.o $(OBJ)/nmscharset.o $(OBJ)/nmstermio_ncurses.o $(OBJ)/nmseffect.o $(OBJ)/nms.o | $(BIN)
$(CC) $(CFLAGS) -o $(BIN)/nms $^ -lncurses

sneakers-ncurses: $(OBJ)/nmscharset.o $(OBJ)/nmstermio_ncurses.o $(OBJ)/nmseffect.o $(OBJ)/sneakers.o | $(BIN)
Expand Down
6 changes: 0 additions & 6 deletions src/nmseffect.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ char nmseffect_exec(unsigned char *string, int string_len) {
int maxRows, maxCols, curRow, curCol, origRow = 0, origCol = 0;
char ret = 0;

// Reassociate STDIN to the terminal if needed
if (!isatty(STDIN_FILENO) && !freopen ("/dev/tty", "r", stdin)) {
fprintf(stderr, "Error. Can't associate STDIN with terminal.\n");
return 0;
}

// Needed for UTF-8 support
setlocale(LC_ALL, "");

Expand Down
4 changes: 4 additions & 0 deletions src/nmstermio.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ static void nmstermio_set_terminal(int s) {
struct termios tp;
static struct termios save;
static int state = 1;

if (!isatty(STDIN_FILENO)) {
stdin = freopen("/dev/tty", "r", stdin);
}

if (s == 0) {
if (tcgetattr(STDIN_FILENO, &tp) == -1) {
Expand Down
5 changes: 5 additions & 0 deletions src/nmstermio_ncurses.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* functionality is defined and implemented here.
*/

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ncurses.h>

Expand All @@ -25,6 +27,9 @@ static int foregroundColor = COLOR_BLUE; // Foreground color settin
* otherwise.
*/
void nmstermio_init_terminal(void) {
if (!isatty(STDIN_FILENO)) {
stdin = freopen("/dev/tty", "r", stdin);
}
initscr();
cbreak();
noecho();
Expand Down

0 comments on commit 1440337

Please sign in to comment.