rofi 2.1.2
dmenu.c
Go to the documentation of this file.
1/*
2 * rofi
3 *
4 * MIT/X11 License
5 * Copyright © 2013-2023 Qball Cow <qball@gmpclient.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
29#define G_LOG_DOMAIN "Modes.DMenu"
30#include "config.h"
31
32#include "display.h"
33#include "helper.h"
34#include "modes/dmenu.h"
35#include "rofi-icon-fetcher.h"
36#include "rofi.h"
37#include "settings.h"
38#include "view.h"
39#include "widgets/textbox.h"
40#include "xrmoptions.h"
41#include <ctype.h>
42#include <errno.h>
43#include <fcntl.h>
44#include <gio/gio.h>
45#include <gio/gunixinputstream.h>
46#include <glib-unix.h>
47#include <stdint.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <strings.h>
52#include <sys/stat.h>
53#include <sys/types.h>
54#include <unistd.h>
55
57
58static int dmenu_mode_init(Mode *sw);
59static int dmenu_token_match(const Mode *sw, rofi_int_matcher **tokens,
60 unsigned int index);
61static cairo_surface_t *
62dmenu_get_icon(const Mode *sw, unsigned int selected_line, unsigned int height);
63static char *dmenu_get_message(const Mode *sw);
64
65static inline unsigned int bitget(uint32_t const *const array,
66 unsigned int index) {
67 uint32_t bit = index % 32;
68 uint32_t val = array[index / 32];
69 return (val >> bit) & 1;
70}
71
72static inline void bittoggle(uint32_t *const array, unsigned int index) {
73 uint32_t bit = index % 32;
74 uint32_t *v = &array[index / 32];
75 *v ^= 1 << bit;
76}
77
78typedef struct {
80 // Separator.
82
83 unsigned int selected_line;
84 char *message;
85 char *format;
87 unsigned int num_urgent_list;
89 unsigned int num_active_list;
90 uint32_t *selected_list;
91 unsigned int num_selected_list;
92 unsigned int do_markup;
93 // List with entries.
96 unsigned int cmd_list_length;
97 unsigned int only_selected;
98 unsigned int selected_count;
99
100 gchar **columns;
102 gboolean multi_select;
103
105 GAsyncQueue *async_queue;
106 gboolean async;
107 FILE *fd_file;
108 int fd;
109 int pipefd[2];
110 int pipefd2[2];
112 gboolean loading;
113
117
120#define BLOCK_LINES_SIZE 2048
126
127static void read_add_block(DmenuModePrivateData *pd, Block **block, char *data,
128 gsize len) {
129
130 if ((*block) == NULL) {
131 (*block) = g_malloc0(sizeof(Block));
132 (*block)->pd = pd;
133 (*block)->length = 0;
134 }
135 gsize data_len = len;
136 // Init.
137 (*block)->values[(*block)->length].icon_fetch_uid = 0;
138 (*block)->values[(*block)->length].icon_fetch_size = 0;
139 (*block)->values[(*block)->length].icon_fetch_scale = 0;
140 (*block)->values[(*block)->length].icon_fallback_index = 0;
141 (*block)->values[(*block)->length].icon_name = NULL;
142 (*block)->values[(*block)->length].meta = NULL;
143 (*block)->values[(*block)->length].info = NULL;
144 (*block)->values[(*block)->length].nonselectable = FALSE;
145 (*block)->values[(*block)->length].permanent = FALSE;
146 char *end = data;
147 while (end < data + len && *end != '\0') {
148 end++;
149 }
150 if (end != data + len) {
151 data_len = end - data;
152 dmenuscript_parse_entry_extras(NULL, &((*block)->values[(*block)->length]),
153 end + 1, len - data_len);
154 }
155 char *utfstr = rofi_force_utf8(data, data_len);
156 (*block)->values[(*block)->length].entry = utfstr;
157
158 (*block)->length++;
159}
160
161static void read_add(DmenuModePrivateData *pd, char *data, gsize len) {
162 gsize data_len = len;
163 if ((pd->cmd_list_length + 2) > pd->cmd_list_real_length) {
164 pd->cmd_list_real_length = MAX(pd->cmd_list_real_length * 2, 512);
165 pd->cmd_list = g_realloc(pd->cmd_list, (pd->cmd_list_real_length) *
166 sizeof(DmenuScriptEntry));
167 }
168 // Init.
173 pd->cmd_list[pd->cmd_list_length].icon_name = NULL;
174 pd->cmd_list[pd->cmd_list_length].display = NULL;
175 pd->cmd_list[pd->cmd_list_length].meta = NULL;
176 pd->cmd_list[pd->cmd_list_length].info = NULL;
177 pd->cmd_list[pd->cmd_list_length].active = FALSE;
178 pd->cmd_list[pd->cmd_list_length].urgent = FALSE;
179 pd->cmd_list[pd->cmd_list_length].nonselectable = FALSE;
180 pd->cmd_list[pd->cmd_list_length].permanent = FALSE;
181 char *end = data;
182 while (end < data + len && *end != '\0') {
183 end++;
184 }
185 if (end != data + len) {
186 data_len = end - data;
188 end + 1, len - data_len);
189 }
190 char *utfstr = rofi_force_utf8(data, data_len);
191 pd->cmd_list[pd->cmd_list_length].entry = utfstr;
192 pd->cmd_list[pd->cmd_list_length + 1].entry = NULL;
193
194 pd->cmd_list_length++;
195}
196
206static gboolean dmenu_async_read_proc(gint fd, GIOCondition condition,
207 gpointer user_data) {
209 char command;
210 // Only interrested in read events.
211 if ((condition & G_IO_IN) != G_IO_IN) {
212 return G_SOURCE_CONTINUE;
213 }
214 // Read the entry from the pipe that was used to signal this action.
215 if (read(fd, &command, 1) == 1) {
216 if (command == 'r') {
217 Block *block = NULL;
218 gboolean changed = FALSE;
219 // Empty out the AsyncQueue (that is thread safe) from all blocks pushed
220 // into it.
221 while ((block = g_async_queue_try_pop(pd->async_queue)) != NULL) {
222
223 if (pd->cmd_list_real_length < (pd->cmd_list_length + block->length)) {
224 pd->cmd_list_real_length = MAX(pd->cmd_list_real_length * 2, 4096);
225 pd->cmd_list = g_realloc(pd->cmd_list, sizeof(DmenuScriptEntry) *
227 }
228 memcpy(&(pd->cmd_list[pd->cmd_list_length]), &(block->values[0]),
229 sizeof(DmenuScriptEntry) * block->length);
230 pd->cmd_list_length += block->length;
231 g_free(block);
232 changed = TRUE;
233 }
234 if (changed) {
236 }
237 } else if (command == 'q') {
238 if (pd->loading) {
240 }
241 }
242 }
243 return G_SOURCE_CONTINUE;
244}
245
246static void read_input_sync(DmenuModePrivateData *pd, unsigned int pre_read) {
247 ssize_t nread = 0;
248 size_t len = 0;
249 char *line = NULL;
250 while (pre_read > 0 &&
251 (nread = getdelim(&line, &len, pd->separator, pd->fd_file)) != -1) {
252 if (line[nread - 1] == pd->separator) {
253 nread--;
254 line[nread] = '\0';
255 }
256 read_add(pd, line, nread);
257 pre_read--;
258 }
259 free(line);
260 return;
261}
262static gpointer read_input_thread(gpointer userdata) {
264 ssize_t nread = 0;
265 ssize_t len = 0;
266 char *line = NULL;
267 Block *block = NULL;
268
269 GTimer *tim = g_timer_new();
270 int fd = pd->fd;
271 while (1) {
272 // Wait for input from the input or from the main thread.
273 fd_set rfds;
274 // We wait for 0.25 seconds, before we flush what we have.
275 struct timeval tv = {.tv_sec = 0, .tv_usec = 250000};
276
277 FD_ZERO(&rfds);
278 FD_SET(fd, &rfds);
279 FD_SET(pd->pipefd[0], &rfds);
280
281 int retval = select(MAX(fd, pd->pipefd[0]) + 1, &rfds, NULL, NULL, &tv);
282 if (retval == -1) {
283 g_warning("select failed, giving up.");
284 break;
285 } else if (retval) {
286 // We get input from the UI thread, this is always an abort.
287 if (FD_ISSET(pd->pipefd[0], &rfds)) {
288 break;
289 }
290 // Input data is available.
291 if (FD_ISSET(fd, &rfds)) {
292 ssize_t readbytes = 0;
293 if ((nread + 1024) > len) {
294 line = g_realloc(line, (len + 2048));
295 len = len + 2048;
296 }
297 readbytes = read(fd, &line[nread], 1023);
298 if (readbytes > 0) {
299 nread += readbytes;
300 line[nread] = '\0';
301 ssize_t i = 0;
302 while (i < nread) {
303 if (line[i] == pd->separator) {
304 line[i] = '\0';
305 read_add_block(pd, &block, line, i);
306 memmove(&line[0], &line[i + 1], nread - (i + 1));
307 nread -= (i + 1);
308 i = 0;
309 if (block) {
310 double elapsed = g_timer_elapsed(tim, NULL);
311 if (elapsed >= 0.1 || block->length == BLOCK_LINES_SIZE) {
312 g_timer_start(tim);
313 g_async_queue_push(pd->async_queue, block);
314 block = NULL;
315 write(pd->pipefd2[1], "r", 1);
316 }
317 }
318 } else {
319 i++;
320 }
321 }
322 } else {
323 // remainder in buffer, then quit.
324 if (nread > 0) {
325 line[nread] = '\0';
326 read_add_block(pd, &block, line, nread);
327 }
328 if (block) {
329 g_timer_start(tim);
330 g_async_queue_push(pd->async_queue, block);
331 block = NULL;
332 write(pd->pipefd2[1], "r", 1);
333 }
334 break;
335 }
336 }
337 } else {
338 // Timeout, pushout remainder data.
339 if (nread > 0) {
340 line[nread] = '\0';
341 read_add_block(pd, &block, line, nread);
342 nread = 0;
343 }
344 if (block) {
345 g_timer_start(tim);
346 g_async_queue_push(pd->async_queue, block);
347 block = NULL;
348 write(pd->pipefd2[1], "r", 1);
349 }
350 }
351 }
352 g_timer_destroy(tim);
353 free(line);
354 write(pd->pipefd2[1], "q", 1);
355 return NULL;
356}
357
358static unsigned int dmenu_mode_get_num_entries(const Mode *sw) {
359 const DmenuModePrivateData *rmpd =
361 unsigned int retv = rmpd->cmd_list_length;
362 return retv;
363}
364
366 const char *input,
367 const unsigned int index,
368 gboolean multi_select) {
369 if (pd->columns == NULL) {
370 if (multi_select) {
371 if (index < pd->num_selected_list &&
372 bitget(pd->selected_list, index) == TRUE) {
373 return g_strdup_printf("%s%s", pd->ballot_selected, input);
374 } else {
375 return g_strdup_printf("%s%s", pd->ballot_unselected, input);
376 }
377 }
378 return g_strdup(input);
379 }
380 char *retv = NULL;
381 char **splitted =
382 g_regex_split_simple(pd->column_separator, input, G_REGEX_CASELESS, 00);
383 uint32_t ns = 0;
384 for (; splitted && splitted[ns]; ns++) {
385 ;
386 }
387 GString *str_retv = g_string_new("");
388
389 if (multi_select) {
390 if (index < pd->num_selected_list &&
391 bitget(pd->selected_list, index) == TRUE) {
392 g_string_append(str_retv, pd->ballot_selected);
393 } else {
394 g_string_append(str_retv, pd->ballot_unselected);
395 }
396 }
397 for (uint32_t i = 0; pd->columns && pd->columns[i]; i++) {
398 unsigned int col_index =
399 (unsigned int)g_ascii_strtoull(pd->columns[i], NULL, 10);
400 if (col_index <= ns && col_index > 0) {
401 if (i == 0) {
402 g_string_append(str_retv, splitted[col_index - 1]);
403 } else {
404 g_string_append_c(str_retv, '\t');
405 g_string_append(str_retv, splitted[col_index - 1]);
406 }
407 }
408 }
409 g_strfreev(splitted);
410 retv = str_retv->str;
411 g_string_free(str_retv, FALSE);
412 return retv;
413}
414
415static inline unsigned int get_index(unsigned int length, int index) {
416 if (index >= 0) {
417 return index;
418 }
419 if (((unsigned int)-index) <= length) {
420 return length + index;
421 }
422 // Out of range.
423 return UINT_MAX;
424}
425
426static char *dmenu_get_completion_data(const Mode *data, unsigned int index) {
427 Mode *sw = (Mode *)data;
430 if (retv[index].display) {
431 return dmenu_format_output_string(pd, retv[index].display, index, FALSE);
432 } else {
433 return dmenu_format_output_string(pd, retv[index].entry, index, FALSE);
434 }
435}
436
437static char *get_display_data(const Mode *data, unsigned int index, int *state,
438 G_GNUC_UNUSED GList **list, int get_entry) {
439 Mode *sw = (Mode *)data;
442 for (unsigned int i = 0; i < pd->num_active_list; i++) {
443 unsigned int start =
445 unsigned int stop = get_index(pd->cmd_list_length, pd->active_list[i].stop);
446 if (index >= start && index <= stop) {
447 *state |= ACTIVE;
448 }
449 }
450 for (unsigned int i = 0; i < pd->num_urgent_list; i++) {
451 unsigned int start =
453 unsigned int stop = get_index(pd->cmd_list_length, pd->urgent_list[i].stop);
454 if (index >= start && index <= stop) {
455 *state |= URGENT;
456 }
457 }
458 if (index < pd->num_selected_list &&
459 bitget(pd->selected_list, index) == TRUE) {
460 *state |= SELECTED;
461 }
462 if (pd->do_markup) {
463 *state |= MARKUP;
464 }
465 if (pd->cmd_list[index].urgent) {
466 *state |= URGENT;
467 }
468 if (pd->cmd_list[index].active) {
469 *state |= ACTIVE;
470 }
471 char *my_retv = NULL;
472 if (retv[index].display) {
473 my_retv = (get_entry ? dmenu_format_output_string(pd, retv[index].display,
474 index, pd->multi_select)
475 : NULL);
476 } else {
477 my_retv = (get_entry ? dmenu_format_output_string(pd, retv[index].entry,
478 index, pd->multi_select)
479 : NULL);
480 }
481 return my_retv;
482}
483
484static void dmenu_mode_free(Mode *sw) {
485 if (mode_get_private_data(sw) == NULL) {
486 return;
487 }
489 if (pd != NULL) {
490
491 for (size_t i = 0; i < pd->cmd_list_length; i++) {
492 if (pd->cmd_list[i].entry) {
493 g_free(pd->cmd_list[i].entry);
494 g_strfreev(pd->cmd_list[i].icon_name);
495 g_free(pd->cmd_list[i].display);
496 g_free(pd->cmd_list[i].meta);
497 g_free(pd->cmd_list[i].info);
498 }
499 }
500 g_free(pd->cmd_list);
501 g_free(pd->urgent_list);
502 g_free(pd->active_list);
503 g_free(pd->selected_list);
504
505 g_free(pd);
506 mode_set_private_data(sw, NULL);
507 }
508}
509
510#include "mode-private.h"
512Mode dmenu_mode = {.name = "dmenu",
513 .cfg_name_key = "display-combi",
514 ._init = dmenu_mode_init,
515 ._get_num_entries = dmenu_mode_get_num_entries,
516 ._result = NULL,
517 ._destroy = dmenu_mode_free,
518 ._token_match = dmenu_token_match,
519 ._get_display_value = get_display_data,
520 ._get_icon = dmenu_get_icon,
521 ._get_completion = dmenu_get_completion_data,
522 ._preprocess_input = NULL,
523 ._get_message = dmenu_get_message,
524 .private_data = NULL,
525 .free = NULL,
526 .display_name = "dmenu",
527 .type = MODE_TYPE_DMENU};
528
529static int dmenu_mode_init(Mode *sw) {
530 if (mode_get_private_data(sw) != NULL) {
531 return TRUE;
532 }
533 mode_set_private_data(sw, g_malloc0(sizeof(DmenuModePrivateData)));
535
536 pd->async = TRUE;
537 pd->multi_select = FALSE;
538
539 // For now these only work in sync mode.
540 if (find_arg("-sync") >= 0 || find_arg("-dump") >= 0 ||
541 find_arg("-select") >= 0 || find_arg("-no-custom") >= 0 ||
542 find_arg("-only-match") >= 0 || config.auto_select ||
543 find_arg("-selected-row") >= 0) {
544 pd->async = FALSE;
545 }
546 if (find_arg("-multi-select") >= 0) {
547 pd->multi_select = TRUE;
548 }
549
550 pd->separator = '\n';
551 pd->selected_line = UINT32_MAX;
552
553 find_arg_str("-mesg", &(pd->message));
554
555 // Input data separator.
556 find_arg_char("-sep", &(pd->separator));
557
558 find_arg_uint("-selected-row", &(pd->selected_line));
559 // By default we print the unescaped line back.
560 pd->format = "s";
561
562 // Allow user to override the output format.
563 find_arg_str("-format", &(pd->format));
564 // Urgent.
565 char *str = NULL;
566 find_arg_str("-u", &str);
567 if (str != NULL) {
568 parse_ranges(str, &(pd->urgent_list), &(pd->num_urgent_list));
569 }
570 // Active
571 str = NULL;
572 find_arg_str("-a", &str);
573 if (str != NULL) {
574 parse_ranges(str, &(pd->active_list), &(pd->num_active_list));
575 }
576
577 // DMENU COMPATIBILITY
578 unsigned int lines = DEFAULT_MENU_LINES;
579 find_arg_uint("-l", &(lines));
580 if (lines != DEFAULT_MENU_LINES) {
582 p->name = g_strdup("lines");
583 p->value.i = lines;
585 GHashTable *table =
586 g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
587 (GDestroyNotify)rofi_theme_property_free);
588
589 g_hash_table_replace(table, p->name, p);
591 g_hash_table_destroy(table);
592 }
593
594 str = NULL;
595 find_arg_str("-window-title", &str);
596 if (str) {
598 }
599
604 if (find_arg("-b") >= 0) {
605 config.location = 6;
606 }
607 /* -i case insensitive */
608 config.case_sensitive = TRUE;
609 if (find_arg("-i") >= 0) {
610 config.case_sensitive = FALSE;
611 }
612 if (pd->async) {
613 pd->fd = STDIN_FILENO;
614 if (find_arg_str("-input", &str)) {
615 char *estr = rofi_expand_path(str);
616 pd->fd = open(str, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
617 if (pd->fd == -1) {
618 char *msg = g_markup_printf_escaped(
619 "Failed to open file: <b>%s</b>:\n\t<i>%s</i>", estr,
620 g_strerror(errno));
621 rofi_view_error_dialog(msg, TRUE);
622 g_free(msg);
623 g_free(estr);
624 return TRUE;
625 }
626 g_free(estr);
627 }
628
629 if (pipe(pd->pipefd) == -1) {
630 g_error("Failed to create pipe");
631 }
632 if (pipe(pd->pipefd2) == -1) {
633 g_error("Failed to create pipe");
634 }
635 pd->wake_source =
636 g_unix_fd_add(pd->pipefd2[0], G_IO_IN, dmenu_async_read_proc, pd);
637 // Create the message passing queue to the UI thread.
638 pd->async_queue = g_async_queue_new();
639 pd->reading_thread =
640 g_thread_new("dmenu-read", (GThreadFunc)read_input_thread, pd);
641 pd->loading = TRUE;
642 } else {
643 pd->fd_file = stdin;
644 str = NULL;
645 if (find_arg_str("-input", &str)) {
646 char *estr = rofi_expand_path(str);
647 pd->fd_file = fopen(str, "r");
648 if (pd->fd_file == NULL) {
649 char *msg = g_markup_printf_escaped(
650 "Failed to open file: <b>%s</b>:\n\t<i>%s</i>", estr,
651 g_strerror(errno));
652 rofi_view_error_dialog(msg, TRUE);
653 g_free(msg);
654 g_free(estr);
655 return TRUE;
656 }
657 g_free(estr);
658 }
659
660 read_input_sync(pd, -1);
661 }
662 gchar *columns = NULL;
663 if (find_arg_str("-display-columns", &columns)) {
664 pd->columns = g_strsplit(columns, ",", 0);
665 pd->column_separator = "\t";
666 find_arg_str("-display-column-separator", &pd->column_separator);
667 }
668 return TRUE;
669}
670
671static int dmenu_token_match(const Mode *sw, rofi_int_matcher **tokens,
672 unsigned int index) {
675
677 char *esc = NULL;
678 if (rmpd->cmd_list[index].permanent == TRUE) {
679 // Always match
680 return 1;
681 }
682
683 if (rmpd->do_markup) {
684 pango_parse_markup(rmpd->cmd_list[index].entry, -1, 0, NULL, &esc, NULL,
685 NULL);
686 } else {
687 esc = rmpd->cmd_list[index].entry;
688 }
689 if (esc) {
690 // int retv = helper_token_match ( tokens, esc );
691 int match = 1;
692 if (tokens) {
693 for (int j = 0; match && tokens[j] != NULL; j++) {
694 rofi_int_matcher *ftokens[2] = {tokens[j], NULL};
695 int test = 0;
696 test = helper_token_match(ftokens, esc);
697 if (test == tokens[j]->invert && rmpd->cmd_list[index].meta) {
698 test = helper_token_match(ftokens, rmpd->cmd_list[index].meta);
699 }
700
701 if (test == 0) {
702 match = 0;
703 }
704 }
705 }
706 if (rmpd->do_markup) {
707 g_free(esc);
708 }
709 return match;
710 }
711 return FALSE;
712}
713static char *dmenu_get_message(const Mode *sw) {
715 if (pd->message) {
716 return g_strdup(pd->message);
717 }
718 return NULL;
719}
720static cairo_surface_t *dmenu_get_icon(const Mode *sw,
721 unsigned int selected_line,
722 unsigned int height) {
724 const guint scale = display_scale();
725
726 g_return_val_if_fail(pd->cmd_list != NULL, NULL);
727 DmenuScriptEntry *dr = &(pd->cmd_list[selected_line]);
728 if (dr->icon_name == NULL) {
729 return NULL;
730 }
731
732 /* A row icon and icon-current-entry ask for the same entry at different
733 * sizes; one cached uid cannot serve both. */
734 cairo_surface_t *surface = NULL;
735 if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height &&
736 dr->icon_fetch_scale == scale) {
737 gboolean query_done =
739
740 if (surface != NULL) {
741 return surface;
742 } else if (query_done) {
744 dr->icon_fetch_uid = 0;
745 } else {
746 return NULL;
747 }
748 }
749
750 char *current_icon = NULL;
751 if (dr->icon_name && dr->icon_fallback_index >= 0) {
752 int icon_count = g_strv_length(dr->icon_name);
753 if (dr->icon_fallback_index < icon_count) {
754 current_icon = dr->icon_name[dr->icon_fallback_index];
755 }
756 }
757 if (current_icon) {
758 dr->icon_fetch_uid = rofi_icon_fetcher_query(current_icon, height);
759 dr->icon_fetch_size = height;
760 dr->icon_fetch_scale = scale;
761 /* The sizes alternate, so returning NULL would blank the icon on every
762 * frame, not just the first. */
764 return surface;
765 }
766
767 dr->icon_fetch_uid = 0;
768 return NULL;
769}
770
772 int retv) {
773
774 if (pd->reading_thread) {
775 // Stop listinig to new messages from reading thread.
776 if (pd->wake_source > 0) {
777 g_source_remove(pd->wake_source);
778 }
779 // signal stop.
780 write(pd->pipefd[1], "q", 1);
781 g_thread_join(pd->reading_thread);
782 pd->reading_thread = NULL;
783 /* empty the queue, remove idle callbacks if still pending. */
784 g_async_queue_lock(pd->async_queue);
785 Block *block = NULL;
786 while ((block = g_async_queue_try_pop_unlocked(pd->async_queue)) != NULL) {
787 g_free(block);
788 }
789 g_async_queue_unlock(pd->async_queue);
790 g_async_queue_unref(pd->async_queue);
791 pd->async_queue = NULL;
792 close(pd->pipefd[0]);
793 close(pd->pipefd[1]);
794 }
795 if (pd->fd_file != NULL) {
796 if (pd->fd_file != stdin) {
797 fclose(pd->fd_file);
798 }
799 }
800 if (retv == FALSE) {
801 rofi_set_return_code(EXIT_FAILURE);
802 } else if (retv >= 10) {
804 } else {
805 rofi_set_return_code(EXIT_SUCCESS);
806 }
808 rofi_view_free(state);
810}
811
812static void dmenu_print_results(DmenuModePrivateData *pd, const char *input) {
813 DmenuScriptEntry *cmd_list = pd->cmd_list;
814 int seen = FALSE;
815 for (unsigned int st = 0; st < pd->num_selected_list; st++) {
816 if (bitget(pd->selected_list, st)) {
817 seen = TRUE;
818 rofi_output_formatted_line(pd->format, cmd_list[st].entry, st, input);
819 }
820 }
821 if (!seen) {
822 const char *cmd = input;
823 if (pd->selected_line != UINT32_MAX) {
824 cmd = cmd_list[pd->selected_line].entry;
825 }
826 if (cmd) {
827 rofi_output_formatted_line(pd->format, cmd, pd->selected_line, input);
828 }
829 }
830}
831
832static void dmenu_finalize(RofiViewState *state) {
833 int retv = FALSE;
836
837 unsigned int cmd_list_length = pd->cmd_list_length;
838 DmenuScriptEntry *cmd_list = pd->cmd_list;
839
840 char *input = g_strdup(rofi_view_get_user_input(state));
842 ;
844 unsigned int next_pos = rofi_view_get_next_position(state);
845 int restart = 0;
846 // Special behavior.
847 if (pd->only_selected) {
851 restart = 1;
852 // Skip if no valid item is selected.
853 if ((mretv & MENU_CANCEL) == MENU_CANCEL) {
854 // In no custom mode we allow canceling.
855 restart = (find_arg("-only-match") >= 0);
856 } else if (pd->selected_line != UINT32_MAX) {
857 if ((mretv & MENU_CUSTOM_ACTION) && pd->multi_select) {
858 restart = TRUE;
859 pd->loading = FALSE;
860 if (pd->num_selected_list != pd->cmd_list_length) {
861 size_t new_length = pd->cmd_list_length / 32 + 1;
862 pd->selected_list =
863 g_realloc(pd->selected_list, sizeof(uint32_t) * (new_length));
864 if (pd->num_selected_list == 0) {
865 memset(pd->selected_list, 0, new_length * sizeof(uint32_t));
866 } else {
867 size_t old_length = pd->num_selected_list / 32 + 1;
868 memset(&pd->selected_list[old_length], 0,
869 (new_length - old_length) * sizeof(uint32_t));
870 }
872 }
873 pd->selected_count +=
874 (bitget(pd->selected_list, pd->selected_line) ? (-1) : (1));
876 // Move to next line.
877 pd->selected_line = MIN(next_pos, cmd_list_length - 1);
878 if (pd->selected_count > 0) {
879 char *str =
880 g_strdup_printf("%u/%u", pd->selected_count, pd->cmd_list_length);
881 rofi_view_set_overlay(state, str);
882 g_free(str);
883 } else {
884 rofi_view_set_overlay(state, NULL);
885 }
886 } else if ((mretv & (MENU_OK | MENU_CUSTOM_COMMAND)) &&
887 cmd_list[pd->selected_line].entry != NULL) {
888 if (cmd_list[pd->selected_line].nonselectable == TRUE) {
889 g_free(input);
890 return;
891 }
892 dmenu_print_results(pd, input);
893 retv = TRUE;
894 if ((mretv & MENU_CUSTOM_COMMAND)) {
895 retv = 10 + (mretv & MENU_LOWER_MASK);
896 }
897 g_free(input);
898 dmenu_finish(pd, state, retv);
899 return;
900 } else {
901 pd->selected_line = next_pos - 1;
902 }
903 }
904 if (!restart) {
905 dmenu_finish(pd, state, retv);
906 } else {
907 // Restart
908 rofi_view_restart(state);
910 }
911 return;
912 }
913 // We normally do not want to restart the loop.
914 restart = FALSE;
915 // Normal mode
916 if ((mretv & MENU_OK) && pd->selected_line != UINT32_MAX &&
917 cmd_list[pd->selected_line].entry != NULL) {
918 // Check if entry is non-selectable.
919 if (cmd_list[pd->selected_line].nonselectable == TRUE) {
920 g_free(input);
921 return;
922 }
923 if ((mretv & MENU_CUSTOM_ACTION) && pd->multi_select) {
924 restart = TRUE;
925 if (pd->num_selected_list != pd->cmd_list_length) {
926 size_t new_length = pd->cmd_list_length / 32 + 1;
927 pd->selected_list =
928 g_realloc(pd->selected_list, sizeof(uint32_t) * (new_length));
929 if (pd->num_selected_list == 0) {
930 memset(pd->selected_list, 0, new_length * sizeof(uint32_t));
931 } else {
932 size_t old_length = pd->num_selected_list / 32 + 1;
933 memset(&pd->selected_list[old_length], 0,
934 (new_length - old_length) * sizeof(uint32_t));
935 }
937 }
938 pd->selected_count +=
939 (bitget(pd->selected_list, pd->selected_line) ? (-1) : (1));
941 // Move to next line.
942 pd->selected_line = MIN(next_pos, cmd_list_length - 1);
943 if (pd->selected_count > 0) {
944 char *str =
945 g_strdup_printf("%u/%u", pd->selected_count, pd->cmd_list_length);
946 rofi_view_set_overlay(state, str);
947 g_free(str);
948 } else {
949 rofi_view_set_overlay(state, NULL);
950 }
951 } else {
952 dmenu_print_results(pd, input);
953 }
954 retv = TRUE;
955 }
956 // Custom input
957 else if ((mretv & (MENU_CUSTOM_INPUT))) {
958 dmenu_print_results(pd, input);
959
960 retv = TRUE;
961 }
962 // Quick switch with entry selected.
963 else if ((mretv & MENU_CUSTOM_COMMAND)) {
964 dmenu_print_results(pd, input);
965
966 restart = FALSE;
967 retv = 10 + (mretv & MENU_LOWER_MASK);
968 }
969 g_free(input);
970 if (restart) {
971 rofi_view_restart(state);
973 } else {
974 dmenu_finish(pd, state, retv);
975 }
976}
977
980 MenuFlags menu_flags = MENU_NORMAL;
982
983 char *input = NULL;
984 unsigned int cmd_list_length = pd->cmd_list_length;
985 DmenuScriptEntry *cmd_list = pd->cmd_list;
986
987 pd->only_selected = FALSE;
988 pd->ballot_selected = "☑ ";
989 pd->ballot_unselected = "☐ ";
990 find_arg_str("-ballot-selected-str", &(pd->ballot_selected));
991 find_arg_str("-ballot-unselected-str", &(pd->ballot_unselected));
992
993 if (find_arg("-markup-rows") >= 0) {
994 pd->do_markup = TRUE;
995 }
996 if (find_arg("-only-match") >= 0 || find_arg("-no-custom") >= 0) {
997 pd->only_selected = TRUE;
998 if (cmd_list_length == 0) {
999 return TRUE;
1000 }
1001 }
1002 if (config.auto_select && cmd_list_length == 1) {
1003 rofi_output_formatted_line(pd->format, cmd_list[0].entry, 0, config.filter);
1004 return TRUE;
1005 }
1006 if (find_arg("-password") >= 0) {
1007 menu_flags |= MENU_PASSWORD;
1008 }
1009 /* copy filter string */
1010 input = g_strdup(config.filter);
1011
1012 char *select = NULL;
1013 find_arg_str("-select", &select);
1014 if (select != NULL) {
1015 rofi_int_matcher **tokens =
1016 helper_tokenize(select, parse_case_sensitivity(select));
1017 unsigned int i = 0;
1018 for (i = 0; i < cmd_list_length; i++) {
1019 if (helper_token_match(tokens, cmd_list[i].entry)) {
1020 pd->selected_line = i;
1021 break;
1022 }
1023 }
1024 helper_tokenize_free(tokens);
1025 }
1026 if (find_arg("-dump") >= 0) {
1027 char *filter = config.filter ? config.filter : "";
1028 rofi_int_matcher **tokens =
1029 helper_tokenize(filter, parse_case_sensitivity(filter));
1030 unsigned int i = 0;
1031 for (i = 0; i < cmd_list_length; i++) {
1032 if (tokens == NULL || helper_token_match(tokens, cmd_list[i].entry)) {
1033 rofi_output_formatted_line(pd->format, cmd_list[i].entry, i,
1034 config.filter);
1035 }
1036 }
1037 helper_tokenize_free(tokens);
1039 g_free(input);
1040 return TRUE;
1041 }
1043 RofiViewState *state =
1044 rofi_view_create(&dmenu_mode, input, menu_flags, dmenu_finalize);
1045
1046 if (find_arg("-keep-right") >= 0) {
1047 rofi_view_ellipsize_listview(state, PANGO_ELLIPSIZE_START);
1048 }
1049 char *ellipsize_mode = NULL;
1050 if (find_arg_str("-ellipsize-mode", &ellipsize_mode) >= 0) {
1051 if (ellipsize_mode) {
1052 if (g_ascii_strcasecmp(ellipsize_mode, "start") == 0) {
1053 rofi_view_ellipsize_listview(state, PANGO_ELLIPSIZE_START);
1054 } else if (g_ascii_strcasecmp(ellipsize_mode, "middle") == 0) {
1055 rofi_view_ellipsize_listview(state, PANGO_ELLIPSIZE_MIDDLE);
1056 } else if (g_ascii_strcasecmp(ellipsize_mode, "end") == 0) {
1057 rofi_view_ellipsize_listview(state, PANGO_ELLIPSIZE_END);
1058 } else {
1059 g_warning("Unrecognized ellipsize mode: '%s'", ellipsize_mode);
1060 }
1061 }
1062 }
1064 rofi_view_set_active(state);
1065 if (pd->loading) {
1066 rofi_view_set_overlay(state, "Loading.. ");
1067 }
1068
1069 return FALSE;
1070}
1071
1073 int is_term = isatty(fileno(stdout));
1075 "-mesg", "[string]",
1076 "Print a small user message under the prompt (uses pango markup)", NULL,
1077 is_term);
1078 print_help_msg("-p", "[string]", "Prompt to display left of entry field",
1079 NULL, is_term);
1080 print_help_msg("-selected-row", "[integer]", "Select row", NULL, is_term);
1081 print_help_msg("-format", "[string]", "Output format string", "s", is_term);
1082 print_help_msg("-u", "[list]", "List of row indexes to mark urgent", NULL,
1083 is_term);
1084 print_help_msg("-a", "[list]", "List of row indexes to mark active", NULL,
1085 is_term);
1086 print_help_msg("-l", "[integer] ", "Number of rows to display", NULL,
1087 is_term);
1088 print_help_msg("-window-title", "[string] ", "Set the dmenu window title",
1089 NULL, is_term);
1090 print_help_msg("-i", "", "Set filter to be case insensitive", NULL, is_term);
1091 print_help_msg("-only-match", "",
1092 "Force selection to be given entry, disallow no match", NULL,
1093 is_term);
1094 print_help_msg("-no-custom", "", "Don't accept custom entry, allow no match",
1095 NULL, is_term);
1096 print_help_msg("-select", "[string]", "Select the first row that matches",
1097 NULL, is_term);
1098 print_help_msg("-password", "",
1099 "Do not show what the user inputs. Show '*' instead.", NULL,
1100 is_term);
1101 print_help_msg("-markup-rows", "",
1102 "Allow and render pango markup as input data.", NULL, is_term);
1103 print_help_msg("-sep", "[char]", "Element separator.", "'\\n'", is_term);
1104 print_help_msg("-input", "[filename]",
1105 "Read input from file instead from standard input.", NULL,
1106 is_term);
1107 print_help_msg("-sync", "",
1108 "Force dmenu to first read all input data, then show dialog.",
1109 NULL, is_term);
1110 print_help_msg("-w", "windowid", "Position over window with X11 windowid.",
1111 NULL, is_term);
1112 print_help_msg("-keep-right", "", "Set ellipsize to end.", NULL, is_term);
1113 print_help_msg("-display-columns", "", "Only show the selected columns", NULL,
1114 is_term);
1115 print_help_msg("-display-column-separator", "\t",
1116 "Separator to use to split columns (regex)", NULL, is_term);
1117 print_help_msg("-ballot-selected-str", "\t",
1118 "When multi-select is enabled prefix this string when element "
1119 "is selected.",
1120 NULL, is_term);
1121 print_help_msg("-ballot-unselected-str", "\t",
1122 "When multi-select is enabled prefix this string when element "
1123 "is not selected.",
1124 NULL, is_term);
1125 print_help_msg("-ellipsize-mode", "end",
1126 "Set ellipsize mode(start | middle | end).", NULL, is_term);
1127}
guint display_scale(void)
Definition display.c:78
static void dmenu_finish(DmenuModePrivateData *pd, RofiViewState *state, int retv)
Definition dmenu.c:771
static void read_add(DmenuModePrivateData *pd, char *data, gsize len)
Definition dmenu.c:161
static cairo_surface_t * dmenu_get_icon(const Mode *sw, unsigned int selected_line, unsigned int height)
Definition dmenu.c:720
Mode dmenu_mode
Definition dmenu.c:512
static char * get_display_data(const Mode *data, unsigned int index, int *state, G_GNUC_UNUSED GList **list, int get_entry)
Definition dmenu.c:437
static gboolean dmenu_async_read_proc(gint fd, GIOCondition condition, gpointer user_data)
Definition dmenu.c:206
static int dmenu_token_match(const Mode *sw, rofi_int_matcher **tokens, unsigned int index)
Definition dmenu.c:671
static unsigned int get_index(unsigned int length, int index)
Definition dmenu.c:415
static void dmenu_mode_free(Mode *sw)
Definition dmenu.c:484
static void read_add_block(DmenuModePrivateData *pd, Block **block, char *data, gsize len)
Definition dmenu.c:127
static char * dmenu_get_message(const Mode *sw)
Definition dmenu.c:713
static gchar * dmenu_format_output_string(const DmenuModePrivateData *pd, const char *input, const unsigned int index, gboolean multi_select)
Definition dmenu.c:365
#define BLOCK_LINES_SIZE
Definition dmenu.c:120
static char * dmenu_get_completion_data(const Mode *data, unsigned int index)
Definition dmenu.c:426
static gpointer read_input_thread(gpointer userdata)
Definition dmenu.c:262
static void dmenu_print_results(DmenuModePrivateData *pd, const char *input)
Definition dmenu.c:812
static void bittoggle(uint32_t *const array, unsigned int index)
Definition dmenu.c:72
static void dmenu_finalize(RofiViewState *state)
Definition dmenu.c:832
static int dmenu_mode_init(Mode *sw)
Definition dmenu.c:529
static void read_input_sync(DmenuModePrivateData *pd, unsigned int pre_read)
Definition dmenu.c:246
static unsigned int dmenu_mode_get_num_entries(const Mode *sw)
Definition dmenu.c:358
static unsigned int bitget(uint32_t const *const array, unsigned int index)
Definition dmenu.c:65
void print_help_msg(const char *option, const char *type, const char *text, const char *def, int isatty)
int dmenu_mode_dialog(void)
Definition dmenu.c:978
void print_dmenu_options(void)
Definition dmenu.c:1072
void parse_ranges(char *input, rofi_range_pair **list, unsigned int *length)
Definition helper.c:1669
rofi_int_matcher ** helper_tokenize(const char *input, int case_sensitive)
Definition helper.c:286
int find_arg_char(const char *const key, char *val)
Definition helper.c:431
void helper_tokenize_free(rofi_int_matcher **tokens)
Definition helper.c:146
void rofi_output_formatted_line(const char *format, const char *string, int selected_line, const char *filter)
Definition helper.c:1708
char * rofi_expand_path(const char *input)
Definition helper.c:784
int find_arg_str(const char *const key, char **val)
Definition helper.c:334
int parse_case_sensitivity(const char *input)
Definition helper.c:1687
int find_arg_uint(const char *const key, unsigned int *val)
Definition helper.c:373
int find_arg(const char *const key)
Definition helper.c:325
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
Definition helper.c:541
char * rofi_force_utf8(const gchar *data, ssize_t length)
Definition helper.c:862
gboolean rofi_icon_fetcher_get_ex(const uint32_t uid, cairo_surface_t **surface)
uint32_t rofi_icon_fetcher_query(const char *name, const int size)
void mode_destroy(Mode *mode)
Definition mode.c:64
int mode_init(Mode *mode)
Definition mode.c:44
struct rofi_mode Mode
Definition mode.h:49
void * mode_get_private_data(const Mode *mode)
Definition mode.c:176
MenuReturn
Definition mode.h:70
void mode_set_private_data(Mode *mode, void *pd)
Definition mode.c:181
@ MENU_CUSTOM_COMMAND
Definition mode.h:84
@ MENU_LOWER_MASK
Definition mode.h:92
@ MENU_CANCEL
Definition mode.h:74
@ MENU_CUSTOM_ACTION
Definition mode.h:90
@ MENU_OK
Definition mode.h:72
@ MENU_CUSTOM_INPUT
Definition mode.h:78
void rofi_set_return_code(int code)
Definition rofi.c:155
@ SELECTED
Definition textbox.h:111
@ URGENT
Definition textbox.h:107
@ ACTIVE
Definition textbox.h:109
@ MARKUP
Definition textbox.h:113
void rofi_view_set_overlay(RofiViewState *state, const char *text)
Definition view.c:2118
Mode * rofi_view_get_mode(RofiViewState *state)
Definition view.c:2092
void rofi_view_reload(void)
Definition view.c:2221
int rofi_view_error_dialog(const char *msg, int markup)
Definition view.c:1980
void rofi_view_set_active(RofiViewState *state)
Definition view.c:347
RofiViewState * rofi_view_get_active(void)
Definition view.c:321
void rofi_view_restart(RofiViewState *state)
Definition view.c:316
MenuFlags
Definition view.h:55
MenuReturn rofi_view_get_return_value(const RofiViewState *state)
Definition view.c:410
const char * rofi_view_get_user_input(const RofiViewState *state)
Definition view.c:439
void rofi_view_set_selected_line(RofiViewState *state, unsigned int selected_line)
Definition view.c:369
void rofi_view_free(RofiViewState *state)
Definition view.c:392
RofiViewState * rofi_view_create(Mode *sw, const char *input, MenuFlags menu_flags, void(*finalize)(RofiViewState *))
Definition view.c:1860
unsigned int rofi_view_get_selected_line(const RofiViewState *state)
Definition view.c:414
unsigned int rofi_view_get_next_position(const RofiViewState *state)
Definition view.c:426
@ MENU_PASSWORD
Definition view.h:59
@ MENU_NORMAL
Definition view.h:57
void rofi_view_ellipsize_listview(RofiViewState *state, PangoEllipsizeMode mode)
Definition view.c:2143
@ MODE_TYPE_DMENU
@ P_INTEGER
Definition rofi-types.h:12
struct rofi_int_matcher_t rofi_int_matcher
void dmenuscript_parse_entry_extras(G_GNUC_UNUSED Mode *sw, DmenuScriptEntry *entry, char *buffer, G_GNUC_UNUSED size_t length)
Definition script.c:89
Settings config
#define DEFAULT_MENU_LINES
Definition settings.h:231
Definition dmenu.c:121
DmenuModePrivateData * pd
Definition dmenu.c:124
DmenuScriptEntry values[BLOCK_LINES_SIZE]
Definition dmenu.c:123
unsigned int length
Definition dmenu.c:122
unsigned int num_urgent_list
Definition dmenu.c:87
uint32_t * selected_list
Definition dmenu.c:90
DmenuScriptEntry * cmd_list
Definition dmenu.c:94
unsigned int cmd_list_real_length
Definition dmenu.c:95
GThread * reading_thread
Definition dmenu.c:104
unsigned int num_selected_list
Definition dmenu.c:91
unsigned int do_markup
Definition dmenu.c:92
unsigned int selected_count
Definition dmenu.c:98
char * ballot_unselected
Definition dmenu.c:115
gchar * column_separator
Definition dmenu.c:101
unsigned int num_active_list
Definition dmenu.c:89
gboolean loading
Definition dmenu.c:112
struct rofi_range_pair * urgent_list
Definition dmenu.c:86
gboolean multi_select
Definition dmenu.c:102
unsigned int cmd_list_length
Definition dmenu.c:96
unsigned int only_selected
Definition dmenu.c:97
GAsyncQueue * async_queue
Definition dmenu.c:105
gchar ** columns
Definition dmenu.c:100
char * ballot_selected
Definition dmenu.c:114
unsigned int selected_line
Definition dmenu.c:83
struct rofi_range_pair * active_list
Definition dmenu.c:88
PropertyValue value
Definition rofi-types.h:293
char * name
Definition rofi-types.h:289
WindowLocation location
Definition settings.h:110
unsigned int auto_select
Definition settings.h:156
char * filter
Definition settings.h:169
unsigned int case_sensitive
Definition settings.h:142
char * display_name
void * private_data
ThemeWidget * rofi_theme_find_or_create_name(ThemeWidget *base, const char *name)
Definition theme.c:83
Property * rofi_theme_property_create(PropertyType type)
Definition theme.c:103
void rofi_theme_property_free(Property *p)
Definition theme.c:205
void rofi_theme_widget_add_properties(ThemeWidget *wid, GHashTable *table)
Definition theme.c:656
ThemeWidget * rofi_theme