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 fileTake it
1#include "dmenu.h"2#include "column.h"3#include "icon.h"45#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>13#include <uchar.h>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"22#include "event.h"23#include "macros.h"24#include "xmalloc.h"2526void27dmenu_load_entries(struct application_list *applications, char delim,28 int with_nth, int event_fd, int abort_fd)29{30 tll(struct application *) entries = tll_init();3132 size_t size = 0;33 size_t alloc_size = 16384;34 char *buffer = xmalloc(alloc_size);3536 int flags = fcntl(STDIN_FILENO, F_GETFL);37 if (flags < 0 || fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK) < 0) {38 LOG_ERRNO("failed to set O_NONBLOCK on stdin");39 goto out;40 }4142 size_t app_idx = 0;4344 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 };5253 size_t count = ALEN(fds);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 }6263 if (fds[1].revents & (POLLIN | POLLHUP)) {64 LOG_DBG("aborted");65 break;66 }6768 /* 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;74 buffer = xrealloc(buffer, alloc_size);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”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”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 */150 if (extra_len > 6 && memcmp(extra, "\0icon\x1f", 6) == 0)151 icon_name = xstrndup(extra + 6, delim_at - (extra + 6));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 }169170 char32_t *title = with_nth == 0171 ? xc32dup(wline)172 : nth_column(wline, with_nth);173174 char32_t *lowercase = xc32dup(title);175 for (size_t i = 0; i < c32len(lowercase); i++)176 lowercase[i] = toc32lower(lowercase[i]);177178 struct application *app = xmalloc(sizeof(*app));179 *app = (struct application){180 .index = app_idx++,181 .dmenu_input = wline,182 .title = title,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 }191192 if (event_fd >= 0) {193 mtx_lock(&applications->lock);194195 const size_t new_count = applications->count + tll_length(entries);196 if (new_count > 0) {197 applications->v = xreallocarray(198 applications->v, new_count, sizeof(applications->v[0]));199200 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);209 }210211 mtx_unlock(&applications->lock);212 }213 }214215 free(buffer);216217out:218219 if (tll_length(entries) == 0)220 return;221222 mtx_lock(&applications->lock);223224 const size_t new_count = applications->count + tll_length(entries);225 applications->v = xreallocarray(226 applications->v, new_count, sizeof(applications->v[0]));227228 size_t i = applications->count;229 tll_foreach(entries, it) {230 applications->v[i++] = it->item;231 tll_remove(entries, it);232 }233234 applications->count = applications->visible_count = new_count;235 mtx_unlock(&applications->lock);236237 tll_free(entries);238}239240bool241dmenu_execute(const struct application *app, ssize_t index,242 const struct prompt *prompt, enum dmenu_mode format, unsigned int column)243{244 switch (format) {245 case DMENU_MODE_TEXT: {246 const char32_t *output = app != NULL247 ? app->dmenu_input248 : prompt_text(prompt);249250 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);257 if (text != NULL)258 printf("%s\n", text);259260 free(column_output);261 free(text);262 break;263 }264265 case DMENU_MODE_INDEX:266 printf("%zd\n", index);267 break;268 }269270 return true;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 }320}