Skip to content
Myr

wayland: wip: reload font at correct DPI when necessary

Before adoption: this commit came in with the history the project adopted, so no landing records it.

If we aren't able to correctly guess which output we actually end up on, the DPI we used when rendering the first frame may be incorrect. When this is the case, reload the font at the correct DPI, re-calculate maximum number of matches and refresh. This needs to be cleaned up.

authored by Daniel Eklöf · committed by Daniel Eklöf · parent c7946c2 · Browse files · 5 files+174 added−77 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+174 added−77 removed

  • other:+174 added−77 removed

5 files+174 added−77 removed

modified:main.c+75 added-33 removed
183 unmodified lines
184 printf("\n");185 printf("Colors must be specified as a 32-bit hexadecimal RGBA quadruple.\n");186}187188struct output_change_context {189 const char *font_name;190 struct application_list *apps;191 struct matches *matches;192 struct prompt *prompt;193 struct render *render;194 const struct render_options *render_options;195 const icon_theme_list_t *themes;196};197198#if 0199static void200foobar(struct wayland *wayl, void *data)201{202 struct output_change_context *ctx = data;203204 const char *font_name = ctx->font_name;205 struct render *render = ctx->render;206 struct matches *matches = ctx->matches;207 struct prompt *prompt = ctx->prompt;208 struct application_list *apps = ctx->apps;209 const icon_theme_list_t *themes = ctx->themes;210 const struct render_options *render_options = ctx->render_options;211212 char font_attrs[128];213 snprintf(font_attrs, sizeof(font_attrs), "dpi=%d", wayl_ppi(wayl));214215 struct fcft_font *font = fcft_from_name(216 1, &(const char *){font_name}, font_attrs);217218 if (font == NULL)219 return;220221 LOG_DBG(222 "font: height: %d, ascent: %d, descent: %d",223 font->height, font->ascent, font->descent);224225 /* Calculate number of entries we can show, based on the total226 * height and the fonts line height */227 const double line_height228 = 2 * render_options->y_margin + font->height;229 const size_t max_matches =230 (render_options->height - 2 * render_options->border_size - line_height)231 / line_height;232233 LOG_DBG("max matches: %zu", max_matches);234235 matches_max_matches_set(matches, max_matches);236 icon_reload_application_icons(*themes, font->height, apps);237 render_set_font(render, font);238239 matches_update(matches, prompt);240}241#endif242int243main(int argc, char *const *argv)244{245 static const struct option longopts[] = {
163 unmodified lines
409 struct prompt *prompt = NULL;410 struct matches *matches = NULL;411 struct render *render = NULL;412 struct wayland *wayl = NULL;359 struct fcft_font *font = NULL;413414 icon_theme_list_t themes = icon_load_theme(icon_theme);415 if (tll_length(themes) > 0)416 LOG_INFO("theme: %s", tll_front(themes).path);
2 unmodified lines
419420 if ((fdm = fdm_init()) == NULL)421 goto out;422370 if ((wayl = wayl_init(371 fdm, render_options.width, render_options.height,372 output_name)) == NULL)373 goto out;423375 char font_attrs[128];376 snprintf(font_attrs, sizeof(font_attrs), "dpi=%d", wayl_ppi(wayl));424 setlocale(LC_CTYPE, "");425378 if ((font = fcft_from_name(1, &(const char *){font_name}, font_attrs)) == NULL)379 goto out;380381 LOG_DBG(382 "font: height: %d, ascent: %d, descent: %d",383 font->height, font->ascent, font->descent);384385 /* Calculate number of entries we can show, based on the total386 * height and the fonts line height */387 const double line_height388 = 2 * render_options.y_margin + font->height;389 const size_t max_matches =390 (render_options.height - 2 * render_options.border_size - line_height)391 / line_height;392393 LOG_DBG("max matches: %zu", max_matches);394395 setlocale(LC_ALL, "");396426 /* Load applications */427 if ((apps = applications_init()) == NULL)428 goto out;429 if (dmenu_mode)
1 unmodified line
431 else432 xdg_find_programs(terminal, apps);433 read_cache(apps);434406 if ((render = render_init(&render_options,407 wayl_subpixel(wayl))) == NULL)435 if ((render = render_init(&render_options)) == NULL)436 goto out;437438 if ((prompt = prompt_init(L"> ")) == NULL)439 goto out;440441 if ((matches = matches_init(apps)) == NULL)442 goto out;443416 matches_max_matches_set(matches, max_matches);417 icon_reload_application_icons(themes, font->height, apps);418 render_set_font(render, font);444 if ((wayl = wayl_init(445 fdm, render, prompt, matches, &render_options,446 dmenu_mode, output_name, font_name,447 &themes, apps448 )) == NULL)449 goto out;450420 matches_update(matches, prompt);421 wayl_configure(wayl, render, prompt, matches, dmenu_mode);451#if 0452 struct output_change_context ctx = {453 .font_name = font_name,454 .apps = apps,455 .matches = matches,456 .prompt = prompt,457 .render = render,458 .render_options = &render_options,459 .themes = &themes,460 };461462 foobar(wayl, &ctx);463#endif464 wayl_refresh(wayl);465466 while (true) {467 wayl_flush(wayl);
24 unmodified lines
modified:render.c+7 added-3 removed
335 unmodified lines
336 }337}338339struct render *340render_init(const struct render_options *options,341 enum fcft_subpixel subpixel)340render_init(const struct render_options *options)341{342 struct render *render = calloc(1, sizeof(*render));343 *render = (struct render){344 .options = *options,346 .subpixel = subpixel,345 };346347 /* TODO: the one providing the options should calculate these */348 render->options.pix_background_color = rgba2pixman(render->options.background_color);
4 unmodified lines
353 return render;354}355356void357render_set_subpixel(struct render *render, enum fcft_subpixel subpixel)358{359 render->subpixel = subpixel;360}361362void363render_set_font(struct render *render, struct fcft_font *font)364{365 fcft_destroy(render->font);366 render->font = font;
11 unmodified lines
modified:render.h+2 added-2 removed
32 unmodified lines
33 pixman_color_t pix_selection_color;34};3536struct render;37struct render *render_init(38 const struct render_options *options, enum fcft_subpixel subpixel);37struct render *render_init(const struct render_options *options);38void render_destroy(struct render *render);3940void render_set_subpixel(struct render *render, enum fcft_subpixel subpixel);41void render_set_font(struct render *render, struct fcft_font *font);4243void render_background(const struct render *render, struct buffer *buf);44
7 unmodified lines
modified:wayland.c+77 added-33 removed
24 unmodified lines
2526#define LOG_MODULE "wayland"27#define LOG_ENABLE_DBG 028#include "log.h"29#include "prompt.h"29#include "dmenu.h"31#include "shm.h"30#include "icon.h"31#include "prompt.h"32#include "render.h"33#include "shm.h"3435struct monitor {36 struct wayland *wayl;37 struct wl_output *output;
97 unmodified lines
135 struct render *render;136 struct prompt *prompt;137 struct matches *matches;138139 const struct render_options *render_options;140 char *font_name;141 const icon_theme_list_t *themes;142 struct application_list *apps;143144 int width;145 int height;146 int scale;147 unsigned dpi;148149 enum { KEEP_RUNNING, EXIT_UPDATE_CACHE, EXIT} status;150 int exit_code;151 bool dmenu_mode;
14 unmodified lines
166 tll(struct monitor) monitors;167 const struct monitor *monitor;168169 tll(struct seat) seats;163170};171172bool173repeat_start(struct repeat *repeat, uint32_t key)
631 unmodified lines
805806 return 1;807}808809static bool810reload_font(struct wayland *wayl, unsigned new_dpi)811{812 LOG_INFO("RELOADING FONT: %d -> %d", wayl->dpi, new_dpi);813814 if (wayl->dpi == new_dpi)815 return true;816817 char attrs[256];818 snprintf(attrs, sizeof(attrs), "dpi=%u", new_dpi);819820 wayl->dpi = new_dpi;821822 struct fcft_font *font = fcft_from_name(823 1, (const char *[]){wayl->font_name}, attrs);824825 if (font == NULL)826 return false;827828 const double line_height829 = 2 * wayl->render_options->y_margin + font->height;830 const size_t max_matches =831 (wayl->render_options->height - 2 * wayl->render_options->border_size - line_height)832 / line_height;833834 matches_max_matches_set(wayl->matches, max_matches);835 icon_reload_application_icons(*wayl->themes, font->height, wayl->apps);836 render_set_font(wayl->render, font);837 matches_update(wayl->matches, wayl->prompt);838 return true;839}840841static void842update_size(struct wayland *wayl)843{844 const struct monitor *mon = wayl->monitor;845 const int scale = mon != NULL ? mon->scale : guess_scale(wayl);846 const int dpi = mon != NULL ? mon->ppi.scaled.y * mon->scale : wayl_ppi(wayl);847809 if (scale == wayl->scale)848 if (scale == wayl->scale && dpi == wayl->dpi)849 return;850851 reload_font(wayl, dpi);852853 wayl->scale = scale;854 wayl->dpi = dpi;855856 wayl->width /= scale; wayl->width *= scale;857 wayl->height /= scale; wayl->height *= scale;858
62 unmodified lines
921 mon->scale = factor;922923 if (mon->wayl->monitor == mon) {924 int old_scale = mon->wayl->scale;925 int old_dpi = mon->wayl->dpi;926927 update_size(mon->wayl);928885 if (mon->wayl->scale != old_scale)929 if (mon->wayl->scale != old_scale || mon->wayl->dpi != old_dpi)930 wayl_refresh(mon->wayl);931 }932}933
393 unmodified lines
1327 if (it->item.output != wl_output)1328 continue;13291330 int old_scale = wayl->scale;1331 int old_dpi = wayl->dpi;13321333 wayl->monitor = &it->item;1334 update_size(wayl);13351291 if (wayl->scale != old_scale)1336 if (wayl->scale != old_scale || wayl->dpi != old_dpi)1337 wayl_refresh(wayl);1338 break;1339 }1340}
11 unmodified lines
1352 .leave = &surface_leave,1353};13541355struct wayland *1311wayl_init(struct fdm *fdm, int width, int height, const char *output_name)1356wayl_init(struct fdm *fdm,1357 struct render *render, struct prompt *prompt, struct matches *matches,1358 const struct render_options *render_options, bool dmenu_mode,1359 const char *output_name, const char *font_name,13601361 const icon_theme_list_t *themes, struct application_list *apps)1362{1313 struct wayland *wayl = calloc(1, sizeof(*wayl));1363 struct wayland *wayl = malloc(sizeof(*wayl));1364 *wayl = (struct wayland){1365 .fdm = fdm,1366 .render = render,1367 .prompt = prompt,1368 .matches = matches,1369 .status = KEEP_RUNNING,1370 .exit_code = EXIT_FAILURE,1371 .dmenu_mode = dmenu_mode,1372 .output_name = output_name != NULL ? strdup(output_name) : NULL,1373 .font_name = strdup(font_name),1374 .render_options = render_options,1375 .themes = themes,1376 .apps = apps,1377 };13781315 wayl->fdm = fdm;1316 //wayl->render = render;1317 //wayl->prompt = prompt;1318 //wayl->matches = matches;1319 wayl->status = KEEP_RUNNING;1320 wayl->exit_code = EXIT_FAILURE;1321 wayl->output_name = output_name != NULL ? strdup(output_name) : NULL;1322 //wayl->dmenu_mode = dmenu_mode;13231379 wayl->display = wl_display_connect(NULL);1380 if (wayl->display == NULL) {1381 LOG_ERR("failed to connect to wayland; no compositor running?");1382 goto out;
79 unmodified lines
14621463 zwlr_layer_surface_v1_add_listener(1464 wayl->layer_surface, &layer_surface_listener, wayl);14651411 wayl->width = width;1412 wayl->height = height;1466 wayl->width = render_options->width;1467 wayl->height = render_options->height;14681469 update_size(wayl);14701471 if (wl_display_prepare_read(wayl->display) != 0) {
13 unmodified lines
1485 return NULL;1486}14871488void1434wayl_configure(1435 struct wayland *wayl, struct render *render, struct prompt *prompt,1436 struct matches *matches, bool dmenu_mode)1437{1438 wayl->render = render;1439 wayl->prompt = prompt;1440 wayl->matches = matches;1441 wayl->status = KEEP_RUNNING;1442 wayl->dmenu_mode = dmenu_mode;1443}14441445void1489wayl_destroy(struct wayland *wayl)1490{1491 if (wayl == NULL)1492 return;
27 unmodified lines
1520 if (wayl->display != NULL)1521 wl_display_disconnect(wayl->display);15221523 free(wayl->output_name);1524 free(wayl->font_name);1525 free(wayl);1526}15271528void
1 unmodified line
1530{1531 wl_display_flush(wayl->display);1532}15331490int1534unsigned1535wayl_ppi(const struct wayland *wayl)1536{1537 /* Use user configured output, if available, otherwise use the1538 * "first" output */
6 unmodified lines
1545 if (mon != NULL)1546 return mon->ppi.scaled.y * mon->scale;15471548 /* No outputs available, return "something" */1505 return 96;1549 return 96u;1550}15511552enum fcft_subpixel1553wayl_subpixel(const struct wayland *wayl)
16 unmodified lines
modified:wayland.h+13 added-6 removed
6 unmodified lines
7#include "prompt.h"8#include "match.h"9#include "render.h"1011/* TODO? */12#include "application.h"13#include "icon.h"1415struct wayland;16struct wayland *wayl_init(13 struct fdm *fdm, int width, int height, const char *output_name);17 struct fdm *fdm,18 struct render *render, struct prompt *prompt, struct matches *matches,19 const struct render_options *render_options, bool dmenu_mode,20 const char *output_name, const char *font_name,2122 /* TODO? */23 const icon_theme_list_t *themes, struct application_list *apps24 );25void wayl_destroy(struct wayland *wayl);2616void wayl_configure(17 struct wayland *wayl, struct render *render, struct prompt *prompt,18 struct matches *matches, bool dmenu_mode);1927void wayl_refresh(struct wayland *wayl);28void wayl_flush(struct wayland *wayl);2923int wayl_ppi(const struct wayland *wayl);30unsigned wayl_ppi(const struct wayland *wayl);31enum fcft_subpixel wayl_subpixel(const struct wayland *wayl);32bool wayl_exit_code(const struct wayland *wayl);33bool wayl_update_cache(const struct wayland *wayl);