foo
authorJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 24 Nov 2019 11:49:28 +0000 (13:49 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 24 Nov 2019 11:49:28 +0000 (13:49 +0200)
libvisor/include/visor.h
libvisor/src/vimpl.h
libvisor/src/visor.c
visor/src/main_unix.c
visor/src/term.c
visor/src/term.h

index 58b6ac4..4d2233a 100644 (file)
@@ -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_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.
 /* 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.
index c7b8ab4..22f4252 100644 (file)
@@ -26,6 +26,8 @@ struct visor {
        struct vi_alloc mm;
        struct vi_ttyops tty;
        void *tty_cls;
        struct vi_alloc mm;
        struct vi_ttyops tty;
        void *tty_cls;
+
+       int term_width, term_height;
 };
 
 struct vi_buffer {
 };
 
 struct vi_buffer {
index 12b5a02..3e1df92 100644 (file)
@@ -33,6 +33,20 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #define vi_write       vi->fop.write
 #define vi_seek                vi->fop.seek
 
 #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
 #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;
 
        memset(vi, 0, sizeof *vi);
        vi->mm = *mm;
 
+       vi->term_width = 80;
+       vi->term_height = 24;
+
        return vi;
 }
 
        return vi;
 }
 
@@ -74,6 +91,30 @@ void vi_set_ttyops(struct visor *vi, struct vi_ttyops *tty)
        vi->tty = *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<term_heig
+}
+
 struct vi_buffer *vi_new_buf(struct visor *vi, const char *path)
 {
        struct vi_buffer *nb;
 struct vi_buffer *vi_new_buf(struct visor *vi, const char *path)
 {
        struct vi_buffer *nb;
index 96d59cb..a9d6932 100644 (file)
@@ -16,6 +16,7 @@ struct file {
 static int parse_args(int argc, char **argv);
 static int init(void);
 static void cleanup(void);
 static int parse_args(int argc, char **argv);
 static int init(void);
 static void cleanup(void);
+static void resized(int x, int y);
 /* file operations */
 static vi_file *file_open(const char *path, unsigned int flags);
 static void file_close(vi_file *file);
 /* file operations */
 static vi_file *file_open(const char *path, unsigned int flags);
 static void file_close(vi_file *file);
@@ -58,6 +59,8 @@ int main(int argc, char **argv)
                return 1;
        }
 
                return 1;
        }
 
+       vi_redraw(vi);
+
        for(;;) {
                int c = term_getchar();
 
        for(;;) {
                int c = term_getchar();
 
@@ -108,7 +111,6 @@ static int init(void)
                if(!vi_new_buf(vi, fpaths[i])) {
                        return -1;
                }
                if(!vi_new_buf(vi, fpaths[i])) {
                        return -1;
                }
-               printf("loaded file: %s\n", fpaths[i]);
        }
        return 0;
 }
        }
        return 0;
 }
index d98d0ac..ee93e03 100644 (file)
@@ -1,6 +1,9 @@
 #include <stdio.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <signal.h>
 #include <errno.h>
 #include <signal.h>
 #include <errno.h>
+#include <stdarg.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <termios.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <termios.h>
@@ -11,8 +14,11 @@ static void sighandler(int s);
 
 static int term_width, term_height;
 static int ttyfd = -1;
 
 static int term_width, term_height;
 static int ttyfd = -1;
+static int selfpipe[2];
 static struct termios saved_term;
 
 static struct termios saved_term;
 
+static void (*cb_resized)(int, int);
+
 
 int term_init(const char *ttypath)
 {
 
 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;
 
        term_width = winsz.ws_col;
        term_height = winsz.ws_row;
 
+       pipe(selfpipe);
+
        signal(SIGWINCH, sighandler);
        return 0;
 }
        signal(SIGWINCH, sighandler);
        return 0;
 }
@@ -53,9 +61,84 @@ void term_cleanup(void)
        ttyfd = -1;
 }
 
        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)
 {
 void term_clear(void)
 {
-       write(ttyfd, "\033[2J", 4);
+       term_puts("\033[2J");
 }
 
 int term_getchar(void)
 }
 
 int term_getchar(void)
index a5cc334..82ccd16 100644 (file)
@@ -1,9 +1,50 @@
 #ifndef TERM_H_
 #define TERM_H_
 
 #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);
 
 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);
 
 void term_clear(void);
 int term_getchar(void);