Skip to content
Myr

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

15 files+273 added−107 removed

modified:application.c+17 added-5 removed
23 unmodified lines
24 return true;2526 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]));2930 if (new_argv == NULL)31 return false;32
279 unmodified lines
312313struct application_list *314applications_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;319320 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 }325326 return ret;327}328329void330applications_destroy(struct application_list *apps)
1 unmodified line
332 if (apps == NULL)333 return;334335 for (size_t i = 0; i < apps->count; i++) {326 struct application *app = &apps->v[i];336 struct application *app = apps->v[i];337338 free(app->id);339 free(app->path);340 free(app->exec);
43 unmodified lines
384 }385 free(app->icon.path);386387 fcft_text_run_destroy(app->shaped);388 free(app);389 }390391 mtx_destroy(&apps->lock);392 free(apps->v);393 free(apps);394}395396void397applications_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 lines
91 const struct application *app, const struct prompt *prompt,92 const char *launch_prefix, const char *xdg_activation_token);9394struct application_list {95 struct application *v;95 struct application **v;96 size_t count;97 mtx_t lock;98};99100struct application_list *applications_init(void);101void applications_destroy(struct application_list *apps);
1 unmodified line
modified:CHANGELOG.md+1 added
57 unmodified lines
58 `--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.6364[344]: https://codeberg.org/dnkl/fuzzel/issues/34465[131]: https://codeberg.org/dnkl/fuzzel/issues/13166[189]: https://codeberg.org/dnkl/fuzzel/issues/189
700 unmodified lines
modified:char32.c+7 added-1 removed
45 unmodified lines
46{47 return wcscmp((const wchar_t *)s1, (const wchar_t *)s2);48}4950int51c32casecmp(const char32_t *s1, const char32_t *s2)52{53 return wcscasecmp((const wchar_t *)s1, (const wchar_t *)s2);54}5556char32_t *57c32cpy(char32_t *dest, const char32_t *src)58{59 return (char32_t *)wcscpy((wchar_t *)dest, (const wchar_t *)src);
150 unmodified lines
210 }211212 *out = U'\0';213208 ret = realloc(ret, (chars + 1) * sizeof(ret[0]));214 ret = reallocarray(ret, chars + 1, sizeof(ret[0]));215 return ret;216217err:218 free(ret);
77 unmodified lines
modified:char32.h+1 added
5 unmodified lines
6#include <stdarg.h>78size_t c32len(const char32_t *s);9int c32cmp(const char32_t *s1, const char32_t *s2);10int c32casecmp(const char32_t *s1, const char32_t *s2);11char32_t *c32cpy(char32_t *dest, const char32_t *src);12char32_t *c32cat(char32_t *dest, const char32_t *src);13char32_t *c32dup(const char32_t *s);14char32_t *c32chr(const char32_t *s, char32_t c);
12 unmodified lines
modified:dmenu.c+42 added-9 removed
14 unmodified lines
15#define LOG_MODULE "dmenu"16#define LOG_ENABLE_DBG 017#include "log.h"18#include "char32.h"19#include "event.h"2021void22dmenu_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();2627 size_t size = 0;27 size_t alloc_size = 256;28 size_t alloc_size = 16384;29 char *buffer = malloc(alloc_size);3031 if (buffer == NULL) {32 LOG_ERRNO("failed to allocate input buffer");
7 unmodified lines
40 LOG_ERRNO("failed to set O_NONBLOCK on stdin");41 goto out;42 }4344 size_t app_idx = 0;4546 errno = 0;47 while (true) {48 struct pollfd fds[] = {49 {.fd = STDIN_FILENO, .events = POLLIN},
11 unmodified lines
61 LOG_ERRNO("failed to poll stdin");62 break;63 }6462 if (fds[1].revents & (POLLIN | POLLHUP))65 if (fds[1].revents & (POLLIN | POLLHUP)) {66 LOG_WARN("aborted");67 break;68 }6970 /* Increase size of input buffer, if necessary */71 if (size >= alloc_size - 2) {72 LOG_DBG("increasing input buffer size %zu -> %zu",
100 unmodified lines
173 char32_t *lowercase = c32dup(wline);174 for (size_t i = 0; i < c32len(lowercase); i++)175 lowercase[i] = toc32lower(lowercase[i]);176172 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 line
184 };185186 tll_push_back(entries, app);187 }188189 if (event_fd >= 0) {190 mtx_lock(&applications->lock);191192 const size_t new_count = applications->count + tll_length(entries);193 applications->v = reallocarray(194 applications->v, new_count, sizeof(applications->v[0]));195196 size_t i = applications->count;197 tll_foreach(entries, it) {198 applications->v[i++] = it->item;199 tll_remove(entries, it);200 }201202 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 }208209 free(buffer);210211out:212188 applications->v = calloc(tll_length(entries), sizeof(applications->v[0]));189 applications->count = tll_length(entries);213 if (tll_length(entries) == 0)214 return;215191 /* Convert linked-list to regular array */192 size_t i = 0;216 mtx_lock(&applications->lock);217218 const size_t new_count = applications->count + tll_length(entries);219 applications->v = reallocarray(220 applications->v, new_count, sizeof(applications->v[0]));221222 size_t i = applications->count;223 tll_foreach(entries, it) {224 applications->v[i++] = it->item;225 tll_remove(entries, it);226 }227228 applications->count = new_count;229 mtx_unlock(&applications->lock);230231 tll_free(entries);232}233234bool
19 unmodified lines
modified:dmenu.h+2 added-1 removed
4 unmodified lines
5#include "application.h"6#include "config.h"7#include "prompt.h"89void dmenu_load_entries(struct application_list *applications, char delim, int abort_fd);9void dmenu_load_entries(10 struct application_list *applications, char delim, int event_fd, int abort_fd);11bool 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"23#include <errno.h>4#include <stdint.h>5#include <unistd.h>67int8send_event(int fd, enum event_type event)9{10 ssize_t bytes = write(fd, &(uint64_t){event}, sizeof(uint64_t));1112 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 once23enum event_type {4 EVENT_APPS_SOME_LOADED,5 EVENT_APPS_ALL_LOADED,6 EVENT_ICONS_LOADED,78 EVENT_INVALID,9};1011int send_event(int fd, enum event_type event);
modified:icon.c+1 added-1 removed
505 unmodified lines
506507 tll(struct icon_data) icons = tll_init();508509 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);512513 if (app->icon.name == NULL)514 continue;
264 unmodified lines
modified:main.c+83 added-48 removed
26 unmodified lines
27#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"4142#define max(x, y) ((x) > (y) ? (x) : (y))4344struct context {
117 unmodified lines
162 fclose(f);163164 /* 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];167168 if ((!dmenu && app->id == NULL) || (dmenu && app->title == NULL)) {169 continue;170 }
57 unmodified lines
228 return;229 }230231 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;234234 if (!apps->v[i].visible)235 if (!apps->v[i]->visible)236 continue;237237 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);241242 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);249250 if (u8_title != NULL) {251 const size_t title_len = strlen(u8_title);
6 unmodified lines
258 }259 }260261 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);264265 if (write(fd, "|", 1) != 1 ||266 write(fd, count_as_str, count_len) != count_len ||
222 unmodified lines
489490 return true;491}492492enum event_type { EVENT_APPS_LOADED = 1, EVENT_ICONS_LOADED = 2 };493494static int495send_event(int fd, enum event_type event)496{497 ssize_t bytes = write(fd, &(uint64_t){event}, sizeof(uint64_t));498499 if (bytes < 0)500 return -errno;501 else if (bytes != (ssize_t)sizeof(uint64_t))502 return 1;503 return 0;504}505506493/* THREAD */494static int495populate_apps(void *_ctx)496{
27 unmodified lines
524 }525526 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 lines
535 read_cache(cache_path, apps, false);536 }537 tll_free_and_free(desktops, free);538553 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;542543 if (icons_enabled) {
20 unmodified lines
564565 return 0;566}567582/*583 * Called when the application list has been populated584 */568static bool586fdm_apps_populated(struct fdm *fdm, int fd, int events, void *data)569process_event(struct context *ctx, enum event_type event)570{588 if (events & EPOLLHUP)589 return false;590591 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 }600601 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;575576 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);581582 if (event == EVENT_APPS_ALL_LOADED)583 matches_all_applications_loaded(matches);584585#define UPDATE_MATCHES 1586587#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);590591 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 }605606 case EVENT_ICONS_LOADED:607 /* Just need to refresh the GUI */608 matches_icons_loaded(matches);
7 unmodified lines
616 wayl_refresh(wayl);617 return true;618}619620/*621 * Called when the application list has been populated622 */623static bool624fdm_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;629630 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 }637638 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 }650651 /* Coalesce multiple, identical events */652 if (last_event != EVENT_INVALID) {653 if (event != last_event)654 process_event(ctx, last_event);655 }656657 last_event = event;658 }659660 return true;661}662663int664main(int argc, char *const *argv)665{666 #define OPT_LETTER_SPACING 256
993 unmodified lines
16601661 if ((apps = applications_init()) == NULL)1662 goto out;16631664 matches_set_applications(matches, apps);16651666 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;16751676 if (conf.icons_enabled) {
136 unmodified lines
modified:match.c+50 added-7 removed
301 unmodified lines
302 free(matches);303}304305void306matches_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}315316void317matches_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);321322 assert(matches->matches == NULL || matches->matches_size > 0);323 assert(applications->count >= matches->matches_size);324325 matches->applications = applications;326327 if (matches->matches == NULL && applications->count == 0) {328 mtx_unlock(&applications->lock);329 return;330 }331313 matches->matches = calloc(314 applications->count, sizeof(matches->matches[0]));332 matches->matches = reallocarray(333 matches->matches, applications->count, sizeof(matches->matches[0]));334335 const size_t old_size = matches->matches_size;336 const size_t diff = applications->count - old_size;337338 memset(&matches->matches[old_size], 0, diff * sizeof(matches->matches[0]));339340 matches->matches_size = applications->count;341 mtx_unlock(&applications->lock);342 matches_icons_loaded(matches);343}344345void346matches_icons_loaded(struct matches *matches)347{321 matches->have_icons = false;348 if (matches->have_icons)349 return;350351 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}360361bool362matches_have_icons(const struct matches *matches)363{364 return matches->have_icons;365}366367void368matches_lock(struct matches *matches)369{370 mtx_lock(&matches->applications->lock);371}372373void374matches_unlock(struct matches *matches)375{376 mtx_unlock(&matches->applications->lock);377}378379size_t380matches_max_matches_per_page(const struct matches *matches)381{382 return matches->max_matches_per_page;
904 unmodified lines
modified:meson.build+1 added
154 unmodified lines
155 '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 lines
modified:path.c+5 added-4 removed
24 unmodified lines
25 return ;2627 char *copy = strdup(_path);28 char *ctx = NULL;29 tll(struct application) entries = tll_init();30 29 tll(struct application *) entries = tll_init();3031 for (const char *tok = strtok_r(copy, ":", &ctx);32 tok != NULL;33 tok = strtok_r(NULL, ":", &ctx))34 {
23 unmodified lines
58 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 lines
70 }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 lines
modified:xdg.c+32 added-30 removed
20 unmodified lines
21#include "log.h"22#include "char32.h"23#include "icon.h"2425typedef tll(struct application) application_llist_t;25typedef tll(struct application *) application_llist_t;2627struct locale_variants {28 char *lang_country_modifier;29 char *lang_country;
422 unmodified lines
452 for (size_t i = 0; i < c32len(it->item); i++)453 it->item[i] = toc32lower(it->item[i]);454 }455456 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}));480456 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 lines
628 char *id = new_id(base_id, e->d_name);629630 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 lines
656657static int658sort_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}664665void666xdg_find_programs(const char *terminal, bool include_actions,
13 unmodified lines
680 close(fd);681 }682 }683684 mtx_lock(&applications->lock);685 applications->count = tll_length(apps);686 applications->v = malloc(tll_length(apps) * sizeof(applications->v[0]));687688 size_t i = 0;
4 unmodified lines
693 tll_free(apps);694695 qsort(applications->v, applications->count, sizeof(applications->v[0]),696 &sort_application_by_title);697 mtx_unlock(&applications->lock);698699 xdg_data_dirs_destroy(dirs);700701#if defined(_DEBUG) && LOG_ENABLE_DBG && 0
143 unmodified lines