Skip to content

Commit

Permalink
remove some ui pointer chasing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rnpnr committed May 21, 2024
1 parent ce94fd6 commit 3bbc0cc
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 73 deletions.
2 changes: 1 addition & 1 deletion event-basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions ui-terminal-curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
25 changes: 11 additions & 14 deletions ui-terminal-vt100.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 5 additions & 7 deletions ui-terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -641,5 +640,4 @@ void ui_terminal_free(Ui *tui) {
termkey_destroy(tui->termkey);
free(tui->cells);
free(tui->styles);
free(tui);
}
3 changes: 2 additions & 1 deletion ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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*);
Expand Down
15 changes: 7 additions & 8 deletions vis-cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]);
Expand All @@ -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]);
Expand All @@ -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);
}

Expand Down Expand Up @@ -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 <Space>)\n");
for (size_t i = 0; i < LENGTH(keys); i++) {
text_appendf(txt, " <%s>\n", termkey_get_keyname(termkey, keys[i]));
Expand Down
2 changes: 1 addition & 1 deletion vis-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ 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 */
File *error_file; /* special internal file used to store lua error messages */
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 */
Expand Down
12 changes: 5 additions & 7 deletions vis-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion vis-prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Loading

0 comments on commit 3bbc0cc

Please sign in to comment.