diff options
Diffstat (limited to 'tis-curses.c')
| -rw-r--r-- | tis-curses.c | 48 |
1 files changed, 36 insertions, 12 deletions
diff --git a/tis-curses.c b/tis-curses.c index 33d62a5..8132f53 100644 --- a/tis-curses.c +++ b/tis-curses.c @@ -353,6 +353,7 @@ tui_seek(struct tpu *tpu, int dx, int dy) if (maxy == -1 || y > maxy) maxy = y; } } + if (minx == -1 || miny == -1) return; } if (dx == MIN) scrx = minx - 2; @@ -388,12 +389,12 @@ handlekey(int key) scrx += 4; break; case 's': - if (tis.stdin_port.out < 0) { + if (tis.stdin_port.attached && tis.stdin_port.out < 0) { c = getc(tis_stdin); if (c >= 0) tis.stdin_port.out = c; } - if (tis.stdout_port.in >= 0) { + if (tis.stdout_port.attached && tis.stdout_port.in >= 0) { putc(tis.stdout_port.in, tis_stdout); tis.stdout_port.in = -1; } @@ -411,7 +412,7 @@ handlekey(int key) case KEY_LEFT: case KEY_RIGHT: dir = key_to_dir(key); - if (tpu_sel->ports[dir].dst_tpu) + if (tpu_sel && tpu_sel->ports[dir].dst_tpu) tpu_sel = tpu_sel->ports[dir].dst_tpu; tui_seek(tpu_sel, MID, MID); break; @@ -419,13 +420,27 @@ handlekey(int key) } } -void -reset(int ifd, int argc, char **argv) +static struct tpu * +first_tpu(void) +{ + size_t i; + + for (i = 0; i < TPU_MAP_BUCKETS; i++) { + if (tis.tpu_map.buckets[i]) + return tis.tpu_map.buckets[i]->tpu; + } + + return NULL; +} + +static void +reset(int ifd, int argc, char **argv, bool watch) { tis_load(&tis, argv[1]); - if (inotify_add_watch(ifd, argv[1], IN_MODIFY) < 0) - die("inotify_add_watch '%s':", argv[1]); + if (watch) + if (inotify_add_watch(ifd, argv[1], IN_MODIFY) < 0) + die("inotify_add_watch '%s':", argv[1]); if (tis_stdin) fclose(tis_stdin); tis_stdin = fopen(argv[2], "r"); @@ -435,7 +450,11 @@ reset(int ifd, int argc, char **argv) tis_stdout = fopen(argv[3], "w+"); if (!tis_stdout) die("fopen '%s':", argv[3]); - tpu_sel = tis.stdin_port.dst_tpu; + if (tis.stdin_port.attached) { + tpu_sel = tis.stdin_port.dst_tpu; + } else { + tpu_sel = first_tpu(); + } tui_seek(NULL, MID, MID); } @@ -471,7 +490,7 @@ main(int argc, char **argv) ifd = inotify_init1(IN_NONBLOCK); - reset(ifd, argc, argv); + reset(ifd, argc, argv, true); quit = false; while (!quit) { @@ -479,7 +498,7 @@ main(int argc, char **argv) if (len < 0 && errno != EAGAIN) die("inotify_read:"); if (len >= 0) { - reset(ifd, argc, argv); + reset(ifd, argc, argv, true); show_reloaded = 1000 / TIMEOUT; } @@ -496,10 +515,15 @@ main(int argc, char **argv) tui_seek(NULL, MID, MID); break; case 'i': - tui_seek(tis.stdin_port.dst_tpu, MID, MID); + if (tis.stdin_port.attached) + tui_seek(tis.stdin_port.dst_tpu, MID, MID); break; case 'o': - tui_seek(tis.stdout_port.dst_tpu, MID, MID); + if (tis.stdout_port.attached) + tui_seek(tis.stdout_port.dst_tpu, MID, MID); + break; + case 'r': + reset(ifd, argc, argv, false); break; case KEY_CTRL('c'): quit = true; |
