diff --git a/CHANGELOG.md b/CHANGELOG.md index a9529ba9..35669232 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,21 @@ ### Added ### Deprecated ### Removed +### Changed + +* Mouse cursor is now always a `left_ptr` when inside the margins, to + indicate it is not possible to start a selection. +* Scrollback position indicator. This feature is optional and + controlled by the **scrollback-indicator-style** and + **scrollback-indicator-format** options in `footrc` + (https://codeberg.org/dnkl/foot/issues/42). + + ### Fixed + +* Crash when starting a selection inside the margins. + + ### Security ### Contributors diff --git a/config.c b/config.c index 283a72ea..2b989d62 100644 --- a/config.c +++ b/config.c @@ -288,7 +288,37 @@ parse_section_main(const char *key, const char *value, struct config *conf, LOG_ERR("%s:%d: expected an integer: %s", path, lineno, value); return false; } - conf->scrollback_lines = lines; + conf->scrollback.lines = lines; + } + + else if (strcmp(key, "scrollback-indicator-style") == 0) { + if (strcmp(value, "none") == 0) + conf->scrollback.indicator.style = SCROLLBACK_INDICATOR_STYLE_NONE; + else if (strcmp(value, "fixed") == 0) + conf->scrollback.indicator.style = SCROLLBACK_INDICATOR_STYLE_FIXED; + else if (strcmp(value, "relative") == 0) + conf->scrollback.indicator.style = SCROLLBACK_INDICATOR_STYLE_RELATIVE; + else { + LOG_ERR("%s:%d: scrollback-indicator-style must be one of " + "'none', 'fixed' or 'moving'", + path, lineno); + return false; + } + } + + else if (strcmp(key, "scrollback-indicator-format") == 0) { + if (strcmp(value, "percent") == 0) { + conf->scrollback.indicator.format + = SCROLLBACK_INDICATOR_FORMAT_PERCENTAGE; + } else if (strcmp(value, "line") == 0) { + conf->scrollback.indicator.format + = SCROLLBACK_INDICATOR_FORMAT_LINENO; + } else { + LOG_ERR("%s:%d: 'scrollback-indicator-format must be one " + "of 'percent' or 'line'", + path, lineno); + return false; + } } else { @@ -917,8 +947,13 @@ config_load(struct config *conf, const char *conf_path) .pad_y = 2, .startup_mode = STARTUP_WINDOWED, .fonts = tll_init(), - .scrollback_lines = 1000, - + .scrollback = { + .lines = 1000, + .indicator = { + .style = SCROLLBACK_INDICATOR_STYLE_RELATIVE, + .format = SCROLLBACK_INDICATOR_FORMAT_PERCENTAGE, + }, + }, .colors = { .fg = default_foreground, .bg = default_background, diff --git a/config.h b/config.h index 2b43b806..456019b5 100644 --- a/config.h +++ b/config.h @@ -39,7 +39,22 @@ struct config { tll(struct config_font) fonts; - int scrollback_lines; + struct { + int lines; + + struct { + enum { + SCROLLBACK_INDICATOR_STYLE_NONE, + SCROLLBACK_INDICATOR_STYLE_FIXED, + SCROLLBACK_INDICATOR_STYLE_RELATIVE + } style; + + enum { + SCROLLBACK_INDICATOR_FORMAT_PERCENTAGE, + SCROLLBACK_INDICATOR_FORMAT_LINENO + } format; + } indicator; + } scrollback; struct { uint32_t fg; diff --git a/doc/foot.5.scd b/doc/foot.5.scd index d837cd1a..188e8edf 100644 --- a/doc/foot.5.scd +++ b/doc/foot.5.scd @@ -68,6 +68,19 @@ in this order: *scrollback* Number of scrollback lines. Default: _1000_. +*scrollback-indicator-style* + Configures the style of the scrollback position indicator. One of + *none*, *fixed* or *relative*. *none* disables the indicator + completely. *fixed* always renders the indicator near the top at + the window, and *relative* renders the indicator at the position + corresponding to the current scrollback position. Default: + _relative_. + +*scrollback-indicator-format* + Which format to use when displaying the scrollback position + indicator. Either _percentage_ or _line_. This option is ignored + if *scrollback-indicator-style=none*. Default: _percentage_. + *workers* Number of threads to use for rendering. Set to 0 to disable multithreading. Default: the number of available logical CPUs diff --git a/footrc b/footrc index e7139a83..117b73fe 100644 --- a/footrc +++ b/footrc @@ -2,6 +2,8 @@ # font=monospace # scrollback=1000 +# scrollback-indicator-style=relative +# scrollback-indicator-format=percentage # geometry=700x500 # pad=2x2 # initial-window-mode=windowed diff --git a/input.c b/input.c index c7f4b073..67661eee 100644 --- a/input.c +++ b/input.c @@ -1023,13 +1023,19 @@ wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, int y = wl_fixed_to_int(surface_y) * term->scale; switch ((term->active_surface = term_surface_kind(term, surface))) { - case TERM_SURF_GRID: - seat->mouse.col = x / term->cell_width; - seat->mouse.row = y / term->cell_height; + case TERM_SURF_GRID: { + int col = (x - term->margins.left) / term->cell_width; + int row = (y - term->margins.top) / term->cell_height; + + seat->mouse.col = col >= 0 && col < term->cols ? col : -1; + seat->mouse.row = row >= 0 && row < term->rows ? row : -1; + term_xcursor_update(term); break; + } case TERM_SURF_SEARCH: + case TERM_SURF_SCROLLBACK_INDICATOR: case TERM_SURF_TITLE: render_xcursor_set(seat, term, XCURSOR_LEFT_PTR); break; @@ -1107,6 +1113,7 @@ wl_pointer_leave(void *data, struct wl_pointer *wl_pointer, case TERM_SURF_NONE: case TERM_SURF_GRID: case TERM_SURF_SEARCH: + case TERM_SURF_SCROLLBACK_INDICATOR: case TERM_SURF_TITLE: case TERM_SURF_BORDER_LEFT: case TERM_SURF_BORDER_RIGHT: @@ -1141,6 +1148,7 @@ wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, switch (term->active_surface) { case TERM_SURF_NONE: case TERM_SURF_SEARCH: + case TERM_SURF_SCROLLBACK_INDICATOR: case TERM_SURF_BUTTON_MINIMIZE: case TERM_SURF_BUTTON_MAXIMIZE: case TERM_SURF_BUTTON_CLOSE: @@ -1165,26 +1173,31 @@ wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, break; case TERM_SURF_GRID: { + term_xcursor_update(term); + int col = (x - term->margins.left) / term->cell_width; int row = (y - term->margins.top) / term->cell_height; - if (col < 0 || row < 0 || col >= term->cols || row >= term->rows) - return; + int old_col = seat->mouse.col; + int old_row = seat->mouse.row; + + seat->mouse.col = col >= 0 && col < term->cols ? col : -1; + seat->mouse.row = row >= 0 && row < term->rows ? row : -1; + + if (seat->mouse.col < 0 || seat->mouse.row < 0) + break; bool update_selection = seat->mouse.button == BTN_LEFT || seat->mouse.button == BTN_RIGHT; bool update_selection_early = term->selection.end.row == -1; if (update_selection && update_selection_early) - selection_update(term, col, row); + selection_update(term, seat->mouse.col, seat->mouse.row); - if (col == seat->mouse.col && row == seat->mouse.row) + if (old_col == seat->mouse.col && old_row == seat->mouse.row) break; - seat->mouse.col = col; - seat->mouse.row = row; - if (update_selection && !update_selection_early) - selection_update(term, col, row); + selection_update(term, seat->mouse.col, seat->mouse.row); if (!term_mouse_grabbed(term, seat)) { term_mouse_motion( @@ -1347,6 +1360,7 @@ wl_pointer_button(void *data, struct wl_pointer *wl_pointer, break; case TERM_SURF_SEARCH: + case TERM_SURF_SCROLLBACK_INDICATOR: break; case TERM_SURF_GRID: { @@ -1360,9 +1374,11 @@ wl_pointer_button(void *data, struct wl_pointer *wl_pointer, if (selection_enabled(term, seat)) { switch (seat->mouse.count) { case 1: - selection_start( - term, seat->mouse.col, seat->mouse.row, - seat->kbd.ctrl ? SELECTION_BLOCK : SELECTION_NORMAL); + if (seat->mouse.col >= 0 && seat->mouse.row >= 0) { + selection_start( + term, seat->mouse.col, seat->mouse.row, + seat->kbd.ctrl ? SELECTION_BLOCK : SELECTION_NORMAL); + } break; case 2: diff --git a/render.c b/render.c index 80611d22..2f95e5b3 100644 --- a/render.c +++ b/render.c @@ -1287,6 +1287,170 @@ render_csd(struct terminal *term) render_csd_title(term); } +static void +render_scrollback_position(struct terminal *term) +{ + if (term->conf->scrollback.indicator.style == SCROLLBACK_INDICATOR_STYLE_NONE) + return; + + struct wayland *wayl = term->wl; + struct wl_window *win = term->window; + + if (term->grid->view == term->grid->offset) { + if (win->scrollback_indicator_surface != NULL) { + wl_subsurface_destroy(win->scrollback_indicator_sub_surface); + wl_surface_destroy(win->scrollback_indicator_surface); + + win->scrollback_indicator_surface = NULL; + win->scrollback_indicator_sub_surface = NULL; + } + return; + } + + if (win->scrollback_indicator_surface == NULL) { + win->scrollback_indicator_surface + = wl_compositor_create_surface(wayl->compositor); + + if (win->scrollback_indicator_surface == NULL) { + LOG_ERR("failed to create scrollback indicator surface"); + return; + } + + wl_surface_set_user_data(win->scrollback_indicator_surface, win); + + term->window->scrollback_indicator_sub_surface + = wl_subcompositor_get_subsurface( + wayl->sub_compositor, + win->scrollback_indicator_surface, + win->surface); + + if (win->scrollback_indicator_sub_surface == NULL) { + LOG_ERR("failed to create scrollback indicator sub-surface"); + wl_surface_destroy(win->scrollback_indicator_surface); + win->scrollback_indicator_surface = NULL; + return; + } + + wl_subsurface_set_sync(win->scrollback_indicator_sub_surface); + } + + assert(win->scrollback_indicator_surface != NULL); + assert(win->scrollback_indicator_sub_surface != NULL); + + /* Find absolute row number of the scrollback start */ + int scrollback_start = term->grid->offset + term->rows; + while (term->grid->rows[scrollback_start & (term->grid->num_rows - 1)] == NULL) + scrollback_start++; + + /* Rebase viewport against scrollback start (so that 0 is at + * the beginning of the scrollback) */ + int rebased_view = term->grid->view - scrollback_start + term->grid->num_rows; + rebased_view &= term->grid->num_rows - 1; + + /* + * How far down in the scrollback we are. + * + * 0% -> at the beginning of the scrollback + * 100% -> at the bottom, i.e. where new lines are inserted + */ + double percent = + rebased_view + term->rows == term->grid->num_rows + ? 1.0 + : (double)rebased_view / term->grid->num_rows; + + wchar_t text[64]; + int cell_count; + + /* *What* to render */ + switch (term->conf->scrollback.indicator.format) { + case SCROLLBACK_INDICATOR_FORMAT_PERCENTAGE: + swprintf(text, sizeof(text) / sizeof(text[0]), L"%u%%", (int)(100 * percent)); + cell_count = 3; + break; + + case SCROLLBACK_INDICATOR_FORMAT_LINENO: + swprintf(text, sizeof(text) / sizeof(text[0]), L"%d", rebased_view + 1); + cell_count = 1 + (int)log10(term->grid->num_rows); + break; + } + + const int scale = term->scale; + const int margin = 3 * scale; + const int width = 2 * margin + cell_count * term->cell_width; + const int height = 2 * margin + term->cell_height; + + unsigned long cookie = shm_cookie_scrollback_indicator(term); + struct buffer *buf = shm_get_buffer( + term->wl->shm, width, height, cookie, false, 1); + + pixman_color_t bg = color_hex_to_pixman(term->colors.table[8 + 4]); + pixman_image_fill_rectangles( + PIXMAN_OP_SRC, buf->pix[0], &bg, 1, + &(pixman_rectangle16_t){0, 0, width, height}); + + struct fcft_font *font = term->fonts[0]; + pixman_color_t fg = color_hex_to_pixman(term->colors.table[0]); + + /* Sub-surface relative coordinates */ + unsigned x = width - margin - wcslen(text) * term->cell_width; + const unsigned y = margin; + + for (size_t i = 0; i < wcslen(text); i++) { + const struct fcft_glyph *glyph = fcft_glyph_rasterize( + font, text[i], term->font_subpixel); + + if (glyph == NULL) + continue; + + pixman_image_t *src = pixman_image_create_solid_fill(&fg); + pixman_image_composite32( + PIXMAN_OP_OVER, src, glyph->pix, buf->pix[0], 0, 0, 0, 0, + x + glyph->x, y + font_baseline(term) - glyph->y, + glyph->width, glyph->height); + pixman_image_unref(src); + + x += term->cell_width; + } + + /* *Where* to render - parent relative coordinates */ + int surf_top = 0; + switch (term->conf->scrollback.indicator.style) { + case SCROLLBACK_INDICATOR_STYLE_NONE: + assert(false); + return; + + case SCROLLBACK_INDICATOR_STYLE_FIXED: + surf_top = term->cell_height - margin; + break; + + case SCROLLBACK_INDICATOR_STYLE_RELATIVE: { + int lines = term->rows - 3; /* Avoid using first and two last rows */ + assert(lines > 0); + + int pixels = lines * term->cell_height - height + 2 * margin; + surf_top = term->cell_height - margin + (int)(percent * pixels); + break; + } + } + + wl_subsurface_set_position( + win->scrollback_indicator_sub_surface, + (term->width - margin - width) / scale, + (term->margins.top + surf_top) / scale); + wl_surface_attach(win->scrollback_indicator_surface, buf->wl_buf, 0, 0); + wl_surface_damage_buffer(win->scrollback_indicator_surface, 0, 0, width, height); + wl_surface_set_buffer_scale(win->scrollback_indicator_surface, scale); + + struct wl_region *region = wl_compositor_create_region(wayl->compositor); + if (region != NULL) { + wl_region_add(region, 0, 0, width, height); + wl_surface_set_opaque_region(win->scrollback_indicator_surface, region); + wl_region_destroy(region); + } + + wl_surface_commit(win->scrollback_indicator_surface); +} + static void frame_callback( void *data, struct wl_callback *wl_callback, uint32_t callback_data); @@ -1498,6 +1662,8 @@ grid_render(struct terminal *term) term->window->surface, 0, 0, term->width, term->height); } + render_scrollback_position(term); + assert(term->grid->offset >= 0 && term->grid->offset < term->grid->num_rows); assert(term->grid->view >= 0 && term->grid->view < term->grid->num_rows); @@ -1616,7 +1782,7 @@ render_search_box(struct terminal *term) draw_bar(term, buf->pix[0], font, &fg, x, y); const struct fcft_glyph *glyph = fcft_glyph_rasterize( - font, term->search.buf[i], true); + font, term->search.buf[i], term->font_subpixel); if (glyph == NULL) continue; diff --git a/selection.c b/selection.c index 354f6f53..ffe06c27 100644 --- a/selection.c +++ b/selection.c @@ -25,9 +25,10 @@ bool selection_enabled(const struct terminal *term, struct seat *seat) { return - term->mouse_tracking == MOUSE_NONE || - term_mouse_grabbed(term, seat) || - term->is_searching; + seat->mouse.col >= 0 && seat->mouse.row >= 0 && + (term->mouse_tracking == MOUSE_NONE || + term_mouse_grabbed(term, seat) || + term->is_searching); } bool diff --git a/shm.h b/shm.h index 4c8e9506..0bfb3839 100644 --- a/shm.h +++ b/shm.h @@ -49,4 +49,5 @@ void shm_purge(struct wl_shm *shm, unsigned long cookie); struct terminal; static inline unsigned long shm_cookie_grid(const struct terminal *term) { return (unsigned long)((uintptr_t)term + 0); } static inline unsigned long shm_cookie_search(const struct terminal *term) { return (unsigned long)((uintptr_t)term + 1); } -static inline unsigned long shm_cookie_csd(const struct terminal *term, int n) { return (unsigned long)((uintptr_t)term + 2 + (n)); } +static inline unsigned long shm_cookie_scrollback_indicator(const struct terminal *term) { return (unsigned long)(uintptr_t)term + 2; } +static inline unsigned long shm_cookie_csd(const struct terminal *term, int n) { return (unsigned long)((uintptr_t)term + 3 + (n)); } diff --git a/terminal.c b/terminal.c index 86672f06..f1b528fb 100644 --- a/terminal.c +++ b/terminal.c @@ -909,7 +909,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, .tab_stops = tll_init(), .wl = wayl, .render = { - .scrollback_lines = conf->scrollback_lines, + .scrollback_lines = conf->scrollback.lines, .app_sync_updates.timer_fd = app_sync_updates_fd, .workers = { .count = conf->render_worker_count, @@ -2407,6 +2407,8 @@ term_surface_kind(const struct terminal *term, const struct wl_surface *surface) return TERM_SURF_GRID; else if (surface == term->window->search_surface) return TERM_SURF_SEARCH; + else if (surface == term->window->scrollback_indicator_surface) + return TERM_SURF_SCROLLBACK_INDICATOR; else if (surface == term->window->csd.surface[CSD_SURF_TITLE]) return TERM_SURF_TITLE; else if (surface == term->window->csd.surface[CSD_SURF_LEFT]) diff --git a/terminal.h b/terminal.h index 85f89abf..eae636cc 100644 --- a/terminal.h +++ b/terminal.h @@ -195,6 +195,7 @@ enum term_surface { TERM_SURF_NONE, TERM_SURF_GRID, TERM_SURF_SEARCH, + TERM_SURF_SCROLLBACK_INDICATOR, TERM_SURF_TITLE, TERM_SURF_BORDER_LEFT, TERM_SURF_BORDER_RIGHT, diff --git a/wayland.c b/wayland.c index 70d5e0f1..66f2c3b3 100644 --- a/wayland.c +++ b/wayland.c @@ -1203,6 +1203,11 @@ wayl_win_destroy(struct wl_window *win) * nor mouse focus). */ + if (win->scrollback_indicator_surface != NULL) { + wl_surface_attach(win->scrollback_indicator_surface, NULL, 0, 0); + wl_surface_commit(win->scrollback_indicator_surface); + } + /* Scrollback search */ if (win->search_surface != NULL) { wl_surface_attach(win->search_surface, NULL, 0, 0); @@ -1227,6 +1232,10 @@ wayl_win_destroy(struct wl_window *win) tll_free(win->on_outputs); csd_destroy(win); + if (win->scrollback_indicator_sub_surface != NULL) + wl_subsurface_destroy(win->scrollback_indicator_sub_surface); + if (win->scrollback_indicator_surface != NULL) + wl_surface_destroy(win->scrollback_indicator_surface); if (win->search_sub_surface != NULL) wl_subsurface_destroy(win->search_sub_surface); if (win->search_surface != NULL) diff --git a/wayland.h b/wayland.h index dbf63c1b..e67d514a 100644 --- a/wayland.h +++ b/wayland.h @@ -273,6 +273,9 @@ struct wl_window { struct wl_surface *search_surface; struct wl_subsurface *search_sub_surface; + struct wl_surface *scrollback_indicator_surface; + struct wl_subsurface *scrollback_indicator_sub_surface; + struct wl_callback *frame_callback; tll(const struct monitor *) on_outputs; /* Outputs we're mapped on */