From c9455d5f211616ce3750e58a472e1b5a4251b8be Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 27 Oct 2019 17:07:44 +0100 Subject: kbd: move into wayland --- terminal.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 95a60682..ada701cc 100644 --- a/terminal.c +++ b/terminal.c @@ -480,7 +480,7 @@ void term_mouse_down(struct terminal *term, int button, int row, int col, bool shift, bool alt, bool ctrl) { - if (term->kbd.shift) { + if (term->wl.kbd.shift) { /* "raw" mouse mode */ return; } @@ -513,7 +513,7 @@ void term_mouse_up(struct terminal *term, int button, int row, int col, bool shift, bool alt, bool ctrl) { - if (term->kbd.shift) { + if (term->wl.kbd.shift) { /* "raw" mouse mode */ return; } @@ -551,7 +551,7 @@ void term_mouse_motion(struct terminal *term, int button, int row, int col, bool shift, bool alt, bool ctrl) { - if (term->kbd.shift) { + if (term->wl.kbd.shift) { /* "raw" mouse mode */ return; } -- cgit v1.3 From 1adab32906b5455781921b779019a6d3f0e4fe56 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 27 Oct 2019 18:51:14 +0100 Subject: term: wayland struct is now not a part of the terminal struct We do however need access to it, so provide a pointer. The difference is that now we can have a *single* wayland instance, but multiple terminal instances. --- main.c | 110 +++++++++++++++++++++++++++++++----------------------------- osc.c | 6 ++-- render.c | 44 ++++++++++++------------ search.c | 18 +++++----- selection.c | 42 +++++++++++------------ terminal.c | 6 ++-- terminal.h | 2 +- wayland.h | 1 - 8 files changed, 115 insertions(+), 114 deletions(-) (limited to 'terminal.c') diff --git a/main.c b/main.c index f2fff1e3..5dd37dfb 100644 --- a/main.c +++ b/main.c @@ -729,6 +729,14 @@ main(int argc, char *const *argv) struct fdm *fdm = NULL; + struct wayland wayl = { + .kbd = { + .repeat = { + .fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK), + }, + }, + }; + struct terminal term = { .quit = false, .ptmx = posix_openpt(O_RDWR | O_NOCTTY), @@ -791,13 +799,7 @@ main(int argc, char *const *argv) .normal = {.damage = tll_init(), .scroll_damage = tll_init()}, .alt = {.damage = tll_init(), .scroll_damage = tll_init()}, .grid = &term.normal, - .wl = { - .kbd = { - .repeat = { - .fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK), - }, - }, - }, + .wl = &wayl, .render = { .scrollback_lines = conf.scrollback_lines, .workers = { @@ -843,7 +845,7 @@ main(int argc, char *const *argv) goto out; } - if (term.flash.fd == -1 || term.blink.fd == -1 || term.wl.kbd.repeat.fd == -1) { + if (term.flash.fd == -1 || term.blink.fd == -1 || term.wl->kbd.repeat.fd == -1) { LOG_ERR("failed to create timers"); goto out; } @@ -895,67 +897,67 @@ main(int argc, char *const *argv) term.cell_height = (int)ceil(term.fextents.height); LOG_INFO("cell width=%d, height=%d", term.cell_width, term.cell_height); - term.wl.term = &term; - term.wl.display = wl_display_connect(NULL); - if (term.wl.display == NULL) { + term.wl->term = &term; + term.wl->display = wl_display_connect(NULL); + if (term.wl->display == NULL) { LOG_ERR("failed to connect to wayland; no compositor running?"); goto out; } - term.wl.registry = wl_display_get_registry(term.wl.display); - if (term.wl.registry == NULL) { + term.wl->registry = wl_display_get_registry(term.wl->display); + if (term.wl->registry == NULL) { LOG_ERR("failed to get wayland registry"); goto out; } - wl_registry_add_listener(term.wl.registry, ®istry_listener, &term.wl); - wl_display_roundtrip(term.wl.display); + wl_registry_add_listener(term.wl->registry, ®istry_listener, term.wl); + wl_display_roundtrip(term.wl->display); - if (term.wl.compositor == NULL) { + if (term.wl->compositor == NULL) { LOG_ERR("no compositor"); goto out; } - if (term.wl.shm == NULL) { + if (term.wl->shm == NULL) { LOG_ERR("no shared memory buffers interface"); goto out; } - if (term.wl.shell == NULL) { + if (term.wl->shell == NULL) { LOG_ERR("no XDG shell interface"); goto out; } - if (!term.wl.have_argb8888) { + if (!term.wl->have_argb8888) { LOG_ERR("compositor does not support ARGB surfaces"); goto out; } - if (term.wl.seat == NULL) { + if (term.wl->seat == NULL) { LOG_ERR("no seat available"); goto out; } - if (term.wl.data_device_manager == NULL) { + if (term.wl->data_device_manager == NULL) { LOG_ERR("no clipboard available " "(wl_data_device_manager not implemented by server)"); goto out; } - if (term.wl.primary_selection_device_manager == NULL) + if (term.wl->primary_selection_device_manager == NULL) LOG_WARN("no primary selection available"); - tll_foreach(term.wl.monitors, it) { + tll_foreach(term.wl->monitors, it) { LOG_INFO("%s: %dx%d+%dx%d (scale=%d, refresh=%.2fHz)", it->item.name, it->item.width_px, it->item.height_px, it->item.x, it->item.y, it->item.scale, it->item.refresh); } /* Clipboard */ - term.wl.data_device = wl_data_device_manager_get_data_device( - term.wl.data_device_manager, term.wl.seat); - wl_data_device_add_listener(term.wl.data_device, &data_device_listener, &term.wl); + term.wl->data_device = wl_data_device_manager_get_data_device( + term.wl->data_device_manager, term.wl->seat); + wl_data_device_add_listener(term.wl->data_device, &data_device_listener, term.wl); /* Primary selection */ - if (term.wl.primary_selection_device_manager != NULL) { - term.wl.primary_selection_device = zwp_primary_selection_device_manager_v1_get_device( - term.wl.primary_selection_device_manager, term.wl.seat); + if (term.wl->primary_selection_device_manager != NULL) { + term.wl->primary_selection_device = zwp_primary_selection_device_manager_v1_get_device( + term.wl->primary_selection_device_manager, term.wl->seat); zwp_primary_selection_device_v1_add_listener( - term.wl.primary_selection_device, &primary_selection_device_listener, &term.wl); + term.wl->primary_selection_device, &primary_selection_device_listener, term.wl); } /* Cursor */ @@ -973,49 +975,49 @@ main(int argc, char *const *argv) /* Note: theme is (re)loaded on scale and output changes */ LOG_INFO("cursor theme: %s, size: %u", cursor_theme, cursor_size); - term.wl.pointer.size = cursor_size; - term.wl.pointer.theme_name = cursor_theme != NULL ? strdup(cursor_theme) : NULL; + term.wl->pointer.size = cursor_size; + term.wl->pointer.theme_name = cursor_theme != NULL ? strdup(cursor_theme) : NULL; - term.wl.pointer.surface = wl_compositor_create_surface(term.wl.compositor); - if (term.wl.pointer.surface == NULL) { + term.wl->pointer.surface = wl_compositor_create_surface(term.wl->compositor); + if (term.wl->pointer.surface == NULL) { LOG_ERR("failed to create cursor surface"); goto out; } /* Main window */ - term.window.surface = wl_compositor_create_surface(term.wl.compositor); + term.window.surface = wl_compositor_create_surface(term.wl->compositor); if (term.window.surface == NULL) { LOG_ERR("failed to create wayland surface"); goto out; } - wl_surface_add_listener(term.window.surface, &surface_listener, &term.wl); + wl_surface_add_listener(term.window.surface, &surface_listener, term.wl); - term.window.xdg_surface = xdg_wm_base_get_xdg_surface(term.wl.shell, term.window.surface); - xdg_surface_add_listener(term.window.xdg_surface, &xdg_surface_listener, &term.wl); + term.window.xdg_surface = xdg_wm_base_get_xdg_surface(term.wl->shell, term.window.surface); + xdg_surface_add_listener(term.window.xdg_surface, &xdg_surface_listener, term.wl); term.window.xdg_toplevel = xdg_surface_get_toplevel(term.window.xdg_surface); - xdg_toplevel_add_listener(term.window.xdg_toplevel, &xdg_toplevel_listener, &term.wl); + xdg_toplevel_add_listener(term.window.xdg_toplevel, &xdg_toplevel_listener, term.wl); xdg_toplevel_set_app_id(term.window.xdg_toplevel, "foot"); term_set_window_title(&term, "foot"); /* Request server-side decorations */ term.window.xdg_toplevel_decoration = zxdg_decoration_manager_v1_get_toplevel_decoration( - term.wl.xdg_decoration_manager, term.window.xdg_toplevel); + term.wl->xdg_decoration_manager, term.window.xdg_toplevel); zxdg_toplevel_decoration_v1_set_mode( term.window.xdg_toplevel_decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); zxdg_toplevel_decoration_v1_add_listener( - term.window.xdg_toplevel_decoration, &xdg_toplevel_decoration_listener, &term.wl); + term.window.xdg_toplevel_decoration, &xdg_toplevel_decoration_listener, term.wl); /* Scrollback search box */ - term.window.search_surface = wl_compositor_create_surface(term.wl.compositor); + term.window.search_surface = wl_compositor_create_surface(term.wl->compositor); term.window.search_sub_surface = wl_subcompositor_get_subsurface( - term.wl.sub_compositor, term.window.search_surface, term.window.surface); + term.wl->sub_compositor, term.window.search_surface, term.window.surface); wl_subsurface_set_desync(term.window.search_sub_surface); wl_surface_commit(term.window.surface); - wl_display_roundtrip(term.wl.display); + wl_display_roundtrip(term.wl->display); if (conf.width == -1) { assert(conf.height == -1); @@ -1026,7 +1028,7 @@ main(int argc, char *const *argv) conf.height = max(conf.height, term.cell_height); render_resize(&term, conf.width, conf.height); - wl_display_dispatch_pending(term.wl.display); + wl_display_dispatch_pending(term.wl->display); { int fork_pipe[2]; @@ -1102,7 +1104,7 @@ main(int argc, char *const *argv) { - int fd = wl_display_get_fd(term.wl.display); + int fd = wl_display_get_fd(term.wl->display); int fd_flags = fcntl(fd, F_GETFL); if (fd_flags == -1) { LOG_ERRNO("failed to set non blocking mode on Wayland display connection"); @@ -1117,16 +1119,16 @@ main(int argc, char *const *argv) if ((fdm = fdm_init()) == NULL) goto out; - fdm_add(fdm, wl_display_get_fd(term.wl.display), EPOLLIN, &fdm_wayl, &term.wl); + fdm_add(fdm, wl_display_get_fd(term.wl->display), EPOLLIN, &fdm_wayl, term.wl); fdm_add(fdm, term.ptmx, EPOLLIN, &fdm_ptmx, &term); - fdm_add(fdm, term.wl.kbd.repeat.fd, EPOLLIN, &fdm_repeat, &term.wl); + fdm_add(fdm, term.wl->kbd.repeat.fd, EPOLLIN, &fdm_repeat, term.wl); fdm_add(fdm, term.flash.fd, EPOLLIN, &fdm_flash, &term); fdm_add(fdm, term.blink.fd, EPOLLIN, &fdm_blink, &term); fdm_add(fdm, term.delayed_render_timer.lower_fd, EPOLLIN, &fdm_delayed_render, &term); fdm_add(fdm, term.delayed_render_timer.upper_fd, EPOLLIN, &fdm_delayed_render, &term); while (true) { - wl_display_flush(term.wl.display); + wl_display_flush(term.wl->display); if (!fdm_poll(fdm)) break; } @@ -1136,9 +1138,9 @@ main(int argc, char *const *argv) out: if (fdm != NULL) { - fdm_del(fdm, wl_display_get_fd(term.wl.display)); + fdm_del(fdm, wl_display_get_fd(term.wl->display)); fdm_del(fdm, term.ptmx); - fdm_del(fdm, term.wl.kbd.repeat.fd); + fdm_del(fdm, term.wl->kbd.repeat.fd); fdm_del(fdm, term.flash.fd); fdm_del(fdm, term.blink.fd); fdm_del(fdm, term.delayed_render_timer.lower_fd); @@ -1162,7 +1164,7 @@ out: shm_fini(); wayl_win_destroy(&term.window); - wayl_destroy(&term.wl); + wayl_destroy(&wayl); free(term.vt.osc.data); for (int row = 0; row < term.normal.num_rows; row++) @@ -1184,8 +1186,8 @@ out: close(term.flash.fd); if (term.blink.fd != -1) close(term.blink.fd); - if (term.wl.kbd.repeat.fd != -1) - close(term.wl.kbd.repeat.fd); + if (term.wl->kbd.repeat.fd != -1) + close(term.wl->kbd.repeat.fd); if (term.ptmx != -1) close(term.ptmx); diff --git a/osc.c b/osc.c index b506f0bb..e860edcd 100644 --- a/osc.c +++ b/osc.c @@ -30,14 +30,14 @@ osc_to_clipboard(struct terminal *term, const char *target, switch (*t) { case 'c': { char *copy = strdup(decoded); - if (!text_to_clipboard(term, copy, term->wl.input_serial)) + if (!text_to_clipboard(term, copy, term->wl->input_serial)) free(copy); break; } case 'p': { char *copy = strdup(decoded); - if (!text_to_primary(term, copy, term->wl.input_serial)) + if (!text_to_primary(term, copy, term->wl->input_serial)) free(copy); break; } @@ -130,7 +130,7 @@ osc_from_clipboard(struct terminal *term, const char *source) switch (src) { case 'c': - text_from_clipboard(term, term->wl.input_serial, &from_clipboard_cb, &ctx); + text_from_clipboard(term, term->wl->input_serial, &from_clipboard_cb, &ctx); break; case 'p': diff --git a/render.c b/render.c index c2840eb8..75f319bf 100644 --- a/render.c +++ b/render.c @@ -440,7 +440,7 @@ grid_render(struct terminal *term) assert(term->width > 0); assert(term->height > 0); - struct buffer *buf = shm_get_buffer(term->wl.shm, term->width, term->height, 1 + term->render.workers.count); + struct buffer *buf = shm_get_buffer(term->wl->shm, term->width, term->height, 1 + term->render.workers.count); wl_surface_attach(term->window.surface, buf->wl_buf, 0, 0); pixman_image_t *pix = buf->pix; @@ -741,7 +741,7 @@ render_search_box(struct terminal *term) const int width = 2 * margin + max(20, term->search.len) * term->cell_width; const int height = 2 * margin + 1 * term->cell_height; - struct buffer *buf = shm_get_buffer(term->wl.shm, width, height, 1); + struct buffer *buf = shm_get_buffer(term->wl->shm, width, height, 1); /* Background - yellow on empty/match, red on mismatch */ pixman_color_t color = color_hex_to_pixman( @@ -945,28 +945,28 @@ render_set_title(struct terminal *term, const char *title) bool render_reload_cursor_theme(struct terminal *term) { - if (term->wl.pointer.size == 0) + if (term->wl->pointer.size == 0) return true; - if (term->wl.pointer.theme != NULL) { - wl_cursor_theme_destroy(term->wl.pointer.theme); - term->wl.pointer.theme = NULL; - term->wl.pointer.cursor = NULL; + if (term->wl->pointer.theme != NULL) { + wl_cursor_theme_destroy(term->wl->pointer.theme); + term->wl->pointer.theme = NULL; + term->wl->pointer.cursor = NULL; } LOG_DBG("reloading cursor theme: %s@%d", - term->wl.pointer.theme_name, term->wl.pointer.size); + term->wl->pointer.theme_name, term->wl->pointer.size); - term->wl.pointer.theme = wl_cursor_theme_load( - term->wl.pointer.theme_name, term->wl.pointer.size * term->scale, term->wl.shm); - if (term->wl.pointer.theme == NULL) { + term->wl->pointer.theme = wl_cursor_theme_load( + term->wl->pointer.theme_name, term->wl->pointer.size * term->scale, term->wl->shm); + if (term->wl->pointer.theme == NULL) { LOG_ERR("failed to load cursor theme"); return false; } - term->wl.pointer.cursor = wl_cursor_theme_get_cursor( - term->wl.pointer.theme, "left_ptr"); - assert(term->wl.pointer.cursor != NULL); + term->wl->pointer.cursor = wl_cursor_theme_get_cursor( + term->wl->pointer.theme, "left_ptr"); + assert(term->wl->pointer.cursor != NULL); render_update_cursor_surface(term); return true; @@ -975,26 +975,26 @@ render_reload_cursor_theme(struct terminal *term) void render_update_cursor_surface(struct terminal *term) { - if (term->wl.pointer.cursor == NULL) + if (term->wl->pointer.cursor == NULL) return; const int scale = term->scale; - wl_surface_set_buffer_scale(term->wl.pointer.surface, scale); + wl_surface_set_buffer_scale(term->wl->pointer.surface, scale); - struct wl_cursor_image *image = term->wl.pointer.cursor->images[0]; + struct wl_cursor_image *image = term->wl->pointer.cursor->images[0]; wl_surface_attach( - term->wl.pointer.surface, wl_cursor_image_get_buffer(image), 0, 0); + term->wl->pointer.surface, wl_cursor_image_get_buffer(image), 0, 0); wl_pointer_set_cursor( - term->wl.pointer.pointer, term->wl.pointer.serial, - term->wl.pointer.surface, + term->wl->pointer.pointer, term->wl->pointer.serial, + term->wl->pointer.surface, image->hotspot_x / scale, image->hotspot_y / scale); wl_surface_damage_buffer( - term->wl.pointer.surface, 0, 0, INT32_MAX, INT32_MAX); + term->wl->pointer.surface, 0, 0, INT32_MAX, INT32_MAX); - wl_surface_commit(term->wl.pointer.surface); + wl_surface_commit(term->wl->pointer.surface); } void diff --git a/search.c b/search.c index 5c0ce1b7..e055b478 100644 --- a/search.c +++ b/search.c @@ -294,13 +294,13 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask { LOG_DBG("search: input: sym=%d/0x%x, mods=0x%08x", sym, sym, mods); - const xkb_mod_mask_t ctrl = 1 << term->wl.kbd.mod_ctrl; - const xkb_mod_mask_t alt = 1 << term->wl.kbd.mod_alt; - //const xkb_mod_mask_t shift = 1 << term->wl.kbd.mod_shift; - //const xkb_mod_mask_t meta = 1 << term->wl.kbd.mod_meta; + const xkb_mod_mask_t ctrl = 1 << term->wl->kbd.mod_ctrl; + const xkb_mod_mask_t alt = 1 << term->wl->kbd.mod_alt; + //const xkb_mod_mask_t shift = 1 << term->wl->kbd.mod_shift; + //const xkb_mod_mask_t meta = 1 << term->wl->kbd.mod_meta; enum xkb_compose_status compose_status = xkb_compose_state_get_status( - term->wl.kbd.xkb_compose_state); + term->wl->kbd.xkb_compose_state); /* Cancel search */ if ((mods == 0 && sym == XKB_KEY_Escape) || @@ -317,7 +317,7 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask /* "Commit" search - copy selection to primary and cancel search */ else if (mods == 0 && sym == XKB_KEY_Return) { - selection_finalize(term, term->wl.input_serial); + selection_finalize(term, term->wl->input_serial); search_cancel_keep_selection(term); return; } @@ -449,11 +449,11 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask if (compose_status == XKB_COMPOSE_COMPOSED) { count = xkb_compose_state_get_utf8( - term->wl.kbd.xkb_compose_state, (char *)buf, sizeof(buf)); - xkb_compose_state_reset(term->wl.kbd.xkb_compose_state); + term->wl->kbd.xkb_compose_state, (char *)buf, sizeof(buf)); + xkb_compose_state_reset(term->wl->kbd.xkb_compose_state); } else { count = xkb_state_key_get_utf8( - term->wl.kbd.xkb_state, key, (char *)buf, sizeof(buf)); + term->wl->kbd.xkb_state, key, (char *)buf, sizeof(buf)); } const char *src = (const char *)buf; diff --git a/selection.c b/selection.c index a3c88307..d2ed7354 100644 --- a/selection.c +++ b/selection.c @@ -22,7 +22,7 @@ selection_enabled(const struct terminal *term) { return term->mouse_tracking == MOUSE_NONE || - term->wl.kbd.shift || + term->wl->kbd.shift || term->is_searching; } @@ -434,12 +434,12 @@ static const struct zwp_primary_selection_source_v1_listener primary_selection_s bool text_to_clipboard(struct terminal *term, char *text, uint32_t serial) { - struct wl_clipboard *clipboard = &term->wl.clipboard; + struct wl_clipboard *clipboard = &term->wl->clipboard; - if (term->wl.clipboard.data_source != NULL) { + if (term->wl->clipboard.data_source != NULL) { /* Kill previous data source */ assert(clipboard->serial != 0); - wl_data_device_set_selection(term->wl.data_device, NULL, clipboard->serial); + wl_data_device_set_selection(term->wl->data_device, NULL, clipboard->serial); wl_data_source_destroy(clipboard->data_source); free(clipboard->text); @@ -448,7 +448,7 @@ text_to_clipboard(struct terminal *term, char *text, uint32_t serial) } clipboard->data_source - = wl_data_device_manager_create_data_source(term->wl.data_device_manager); + = wl_data_device_manager_create_data_source(term->wl->data_device_manager); if (clipboard->data_source == NULL) { LOG_ERR("failed to create clipboard data source"); @@ -460,7 +460,7 @@ text_to_clipboard(struct terminal *term, char *text, uint32_t serial) /* Configure source */ wl_data_source_offer(clipboard->data_source, "text/plain;charset=utf-8"); wl_data_source_add_listener(clipboard->data_source, &data_source_listener, term); - wl_data_device_set_selection(term->wl.data_device, clipboard->data_source, serial); + wl_data_device_set_selection(term->wl->data_device, clipboard->data_source, serial); wl_data_source_set_user_data(clipboard->data_source, clipboard); /* Needed when sending the selection to other client */ @@ -482,7 +482,7 @@ text_from_clipboard(struct terminal *term, uint32_t serial, void (*cb)(const char *data, size_t size, void *user), void *user) { - struct wl_clipboard *clipboard = &term->wl.clipboard; + struct wl_clipboard *clipboard = &term->wl->clipboard; if (clipboard->data_offer == NULL) return; @@ -499,7 +499,7 @@ text_from_clipboard(struct terminal *term, uint32_t serial, /* Give write-end of pipe to other client */ wl_data_offer_receive( clipboard->data_offer, "text/plain;charset=utf-8", write_fd); - wl_display_roundtrip(term->wl.display); + wl_display_roundtrip(term->wl->display); /* Don't keep our copy of the write-end open (or we'll never get EOF) */ close(write_fd); @@ -539,7 +539,7 @@ from_clipboard_cb(const char *data, size_t size, void *user) void selection_from_clipboard(struct terminal *term, uint32_t serial) { - struct wl_clipboard *clipboard = &term->wl.clipboard; + struct wl_clipboard *clipboard = &term->wl->clipboard; if (clipboard->data_offer == NULL) return; @@ -555,18 +555,18 @@ selection_from_clipboard(struct terminal *term, uint32_t serial) bool text_to_primary(struct terminal *term, char *text, uint32_t serial) { - if (term->wl.primary_selection_device_manager == NULL) + if (term->wl->primary_selection_device_manager == NULL) return false; - struct wl_primary *primary = &term->wl.primary; + struct wl_primary *primary = &term->wl->primary; /* TODO: somehow share code with the clipboard equivalent */ - if (term->wl.primary.data_source != NULL) { + if (term->wl->primary.data_source != NULL) { /* Kill previous data source */ assert(primary->serial != 0); zwp_primary_selection_device_v1_set_selection( - term->wl.primary_selection_device, NULL, primary->serial); + term->wl->primary_selection_device, NULL, primary->serial); zwp_primary_selection_source_v1_destroy(primary->data_source); free(primary->text); @@ -576,7 +576,7 @@ text_to_primary(struct terminal *term, char *text, uint32_t serial) primary->data_source = zwp_primary_selection_device_manager_v1_create_source( - term->wl.primary_selection_device_manager); + term->wl->primary_selection_device_manager); if (primary->data_source == NULL) { LOG_ERR("failed to create clipboard data source"); @@ -589,7 +589,7 @@ text_to_primary(struct terminal *term, char *text, uint32_t serial) /* Configure source */ zwp_primary_selection_source_v1_offer(primary->data_source, "text/plain;charset=utf-8"); zwp_primary_selection_source_v1_add_listener(primary->data_source, &primary_selection_source_listener, term); - zwp_primary_selection_device_v1_set_selection(term->wl.primary_selection_device, primary->data_source, serial); + zwp_primary_selection_device_v1_set_selection(term->wl->primary_selection_device, primary->data_source, serial); zwp_primary_selection_source_v1_set_user_data(primary->data_source, primary); /* Needed when sending the selection to other client */ @@ -600,7 +600,7 @@ text_to_primary(struct terminal *term, char *text, uint32_t serial) void selection_to_primary(struct terminal *term, uint32_t serial) { - if (term->wl.primary_selection_device_manager == NULL) + if (term->wl->primary_selection_device_manager == NULL) return; /* Get selection as a string */ @@ -614,10 +614,10 @@ text_from_primary( struct terminal *term, void (*cb)(const char *data, size_t size, void *user), void *user) { - if (term->wl.primary_selection_device_manager == NULL) + if (term->wl->primary_selection_device_manager == NULL) return; - struct wl_primary *primary = &term->wl.primary; + struct wl_primary *primary = &term->wl->primary; if (primary->data_offer == NULL) return; @@ -634,7 +634,7 @@ text_from_primary( /* Give write-end of pipe to other client */ zwp_primary_selection_offer_v1_receive( primary->data_offer, "text/plain;charset=utf-8", write_fd); - wl_display_roundtrip(term->wl.display); + wl_display_roundtrip(term->wl->display); /* Don't keep our copy of the write-end open (or we'll never get EOF) */ close(write_fd); @@ -667,10 +667,10 @@ text_from_primary( void selection_from_primary(struct terminal *term) { - if (term->wl.primary_selection_device_manager == NULL) + if (term->wl->primary_selection_device_manager == NULL) return; - struct wl_clipboard *clipboard = &term->wl.clipboard; + struct wl_clipboard *clipboard = &term->wl->clipboard; if (clipboard->data_offer == NULL) return; diff --git a/terminal.c b/terminal.c index ada701cc..684f4c30 100644 --- a/terminal.c +++ b/terminal.c @@ -480,7 +480,7 @@ void term_mouse_down(struct terminal *term, int button, int row, int col, bool shift, bool alt, bool ctrl) { - if (term->wl.kbd.shift) { + if (term->wl->kbd.shift) { /* "raw" mouse mode */ return; } @@ -513,7 +513,7 @@ void term_mouse_up(struct terminal *term, int button, int row, int col, bool shift, bool alt, bool ctrl) { - if (term->wl.kbd.shift) { + if (term->wl->kbd.shift) { /* "raw" mouse mode */ return; } @@ -551,7 +551,7 @@ void term_mouse_motion(struct terminal *term, int button, int row, int col, bool shift, bool alt, bool ctrl) { - if (term->wl.kbd.shift) { + if (term->wl->kbd.shift) { /* "raw" mouse mode */ return; } diff --git a/terminal.h b/terminal.h index 7d7d1fcd..724bc6f1 100644 --- a/terminal.h +++ b/terminal.h @@ -246,7 +246,7 @@ struct terminal { int max_x_advance; } fextents; - struct wayland wl; + struct wayland *wl; struct wl_window window; struct { diff --git a/wayland.h b/wayland.h index f82d2630..e0f7cd2f 100644 --- a/wayland.h +++ b/wayland.h @@ -12,7 +12,6 @@ #include "tllist.h" struct monitor { - //struct terminal *term; struct wayland *wayl; struct wl_output *output; struct zxdg_output_v1 *xdg; -- cgit v1.3 From 0979a0e2e56f196684260e96e2554ebf0b828667 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 28 Oct 2019 18:25:19 +0100 Subject: terminal: implement term_init() and term_destroy() --- main.c | 345 +++--------------------------------------------------------- terminal.c | 348 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ terminal.h | 10 ++ 3 files changed, 375 insertions(+), 328 deletions(-) (limited to 'terminal.c') diff --git a/main.c b/main.c index b4e550fa..3d951b67 100644 --- a/main.c +++ b/main.c @@ -330,106 +330,10 @@ main(int argc, char *const *argv) setlocale(LC_ALL, ""); setenv("TERM", conf.term, 1); - struct terminal term = { - .quit = false, - .ptmx = posix_openpt(O_RDWR | O_NOCTTY), - .cursor_keys_mode = CURSOR_KEYS_NORMAL, - .keypad_keys_mode = KEYPAD_NUMERICAL, - .auto_margin = true, - .window_title_stack = tll_init(), - .scale = 1, - .flash = { - .fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK), - }, - .blink = { - .fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK), - }, - .vt = { - .state = 1, /* STATE_GROUND */ - }, - .colors = { - .default_fg = conf.colors.fg, - .default_bg = conf.colors.bg, - .default_table = { - conf.colors.regular[0], - conf.colors.regular[1], - conf.colors.regular[2], - conf.colors.regular[3], - conf.colors.regular[4], - conf.colors.regular[5], - conf.colors.regular[6], - conf.colors.regular[7], - - conf.colors.bright[0], - conf.colors.bright[1], - conf.colors.bright[2], - conf.colors.bright[3], - conf.colors.bright[4], - conf.colors.bright[5], - conf.colors.bright[6], - conf.colors.bright[7], - }, - .alpha = conf.colors.alpha, - }, - .default_cursor_style = conf.cursor.style, - .cursor_style = conf.cursor.style, - .default_cursor_color = { - .text = conf.cursor.color.text, - .cursor = conf.cursor.color.cursor, - }, - .cursor_color = { - .text = conf.cursor.color.text, - .cursor = conf.cursor.color.cursor, - }, - .selection = { - .start = {-1, -1}, - .end = {-1, -1}, - }, - .normal = {.damage = tll_init(), .scroll_damage = tll_init()}, - .alt = {.damage = tll_init(), .scroll_damage = tll_init()}, - .grid = &term.normal, - .render = { - .scrollback_lines = conf.scrollback_lines, - .workers = { - .count = conf.render_worker_count, - .queue = tll_init(), - }, - }, - .delayed_render_timer = { - .is_armed = false, - .lower_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC), - .upper_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC), - }, - }; - - LOG_INFO("using %zu rendering threads", term.render.workers.count); - - struct render_worker_context worker_context[term.render.workers.count]; - - /* Initialize 'current' colors from the default colors */ - term.colors.fg = term.colors.default_fg; - term.colors.bg = term.colors.default_bg; - - /* Initialize the 256 gray-scale color cube */ - { - /* First 16 entries have already been initialized from conf */ - for (size_t r = 0; r < 6; r++) { - for (size_t g = 0; g < 6; g++) { - for (size_t b = 0; b < 6; b++) { - term.colors.default_table[16 + r * 6 * 6 + g * 6 + b] - = r * 51 << 16 | g * 51 << 8 | b * 51; - } - } - } - - for (size_t i = 0; i < 24; i++) - term.colors.default_table[232 + i] = i * 11 << 16 | i * 11 << 8 | i * 11; - - memcpy(term.colors.table, term.colors.default_table, sizeof(term.colors.table)); - } struct fdm *fdm = NULL; struct wayland *wayl = NULL; + struct terminal *term = NULL; if ((fdm = fdm_init()) == NULL) goto out; @@ -437,256 +341,41 @@ main(int argc, char *const *argv) if ((wayl = wayl_init(fdm)) == NULL) goto out; - term.wl = wayl; - wayl->term = &term; - - if (term.ptmx == -1) { - LOG_ERR("failed to open pseudo terminal"); - goto out; - } - - if (term.flash.fd == -1 || term.blink.fd == -1) { - LOG_ERR("failed to create timers"); - goto out; - } - - sem_init(&term.render.workers.start, 0, 0); - sem_init(&term.render.workers.done, 0, 0); - mtx_init(&term.render.workers.lock, mtx_plain); - cnd_init(&term.render.workers.cond); - - term.render.workers.threads = calloc(term.render.workers.count, sizeof(term.render.workers.threads[0])); - for (size_t i = 0; i < term.render.workers.count; i++) { - worker_context[i].term = &term; - worker_context[i].my_id = 1 + i; - thrd_create(&term.render.workers.threads[i], &render_worker_thread, &worker_context[i]); - } - - font_list_t font_names = tll_init(); - tll_foreach(conf.fonts, it) - tll_push_back(font_names, it->item); - - if ((term.fonts[0] = font_from_name(font_names, "")) == NULL) { - tll_free(font_names); - goto out; - } - - term.fonts[1] = font_from_name(font_names, "style=bold"); - term.fonts[2] = font_from_name(font_names, "style=italic"); - term.fonts[3] = font_from_name(font_names, "style=bold italic"); - - tll_free(font_names); - - { - FT_Face ft_face = term.fonts[0]->face; - int max_x_advance = ft_face->size->metrics.max_advance / 64; - int height = ft_face->size->metrics.height / 64; - int descent = ft_face->size->metrics.descender / 64; - int ascent = ft_face->size->metrics.ascender / 64; - - term.fextents.height = height * term.fonts[0]->pixel_size_fixup; - term.fextents.descent = -descent * term.fonts[0]->pixel_size_fixup; - term.fextents.ascent = ascent * term.fonts[0]->pixel_size_fixup; - term.fextents.max_x_advance = max_x_advance * term.fonts[0]->pixel_size_fixup; - - LOG_DBG("metrics: height: %d, descent: %d, ascent: %d, x-advance: %d", - height, descent, ascent, max_x_advance); - } - - term.cell_width = (int)ceil(term.fextents.max_x_advance); - term.cell_height = (int)ceil(term.fextents.height); - LOG_INFO("cell width=%d, height=%d", term.cell_width, term.cell_height); - - /* Main window */ - term.window = wayl_win_init(wayl); - if (term.window == NULL) + if ((term = term_init(&conf, fdm, wayl, argc, argv)) == NULL) goto out; - term_set_window_title(&term, "foot"); - - if (conf.width == -1) { - assert(conf.height == -1); - conf.width = 80 * term.cell_width; - conf.height = 24 * term.cell_height; - } - - conf.width = max(conf.width, term.cell_width); - conf.height = max(conf.height, term.cell_height); - render_resize(&term, conf.width, conf.height); - - { - int fork_pipe[2]; - if (pipe2(fork_pipe, O_CLOEXEC) < 0) { - LOG_ERRNO("failed to create pipe"); - goto out; - } - - term.slave = fork(); - switch (term.slave) { - case -1: - LOG_ERRNO("failed to fork"); - close(fork_pipe[0]); - close(fork_pipe[1]); - goto out; - - case 0: - /* Child */ - close(fork_pipe[0]); /* Close read end */ - - char **_shell_argv = NULL; - char *const *shell_argv = argv; - - if (argc == 0) { - if (!tokenize_cmdline(conf.shell, &_shell_argv)) { - (void)!write(fork_pipe[1], &errno, sizeof(errno)); - _exit(0); - } - shell_argv = _shell_argv; - } - - slave_spawn(term.ptmx, shell_argv, fork_pipe[1]); - assert(false); - break; - - default: { - close(fork_pipe[1]); /* Close write end */ - LOG_DBG("slave has PID %d", term.slave); - - int _errno; - static_assert(sizeof(errno) == sizeof(_errno), "errno size mismatch"); - - ssize_t ret = read(fork_pipe[0], &_errno, sizeof(_errno)); - close(fork_pipe[0]); - - if (ret < 0) { - LOG_ERRNO("failed to read from pipe"); - goto out; - } else if (ret == sizeof(_errno)) { - LOG_ERRNO( - "%s: failed to execute", argc == 0 ? conf.shell : argv[0]); - goto out; - } else - LOG_DBG("%s: successfully started", conf.shell); - break; - } - } - } - - /* Read logic requires non-blocking mode */ - { - int fd_flags = fcntl(term.ptmx, F_GETFL); - if (fd_flags == -1) { - LOG_ERRNO("failed to set non blocking mode on PTY master"); - goto out; - } - - if (fcntl(term.ptmx, F_SETFL, fd_flags | O_NONBLOCK) == -1) { - LOG_ERRNO("failed to set non blocking mode on PTY master"); - goto out; - } - } - - fdm_add(fdm, term.ptmx, EPOLLIN, &fdm_ptmx, &term); - fdm_add(fdm, term.flash.fd, EPOLLIN, &fdm_flash, &term); - fdm_add(fdm, term.blink.fd, EPOLLIN, &fdm_blink, &term); - fdm_add(fdm, term.delayed_render_timer.lower_fd, EPOLLIN, &fdm_delayed_render, &term); - fdm_add(fdm, term.delayed_render_timer.upper_fd, EPOLLIN, &fdm_delayed_render, &term); + fdm_add(fdm, term->ptmx, EPOLLIN, &fdm_ptmx, term); + fdm_add(fdm, term->flash.fd, EPOLLIN, &fdm_flash, term); + fdm_add(fdm, term->blink.fd, EPOLLIN, &fdm_blink, term); + fdm_add(fdm, term->delayed_render_timer.lower_fd, EPOLLIN, &fdm_delayed_render, term); + fdm_add(fdm, term->delayed_render_timer.upper_fd, EPOLLIN, &fdm_delayed_render, term); while (true) { - wl_display_flush(term.wl->display); /* TODO: figure out how to get rid of this */ + wl_display_flush(term->wl->display); /* TODO: figure out how to get rid of this */ if (!fdm_poll(fdm)) break; } - if (term.quit) + if (term->quit) ret = EXIT_SUCCESS; out: if (fdm != NULL) { - fdm_del(fdm, term.ptmx); - fdm_del(fdm, term.flash.fd); - fdm_del(fdm, term.blink.fd); - fdm_del(fdm, term.delayed_render_timer.lower_fd); - fdm_del(fdm, term.delayed_render_timer.upper_fd); - } - - if (term.delayed_render_timer.lower_fd != -1) - close(term.delayed_render_timer.lower_fd); - if (term.delayed_render_timer.upper_fd != -1) - close(term.delayed_render_timer.upper_fd); - - mtx_lock(&term.render.workers.lock); - assert(tll_length(term.render.workers.queue) == 0); - for (size_t i = 0; i < term.render.workers.count; i++) { - sem_post(&term.render.workers.start); - tll_push_back(term.render.workers.queue, -2); + fdm_del(fdm, term->ptmx); + fdm_del(fdm, term->flash.fd); + fdm_del(fdm, term->blink.fd); + fdm_del(fdm, term->delayed_render_timer.lower_fd); + fdm_del(fdm, term->delayed_render_timer.upper_fd); } - cnd_broadcast(&term.render.workers.cond); - mtx_unlock(&term.render.workers.lock); shm_fini(); - wayl_win_destroy(term.window); + int child_ret = term_destroy(term); wayl_destroy(wayl); - - free(term.vt.osc.data); - for (int row = 0; row < term.normal.num_rows; row++) - grid_row_free(term.normal.rows[row]); - free(term.normal.rows); - for (int row = 0; row < term.alt.num_rows; row++) - grid_row_free(term.alt.rows[row]); - free(term.alt.rows); - - free(term.window_title); - tll_free_and_free(term.window_title_stack, free); - - for (size_t i = 0; i < sizeof(term.fonts) / sizeof(term.fonts[0]); i++) - font_destroy(term.fonts[i]); - - free(term.search.buf); - - if (term.flash.fd != -1) - close(term.flash.fd); - if (term.blink.fd != -1) - close(term.blink.fd); - - if (term.ptmx != -1) - close(term.ptmx); - - for (size_t i = 0; i < term.render.workers.count; i++) - thrd_join(term.render.workers.threads[i], NULL); - free(term.render.workers.threads); - cnd_destroy(&term.render.workers.cond); - mtx_destroy(&term.render.workers.lock); - sem_destroy(&term.render.workers.start); - sem_destroy(&term.render.workers.done); - assert(tll_length(term.render.workers.queue) == 0); - tll_free(term.render.workers.queue); - - if (term.slave > 0) { - /* Note: we've closed ptmx, so the slave *should* exit... */ - int status; - waitpid(term.slave, &status, 0); - - int child_ret = EXIT_FAILURE; - if (WIFEXITED(status)) { - child_ret = WEXITSTATUS(status); - LOG_DBG("slave exited with code %d", child_ret); - } else if (WIFSIGNALED(status)) { - child_ret = WTERMSIG(status); - LOG_WARN("slave exited with signal %d", child_ret); - } else { - LOG_WARN("slave exited for unknown reason (status = 0x%08x)", status); - } - - if (ret == EXIT_SUCCESS) - ret = child_ret; - } - fdm_destroy(fdm); - config_free(conf); - return ret; + + return ret == EXIT_SUCCESS ? child_ret : ret; } diff --git a/terminal.c b/terminal.c index 684f4c30..023390d9 100644 --- a/terminal.c +++ b/terminal.c @@ -3,8 +3,12 @@ #include #include #include +#include +#include +#include #include +#include #include #define LOG_MODULE "terminal" @@ -14,10 +18,354 @@ #include "render.h" #include "vt.h" #include "selection.h" +#include "config.h" +#include "tokenize.h" +#include "slave.h" #define min(x, y) ((x) < (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y)) +struct terminal * +term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, + int argc, char *const *argv) +{ + struct terminal *term = malloc(sizeof(*term)); + *term = (struct terminal) { + .fdm = fdm, + .quit = false, + .ptmx = posix_openpt(O_RDWR | O_NOCTTY), + .cursor_keys_mode = CURSOR_KEYS_NORMAL, + .keypad_keys_mode = KEYPAD_NUMERICAL, + .auto_margin = true, + .window_title_stack = tll_init(), + .scale = 1, + .flash = { + .fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK), + }, + .blink = { + .fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK), + }, + .vt = { + .state = 1, /* STATE_GROUND */ + }, + .colors = { + .default_fg = conf->colors.fg, + .default_bg = conf->colors.bg, + .default_table = { + conf->colors.regular[0], + conf->colors.regular[1], + conf->colors.regular[2], + conf->colors.regular[3], + conf->colors.regular[4], + conf->colors.regular[5], + conf->colors.regular[6], + conf->colors.regular[7], + + conf->colors.bright[0], + conf->colors.bright[1], + conf->colors.bright[2], + conf->colors.bright[3], + conf->colors.bright[4], + conf->colors.bright[5], + conf->colors.bright[6], + conf->colors.bright[7], + }, + .alpha = conf->colors.alpha, + }, + .default_cursor_style = conf->cursor.style, + .cursor_style = conf->cursor.style, + .default_cursor_color = { + .text = conf->cursor.color.text, + .cursor = conf->cursor.color.cursor, + }, + .cursor_color = { + .text = conf->cursor.color.text, + .cursor = conf->cursor.color.cursor, + }, + .selection = { + .start = {-1, -1}, + .end = {-1, -1}, + }, + .normal = {.damage = tll_init(), .scroll_damage = tll_init()}, + .alt = {.damage = tll_init(), .scroll_damage = tll_init()}, + .grid = &term->normal, + .wl = wayl, + .render = { + .scrollback_lines = conf->scrollback_lines, + .workers = { + .count = conf->render_worker_count, + .queue = tll_init(), + }, + }, + .delayed_render_timer = { + .is_armed = false, + .lower_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC), + .upper_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC), + }, + }; + + LOG_INFO("using %zu rendering threads", term->render.workers.count); + + struct render_worker_context worker_context[term->render.workers.count]; + + /* Initialize 'current' colors from the default colors */ + term->colors.fg = term->colors.default_fg; + term->colors.bg = term->colors.default_bg; + + /* Initialize the 256 gray-scale color cube */ + { + /* First 16 entries have already been initialized from conf */ + for (size_t r = 0; r < 6; r++) { + for (size_t g = 0; g < 6; g++) { + for (size_t b = 0; b < 6; b++) { + term->colors.default_table[16 + r * 6 * 6 + g * 6 + b] + = r * 51 << 16 | g * 51 << 8 | b * 51; + } + } + } + + for (size_t i = 0; i < 24; i++) + term->colors.default_table[232 + i] = i * 11 << 16 | i * 11 << 8 | i * 11; + + memcpy(term->colors.table, term->colors.default_table, sizeof(term->colors.table)); + } + if (term->ptmx == -1) { + LOG_ERR("failed to open pseudo terminal"); + goto out; + } + + if (term->flash.fd == -1 || term->blink.fd == -1) { + LOG_ERR("failed to create timers"); + goto out; + } + + sem_init(&term->render.workers.start, 0, 0); + sem_init(&term->render.workers.done, 0, 0); + mtx_init(&term->render.workers.lock, mtx_plain); + cnd_init(&term->render.workers.cond); + + term->render.workers.threads = calloc(term->render.workers.count, sizeof(term->render.workers.threads[0])); + for (size_t i = 0; i < term->render.workers.count; i++) { + worker_context[i].term = term; + worker_context[i].my_id = 1 + i; + thrd_create(&term->render.workers.threads[i], &render_worker_thread, &worker_context[i]); + } + + font_list_t font_names = tll_init(); + tll_foreach(conf->fonts, it) + tll_push_back(font_names, it->item); + + if ((term->fonts[0] = font_from_name(font_names, "")) == NULL) { + tll_free(font_names); + goto out; + } + + term->fonts[1] = font_from_name(font_names, "style=bold"); + term->fonts[2] = font_from_name(font_names, "style=italic"); + term->fonts[3] = font_from_name(font_names, "style=bold italic"); + + tll_free(font_names); + + { + FT_Face ft_face = term->fonts[0]->face; + int max_x_advance = ft_face->size->metrics.max_advance / 64; + int height = ft_face->size->metrics.height / 64; + int descent = ft_face->size->metrics.descender / 64; + int ascent = ft_face->size->metrics.ascender / 64; + + term->fextents.height = height * term->fonts[0]->pixel_size_fixup; + term->fextents.descent = -descent * term->fonts[0]->pixel_size_fixup; + term->fextents.ascent = ascent * term->fonts[0]->pixel_size_fixup; + term->fextents.max_x_advance = max_x_advance * term->fonts[0]->pixel_size_fixup; + + LOG_DBG("metrics: height: %d, descent: %d, ascent: %d, x-advance: %d", + height, descent, ascent, max_x_advance); + } + + term->cell_width = (int)ceil(term->fextents.max_x_advance); + term->cell_height = (int)ceil(term->fextents.height); + LOG_INFO("cell width=%d, height=%d", term->cell_width, term->cell_height); + + /* Main window */ + term->window = wayl_win_init(wayl); + if (term->window == NULL) + goto out; + + term_set_window_title(term, "foot"); + + unsigned width = conf->width; + unsigned height = conf->height; + + if (width == -1) { + assert(height == -1); + width = 80 * term->cell_width; + height = 24 * term->cell_height; + } + + width = max(width, term->cell_width); + height = max(height, term->cell_height); + render_resize(term, width, height); + + { + int fork_pipe[2]; + if (pipe2(fork_pipe, O_CLOEXEC) < 0) { + LOG_ERRNO("failed to create pipe"); + goto out; + } + + term->slave = fork(); + switch (term->slave) { + case -1: + LOG_ERRNO("failed to fork"); + close(fork_pipe[0]); + close(fork_pipe[1]); + goto out; + + case 0: + /* Child */ + close(fork_pipe[0]); /* Close read end */ + + char **_shell_argv = NULL; + char *const *shell_argv = argv; + + if (argc == 0) { + if (!tokenize_cmdline(conf->shell, &_shell_argv)) { + (void)!write(fork_pipe[1], &errno, sizeof(errno)); + _exit(0); + } + shell_argv = _shell_argv; + } + + slave_spawn(term->ptmx, shell_argv, fork_pipe[1]); + assert(false); + break; + + default: { + close(fork_pipe[1]); /* Close write end */ + LOG_DBG("slave has PID %d", term->slave); + + int _errno; + static_assert(sizeof(errno) == sizeof(_errno), "errno size mismatch"); + + ssize_t ret = read(fork_pipe[0], &_errno, sizeof(_errno)); + close(fork_pipe[0]); + + if (ret < 0) { + LOG_ERRNO("failed to read from pipe"); + goto out; + } else if (ret == sizeof(_errno)) { + LOG_ERRNO( + "%s: failed to execute", argc == 0 ? conf->shell : argv[0]); + goto out; + } else + LOG_DBG("%s: successfully started", conf->shell); + break; + } + } + } + + /* Read logic requires non-blocking mode */ + { + int fd_flags = fcntl(term->ptmx, F_GETFL); + if (fd_flags == -1) { + LOG_ERRNO("failed to set non blocking mode on PTY master"); + goto out; + } + + if (fcntl(term->ptmx, F_SETFL, fd_flags | O_NONBLOCK) == -1) { + LOG_ERRNO("failed to set non blocking mode on PTY master"); + goto out; + } + } + + wayl->term = term; + return term; + +out: + term_destroy(term); + return NULL; +} + +int +term_destroy(struct terminal *term) +{ + if (term == NULL) + return 0; + + wayl_win_destroy(term->window); + + if (term->delayed_render_timer.lower_fd != -1) + close(term->delayed_render_timer.lower_fd); + if (term->delayed_render_timer.upper_fd != -1) + close(term->delayed_render_timer.upper_fd); + + mtx_lock(&term->render.workers.lock); + assert(tll_length(term->render.workers.queue) == 0); + for (size_t i = 0; i < term->render.workers.count; i++) { + sem_post(&term->render.workers.start); + tll_push_back(term->render.workers.queue, -2); + } + cnd_broadcast(&term->render.workers.cond); + mtx_unlock(&term->render.workers.lock); + free(term->vt.osc.data); + for (int row = 0; row < term->normal.num_rows; row++) + grid_row_free(term->normal.rows[row]); + free(term->normal.rows); + for (int row = 0; row < term->alt.num_rows; row++) + grid_row_free(term->alt.rows[row]); + free(term->alt.rows); + + free(term->window_title); + tll_free_and_free(term->window_title_stack, free); + + for (size_t i = 0; i < sizeof(term->fonts) / sizeof(term->fonts[0]); i++) + font_destroy(term->fonts[i]); + + free(term->search.buf); + + if (term->flash.fd != -1) + close(term->flash.fd); + if (term->blink.fd != -1) + close(term->blink.fd); + + if (term->ptmx != -1) + close(term->ptmx); + + for (size_t i = 0; i < term->render.workers.count; i++) + thrd_join(term->render.workers.threads[i], NULL); + free(term->render.workers.threads); + cnd_destroy(&term->render.workers.cond); + mtx_destroy(&term->render.workers.lock); + sem_destroy(&term->render.workers.start); + sem_destroy(&term->render.workers.done); + assert(tll_length(term->render.workers.queue) == 0); + tll_free(term->render.workers.queue); + + int ret = EXIT_SUCCESS; + + if (term->slave > 0) { + /* Note: we've closed ptmx, so the slave *should* exit... */ + int status; + waitpid(term->slave, &status, 0); + + int child_ret = EXIT_FAILURE; + if (WIFEXITED(status)) { + child_ret = WEXITSTATUS(status); + LOG_DBG("slave exited with code %d", child_ret); + } else if (WIFSIGNALED(status)) { + child_ret = WTERMSIG(status); + LOG_WARN("slave exited with signal %d", child_ret); + } else { + LOG_WARN("slave exited for unknown reason (status = 0x%08x)", status); + } + + ret = child_ret; + } + + free(term); + return ret; +} + void term_reset(struct terminal *term, bool hard) { diff --git a/terminal.h b/terminal.h index 2f3fcdc5..7303f503 100644 --- a/terminal.h +++ b/terminal.h @@ -8,6 +8,8 @@ #include #include +//#include "config.h" +#include "fdm.h" #include "font.h" #include "tllist.h" #include "wayland.h" @@ -142,6 +144,8 @@ enum mouse_reporting { enum cursor_style { CURSOR_BLOCK, CURSOR_UNDERLINE, CURSOR_BAR }; struct terminal { + struct fdm *fdm; + pid_t slave; int ptmx; bool quit; @@ -283,6 +287,12 @@ struct terminal { } delayed_render_timer; }; +struct config; +struct terminal *term_init( + const struct config *conf, struct fdm *fdm, struct wayland *wayl, + int argc, char *const *argv); +int term_destroy(struct terminal *term); + void term_reset(struct terminal *term, bool hard); void term_damage_rows(struct terminal *term, int start, int end); -- cgit v1.3 From fe974956b045de20df3f39f6804c5ae934ea8e8d Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 28 Oct 2019 18:35:16 +0100 Subject: term: integrate directly with FDM --- main.c | 218 +--------------------------------------------------------- terminal.c | 227 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 223 insertions(+), 222 deletions(-) (limited to 'terminal.c') diff --git a/main.c b/main.c index 3d951b67..64dea222 100644 --- a/main.c +++ b/main.c @@ -3,227 +3,26 @@ #include #include #include -#include -#include -#include #include #include -#include #include -#include #include -#include -#include - -#include -#include -#include - -#include -#include #define LOG_MODULE "main" -#define LOG_ENABLE_DBG 1 +#define LOG_ENABLE_DBG 0 #include "log.h" #include "config.h" #include "fdm.h" #include "font.h" -#include "grid.h" -#include "render.h" #include "shm.h" -#include "slave.h" #include "terminal.h" -#include "tokenize.h" #include "version.h" -#include "vt.h" #define min(x, y) ((x) < (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y)) -static bool -fdm_ptmx(struct fdm *fdm, int fd, int events, void *data) -{ - struct terminal *term = data; - - if (events & EPOLLHUP) { - term->quit = true; - - if (!(events & EPOLLIN)) - return false; - } - - assert(events & EPOLLIN); - - uint8_t buf[24 * 1024]; - ssize_t count = read(term->ptmx, buf, sizeof(buf)); - - if (count < 0) { - if (errno == EAGAIN) - return true; - - LOG_ERRNO("failed to read from pseudo terminal"); - return false; - } - - vt_from_slave(term, buf, count); - - /* - * We likely need to re-render. But, we don't want to - * do it immediately. Often, a single client operation - * is done through multiple writes. Many times, we're - * so fast that we render mid-operation frames. - * - * For example, we might end up rendering a frame - * where the client just erased a line, while in the - * next frame, the client wrote to the same line. This - * causes screen "flashes". - * - * Mitigate by always incuring a small delay before - * rendering the next frame. This gives the client - * some time to finish the operation (and thus gives - * us time to receive the last writes before doing any - * actual rendering). - * - * We incur this delay *every* time we receive - * input. To ensure we don't delay rendering - * indefinitely, we start a second timer that is only - * reset when we render. - * - * Note that when the client is producing data at a - * very high pace, we're rate limited by the wayland - * compositor anyway. The delay we introduce here only - * has any effect when the renderer is idle. - * - * TODO: this adds input latency. Can we somehow hint - * ourselves we just received keyboard input, and in - * this case *not* delay rendering? - */ - if (term->window->frame_callback == NULL) { - /* First timeout - reset each time we receive input. */ - timerfd_settime( - term->delayed_render_timer.lower_fd, 0, - &(struct itimerspec){.it_value = {.tv_nsec = 1000000}}, - NULL); - - /* Second timeout - only reset when we render. Set to one - * frame (assuming 60Hz) */ - if (!term->delayed_render_timer.is_armed) { - timerfd_settime( - term->delayed_render_timer.upper_fd, 0, - &(struct itimerspec){.it_value = {.tv_nsec = 16666666}}, - NULL); - term->delayed_render_timer.is_armed = true; - } - } - - return !(events & EPOLLHUP); -} - -static bool -fdm_flash(struct fdm *fdm, int fd, int events, void *data) -{ - if (events & EPOLLHUP) - return false; - - struct terminal *term = data; - uint64_t expiration_count; - ssize_t ret = read( - term->flash.fd, &expiration_count, sizeof(expiration_count)); - - if (ret < 0) { - if (errno == EAGAIN) - return true; - - LOG_ERRNO("failed to read flash timer"); - return false; - } - - LOG_DBG("flash timer expired %llu times", - (unsigned long long)expiration_count); - - term->flash.active = false; - term_damage_view(term); - render_refresh(term); - return true; -} - -static bool -fdm_blink(struct fdm *fdm, int fd, int events, void *data) -{ - if (events & EPOLLHUP) - return false; - - struct terminal *term = data; - uint64_t expiration_count; - ssize_t ret = read( - term->blink.fd, &expiration_count, sizeof(expiration_count)); - - if (ret < 0) { - if (errno == EAGAIN) - return true; - - LOG_ERRNO("failed to read blink timer"); - return false; - } - - LOG_DBG("blink timer expired %llu times", - (unsigned long long)expiration_count); - - term->blink.state = term->blink.state == BLINK_ON - ? BLINK_OFF : BLINK_ON; - - /* Scan all visible cells and mark rows with blinking cells dirty */ - for (int r = 0; r < term->rows; r++) { - struct row *row = grid_row_in_view(term->grid, r); - for (int col = 0; col < term->cols; col++) { - struct cell *cell = &row->cells[col]; - - if (cell->attrs.blink) { - cell->attrs.clean = 0; - row->dirty = true; - } - } - } - - render_refresh(term); - return true; -} - -static bool -fdm_delayed_render(struct fdm *fdm, int fd, int events, void *data) -{ - if (events & EPOLLHUP) - return false; - - struct terminal *term = data; - assert(term->delayed_render_timer.is_armed); - - uint64_t unused; - ssize_t ret1 = 0; - ssize_t ret2 = 0; - - if (fd == term->delayed_render_timer.lower_fd) - ret1 = read(term->delayed_render_timer.lower_fd, &unused, sizeof(unused)); - if (fd == term->delayed_render_timer.upper_fd) - ret2 = read(term->delayed_render_timer.upper_fd, &unused, sizeof(unused)); - - if ((ret1 < 0 || ret2 < 0) && errno != EAGAIN) - LOG_ERRNO("failed to read timeout timer"); - else if (ret1 > 0 || ret2 > 0) { - render_refresh(term); - - /* Reset timers */ - term->delayed_render_timer.is_armed = false; - timerfd_settime(term->delayed_render_timer.lower_fd, 0, &(struct itimerspec){.it_value = {0}}, NULL); - timerfd_settime(term->delayed_render_timer.upper_fd, 0, &(struct itimerspec){.it_value = {0}}, NULL); - } else - assert(false); - - return true; -} - static void print_usage(const char *prog_name) { @@ -330,7 +129,6 @@ main(int argc, char *const *argv) setlocale(LC_ALL, ""); setenv("TERM", conf.term, 1); - struct fdm *fdm = NULL; struct wayland *wayl = NULL; struct terminal *term = NULL; @@ -344,12 +142,6 @@ main(int argc, char *const *argv) if ((term = term_init(&conf, fdm, wayl, argc, argv)) == NULL) goto out; - fdm_add(fdm, term->ptmx, EPOLLIN, &fdm_ptmx, term); - fdm_add(fdm, term->flash.fd, EPOLLIN, &fdm_flash, term); - fdm_add(fdm, term->blink.fd, EPOLLIN, &fdm_blink, term); - fdm_add(fdm, term->delayed_render_timer.lower_fd, EPOLLIN, &fdm_delayed_render, term); - fdm_add(fdm, term->delayed_render_timer.upper_fd, EPOLLIN, &fdm_delayed_render, term); - while (true) { wl_display_flush(term->wl->display); /* TODO: figure out how to get rid of this */ @@ -361,14 +153,6 @@ main(int argc, char *const *argv) ret = EXIT_SUCCESS; out: - if (fdm != NULL) { - fdm_del(fdm, term->ptmx); - fdm_del(fdm, term->flash.fd); - fdm_del(fdm, term->blink.fd); - fdm_del(fdm, term->delayed_render_timer.lower_fd); - fdm_del(fdm, term->delayed_render_timer.upper_fd); - } - shm_fini(); int child_ret = term_destroy(term); diff --git a/terminal.c b/terminal.c index 023390d9..2cf87fa6 100644 --- a/terminal.c +++ b/terminal.c @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -25,6 +26,188 @@ #define min(x, y) ((x) < (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y)) +static bool +fdm_ptmx(struct fdm *fdm, int fd, int events, void *data) +{ + struct terminal *term = data; + + if (events & EPOLLHUP) { + term->quit = true; + + if (!(events & EPOLLIN)) + return false; + } + + assert(events & EPOLLIN); + + uint8_t buf[24 * 1024]; + ssize_t count = read(term->ptmx, buf, sizeof(buf)); + + if (count < 0) { + if (errno == EAGAIN) + return true; + + LOG_ERRNO("failed to read from pseudo terminal"); + return false; + } + + vt_from_slave(term, buf, count); + + /* + * We likely need to re-render. But, we don't want to + * do it immediately. Often, a single client operation + * is done through multiple writes. Many times, we're + * so fast that we render mid-operation frames. + * + * For example, we might end up rendering a frame + * where the client just erased a line, while in the + * next frame, the client wrote to the same line. This + * causes screen "flashes". + * + * Mitigate by always incuring a small delay before + * rendering the next frame. This gives the client + * some time to finish the operation (and thus gives + * us time to receive the last writes before doing any + * actual rendering). + * + * We incur this delay *every* time we receive + * input. To ensure we don't delay rendering + * indefinitely, we start a second timer that is only + * reset when we render. + * + * Note that when the client is producing data at a + * very high pace, we're rate limited by the wayland + * compositor anyway. The delay we introduce here only + * has any effect when the renderer is idle. + * + * TODO: this adds input latency. Can we somehow hint + * ourselves we just received keyboard input, and in + * this case *not* delay rendering? + */ + if (term->window->frame_callback == NULL) { + /* First timeout - reset each time we receive input. */ + timerfd_settime( + term->delayed_render_timer.lower_fd, 0, + &(struct itimerspec){.it_value = {.tv_nsec = 1000000}}, + NULL); + + /* Second timeout - only reset when we render. Set to one + * frame (assuming 60Hz) */ + if (!term->delayed_render_timer.is_armed) { + timerfd_settime( + term->delayed_render_timer.upper_fd, 0, + &(struct itimerspec){.it_value = {.tv_nsec = 16666666}}, + NULL); + term->delayed_render_timer.is_armed = true; + } + } + + return !(events & EPOLLHUP); +} + +static bool +fdm_flash(struct fdm *fdm, int fd, int events, void *data) +{ + if (events & EPOLLHUP) + return false; + + struct terminal *term = data; + uint64_t expiration_count; + ssize_t ret = read( + term->flash.fd, &expiration_count, sizeof(expiration_count)); + + if (ret < 0) { + if (errno == EAGAIN) + return true; + + LOG_ERRNO("failed to read flash timer"); + return false; + } + + LOG_DBG("flash timer expired %llu times", + (unsigned long long)expiration_count); + + term->flash.active = false; + term_damage_view(term); + render_refresh(term); + return true; +} + +static bool +fdm_blink(struct fdm *fdm, int fd, int events, void *data) +{ + if (events & EPOLLHUP) + return false; + + struct terminal *term = data; + uint64_t expiration_count; + ssize_t ret = read( + term->blink.fd, &expiration_count, sizeof(expiration_count)); + + if (ret < 0) { + if (errno == EAGAIN) + return true; + + LOG_ERRNO("failed to read blink timer"); + return false; + } + + LOG_DBG("blink timer expired %llu times", + (unsigned long long)expiration_count); + + term->blink.state = term->blink.state == BLINK_ON + ? BLINK_OFF : BLINK_ON; + + /* Scan all visible cells and mark rows with blinking cells dirty */ + for (int r = 0; r < term->rows; r++) { + struct row *row = grid_row_in_view(term->grid, r); + for (int col = 0; col < term->cols; col++) { + struct cell *cell = &row->cells[col]; + + if (cell->attrs.blink) { + cell->attrs.clean = 0; + row->dirty = true; + } + } + } + + render_refresh(term); + return true; +} + +static bool +fdm_delayed_render(struct fdm *fdm, int fd, int events, void *data) +{ + if (events & EPOLLHUP) + return false; + + struct terminal *term = data; + assert(term->delayed_render_timer.is_armed); + + uint64_t unused; + ssize_t ret1 = 0; + ssize_t ret2 = 0; + + if (fd == term->delayed_render_timer.lower_fd) + ret1 = read(term->delayed_render_timer.lower_fd, &unused, sizeof(unused)); + if (fd == term->delayed_render_timer.upper_fd) + ret2 = read(term->delayed_render_timer.upper_fd, &unused, sizeof(unused)); + + if ((ret1 < 0 || ret2 < 0) && errno != EAGAIN) + LOG_ERRNO("failed to read timeout timer"); + else if (ret1 > 0 || ret2 > 0) { + render_refresh(term); + + /* Reset timers */ + term->delayed_render_timer.is_armed = false; + timerfd_settime(term->delayed_render_timer.lower_fd, 0, &(struct itimerspec){.it_value = {0}}, NULL); + timerfd_settime(term->delayed_render_timer.upper_fd, 0, &(struct itimerspec){.it_value = {0}}, NULL); + } else + assert(false); + + return true; +} + struct terminal * term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, int argc, char *const *argv) @@ -278,6 +461,28 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, } } + if (!fdm_add(fdm, term->ptmx, EPOLLIN, &fdm_ptmx, term)) { + LOG_ERR("failed to add ptmx to FDM"); + goto out; + } + + if (!fdm_add(fdm, term->flash.fd, EPOLLIN, &fdm_flash, term)) { + LOG_ERR("failed to add flash timer FD to FDM"); + goto out; + } + + if (!fdm_add(fdm, term->blink.fd, EPOLLIN, &fdm_blink, term)) { + LOG_ERR("failed to add blink tiemr FD to FDM"); + goto out; + } + + if (!fdm_add(fdm, term->delayed_render_timer.lower_fd, EPOLLIN, &fdm_delayed_render, term) || + !fdm_add(fdm, term->delayed_render_timer.upper_fd, EPOLLIN, &fdm_delayed_render, term)) + { + LOG_ERR("failed to add delayed rendering timer FDs to FDM"); + goto out; + } + wayl->term = term; return term; @@ -294,10 +499,15 @@ term_destroy(struct terminal *term) wayl_win_destroy(term->window); - if (term->delayed_render_timer.lower_fd != -1) + if (term->delayed_render_timer.lower_fd != -1) { + fdm_del(term->fdm, term->delayed_render_timer.lower_fd); close(term->delayed_render_timer.lower_fd); - if (term->delayed_render_timer.upper_fd != -1) + } + + if (term->delayed_render_timer.upper_fd != -1) { + fdm_del(term->fdm, term->delayed_render_timer.upper_fd); close(term->delayed_render_timer.upper_fd); + } mtx_lock(&term->render.workers.lock); assert(tll_length(term->render.workers.queue) == 0); @@ -323,13 +533,20 @@ term_destroy(struct terminal *term) free(term->search.buf); - if (term->flash.fd != -1) + if (term->flash.fd != -1) { + fdm_del(term->fdm, term->flash.fd); close(term->flash.fd); - if (term->blink.fd != -1) + } + + if (term->blink.fd != -1) { + fdm_del(term->fdm, term->blink.fd); close(term->blink.fd); + } - if (term->ptmx != -1) + if (term->ptmx != -1) { + fdm_del(term->fdm, term->ptmx); close(term->ptmx); + } for (size_t i = 0; i < term->render.workers.count; i++) thrd_join(term->render.workers.threads[i], NULL); -- cgit v1.3 From 720d0df067d0e8add7793f25398e21ca0486bce2 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 28 Oct 2019 18:46:03 +0100 Subject: term: open FDs before allocating terminal struct --- terminal.c | 111 ++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 69 insertions(+), 42 deletions(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 2cf87fa6..cd8d0ed9 100644 --- a/terminal.c +++ b/terminal.c @@ -212,22 +212,46 @@ struct terminal * term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, int argc, char *const *argv) { - struct terminal *term = malloc(sizeof(*term)); + int ptmx = -1; + int flash_fd = -1; + int blink_fd = -1; + int delay_lower_fd = -1; + int delay_upper_fd = -1; + + struct terminal *term = NULL; + struct render_worker_context worker_context[conf->render_worker_count]; + + if ((ptmx = posix_openpt(O_RDWR | O_NOCTTY)) == -1) { + LOG_ERRNO("failed to open PTY"); + goto close_fds; + } + if ((flash_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK)) == -1) { + LOG_ERRNO("failed to create flash timer FD"); + goto close_fds; + } + if ((blink_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK)) == -1) { + LOG_ERRNO("failed to create blink timer FD"); + goto close_fds; + } + if ((delay_lower_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK)) == -1 || + (delay_upper_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK)) == -1) + { + LOG_ERRNO("failed to create delayed rendering timer FDs"); + goto close_fds; + } + + term = malloc(sizeof(*term)); *term = (struct terminal) { .fdm = fdm, .quit = false, - .ptmx = posix_openpt(O_RDWR | O_NOCTTY), + .ptmx = ptmx, .cursor_keys_mode = CURSOR_KEYS_NORMAL, .keypad_keys_mode = KEYPAD_NUMERICAL, .auto_margin = true, .window_title_stack = tll_init(), .scale = 1, - .flash = { - .fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK), - }, - .blink = { - .fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK), - }, + .flash = {.fd = flash_fd}, + .blink = {.fd = blink_fd}, .vt = { .state = 1, /* STATE_GROUND */ }, @@ -282,14 +306,13 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, }, .delayed_render_timer = { .is_armed = false, - .lower_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC), - .upper_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC), + .lower_fd = delay_lower_fd, + .upper_fd = delay_upper_fd, }, }; LOG_INFO("using %zu rendering threads", term->render.workers.count); - struct render_worker_context worker_context[term->render.workers.count]; /* Initialize 'current' colors from the default colors */ term->colors.fg = term->colors.default_fg; @@ -312,15 +335,6 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, memcpy(term->colors.table, term->colors.default_table, sizeof(term->colors.table)); } - if (term->ptmx == -1) { - LOG_ERR("failed to open pseudo terminal"); - goto out; - } - - if (term->flash.fd == -1 || term->blink.fd == -1) { - LOG_ERR("failed to create timers"); - goto out; - } sem_init(&term->render.workers.start, 0, 0); sem_init(&term->render.workers.done, 0, 0); @@ -340,7 +354,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, if ((term->fonts[0] = font_from_name(font_names, "")) == NULL) { tll_free(font_names); - goto out; + goto err; } term->fonts[1] = font_from_name(font_names, "style=bold"); @@ -369,10 +383,9 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, term->cell_height = (int)ceil(term->fextents.height); LOG_INFO("cell width=%d, height=%d", term->cell_width, term->cell_height); - /* Main window */ term->window = wayl_win_init(wayl); if (term->window == NULL) - goto out; + goto err; term_set_window_title(term, "foot"); @@ -393,7 +406,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, int fork_pipe[2]; if (pipe2(fork_pipe, O_CLOEXEC) < 0) { LOG_ERRNO("failed to create pipe"); - goto out; + goto err; } term->slave = fork(); @@ -402,7 +415,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, LOG_ERRNO("failed to fork"); close(fork_pipe[0]); close(fork_pipe[1]); - goto out; + goto err; case 0: /* Child */ @@ -419,7 +432,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, shell_argv = _shell_argv; } - slave_spawn(term->ptmx, shell_argv, fork_pipe[1]); + slave_spawn(ptmx, shell_argv, fork_pipe[1]); assert(false); break; @@ -435,11 +448,11 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, if (ret < 0) { LOG_ERRNO("failed to read from pipe"); - goto out; + goto err; } else if (ret == sizeof(_errno)) { LOG_ERRNO( "%s: failed to execute", argc == 0 ? conf->shell : argv[0]); - goto out; + goto err; } else LOG_DBG("%s: successfully started", conf->shell); break; @@ -449,46 +462,60 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, /* Read logic requires non-blocking mode */ { - int fd_flags = fcntl(term->ptmx, F_GETFL); + int fd_flags = fcntl(ptmx, F_GETFL); if (fd_flags == -1) { LOG_ERRNO("failed to set non blocking mode on PTY master"); - goto out; + goto err; } - if (fcntl(term->ptmx, F_SETFL, fd_flags | O_NONBLOCK) == -1) { + if (fcntl(ptmx, F_SETFL, fd_flags | O_NONBLOCK) == -1) { LOG_ERRNO("failed to set non blocking mode on PTY master"); - goto out; + goto err; } } - if (!fdm_add(fdm, term->ptmx, EPOLLIN, &fdm_ptmx, term)) { + if (!fdm_add(fdm, ptmx, EPOLLIN, &fdm_ptmx, term)) { LOG_ERR("failed to add ptmx to FDM"); - goto out; + goto err; } - if (!fdm_add(fdm, term->flash.fd, EPOLLIN, &fdm_flash, term)) { + if (!fdm_add(fdm, flash_fd, EPOLLIN, &fdm_flash, term)) { LOG_ERR("failed to add flash timer FD to FDM"); - goto out; + goto err; } - if (!fdm_add(fdm, term->blink.fd, EPOLLIN, &fdm_blink, term)) { + if (!fdm_add(fdm, blink_fd, EPOLLIN, &fdm_blink, term)) { LOG_ERR("failed to add blink tiemr FD to FDM"); - goto out; + goto err; } - if (!fdm_add(fdm, term->delayed_render_timer.lower_fd, EPOLLIN, &fdm_delayed_render, term) || - !fdm_add(fdm, term->delayed_render_timer.upper_fd, EPOLLIN, &fdm_delayed_render, term)) + if (!fdm_add(fdm, delay_lower_fd, EPOLLIN, &fdm_delayed_render, term) || + !fdm_add(fdm, delay_upper_fd, EPOLLIN, &fdm_delayed_render, term)) { LOG_ERR("failed to add delayed rendering timer FDs to FDM"); - goto out; + goto err; } wayl->term = term; return term; -out: +err: term_destroy(term); return NULL; + +close_fds: + if (ptmx != -1) + close(ptmx); + if (flash_fd != -1) + close(flash_fd); + if (blink_fd != -1) + close(blink_fd); + if (delay_lower_fd != -1) + close(delay_lower_fd); + if (delay_upper_fd != -1) + close(delay_upper_fd); + assert(term == NULL); + return NULL; } int -- cgit v1.3 From 8e6f87eb17b562fd3d1def7a65b5dc8346642f8e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 28 Oct 2019 18:48:43 +0100 Subject: render worker context: allocate, and let worker threads free Since we now initialize the worker threads from term_init(), which returns before the threads terminate, we can no longer use stack-allocated worker contexts. We _could_ put them in the terminal struct. But a simpler solution is to allocate them in term_init(), and let the threads free them when they don't need them anymore. --- render.c | 1 + terminal.c | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'terminal.c') diff --git a/render.c b/render.c index f1eaa9fa..0c559ce0 100644 --- a/render.c +++ b/render.c @@ -374,6 +374,7 @@ render_worker_thread(void *_ctx) struct render_worker_context *ctx = _ctx; struct terminal *term = ctx->term; const int my_id = ctx->my_id; + free(ctx); char proc_title[16]; snprintf(proc_title, sizeof(proc_title), "foot:render:%d", my_id); diff --git a/terminal.c b/terminal.c index cd8d0ed9..5c9c7b37 100644 --- a/terminal.c +++ b/terminal.c @@ -219,7 +219,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, int delay_upper_fd = -1; struct terminal *term = NULL; - struct render_worker_context worker_context[conf->render_worker_count]; + struct render_worker_context *worker_context[conf->render_worker_count]; if ((ptmx = posix_openpt(O_RDWR | O_NOCTTY)) == -1) { LOG_ERRNO("failed to open PTY"); @@ -343,9 +343,10 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, term->render.workers.threads = calloc(term->render.workers.count, sizeof(term->render.workers.threads[0])); for (size_t i = 0; i < term->render.workers.count; i++) { - worker_context[i].term = term; - worker_context[i].my_id = 1 + i; - thrd_create(&term->render.workers.threads[i], &render_worker_thread, &worker_context[i]); + worker_context[i] = malloc(sizeof(*worker_context[i])); + worker_context[i]->term = term; + worker_context[i]->my_id = 1 + i; + thrd_create(&term->render.workers.threads[i], &render_worker_thread, worker_context[i]); } font_list_t font_names = tll_init(); -- cgit v1.3 From 4ec9db8e186675793f4d2a71cbeaf70882d65efa Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 28 Oct 2019 19:23:41 +0100 Subject: term: no need to stack-allocate an array of worker contexts --- terminal.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 5c9c7b37..9aadd549 100644 --- a/terminal.c +++ b/terminal.c @@ -219,7 +219,6 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, int delay_upper_fd = -1; struct terminal *term = NULL; - struct render_worker_context *worker_context[conf->render_worker_count]; if ((ptmx = posix_openpt(O_RDWR | O_NOCTTY)) == -1) { LOG_ERRNO("failed to open PTY"); @@ -341,12 +340,16 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, mtx_init(&term->render.workers.lock, mtx_plain); cnd_init(&term->render.workers.cond); - term->render.workers.threads = calloc(term->render.workers.count, sizeof(term->render.workers.threads[0])); + term->render.workers.threads = calloc( + term->render.workers.count, sizeof(term->render.workers.threads[0])); + for (size_t i = 0; i < term->render.workers.count; i++) { - worker_context[i] = malloc(sizeof(*worker_context[i])); - worker_context[i]->term = term; - worker_context[i]->my_id = 1 + i; - thrd_create(&term->render.workers.threads[i], &render_worker_thread, worker_context[i]); + struct render_worker_context *ctx = malloc(sizeof(*ctx)); + *ctx = (struct render_worker_context) { + .term = term, + .my_id = 1 + i, + }; + thrd_create(&term->render.workers.threads[i], &render_worker_thread, ctx); } font_list_t font_names = tll_init(); -- cgit v1.3 From ce5f5e4d51d693933cb7193ccce2c19d4feac79c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 28 Oct 2019 18:53:41 +0100 Subject: slave: rename slave_spawn() -> slave_exec() --- slave.c | 2 +- slave.h | 2 +- terminal.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'terminal.c') diff --git a/slave.c b/slave.c index 18fcf3ad..92571fde 100644 --- a/slave.c +++ b/slave.c @@ -13,7 +13,7 @@ #include "log.h" void -slave_spawn(int ptmx, char *const argv[], int err_fd) +slave_exec(int ptmx, char *const argv[], int err_fd) { int pts = -1; const char *pts_name = ptsname(ptmx); diff --git a/slave.h b/slave.h index 4161dbda..5ec59149 100644 --- a/slave.h +++ b/slave.h @@ -1,4 +1,4 @@ #pragma once #include -void slave_spawn(int ptmx, char *const argv[], int err_fd); +void slave_exec(int ptmx, char *const argv[], int err_fd); diff --git a/terminal.c b/terminal.c index 9aadd549..9259cfd5 100644 --- a/terminal.c +++ b/terminal.c @@ -436,7 +436,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, shell_argv = _shell_argv; } - slave_spawn(ptmx, shell_argv, fork_pipe[1]); + slave_exec(ptmx, shell_argv, fork_pipe[1]); assert(false); break; -- cgit v1.3