lt

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

commit 3aaf3b730a949e74109d955a84932f12050234ae
parent 562aa0b3d211de8ed9d088ddb3e67f0923cc3786
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri,  8 Jun 2018 11:24:33 +0200

NEW: Basic run / render while

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

Diffstat:
Mconfig.h | 4++++
Mmain.c | 5+++--
Mrun.c | 63++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mutils.h | 1+
Mx.c | 1+
Mx.h | 1+
6 files changed, 72 insertions(+), 3 deletions(-)

diff --git a/config.h b/config.h @@ -17,5 +17,9 @@ // CF: linux/Documentation/CodingStyle #define TABSPACES 8 #define SHELL "/bin/sh" +#define DEF_COL 80 +#define DEF_ROW 25 /* Deathrow, haha, get it? */ +#define ACTION_FPS 30 +#define XFPS 120 #endif /* CONFIG_H */ diff --git a/main.c b/main.c @@ -2,6 +2,7 @@ #include <locale.h> #include "run.h" +#include "config.h" void lt_setenv(x_window_t *x) { @@ -18,8 +19,8 @@ int main(void) setlocale(LC_CTYPE, ""); XSetLocaleModifiers(""); - x_window_init(&lt.x, 80, 25); - term_init(&lt.t, 80, 25); + x_window_init(&lt.x, DEF_COL, DEF_ROW); + term_init(&lt.t, DEF_COL, DEF_ROW); tty_init(&lt.tty); lt_setenv(&lt.x); diff --git a/run.c b/run.c @@ -1,6 +1,67 @@ +#include <time.h> + #include "run.h" +#include "config.h" +#include "utils.h" + +static void fps_render(lt_t *lt, fd_set *readable, struct timespec *tv) +{ + static struct timespec last = { 0 }; + static int xev = ACTION_FPS; + XEvent ev; + struct timespec now; + bool draw = false; + + if (last.tv_sec == 0 && last.tv_nsec == 0) + clock_gettime(CLOCK_MONOTONIC, &last); + + if (FD_ISSET(lt->x.fd, readable)) + xev = ACTION_FPS; + + clock_gettime(CLOCK_MONOTONIC, &now); + tv->tv_sec = 0; + tv->tv_nsec = (1000 * 1E6) / XFPS; + + if (TIMEDIFF(now, last) > 1000 / (xev ? XFPS : ACTION_FPS)) + { + draw = true; + last = now; + } + + if (draw) + { + // events + // render + } +} + void run(lt_t *lt) { - (void)lt; + int max_fd; + size_t size; + fd_set readable; + struct timespec timeout = { 0 }; + + max_fd = MAX(lt->tty.master, lt->x.fd); + + while (true) + { + FD_ZERO(&readable); + FD_SET(lt->tty.master, &readable); + FD_SET(lt->x.fd, &readable); + + if (pselect(max_fd + 1, &readable, NULL, NULL, &timeout, NULL) == -1) + { + ERROR("pselect() error"); + return ; + } + + if (FD_ISSET(lt->tty.master, &readable)) + { + /*tty_read(&lt->tty);*/ + } + + fps_render(lt, &readable, &timeout); + } } diff --git a/utils.h b/utils.h @@ -15,6 +15,7 @@ #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b)) #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) static inline unsigned short sixd_to_16bit(int x) { diff --git a/x.c b/x.c @@ -223,6 +223,7 @@ bool x_window_init(x_window_t *x, int cols, int rows) x->screen = XDefaultScreen(x->dsp); x->vis = XDefaultVisual(x->dsp, x->screen); + x->fd = ConnectionNumber(x->dsp); if (!fonts_load(x, "Inconsolata", 12)) goto error; diff --git a/x.h b/x.h @@ -15,6 +15,7 @@ struct x_window_s { Colormap cmap; /*!< Color Map */ Window win; /*!< Main Window */ Drawable buf; /*!< Drawing buffer */ + int fd; /*!< Connection number */ XftDraw *draw; /*!< Drawing context */ Visual *vis; /*!< Visual context */ XSetWindowAttributes attrs; /*!< Window attributes */