From 9a3ac3a4063b6dd4ee8f0eea35249cc478dbed62 Mon Sep 17 00:00:00 2001 From: Raimund Sacherer Date: Wed, 1 Nov 2023 10:32:46 +0100 Subject: [PATCH] Crosshair coredump fix. Crosshair styles. Fixed possibilities for coredumps when having a fixed cross hair and modifying the window size to the extend the crosshair would now be out of the window boundaries. Implemented a Crosshair style switch from Full Cross to Vertical Line to Horizontal Line. --- config.c | 2 ++ config.h | 8 +++++++- doc/foot.ini.5.scd | 6 ++++++ input.c | 20 ++++++++++++++++++++ key-binding.h | 1 + render.c | 46 +++++++++++++++++++++------------------------- terminal.c | 4 ++++ terminal.h | 2 ++ 8 files changed, 63 insertions(+), 26 deletions(-) diff --git a/config.c b/config.c index 36f58662..b07578b2 100644 --- a/config.c +++ b/config.c @@ -105,6 +105,7 @@ static const char *const binding_action_map[] = { [BIND_ACTION_CROSSHAIR_FIX_POSITION] = "crosshair-fix-position", [BIND_ACTION_CROSSHAIR_PIXEL_POSITION] = "crosshair-use-pixel-position", [BIND_ACTION_CROSSHAIR_MOUSE_POSITION] = "crosshair-use-mouse-position", + [BIND_ACTION_CROSSHAIR_STYLE] = "crosshair-style", [BIND_ACTION_FONT_SIZE_UP] = "font-increase", [BIND_ACTION_FONT_SIZE_DOWN] = "font-decrease", [BIND_ACTION_FONT_SIZE_RESET] = "font-reset", @@ -2868,6 +2869,7 @@ add_default_key_bindings(struct config *conf) {BIND_ACTION_CROSSHAIR_FIX_POSITION, m_ctrl_shift, {{XKB_KEY_f}}}, {BIND_ACTION_CROSSHAIR_MOUSE_POSITION, m_ctrl_shift, {{XKB_KEY_m}}}, {BIND_ACTION_CROSSHAIR_PIXEL_POSITION, m_ctrl_shift, {{XKB_KEY_p}}}, + {BIND_ACTION_CROSSHAIR_STYLE, m_ctrl_shift, {{XKB_KEY_s}}}, {BIND_ACTION_FONT_SIZE_UP, m_ctrl, {{XKB_KEY_plus}}}, {BIND_ACTION_FONT_SIZE_UP, m_ctrl, {{XKB_KEY_equal}}}, {BIND_ACTION_FONT_SIZE_UP, m_ctrl, {{XKB_KEY_KP_Add}}}, diff --git a/config.h b/config.h index 04b57667..9ac62721 100644 --- a/config.h +++ b/config.h @@ -29,7 +29,13 @@ struct font_size_adjustment { enum cursor_style { CURSOR_BLOCK, CURSOR_UNDERLINE, CURSOR_BEAM }; -enum conf_size_type {CONF_SIZE_PX, CONF_SIZE_CELLS}; +enum conf_size_type { CONF_SIZE_PX, CONF_SIZE_CELLS }; + +enum crosshair_style { + CROSSHAIR_FULL, + CROSSHAIR_HORIZONTAL, + CROSSHAIR_VERTICAL +}; struct config_font { char *pattern; diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index 6ea7b9a4..0d2aa01f 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -948,6 +948,12 @@ e.g. *search-start=none*. Default: _Control+Shift+p_. +*crosshair-style* + A toggle to switch the style from full cross to vertical line to + horizontal line and back to full cross. + + Default: _Control+Shfit+s_. + # SECTION: search-bindings This section lets you override the default key bindings used in diff --git a/input.c b/input.c index 188e96c7..497b879f 100644 --- a/input.c +++ b/input.c @@ -202,6 +202,26 @@ execute_binding(struct seat *seat, struct terminal *term, term->crosshair.use_mouse_pixel_coordinates = !term->crosshair.use_mouse_pixel_coordinates; return true; + case BIND_ACTION_CROSSHAIR_STYLE: + switch (term->crosshair.style) { + case CROSSHAIR_FULL: + term->crosshair.style = CROSSHAIR_VERTICAL; + break; + + case CROSSHAIR_VERTICAL: + term->crosshair.style = CROSSHAIR_HORIZONTAL; + break; + + case CROSSHAIR_HORIZONTAL: + term->crosshair.style = CROSSHAIR_FULL; + break; + + default: + BUG("Unhandled crosshair style"); + break; + } + return true; + case BIND_ACTION_FONT_SIZE_UP: term_font_size_increase(term); return true; diff --git a/key-binding.h b/key-binding.h index 5cb6f68b..205c630c 100644 --- a/key-binding.h +++ b/key-binding.h @@ -26,6 +26,7 @@ enum bind_action_normal { BIND_ACTION_CROSSHAIR_MOUSE_POSITION, BIND_ACTION_CROSSHAIR_FIX_POSITION, BIND_ACTION_CROSSHAIR_PIXEL_POSITION, + BIND_ACTION_CROSSHAIR_STYLE, BIND_ACTION_FONT_SIZE_UP, BIND_ACTION_FONT_SIZE_DOWN, BIND_ACTION_FONT_SIZE_RESET, diff --git a/render.c b/render.c index 19cbae63..7642ca9c 100644 --- a/render.c +++ b/render.c @@ -1746,17 +1746,18 @@ render_overlay(struct terminal *term) damage_bounds = (pixman_box32_t){0, 0, buf->width, buf->height}; } - if (style == OVERLAY_CROSSHAIR) { int cursor_row = term->grid->cursor.point.row; int cursor_col = term->grid->cursor.point.col; int mouse_x = 0; int mouse_y = 0; + int y = 0; + int x = 0; tll_foreach(term->wl->seats, it) { if (it->item.kbd_focus == term) { if (it->item.pointer.hidden == 0 && - term->crosshair.use_mouse_position == true) { + term->crosshair.use_mouse_position == true) { cursor_row = it->item.mouse.row > 0 ? it->item.mouse.row : 0; cursor_col = it->item.mouse.col > 0 ? it->item.mouse.col : 0; @@ -1779,33 +1780,28 @@ render_overlay(struct terminal *term) &(pixman_rectangle16_t){0, 0, term->width, term->height}); if (term->crosshair.use_mouse_pixel_coordinates == false) { - int y = term->margins.top + (cursor_row * term->cell_height) + term->cell_height; - int y2 = 1; // --FIXME-- maybe define crosshair thickness? - - if (y + y2 > term->height) - y = term->height - 1; - - pixman_image_fill_rectangles( - PIXMAN_OP_SRC, buf->pix[0], &color, 1, - &(pixman_rectangle16_t){term->margins.left, y, term->width, y2} ); - - pixman_image_fill_rectangles( - PIXMAN_OP_SRC, buf->pix[0], &color, 1, - &(pixman_rectangle16_t){term->margins.left + (cursor_col * term->cell_width), - term->margins.top, 1, term->height} ); + y = min(term->margins.top + (cursor_row * term->cell_height) + term->cell_height, + term->height - term->crosshair.width); + x = min(term->margins.left + (cursor_col * term->cell_width), + term->width - term->crosshair.width); } else { - if (mouse_x > 0 || mouse_y > 0) { - pixman_image_fill_rectangles( - PIXMAN_OP_SRC, buf->pix[0], &color, 1, - &(pixman_rectangle16_t){term->margins.left, mouse_y, term->width, 1} ); - - pixman_image_fill_rectangles( - PIXMAN_OP_SRC, buf->pix[0], &color, 1, - &(pixman_rectangle16_t){mouse_x, term->margins.top, 1, term->height} ); - } + y = max(min(mouse_y, term->height - term->crosshair.width), 1); + x = max(min(mouse_x, term->width - term->crosshair.width), 1); } + if (term->crosshair.style == CROSSHAIR_FULL || + term->crosshair.style == CROSSHAIR_HORIZONTAL) + pixman_image_fill_rectangles( + PIXMAN_OP_SRC, buf->pix[0], &color, 1, + &(pixman_rectangle16_t){term->margins.left, y, term->width, term->crosshair.width}); + + if (term->crosshair.style == CROSSHAIR_FULL || + term->crosshair.style == CROSSHAIR_VERTICAL) + pixman_image_fill_rectangles( + PIXMAN_OP_SRC, buf->pix[0], &color, 1, + &(pixman_rectangle16_t){x, term->margins.top, term->crosshair.width, term->height}); + if (term->crosshair.position_fixed == false) { term->render.last_crosshair.row = cursor_row; term->render.last_crosshair.col = cursor_col; diff --git a/terminal.c b/terminal.c index 4d7b7579..901b4ed5 100644 --- a/terminal.c +++ b/terminal.c @@ -1217,6 +1217,8 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, .crosshair.position_fixed = false, .crosshair.use_mouse_position = false, .crosshair.use_mouse_pixel_coordinates = false, + .crosshair.width = 1, + .crosshair.style = CROSSHAIR_FULL, .tab_stops = tll_init(), .wl = wayl, .render = { @@ -1973,6 +1975,8 @@ term_reset(struct terminal *term, bool hard) term->crosshair.position_fixed = false; term->crosshair.use_mouse_position = false; term->crosshair.use_mouse_pixel_coordinates = false; + term->crosshair.width = 1; + term->crosshair.style = CROSSHAIR_FULL; tll_free(term->normal.scroll_damage); tll_free(term->alt.scroll_damage); diff --git a/terminal.h b/terminal.h index 064ef3fb..e145e8b3 100644 --- a/terminal.h +++ b/terminal.h @@ -564,10 +564,12 @@ struct terminal { } search; struct { + int width; bool active; bool position_fixed; bool use_mouse_position; bool use_mouse_pixel_coordinates; + enum crosshair_style style; } crosshair; struct wayland *wl;