Skip to content
Myr

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

4 files+58 added−4 removed

modified:CHANGELOG.md+3 added
30 unmodified lines
31* 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).383940### Deprecated41### Removed
301 unmodified lines
modified:dmenu.c+34 added-1 removed
4 unmodified lines
5#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>1213#include <tllist.h>1415#define LOG_MODULE "dmenu"
1 unmodified line
17#include "log.h"18#include "char32.h"1920void18dmenu_load_entries(struct application_list *applications)21dmenu_load_entries(struct application_list *applications, int abort_fd)22{23 tll(char32_t *) entries = tll_init();2425 char *line = NULL;26 size_t alloc_size = 0;2728 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 }3536 errno = 0;37 while (true) {38 struct pollfd fds[] = {39 {.fd = STDIN_FILENO, .events = POLLIN},4041 /* Must be last */42 {.fd = abort_fd, .events = POLLIN},43 };4445 size_t count = sizeof(fds) / sizeof(fds[0]);46 if (abort_fd < 0)47 count--;4849 int ret = poll(fds, count, -1);50 if (ret < 0) {51 LOG_ERRNO("failed to poll stdin");52 break;53 }5455 if (fds[1].revents & (POLLIN | POLLHUP))56 break;5758 ssize_t len = getline(&line, &alloc_size, stdin);5960 if (len == -1) {61 if (errno != 0)
14 unmodified lines
76 }7778 free(line);7980out:8182 applications->v = calloc(tll_length(entries), sizeof(applications->v[0]));83 applications->count = tll_length(entries);8485 size_t i = 0;
35 unmodified lines
modified:dmenu.h+1 added-1 removed
5 unmodified lines
6#include "prompt.h"78enum dmenu_mode { DMENU_MODE_NONE, DMENU_MODE_TEXT, DMENU_MODE_INDEX };910void dmenu_load_entries(struct application_list *applications);10void dmenu_load_entries(struct application_list *applications, int abort_fd);11bool 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 lines
13#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>2021#include <tllist.h>
32 unmodified lines
54 bool dmenu_mode;55 } options;5657 int event_fd;58 int dmenu_abort_fd;59};6061static struct rgba62hex_to_rgba(uint32_t color)
315 unmodified lines
378 bool dmenu_mode = ctx->options.dmenu_mode;379 bool icons_enabled = ctx->options.icons_enabled;380381 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 }385386 xdg_find_programs(terminal, actions_enabled, apps);
553 unmodified lines
940941 thrd_t app_thread_id;942 bool join_app_thread = false;943 int event_pipe[2] = {-1, -1};944 int dmenu_abort_fd = -1;945946 char *lock_file = NULL;947 int file_lock_fd = -1;948 bool unlink_lock_file = true;
31 unmodified lines
980 /*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;987988 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 }997998 struct context ctx = {999 .render = render,
9 unmodified lines
1009 .actions_enabled = actions_enabled,1010 .dmenu_mode = dmenu_mode,1011 },1012 .event_fd = -1,1013 .dmenu_abort_fd = dmenu_abort_fd,1014 };10151016 if ((wayl = wayl_init(1017 fdm, render, prompt, matches, &render_options,
38 unmodified lines
1056 ret = wayl_exit_code(wayl);10571058out: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 }10641065 int res;1066 thrd_join(app_thread_id, &res);10671068 if (res != 0) {
11 unmodified lines
1080 }1081 if (event_pipe[1] >= 0)1082 close(event_pipe[1]);10831084 if (dmenu_abort_fd >= 0)1085 close(dmenu_abort_fd);10861087 mtx_destroy(&icon_lock);10881089 shm_fini();1090
23 unmodified lines