Skip to content
Myr

dmenu.c

This file

Path

dmenu.c

320 lines · 9.2 KiB · C

Last changed by

dmenu: --with-nth: don't match "other" columnsDaniel Eklöfbefore adoption56526e8

History of this file

Take it

The walk stopped after 200 commits: lines older than c18512f point at it.

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

line 1why, one step back
1#include "dmenu.h"

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

line 2why, one step back
2#include "column.h"

dmenu: add support for fallbacks in icon metadatae-thobefore adoptionb789998

line 3why, one step back
3#include "icon.h"

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 4–12why, one step back
4
5#include <stdlib.h>6#include <stdbool.h>7#include <string.h>8#include <stdio.h>9#include <errno.h>10#include <sys/types.h>11#include <fcntl.h>12#include <poll.h>

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

line 13why, one step back
13#include <uchar.h>

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 14–21why, one step back
14#include <unistd.h>
1516#include <tllist.h>1718#define LOG_MODULE "dmenu"19#define LOG_ENABLE_DBG 020#include "log.h"21#include "char32.h"

dmenu: start rendering input before STDIN has been closedDaniel Eklöfbefore adoptionb69ba92

line 22why, one step back
22#include "event.h"

Add xmalloc.h and xsnprintf.h and use to improve/simplify error handlingCraig Barnesbefore adoption539d514

lines 23–24why, one step back
23#include "macros.h"
24#include "xmalloc.h"

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 25–27why, one step back
25
26void27dmenu_load_entries(struct application_list *applications, char delim,

dmenu: --with-nth: don't match "other" columnsDaniel Eklöfbefore adoption56526e8

line 28why, one step back
28 int with_nth, int event_fd, int abort_fd)

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

line 29why, one step back
29{

dmenu: start rendering input before STDIN has been closedDaniel Eklöfbefore adoptionb69ba92

line 30why, one step back
30 tll(struct application *) entries = tll_init();

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 31–32why, one step back
31
32 size_t size = 0;

dmenu: start rendering input before STDIN has been closedDaniel Eklöfbefore adoptionb69ba92

line 33why, one step back
33 size_t alloc_size = 16384;

Add xmalloc.h and xsnprintf.h and use to improve/simplify error handlingCraig Barnesbefore adoption539d514

line 34why, one step back
34 char *buffer = xmalloc(alloc_size);

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

line 35why, one step back
35

Add xmalloc.h and xsnprintf.h and use to improve/simplify error handlingCraig Barnesbefore adoption539d514

lines 36–37why, one step back
36 int flags = fcntl(STDIN_FILENO, F_GETFL);
37 if (flags < 0 || fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK) < 0) {

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 38–41why, one step back
38 LOG_ERRNO("failed to set O_NONBLOCK on stdin");
39 goto out;40 }41

dmenu: start rendering input before STDIN has been closedDaniel Eklöfbefore adoptionb69ba92

lines 42–43why, one step back
42 size_t app_idx = 0;
43

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 44–52why, one step back
44 errno = 0;
45 while (true) {46 struct pollfd fds[] = {47 {.fd = STDIN_FILENO, .events = POLLIN},4849 /* Must be last */50 {.fd = abort_fd, .events = POLLIN},51 };52

Add xmalloc.h and xsnprintf.h and use to improve/simplify error handlingCraig Barnesbefore adoption539d514

line 53why, one step back
53 size_t count = ALEN(fds);

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 54–62why, one step back
54 if (abort_fd < 0)
55 count--;5657 int ret = poll(fds, count, -1);58 if (ret < 0) {59 LOG_ERRNO("failed to poll stdin");60 break;61 }62

dmenu: start rendering input before STDIN has been closedDaniel Eklöfbefore adoptionb69ba92

line 63why, one step back
63 if (fds[1].revents & (POLLIN | POLLHUP)) {

diagnostics: demote 'aborted' logging to debug level.Mark Stosbergbefore adoption2e9f5f2

line 64why, one step back
64 LOG_DBG("aborted");

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

line 65why, one step back
65 break;

dmenu: start rendering input before STDIN has been closedDaniel Eklöfbefore adoptionb69ba92

line 66why, one step back
66 }

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 67–73why, one step back
67
68 /* Increase size of input buffer, if necessary */69 if (size >= alloc_size - 2) {70 LOG_DBG("increasing input buffer size %zu -> %zu",71 alloc_size, alloc_size * 2);7273 alloc_size *= 2;

Add xmalloc.h and xsnprintf.h and use to improve/simplify error handlingCraig Barnesbefore adoption539d514

line 74why, one step back
74 buffer = xrealloc(buffer, alloc_size);

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 75–132why, one step back
75 }
7677 ssize_t bytes_read = read(78 STDIN_FILENO, &buffer[size], alloc_size - size - 1);7980 if (bytes_read < 0) {81 if (errno == EINTR)82 continue;83 LOG_ERRNO("failed to read from stdin");84 break;85 } else if (bytes_read == 0 && size == 0) {86 /* No more data on stdin, and all buffered data consumed */87 break;88 }8990 size += bytes_read;9192 /*93 * Consume as much as possible from the input buffer94 *95 * Input buffer may contain many lines, resulting in multiple96 * applications being added.97 *98 * Input buffer may not even contain a single line, in which99 * case we can't consume anything at all.100 *101 * Input buffer may contain one or more full lines, followed102 * by a final partial line. If stdin has been closed103 * (i.e. there's no more input to read), then treat the last,104 * partial line as a complete line (i.e. pretend it's followed105 * by a delimiter). But, if stdin is still open, don't consume106 * the last, partial line. Instead, wait for more input.107 */108 while (size > 0) {109 char *delim_at = memchr(buffer, delim, size);110 if (delim_at == NULL) {111 if (bytes_read > 0) {112 /* No delimiter yet, wait for more data */113 LOG_DBG("no delimiter found, waiting for more data...");114 break;115 } else {116 /* No delimiter, but stdin has been closed. Treat117 the last chunk of data as a complete line. */118 LOG_DBG("last line: pointing delim_at to the end of the buffer");119 delim_at = &buffer[size];120 size++;121 }122 }123124 const size_t entry_len = delim_at - buffer;125 *delim_at = '\0';126127 /*128 * Support Rofi’s extended dmenu protocol. One can specify129 * an icon by appending ‘\0icon\x1f<icon-name>’ to the130 * entry:131 *132 * “hello world\0icon\x1ffirefox”

dmenu: add support for fallbacks in icon metadatae-thobefore adoptionb789998

lines 133–138why, one step back
133 *
134 * We also support fallback icons using comma-separated values.135 * When the primary icon is not found, subsequent icons in the136 * list will be tried until one is successfully loaded:137 *138 * “hello world\0icon\x1ffirefox,web-browser,application-x-executable”

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 139–149why, one step back
139 */
140 char *icon_name = NULL;141 const char *extra = memchr(buffer, '\0', entry_len);142143 if (extra != NULL) {144 const size_t extra_len = delim_at - extra;145146 /*147 * 'extra' is "\0icon\x1f" - 6 characters. Require148 * *more* than 6, since icon *name* cannot be empty149 */

Add xmalloc.h and xsnprintf.h and use to improve/simplify error handlingCraig Barnesbefore adoption539d514

lines 150–151why, one step back
150 if (extra_len > 6 && memcmp(extra, "\0icon\x1f", 6) == 0)
151 icon_name = xstrndup(extra + 6, delim_at - (extra + 6));

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 152–169why, one step back
152 }
153154 LOG_DBG("%s (icon=%s)", buffer, icon_name);155156 char32_t *wline = ambstoc32(buffer);157158 /* Consume entry from input buffer */159 const size_t consume = delim_at + 1 - buffer;160 assert(consume <= size);161162 memmove(buffer, delim_at + 1, size - consume);163 size -= consume;164165 if (wline == NULL) {166 free(icon_name);167 continue;168 }169

dmenu: --with-nth: don't match "other" columnsDaniel Eklöfbefore adoption56526e8

lines 170–174why, one step back
170 char32_t *title = with_nth == 0
171 ? xc32dup(wline)172 : nth_column(wline, with_nth);173174 char32_t *lowercase = xc32dup(title);

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 175–177why, one step back
175 for (size_t i = 0; i < c32len(lowercase); i++)
176 lowercase[i] = toc32lower(lowercase[i]);177

Add xmalloc.h and xsnprintf.h and use to improve/simplify error handlingCraig Barnesbefore adoption539d514

line 178why, one step back
178 struct application *app = xmalloc(sizeof(*app));

dmenu: start rendering input before STDIN has been closedDaniel Eklöfbefore adoptionb69ba92

line 179why, one step back
179 *app = (struct application){

match: do matching/filtering using a thread poolDaniel Eklöfbefore adoption0c3be18

line 180why, one step back
180 .index = app_idx++,

dmenu: --with-nth: don't match "other" columnsDaniel Eklöfbefore adoption56526e8

lines 181–182why, one step back
181 .dmenu_input = wline,
182 .title = title,

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 183–190why, one step back
183 .title_lowercase = lowercase,
184 .title_len = c32len(lowercase),185 .icon = {.name = icon_name},186 .visible = true,187 };188189 tll_push_back(entries, app);190 }

dmenu: start rendering input before STDIN has been closedDaniel Eklöfbefore adoptionb69ba92

lines 191–195why, one step back
191
192 if (event_fd >= 0) {193 mtx_lock(&applications->lock);194195 const size_t new_count = applications->count + tll_length(entries);

dmenu: don't try to allocate a zero-length arrayDaniel Eklöfbefore adoption71cbf97

lines 196–198why, one step back
196 if (new_count > 0) {
197 applications->v = xreallocarray(198 applications->v, new_count, sizeof(applications->v[0]));

dmenu: start rendering input before STDIN has been closedDaniel Eklöfbefore adoptionb69ba92

line 199why, one step back
199

dmenu: don't try to allocate a zero-length arrayDaniel Eklöfbefore adoption71cbf97

lines 200–208why, one step back
200 size_t i = applications->count;
201 tll_foreach(entries, it) {202 applications->v[i++] = it->item;203 tll_remove(entries, it);204 }205206 assert(i == new_count);207 applications->count = applications->visible_count = new_count;208 send_event(event_fd, EVENT_APPS_SOME_LOADED);

dmenu: start rendering input before STDIN has been closedDaniel Eklöfbefore adoptionb69ba92

lines 209–212why, one step back
209 }
210211 mtx_unlock(&applications->lock);212 }

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 213–218why, one step back
213 }
214215 free(buffer);216217out:218

dmenu: start rendering input before STDIN has been closedDaniel Eklöfbefore adoptionb69ba92

lines 219–220why, one step back
219 if (tll_length(entries) == 0)
220 return;

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

line 221why, one step back
221

dmenu: start rendering input before STDIN has been closedDaniel Eklöfbefore adoptionb69ba92

lines 222–224why, one step back
222 mtx_lock(&applications->lock);
223224 const size_t new_count = applications->count + tll_length(entries);

Add xmalloc.h and xsnprintf.h and use to improve/simplify error handlingCraig Barnesbefore adoption539d514

line 225why, one step back
225 applications->v = xreallocarray(

dmenu: start rendering input before STDIN has been closedDaniel Eklöfbefore adoptionb69ba92

lines 226–228why, one step back
226 applications->v, new_count, sizeof(applications->v[0]));
227228 size_t i = applications->count;

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 229–232why, one step back
229 tll_foreach(entries, it) {
230 applications->v[i++] = it->item;231 tll_remove(entries, it);232 }

dmenu: start rendering input before STDIN has been closedDaniel Eklöfbefore adoptionb69ba92

line 233why, one step back
233

xdg: don't count NoDisplay=true apps towards the match counter's totalChristian Fillionbefore adoptiondb12816

line 234why, one step back
234 applications->count = applications->visible_count = new_count;

dmenu: start rendering input before STDIN has been closedDaniel Eklöfbefore adoptionb69ba92

line 235why, one step back
235 mtx_unlock(&applications->lock);

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 236–241why, one step back
236
237 tll_free(entries);238}239240bool241dmenu_execute(const struct application *app, ssize_t index,

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

line 242why, one step back
242 const struct prompt *prompt, enum dmenu_mode format, unsigned int column)

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 243–245why, one step back
243{
244 switch (format) {245 case DMENU_MODE_TEXT: {

dmenu: --with-nth: don't match "other" columnsDaniel Eklöfbefore adoption56526e8

lines 246–249why, one step back
246 const char32_t *output = app != NULL
247 ? app->dmenu_input248 : prompt_text(prompt);249

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

lines 250–256why, one step back
250 char32_t *column_output = NULL;
251 if (column > 0) {252 column_output = nth_column(output, column);253 output = column_output;254 }255256 char *text = ac32tombs(output);

dmenu: --with-nth: don't match "other" columnsDaniel Eklöfbefore adoption56526e8

line 257why, one step back
257 if (text != NULL)

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

line 258why, one step back
258 printf("%s\n", text);

dmenu: --with-nth: don't match "other" columnsDaniel Eklöfbefore adoption56526e8

line 259why, one step back
259

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

line 260why, one step back
260 free(column_output);

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

lines 261–270why, one step back
261 free(text);
262 break;263 }264265 case DMENU_MODE_INDEX:266 printf("%zd\n", index);267 break;268 }269270 return true;

dmenu: add support for fallbacks in icon metadatae-thobefore adoptionb789998

lines 271–319why, one step back
271}
272273static void274try_icon_list(struct application *app, icon_theme_list_t themes, int icon_size)275{276 if (app->icon.name == NULL || strchr(app->icon.name, ',') == NULL)277 return;278 279 if (app->icon.type != ICON_NONE) {280 return;281 }282 283 char *icon_list = xstrdup(app->icon.name);284 char *saveptr = NULL;285 286 for (char *icon_name = strtok_r(icon_list, ",", &saveptr);287 icon_name != NULL;288 icon_name = strtok_r(NULL, ",", &saveptr))289 {290 free(app->icon.name);291 app->icon.name = xstrdup(icon_name);292 293 app->icon.type = ICON_NONE;294 app->icon.path = NULL;295 app->icon.svg = NULL;296 app->icon.png = NULL;297 298 struct application_list temp_list = {299 .v = &app,300 .count = 1,301 .visible_count = 1302 };303 304 icon_lookup_application_icons(themes, icon_size, &temp_list);305 306 if (app->icon.type != ICON_NONE)307 break;308 }309 310 free(icon_list);311}312313void314dmenu_try_icon_list(struct application_list *applications, icon_theme_list_t themes, int icon_size)315{316 for (size_t i = 0; i < applications->count; i++) {317 struct application *app = applications->v[i];318 try_icon_list(app, themes, icon_size);319 }

docs: correct with the prev-with-wrap and next-with-wrap docs.Mark Stosbergbefore adoptionc18512f

line 320why, one step back
320}