Merge pull request 'Don't copy default colors from config to each terminal instance' (#438) from refactor-colors into master

Reviewed-on: https://codeberg.org/dnkl/foot/pulls/438
This commit is contained in:
dnkl 2021-04-07 09:31:59 +02:00
commit b119a8a24a
6 changed files with 61 additions and 86 deletions

View file

@ -857,22 +857,22 @@ parse_section_colors(const char *key, const char *value, struct config *conf,
if (strcmp(key, "foreground") == 0) color = &conf->colors.fg; if (strcmp(key, "foreground") == 0) color = &conf->colors.fg;
else if (strcmp(key, "background") == 0) color = &conf->colors.bg; else if (strcmp(key, "background") == 0) color = &conf->colors.bg;
else if (strcmp(key, "regular0") == 0) color = &conf->colors.regular[0]; else if (strcmp(key, "regular0") == 0) color = &conf->colors.table[0];
else if (strcmp(key, "regular1") == 0) color = &conf->colors.regular[1]; else if (strcmp(key, "regular1") == 0) color = &conf->colors.table[1];
else if (strcmp(key, "regular2") == 0) color = &conf->colors.regular[2]; else if (strcmp(key, "regular2") == 0) color = &conf->colors.table[2];
else if (strcmp(key, "regular3") == 0) color = &conf->colors.regular[3]; else if (strcmp(key, "regular3") == 0) color = &conf->colors.table[3];
else if (strcmp(key, "regular4") == 0) color = &conf->colors.regular[4]; else if (strcmp(key, "regular4") == 0) color = &conf->colors.table[4];
else if (strcmp(key, "regular5") == 0) color = &conf->colors.regular[5]; else if (strcmp(key, "regular5") == 0) color = &conf->colors.table[5];
else if (strcmp(key, "regular6") == 0) color = &conf->colors.regular[6]; else if (strcmp(key, "regular6") == 0) color = &conf->colors.table[6];
else if (strcmp(key, "regular7") == 0) color = &conf->colors.regular[7]; else if (strcmp(key, "regular7") == 0) color = &conf->colors.table[7];
else if (strcmp(key, "bright0") == 0) color = &conf->colors.bright[0]; else if (strcmp(key, "bright0") == 0) color = &conf->colors.table[8 + 0];
else if (strcmp(key, "bright1") == 0) color = &conf->colors.bright[1]; else if (strcmp(key, "bright1") == 0) color = &conf->colors.table[8 + 1];
else if (strcmp(key, "bright2") == 0) color = &conf->colors.bright[2]; else if (strcmp(key, "bright2") == 0) color = &conf->colors.table[8 + 2];
else if (strcmp(key, "bright3") == 0) color = &conf->colors.bright[3]; else if (strcmp(key, "bright3") == 0) color = &conf->colors.table[8 + 3];
else if (strcmp(key, "bright4") == 0) color = &conf->colors.bright[4]; else if (strcmp(key, "bright4") == 0) color = &conf->colors.table[8 + 4];
else if (strcmp(key, "bright5") == 0) color = &conf->colors.bright[5]; else if (strcmp(key, "bright5") == 0) color = &conf->colors.table[8 + 5];
else if (strcmp(key, "bright6") == 0) color = &conf->colors.bright[6]; else if (strcmp(key, "bright6") == 0) color = &conf->colors.table[8 + 6];
else if (strcmp(key, "bright7") == 0) color = &conf->colors.bright[7]; else if (strcmp(key, "bright7") == 0) color = &conf->colors.table[8 + 7];
else if (strcmp(key, "selection-foreground") == 0) color = &conf->colors.selection_fg; else if (strcmp(key, "selection-foreground") == 0) color = &conf->colors.selection_fg;
else if (strcmp(key, "selection-background") == 0) color = &conf->colors.selection_bg; else if (strcmp(key, "selection-background") == 0) color = &conf->colors.selection_bg;
@ -2211,7 +2211,7 @@ config_load(struct config *conf, const char *conf_path,
.colors = { .colors = {
.fg = default_foreground, .fg = default_foreground,
.bg = default_background, .bg = default_background,
.regular = { .table = {
default_regular[0], default_regular[0],
default_regular[1], default_regular[1],
default_regular[2], default_regular[2],
@ -2220,8 +2220,7 @@ config_load(struct config *conf, const char *conf_path,
default_regular[5], default_regular[5],
default_regular[6], default_regular[6],
default_regular[7], default_regular[7],
},
.bright = {
default_bright[0], default_bright[0],
default_bright[1], default_bright[1],
default_bright[2], default_bright[2],
@ -2287,6 +2286,26 @@ config_load(struct config *conf, const char *conf_path,
.notifications = tll_init(), .notifications = tll_init(),
}; };
/* Initialize the color cube */
{
/* First 16 entries correspond to the "regular" and "bright"
* colors, and have already been initialized */
/* Then follows 216 RGB shades */
for (size_t r = 0; r < 6; r++) {
for (size_t g = 0; g < 6; g++) {
for (size_t b = 0; b < 6; b++) {
conf->colors.table[16 + r * 6 * 6 + g * 6 + b]
= r * 51 << 16 | g * 51 << 8 | b * 51;
}
}
}
/* And finally 24 shades of gray */
for (size_t i = 0; i < 24; i++)
conf->colors.table[232 + i] = i * 11 << 16 | i * 11 << 8 | i * 11;
}
conf->notify.raw_cmd = xstrdup( conf->notify.raw_cmd = xstrdup(
"notify-send -a foot -i foot ${title} ${body}"); "notify-send -a foot -i foot ${title} ${body}");
tokenize_cmdline(conf->notify.raw_cmd, &conf->notify.argv); tokenize_cmdline(conf->notify.raw_cmd, &conf->notify.argv);

View file

@ -123,8 +123,7 @@ struct config {
struct { struct {
uint32_t fg; uint32_t fg;
uint32_t bg; uint32_t bg;
uint32_t regular[8]; uint32_t table[256];
uint32_t bright[8];
uint16_t alpha; uint16_t alpha;
uint32_t selection_fg; uint32_t selection_fg;
uint32_t selection_bg; uint32_t selection_bg;

16
osc.c
View file

@ -705,8 +705,8 @@ osc_dispatch(struct terminal *term)
LOG_DBG("resetting all colors"); LOG_DBG("resetting all colors");
for (size_t i = 0; i < ALEN(term->colors.table); i++) { for (size_t i = 0; i < ALEN(term->colors.table); i++) {
update_color_in_grids( update_color_in_grids(
term, term->colors.table[i], term->colors.default_table[i]); term, term->colors.table[i], term->conf->colors.table[i]);
term->colors.table[i] = term->colors.default_table[i]; term->colors.table[i] = term->conf->colors.table[i];
} }
} }
@ -729,8 +729,8 @@ osc_dispatch(struct terminal *term)
LOG_DBG("resetting color #%u", idx); LOG_DBG("resetting color #%u", idx);
update_color_in_grids( update_color_in_grids(
term, term->colors.table[idx], term->colors.default_table[idx]); term, term->colors.table[idx], term->conf->colors.table[idx]);
term->colors.table[idx] = term->colors.default_table[idx]; term->colors.table[idx] = term->conf->colors.table[idx];
} }
} }
@ -741,14 +741,14 @@ osc_dispatch(struct terminal *term)
break; break;
case 110: /* Reset default text foreground color */ case 110: /* Reset default text foreground color */
LOG_DBG("resetting foreground"); LOG_DBG("resetting foreground color");
term->colors.fg = term->colors.default_fg; term->colors.fg = term->conf->colors.fg;
term_damage_view(term); term_damage_view(term);
break; break;
case 111: /* Reset default text background color */ case 111: /* Reset default text background color */
LOG_DBG("resetting background"); LOG_DBG("resetting background color");
term->colors.bg = term->colors.default_bg; term->colors.bg = term->conf->colors.bg;
term_damage_view(term); term_damage_view(term);
term_damage_margins(term); term_damage_margins(term);
break; break;

View file

@ -1450,7 +1450,7 @@ render_csd_title(struct terminal *term)
struct buffer *buf = shm_get_buffer( struct buffer *buf = shm_get_buffer(
term->wl->shm, info.width, info.height, cookie, false, 1); term->wl->shm, info.width, info.height, cookie, false, 1);
uint32_t _color = term->colors.default_fg; uint32_t _color = term->conf->colors.fg;
uint16_t alpha = 0xffff; uint16_t alpha = 0xffff;
if (term->conf->csd.color.title_set) { if (term->conf->csd.color.title_set) {
@ -1493,7 +1493,7 @@ render_csd_border(struct terminal *term, enum csd_surface surf_idx)
static void static void
render_csd_button_minimize(struct terminal *term, struct buffer *buf) render_csd_button_minimize(struct terminal *term, struct buffer *buf)
{ {
pixman_color_t color = color_hex_to_pixman(term->colors.default_bg); pixman_color_t color = color_hex_to_pixman(term->conf->colors.bg);
pixman_image_t *src = pixman_image_create_solid_fill(&color); pixman_image_t *src = pixman_image_create_solid_fill(&color);
const int max_height = buf->height / 2; const int max_height = buf->height / 2;
@ -1538,7 +1538,7 @@ static void
render_csd_button_maximize_maximized( render_csd_button_maximize_maximized(
struct terminal *term, struct buffer *buf) struct terminal *term, struct buffer *buf)
{ {
pixman_color_t color = color_hex_to_pixman(term->colors.default_bg); pixman_color_t color = color_hex_to_pixman(term->conf->colors.bg);
pixman_image_t *src = pixman_image_create_solid_fill(&color); pixman_image_t *src = pixman_image_create_solid_fill(&color);
const int max_height = buf->height / 3; const int max_height = buf->height / 3;
@ -1566,7 +1566,7 @@ static void
render_csd_button_maximize_window( render_csd_button_maximize_window(
struct terminal *term, struct buffer *buf) struct terminal *term, struct buffer *buf)
{ {
pixman_color_t color = color_hex_to_pixman(term->colors.default_bg); pixman_color_t color = color_hex_to_pixman(term->conf->colors.bg);
pixman_image_t *src = pixman_image_create_solid_fill(&color); pixman_image_t *src = pixman_image_create_solid_fill(&color);
const int max_height = buf->height / 2; const int max_height = buf->height / 2;
@ -1620,7 +1620,7 @@ render_csd_button_maximize(struct terminal *term, struct buffer *buf)
static void static void
render_csd_button_close(struct terminal *term, struct buffer *buf) render_csd_button_close(struct terminal *term, struct buffer *buf)
{ {
pixman_color_t color = color_hex_to_pixman(term->colors.default_bg); pixman_color_t color = color_hex_to_pixman(term->conf->colors.bg);
pixman_image_t *src = pixman_image_create_solid_fill(&color); pixman_image_t *src = pixman_image_create_solid_fill(&color);
const int max_height = buf->height / 3; const int max_height = buf->height / 3;
@ -1665,21 +1665,21 @@ render_csd_button(struct terminal *term, enum csd_surface surf_idx)
switch (surf_idx) { switch (surf_idx) {
case CSD_SURF_MINIMIZE: case CSD_SURF_MINIMIZE:
_color = term->colors.default_table[4]; /* blue */ _color = term->conf->colors.table[4]; /* blue */
is_set = term->conf->csd.color.minimize_set; is_set = term->conf->csd.color.minimize_set;
conf_color = &term->conf->csd.color.minimize; conf_color = &term->conf->csd.color.minimize;
is_active = term->active_surface == TERM_SURF_BUTTON_MINIMIZE; is_active = term->active_surface == TERM_SURF_BUTTON_MINIMIZE;
break; break;
case CSD_SURF_MAXIMIZE: case CSD_SURF_MAXIMIZE:
_color = term->colors.default_table[2]; /* green */ _color = term->conf->colors.table[2]; /* green */
is_set = term->conf->csd.color.maximize_set; is_set = term->conf->csd.color.maximize_set;
conf_color = &term->conf->csd.color.maximize; conf_color = &term->conf->csd.color.maximize;
is_active = term->active_surface == TERM_SURF_BUTTON_MAXIMIZE; is_active = term->active_surface == TERM_SURF_BUTTON_MAXIMIZE;
break; break;
case CSD_SURF_CLOSE: case CSD_SURF_CLOSE:
_color = term->colors.default_table[1]; /* red */ _color = term->conf->colors.table[1]; /* red */
is_set = term->conf->csd.color.close_set; is_set = term->conf->csd.color.close_set;
conf_color = &term->conf->csd.color.close; conf_color = &term->conf->csd.color.close;
is_active = term->active_surface == TERM_SURF_BUTTON_CLOSE; is_active = term->active_surface == TERM_SURF_BUTTON_CLOSE;

View file

@ -544,25 +544,6 @@ fdm_app_sync_updates_timeout(
return true; return true;
} }
static void
initialize_color_cube(struct terminal *term)
{
/* 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));
}
static bool static bool
initialize_render_workers(struct terminal *term) initialize_render_workers(struct terminal *term)
{ {
@ -1100,27 +1081,6 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper,
.colors = { .colors = {
.fg = conf->colors.fg, .fg = conf->colors.fg,
.bg = conf->colors.bg, .bg = conf->colors.bg,
.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, .alpha = conf->colors.alpha,
}, },
.origin = ORIGIN_ABSOLUTE, .origin = ORIGIN_ABSOLUTE,
@ -1215,7 +1175,8 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper,
term->scale = it->item.scale; term->scale = it->item.scale;
} }
initialize_color_cube(term); memcpy(term->colors.table, term->conf->colors.table,
sizeof(term->colors.table));
/* Initialize the Wayland window backend */ /* Initialize the Wayland window backend */
if ((term->window = wayl_win_init(term)) == NULL) if ((term->window = wayl_win_init(term)) == NULL)
@ -1714,10 +1675,10 @@ term_reset(struct terminal *term, bool hard)
term->flash.active = false; term->flash.active = false;
term->blink.state = BLINK_ON; term->blink.state = BLINK_ON;
fdm_del(term->fdm, term->blink.fd); term->blink.fd = -1; fdm_del(term->fdm, term->blink.fd); term->blink.fd = -1;
term->colors.fg = term->colors.default_fg; term->colors.fg = term->conf->colors.fg;
term->colors.bg = term->colors.default_bg; term->colors.bg = term->conf->colors.bg;
for (size_t i = 0; i < 256; i++) memcpy(term->colors.table, term->conf->colors.table,
term->colors.table[i] = term->colors.default_table[i]; sizeof(term->colors.table));
term->origin = ORIGIN_ABSOLUTE; term->origin = ORIGIN_ABSOLUTE;
term->normal.cursor.lcf = false; term->normal.cursor.lcf = false;
term->alt.cursor.lcf = false; term->alt.cursor.lcf = false;

View file

@ -407,10 +407,6 @@ struct terminal {
uint32_t bg; uint32_t bg;
uint32_t table[256]; uint32_t table[256];
uint16_t alpha; uint16_t alpha;
uint32_t default_fg;
uint32_t default_bg;
uint32_t default_table[256];
} colors; } colors;
enum cursor_style cursor_style; enum cursor_style cursor_style;