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
Provenance
- Commit
917c785- Tree
594b962- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent 1
c264b92- Parent 2
91aa7f0
9 files changed+307 added−62 removed
modified:application.c+1 added-3 removed
215 unmodified lines216 217 struct application_list *218 applications_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 }222 223 void224 applications_destroy(struct application_list *apps)62 unmodified linesmodified:CHANGELOG.md+6 added
46 unmodified lines47 (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).57 58 59 ### Deprecated60 ### Removed236 unmodified linesmodified:main.c+233 added-38 removed
2 unmodified lines3 #include <stdbool.h>4 #include <string.h>5 #include <getopt.h>6 #include <errno.h>7+#include <threads.h>8 9 #include <locale.h>10 11 #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>18 19 #include <tllist.h>11 unmodified lines31 #include "version.h"32 #include "wayland.h"33 #include "xdg.h"34 35+struct context {36+ struct wayland *wayl;37+ struct render *render;38+ struct matches *matches;39+ struct prompt *prompt;40+ struct application_list *apps;41+42+ icon_theme_list_t *themes;43+ int icon_size;44+ mtx_t *icon_lock;45+46+ struct {47+ const char *icon_theme;48+ const char *terminal;49+ bool icons_enabled;50+ bool actions_enabled;51+ bool dmenu_mode;52+ } options;53+54+ int event_fd;55+};56+57 static struct rgba58 hex_to_rgba(uint32_t color)59 {60 return (struct rgba){185 unmodified lines246 printf("\n");247 printf("All colors are RGBA - i.e. 8-digit hex values, without prefix.\n");248 }249 226-struct font_reload_context {227- bool icons_enabled;228- const icon_theme_list_t *themes;229- struct application_list *apps;230-};231-250 static void251 font_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;254+255 applications_flush_text_run_cache(ctx->apps);256+257+ 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 }267 268 static bool269 pt_or_px_from_string(const char *s, struct pt_or_px *res)69 unmodified lines339 340 return true;341 }342 343+enum event_type { EVENT_APPS_LOADED = 1, EVENT_ICONS_LOADED = 2 };344+345+static int346+send_event(int fd, enum event_type event)347+{348+ ssize_t bytes = write(fd, &(uint64_t){event}, sizeof(uint64_t));349+350+ if (bytes < 0)351+ return -errno;352+ else if (bytes != (ssize_t)sizeof(uint64_t))353+ return 1;354+ return 0;355+}356+357+358+/* THREAD */359+static int360+populate_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;369+370+ if (dmenu_mode) {371+ dmenu_load_entries(apps);372+ return send_event(ctx->event_fd, EVENT_APPS_LOADED);373+ }374+375+ xdg_find_programs(terminal, actions_enabled, apps);376+ read_cache(apps);377+378+ int r = send_event(ctx->event_fd, EVENT_APPS_LOADED);379+ if (r != 0)380+ return r;381+382+ 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);388+389+ 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);398+399+ r = send_event(ctx->event_fd, EVENT_ICONS_LOADED);400+ if (r != 0)401+ return r;402+ }403+404+ return 0;405+}406+407+/*408+ * Called when the application list has been populated409+ */410+static bool411+fdm_apps_populated(struct fdm *fdm, int fd, int events, void *data)412+{413+ if (events & EPOLLHUP)414+ return false;415+416+ 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+ }425+426+ 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;431+432+ 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;438+439+ case EVENT_ICONS_LOADED:440+ /* Just need to refresh the GUI */441+ break;442+443+ default:444+ LOG_ERR("unknown event: %llx", (long long)event);445+ return false;446+ }447+448+ wayl_refresh(wayl);449+ return true;450+}451+452 int453 main(int argc, char *const *argv)454 {455 #define OPT_LETTER_SPACING 256386 unmodified lines842 843 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);845 710- /* 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+ }851+852 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;858 859+ thrd_t app_thread_id;860+ bool join_app_thread = false;861+ int event_pipe[2] = {-1, -1};862+863 char *lock_file = NULL;864 int file_lock_fd = -1;865 bool unlink_lock_file = true;866 7 unmodified lines874 goto out;875 }876 }877 733- 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- }740-878 if ((fdm = fdm_init()) == NULL)879 goto out;880 744-745- /* 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);883 756- if ((render = render_init(&render_options)) == NULL)757- goto out;758-884 if ((prompt = prompt_init(prompt_content)) == NULL)885 goto out;886 887 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);892 767- struct font_reload_context font_reloaded_data = {768- .icons_enabled = icons_enabled,769- .themes = &themes,893+ if ((apps = applications_init()) == NULL)894+ goto out;895+896+ 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;904+905+ matches_set_applications(matches, apps);906+ matches_update(matches, prompt);907+ }908+909+ 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 };925 926 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;931 779- matches_max_matches_per_page_set(matches, render_options.lines);780- matches_update(matches, prompt);932+ ctx.wayl = wayl;933+934+ /* 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+ }940+941+ ctx.event_fd = event_pipe[1];942+943+ if (!fdm_add(fdm, event_pipe[0], EPOLLIN, &fdm_apps_populated, &ctx))944+ goto out;945+946+ if (thrd_create(&app_thread_id, &populate_apps, &ctx) != thrd_success) {947+ LOG_ERR("failed to create thread");948+ goto out;949+ }950+951+ join_app_thread = true;952+ }953+954 wayl_refresh(wayl);955 956 while (true) {957 wayl_flush(wayl);6 unmodified lines964 965 ret = wayl_exit_code(wayl);966 967 out:968+ if (join_app_thread) {969+ int res;970+ thrd_join(app_thread_id, &res);971+972+ 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+ }980+981+ 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]);987+988+ mtx_destroy(&icon_lock);989+990 shm_fini();991 992 wayl_destroy(wayl);993 render_destroy(render);21 unmodified linesmodified:match.c+27 added-7 removed
158 unmodified lines159 return NULL;160 }161 162 struct matches *163-matches_init(const struct application_list *applications,164- enum match_fields fields, bool fuzzy, size_t fuzzy_min_length,163+matches_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 lines189 free(matches->matches);190 free(matches);191 }192 193+void194+matches_set_applications(struct matches *matches,195+ const struct application_list *applications)196+{197+ assert(matches->applications == NULL);198+ assert(matches->matches == NULL);199+200+ matches->applications = applications;201+ matches->matches = malloc(202+ applications->count * sizeof(matches->matches[0]));203+}204+205 size_t206 matches_max_matches_per_page(const struct matches *matches)207 {208 return matches->max_matches_per_page;7 unmodified lines216 217 size_t218 matches_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 }224 225 size_t226 matches_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 }232 233 const struct match *234 matches_get(const struct matches *matches, size_t idx)39 unmodified lines274 275 size_t276 matches_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 }282 283 bool284 matches_selected_prev(struct matches *matches, bool wrap)116 unmodified lines401 matches_update(struct matches *matches, const struct prompt *prompt)402 {403 assert(matches->max_matches_per_page > 0);404 405+ if (matches->applications == NULL)406+ return;407+408 const wchar_t *ptext = prompt_text(prompt);409 410 /* Nothing entered; all programs found matches */411 if (wcslen(ptext) == 0) {157 unmodified linesmodified:match.h+4 added-2 removed
27 unmodified lines28 MATCH_COMMENT = 0x40,29 };30 31 struct matches;32-struct matches *matches_init(const struct application_list *applications,33- enum match_fields fields, bool fuzzy,32+struct 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);36 void matches_destroy(struct matches *matches);37 38+void matches_set_applications(39+ struct matches *matches, const struct application_list *applications);40+41 size_t matches_max_matches_per_page(const struct matches *matches);42 void matches_max_matches_per_page_set(43 struct matches *matches, size_t max_matches);44 15 unmodified linesmodified:meson.build+2 added
49 unmodified lines50 add_project_arguments('-fmacro-prefix-map=@0@='.format(relative_dir), language: 'c')51 endif52 53 math = cc.find_library('m')54+threads = [dependency('threads'), cc.find_library('stdthreads', required: false)]55 fontconfig = dependency('fontconfig')56 pixman = dependency('pixman-1')57 cairo = dependency('cairo', required: get_option('enable-cairo'))58 libepoll = dependency('epoll-shim', required: false)87 unmodified lines146 '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 linesmodified:render.c+16 added-4 removed
32 unmodified lines33 unsigned inner_pad;34 unsigned border_size;35 unsigned row_height;36 unsigned icon_height;37+38+ mtx_t *icon_lock;39 };40 41 static pixman_color_t42 rgba2pixman(struct rgba rgba)484 unmodified lines527 const int sel_margin = x_margin / 3;528 529 int y = first_row + (row_height + font->height) / 2 - font->descent;530 531+ bool render_icons = mtx_trylock(render->icon_lock) == thrd_success;532+533 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 lines549 const double img_y = first_row + (buf->height - size) / 2.;550 551 const double list_end = first_row + match_count * row_height;552 549- 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+ }559 560 pixman_color_t sc = rgba2pixman(render->options.selection_color);561 pixman_image_fill_rectangles(562 PIXMAN_OP_SRC, buf->pix, &sc, 1,17 unmodified lines580 max_x - cur_x, row_height}581 );582 #endif583 576- {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 lines616 render->options.pix_match_color, &match->application->shaped);617 618 y += row_height;619 }620+621+ if (render_icons)622+ mtx_unlock(render->icon_lock);623 }624 625 struct render *615-render_init(const struct render_options *options)626+render_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 };633 622- /* 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 linesmodified:render.h+3 added-1 removed
1 #pragma once2 3+#include <threads.h>4 #include <pixman.h>5 6 #include <fcft/fcft.h>7 34 unmodified lines42 pixman_color_t pix_selection_text_color;43 };44 45 struct render;45-struct render *render_init(const struct render_options *options);46+struct render *render_init(47+ const struct render_options *options, mtx_t *icon_lock);48 void render_destroy(struct render *render);49 50 void render_set_subpixel(struct render *render, enum fcft_subpixel subpixel);51 bool render_set_font(struct render *render, struct fcft_font *font,12 unmodified linesmodified:wayland.c+15 added-7 removed
414 unmodified lines415 struct wl_surface *surface, struct wl_array *keys)416 {417 struct seat *seat = data;418 seat->kbd.serial = serial;419+420+ 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 lines906 907 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");912 913 wayl->font_is_sized_by_dpi = will_size_using_dpi;914 915 if (need_font_reload) {591 unmodified lines1507 {1508 struct wayland *wayl = data;1509 int event_count = 0;1510 1509- 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+ }1516 1512- 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+ }1523 1524 if (events & EPOLLHUP) {1525 LOG_WARN("disconnected from Wayland");1517- wl_display_cancel_read(wayl->display);1526 return false;1527 }1528 1529 wl_display_flush(wayl->display);327 unmodified lines