From f0fc82f0987166b3fe4724e50da05ba611231c67 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 21 Feb 2020 23:40:35 +0100 Subject: sixel: wip: maintain a list of finished, and "active" sixel images In unhook, add the generated image to a list of finished sixel images, along with positioning and size information. When rendering, loop this list of images, and render the images (or parts of) that are visible. When scrolling, check if any part of the images cover the re-cycled lines, and if so, remove the *entire* image from the list. This means we have the following limitations: * The renderer always renders the whole (visible area of) the image(s). There are times when this isn't necessary - for example, when the image is scrolled inside the visible area. * It would be nice if we could crop the image when parts of it is scrolled out. --- terminal.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index ba19b552..b80b9898 100644 --- a/terminal.c +++ b/terminal.c @@ -740,6 +740,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, .lower_fd = delay_lower_fd, .upper_fd = delay_upper_fd, }, + .sixel_images = tll_init(), .hold_at_exit = conf->hold_at_exit, .shutdown_cb = shutdown_cb, .shutdown_data = shutdown_data, @@ -993,6 +994,12 @@ term_destroy(struct terminal *term) tll_free(term->ptmx_buffer); tll_free(term->tab_stops); + tll_foreach(term->sixel_images, it) { + pixman_image_unref(it->item.pix); + free(it->item.data); + } + tll_free(term->sixel_images); + free(term->foot_exe); free(term->cwd); @@ -1521,6 +1528,21 @@ term_scroll_partial(struct terminal *term, struct scroll_region region, int rows erase_line(term, grid_row_and_alloc(term->grid, r)); if (selection_on_row_in_view(term, r)) selection_cancel(term); + + + tll_foreach(term->sixel_images, it) { + /* Make it simple - remove the entire image if it starts + * getting scrolled out */ + + int img_top_row = it->item.pos.row & (term->grid->num_rows - 1); + int new_row = (term->grid->offset + r) & (term->grid->num_rows - 1); + + if (img_top_row == new_row) { + pixman_image_unref(it->item.pix); + free(it->item.data); + tll_remove(term->sixel_images, it); + } + } } term_damage_scroll(term, DAMAGE_SCROLL, region, rows); @@ -1572,6 +1594,23 @@ term_scroll_reverse_partial(struct terminal *term, erase_line(term, grid_row_and_alloc(term->grid, r)); if (selection_on_row_in_view(term, r)) selection_cancel(term); + + tll_foreach(term->sixel_images, it) { + /* Make it simple - remove the entire image if it starts + * getting scrolled out */ + + /* TODO: untested */ + + int img_rows = (it->item.height + term->cell_height - 1) / term->cell_height; + int img_bottom_row = (it->item.pos.row + img_rows) & (term->grid->num_rows - 1); + int new_row = (term->grid->offset + r) & (term->grid->num_rows - 1); + + if (img_bottom_row == new_row) { + pixman_image_unref(it->item.pix); + free(it->item.data); + tll_remove(term->sixel_images, it); + } + } } term_damage_scroll(term, DAMAGE_SCROLL_REVERSE, region, rows); -- cgit v1.3 From 63140a68f516c8b4616bf6c726d23a75b73e5713 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 00:05:25 +0100 Subject: sixel: calculate image height in (cell) rows --- render.c | 43 ++++++++++++++++++++++--------------------- sixel.c | 1 + terminal.c | 3 +-- terminal.h | 1 + 4 files changed, 25 insertions(+), 23 deletions(-) (limited to 'terminal.c') diff --git a/render.c b/render.c index cd96ba24..a4741b90 100644 --- a/render.c +++ b/render.c @@ -520,30 +520,23 @@ grid_render_scroll_reverse(struct terminal *term, struct buffer *buf, } static void -render_sixel(struct terminal *term, pixman_image_t *pix, const struct sixel *sixel) +render_sixel(struct terminal *term, pixman_image_t *pix, + const struct sixel *sixel) { int view_end = (term->grid->view + term->rows - 1) & (term->grid->num_rows - 1); int first_visible_row = -1; - if (view_end >= term->grid->view) { - /* Not wrapped */ - for (size_t i = sixel->pos.row; - i < sixel->pos.row + (sixel->height + term->cell_height - 1) / term->cell_height; - i++) - { - int row = i & (term->grid->num_rows - 1); + for (size_t i = sixel->pos.row; i < sixel->pos.row + sixel->rows; i++) { + int row = i & (term->grid->num_rows - 1); + + if (view_end >= term->grid->view) { + /* Not wrapped */ if (row >= term->grid->view && row <= view_end) { first_visible_row = i; break; } - } - } else { - /* Wrapped */ - for (size_t i = sixel->pos.row; - i < sixel->pos.row + (sixel->height + term->cell_height - 1) / term->cell_height; - i++) - { - int row = i & (term->grid->num_rows - 1); + } else { + /* Wrapped */ if (row >= term->grid->view || row <= view_end) { first_visible_row = i; break; @@ -554,20 +547,30 @@ render_sixel(struct terminal *term, pixman_image_t *pix, const struct sixel *six if (first_visible_row < 0) return; + /* First visible (0 based) row of the image */ const int first_img_row = first_visible_row - sixel->pos.row; + + /* Map first visible line to current grid view */ const int row = first_visible_row & (term->grid->num_rows - 1); const int view_aligned = (row - term->grid->view + term->grid->num_rows) & (term->grid->num_rows - 1); + /* Translate row/column to x/y pixel values */ const int x = term->x_margin + sixel->pos.col * term->cell_width; - const int y = max(term->y_margin, term->y_margin + view_aligned * term->cell_height); + const int y = max( + term->y_margin, term->y_margin + view_aligned * term->cell_height); + /* Width/height, in pixels - and don't touch the window margins */ const int width = min(sixel->width, term->width - x - term->x_margin); const int height = min( sixel->height - first_img_row * term->cell_height, term->height - y - term->y_margin); - //LOG_INFO("x=%d, y=%d, width=%d, height=%d", x, y, width, height); + /* Verify we're not stepping outside the grid */ + assert(x >= term->x_margin); + assert(y >= term->y_margin); + assert(x + width <= term->width - term->x_margin); + assert(y + height <= term->height - term->y_margin); pixman_image_composite( PIXMAN_OP_SRC, @@ -579,9 +582,7 @@ render_sixel(struct terminal *term, pixman_image_t *pix, const struct sixel *six x, y, width, height); - wl_surface_damage_buffer( - term->window->surface, - x, y, width, height); + wl_surface_damage_buffer(term->window->surface, x, y, width, height); } static void diff --git a/sixel.c b/sixel.c index f9c4c4cf..0cfb2a24 100644 --- a/sixel.c +++ b/sixel.c @@ -56,6 +56,7 @@ sixel_unhook(struct terminal *term) .data = term->sixel.image, .width = term->sixel.max_col, .height = term->sixel.row * 6, + .rows = (term->sixel.row * 6 + term->cell_height - 1) / term->cell_height, .pos = (struct coord){term->cursor.point.col, term->grid->offset + term->cursor.point.row}, }; diff --git a/terminal.c b/terminal.c index b80b9898..c6c349c3 100644 --- a/terminal.c +++ b/terminal.c @@ -1601,8 +1601,7 @@ term_scroll_reverse_partial(struct terminal *term, /* TODO: untested */ - int img_rows = (it->item.height + term->cell_height - 1) / term->cell_height; - int img_bottom_row = (it->item.pos.row + img_rows) & (term->grid->num_rows - 1); + int img_bottom_row = (it->item.pos.row + it->item.rows) & (term->grid->num_rows - 1); int new_row = (term->grid->offset + r) & (term->grid->num_rows - 1); if (img_bottom_row == new_row) { diff --git a/terminal.h b/terminal.h index 69b94624..bf113aa3 100644 --- a/terminal.h +++ b/terminal.h @@ -180,6 +180,7 @@ struct sixel { pixman_image_t *pix; int width; int height; + int rows; struct coord pos; }; -- cgit v1.3 From bb82b9fabc00e1fadca6fe630e91f07dbe1168b3 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 00:23:19 +0100 Subject: sixel: add sixel_destroy() --- sixel.c | 17 +++++++++++++---- sixel.h | 2 ++ terminal.c | 17 +++++++---------- 3 files changed, 22 insertions(+), 14 deletions(-) (limited to 'terminal.c') diff --git a/sixel.c b/sixel.c index 4bb7a28e..fdad65a9 100644 --- a/sixel.c +++ b/sixel.c @@ -37,14 +37,22 @@ sixel_init(struct terminal *term) /* TODO: default palette */ } +void +sixel_destroy(struct sixel *sixel) +{ + pixman_image_unref(sixel->pix); + free(sixel->data); + + sixel->pix = NULL; + sixel->data = NULL; +} + void sixel_unhook(struct terminal *term) { free(term->sixel.palette); term->sixel.palette = NULL; - LOG_DBG("generating %dx%d pixman image", term->sixel.row * 6, term->sixel.max_col); - if (term->sixel.col > term->sixel.max_col) term->sixel.max_col = term->sixel.col; term->sixel.row++; @@ -58,6 +66,8 @@ sixel_unhook(struct terminal *term) .pos = (struct coord){term->cursor.point.col, term->grid->offset + term->cursor.point.row}, }; + LOG_DBG("generating %dx%d pixman image", image.width, image.height); + image.pix = pixman_image_create_bits_no_clear( PIXMAN_a8r8g8b8, image.width, image.height, @@ -66,8 +76,7 @@ sixel_unhook(struct terminal *term) tll_foreach(term->sixel_images, it) { if (it->item.pos.row == image.pos.row) { - pixman_image_unref(it->item.pix); - free(it->item.data); + sixel_destroy(&it->item); tll_remove(term->sixel_images, it); } } diff --git a/sixel.h b/sixel.h index 0cb24bfe..5933cf81 100644 --- a/sixel.h +++ b/sixel.h @@ -5,3 +5,5 @@ void sixel_init(struct terminal *term); void sixel_put(struct terminal *term, uint8_t c); void sixel_unhook(struct terminal *term); + +void sixel_destroy(struct sixel *sixel); diff --git a/terminal.c b/terminal.c index c6c349c3..cb7c08e9 100644 --- a/terminal.c +++ b/terminal.c @@ -19,12 +19,13 @@ #include "log.h" #include "async.h" +#include "config.h" #include "grid.h" #include "render.h" -#include "vt.h" #include "selection.h" -#include "config.h" +#include "sixel.h" #include "slave.h" +#include "vt.h" #define min(x, y) ((x) < (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y)) @@ -994,10 +995,8 @@ term_destroy(struct terminal *term) tll_free(term->ptmx_buffer); tll_free(term->tab_stops); - tll_foreach(term->sixel_images, it) { - pixman_image_unref(it->item.pix); - free(it->item.data); - } + tll_foreach(term->sixel_images, it) + sixel_destroy(&it->item); tll_free(term->sixel_images); free(term->foot_exe); @@ -1538,8 +1537,7 @@ term_scroll_partial(struct terminal *term, struct scroll_region region, int rows int new_row = (term->grid->offset + r) & (term->grid->num_rows - 1); if (img_top_row == new_row) { - pixman_image_unref(it->item.pix); - free(it->item.data); + sixel_destroy(&it->item); tll_remove(term->sixel_images, it); } } @@ -1605,8 +1603,7 @@ term_scroll_reverse_partial(struct terminal *term, int new_row = (term->grid->offset + r) & (term->grid->num_rows - 1); if (img_bottom_row == new_row) { - pixman_image_unref(it->item.pix); - free(it->item.data); + sixel_destroy(&it->item); tll_remove(term->sixel_images, it); } } -- cgit v1.3 From ad5b2030f0094f9232af5170823ef66fdde7a625 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 10:47:16 +0100 Subject: term: free sixel images on reset --- terminal.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index cb7c08e9..3350c065 100644 --- a/terminal.c +++ b/terminal.c @@ -1121,6 +1121,10 @@ term_reset(struct terminal *term, bool hard) term->meta.esc_prefix = true; term->meta.eight_bit = true; + tll_foreach(term->sixel_images, it) + sixel_destroy(&it->item); + tll_free(term->sixel_images); + if (!hard) return; -- cgit v1.3 From 8e37a18083e8cdba1cfd4ee042d8f8edf7cfad39 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 14:02:00 +0100 Subject: sixel: application configurable palette size (color count) This implements the CSI escapes for retrieving and (re)setting the palette size. --- csi.c | 25 +++++++++++++++++++++++++ sixel.c | 41 +++++++++++++++++++++++++++++++++++------ sixel.h | 7 +++++++ terminal.c | 3 +++ terminal.h | 3 +++ 5 files changed, 73 insertions(+), 6 deletions(-) (limited to 'terminal.c') diff --git a/csi.c b/csi.c index ed237592..d5c06905 100644 --- a/csi.c +++ b/csi.c @@ -15,6 +15,7 @@ #include "grid.h" #include "vt.h" #include "selection.h" +#include "sixel.h" #define min(x, y) ((x) < (y) ? (x) : (y)) @@ -1227,6 +1228,30 @@ csi_dispatch(struct terminal *term, uint8_t final) } break; + case 'S': { + unsigned target = vt_param_get(term, 0, 0); + unsigned operation = vt_param_get(term, 1, 0); + + switch (target) { + case 1: + switch (operation) { + case 1: sixel_colors_report_current(term); break; + case 2: sixel_colors_reset(term); break; + case 3: sixel_colors_set(term, vt_param_get(term, 2, 0)); break; + case 4: sixel_colors_report_max(term); + } + break; + + case 2: + break; + + case 3: + break; + + } + break; + } + default: UNHANDLED(); break; diff --git a/sixel.c b/sixel.c index 69025669..2536f467 100644 --- a/sixel.c +++ b/sixel.c @@ -11,7 +11,6 @@ #define max(x, y) ((x) > (y) ? (x) : (y)) #define min(x, y) ((x) < (y) ? (x) : (y)) -static const size_t COLOR_COUNT = 1024; static size_t count; @@ -20,6 +19,7 @@ sixel_init(struct terminal *term) { assert(term->sixel.palette == NULL); assert(term->sixel.image.data == NULL); + assert(term->sixel.palette_size <= SIXEL_MAX_COLORS); term->sixel.state = SIXEL_DECSIXEL; term->sixel.pos = (struct coord){0, 0}; @@ -28,7 +28,7 @@ sixel_init(struct terminal *term) term->sixel.param = 0; term->sixel.param_idx = 0; memset(term->sixel.params, 0, sizeof(term->sixel.params)); - term->sixel.palette = calloc(COLOR_COUNT, sizeof(term->sixel.palette[0])); + term->sixel.palette = calloc(term->sixel.palette_size, sizeof(term->sixel.palette[0])); term->sixel.image.data = malloc(1 * 6 * sizeof(term->sixel.image.data[0])); term->sixel.image.width = 1; term->sixel.image.height = 6; @@ -309,10 +309,8 @@ decgci(struct terminal *term, uint8_t c) int nparams = term->sixel.param_idx; - if (nparams > 0) { - /* Add one, as we use idx==0 for background color (TODO) */ - term->sixel.color_idx = min(1 + term->sixel.params[0], COLOR_COUNT - 1); - } + if (nparams > 0) + term->sixel.color_idx = min(term->sixel.params[0], term->sixel.palette_size - 1); if (nparams > 4) { unsigned format = term->sixel.params[1]; @@ -360,3 +358,34 @@ sixel_put(struct terminal *term, uint8_t c) count++; } + +void +sixel_colors_report_current(struct terminal *term) +{ + char reply[24]; + snprintf(reply, sizeof(reply), "\033[?1;0;%uS", term->sixel.palette_size); + term_to_slave(term, reply, strlen(reply)); +} + +void +sixel_colors_reset(struct terminal *term) +{ + term->sixel.palette_size = SIXEL_MAX_COLORS; + LOG_DBG("sixel palette size reset to %u", SIXEL_MAX_COLORS); +} + +void +sixel_colors_set(struct terminal *term, unsigned count) +{ + unsigned new_palette_size = min(max(2, count), SIXEL_MAX_COLORS); + term->sixel.palette_size = new_palette_size; + LOG_DBG("sixel palette size set to %u", new_palette_size); +} + +void +sixel_colors_report_max(struct terminal *term) +{ + char reply[24]; + snprintf(reply, sizeof(reply), "\033[?1;0;%uS", SIXEL_MAX_COLORS); + term_to_slave(term, reply, strlen(reply)); +} diff --git a/sixel.h b/sixel.h index 5933cf81..8e6d2949 100644 --- a/sixel.h +++ b/sixel.h @@ -2,8 +2,15 @@ #include "terminal.h" +#define SIXEL_MAX_COLORS 1024u + void sixel_init(struct terminal *term); void sixel_put(struct terminal *term, uint8_t c); void sixel_unhook(struct terminal *term); void sixel_destroy(struct sixel *sixel); + +void sixel_colors_report_current(struct terminal *term); +void sixel_colors_reset(struct terminal *term); +void sixel_colors_set(struct terminal *term, unsigned count); +void sixel_colors_report_max(struct terminal *term); diff --git a/terminal.c b/terminal.c index 3350c065..493a8ada 100644 --- a/terminal.c +++ b/terminal.c @@ -741,6 +741,9 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, .lower_fd = delay_lower_fd, .upper_fd = delay_upper_fd, }, + .sixel = { + .palette_size = SIXEL_MAX_COLORS, + }, .sixel_images = tll_init(), .hold_at_exit = conf->hold_at_exit, .shutdown_cb = shutdown_cb, diff --git a/terminal.h b/terminal.h index 882abfba..e0738057 100644 --- a/terminal.h +++ b/terminal.h @@ -376,6 +376,9 @@ struct terminal { unsigned params[5]; /* Collected parmaeters, for RASTER, COLOR_SPEC */ unsigned param; /* Currently collecting parameter, for RASTER, COLOR_SPEC and REPEAT */ unsigned param_idx; /* Parameters seen */ + + /* Application configurable */ + unsigned palette_size; } sixel; tll(struct sixel) sixel_images; -- cgit v1.3 From 80361ca04e12b65396d27fa1d981f6739861e453 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 21:35:45 +0100 Subject: sixel: purge images at current cursor row When printing a character, or starting a new sixel image, purge all images that cover the cursor's current row. --- sixel.c | 25 ++++++++++++++++++------- sixel.h | 1 + terminal.c | 2 ++ 3 files changed, 21 insertions(+), 7 deletions(-) (limited to 'terminal.c') diff --git a/sixel.c b/sixel.c index 7d31dea4..db87844e 100644 --- a/sixel.c +++ b/sixel.c @@ -39,6 +39,8 @@ sixel_init(struct terminal *term) count = 0; + sixel_purge_at_cursor(term); + /* TODO: default palette */ } @@ -52,6 +54,22 @@ sixel_destroy(struct sixel *sixel) sixel->data = NULL; } +void +sixel_purge_at_cursor(struct terminal *term) +{ + const int row = term->grid->offset + term->cursor.point.row; + + tll_foreach(term->sixel_images, it) { + const int start = it->item.pos.row; + const int end = start + it->item.rows; + + if (row >= start && row < end) { + sixel_destroy(&it->item); + tll_remove(term->sixel_images, it); + } + } +} + void sixel_unhook(struct terminal *term) { @@ -74,13 +92,6 @@ sixel_unhook(struct terminal *term) term->sixel.image.data, term->sixel.image.width * sizeof(uint32_t)); - tll_foreach(term->sixel_images, it) { - if (it->item.pos.row == image.pos.row) { - sixel_destroy(&it->item); - tll_remove(term->sixel_images, it); - } - } - tll_push_back(term->sixel_images, image); term->sixel.image.data = NULL; diff --git a/sixel.h b/sixel.h index dbce21c5..c5c489af 100644 --- a/sixel.h +++ b/sixel.h @@ -9,6 +9,7 @@ void sixel_put(struct terminal *term, uint8_t c); void sixel_unhook(struct terminal *term); void sixel_destroy(struct sixel *sixel); +void sixel_purge_at_cursor(struct terminal *term); void sixel_colors_report_current(struct terminal *term); void sixel_colors_reset(struct terminal *term); diff --git a/terminal.c b/terminal.c index 493a8ada..5c7b94e5 100644 --- a/terminal.c +++ b/terminal.c @@ -2128,6 +2128,8 @@ term_print(struct terminal *term, wchar_t wc, int width) print_linewrap(term); print_insert(term, width); + sixel_purge_at_cursor(term); + /* *Must* get current cell *after* linewrap+insert */ struct row *row = term->grid->cur_row; struct cell *cell = &row->cells[term->cursor.point.col]; -- cgit v1.3