From 6ceb606a155d2950ca7e5fd2165997246494caeb Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 4 May 2014 21:52:59 -0400 Subject: more refactoring --- src/display.c | 96 +++++++++++++++++++++++++++++++------------------------ src/display.h | 14 ++++++++ src/pty-unix.c | 20 ++++++------ src/pty-unix.h | 4 +++ src/runes.h | 5 ++- src/screen.c | 12 ++++--- src/term.h | 16 +--------- src/window-xlib.c | 60 +++++++++++++++++----------------- src/window-xlib.h | 4 +-- 9 files changed, 128 insertions(+), 103 deletions(-) diff --git a/src/display.c b/src/display.c index 7c8d063..e8b8588 100644 --- a/src/display.c +++ b/src/display.c @@ -19,20 +19,21 @@ void runes_display_init(RunesTerm *t) void runes_display_set_window_size(RunesTerm *t) { + RunesDisplay *display = &t->display; int width, height; cairo_t *old_cr = NULL; cairo_surface_t *surface; runes_window_backend_get_size(t, &width, &height); - if (width == t->xpixel && height == t->ypixel) { + if (width == display->xpixel && height == display->ypixel) { return; } - t->xpixel = width; - t->ypixel = height; + display->xpixel = width; + display->ypixel = height; - old_cr = t->cr; + old_cr = display->cr; /* XXX this should really use cairo_surface_create_similar_image, but when * i did that, drawing calls would occasionally block until an X event @@ -40,11 +41,11 @@ void runes_display_set_window_size(RunesTerm *t) * create_similar_image does things that are more efficient (using some * xlib shm stuff) */ surface = cairo_image_surface_create( - CAIRO_FORMAT_RGB24, t->xpixel, t->ypixel); - t->cr = cairo_create(surface); + CAIRO_FORMAT_RGB24, display->xpixel, display->ypixel); + display->cr = cairo_create(surface); cairo_surface_destroy(surface); - if (t->layout) { - pango_cairo_update_layout(t->cr, t->layout); + if (display->layout) { + pango_cairo_update_layout(display->cr, display->layout); } else { PangoAttrList *attrs; @@ -53,26 +54,27 @@ void runes_display_set_window_size(RunesTerm *t) attrs = pango_attr_list_new(); font_desc = pango_font_description_from_string(t->config.font_name); - t->layout = pango_cairo_create_layout(t->cr); - pango_layout_set_attributes(t->layout, attrs); - pango_layout_set_font_description(t->layout, font_desc); + display->layout = pango_cairo_create_layout(display->cr); + pango_layout_set_attributes(display->layout, attrs); + pango_layout_set_font_description(display->layout, font_desc); pango_attr_list_unref(attrs); pango_font_description_free(font_desc); } - cairo_save(t->cr); + cairo_save(display->cr); if (old_cr) { - cairo_set_source_surface(t->cr, cairo_get_target(old_cr), 0.0, 0.0); + cairo_set_source_surface( + display->cr, cairo_get_target(old_cr), 0.0, 0.0); } else { - cairo_set_source(t->cr, t->config.bgdefault); + cairo_set_source(display->cr, t->config.bgdefault); } - cairo_paint(t->cr); + cairo_paint(display->cr); - cairo_restore(t->cr); + cairo_restore(display->cr); if (old_cr) { cairo_destroy(old_cr); @@ -106,6 +108,8 @@ void runes_display_draw_screen(RunesTerm *t) void runes_display_draw_cursor(RunesTerm *t, cairo_t *cr) { + RunesDisplay *display = &t->display; + if (!t->scr.hide_cursor) { int row = t->scr.grid->cur.row, col = t->scr.grid->cur.col; @@ -115,13 +119,13 @@ void runes_display_draw_cursor(RunesTerm *t, cairo_t *cr) cairo_save(cr); cairo_set_source(cr, t->config.cursorcolor); - if (t->unfocused) { + if (display->unfocused) { cairo_set_line_width(cr, 1); cairo_rectangle( cr, - col * t->fontx + 0.5, - (row + t->w.row_visible_offset) * t->fonty + 0.5, - t->fontx - 1, t->fonty - 1); + col * display->fontx + 0.5, + (row + display->row_visible_offset) * display->fonty + 0.5, + display->fontx - 1, display->fonty - 1); cairo_stroke(cr); } else { @@ -129,14 +133,14 @@ void runes_display_draw_cursor(RunesTerm *t, cairo_t *cr) cairo_rectangle( cr, - col * t->fontx, - (row + t->w.row_visible_offset) * t->fonty, - t->fontx, t->fonty); + col * display->fontx, + (row + display->row_visible_offset) * display->fonty, + display->fontx, display->fonty); cairo_fill(cr); runes_display_draw_glyph( t, cr, t->config.bgdefault, cell->attrs, cell->contents, cell->len, - row + t->w.row_visible_offset, col); + row + display->row_visible_offset, col); } cairo_restore(cr); } @@ -144,21 +148,24 @@ void runes_display_draw_cursor(RunesTerm *t, cairo_t *cr) void runes_display_cleanup(RunesTerm *t) { - g_object_unref(t->layout); - cairo_destroy(t->cr); + RunesDisplay *display = &t->display; + + g_object_unref(display->layout); + cairo_destroy(display->cr); } static void runes_display_recalculate_font_metrics(RunesTerm *t) { + RunesDisplay *display = &t->display; PangoFontDescription *desc; PangoContext *context; PangoFontMetrics *metrics; int ascent, descent; - if (t->layout) { + if (display->layout) { desc = (PangoFontDescription *)pango_layout_get_font_description( - t->layout); - context = pango_layout_get_context(t->layout); + display->layout); + context = pango_layout_get_context(display->layout); } else { desc = pango_font_description_from_string(t->config.font_name); @@ -168,14 +175,14 @@ static void runes_display_recalculate_font_metrics(RunesTerm *t) metrics = pango_context_get_metrics(context, desc, NULL); - t->fontx = PANGO_PIXELS( + display->fontx = PANGO_PIXELS( pango_font_metrics_get_approximate_digit_width(metrics)); ascent = pango_font_metrics_get_ascent(metrics); descent = pango_font_metrics_get_descent(metrics); - t->fonty = PANGO_PIXELS(ascent + descent); + display->fonty = PANGO_PIXELS(ascent + descent); pango_font_metrics_unref(metrics); - if (!t->layout) { + if (!display->layout) { pango_font_description_free(desc); g_object_unref(context); } @@ -183,7 +190,8 @@ static void runes_display_recalculate_font_metrics(RunesTerm *t) static int runes_display_draw_cell(RunesTerm *t, int row, int col) { - struct runes_cell *cell = &t->scr.grid->rows[row + t->scr.grid->row_top - t->w.row_visible_offset].cells[col]; + RunesDisplay *display = &t->display; + struct runes_cell *cell = &t->scr.grid->rows[row + t->scr.grid->row_top - display->row_visible_offset].cells[col]; cairo_pattern_t *bg = NULL, *fg = NULL; int bg_is_custom = 0, fg_is_custom = 0; @@ -238,11 +246,12 @@ static int runes_display_draw_cell(RunesTerm *t, int row, int col) } runes_display_paint_rectangle( - t, t->cr, bg, row, col, cell->is_wide ? 2 : 1, 1); + t, display->cr, bg, row, col, cell->is_wide ? 2 : 1, 1); if (cell->len) { runes_display_draw_glyph( - t, t->cr, fg, cell->attrs, cell->contents, cell->len, row, col); + t, display->cr, fg, cell->attrs, cell->contents, cell->len, + row, col); } if (bg_is_custom) { @@ -260,11 +269,13 @@ static void runes_display_paint_rectangle( RunesTerm *t, cairo_t *cr, cairo_pattern_t *pattern, int row, int col, int width, int height) { + RunesDisplay *display = &t->display; + cairo_save(cr); cairo_set_source(cr, pattern); cairo_rectangle( - cr, col * t->fontx, row * t->fonty, - width * t->fontx, height * t->fonty); + cr, col * display->fontx, row * display->fonty, + width * display->fontx, height * display->fonty); cairo_fill(cr); cairo_restore(cr); } @@ -273,9 +284,10 @@ static void runes_display_draw_glyph( RunesTerm *t, cairo_t *cr, cairo_pattern_t *pattern, struct runes_cell_attrs attrs, char *buf, size_t len, int row, int col) { + RunesDisplay *display = &t->display; PangoAttrList *pango_attrs; - pango_attrs = pango_layout_get_attributes(t->layout); + pango_attrs = pango_layout_get_attributes(display->layout); if (t->config.bold_is_bold) { pango_attr_list_change( pango_attrs, pango_attr_weight_new( @@ -289,10 +301,10 @@ static void runes_display_draw_glyph( attrs.underline ? PANGO_UNDERLINE_SINGLE : PANGO_UNDERLINE_NONE)); cairo_save(cr); - cairo_move_to(cr, col * t->fontx, row * t->fonty); + cairo_move_to(cr, col * display->fontx, row * display->fonty); cairo_set_source(cr, pattern); - pango_layout_set_text(t->layout, buf, len); - pango_cairo_update_layout(cr, t->layout); - pango_cairo_show_layout(cr, t->layout); + pango_layout_set_text(display->layout, buf, len); + pango_cairo_update_layout(cr, display->layout); + pango_cairo_show_layout(cr, display->layout); cairo_restore(cr); } diff --git a/src/display.h b/src/display.h index 182bef7..1a63af0 100644 --- a/src/display.h +++ b/src/display.h @@ -1,6 +1,20 @@ #ifndef _RUNES_DISPLAY_H #define _RUNES_DISPLAY_H +struct runes_display { + cairo_t *cr; + PangoLayout *layout; + + int row_visible_offset; + + int xpixel; + int ypixel; + int fontx; + int fonty; + + char unfocused: 1; +}; + void runes_display_init(RunesTerm *t); void runes_display_set_window_size(RunesTerm *t); void runes_display_draw_screen(RunesTerm *t); diff --git a/src/pty-unix.c b/src/pty-unix.c index 59636e7..82a3809 100644 --- a/src/pty-unix.c +++ b/src/pty-unix.c @@ -101,16 +101,16 @@ void runes_pty_backend_set_window_size(RunesTerm *t) size.ws_row = t->scr.grid->max.row; size.ws_col = t->scr.grid->max.col; - size.ws_xpixel = t->xpixel; - size.ws_ypixel = t->ypixel; + size.ws_xpixel = t->display.xpixel; + size.ws_ypixel = t->display.ypixel; ioctl(t->pty.master, TIOCSWINSZ, &size); } void runes_pty_backend_write(RunesTerm *t, char *buf, size_t len) { write(t->pty.master, buf, len); - if (t->w.row_visible_offset != 0) { - t->w.row_visible_offset = 0; + if (t->display.row_visible_offset != 0) { + t->display.row_visible_offset = 0; t->scr.dirty = 1; runes_window_backend_request_flush(t); } @@ -134,23 +134,25 @@ static void runes_pty_backend_read(uv_work_t *req) { RunesLoopData *data = req->data; RunesTerm *t = data->t; + RunesPtyBackend *pty = &t->pty; runes_window_backend_request_flush(t); - t->readlen = read( - t->pty.master, t->readbuf + t->remaininglen, - RUNES_READ_BUFFER_LENGTH - t->remaininglen); + pty->readlen = read( + pty->master, pty->readbuf + pty->remaininglen, + RUNES_READ_BUFFER_LENGTH - pty->remaininglen); } static void runes_pty_backend_got_data(uv_work_t *req, int status) { RunesLoopData *data = req->data; RunesTerm *t = data->t; + RunesPtyBackend *pty = &t->pty; UNUSED(status); - if (t->readlen > 0) { + if (pty->readlen > 0) { runes_screen_process_string( - t, t->readbuf, t->readlen + t->remaininglen); + t, pty->readbuf, pty->readlen + pty->remaininglen); uv_queue_work( t->loop, req, runes_pty_backend_read, runes_pty_backend_got_data); } diff --git a/src/pty-unix.h b/src/pty-unix.h index 0d96aa9..cf9fdf6 100644 --- a/src/pty-unix.h +++ b/src/pty-unix.h @@ -5,6 +5,10 @@ struct runes_pty { int master; int slave; pid_t child_pid; + + char readbuf[RUNES_READ_BUFFER_LENGTH]; + int readlen; + int remaininglen; }; void runes_pty_backend_spawn_subprocess(RunesTerm *t); diff --git a/src/runes.h b/src/runes.h index d30bbba..05398e9 100644 --- a/src/runes.h +++ b/src/runes.h @@ -12,6 +12,7 @@ struct runes_window; struct runes_pty; struct runes_screen; struct runes_config; +struct runes_display; struct runes_loop_data; typedef struct runes_term RunesTerm; @@ -19,6 +20,7 @@ typedef struct runes_window RunesWindowBackend; typedef struct runes_pty RunesPtyBackend; typedef struct runes_screen RunesScreen; typedef struct runes_config RunesConfig; +typedef struct runes_display RunesDisplay; typedef struct runes_loop_data RunesLoopData; struct runes_loop_data { @@ -31,9 +33,10 @@ struct runes_loop_data { #include "screen.h" #include "config.h" -#include "term.h" #include "display.h" +#include "term.h" + #define UNUSED(x) ((void)x) #endif diff --git a/src/screen.c b/src/screen.c index 86a29c0..ebff44b 100644 --- a/src/screen.c +++ b/src/screen.c @@ -27,8 +27,8 @@ void runes_screen_set_window_size(RunesTerm *t) old_size.row = scr->grid->max.row; old_size.col = scr->grid->max.col; - scr->grid->max.row = t->ypixel / t->fonty; - scr->grid->max.col = t->xpixel / t->fontx; + scr->grid->max.row = t->display.ypixel / t->display.fonty; + scr->grid->max.col = t->display.xpixel / t->display.fontx; if (scr->grid->max.row == 0) { scr->grid->max.row = 1; @@ -90,9 +90,11 @@ void runes_screen_process_string(RunesTerm *t, char *buf, size_t len) runes_parser_yylex_init_extra(t, &scanner); state = runes_parser_yy_scan_bytes(buf, len, scanner); remaining = runes_parser_yylex(scanner); - t->remaininglen = remaining; - if (t->remaininglen) { - memmove(t->readbuf, &buf[len - t->remaininglen], t->remaininglen); + t->pty.remaininglen = remaining; + if (t->pty.remaininglen) { + memmove( + t->pty.readbuf, &buf[len - t->pty.remaininglen], + t->pty.remaininglen); } runes_parser_yy_delete_buffer(state, scanner); runes_parser_yylex_destroy(scanner); diff --git a/src/term.h b/src/term.h index 41f1456..2b1ad93 100644 --- a/src/term.h +++ b/src/term.h @@ -6,23 +6,9 @@ struct runes_term { RunesPtyBackend pty; RunesScreen scr; RunesConfig config; + RunesDisplay display; - cairo_t *cr; uv_loop_t *loop; - - PangoLayout *layout; - - char readbuf[RUNES_READ_BUFFER_LENGTH]; - int readlen; - int remaininglen; - - int xpixel; - int ypixel; - int fontx; - int fonty; - - char visual_bell_is_ringing: 1; - char unfocused: 1; }; void runes_term_init(RunesTerm *t, int argc, char *argv[]); diff --git a/src/window-xlib.c b/src/window-xlib.c index ee4beaa..e248f98 100644 --- a/src/window-xlib.c +++ b/src/window-xlib.c @@ -131,12 +131,12 @@ void runes_window_backend_create_window(RunesTerm *t, int argc, char *argv[]) normal_hints.flags = PMinSize | PResizeInc | PBaseSize; - normal_hints.min_width = t->fontx + 4; - normal_hints.min_height = t->fonty + 4; - normal_hints.width_inc = t->fontx; - normal_hints.height_inc = t->fonty; - normal_hints.base_width = t->fontx * t->config.default_cols + 4; - normal_hints.base_height = t->fonty * t->config.default_rows + 4; + normal_hints.min_width = t->display.fontx + 4; + normal_hints.min_height = t->display.fonty + 4; + normal_hints.width_inc = t->display.fontx; + normal_hints.height_inc = t->display.fonty; + normal_hints.base_width = t->display.fontx * t->config.default_cols + 4; + normal_hints.base_height = t->display.fonty * t->config.default_rows + 4; cairo_pattern_get_rgba(t->config.bgdefault, &bg_r, &bg_g, &bg_b, NULL); bgcolor.red = bg_r * 65535; @@ -451,7 +451,7 @@ static void runes_window_backend_resize_window( height = 1; } - if (width != t->xpixel || height != t->ypixel) { + if (width != t->display.xpixel || height != t->display.ypixel) { XResizeWindow(w->dpy, w->w, width - 4, height - 4); cairo_xlib_surface_set_size( cairo_get_target(w->backend_cr), width - 4, height - 4); @@ -485,13 +485,14 @@ static void runes_window_backend_flush(RunesTerm *t) t->scr.update_icon_name = 0; } - if (t->visual_bell_is_ringing) { + if (w->visual_bell_is_ringing) { return; } runes_display_draw_screen(t); - cairo_set_source_surface(w->backend_cr, cairo_get_target(t->cr), 0.0, 0.0); + cairo_set_source_surface( + w->backend_cr, cairo_get_target(t->display.cr), 0.0, 0.0); cairo_paint(w->backend_cr); runes_display_draw_cursor(t, w->backend_cr); cairo_surface_flush(cairo_get_target(w->backend_cr)); @@ -499,19 +500,18 @@ static void runes_window_backend_flush(RunesTerm *t) static void runes_window_backend_visible_scroll(RunesTerm *t, int count) { - RunesWindowBackend *w = &t->w; int min = 0, max = t->scr.grid->row_count - t->scr.grid->max.row; - int old_offset = w->row_visible_offset; + int old_offset = t->display.row_visible_offset; - w->row_visible_offset += count; - if (w->row_visible_offset < min) { - w->row_visible_offset = min; + t->display.row_visible_offset += count; + if (t->display.row_visible_offset < min) { + t->display.row_visible_offset = min; } - if (w->row_visible_offset > max) { - w->row_visible_offset = max; + if (t->display.row_visible_offset > max) { + t->display.row_visible_offset = max; } - if (w->row_visible_offset == old_offset) { + if (t->display.row_visible_offset == old_offset) { return; } @@ -521,15 +521,16 @@ static void runes_window_backend_visible_scroll(RunesTerm *t, int count) static void runes_window_backend_visual_bell(RunesTerm *t) { + RunesWindowBackend *w = &t->w; + if (t->config.bell_is_urgent) { runes_window_backend_set_urgent(t); } - if (!t->visual_bell_is_ringing) { - RunesWindowBackend *w = &t->w; + if (!w->visual_bell_is_ringing) { uv_timer_t *timer_req; - t->visual_bell_is_ringing = 1; + w->visual_bell_is_ringing = 1; cairo_set_source(w->backend_cr, t->config.fgdefault); cairo_paint(w->backend_cr); cairo_surface_flush(cairo_get_target(w->backend_cr)); @@ -546,9 +547,10 @@ static void runes_window_backend_visual_bell(RunesTerm *t) static void runes_window_backend_reset_visual_bell(uv_timer_t *handle) { RunesTerm *t = handle->data; + RunesWindowBackend *w = &t->w; runes_window_backend_request_flush(t); - t->visual_bell_is_ringing = 0; + w->visual_bell_is_ringing = 0; uv_close( (uv_handle_t *)handle, runes_window_backend_visual_bell_free_handle); } @@ -750,8 +752,8 @@ static void runes_window_backend_handle_button_event( sprintf( response, "\e[M%c%c%c", ' ' + (status), - ' ' + (e->x / t->fontx + 1), - ' ' + (e->y / t->fonty + 1)); + ' ' + (e->x / t->display.fontx + 1), + ' ' + (e->y / t->display.fonty + 1)); runes_pty_backend_write(t, response, 6); } else if (t->scr.mouse_reporting_press && e->type == ButtonPress) { @@ -760,8 +762,8 @@ static void runes_window_backend_handle_button_event( sprintf( response, "\e[M%c%c%c", ' ' + (e->button - 1), - ' ' + (e->x / t->fontx + 1), - ' ' + (e->y / t->fonty + 1)); + ' ' + (e->x / t->display.fontx + 1), + ' ' + (e->y / t->display.fonty + 1)); runes_pty_backend_write(t, response, 6); } else { @@ -826,19 +828,19 @@ static void runes_window_backend_handle_focus_event( return; } - if (e->type == FocusIn && !t->unfocused) { + if (e->type == FocusIn && !t->display.unfocused) { return; } - if (e->type == FocusOut && t->unfocused) { + if (e->type == FocusOut && t->display.unfocused) { return; } runes_window_backend_clear_urgent(t); if (e->type == FocusIn) { - t->unfocused = 0; + t->display.unfocused = 0; } else { - t->unfocused = 1; + t->display.unfocused = 1; } runes_window_backend_flush(t); } diff --git a/src/window-xlib.h b/src/window-xlib.h index 1989cfd..70f7516 100644 --- a/src/window-xlib.h +++ b/src/window-xlib.h @@ -25,9 +25,9 @@ struct runes_window { cairo_t *backend_cr; - int row_visible_offset; - Atom atoms[RUNES_NUM_ATOMS]; + + char visual_bell_is_ringing: 1; }; typedef struct { -- cgit v1.2.3-54-g00ecf