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
Provenance
- Commit
b65d052- Tree
1198d2a- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent
c7946c2
5 files changed+174 added−77 removed
modified:main.c+75 added-33 removed
183 unmodified lines184 printf("\n");185 printf("Colors must be specified as a 32-bit hexadecimal RGBA quadruple.\n");186 }187 188+struct 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+};197+198+#if 0199+static void200+foobar(struct wayland *wayl, void *data)201+{202+ struct output_change_context *ctx = data;203+204+ 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;211+212+ char font_attrs[128];213+ snprintf(font_attrs, sizeof(font_attrs), "dpi=%d", wayl_ppi(wayl));214+215+ struct fcft_font *font = fcft_from_name(216+ 1, &(const char *){font_name}, font_attrs);217+218+ if (font == NULL)219+ return;220+221+ LOG_DBG(222+ "font: height: %d, ascent: %d, descent: %d",223+ font->height, font->ascent, font->descent);224+225+ /* 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;232+233+ LOG_DBG("max matches: %zu", max_matches);234+235+ matches_max_matches_set(matches, max_matches);236+ icon_reload_application_icons(*themes, font->height, apps);237+ render_set_font(render, font);238+239+ matches_update(matches, prompt);240+}241+#endif242 int243 main(int argc, char *const *argv)244 {245 static const struct option longopts[] = {163 unmodified lines409 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;413 414 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 lines419 420 if ((fdm = fdm_init()) == NULL)421 goto out;422 370- if ((wayl = wayl_init(371- fdm, render_options.width, render_options.height,372- output_name)) == NULL)373- goto out;423 375- char font_attrs[128];376- snprintf(font_attrs, sizeof(font_attrs), "dpi=%d", wayl_ppi(wayl));424+ setlocale(LC_CTYPE, "");425 378- if ((font = fcft_from_name(1, &(const char *){font_name}, font_attrs)) == NULL)379- goto out;380-381- LOG_DBG(382- "font: height: %d, ascent: %d, descent: %d",383- font->height, font->ascent, font->descent);384-385- /* 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;392-393- LOG_DBG("max matches: %zu", max_matches);394-395- setlocale(LC_ALL, "");396-426 /* Load applications */427 if ((apps = applications_init()) == NULL)428 goto out;429 if (dmenu_mode)1 unmodified line431 else432 xdg_find_programs(terminal, apps);433 read_cache(apps);434 406- if ((render = render_init(&render_options,407- wayl_subpixel(wayl))) == NULL)435+ if ((render = render_init(&render_options)) == NULL)436 goto out;437 438 if ((prompt = prompt_init(L"> ")) == NULL)439 goto out;440 441 if ((matches = matches_init(apps)) == NULL)442 goto out;443 416- 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;450 420- 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+ };461+462+ foobar(wayl, &ctx);463+#endif464 wayl_refresh(wayl);465 466 while (true) {467 wayl_flush(wayl);24 unmodified linesmodified:render.c+7 added-3 removed
335 unmodified lines336 }337 }338 339 struct render *340-render_init(const struct render_options *options,341- enum fcft_subpixel subpixel)340+render_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 };346 347 /* TODO: the one providing the options should calculate these */348 render->options.pix_background_color = rgba2pixman(render->options.background_color);4 unmodified lines353 return render;354 }355 356 void357+render_set_subpixel(struct render *render, enum fcft_subpixel subpixel)358+{359+ render->subpixel = subpixel;360+}361+362+void363 render_set_font(struct render *render, struct fcft_font *font)364 {365 fcft_destroy(render->font);366 render->font = font;11 unmodified linesmodified:render.h+2 added-2 removed
32 unmodified lines33 pixman_color_t pix_selection_color;34 };35 36 struct render;37-struct render *render_init(38- const struct render_options *options, enum fcft_subpixel subpixel);37+struct render *render_init(const struct render_options *options);38 void render_destroy(struct render *render);39 40+void render_set_subpixel(struct render *render, enum fcft_subpixel subpixel);41 void render_set_font(struct render *render, struct fcft_font *font);42 43 void render_background(const struct render *render, struct buffer *buf);44 7 unmodified linesmodified:wayland.c+77 added-33 removed
24 unmodified lines25 26 #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"34 35 struct monitor {36 struct wayland *wayl;37 struct wl_output *output;97 unmodified lines135 struct render *render;136 struct prompt *prompt;137 struct matches *matches;138 139+ const struct render_options *render_options;140+ char *font_name;141+ const icon_theme_list_t *themes;142+ struct application_list *apps;143+144 int width;145 int height;146 int scale;147+ unsigned dpi;148 149 enum { KEEP_RUNNING, EXIT_UPDATE_CACHE, EXIT} status;150 int exit_code;151 bool dmenu_mode;14 unmodified lines166 tll(struct monitor) monitors;167 const struct monitor *monitor;168 169 tll(struct seat) seats;163-170 };171 172 bool173 repeat_start(struct repeat *repeat, uint32_t key)631 unmodified lines805 806 return 1;807 }808 809+static bool810+reload_font(struct wayland *wayl, unsigned new_dpi)811+{812+ LOG_INFO("RELOADING FONT: %d -> %d", wayl->dpi, new_dpi);813+814+ if (wayl->dpi == new_dpi)815+ return true;816+817+ char attrs[256];818+ snprintf(attrs, sizeof(attrs), "dpi=%u", new_dpi);819+820+ wayl->dpi = new_dpi;821+822+ struct fcft_font *font = fcft_from_name(823+ 1, (const char *[]){wayl->font_name}, attrs);824+825+ if (font == NULL)826+ return false;827+828+ 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;833+834+ 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+}840+841 static void842 update_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);847 809- if (scale == wayl->scale)848+ if (scale == wayl->scale && dpi == wayl->dpi)849 return;850 851+ reload_font(wayl, dpi);852+853 wayl->scale = scale;854+ wayl->dpi = dpi;855 856 wayl->width /= scale; wayl->width *= scale;857 wayl->height /= scale; wayl->height *= scale;858 62 unmodified lines921 mon->scale = factor;922 923 if (mon->wayl->monitor == mon) {924 int old_scale = mon->wayl->scale;925+ int old_dpi = mon->wayl->dpi;926+927 update_size(mon->wayl);928 885- 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 lines1327 if (it->item.output != wl_output)1328 continue;1329 1330 int old_scale = wayl->scale;1331+ int old_dpi = wayl->dpi;1332 1333 wayl->monitor = &it->item;1334 update_size(wayl);1335 1291- 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 lines1352 .leave = &surface_leave,1353 };1354 1355 struct wayland *1311-wayl_init(struct fdm *fdm, int width, int height, const char *output_name)1356+wayl_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,1360+1361+ 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+ };1378 1315- 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;1323-1379 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 lines1462 1463 zwlr_layer_surface_v1_add_listener(1464 wayl->layer_surface, &layer_surface_listener, wayl);1465 1411- wayl->width = width;1412- wayl->height = height;1466+ wayl->width = render_options->width;1467+ wayl->height = render_options->height;1468 1469 update_size(wayl);1470 1471 if (wl_display_prepare_read(wayl->display) != 0) {13 unmodified lines1485 return NULL;1486 }1487 1488 void1434-wayl_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-}1444-1445-void1489 wayl_destroy(struct wayland *wayl)1490 {1491 if (wayl == NULL)1492 return;27 unmodified lines1520 if (wayl->display != NULL)1521 wl_display_disconnect(wayl->display);1522 1523 free(wayl->output_name);1524+ free(wayl->font_name);1525 free(wayl);1526 }1527 1528 void1 unmodified line1530 {1531 wl_display_flush(wayl->display);1532 }1533 1490-int1534+unsigned1535 wayl_ppi(const struct wayland *wayl)1536 {1537 /* Use user configured output, if available, otherwise use the1538 * "first" output */6 unmodified lines1545 if (mon != NULL)1546 return mon->ppi.scaled.y * mon->scale;1547 1548 /* No outputs available, return "something" */1505- return 96;1549+ return 96u;1550 }1551 1552 enum fcft_subpixel1553 wayl_subpixel(const struct wayland *wayl)16 unmodified linesmodified:wayland.h+13 added-6 removed
6 unmodified lines7 #include "prompt.h"8 #include "match.h"9 #include "render.h"10 11+/* TODO? */12+#include "application.h"13+#include "icon.h"14+15 struct wayland;16 struct 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,21+22+ /* TODO? */23+ const icon_theme_list_t *themes, struct application_list *apps24+ );25 void wayl_destroy(struct wayland *wayl);26 16-void wayl_configure(17- struct wayland *wayl, struct render *render, struct prompt *prompt,18- struct matches *matches, bool dmenu_mode);19-27 void wayl_refresh(struct wayland *wayl);28 void wayl_flush(struct wayland *wayl);29 23-int wayl_ppi(const struct wayland *wayl);30+unsigned wayl_ppi(const struct wayland *wayl);31 enum fcft_subpixel wayl_subpixel(const struct wayland *wayl);32 bool wayl_exit_code(const struct wayland *wayl);33 bool wayl_update_cache(const struct wayland *wayl);