From fad4734c206235ffcf52903e01f23d24f1e1e087 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 10 Apr 2014 19:14:41 -0400 Subject: split terminal processing out to a separate file --- Makefile | 2 +- display.c | 46 ++++++++++++++++++++++++++-------------------- display.h | 4 +++- events.c | 2 +- runes.h | 1 + vt100.c | 20 ++++++++++++++++++++ vt100.h | 6 ++++++ 7 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 vt100.c create mode 100644 vt100.h diff --git a/Makefile b/Makefile index 397dd7f..003027b 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ OUT = runes -OBJ = runes.o display.o term.o events.o window-xlib.o pty-unix.o +OBJ = runes.o display.o term.o events.o vt100.o window-xlib.o pty-unix.o LIBS = cairo cairo-xlib libuv CFLAGS ?= -g -Wall -Wextra -Werror LDFLAGS ?= -g -Wall -Wextra -Werror diff --git a/display.c b/display.c index da15311..2766545 100644 --- a/display.c +++ b/display.c @@ -4,19 +4,16 @@ void runes_display_init(RunesTerm *t) { - cairo_font_extents_t extents; - cairo_select_font_face(t->cr, "monospace", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); cairo_set_font_size(t->cr, 14.0); cairo_set_source_rgb(t->cr, 0.0, 0.0, 1.0); - cairo_font_extents(t->cr, &extents); - cairo_move_to(t->cr, 0.0, extents.ascent); + runes_display_move_to(t, 0, 0); runes_pty_backend_set_window_size(t); } -static void runes_display_get_font_dimensions(RunesTerm *t, double *fontx, double *fonty) +static void runes_display_get_font_dimensions(RunesTerm *t, double *fontx, double *fonty, double *ascent) { cairo_font_extents_t extents; @@ -24,34 +21,43 @@ static void runes_display_get_font_dimensions(RunesTerm *t, double *fontx, doubl *fontx = extents.max_x_advance; *fonty = extents.height; + *ascent = extents.ascent; } void runes_display_get_term_size(RunesTerm *t, int *row, int *col, int *xpixel, int *ypixel) { - double fontx, fonty; + double fontx, fonty, ascent; runes_window_backend_get_size(t, xpixel, ypixel); - runes_display_get_font_dimensions(t, &fontx, &fonty); + runes_display_get_font_dimensions(t, &fontx, &fonty, &ascent); *row = (int)(*ypixel / fonty); *col = (int)(*xpixel / fontx); } -void runes_display_glyph(RunesTerm *t, char *buf, size_t len) +void runes_display_get_position(RunesTerm *t, int *row, int *col) { - if (len) { - cairo_font_extents_t extents; + double x, y; + double fontx, fonty, ascent; - cairo_font_extents(t->cr, &extents); + cairo_get_current_point(t->cr, &x, &y); + runes_display_get_font_dimensions(t, &fontx, &fonty, &ascent); - char *nl; + *row = (y - ascent) / fonty; + *col = x / fontx; +} + +void runes_display_move_to(RunesTerm *t, int row, int col) +{ + double fontx, fonty, ascent; + + runes_display_get_font_dimensions(t, &fontx, &fonty, &ascent); + + cairo_move_to(t->cr, col * fontx, row * fonty + ascent); +} + +void runes_display_show_string(RunesTerm *t, char *buf, size_t len) +{ + if (len) { buf[len] = '\0'; - while ((nl = strchr(buf, '\n'))) { - double x, y; - *nl = '\0'; - cairo_show_text(t->cr, buf); - buf = nl + 1; - cairo_get_current_point(t->cr, &x, &y); - cairo_move_to(t->cr, 0.0, y + extents.height); - } cairo_show_text(t->cr, buf); /* we have to flush manually because XNextEvent (which normally handles * flushing) will most likely be called again before the keystroke is diff --git a/display.h b/display.h index 837032f..2ea1d1b 100644 --- a/display.h +++ b/display.h @@ -3,6 +3,8 @@ void runes_display_init(RunesTerm *t); void runes_display_get_term_size(RunesTerm *t, int *row, int *col, int *xpixel, int *ypixel); -void runes_display_glyph(RunesTerm *t, char *buf, size_t len); +void runes_display_get_position(RunesTerm *t, int *row, int *col); +void runes_display_move_to(RunesTerm *t, int row, int col); +void runes_display_show_string(RunesTerm *t, char *buf, size_t len); #endif diff --git a/events.c b/events.c index 95eaa53..32ca790 100644 --- a/events.c +++ b/events.c @@ -7,7 +7,7 @@ void runes_handle_keyboard_event(RunesTerm *t, char *buf, size_t len) void runes_handle_pty_read(RunesTerm *t, char *buf, ssize_t len) { - runes_display_glyph(t, buf, len); + runes_vt100_process_string(t, buf, len); } void runes_handle_pty_close(RunesTerm *t) diff --git a/runes.h b/runes.h index 9b90b84..9233937 100644 --- a/runes.h +++ b/runes.h @@ -21,6 +21,7 @@ typedef struct runes_loop_data RunesLoopData; #include "term.h" #include "display.h" +#include "vt100.h" #define UNUSED(x) ((void)x) diff --git a/vt100.c b/vt100.c new file mode 100644 index 0000000..6e75379 --- /dev/null +++ b/vt100.c @@ -0,0 +1,20 @@ +#include + +#include "runes.h" + +void runes_vt100_process_string(RunesTerm *t, char *buf, size_t len) +{ + int row, col; + char *nl; + + runes_display_get_position(t, &row, &col); + + buf[len] = '\0'; + while ((nl = strchr(buf, '\n'))) { + *nl = '\0'; + runes_display_show_string(t, buf, strlen(buf)); + runes_display_move_to(t, ++row, 0); + buf = nl + 1; + } + runes_display_show_string(t, buf, strlen(buf)); +} diff --git a/vt100.h b/vt100.h new file mode 100644 index 0000000..3a692a4 --- /dev/null +++ b/vt100.h @@ -0,0 +1,6 @@ +#ifndef _RUNES_VT100_H +#define _RUNES_VT100_H + +void runes_vt100_process_string(RunesTerm *t, char *buf, size_t len); + +#endif -- cgit v1.2.3-54-g00ecf