lt

simple terminal emulator
Log | Files | Refs | git clone https://git.ne02ptzero.me/git/lt

commit 474dbbcc8f41107cb88c094b050ae59dfccce8d3
parent 3aaf3b730a949e74109d955a84932f12050234ae
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri,  8 Jun 2018 17:20:13 +0200

ADD: Now reading from tty

Signed-off-by: Ne02ptzero <louis@ne02ptzero.me>

Diffstat:
Mconfig.h | 4++--
Mrun.c | 6++----
Mterm.c | 44++++++++++++++++++++++++++++++++++++++++++++
Mterm.h | 3+++
Mtty.c | 25+++++++++++++++++++++++++
Mtty.h | 3+++
Mutils.h | 5+++++
7 files changed, 84 insertions(+), 6 deletions(-)

diff --git a/config.h b/config.h @@ -16,9 +16,9 @@ */ // CF: linux/Documentation/CodingStyle #define TABSPACES 8 -#define SHELL "/bin/sh" +#define SHELL "/bin/bash" #define DEF_COL 80 -#define DEF_ROW 25 /* Deathrow, haha, get it? */ +#define DEF_ROW 25 /* Death Row, haha, get it? */ #define ACTION_FPS 30 #define XFPS 120 diff --git a/run.c b/run.c @@ -4,6 +4,7 @@ #include "config.h" #include "utils.h" +#include "tty.h" static void fps_render(lt_t *lt, fd_set *readable, struct timespec *tv) { @@ -39,7 +40,6 @@ static void fps_render(lt_t *lt, fd_set *readable, struct timespec *tv) void run(lt_t *lt) { int max_fd; - size_t size; fd_set readable; struct timespec timeout = { 0 }; @@ -58,9 +58,7 @@ void run(lt_t *lt) } if (FD_ISSET(lt->tty.master, &readable)) - { - /*tty_read(&lt->tty);*/ - } + tty_read(&lt->tty, &lt->t); fps_render(lt, &readable, &timeout); } diff --git a/term.c b/term.c @@ -212,3 +212,47 @@ void term_cursor(term_t *t, int mode) term_move_to(t, c[alt].x, c[alt].y); } } + +int term_write(term_t *term, const char *buf, int len, bool show_ctrl) +{ + uint_least32_t u; + int charsize; + int n; + + for (n = 0; n < len; n += charsize) + { + if (IS_SET(term->mode, MODE_UTF8) && !IS_SET(term->mode, MODE_SIXEL)) + { + // UTF-8 + } + else + { + u = buf[n] & 0xFF; + charsize = 1; + } + + if (show_ctrl && IS_CONTROL(u)) + { + if (u & 0x80) + { + u &= 0x7f; + term_putc(term, '^'); + term_putc(term, '['); + } + else if (u != '\n' && u != '\r' && u != '\t') + { + u ^= 0x40; + term_putc(term, '^'); + } + } + term_putc(term, u); + } + + return n; +} + +void term_putc(term_t *t, uint_least32_t u) +{ + (void)u; + (void)t; +} diff --git a/term.h b/term.h @@ -2,6 +2,7 @@ #define TERM_H #include <stdint.h> +#include <stdbool.h> typedef struct { uint_least32_t u; /*!< Character code (UTF-8) */ @@ -65,5 +66,7 @@ void term_move_to(term_t *t, int x, int y); void term_clear_region(term_t *t, int x1, int y1, int x2, int y2); void term_swap_screen(term_t *t); void term_cursor(term_t *t, int mode); +int term_write(term_t *term, const char *buf, int len, bool show_ctrl); +void term_putc(term_t *t, uint_least32_t u); #endif /* TERM_H */ diff --git a/tty.c b/tty.c @@ -10,6 +10,7 @@ #include <sys/ioctl.h> #include <unistd.h> #include <fcntl.h> +#include <string.h> #define DUP_ALL(fd) \ do { \ @@ -86,3 +87,27 @@ void tty_dtr(tty_t *tty) close(tty->slave); close(tty->master); } + +int tty_read(tty_t *tty, term_t *t) +{ + static char buf[BUFSIZ]; + static int buf_len = 0; + int ret; + int written; + + ret = read(tty->master, buf + buf_len, sizeof(buf) - buf_len); + if (ret == -1) + { + ERROR("Can't read from shell"); + return -1; + } + + buf_len += ret; + written = term_write(t, buf, buf_len, false); + buf_len -= written; + + if (buf_len > 0) + memmove(buf, buf + written, buf_len); + + return ret; +} diff --git a/tty.h b/tty.h @@ -2,6 +2,8 @@ #define TTY_H #include <stdbool.h> +#include <stdlib.h> +#include "term.h" typedef struct { int master; @@ -11,5 +13,6 @@ typedef struct { bool tty_init(tty_t *tty); void tty_dtr(tty_t *tty); void tty_resize(tty_t *tty, int cols, int rows); +int tty_read(tty_t *tty, term_t *t); #endif /* TTY_H */ diff --git a/utils.h b/utils.h @@ -16,6 +16,11 @@ #define DIVCEIL(a, b) (((a) + ((b) - 1)) / (b)) #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x) #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec) * 1000 + (t1.tv_nsec - t2.tv_nsec) / 1E6) +#define IS_SET(mode, flag) ((mode & (flag)) != 0) +#define IS_CONTROLC0(c) (BETWEEN(c, 1, 0x1f) || (c) == '\177') +#define IS_CONTROLC1(c) (BETWEEN(c, 0x80, 0x9f)) +#define IS_CONTROL(c) (IS_CONTROLC0(c) || IS_CONTROLC1(c)) + static inline unsigned short sixd_to_16bit(int x) {