aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2015-06-26 16:26:15 -0500
committerRob Landley <rob@landley.net>2015-06-26 16:26:15 -0500
commitb20c80b603c1795c473b3957fd2538485ec4eb90 (patch)
tree4d34cc6564010d9a20982b83bcc420c17ad93132 /lib
parent325e02ec918246242e89a32c053bf5f767fc37d3 (diff)
downloadtoybox-b20c80b603c1795c473b3957fd2538485ec4eb90.tar.gz
Factor out more not-curses infrastructure into lib.
Diffstat (limited to 'lib')
-rw-r--r--lib/interestingtimes.c34
-rw-r--r--lib/lib.h17
2 files changed, 48 insertions, 3 deletions
diff --git a/lib/interestingtimes.c b/lib/interestingtimes.c
index 85b8eb4c..8f8b35c2 100644
--- a/lib/interestingtimes.c
+++ b/lib/interestingtimes.c
@@ -81,13 +81,15 @@ int set_terminal(int fd, int raw, struct termios *old)
}
// Scan stdin for a keypress, parsing known escape sequences
-// seqs is array of char * strings, ends with NULL ptr
// Returns: 0-255=literal, -1=EOF, -2=NONE, 256-...=index into seq
// scratch space is necessary because last char of !seq could start new seq
// Zero out first byte of scratch before first call to scan_key
// block=0 allows fetching multiple characters before updating display
-int scan_key(char *scratch, char **seqs, int block)
+int scan_key(char *scratch, int block)
{
+ // up down right left pgup pgdn home end ins
+ char *seqs[] = {"\033[A", "\033[B", "\033[C", "\033[D", "\033[5~", "\033[6~",
+ "\033OH", "\033OF", "\033[2~", 0};
struct pollfd pfd;
int maybe, i, j;
char *test;
@@ -131,3 +133,31 @@ int scan_key(char *scratch, char **seqs, int block)
return i;
}
+
+void tty_esc(char *s)
+{
+ printf("\033[%s", s);
+}
+
+void tty_jump(int x, int y)
+{
+ char s[32];
+
+ sprintf(s, "%d;%dH", y+1, x+1);
+ tty_esc(s);
+}
+
+void tty_reset(void)
+{
+ set_terminal(1, 0, 0);
+ tty_esc("?25h");
+ tty_esc("0m");
+ tty_jump(0, 999);
+ tty_esc("K");
+}
+
+void tty_sigreset(int i)
+{
+ tty_reset();
+ _exit(128+i);
+}
diff --git a/lib/lib.h b/lib/lib.h
index 54876b6a..92bd6b5d 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -184,7 +184,22 @@ int xpoll(struct pollfd *fds, int nfds, int timeout);
int xgettty(void);
int terminal_size(unsigned *xx, unsigned *yy);
int set_terminal(int fd, int raw, struct termios *old);
-int scan_key(char *scratch, char **seqs, int block);
+int scan_key(char *scratch, int block);
+void tty_esc(char *s);
+void tty_jump(int x, int y);
+void tty_reset(void);
+void tty_sigreset(int i);
+
+// Results from scan_key()
+#define KEY_UP 256
+#define KEY_DOWN 257
+#define KEY_RIGHT 258
+#define KEY_LEFT 259
+#define KEY_PGUP 260
+#define KEY_PGDN 261
+#define KEY_HOME 262
+#define KEY_END 263
+#define KEY_INSERT 264
// net.c
int xsocket(int domain, int type, int protocol);