dmenu: don’t require stdin to be closed
Before adoption: this commit came in with the history the project adopted, so no landing records it.
While loading dmenu entries, use a select() before calling getline(). This allows us to also select on an “abort” event FD, which main will signal when the user has “executed” a dmenu entry. Closes #106
authored by Daniel Eklöf · committed by Daniel Eklöf · parent 92c590e · Browse files · 4 files·+58 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+58 added−4 removed
- other:+58 added−4 removed
Provenance
- Commit
bd08b63- Tree
52e71bb- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent
92c590e
4 files changed+58 added−4 removed
modified:CHANGELOG.md+3 added
30 unmodified lines31 * libpng warnings are now routed through fuzzel’s logging32 (https://codeberg.org/dnkl/fuzzel/issues/101).33 * Nanosvg is now the default SVG backend. librsvg is still supported,34 and can be used by setting the `-Dsvg-backend=librsvg` meson option.35+* It is no longer necessary to close stdin when using fuzzel in dmenu36+ mode, as long as `--no-run-if-empty` is **not** being used37+ (https://codeberg.org/dnkl/fuzzel/issues/106).38 39 40 ### Deprecated41 ### Removed301 unmodified linesmodified:dmenu.c+34 added-1 removed
4 unmodified lines5 #include <string.h>6 #include <stdio.h>7 #include <errno.h>8 #include <sys/types.h>9+#include <fcntl.h>10+#include <poll.h>11+#include <unistd.h>12 13 #include <tllist.h>14 15 #define LOG_MODULE "dmenu"1 unmodified line17 #include "log.h"18 #include "char32.h"19 20 void18-dmenu_load_entries(struct application_list *applications)21+dmenu_load_entries(struct application_list *applications, int abort_fd)22 {23 tll(char32_t *) entries = tll_init();24 25 char *line = NULL;26 size_t alloc_size = 0;27 28+ int flags;29+ if ((flags = fcntl(STDIN_FILENO, F_GETFL)) < 0 ||30+ fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK) < 0)31+ {32+ LOG_ERRNO("failed to set O_NONBLOCK on stdin");33+ goto out;34+ }35+36 errno = 0;37 while (true) {38+ struct pollfd fds[] = {39+ {.fd = STDIN_FILENO, .events = POLLIN},40+41+ /* Must be last */42+ {.fd = abort_fd, .events = POLLIN},43+ };44+45+ size_t count = sizeof(fds) / sizeof(fds[0]);46+ if (abort_fd < 0)47+ count--;48+49+ int ret = poll(fds, count, -1);50+ if (ret < 0) {51+ LOG_ERRNO("failed to poll stdin");52+ break;53+ }54+55+ if (fds[1].revents & (POLLIN | POLLHUP))56+ break;57+58 ssize_t len = getline(&line, &alloc_size, stdin);59 60 if (len == -1) {61 if (errno != 0)14 unmodified lines76 }77 78 free(line);79 80+out:81+82 applications->v = calloc(tll_length(entries), sizeof(applications->v[0]));83 applications->count = tll_length(entries);84 85 size_t i = 0;35 unmodified linesmodified:dmenu.h+1 added-1 removed
5 unmodified lines6 #include "prompt.h"7 8 enum dmenu_mode { DMENU_MODE_NONE, DMENU_MODE_TEXT, DMENU_MODE_INDEX };9 10-void dmenu_load_entries(struct application_list *applications);10+void dmenu_load_entries(struct application_list *applications, int abort_fd);11 bool dmenu_execute(const struct application *app, ssize_t index,12 const struct prompt *prompt, enum dmenu_mode format);modified:main.c+20 added-2 removed
12 unmodified lines13 #include <sys/mman.h>14 #include <sys/stat.h>15 #include <sys/file.h>16 #include <sys/epoll.h>17+#include <sys/eventfd.h>18 #include <fcntl.h>19 #include <dirent.h>20 21 #include <tllist.h>32 unmodified lines54 bool dmenu_mode;55 } options;56 57 int event_fd;58+ int dmenu_abort_fd;59 };60 61 static struct rgba62 hex_to_rgba(uint32_t color)315 unmodified lines378 bool dmenu_mode = ctx->options.dmenu_mode;379 bool icons_enabled = ctx->options.icons_enabled;380 381 if (dmenu_mode) {380- dmenu_load_entries(apps);382+ dmenu_load_entries(apps, ctx->dmenu_abort_fd);383 return send_event(ctx->event_fd, EVENT_APPS_LOADED);384 }385 386 xdg_find_programs(terminal, actions_enabled, apps);553 unmodified lines940 941 thrd_t app_thread_id;942 bool join_app_thread = false;943 int event_pipe[2] = {-1, -1};944+ int dmenu_abort_fd = -1;945 946 char *lock_file = NULL;947 int file_lock_fd = -1;948 bool unlink_lock_file = true;31 unmodified lines980 /*981 * If no_run_if_empty is set, we *must* load the entries982 * *before displaying the window.983 */981- dmenu_load_entries(apps);984+ dmenu_load_entries(apps, -1);985 if (apps->count == 0)986 goto out;987 988 matches_set_applications(matches, apps);989 matches_update(matches, prompt);990+ } else if (dmenu_mode) {991+ dmenu_abort_fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);992+ if (dmenu_abort_fd < 0) {993+ LOG_ERRNO("failed to create event FD for dmenu mode");994+ goto out;995+ }996 }997 998 struct context ctx = {999 .render = render,9 unmodified lines1009 .actions_enabled = actions_enabled,1010 .dmenu_mode = dmenu_mode,1011 },1012 .event_fd = -1,1013+ .dmenu_abort_fd = dmenu_abort_fd,1014 };1015 1016 if ((wayl = wayl_init(1017 fdm, render, prompt, matches, &render_options,38 unmodified lines1056 ret = wayl_exit_code(wayl);1057 1058 out:1059 if (join_app_thread) {1060+ if (dmenu_abort_fd >= 0) {1061+ if (write(dmenu_abort_fd, &(uint64_t){1}, sizeof(uint64_t)) < 0)1062+ LOG_ERRNO("failed to signal abort");1063+ }1064+1065 int res;1066 thrd_join(app_thread_id, &res);1067 1068 if (res != 0) {11 unmodified lines1080 }1081 if (event_pipe[1] >= 0)1082 close(event_pipe[1]);1083 1084+ if (dmenu_abort_fd >= 0)1085+ close(dmenu_abort_fd);1086+1087 mtx_destroy(&icon_lock);1088 1089 shm_fini();1090 23 unmodified lines