Skip to content
Myr

Merge branch 'delay-application-listing'

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

Closes #82

authored by Daniel Eklöf · committed by Daniel Eklöf · parents c264b92, 91aa7f0 · Browse files · 9 files+307 added−62 removed
This commit

State

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

Diff stats

9 files+307 added−62 removed

  • other:+307 added−62 removed

9 files+307 added−62 removed

modified:application.c+1 added-3 removed
215 unmodified lines
216217struct application_list *218applications_init(void)219{220 struct application_list *apps = malloc(sizeof(*apps));221 *apps = (struct application_list){};222 return apps;220 return calloc(1, sizeof(struct application_list));221}222223void224applications_destroy(struct application_list *apps)
62 unmodified lines
modified:CHANGELOG.md+6 added
46 unmodified lines
47 (https://codeberg.org/dnkl/fuzzel/issues/77).48* Use a lock file instead of parsing `/proc` to prevent multiple49 fuzzel instances from running at the same time50 (https://codeberg.org/dnkl/fuzzel/issues/84).51* The application list is now populated in a separate thread, in52 parallel to initializing the GUI. This reduces the risk of missing53 keyboard input (https://codeberg.org/dnkl/fuzzel/issues/82).54* Icons are now loaded in a thread. This allows us to display the55 application list quickly (initially without icons, if loading them56 takes “too” long).575859### Deprecated60### Removed
236 unmodified lines
modified:main.c+233 added-38 removed
2 unmodified lines
3#include <stdbool.h>4#include <string.h>5#include <getopt.h>6#include <errno.h>7#include <threads.h>89#include <locale.h>1011#include <sys/types.h>12#include <sys/mman.h>13#include <sys/stat.h>14#include <sys/file.h>15#include <sys/epoll.h>16#include <fcntl.h>17#include <dirent.h>1819#include <tllist.h>
11 unmodified lines
31#include "version.h"32#include "wayland.h"33#include "xdg.h"3435struct context {36 struct wayland *wayl;37 struct render *render;38 struct matches *matches;39 struct prompt *prompt;40 struct application_list *apps;4142 icon_theme_list_t *themes;43 int icon_size;44 mtx_t *icon_lock;4546 struct {47 const char *icon_theme;48 const char *terminal;49 bool icons_enabled;50 bool actions_enabled;51 bool dmenu_mode;52 } options;5354 int event_fd;55};5657static struct rgba58hex_to_rgba(uint32_t color)59{60 return (struct rgba){
185 unmodified lines
246 printf("\n");247 printf("All colors are RGBA - i.e. 8-digit hex values, without prefix.\n");248}249226struct font_reload_context {227 bool icons_enabled;228 const icon_theme_list_t *themes;229 struct application_list *apps;230};231250static void251font_reloaded(struct wayland *wayl, struct fcft_font *font, void *data)252{235 struct font_reload_context *ctx = data;236 if (ctx->icons_enabled)237 icon_reload_application_icons(*ctx->themes, font->height, ctx->apps);253 struct context *ctx = data;254255 applications_flush_text_run_cache(ctx->apps);256257 mtx_lock(ctx->icon_lock);258 {259 ctx->icon_size = font->height;260 if (ctx->options.icons_enabled) {261 icon_reload_application_icons(262 *ctx->themes, ctx->icon_size, ctx->apps);263 }264 }265 mtx_unlock(ctx->icon_lock);266}267268static bool269pt_or_px_from_string(const char *s, struct pt_or_px *res)
69 unmodified lines
339340 return true;341}342343enum event_type { EVENT_APPS_LOADED = 1, EVENT_ICONS_LOADED = 2 };344345static int346send_event(int fd, enum event_type event)347{348 ssize_t bytes = write(fd, &(uint64_t){event}, sizeof(uint64_t));349350 if (bytes < 0)351 return -errno;352 else if (bytes != (ssize_t)sizeof(uint64_t))353 return 1;354 return 0;355}356357358/* THREAD */359static int360populate_apps(void *_ctx)361{362 struct context *ctx = _ctx;363 struct application_list *apps = ctx->apps;364 const char *icon_theme = ctx->options.icon_theme;365 const char *terminal = ctx->options.terminal;366 bool actions_enabled = ctx->options.actions_enabled;367 bool dmenu_mode = ctx->options.dmenu_mode;368 bool icons_enabled = ctx->options.icons_enabled;369370 if (dmenu_mode) {371 dmenu_load_entries(apps);372 return send_event(ctx->event_fd, EVENT_APPS_LOADED);373 }374375 xdg_find_programs(terminal, actions_enabled, apps);376 read_cache(apps);377378 int r = send_event(ctx->event_fd, EVENT_APPS_LOADED);379 if (r != 0)380 return r;381382 if (icons_enabled) {383 icon_theme_list_t icon_themes = icon_load_theme(icon_theme);384 if (tll_length(icon_themes) > 0)385 LOG_INFO("theme: %s", tll_front(icon_themes).name);386 else387 LOG_WARN("%s: icon theme not found", icon_theme);388389 mtx_lock(ctx->icon_lock);390 {391 *ctx->themes = icon_themes;392 if (ctx->icon_size > 0) {393 icon_reload_application_icons(394 *ctx->themes, ctx->icon_size, apps);395 }396 }397 mtx_unlock(ctx->icon_lock);398399 r = send_event(ctx->event_fd, EVENT_ICONS_LOADED);400 if (r != 0)401 return r;402 }403404 return 0;405}406407/*408 * Called when the application list has been populated409 */410static bool411fdm_apps_populated(struct fdm *fdm, int fd, int events, void *data)412{413 if (events & EPOLLHUP)414 return false;415416 uint64_t event;417 ssize_t bytes = read(fd, &event, sizeof(event));418 if (bytes != (ssize_t)sizeof(event)) {419 if (bytes < 0)420 LOG_ERRNO("failed to read event FD");421 else422 LOG_ERR("partial read from event FD");423 return false;424 }425426 struct context *ctx = data;427 struct wayland *wayl = ctx->wayl;428 struct application_list *apps = ctx->apps;429 struct matches *matches = ctx->matches;430 struct prompt *prompt = ctx->prompt;431432 switch (event) {433 case EVENT_APPS_LOADED:434 /* Update matches list, then refresh the GUI */435 matches_set_applications(matches, apps);436 matches_update(matches, prompt);437 break;438439 case EVENT_ICONS_LOADED:440 /* Just need to refresh the GUI */441 break;442443 default:444 LOG_ERR("unknown event: %llx", (long long)event);445 return false;446 }447448 wayl_refresh(wayl);449 return true;450}451452int453main(int argc, char *const *argv)454{455 #define OPT_LETTER_SPACING 256
386 unmodified lines
842843 log_init(LOG_COLORIZE_AUTO, true, LOG_FACILITY_USER, LOG_CLASS_INFO);844 fcft_log_init(FCFT_LOG_COLORIZE_AUTO, true, FCFT_LOG_CLASS_INFO);845710 /* Load applications */846 mtx_t icon_lock;847 if (mtx_init(&icon_lock, mtx_plain) != thrd_success) {848 LOG_ERR("failed to create icon lock");849 return EXIT_FAILURE;850 }851852 struct application_list *apps = NULL;853 struct fdm *fdm = NULL;854 struct prompt *prompt = NULL;855 struct matches *matches = NULL;856 struct render *render = NULL;857 struct wayland *wayl = NULL;858859 thrd_t app_thread_id;860 bool join_app_thread = false;861 int event_pipe[2] = {-1, -1};862863 char *lock_file = NULL;864 int file_lock_fd = -1;865 bool unlink_lock_file = true;866
7 unmodified lines
874 goto out;875 }876 }877733 if (icons_enabled) {734 themes = icon_load_theme(icon_theme);735 if (tll_length(themes) > 0)736 LOG_INFO("theme: %s", tll_front(themes).name);737 else738 LOG_WARN("%s: icon theme not found", icon_theme);739 }740878 if ((fdm = fdm_init()) == NULL)879 goto out;880744745 /* Load applications */746 if ((apps = applications_init()) == NULL)881 if ((render = render_init(&render_options, &icon_lock)) == NULL)882 goto out;748 if (dmenu_mode) {749 dmenu_load_entries(apps);750 if (no_run_if_empty && apps->count == 0)751 goto out;752 } else753 xdg_find_programs(terminal, actions_enabled, apps);754 read_cache(apps);883756 if ((render = render_init(&render_options)) == NULL)757 goto out;758884 if ((prompt = prompt_init(prompt_content)) == NULL)885 goto out;886887 if ((matches = matches_init(763 apps, match_fields, fuzzy, fuzzy_min_length,888 match_fields, fuzzy, fuzzy_min_length,889 fuzzy_max_length_discrepancy, fuzzy_max_distance)) == NULL)890 goto out;891 matches_max_matches_per_page_set(matches, render_options.lines);892767 struct font_reload_context font_reloaded_data = {768 .icons_enabled = icons_enabled,769 .themes = &themes,893 if ((apps = applications_init()) == NULL)894 goto out;895896 if (dmenu_mode && no_run_if_empty) {897 /*898 * If no_run_if_empty is set, we *must* load the entries899 * *before displaying the window.900 */901 dmenu_load_entries(apps);902 if (apps->count == 0)903 goto out;904905 matches_set_applications(matches, apps);906 matches_update(matches, prompt);907 }908909 struct context ctx = {910 .render = render,911 .matches = matches,912 .prompt = prompt,913 .apps = apps,914 .themes = &themes,915 .icon_lock = &icon_lock,916 .options = {917 .icon_theme = icon_theme,918 .terminal = terminal,919 .icons_enabled = icons_enabled,920 .actions_enabled = actions_enabled,921 .dmenu_mode = dmenu_mode,922 },923 .event_fd = -1,924 };925926 if ((wayl = wayl_init(927 fdm, render, prompt, matches, &render_options,928 dmenu_mode, launch_prefix, output_name, font_name, dpi_aware,776 &font_reloaded, &font_reloaded_data)) == NULL)929 &font_reloaded, &ctx)) == NULL)930 goto out;931779 matches_max_matches_per_page_set(matches, render_options.lines);780 matches_update(matches, prompt);932 ctx.wayl = wayl;933934 /* Create thread that will populate the application list */935 if (!dmenu_mode || !no_run_if_empty) {936 if (pipe2(event_pipe, O_CLOEXEC | O_NONBLOCK) < 0) {937 LOG_ERRNO("failed to create event pipe");938 goto out;939 }940941 ctx.event_fd = event_pipe[1];942943 if (!fdm_add(fdm, event_pipe[0], EPOLLIN, &fdm_apps_populated, &ctx))944 goto out;945946 if (thrd_create(&app_thread_id, &populate_apps, &ctx) != thrd_success) {947 LOG_ERR("failed to create thread");948 goto out;949 }950951 join_app_thread = true;952 }953954 wayl_refresh(wayl);955956 while (true) {957 wayl_flush(wayl);
6 unmodified lines
964965 ret = wayl_exit_code(wayl);966967out:968 if (join_app_thread) {969 int res;970 thrd_join(app_thread_id, &res);971972 if (res != 0) {973 if (res < 0)974 LOG_ERRNO_P("populate application list thread failed", res);975 else976 LOG_ERRNO("populate application list thread failed: "977 "failed to signal done event");978 }979 }980981 if (event_pipe[0] >= 0) {982 fdm_del_no_close(fdm, event_pipe[0]);983 close(event_pipe[0]);984 }985 if (event_pipe[1] >= 0)986 close(event_pipe[1]);987988 mtx_destroy(&icon_lock);989990 shm_fini();991992 wayl_destroy(wayl);993 render_destroy(render);
21 unmodified lines
modified:match.c+27 added-7 removed
158 unmodified lines
159 return NULL;160}161162struct matches *163matches_init(const struct application_list *applications,164 enum match_fields fields, bool fuzzy, size_t fuzzy_min_length,163matches_init(enum match_fields fields, bool fuzzy, size_t fuzzy_min_length,164 size_t fuzzy_max_length_discrepancy, size_t fuzzy_max_distance)165{166 struct matches *matches = malloc(sizeof(*matches));167 *matches = (struct matches) {169 .applications = applications,168 .applications = NULL,169 .fields = fields,171 .matches = malloc(applications->count * sizeof(matches->matches[0])),170 .matches = NULL,171 .fuzzy = fuzzy,172 .page_count = 0,173 .match_count = 0,174 .selected = 0,
14 unmodified lines
189 free(matches->matches);190 free(matches);191}192193void194matches_set_applications(struct matches *matches,195 const struct application_list *applications)196{197 assert(matches->applications == NULL);198 assert(matches->matches == NULL);199200 matches->applications = applications;201 matches->matches = malloc(202 applications->count * sizeof(matches->matches[0]));203}204205size_t206matches_max_matches_per_page(const struct matches *matches)207{208 return matches->max_matches_per_page;
7 unmodified lines
216217size_t218matches_get_page_count(const struct matches *matches)219{209 return matches->match_count / matches->max_matches_per_page;220 return matches->max_matches_per_page != 0221 ? matches->match_count / matches->max_matches_per_page222 : 0;223}224225size_t226matches_get_page(const struct matches *matches)227{215 return matches->selected / matches->max_matches_per_page;228 return matches->max_matches_per_page != 0229 ? matches->selected / matches->max_matches_per_page230 : 0;231}232233const struct match *234matches_get(const struct matches *matches, size_t idx)
39 unmodified lines
274275size_t276matches_get_match_index(const struct matches *matches)277{263 return matches->selected % matches->max_matches_per_page;278 return matches->max_matches_per_page != 0279 ? matches->selected % matches->max_matches_per_page280 : 0;281}282283bool284matches_selected_prev(struct matches *matches, bool wrap)
116 unmodified lines
401matches_update(struct matches *matches, const struct prompt *prompt)402{403 assert(matches->max_matches_per_page > 0);404405 if (matches->applications == NULL)406 return;407408 const wchar_t *ptext = prompt_text(prompt);409410 /* Nothing entered; all programs found matches */411 if (wcslen(ptext) == 0) {
157 unmodified lines
modified:match.h+4 added-2 removed
27 unmodified lines
28 MATCH_COMMENT = 0x40,29};3031struct matches;32struct matches *matches_init(const struct application_list *applications,33 enum match_fields fields, bool fuzzy,32struct matches *matches_init(enum match_fields fields, bool fuzzy,33 size_t fuzzy_min_length,34 size_t fuzzy_max_length_discrepancy,35 size_t fuzzy_max_distance);36void matches_destroy(struct matches *matches);3738void matches_set_applications(39 struct matches *matches, const struct application_list *applications);4041size_t matches_max_matches_per_page(const struct matches *matches);42void matches_max_matches_per_page_set(43 struct matches *matches, size_t max_matches);44
15 unmodified lines
modified:meson.build+2 added
49 unmodified lines
50 add_project_arguments('-fmacro-prefix-map=@0@='.format(relative_dir), language: 'c')51endif5253math = cc.find_library('m')54threads = [dependency('threads'), cc.find_library('stdthreads', required: false)]55fontconfig = dependency('fontconfig')56pixman = dependency('pixman-1')57cairo = dependency('cairo', required: get_option('enable-cairo'))58libepoll = dependency('epoll-shim', required: false)
87 unmodified lines
146 'wayland.c', 'wayland.h',147 'xdg.c', 'xdg.h',148 wl_proto_src + wl_proto_headers, version,149 dependencies: [math,150 threads,151 pixman,152 fontconfig,153 cairo,154 libepoll,
25 unmodified lines
modified:render.c+16 added-4 removed
32 unmodified lines
33 unsigned inner_pad;34 unsigned border_size;35 unsigned row_height;36 unsigned icon_height;3738 mtx_t *icon_lock;39};4041static pixman_color_t42rgba2pixman(struct rgba rgba)
484 unmodified lines
527 const int sel_margin = x_margin / 3;528529 int y = first_row + (row_height + font->height) / 2 - font->descent;530531 bool render_icons = mtx_trylock(render->icon_lock) == thrd_success;532533 for (size_t i = 0; i < match_count; i++) {534 if (y + font->descent > buf->height - y_margin - border_size) {535 /* Window too small - happens if the compositor doesn't536 * respect our requested size */
12 unmodified lines
549 const double img_y = first_row + (buf->height - size) / 2.;550551 const double list_end = first_row + match_count * row_height;552549 if (match->application->icon.type == ICON_SVG && img_y > list_end)553 if (render_icons &&554 match->application->icon.type == ICON_SVG &&555 img_y > list_end)556 {557 render_svg(&match->application->icon, img_x, img_y, size, buf);558 }559560 pixman_color_t sc = rgba2pixman(render->options.selection_color);561 pixman_image_fill_rectangles(562 PIXMAN_OP_SRC, buf->pix, &sc, 1,
17 unmodified lines
580 max_x - cur_x, row_height}581 );582#endif583576 {584 if (render_icons) {585 struct icon *icon = &match->application->icon;586 const int size = render->icon_height;587 const int img_x = cur_x;588 const int img_y = first_row + i * row_height + (row_height - size) / 2;
27 unmodified lines
616 render->options.pix_match_color, &match->application->shaped);617618 y += row_height;619 }620621 if (render_icons)622 mtx_unlock(render->icon_lock);623}624625struct render *615render_init(const struct render_options *options)626render_init(const struct render_options *options, mtx_t *icon_lock)627{628 struct render *render = calloc(1, sizeof(*render));629 *render = (struct render){630 .options = *options,631 .icon_lock = icon_lock,632 };633622 /* TODO: the one providing the opti3Dons should calculate these */634 /* TODO: the one providing the options should calculate these */635 render->options.pix_background_color = rgba2pixman(render->options.background_color);636 render->options.pix_border_color = rgba2pixman(render->options.border_color);637 render->options.pix_text_color = rgba2pixman(render->options.text_color);638 render->options.pix_match_color = rgba2pixman(render->options.match_color);
91 unmodified lines
modified:render.h+3 added-1 removed
1#pragma once23#include <threads.h>4#include <pixman.h>56#include <fcft/fcft.h>7
34 unmodified lines
42 pixman_color_t pix_selection_text_color;43};4445struct render;45struct render *render_init(const struct render_options *options);46struct render *render_init(47 const struct render_options *options, mtx_t *icon_lock);48void render_destroy(struct render *render);4950void render_set_subpixel(struct render *render, enum fcft_subpixel subpixel);51bool render_set_font(struct render *render, struct fcft_font *font,
12 unmodified lines
modified:wayland.c+15 added-7 removed
414 unmodified lines
415 struct wl_surface *surface, struct wl_array *keys)416{417 struct seat *seat = data;418 seat->kbd.serial = serial;419420 LOG_DBG("keyboard enter");421#if 0422 uint32_t *key;423 wl_array_for_each(key, keys)424 xkb_state_update_key(xkb_state, *key, 1);
481 unmodified lines
906907 LOG_DBG(908 "font reload: scale: %u -> %u, dpi: %.2f -> %.2f size method: %s -> %s",909 wayl->scale, new_scale, wayl->dpi, new_dpi,908 was_scaled_using_dpi ? "DPI" : "scaling factor",909 will_scale_using_dpi ? "DPI" : "scaling factor");910 was_sized_using_dpi ? "DPI" : "scaling factor",911 will_size_using_dpi ? "DPI" : "scaling factor");912913 wayl->font_is_sized_by_dpi = will_size_using_dpi;914915 if (need_font_reload) {
591 unmodified lines
1507{1508 struct wayland *wayl = data;1509 int event_count = 0;15101509 if (events & EPOLLIN)1510 wl_display_read_events(wayl->display);1511 if (events & EPOLLIN) {1512 if (wl_display_read_events(wayl->display) < 0) {1513 LOG_ERRNO("failed to read events from the Wayland socket");1514 return false;1515 }15161512 while (wl_display_prepare_read(wayl->display) != 0)1513 wl_display_dispatch_pending(wayl->display);1517 while (wl_display_prepare_read(wayl->display) != 0)1518 if (wl_display_dispatch_pending(wayl->display) < 0) {1519 LOG_ERRNO("failed to dispatch pending Wayland events");1520 return false;1521 }1522 }15231524 if (events & EPOLLHUP) {1525 LOG_WARN("disconnected from Wayland");1517 wl_display_cancel_read(wayl->display);1526 return false;1527 }15281529 wl_display_flush(wayl->display);
327 unmodified lines