dmenu: start rendering input before STDIN has been closed
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Note: this commit is known broken, and will be repaired by follow up commits.
authored by Daniel Eklöf · committed by Daniel Eklöf · parent b81bca1 · Browse files · 15 files·+273 added−107 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
15 files+273 added−107 removed
- other:+273 added−107 removed
Provenance
- Commit
b69ba92- Tree
17c49b6- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent
b81bca1
15 files changed+273 added−107 removed
modified:application.c+17 added-5 removed
23 unmodified lines24 return true;25 26 if (*argc >= *size) {27 size_t new_size = *size > 0 ? 2 * *size : 10;28- char **new_argv = realloc(*argv, new_size * sizeof(new_argv[0]));28+ char **new_argv = reallocarray(*argv, new_size, sizeof(new_argv[0]));29 30 if (new_argv == NULL)31 return false;32 279 unmodified lines312 313 struct application_list *314 applications_init(void)315 {316- return calloc(1, sizeof(struct application_list));316+ struct application_list *ret = calloc(1, sizeof(*ret));317+ if (ret == NULL)318+ return NULL;319+320+ if (mtx_init(&ret->lock, mtx_plain) != thrd_success) {321+ LOG_ERR("failed to instantiate application list mutex");322+ free(ret);323+ return NULL;324+ }325+326+ return ret;327 }328 329 void330 applications_destroy(struct application_list *apps)1 unmodified line332 if (apps == NULL)333 return;334 335 for (size_t i = 0; i < apps->count; i++) {326- struct application *app = &apps->v[i];336+ struct application *app = apps->v[i];337 338 free(app->id);339 free(app->path);340 free(app->exec);43 unmodified lines384 }385 free(app->icon.path);386 387 fcft_text_run_destroy(app->shaped);388+ free(app);389 }390 391+ mtx_destroy(&apps->lock);392 free(apps->v);393 free(apps);394 }395 396 void397 applications_flush_text_run_cache(struct application_list *apps)398 {399 for (size_t i = 0; i < apps->count; i++) {388- fcft_text_run_destroy(apps->v[i].shaped);389- apps->v[i].shaped = NULL;400+ fcft_text_run_destroy(apps->v[i]->shaped);401+ apps->v[i]->shaped = NULL;402 }403 }modified:application.h+2 added-1 removed
90 unmodified lines91 const struct application *app, const struct prompt *prompt,92 const char *launch_prefix, const char *xdg_activation_token);93 94 struct application_list {95- struct application *v;95+ struct application **v;96 size_t count;97+ mtx_t lock;98 };99 100 struct application_list *applications_init(void);101 void applications_destroy(struct application_list *apps);1 unmodified linemodified:CHANGELOG.md+1 added
57 unmodified lines58 `--workers` command line option, and the new `workers` option in59 `fuzzel.ini`.60 * dmenu mode: `--prompt-only=PROMPT` command line option61 ([#276][276]).62+* dmenu mode: start rendering input before STDIN has been closed.63 64 [344]: https://codeberg.org/dnkl/fuzzel/issues/34465 [131]: https://codeberg.org/dnkl/fuzzel/issues/13166 [189]: https://codeberg.org/dnkl/fuzzel/issues/189700 unmodified linesmodified:char32.c+7 added-1 removed
45 unmodified lines46 {47 return wcscmp((const wchar_t *)s1, (const wchar_t *)s2);48 }49 50+int51+c32casecmp(const char32_t *s1, const char32_t *s2)52+{53+ return wcscasecmp((const wchar_t *)s1, (const wchar_t *)s2);54+}55+56 char32_t *57 c32cpy(char32_t *dest, const char32_t *src)58 {59 return (char32_t *)wcscpy((wchar_t *)dest, (const wchar_t *)src);150 unmodified lines210 }211 212 *out = U'\0';213 208- ret = realloc(ret, (chars + 1) * sizeof(ret[0]));214+ ret = reallocarray(ret, chars + 1, sizeof(ret[0]));215 return ret;216 217 err:218 free(ret);77 unmodified linesmodified:char32.h+1 added
5 unmodified lines6 #include <stdarg.h>7 8 size_t c32len(const char32_t *s);9 int c32cmp(const char32_t *s1, const char32_t *s2);10+int c32casecmp(const char32_t *s1, const char32_t *s2);11 char32_t *c32cpy(char32_t *dest, const char32_t *src);12 char32_t *c32cat(char32_t *dest, const char32_t *src);13 char32_t *c32dup(const char32_t *s);14 char32_t *c32chr(const char32_t *s, char32_t c);12 unmodified linesmodified:dmenu.c+42 added-9 removed
14 unmodified lines15 #define LOG_MODULE "dmenu"16 #define LOG_ENABLE_DBG 017 #include "log.h"18 #include "char32.h"19+#include "event.h"20 21 void22 dmenu_load_entries(struct application_list *applications, char delim,22- int abort_fd)23+ int event_fd, int abort_fd)24 {24- tll(struct application) entries = tll_init();25+ tll(struct application *) entries = tll_init();26 27 size_t size = 0;27- size_t alloc_size = 256;28+ size_t alloc_size = 16384;29 char *buffer = malloc(alloc_size);30 31 if (buffer == NULL) {32 LOG_ERRNO("failed to allocate input buffer");7 unmodified lines40 LOG_ERRNO("failed to set O_NONBLOCK on stdin");41 goto out;42 }43 44+ size_t app_idx = 0;45+46 errno = 0;47 while (true) {48 struct pollfd fds[] = {49 {.fd = STDIN_FILENO, .events = POLLIN},11 unmodified lines61 LOG_ERRNO("failed to poll stdin");62 break;63 }64 62- if (fds[1].revents & (POLLIN | POLLHUP))65+ if (fds[1].revents & (POLLIN | POLLHUP)) {66+ LOG_WARN("aborted");67 break;68+ }69 70 /* Increase size of input buffer, if necessary */71 if (size >= alloc_size - 2) {72 LOG_DBG("increasing input buffer size %zu -> %zu",100 unmodified lines173 char32_t *lowercase = c32dup(wline);174 for (size_t i = 0; i < c32len(lowercase); i++)175 lowercase[i] = toc32lower(lowercase[i]);176 172- struct application app = {177+ struct application *app = malloc(sizeof(*app));178+ *app = (struct application){179 .title = wline,180 .title_lowercase = lowercase,181 .title_len = c32len(lowercase),182 .icon = {.name = icon_name},1 unmodified line184 };185 186 tll_push_back(entries, app);187 }188+189+ if (event_fd >= 0) {190+ mtx_lock(&applications->lock);191+192+ const size_t new_count = applications->count + tll_length(entries);193+ applications->v = reallocarray(194+ applications->v, new_count, sizeof(applications->v[0]));195+196+ size_t i = applications->count;197+ tll_foreach(entries, it) {198+ applications->v[i++] = it->item;199+ tll_remove(entries, it);200+ }201+202+ assert(i == new_count);203+ applications->count = new_count;204+ send_event(event_fd, EVENT_APPS_SOME_LOADED);205+ mtx_unlock(&applications->lock);206+ }207 }208 209 free(buffer);210 211 out:212 188- applications->v = calloc(tll_length(entries), sizeof(applications->v[0]));189- applications->count = tll_length(entries);213+ if (tll_length(entries) == 0)214+ return;215 191- /* Convert linked-list to regular array */192- size_t i = 0;216+ mtx_lock(&applications->lock);217+218+ const size_t new_count = applications->count + tll_length(entries);219+ applications->v = reallocarray(220+ applications->v, new_count, sizeof(applications->v[0]));221+222+ size_t i = applications->count;223 tll_foreach(entries, it) {224 applications->v[i++] = it->item;225 tll_remove(entries, it);226 }227 228+ applications->count = new_count;229+ mtx_unlock(&applications->lock);230+231 tll_free(entries);232 }233 234 bool19 unmodified linesmodified:dmenu.h+2 added-1 removed
4 unmodified lines5 #include "application.h"6 #include "config.h"7 #include "prompt.h"8 9-void dmenu_load_entries(struct application_list *applications, char delim, int abort_fd);9+void dmenu_load_entries(10+ struct application_list *applications, char delim, int event_fd, int abort_fd);11 bool dmenu_execute(const struct application *app, ssize_t index,12 const struct prompt *prompt, enum dmenu_mode format);added:event.c+18 added
1+#include "event.h"2+3+#include <errno.h>4+#include <stdint.h>5+#include <unistd.h>6+7+int8+send_event(int fd, enum event_type event)9+{10+ ssize_t bytes = write(fd, &(uint64_t){event}, sizeof(uint64_t));11+12+ if (bytes < 0)13+ return -errno;14+ else if (bytes != (ssize_t)sizeof(uint64_t))15+ return 1;16+ return 0;17+}18+added:event.h+11 added
1+#pragma once2+3+enum event_type {4+ EVENT_APPS_SOME_LOADED,5+ EVENT_APPS_ALL_LOADED,6+ EVENT_ICONS_LOADED,7+8+ EVENT_INVALID,9+};10+11+int send_event(int fd, enum event_type event);modified:icon.c+1 added-1 removed
505 unmodified lines506 507 tll(struct icon_data) icons = tll_init();508 509 for (size_t i = 0; i < applications->count; i++) {510- struct application *app = &applications->v[i];510+ struct application *app = applications->v[i];511 icon_reset(&app->icon);512 513 if (app->icon.name == NULL)514 continue;264 unmodified linesmodified:main.c+83 added-48 removed
26 unmodified lines27 #include "application.h"28 #include "char32.h"29 #include "config.h"30 #include "dmenu.h"31+#include "event.h"32 #include "fdm.h"33 #include "key-binding.h"34 #include "match.h"35+#include "path.h"36 #include "render.h"37 #include "shm.h"38 #include "version.h"39 #include "wayland.h"40 #include "xdg.h"39-#include "path.h"41 42 #define max(x, y) ((x) > (y) ? (x) : (y))43 44 struct context {117 unmodified lines162 fclose(f);163 164 /* Loop all applications, and search for a matching cache entry */165 for (size_t i = 0; i < apps->count; i++) {165- struct application *app = &apps->v[i];166+ struct application *app = apps->v[i];167 168 if ((!dmenu && app->id == NULL) || (dmenu && app->title == NULL)) {169 continue;170 }57 unmodified lines228 return;229 }230 231 for (size_t i = 0; i < apps->count; i++) {231- if (apps->v[i].count == 0)232+ if (apps->v[i]->count == 0)233 continue;234 234- if (!apps->v[i].visible)235+ if (!apps->v[i]->visible)236 continue;237 237- if (!dmenu || apps->v[i].id == NULL) {238- const char *id = apps->v[i].id;238+ if (!dmenu || apps->v[i]->id != NULL) {239+ const char *id = apps->v[i]->id;240 const size_t id_len = strlen(id);241 242 if (write(fd, id, id_len) != id_len) {243 LOG_ERRNO("failed to write cache");244 break;245 }246 } else {246- const char32_t *title = apps->v[i].title;247+ const char32_t *title = apps->v[i]->title;248 char *u8_title = ac32tombs(title);249 250 if (u8_title != NULL) {251 const size_t title_len = strlen(u8_title);6 unmodified lines258 }259 }260 261 char count_as_str[11];261- sprintf(count_as_str, "%u", apps->v[i].count);262+ sprintf(count_as_str, "%u", apps->v[i]->count);263 const size_t count_len = strlen(count_as_str);264 265 if (write(fd, "|", 1) != 1 ||266 write(fd, count_as_str, count_len) != count_len ||222 unmodified lines489 490 return true;491 }492 492-enum event_type { EVENT_APPS_LOADED = 1, EVENT_ICONS_LOADED = 2 };493-494-static int495-send_event(int fd, enum event_type event)496-{497- ssize_t bytes = write(fd, &(uint64_t){event}, sizeof(uint64_t));498-499- if (bytes < 0)500- return -errno;501- else if (bytes != (ssize_t)sizeof(uint64_t))502- return 1;503- return 0;504-}505-506-493 /* THREAD */494 static int495 populate_apps(void *_ctx)496 {27 unmodified lines524 }525 526 if (dmenu_enabled) {527 if (!conf->prompt_only) {542- dmenu_load_entries(apps, dmenu_delim, ctx->dmenu_abort_fd);528+ dmenu_load_entries(apps, dmenu_delim, ctx->event_fd, ctx->dmenu_abort_fd);529 read_cache(cache_path, apps, true);530 }531 } else {532 xdg_find_programs(terminal, actions_enabled, filter_desktop, &desktops, apps);2 unmodified lines535 read_cache(cache_path, apps, false);536 }537 tll_free_and_free(desktops, free);538 553- int r = send_event(ctx->event_fd, EVENT_APPS_LOADED);539+ int r = send_event(ctx->event_fd, EVENT_APPS_ALL_LOADED);540 if (r != 0)541 return r;542 543 if (icons_enabled) {20 unmodified lines564 565 return 0;566 }567 582-/*583- * Called when the application list has been populated584- */568 static bool586-fdm_apps_populated(struct fdm *fdm, int fd, int events, void *data)569+process_event(struct context *ctx, enum event_type event)570 {588- if (events & EPOLLHUP)589- return false;590-591- uint64_t event;592- ssize_t bytes = read(fd, &event, sizeof(event));593- if (bytes != (ssize_t)sizeof(event)) {594- if (bytes < 0)595- LOG_ERRNO("failed to read event FD");596- else597- LOG_ERR("partial read from event FD");598- return false;599- }600-601- struct context *ctx = data;571 struct wayland *wayl = ctx->wayl;572 struct application_list *apps = ctx->apps;573 struct matches *matches = ctx->matches;605- struct prompt *prompt = ctx->prompt;574 const char *select = ctx->select_initial;575 576 switch (event) {609- case EVENT_APPS_LOADED:577+ case EVENT_APPS_SOME_LOADED:578+ case EVENT_APPS_ALL_LOADED: {579 /* Update matches list, then refresh the GUI */580 matches_set_applications(matches, apps);581+582+ if (event == EVENT_APPS_ALL_LOADED)583+ matches_all_applications_loaded(matches);584+585+#define UPDATE_MATCHES 1586+587+#if !UPDATE_MATCHES588+ const size_t match_count = matches_get_count(matches);589+ const size_t matches_per_page = matches_max_matches_per_page(matches);590+591+ if (event == EVENT_APPS_ALL_LOADED || match_count < matches_per_page) {592+#endif593+ /*594+ * Only update matches if595+ * a) all applications have been loaded596+ * b) a partial load will cause more matches to be displayed597+ */612- matches_update(matches, prompt);613- matches_selected_select(matches, select);598+ matches_update_no_delay(matches);599+ matches_selected_select(matches, select);600+#if !UPDATE_MATCHES601+ }602+#endif603 break;604+ }605 606 case EVENT_ICONS_LOADED:607 /* Just need to refresh the GUI */608 matches_icons_loaded(matches);7 unmodified lines616 wayl_refresh(wayl);617 return true;618 }619 620+/*621+ * Called when the application list has been populated622+ */623+static bool624+fdm_apps_populated(struct fdm *fdm, int fd, int events, void *data)625+{626+ struct context *ctx = data;627+ uint64_t last_event = EVENT_INVALID;628+ uint64_t event = EVENT_INVALID;629+630+ while (true) {631+ ssize_t bytes = read(fd, &event, sizeof(event));632+ if (bytes == 0) {633+ if (event != EVENT_INVALID)634+ process_event(ctx, event);635+ break;636+ }637+638+ if (bytes != (ssize_t)sizeof(event)) {639+ if (bytes < 0) {640+ if (errno == EAGAIN) {641+ if (event != EVENT_INVALID)642+ process_event(ctx, event);643+ break;644+ }645+ LOG_ERRNO("failed to read event FD");646+ } else647+ LOG_ERR("partial read from event FD");648+ return false;649+ }650+651+ /* Coalesce multiple, identical events */652+ if (last_event != EVENT_INVALID) {653+ if (event != last_event)654+ process_event(ctx, last_event);655+ }656+657+ last_event = event;658+ }659+660+ return true;661+}662+663 int664 main(int argc, char *const *argv)665 {666 #define OPT_LETTER_SPACING 256993 unmodified lines1660 1661 if ((apps = applications_init()) == NULL)1662 goto out;1663 1664+ matches_set_applications(matches, apps);1665+1666 if (conf.dmenu.enabled) {1667 if (conf.dmenu.exit_immediately_if_empty) {1668 /*1669 * If no_run_if_empty is set, we *must* load the entries1670 * *before displaying the window.1671 */1637- dmenu_load_entries(apps, conf.dmenu.delim, -1);1672+ dmenu_load_entries(apps, conf.dmenu.delim, -1, -1);1673 if (apps->count == 0)1674 goto out;1675 1676 if (conf.icons_enabled) {136 unmodified linesmodified:match.c+50 added-7 removed
301 unmodified lines302 free(matches);303 }304 305 void306+matches_all_applications_loaded(struct matches *matches)307+{308+ matches->all_apps_loaded = true;309+#if defined(_DEBUG)310+ matches_lock(matches);311+ assert(matches->matches_size == matches->applications->count);312+ matches_unlock(matches);313+#endif314+}315+316+void317 matches_set_applications(struct matches *matches,307- const struct application_list *applications)318+ struct application_list *applications)319 {309- assert(matches->applications == NULL);310- assert(matches->matches == NULL);320+ mtx_lock(&applications->lock);321 322+ assert(matches->matches == NULL || matches->matches_size > 0);323+ assert(applications->count >= matches->matches_size);324+325 matches->applications = applications;326+327+ if (matches->matches == NULL && applications->count == 0) {328+ mtx_unlock(&applications->lock);329+ return;330+ }331+313- matches->matches = calloc(314- applications->count, sizeof(matches->matches[0]));332+ matches->matches = reallocarray(333+ matches->matches, applications->count, sizeof(matches->matches[0]));334+335+ const size_t old_size = matches->matches_size;336+ const size_t diff = applications->count - old_size;337+338+ memset(&matches->matches[old_size], 0, diff * sizeof(matches->matches[0]));339+340+ matches->matches_size = applications->count;341+ mtx_unlock(&applications->lock);342 matches_icons_loaded(matches);343 }344 345 void346 matches_icons_loaded(struct matches *matches)347 {321- matches->have_icons = false;348+ if (matches->have_icons)349+ return;350+351+ mtx_lock(&matches->applications->lock);352 for (size_t i = 0; i < matches->applications->count; i++) {323- if (matches->applications->v[i].icon.name != NULL) {353+ if (matches->applications->v[i]->icon.name != NULL) {354 matches->have_icons = true;355 break;356 }357 }358+ mtx_unlock(&matches->applications->lock);359 }360 361 bool362 matches_have_icons(const struct matches *matches)363 {364 return matches->have_icons;365 }366 367+void368+matches_lock(struct matches *matches)369+{370+ mtx_lock(&matches->applications->lock);371+}372+373+void374+matches_unlock(struct matches *matches)375+{376+ mtx_unlock(&matches->applications->lock);377+}378+379 size_t380 matches_max_matches_per_page(const struct matches *matches)381 {382 return matches->max_matches_per_page;904 unmodified linesmodified:meson.build+1 added
154 unmodified lines155 'config.c', 'config.h',156 'char32.c', 'char32.h',157 'debug.c', 'debug.h',158 'dmenu.c', 'dmenu.h',159+ 'event.c', 'event.h',160 'fdm.c', 'fdm.h',161 'icon.c', 'icon.h',162 'key-binding.c', 'key-binding.h',163 'log.c', 'log.h',46 unmodified linesmodified:path.c+5 added-4 removed
24 unmodified lines25 return ;26 27 char *copy = strdup(_path);28 char *ctx = NULL;29- tll(struct application) entries = tll_init();30- 29+ tll(struct application *) entries = tll_init();30+31 for (const char *tok = strtok_r(copy, ":", &ctx);32 tok != NULL;33 tok = strtok_r(NULL, ":", &ctx))34 {23 unmodified lines58 if (wtitle == NULL)59 continue;60 bool already_exist = false;61 tll_foreach(entries, it) {62- if (c32cmp(wtitle, it->item.title) == 0) {62+ if (c32cmp(wtitle, it->item->title) == 0) {63 already_exist = true;64 break;65 }66 };3 unmodified lines70 }71 size_t exec_size = path_size + 1 + strlen(e->d_name) + 1;72 char *exec = malloc(exec_size);73 snprintf(exec, exec_size, "%s/%s", tok, e->d_name);74- struct application app = {74+ struct application *app = malloc(sizeof(*app));75+ *app = (struct application){76 .title = wtitle,77 .exec = exec,78 .visible = true,79 };16 unmodified linesmodified:xdg.c+32 added-30 removed
20 unmodified lines21 #include "log.h"22 #include "char32.h"23 #include "icon.h"24 25-typedef tll(struct application) application_llist_t;25+typedef tll(struct application *) application_llist_t;26 27 struct locale_variants {28 char *lang_country_modifier;29 char *lang_country;422 unmodified lines452 for (size_t i = 0; i < c32len(it->item); i++)453 it->item[i] = toc32lower(it->item[i]);454 }455 456- tll_push_back(457- *applications,458- ((struct application){459- .id = strdup(id),460- .path = a->path,461- .exec = a->exec,462- .app_id = a->app_id,463- .title = title,464- .title_lowercase = title_lowercase,465- .basename = c32dup(file_basename_lowercase),466- .wexec = a->wexec,467- .generic_name = a->generic_name,468- .comment = a->comment,469- .keywords = a->keywords,470- .categories = a->categories,471- .title_len = title_len,472- .basename_len = basename_len,473- .wexec_len = wexec_len,474- .generic_name_len = generic_name_len,475- .comment_len = comment_len,476- .icon = {.name = a->icon},477- .visible = a->visible && (!filter_desktops || filter_desktop_entry(a, desktops)),478- .startup_notify = !a->no_startup_notify,479- .count = 0}));480-456+ struct application *app = malloc(sizeof(*app));457+ *app = (struct application){458+ .id = strdup(id),459+ .path = a->path,460+ .exec = a->exec,461+ .app_id = a->app_id,462+ .title = title,463+ .title_lowercase = title_lowercase,464+ .basename = c32dup(file_basename_lowercase),465+ .wexec = a->wexec,466+ .generic_name = a->generic_name,467+ .comment = a->comment,468+ .keywords = a->keywords,469+ .categories = a->categories,470+ .title_len = title_len,471+ .basename_len = basename_len,472+ .wexec_len = wexec_len,473+ .generic_name_len = generic_name_len,474+ .comment_len = comment_len,475+ .icon = {.name = a->icon},476+ .visible = a->visible && (!filter_desktops || filter_desktop_entry(a, desktops)),477+ .startup_notify = !a->no_startup_notify,478+ .count = 0,479+ };480+ tll_push_back(*applications, app);481 tll_free_and_free(a->onlyshowin, free);482 tll_free_and_free(a->notshowin, free);483 }484 143 unmodified lines628 char *id = new_id(base_id, e->d_name);629 630 bool already_added = false;631 tll_foreach(*applications, it) {632- if (strcmp(it->item.id, id) == 0) {632+ if (strcmp(it->item->id, id) == 0) {633 already_added = true;634 break;635 }636 }19 unmodified lines656 657 static int658 sort_application_by_title(const void *_a, const void *_b)659 {660- const struct application *a = _a;661- const struct application *b = _b;662- return c32cmp(a->title, b->title);660+ const struct application *const *a = _a;661+ const struct application *const *b = _b;662+ return c32casecmp((*a)->title, (*b)->title);663 }664 665 void666 xdg_find_programs(const char *terminal, bool include_actions,13 unmodified lines680 close(fd);681 }682 }683 684+ mtx_lock(&applications->lock);685 applications->count = tll_length(apps);686 applications->v = malloc(tll_length(apps) * sizeof(applications->v[0]));687 688 size_t i = 0;4 unmodified lines693 tll_free(apps);694 695 qsort(applications->v, applications->count, sizeof(applications->v[0]),696 &sort_application_by_title);697+ mtx_unlock(&applications->lock);698 699 xdg_data_dirs_destroy(dirs);700 701 #if defined(_DEBUG) && LOG_ENABLE_DBG && 0143 unmodified lines