From 3bbc0cc9f23ab314c8a1d79553d92df4584e4038 Mon Sep 17 00:00:00 2001 From: Randy Palamar Date: Tue, 21 May 2024 10:23:25 -0600 Subject: [PATCH] remove some ui pointer chasing There only exists a single Ui so there is no need to force a pointer redirection for accessing it. The Ui member was moved down in vis-core.h to punt around an issue with the way lua checks for existing objects. It may show up again as I flatten more structs. --- event-basic.c | 2 +- main.c | 4 ++-- ui-terminal-curses.c | 4 ++-- ui-terminal-vt100.c | 25 +++++++++------------ ui-terminal.c | 12 +++++----- ui.h | 3 ++- vis-cmds.c | 15 ++++++------- vis-core.h | 2 +- vis-lua.c | 12 +++++----- vis-prompt.c | 2 +- vis.c | 53 ++++++++++++++++++++++---------------------- vis.h | 4 ++-- 12 files changed, 65 insertions(+), 73 deletions(-) diff --git a/event-basic.c b/event-basic.c index cb2d2c191..d59ef8aa8 100644 --- a/event-basic.c +++ b/event-basic.c @@ -6,7 +6,7 @@ bool vis_event_emit(Vis *vis, enum VisEvents id, ...) { if (!vis->initialized) { vis->initialized = true; - ui_init(vis->ui, vis); + ui_init(&vis->ui, vis); } va_list ap; diff --git a/main.c b/main.c index d19b6f470..85330cbc5 100644 --- a/main.c +++ b/main.c @@ -1229,7 +1229,7 @@ static const char *macro_replay(Vis *vis, const char *keys, const Arg *arg) { } static const char *suspend(Vis *vis, const char *keys, const Arg *arg) { - ui_terminal_suspend(vis->ui); + ui_terminal_suspend(&vis->ui); return keys; } @@ -2214,7 +2214,7 @@ static void signal_handler(int signum, siginfo_t *siginfo, void *context) { } int main(int argc, char *argv[]) { - vis = vis_new(ui_terminal_new()); + vis = vis_new(); if (!vis) return EXIT_FAILURE; diff --git a/ui-terminal-curses.c b/ui-terminal-curses.c index 2b4a6fd6c..c3ae06488 100644 --- a/ui-terminal-curses.c +++ b/ui-terminal-curses.c @@ -276,8 +276,8 @@ static bool ui_term_backend_init(Ui *tui, char *term) { return true; } -static Ui *ui_term_backend_new(void) { - return calloc(1, sizeof(Ui)); +static bool ui_backend_init(Ui *ui) { + return true; } void ui_terminal_resume(Ui *term) { } diff --git a/ui-terminal-vt100.c b/ui-terminal-vt100.c index dffc23526..a1c92f35b 100644 --- a/ui-terminal-vt100.c +++ b/ui-terminal-vt100.c @@ -59,11 +59,6 @@ #define CELL_ATTR_ITALIC (1 << 4) #define CELL_ATTR_DIM (1 << 5) -typedef struct { - Ui uiterm; - Buffer buf; -} UiVt100; - static inline bool cell_color_equal(CellColor c1, CellColor c2) { if (c1.index != (uint8_t)-1 || c2.index != (uint8_t)-1) return c1.index == c2.index; @@ -97,7 +92,7 @@ static void cursor_visible(bool visible) { } static void ui_term_backend_blit(Ui *tui) { - Buffer *buf = &((UiVt100*)tui)->buf; + Buffer *buf = tui->ctx; buffer_clear(buf); CellAttr attr = CELL_ATTR_NORMAL; CellColor fg = CELL_COLOR_DEFAULT, bg = CELL_COLOR_DEFAULT; @@ -199,18 +194,20 @@ static bool ui_term_backend_init(Ui *tui, char *term) { return true; } -static Ui *ui_term_backend_new(void) { - UiVt100 *vtui = calloc(1, sizeof *vtui); - if (!vtui) - return NULL; - buffer_init(&vtui->buf); - return (Ui*)vtui; +static bool ui_backend_init(Ui *ui) { + Buffer *buf = calloc(1, sizeof(Buffer)); + if (!buf) + return false; + buffer_init(buf); + ui->ctx = buf; + return true; } static void ui_term_backend_free(Ui *tui) { - UiVt100 *vtui = (UiVt100*)tui; + Buffer *buf = tui->ctx; ui_term_backend_suspend(tui); - buffer_release(&vtui->buf); + buffer_release(buf); + free(buf); } static bool is_default_color(CellColor c) { diff --git a/ui-terminal.c b/ui-terminal.c index 048ba7fa6..d19724ab1 100644 --- a/ui-terminal.c +++ b/ui-terminal.c @@ -615,20 +615,19 @@ bool ui_init(Ui *tui, Vis *vis) { return false; } -Ui *ui_terminal_new(void) { +bool ui_terminal_init(Ui *tui) { size_t styles_size = UI_STYLE_MAX * sizeof(CellStyle); CellStyle *styles = calloc(1, styles_size); if (!styles) - return NULL; - Ui *tui = ui_term_backend_new(); - if (!tui) { + return false; + if (!ui_backend_init(tui)) { free(styles); - return NULL; + return false; } tui->styles_size = styles_size; tui->styles = styles; tui->doupdate = true; - return tui; + return true; } void ui_terminal_free(Ui *tui) { @@ -641,5 +640,4 @@ void ui_terminal_free(Ui *tui) { termkey_destroy(tui->termkey); free(tui->cells); free(tui->styles); - free(tui); } diff --git a/ui.h b/ui.h index 806b9ce03..c03a7096f 100644 --- a/ui.h +++ b/ui.h @@ -107,6 +107,7 @@ typedef struct Ui { size_t cells_size; /* #bytes allocated for 2D grid (grows only) */ Cell *cells; /* 2D grid of cells, at least as large as current terminal size */ bool doupdate; /* Whether to update the screen after refreshing contents */ + void *ctx; /* Any additional data needed by the backend */ } Ui; #include "view.h" @@ -115,7 +116,7 @@ typedef struct Ui { #define UI_OPTIONS_GET(ui) ((ui) ? (ui)->options : 0) -Ui *ui_terminal_new(void); +bool ui_terminal_init(Ui*); int ui_terminal_colors(void); void ui_terminal_free(Ui*); void ui_terminal_restore(Ui*); diff --git a/vis-cmds.c b/vis-cmds.c index 1515bc849..ae9ba4906 100644 --- a/vis-cmds.c +++ b/vis-cmds.c @@ -246,8 +246,7 @@ static bool cmd_set(Vis *vis, Win *win, Command *cmd, const char *argv[], Select break; case OPTION_ESCDELAY: { - TermKey *termkey = vis->ui->termkey; - termkey_set_waittime(termkey, arg.i); + termkey_set_waittime(vis->ui.termkey, arg.i); break; } case OPTION_EXPANDTAB: @@ -354,7 +353,7 @@ static bool cmd_set(Vis *vis, Win *win, Command *cmd, const char *argv[], Select vis_info_show(vis, "Invalid layout `%s', expected 'h' or 'v'", arg.s); return false; } - ui_arrange(vis->ui, layout); + ui_arrange(&vis->ui, layout); break; } case OPTION_IGNORECASE: @@ -542,7 +541,7 @@ static bool cmd_split(Vis *vis, Win *win, Command *cmd, const char *argv[], Sele if (!win) return false; enum UiOption options = UI_OPTIONS_GET(win->view->ui); - ui_arrange(vis->ui, UI_LAYOUT_HORIZONTAL); + ui_arrange(&vis->ui, UI_LAYOUT_HORIZONTAL); if (!argv[1]) return vis_window_split(win); bool ret = openfiles(vis, &argv[1]); @@ -555,7 +554,7 @@ static bool cmd_vsplit(Vis *vis, Win *win, Command *cmd, const char *argv[], Sel if (!win) return false; enum UiOption options = UI_OPTIONS_GET(win->view->ui); - ui_arrange(vis->ui, UI_LAYOUT_VERTICAL); + ui_arrange(&vis->ui, UI_LAYOUT_VERTICAL); if (!argv[1]) return vis_window_split(win); bool ret = openfiles(vis, &argv[1]); @@ -565,12 +564,12 @@ static bool cmd_vsplit(Vis *vis, Win *win, Command *cmd, const char *argv[], Sel } static bool cmd_new(Vis *vis, Win *win, Command *cmd, const char *argv[], Selection *sel, Filerange *range) { - ui_arrange(vis->ui, UI_LAYOUT_HORIZONTAL); + ui_arrange(&vis->ui, UI_LAYOUT_HORIZONTAL); return vis_window_new(vis, NULL); } static bool cmd_vnew(Vis *vis, Win *win, Command *cmd, const char *argv[], Selection *sel, Filerange *range) { - ui_arrange(vis->ui, UI_LAYOUT_VERTICAL); + ui_arrange(&vis->ui, UI_LAYOUT_VERTICAL); return vis_window_new(vis, NULL); } @@ -775,7 +774,7 @@ static void print_symbolic_keys(Vis *vis, Text *txt) { TERMKEY_SYM_KPEQUALS, }; - TermKey *termkey = vis->ui->termkey; + TermKey *termkey = vis->ui.termkey; text_appendf(txt, " ␣ (a literal \" \" space symbol must be used to refer to )\n"); for (size_t i = 0; i < LENGTH(keys); i++) { text_appendf(txt, " <%s>\n", termkey_get_keyname(termkey, keys[i])); diff --git a/vis-core.h b/vis-core.h index 9746fc9d9..7d729f78f 100644 --- a/vis-core.h +++ b/vis-core.h @@ -168,7 +168,6 @@ struct Win { }; struct Vis { - Ui *ui; /* user interface responsible for visual appearance */ File *files; /* all files currently managed by this editor instance */ File *command_file; /* special internal file used to store :-command prompt */ File *search_file; /* special internal file used to store /,? search prompt */ @@ -176,6 +175,7 @@ struct Vis { Win *windows; /* all windows currently managed by this editor instance */ Win *win; /* currently active/focused window */ Win *message_window; /* special window to display multi line messages */ + Ui ui; /* user interface responsible for visual appearance */ Register registers[VIS_REG_INVALID]; /* registers used for text manipulations yank/put etc. and macros */ Macro *recording, *last_recording; /* currently (if non NULL) and least recently recorded macro */ const Macro *replaying; /* macro currently being replayed */ diff --git a/vis-lua.c b/vis-lua.c index 3b5f7840d..6bd8a080e 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -1399,7 +1399,7 @@ static int vis_index(lua_State *L) { } if (strcmp(key, "registers") == 0) { - obj_ref_new(L, vis->ui, VIS_LUA_TYPE_REGISTERS); + obj_ref_new(L, &vis->ui, VIS_LUA_TYPE_REGISTERS); return 1; } @@ -1415,7 +1415,7 @@ static int vis_index(lua_State *L) { } if (strcmp(key, "ui") == 0) { - obj_ref_new(L, vis->ui, VIS_LUA_TYPE_UI); + obj_ref_new(L, &vis->ui, VIS_LUA_TYPE_UI); return 1; } } @@ -1429,8 +1429,7 @@ static int vis_options_assign(Vis *vis, lua_State *L, const char *key, int next) } else if (strcmp(key, "changecolors") == 0) { vis->change_colors = lua_toboolean(L, next); } else if (strcmp(key, "escdelay") == 0) { - TermKey *tk = vis->ui->termkey; - termkey_set_waittime(tk, luaL_checkint(L, next)); + termkey_set_waittime(vis->ui.termkey, luaL_checkint(L, next)); } else if (strcmp(key, "ignorecase") == 0 || strcmp(key, "ic") == 0) { vis->ignorecase = lua_toboolean(L, next); } else if (strcmp(key, "loadmethod") == 0) { @@ -1570,8 +1569,7 @@ static int vis_options_index(lua_State *L) { lua_pushboolean(L, vis->change_colors); return 1; } else if (strcmp(key, "escdelay") == 0) { - TermKey *tk = vis->ui->termkey; - lua_pushunsigned(L, termkey_get_waittime(tk)); + lua_pushunsigned(L, termkey_get_waittime(vis->ui.termkey)); return 1; } else if (strcmp(key, "ignorecase") == 0 || strcmp(key, "ic") == 0) { lua_pushboolean(L, vis->ignorecase); @@ -3612,7 +3610,7 @@ static void vis_lua_ui_draw(Vis *vis) { bool vis_event_emit(Vis *vis, enum VisEvents id, ...) { if (!vis->initialized) { vis->initialized = true; - ui_init(vis->ui, vis); + ui_init(&vis->ui, vis); vis_lua_init(vis); } diff --git a/vis-prompt.c b/vis-prompt.c index 474a32c98..dd6716136 100644 --- a/vis-prompt.c +++ b/vis-prompt.c @@ -182,7 +182,7 @@ void vis_prompt_show(Vis *vis, const char *title) { void vis_info_show(Vis *vis, const char *msg, ...) { va_list ap; va_start(ap, msg); - ui_info_show(vis->ui, msg, ap); + ui_info_show(&vis->ui, msg, ap); va_end(ap); } diff --git a/vis.c b/vis.c index be4a66a64..9bac2a8ca 100644 --- a/vis.c +++ b/vis.c @@ -202,8 +202,7 @@ static void window_free(Win *win) { if (other->parent == win) other->parent = NULL; } - if (vis->ui) - ui_window_free(win->ui); + ui_window_free(win->ui); view_free(win->view); for (size_t i = 0; i < LENGTH(win->modes); i++) map_free(win->modes[i].bindings); @@ -382,7 +381,7 @@ Win *window_new_file(Vis *vis, File *file, enum UiOption options) { win->file = file; win->view = view_new(file->text); win->expandtab = false; - win->ui = ui_window_new(vis->ui, win, options); + win->ui = ui_window_new(&vis->ui, win, options); if (!win->view || !win->ui) { window_free(win); return NULL; @@ -434,7 +433,7 @@ bool vis_window_change_file(Win *win, const char* filename) { } bool vis_window_split(Win *original) { - original->vis->ui->doupdate = false; + original->vis->ui.doupdate = false; Win *win = window_new_file(original->vis, original->file, UI_OPTION_STATUSBAR); if (!win) return false; @@ -447,7 +446,7 @@ bool vis_window_split(Win *original) { win->file = original->file; view_options_set(win->view, UI_OPTIONS_GET(original->view->ui)); view_cursors_to(win->view->selection, view_cursor_get(original->view)); - win->vis->ui->doupdate = true; + win->vis->ui.doupdate = true; return true; } @@ -482,21 +481,21 @@ void vis_draw(Vis *vis) { } void vis_redraw(Vis *vis) { - ui_redraw(vis->ui); - ui_draw(vis->ui); + ui_redraw(&vis->ui); + ui_draw(&vis->ui); } bool vis_window_new(Vis *vis, const char *filename) { File *file = file_new(vis, filename, false); if (!file) return false; - vis->ui->doupdate = false; + vis->ui.doupdate = false; Win *win = window_new_file(vis, file, UI_OPTION_STATUSBAR|UI_OPTION_SYMBOL_EOF); if (!win) { file_free(vis, file); return false; } - vis->ui->doupdate = true; + vis->ui.doupdate = true; return true; } @@ -567,14 +566,15 @@ void vis_window_close(Win *win) { vis_draw(vis); } -Vis *vis_new(Ui *ui) { - if (!ui) - return NULL; +Vis *vis_new(void) { Vis *vis = calloc(1, sizeof(Vis)); if (!vis) return NULL; vis->exit_status = -1; - vis->ui = ui; + if (!ui_terminal_init(&vis->ui)) { + free(vis); + return NULL; + } vis->change_colors = true; for (size_t i = 0; i < LENGTH(vis->registers); i++) register_init(&vis->registers[i]); @@ -629,7 +629,7 @@ void vis_free(Vis *vis) { file_free(vis, vis->error_file); for (int i = 0; i < LENGTH(vis->registers); i++) register_release(&vis->registers[i]); - ui_terminal_free(vis->ui); + ui_terminal_free(&vis->ui); if (vis->usercmds) { const char *name; while (map_first(vis->usercmds, &name) && vis_cmd_unregister(vis, name)); @@ -960,7 +960,7 @@ void vis_cancel(Vis *vis) { void vis_die(Vis *vis, const char *msg, ...) { va_list ap; va_start(ap, msg); - ui_die(vis->ui, msg, ap); + ui_die(&vis->ui, msg, ap); va_end(ap); } @@ -968,7 +968,7 @@ const char *vis_keys_next(Vis *vis, const char *keys) { if (!keys || !*keys) return NULL; TermKeyKey key; - TermKey *termkey = vis->ui->termkey; + TermKey *termkey = vis->ui.termkey; const char *next = NULL; /* first try to parse a special key of the form */ if (*keys == '<' && keys[1] && (next = termkey_strpkey(termkey, keys+1, &key, TERMKEY_FORMAT_VIM)) && *next == '>') @@ -996,7 +996,7 @@ long vis_keys_codepoint(Vis *vis, const char *keys) { long codepoint = -1; const char *next; TermKeyKey key; - TermKey *termkey = vis->ui->termkey; + TermKey *termkey = vis->ui.termkey; if (!keys[0]) return -1; @@ -1189,9 +1189,9 @@ static void vis_keys_push(Vis *vis, const char *input, size_t pos, bool record) static const char *getkey(Vis *vis) { TermKeyKey key = { 0 }; - if (!ui_getkey(vis->ui, &key)) + if (!ui_getkey(&vis->ui, &key)) return NULL; - ui_info_hide(vis->ui); + ui_info_hide(&vis->ui); bool use_keymap = vis->mode->id != VIS_MODE_INSERT && vis->mode->id != VIS_MODE_REPLACE && !vis->keymap_disabled; @@ -1205,7 +1205,7 @@ static const char *getkey(Vis *vis) { } } - TermKey *termkey = vis->ui->termkey; + TermKey *termkey = vis->ui.termkey; if (key.type == TERMKEY_TYPE_UNKNOWN_CSI) { long args[18]; size_t nargs; @@ -1299,16 +1299,16 @@ int vis_run(Vis *vis) { } if (vis->resume) { - ui_terminal_resume(vis->ui); + ui_terminal_resume(&vis->ui); vis->resume = false; } if (vis->need_resize) { - ui_resize(vis->ui); + ui_resize(&vis->ui); vis->need_resize = false; } - ui_draw(vis->ui); + ui_draw(&vis->ui); idle.tv_sec = vis->mode->idle_timeout; int r = pselect(vis_process_before_tick(&fds) + 1, &fds, NULL, NULL, timeout, &emptyset); @@ -1328,8 +1328,7 @@ int vis_run(Vis *vis) { continue; } - TermKey *termkey = vis->ui->termkey; - termkey_advisereadable(termkey); + termkey_advisereadable(vis->ui.termkey); const char *key; while ((key = getkey(vis))) @@ -1624,7 +1623,7 @@ int vis_pipe(Vis *vis, File *file, Filerange *range, const char *argv[], return -1; } - ui_terminal_save(vis->ui, fullscreen); + ui_terminal_save(&vis->ui, fullscreen); pid_t pid = fork(); if (pid == -1) { @@ -1818,7 +1817,7 @@ int vis_pipe(Vis *vis, File *file, Filerange *range, const char *argv[], sigaction(SIGTERM, &sigterm_old, NULL); vis->interrupted = false; - ui_terminal_restore(vis->ui); + ui_terminal_restore(&vis->ui); if (WIFEXITED(status)) return WEXITSTATUS(status); diff --git a/vis.h b/vis.h index 032b00bd0..eb8ec92d1 100644 --- a/vis.h +++ b/vis.h @@ -90,8 +90,8 @@ typedef struct { * @defgroup vis_lifecycle * @{ */ -/** Create a new editor instance using the given user interface. */ -Vis *vis_new(Ui*); +/** Create a new editor instance. */ +Vis *vis_new(void); /** Free all resources associated with this editor instance, terminates UI. */ void vis_free(Vis*); /**