diff options
author | Dominic Radermacher <blip@mockmoon-cybernetics.ch> | 2017-07-19 14:08:49 +0200 |
---|---|---|
committer | Dominic Radermacher <blip@mockmoon-cybernetics.ch> | 2017-07-19 14:08:49 +0200 |
commit | c57a8a9bf49fa7a3231ce8d6e435603e8ab016eb (patch) | |
tree | 6c83a316511a185cc497a4fc2237742417d3c718 /src/tty.c |
initial importv0.9
Diffstat (limited to 'src/tty.c')
-rw-r--r-- | src/tty.c | 232 |
1 files changed, 232 insertions, 0 deletions
diff --git a/src/tty.c b/src/tty.c new file mode 100644 index 0000000..0a7b7fc --- /dev/null +++ b/src/tty.c @@ -0,0 +1,232 @@ + +/* -------------------------------------------------------------------- + tty code for my 68HC11 devtools project + + Copyright (C) 2004 Dominic Radermacher <dominic.radermacher@gmail.com> + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + -------------------------------------------------------------------- */ + +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> /* strncpy() */ +#include <termios.h> +#include <sys/time.h> +#include <fcntl.h> +#include <sys/types.h> + +#include "tty.h" + +struct _tty_keyseq_struct { + char *seq; + int code; +} tty_keyseqs[] = { + {"[[A", KEY_F1}, {"OP", KEY_F1}, + {"[[B", KEY_F2}, {"OQ", KEY_F2}, + {"[[C", KEY_F3}, {"OR", KEY_F3}, + {"[[D", KEY_F4}, {"OS", KEY_F4}, + {"[[E", KEY_F5}, {"[15~", KEY_F5}, + {"[17~", KEY_F6}, + {"[18~", KEY_F7}, + {"[19~", KEY_F8}, + {"[20~", KEY_F9}, + {"[21~", KEY_F10}, + {"[23~", KEY_F11}, + {"[24~", KEY_F12}, + {"[25~", KEY_F13}, + {"[A", KEY_C_UP}, + {"[B", KEY_C_DOWN}, + {"[C", KEY_C_RIGHT}, + {"[D", KEY_C_LEFT}, + {"[2~", KEY_INS}, + {"[3~", KEY_DEL}, + {"[1~", KEY_HOME}, {"[H", KEY_HOME}, + {"[5~", KEY_PG_UP}, + {"[6~", KEY_PG_DOWN}, + {"[4~", KEY_END}, {"[F", KEY_END}, + {"[P", KEY_BREAK}, + {0, 0} +}; + +/* -------------------------------------------------------------------- + Function : Initialisation of the tty. + Input value : Structure of screen. + Output value : Structure of screen with height and width + -------------------------------------------------------------------- */ + +int tty_init(void) +{ + struct termios tios; + + if(tcgetattr(0, &tios) == 0) { + tios.c_lflag &= ~ICANON; /* Turn char buffering off */ + tios.c_lflag &= ~ECHO; /* Turn char displaying off */ + tios.c_cflag |= CRTSCTS; + if(tcsetattr(0, 0, &tios) != 0) { + fprintf(stderr, "TTY Error\n"); + } + } + printf("\033[r\033[999;999f\033[6n"); + scanf("\033[%i;%i", &(my_scr.height), &(my_scr.width)); + return 0; +} + +int _tty_getkeycode(int t) +{ + fd_set fds; + struct timeval TimeOut; + unsigned char b[1]; + TimeOut.tv_sec = t; + TimeOut.tv_usec = 1000; /* Mikrosekunden !! */ + + FD_ZERO(&fds); + FD_SET(0, &fds); /* 0 means socket stdin */ + if(select(1, &fds, NULL, NULL, &TimeOut) != 0) { + read(0, b, 1); + return 0x0000+b[0]; + } /* else ... */ + return KEY_TIMEOUT; +} + +/* --------------------------------------------------------------------- + Function : Wait for key until timeout, if a key is pressed + return its code. + Input value : Timeout in seconds. + Output value : 0x00xx, xx=keycode, if normal keycode + 0x01xx xx=keycode with ALT modifier + 0x02xx xx=special ESC sequence code + 0xffff if timeout occurs. + --------------------------------------------------------------------- */ + +int tty_getkey(void) +{ + int count, k; + char b[8]; + + if((k=_tty_getkeycode(1)) == KEY_TIMEOUT) { + return KEY_TIMEOUT; + } else if(k != 0x1b) { + return k; + } else { + for(count=0; ((k=_tty_getkeycode(0)) != KEY_TIMEOUT); count++) { + if(count < 7) { + b[count]=(k & 0xff); + } + } + if(count == 0) { + return 0x001b; + } else if(count == 1) { /* ESC <key> = Alt + <key> */ + return 0x0100 + b[0]; + } /* all others must be looked up */ + for(k=0; tty_keyseqs[k].seq != NULL; k++) { + if(strncmp(tty_keyseqs[k].seq, b, count) == 0) { + return tty_keyseqs[k].code; + } + } +// printf("%i ESC %02x %02x %02x %02x\n", count, b[0], b[1], b[2], b[3]); + return KEY_UNKNOWN; + } +} + +/* -------------------------------------------------------------------- + Function : Turns display of chars on or off. + Input value : Mode, can be 0 (off) or 1 (on) + Output value : None. + -------------------------------------------------------------------- */ + +int tty_setechomode(int mode) +{ + struct termios tios; + + if(tcgetattr(0, &tios) != 0) { + return -1; + } + if(mode == 0) { + tios.c_lflag &= ~ECHO; /* Char displaying off */ + } else { + tios.c_lflag |= ECHO; /* Char displaying on */ + } + if(tcsetattr(0, 0, &tios) != 0) { + return -1; + } + return 0; +} + +inline int tty_width() +{ + return my_scr.width; +} + +inline int tty_height() +{ + return my_scr.height; +} + +inline void tty_cls(void) +{ + printf("\033[2J"); +} + +inline void tty_clreol(void) +{ + printf("\033[0K"); /* clear from cursor to end of line */ +} + +void tty_gotoxy(int x, int y) +{ + if(x < 1) x=1; + if(x > my_scr.width) x=my_scr.width; + if(y < 1) y=1; + if(y > my_scr.height) y=my_scr.height; + printf("\033[%i;%if", y, x); +} + +int tty_setscrollregion(int from, int to) +{ + if((from < 1) || (from >= my_scr.height) || (to < from) || (to > my_scr.height)) { + return -1; + } + printf("\033[%i;%ir", from, to); /* Set scrolling region */ + return 0; +} + +void tty_setmode(int mode) +{ + printf("\033[%im", mode); +} + +void tty_setcolor(int bg, int fg) +{ + if((bg < 0) || (bg > 7) || (fg < 0) || (fg > 7)) { + return; + } + printf("\033[%i;%im", 40+bg, 30+fg); +} + +inline void tty_savecursorpos(void) +{ + printf("\0337"); /* Save cursor position */ +} + +inline void tty_restorecursorpos(void) +{ + printf("\0338"); /* Restore cursor */ +} + +void tty_reset(void) +{ +// tty_setscrollregion(1, my_scr.height); + tty_setechomode(1); /* is NOT included in full reset */ + printf("\033c"); /* Full reset */ +} |