From: John Tsiombikas Date: Sun, 24 Nov 2019 11:49:28 +0000 (+0200) Subject: foo X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=visor;a=commitdiff_plain;h=519bb5db64dd400724d8beedd10bf54d8cf2b7af foo --- diff --git a/libvisor/include/visor.h b/libvisor/include/visor.h index 58b6ac4..4d2233a 100644 --- a/libvisor/include/visor.h +++ b/libvisor/include/visor.h @@ -111,6 +111,9 @@ void vi_destroy(struct visor *vi); void vi_set_fileops(struct visor *vi, struct vi_fileops *fop); void vi_set_ttyops(struct visor *vi, struct vi_ttyops *tty); +void vi_window(struct visor *vi, int xsz, int ysz); +void vi_redraw(struct visor *vi); + /* vi_new_buf creates a new buffer and inserts it in the buffer list. If the * path pointer is null, the new buffer will be empty, otherwise it's as if it * was followed by a vi_buf_read call to read a file into the buffer. diff --git a/libvisor/src/vimpl.h b/libvisor/src/vimpl.h index c7b8ab4..22f4252 100644 --- a/libvisor/src/vimpl.h +++ b/libvisor/src/vimpl.h @@ -26,6 +26,8 @@ struct visor { struct vi_alloc mm; struct vi_ttyops tty; void *tty_cls; + + int term_width, term_height; }; struct vi_buffer { diff --git a/libvisor/src/visor.c b/libvisor/src/visor.c index 12b5a02..3e1df92 100644 --- a/libvisor/src/visor.c +++ b/libvisor/src/visor.c @@ -33,6 +33,20 @@ along with this program. If not, see . #define vi_write vi->fop.write #define vi_seek vi->fop.seek +#define vi_clear() vi->tty.clear(vi->tty_cls) +#define vi_clear_line() vi->tty.clear_line(vi->tty_cls) +#define vi_clear_line_at(y) vi->tty.clear_line_at(y, vi->tty_cls) +#define vi_setcursor(x, y) vi->tty.setcursor(x, y, vi->tty_cls) +#define vi_putchar(c) vi->tty.putchar(c, vi->tty_cls) +#define vi_putchar_at(x, y, c) v->tty.putchar_at(x, y, c, vi->tty_cls) +#define vi_scroll(n) vi->tty.scroll(n, vi->tty_cls) +#define vi_del_back() vi->tty.del_back(vi->tty_cls) +#define vi_del_fwd() vi->tty.del_fwd(vi->tty_cls) +#define vi_status(s) vi->tty.status(s, vi->tty_cls) + +static int remove_buf(struct visor *vi, struct vi_buffer *vb); +static int add_span(struct vi_buffer *vb, vi_addr at, int src, vi_addr start, unsigned long size); + #ifdef HAVE_LIBC static const struct vi_alloc stdalloc = { malloc, free, realloc }; #endif @@ -53,6 +67,9 @@ struct visor *vi_create(struct vi_alloc *mm) memset(vi, 0, sizeof *vi); vi->mm = *mm; + vi->term_width = 80; + vi->term_height = 24; + return vi; } @@ -74,6 +91,30 @@ void vi_set_ttyops(struct visor *vi, struct vi_ttyops *tty) vi->tty = *tty; } +void vi_term_size(struct visor *vi, int xsz, int ysz) +{ + vi->term_width = xsz; + vi->term_height = ysz; +} + +void vi_redraw(struct visor *vi) +{ + struct vi_buffer *vb; + struct vi_span *sp, *spend; + vi_addr spoffs; + + vb = vi->buflist; + if(!(sp = vi_buf_find_span(vb, vi->view_start, &spoffs))) { + sp = vb->spans; + spoffs = 0; + } + spend = vb->spans + vb->num_spans; + + vi_clear(); + + for(i=0; i +#include +#include #include #include +#include #include #include #include @@ -11,8 +14,11 @@ static void sighandler(int s); static int term_width, term_height; static int ttyfd = -1; +static int selfpipe[2]; static struct termios saved_term; +static void (*cb_resized)(int, int); + int term_init(const char *ttypath) { @@ -42,6 +48,8 @@ int term_init(const char *ttypath) term_width = winsz.ws_col; term_height = winsz.ws_row; + pipe(selfpipe); + signal(SIGWINCH, sighandler); return 0; } @@ -53,9 +61,84 @@ void term_cleanup(void) ttyfd = -1; } +void term_getsize(int *width, int *height) +{ + *width = term_width; + *height = term_height; +} + +void term_resize_func(void (*func)(int, int)) +{ + cb_resized = func; +} + + +static char termbuf[1024]; +static int termbuf_len; + +void term_send(const char *s, int size) +{ + if(size >= sizeof termbuf) { + /* too large, just flush the buffer and write directly to the tty */ + term_flush(); + write(ttyfd, s, size); + } else { + if(size >= sizeof termbuf - termbuf_len) { + term_flush(); + } + memcpy(termbuf + termbuf_len, s, size); + termbuf_len += size; + } +} + +void term_puts(const char *s) +{ + term_send(s, strlen(s)); +} + +void term_printf(const char *fmt, ...) +{ + static char *buf; + static long bufsz; + va_list ap; + long len; + + if(!buf) { + bufsz = 512; + if(!(buf = malloc(bufsz))) { + return; + } + } + + for(;;) { + va_start(ap, fmt); + len = vsnprintf(buf, bufsz, fmt, ap); + va_end(ap); + + if(len < bufsz) break; + if(len < 0) { + void *tmp; + long n = bufsz << 1; + if(!(tmp = realloc(buf, n))) { + break; /* if realloc fails, will result in truncated output */ + } + } + } + + term_send(buf, len); +} + +void term_flush(void) +{ + if(termbuf_len > 0) { + write(ttyfd, termbuf, termbuf_len); + termbuf_len = 0; + } +} + void term_clear(void) { - write(ttyfd, "\033[2J", 4); + term_puts("\033[2J"); } int term_getchar(void) diff --git a/visor/src/term.h b/visor/src/term.h index a5cc334..82ccd16 100644 --- a/visor/src/term.h +++ b/visor/src/term.h @@ -1,9 +1,50 @@ #ifndef TERM_H_ #define TERM_H_ +enum term_event_type { + TERM_EV_KEY, + TERM_EV_RESIZE, + TERM_EV_MBUTTON, + TERM_EV_MMOTION +}; + +struct term_ev_any { + int type; +}; + +struct term_ev_resize { + int type; + int width, height; +}; + +struct term_ev_mbutton { + int type; + /* TODO */ +}; + +struct term_ev_mmotion { + int type; + int x, y; +}; + +union term_event { + struct term_ev_any any; + struct term_ev_resize resize; + struct term_ev_mbutton mbutton; + struct term_ev_mmotion mmotion; +}; + int term_init(const char *ttypath); void term_cleanup(void); +void term_getsize(int *width, int *height); +void term_resize_func(void (*func)(int, int)); + +void term_send(const char *s, int size); +void term_puts(const char *s); +void term_printf(const char *fmt, ...); +void term_flush(void); + void term_clear(void); int term_getchar(void);