Merge branch 'scrollback-position-indicator'

Closes #42
This commit is contained in:
Daniel Eklöf 2020-07-27 19:08:49 +02:00
commit 1d3498989b
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
13 changed files with 302 additions and 24 deletions

View file

@ -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

View file

@ -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,

View file

@ -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;

View file

@ -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

2
footrc
View file

@ -2,6 +2,8 @@
# font=monospace
# scrollback=1000
# scrollback-indicator-style=relative
# scrollback-indicator-format=percentage
# geometry=700x500
# pad=2x2
# initial-window-mode=windowed

44
input.c
View file

@ -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:

168
render.c
View file

@ -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;

View file

@ -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

3
shm.h
View file

@ -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)); }

View file

@ -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])

View file

@ -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,

View file

@ -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)

View file

@ -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 */