1 /* interestingtimes.c - cursor control
3 * Copyright 2015 Rob Landley <rob@landley.net>
12 for (i = 0; i<3; i++) if (isatty(j = (i+1)%3)) return j;
14 return xopen("/dev/tty", O_RDWR);
17 // Quick and dirty query size of terminal, doesn't do ANSI probe fallback.
18 // set x=80 y=25 before calling to provide defaults. Returns 0 if couldn't
21 int terminal_size(unsigned *xx, unsigned *yy)
24 unsigned i, x = 0, y = 0;
27 // stdin, stdout, stderr
29 memset(&ws, 0, sizeof(ws));
30 if (!ioctl(i, TIOCGWINSZ, &ws)) {
31 if (ws.ws_col) x = ws.ws_col;
32 if (ws.ws_row) y = ws.ws_row;
37 s = getenv("COLUMNS");
38 if (s) sscanf(s, "%u", &x);
40 if (s) sscanf(s, "%u", &y);
42 // Never return 0 for either value, leave it at default instead.
49 // Reset terminal to known state, saving copy of old state if old != NULL.
50 int set_terminal(int fd, int raw, struct termios *old)
52 struct termios termio;
54 // Fetch local copy of old terminfo, and copy struct contents to *old if set
55 if (!tcgetattr(fd, &termio) && old) *old = termio;
57 // the following are the bits set for an xterm. Linux text mode TTYs by
58 // default add two additional bits that only matter for serial processing
59 // (turn serial line break into an interrupt, and XON/XOFF flow control)
61 // Any key unblocks output, swap CR and NL on input
62 termio.c_iflag = IXANY|ICRNL|INLCR;
63 if (toys.which->flags & TOYFLAG_LOCALE) termio.c_iflag |= IUTF8;
65 // Output appends CR to NL, does magic undocumented postprocessing
66 termio.c_oflag = ONLCR|OPOST;
68 // Leave serial port speed alone
69 // termio.c_cflag = C_READ|CS8|EXTB;
71 // Generate signals, input entire line at once, echo output
72 // erase, line kill, escape control characters with ^
73 // erase line char at a time
74 // "extended" behavior: ctrl-V quotes next char, ctrl-R reprints unread chars,
76 termio.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
78 if (raw) cfmakeraw(&termio);
80 return tcsetattr(fd, TCSANOW, &termio);
83 // Scan stdin for a keypress, parsing known escape sequences
84 // Returns: 0-255=literal, -1=EOF, -2=NONE, 256-...=index into seq
85 // scratch space is necessary because last char of !seq could start new seq
86 // Zero out first byte of scratch before first call to scan_key
87 // block=0 allows fetching multiple characters before updating display
88 int scan_key(char *scratch, int block)
90 // up down right left pgup pgdn home end ins
91 char *seqs[] = {"\033[A", "\033[B", "\033[C", "\033[D", "\033[5~", "\033[6~",
92 "\033OH", "\033OF", "\033[2~", 0};
105 for (i = maybe = 0; (test = seqs[i]); i++) {
106 for (j = 0; j<*scratch; j++) if (scratch[j+1] != test[j]) break;
110 // We recognized current sequence: consume and return
116 // If current data can't be a known sequence, return next raw char
120 // Need more data to decide
122 // 30 miliseconds is about the gap between characters at 300 baud
123 if (maybe || !block) if (!xpoll(&pfd, 1, 30*maybe)) break;
125 if (1 != read(0, scratch+1+*scratch, 1)) return -1;
129 // Was not a sequence
130 if (!*scratch) return -2;
132 if (--*scratch) memmove(scratch+1, scratch+2, *scratch);
137 void tty_esc(char *s)
139 printf("\033[%s", s);
142 void tty_jump(int x, int y)
146 sprintf(s, "%d;%dH", y+1, x+1);
152 set_terminal(1, 0, 0);
159 void tty_sigreset(int i)