rofi 1.7.7
drun.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
28#define G_LOG_DOMAIN "Modes.DRun"
29#include "config.h"
31#include "glib.h"
32
33#ifdef ENABLE_DRUN
34#include <limits.h>
35#include <stdio.h>
36#include <stdlib.h>
37
38#include <dirent.h>
39#include <errno.h>
40#include <limits.h>
41#include <signal.h>
42#include <string.h>
43#include <strings.h>
44#include <sys/stat.h>
45#include <sys/types.h>
46#include <unistd.h>
47
48#include <gio/gio.h>
49
50#include "helper.h"
51#include "history.h"
52#include "mode-private.h"
53#include "modes/drun.h"
54#include "modes/filebrowser.h"
55#include "rofi.h"
56#include "settings.h"
57#include "timings.h"
58#include "widgets/textbox.h"
59#include "xcb.h"
60
61#include "rofi-icon-fetcher.h"
62
64#define DRUN_CACHE_FILE "rofi3.druncache"
65
67#define DRUN_DESKTOP_CACHE_FILE "rofi-drun-desktop.cache"
68
70char *DRUN_GROUP_NAME = "Desktop Entry";
71
75typedef struct _DRunModePrivateData DRunModePrivateData;
76
80typedef enum {
82 DRUN_DESKTOP_ENTRY_TYPE_UNDETERMINED = 0,
84 DRUN_DESKTOP_ENTRY_TYPE_APPLICATION,
86 DRUN_DESKTOP_ENTRY_TYPE_LINK,
88 DRUN_DESKTOP_ENTRY_TYPE_SERVICE,
90 DRUN_DESKTOP_ENTRY_TYPE_DIRECTORY,
91} DRunDesktopEntryType;
92
97typedef struct {
98 DRunModePrivateData *pd;
99 /* category */
100 char *action;
101 /* Root */
102 char *root;
103 /* Path to desktop file */
104 char *path;
105 /* Application id (.desktop filename) */
106 char *app_id;
107 /* Desktop id */
108 char *desktop_id;
109 /* Icon stuff */
110 char *icon_name;
111 /* Icon size is used to indicate what size is requested by the
112 * gui. secondary it indicates if the request for a lookup has
113 * been issued (0 not issued )
114 */
115 int icon_size;
116 /* Surface holding the icon. */
117 cairo_surface_t *icon;
118 /* Executable - for Application entries only */
119 char *exec;
120 /* Name of the Entry */
121 char *name;
122 /* Generic Name */
123 char *generic_name;
124 /* Categories */
125 char **categories;
126 /* Keywords */
127 char **keywords;
128 /* Comments */
129 char *comment;
130 /* Url */
131 char *url;
132 /* Underlying key-file. */
133 GKeyFile *key_file;
134 /* Used for sorting. */
135 gint sort_index;
136 /* UID for the icon to display */
137 uint32_t icon_fetch_uid;
138 uint32_t icon_fetch_size;
139 /* Type of desktop file */
140 DRunDesktopEntryType type;
141} DRunModeEntry;
142
143typedef struct {
144 const char *entry_field_name;
145 gboolean enabled_match;
146 gboolean enabled_display;
147} DRunEntryField;
148
150typedef enum {
152 DRUN_MATCH_FIELD_NAME,
154 DRUN_MATCH_FIELD_GENERIC,
156 DRUN_MATCH_FIELD_EXEC,
158 DRUN_MATCH_FIELD_CATEGORIES,
160 DRUN_MATCH_FIELD_KEYWORDS,
162 DRUN_MATCH_FIELD_COMMENT,
164 DRUN_MATCH_FIELD_URL,
166 DRUN_MATCH_NUM_FIELDS,
167} DRunMatchingFields;
168
171static DRunEntryField matching_entry_fields[DRUN_MATCH_NUM_FIELDS] = {
172 {
173 .entry_field_name = "name",
174 .enabled_match = TRUE,
175 .enabled_display = TRUE,
176 },
177 {
178 .entry_field_name = "generic",
179 .enabled_match = TRUE,
180 .enabled_display = TRUE,
181 },
182 {
183 .entry_field_name = "exec",
184 .enabled_match = TRUE,
185 .enabled_display = TRUE,
186 },
187 {
188 .entry_field_name = "categories",
189 .enabled_match = TRUE,
190 .enabled_display = TRUE,
191 },
192 {
193 .entry_field_name = "keywords",
194 .enabled_match = TRUE,
195 .enabled_display = TRUE,
196 },
197 {
198 .entry_field_name = "comment",
199 .enabled_match = FALSE,
200 .enabled_display = FALSE,
201 },
202 {
203 .entry_field_name = "url",
204 .enabled_match = FALSE,
205 .enabled_display = FALSE,
206 }};
207
208struct _DRunModePrivateData {
209 DRunModeEntry *entry_list;
210 unsigned int cmd_list_length;
211 unsigned int cmd_list_length_actual;
212 // List of disabled entries.
213 GHashTable *disabled_entries;
214 unsigned int disabled_entries_length;
215 unsigned int expected_line_height;
216
217 char **show_categories;
218
219 // Theme
220 const gchar *icon_theme;
221 // DE
222 gchar **current_desktop_list;
223
224 gboolean file_complete;
225 Mode *completer;
226 char *old_completer_input;
227 uint32_t selected_line;
228 char *old_input;
229};
230
231struct RegexEvalArg {
232 DRunModeEntry *e;
233 const char *path;
234 gboolean success;
235};
236
237static gboolean drun_helper_eval_cb(const GMatchInfo *info, GString *res,
238 gpointer data) {
239 // TODO quoting is not right? Find description not very clear, need to check.
240 struct RegexEvalArg *e = (struct RegexEvalArg *)data;
241
242 gchar *match;
243 // Get the match
244 match = g_match_info_fetch(info, 0);
245 if (match != NULL) {
246 switch (match[1]) {
247 case 'f':
248 case 'F':
249 case 'u':
250 case 'U':
251 if (e->path) {
252 g_string_append(res, e->path);
253 }
254 break;
255 // Unsupported
256 case 'i':
257 // TODO
258 if (e->e && e->e->icon) {
259 g_string_append_printf(res, "--icon %s", e->e->icon_name);
260 }
261 break;
262 // Deprecated
263 case 'd':
264 case 'D':
265 case 'n':
266 case 'N':
267 case 'v':
268 case 'm':
269 break;
270 case '%':
271 g_string_append(res, "%");
272 break;
273 case 'k':
274 if (e->e->path) {
275 char *esc = g_shell_quote(e->e->path);
276 g_string_append(res, esc);
277 g_free(esc);
278 }
279 break;
280 case 'c':
281 if (e->e->name) {
282 char *esc = g_shell_quote(e->e->name);
283 g_string_append(res, esc);
284 g_free(esc);
285 }
286 break;
287 // Invalid, this entry should not be processed -> throw error.
288 default:
289 e->success = FALSE;
290 g_free(match);
291 return TRUE;
292 }
293 g_free(match);
294 }
295 // Continue replacement.
296 return FALSE;
297}
298static void launch_link_entry(DRunModeEntry *e) {
299 if (e->key_file == NULL) {
300 GKeyFile *kf = g_key_file_new();
301 GError *error = NULL;
302 gboolean res = g_key_file_load_from_file(kf, e->path, 0, &error);
303 if (res) {
304 e->key_file = kf;
305 } else {
306 g_warning("[%s] [%s] Failed to parse desktop file because: %s.",
307 e->app_id, e->path, error->message);
308 g_error_free(error);
309 g_key_file_free(kf);
310 return;
311 }
312 }
313
314 gchar *url = g_key_file_get_string(e->key_file, e->action, "URL", NULL);
315 if (url == NULL || strlen(url) == 0) {
316 g_warning("[%s] [%s] No URL found.", e->app_id, e->path);
317 g_free(url);
318 return;
319 }
320
321 gsize command_len = strlen(config.drun_url_launcher) + strlen(url) +
322 2; // space + terminator = 2
323 gchar *command = g_newa(gchar, command_len);
324 g_snprintf(command, command_len, "%s %s", config.drun_url_launcher, url);
325 g_free(url);
326
327 g_debug("Link launch command: |%s|", command);
328 if (helper_execute_command(NULL, command, FALSE, NULL)) {
329 char *path = g_build_filename(cache_dir, DRUN_CACHE_FILE, NULL);
330 // Store it based on the unique identifiers (desktop_id).
331 history_set(path, e->desktop_id);
332 g_free(path);
333 }
334}
335static gchar *app_path_for_id(const gchar *app_id) {
336 gchar *path;
337 gint i;
338
339 path = g_strconcat("/", app_id, NULL);
340 for (i = 0; path[i]; i++) {
341 if (path[i] == '.')
342 path[i] = '/';
343 if (path[i] == '-')
344 path[i] = '_';
345 }
346
347 return path;
348}
349static GVariant *app_get_platform_data(void) {
350 GVariantBuilder builder;
351 const gchar *startup_id;
352
353 g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
354
355 if ((startup_id = g_getenv("DESKTOP_STARTUP_ID")))
356 g_variant_builder_add(&builder, "{sv}", "desktop-startup-id",
357 g_variant_new_string(startup_id));
358
359 if ((startup_id = g_getenv("XDG_ACTIVATION_TOKEN")))
360 g_variant_builder_add(&builder, "{sv}", "activation-token",
361 g_variant_new_string(startup_id));
362
363 return g_variant_builder_end(&builder);
364}
365
366static gboolean exec_dbus_entry(DRunModeEntry *e, const char *path) {
367 GVariantBuilder files;
368 GDBusConnection *session;
369 GError *error = NULL;
370 gchar *object_path;
371 GVariant *result;
372 GVariant *params = NULL;
373 const char *method = "Activate";
374 g_debug("Trying to launch desktop file using dbus activation.");
375
376 session = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
377 if (!session) {
378 g_warning("unable to connect to D-Bus: %s\n", error->message);
379 g_error_free(error);
380 return FALSE;
381 }
382
383 object_path = app_path_for_id(e->app_id);
384
385 g_variant_builder_init(&files, G_VARIANT_TYPE_STRING_ARRAY);
386
387 if (path != NULL) {
388 method = "Open";
389 params = g_variant_new("(as@a{sv})", &files, app_get_platform_data());
390 } else {
391 params = g_variant_new("(@a{sv})", app_get_platform_data());
392 }
393 if (path) {
394 GFile *file = g_file_new_for_commandline_arg(path);
395 g_variant_builder_add_value(
396 &files, g_variant_new_take_string(g_file_get_uri(file)));
397 g_object_unref(file);
398 }
399 result = g_dbus_connection_call_sync(
400 session, e->app_id, object_path, "org.freedesktop.Application", method,
401 params, G_VARIANT_TYPE_UNIT, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
402
403 g_free(object_path);
404
405 if (result) {
406 g_variant_unref(result);
407 } else {
408 g_warning("error sending %s message to application: %s\n", "Open",
409 error->message);
410 g_error_free(error);
411 g_object_unref(session);
412 return FALSE;
413 }
414 g_object_unref(session);
415 return TRUE;
416}
417
418static void exec_cmd_entry(DRunModeEntry *e, const char *path) {
419 GError *error = NULL;
420 GRegex *reg = g_regex_new("%[a-zA-Z%]", 0, 0, &error);
421 if (error != NULL) {
422 g_warning("Internal error, failed to create regex: %s.", error->message);
423 g_error_free(error);
424 return;
425 }
426 struct RegexEvalArg earg = {.e = e, .path = path, .success = TRUE};
427 char *str = g_regex_replace_eval(reg, e->exec, -1, 0, 0, drun_helper_eval_cb,
428 &earg, &error);
429 if (error != NULL) {
430 g_warning("Internal error, failed replace field codes: %s.",
431 error->message);
432 g_error_free(error);
433 return;
434 }
435 g_regex_unref(reg);
436 if (earg.success == FALSE) {
437 g_warning("Invalid field code in Exec line: %s.", e->exec);
438 ;
439 return;
440 }
441 if (str == NULL) {
442 g_warning("Nothing to execute after processing: %s.", e->exec);
443 ;
444 return;
445 }
446 g_debug("Parsed command: |%s| into |%s|.", e->exec, str);
447
448 if (e->key_file == NULL) {
449 GKeyFile *kf = g_key_file_new();
450 GError *key_error = NULL;
451 gboolean res = g_key_file_load_from_file(kf, e->path, 0, &key_error);
452 if (res) {
453 e->key_file = kf;
454 } else {
455 g_warning("[%s] [%s] Failed to parse desktop file because: %s.",
456 e->app_id, e->path, key_error->message);
457 g_error_free(key_error);
458 g_key_file_free(kf);
459
460 return;
461 }
462 }
463
464 const gchar *fp = g_strstrip(str);
465 gchar *exec_path =
466 g_key_file_get_string(e->key_file, e->action, "Path", NULL);
467 if (exec_path != NULL && strlen(exec_path) == 0) {
468 // If it is empty, ignore this property. (#529)
469 g_free(exec_path);
470 exec_path = NULL;
471 }
472
473 RofiHelperExecuteContext context = {
474 .name = e->name,
475 .icon = e->icon_name,
476 .app_id = e->app_id,
477 };
478 gboolean sn =
479 g_key_file_get_boolean(e->key_file, e->action, "StartupNotify", NULL);
480 gchar *wmclass = NULL;
481 if (sn &&
482 g_key_file_has_key(e->key_file, e->action, "StartupWMClass", NULL)) {
483 context.wmclass = wmclass =
484 g_key_file_get_string(e->key_file, e->action, "StartupWMClass", NULL);
485 }
486
490 gboolean launched = FALSE;
491 if (g_key_file_get_boolean(e->key_file, e->action, "DBusActivatable", NULL)) {
492 launched = exec_dbus_entry(e, path);
493 }
494 if (launched == FALSE) {
496
497 // Returns false if not found, if key not found, we don't want run in
498 // terminal.
499 gboolean terminal =
500 g_key_file_get_boolean(e->key_file, e->action, "Terminal", NULL);
501 if (helper_execute_command(exec_path, fp, terminal, sn ? &context : NULL)) {
502 char *drun_cach_path = g_build_filename(cache_dir, DRUN_CACHE_FILE, NULL);
503 // Store it based on the unique identifiers (desktop_id).
504 history_set(drun_cach_path, e->desktop_id);
505 g_free(drun_cach_path);
506 }
507 }
508 g_free(wmclass);
509 g_free(exec_path);
510 g_free(str);
511}
512
513static gboolean rofi_strv_contains(const char *const *categories,
514 const char *const *field) {
515 for (int i = 0; categories && categories[i]; i++) {
516 for (int j = 0; field[j]; j++) {
517 if (g_str_equal(categories[i], field[j])) {
518 return TRUE;
519 }
520 }
521 }
522 return FALSE;
523}
527static void read_desktop_file(DRunModePrivateData *pd, const char *root,
528 const char *path, const gchar *basename,
529 const char *action) {
530 DRunDesktopEntryType desktop_entry_type =
531 DRUN_DESKTOP_ENTRY_TYPE_UNDETERMINED;
532 int parse_action = (config.drun_show_actions && action != DRUN_GROUP_NAME);
533 // Create ID on stack.
534 // We know strlen (path ) > strlen(root)+1
535 const ssize_t id_len = strlen(path) - strlen(root);
536 char id[id_len];
537 g_strlcpy(id, &(path[strlen(root) + 1]), id_len);
538 for (int index = 0; index < id_len; index++) {
539 if (id[index] == '/') {
540 id[index] = '-';
541 }
542 }
543
544 // Check if item is on disabled list.
545 if (g_hash_table_contains(pd->disabled_entries, id) && !parse_action) {
546 g_debug("[%s] [%s] Skipping, was previously seen.", id, path);
547 return;
548 }
549 GKeyFile *kf = g_key_file_new();
550 GError *error = NULL;
551 gboolean res = g_key_file_load_from_file(kf, path, 0, &error);
552 // If error, skip to next entry
553 if (!res) {
554 g_debug("[%s] [%s] Failed to parse desktop file because: %s.", id, path,
555 error->message);
556 g_error_free(error);
557 g_key_file_free(kf);
558 return;
559 }
560
561 if (g_key_file_has_group(kf, action) == FALSE) {
562 // No type? ignore.
563 g_debug("[%s] [%s] Invalid desktop file: No %s group", id, path, action);
564 g_key_file_free(kf);
565 return;
566 }
567 // Skip non Application entries.
568 gchar *key = g_key_file_get_string(kf, DRUN_GROUP_NAME, "Type", NULL);
569 if (key == NULL) {
570 // No type? ignore.
571 g_debug("[%s] [%s] Invalid desktop file: No type indicated", id, path);
572 g_key_file_free(kf);
573 return;
574 }
575 if (!g_strcmp0(key, "Application")) {
576 desktop_entry_type = DRUN_DESKTOP_ENTRY_TYPE_APPLICATION;
577 } else if (!g_strcmp0(key, "Link")) {
578 desktop_entry_type = DRUN_DESKTOP_ENTRY_TYPE_LINK;
579 } else if (!g_strcmp0(key, "Service")) {
580 desktop_entry_type = DRUN_DESKTOP_ENTRY_TYPE_SERVICE;
581 g_debug("Service file detected.");
582 } else {
583 g_debug(
584 "[%s] [%s] Skipping desktop file: Not of type Application or Link (%s)",
585 id, path, key);
586 g_free(key);
587 g_key_file_free(kf);
588 return;
589 }
590 g_free(key);
591
592 // Name key is required.
593 if (!g_key_file_has_key(kf, DRUN_GROUP_NAME, "Name", NULL)) {
594 g_debug("[%s] [%s] Invalid desktop file: no 'Name' key present.", id, path);
595 g_key_file_free(kf);
596 return;
597 }
598
599 // Skip hidden entries.
600 if (g_key_file_get_boolean(kf, DRUN_GROUP_NAME, "Hidden", NULL)) {
601 g_debug(
602 "[%s] [%s] Adding desktop file to disabled list: 'Hidden' key is true",
603 id, path);
604 g_key_file_free(kf);
605 g_hash_table_add(pd->disabled_entries, g_strdup(id));
606 return;
607 }
608 if (pd->current_desktop_list) {
609 gboolean show = TRUE;
610 // If the DE is set, check the keys.
611 if (g_key_file_has_key(kf, DRUN_GROUP_NAME, "OnlyShowIn", NULL)) {
612 gsize llength = 0;
613 show = FALSE;
614 gchar **list = g_key_file_get_string_list(kf, DRUN_GROUP_NAME,
615 "OnlyShowIn", &llength, NULL);
616 if (list) {
617 for (gsize lcd = 0; !show && pd->current_desktop_list[lcd]; lcd++) {
618 for (gsize lle = 0; !show && lle < llength; lle++) {
619 show = (g_strcmp0(pd->current_desktop_list[lcd], list[lle]) == 0);
620 }
621 }
622 g_strfreev(list);
623 }
624 }
625 if (show && g_key_file_has_key(kf, DRUN_GROUP_NAME, "NotShowIn", NULL)) {
626 gsize llength = 0;
627 gchar **list = g_key_file_get_string_list(kf, DRUN_GROUP_NAME,
628 "NotShowIn", &llength, NULL);
629 if (list) {
630 for (gsize lcd = 0; show && pd->current_desktop_list[lcd]; lcd++) {
631 for (gsize lle = 0; show && lle < llength; lle++) {
632 show = !(g_strcmp0(pd->current_desktop_list[lcd], list[lle]) == 0);
633 }
634 }
635 g_strfreev(list);
636 }
637 }
638
639 if (!show) {
640 g_debug("[%s] [%s] Adding desktop file to disabled list: "
641 "'OnlyShowIn'/'NotShowIn' keys don't match current desktop",
642 id, path);
643 g_key_file_free(kf);
644 g_hash_table_add(pd->disabled_entries, g_strdup(id));
645 return;
646 }
647 }
648 // Skip entries that have NoDisplay set.
649 if (g_key_file_get_boolean(kf, DRUN_GROUP_NAME, "NoDisplay", NULL)) {
650 g_debug("[%s] [%s] Adding desktop file to disabled list: 'NoDisplay' key "
651 "is true",
652 id, path);
653 g_key_file_free(kf);
654 g_hash_table_add(pd->disabled_entries, g_strdup(id));
655 return;
656 }
657
658 // We need Exec, don't support DBusActivatable
659 if (desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_APPLICATION &&
660 !g_key_file_has_key(kf, DRUN_GROUP_NAME, "Exec", NULL)) {
661 g_debug("[%s] [%s] Unsupported desktop file: no 'Exec' key present for "
662 "type Application.",
663 id, path);
664 g_key_file_free(kf);
665 return;
666 }
667 if (desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_SERVICE &&
668 !g_key_file_has_key(kf, DRUN_GROUP_NAME, "Exec", NULL)) {
669 g_debug("[%s] [%s] Unsupported desktop file: no 'Exec' key present for "
670 "type Service.",
671 id, path);
672 g_key_file_free(kf);
673 return;
674 }
675 if (desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_LINK &&
676 !g_key_file_has_key(kf, DRUN_GROUP_NAME, "URL", NULL)) {
677 g_debug("[%s] [%s] Unsupported desktop file: no 'URL' key present for type "
678 "Link.",
679 id, path);
680 g_key_file_free(kf);
681 return;
682 }
683
684 if (g_key_file_has_key(kf, DRUN_GROUP_NAME, "TryExec", NULL)) {
685 char *te = g_key_file_get_string(kf, DRUN_GROUP_NAME, "TryExec", NULL);
686 if (!g_path_is_absolute(te)) {
687 char *fp = g_find_program_in_path(te);
688 if (fp == NULL) {
689 g_free(te);
690 g_key_file_free(kf);
691 return;
692 }
693 g_free(fp);
694 } else {
695 if (g_file_test(te, G_FILE_TEST_IS_EXECUTABLE) == FALSE) {
696 g_free(te);
697 g_key_file_free(kf);
698 return;
699 }
700 }
701 g_free(te);
702 }
703
704 char **categories = NULL;
705 if (pd->show_categories) {
706 categories = g_key_file_get_locale_string_list(
707 kf, DRUN_GROUP_NAME, "Categories", NULL, NULL, NULL);
708 if (!rofi_strv_contains((const char *const *)categories,
709 (const char *const *)pd->show_categories)) {
710 g_strfreev(categories);
711 g_key_file_free(kf);
712 return;
713 }
714 }
715
716 size_t nl = ((pd->cmd_list_length) + 1);
717 if (nl >= pd->cmd_list_length_actual) {
718 pd->cmd_list_length_actual += 256;
719 pd->entry_list = g_realloc(pd->entry_list, pd->cmd_list_length_actual *
720 sizeof(*(pd->entry_list)));
721 }
722 // Make sure order is preserved, this will break when cmd_list_length is
723 // bigger then INT_MAX. This is not likely to happen.
724 if (G_UNLIKELY(pd->cmd_list_length > INT_MAX)) {
725 // Default to smallest value.
726 pd->entry_list[pd->cmd_list_length].sort_index = INT_MIN;
727 } else {
728 pd->entry_list[pd->cmd_list_length].sort_index = -nl;
729 }
730 pd->entry_list[pd->cmd_list_length].icon_size = 0;
731 pd->entry_list[pd->cmd_list_length].icon_fetch_uid = 0;
732 pd->entry_list[pd->cmd_list_length].icon_fetch_size = 0;
733 pd->entry_list[pd->cmd_list_length].root = g_strdup(root);
734 pd->entry_list[pd->cmd_list_length].path = g_strdup(path);
735 pd->entry_list[pd->cmd_list_length].desktop_id = g_strdup(id);
736 pd->entry_list[pd->cmd_list_length].app_id =
737 g_strndup(basename, strlen(basename) - strlen(".desktop"));
738 gchar *n =
739 g_key_file_get_locale_string(kf, DRUN_GROUP_NAME, "Name", NULL, NULL);
740
741 if (action != DRUN_GROUP_NAME) {
742 gchar *na = g_key_file_get_locale_string(kf, action, "Name", NULL, NULL);
743 gchar *l = g_strdup_printf("%s - %s", n, na);
744 g_free(n);
745 n = l;
746 }
747 pd->entry_list[pd->cmd_list_length].name = n;
748 pd->entry_list[pd->cmd_list_length].action = DRUN_GROUP_NAME;
749 gchar *gn = g_key_file_get_locale_string(kf, DRUN_GROUP_NAME, "GenericName",
750 NULL, NULL);
751 pd->entry_list[pd->cmd_list_length].generic_name = gn;
752 if (matching_entry_fields[DRUN_MATCH_FIELD_KEYWORDS].enabled_match ||
753 matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_display) {
754 pd->entry_list[pd->cmd_list_length].keywords =
755 g_key_file_get_locale_string_list(kf, DRUN_GROUP_NAME, "Keywords", NULL,
756 NULL, NULL);
757 } else {
758 pd->entry_list[pd->cmd_list_length].keywords = NULL;
759 }
760
761 if (matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_match ||
762 matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_display) {
763 if (categories) {
764 pd->entry_list[pd->cmd_list_length].categories = categories;
765 categories = NULL;
766 } else {
767 pd->entry_list[pd->cmd_list_length].categories =
768 g_key_file_get_locale_string_list(kf, DRUN_GROUP_NAME, "Categories",
769 NULL, NULL, NULL);
770 }
771 } else {
772 pd->entry_list[pd->cmd_list_length].categories = NULL;
773 }
774 g_strfreev(categories);
775
776 pd->entry_list[pd->cmd_list_length].type = desktop_entry_type;
777 if (desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_APPLICATION ||
778 desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_SERVICE) {
779 pd->entry_list[pd->cmd_list_length].exec =
780 g_key_file_get_string(kf, action, "Exec", NULL);
781 } else {
782 pd->entry_list[pd->cmd_list_length].exec = NULL;
783 }
784
785 if (matching_entry_fields[DRUN_MATCH_FIELD_COMMENT].enabled_match ||
786 matching_entry_fields[DRUN_MATCH_FIELD_COMMENT].enabled_display) {
787 pd->entry_list[pd->cmd_list_length].comment = g_key_file_get_locale_string(
788 kf, DRUN_GROUP_NAME, "Comment", NULL, NULL);
789 } else {
790 pd->entry_list[pd->cmd_list_length].comment = NULL;
791 }
792 if (matching_entry_fields[DRUN_MATCH_FIELD_URL].enabled_match ||
793 matching_entry_fields[DRUN_MATCH_FIELD_URL].enabled_display) {
794 pd->entry_list[pd->cmd_list_length].url =
795 g_key_file_get_locale_string(kf, DRUN_GROUP_NAME, "URL", NULL, NULL);
796 } else {
797 pd->entry_list[pd->cmd_list_length].url = NULL;
798 }
799 pd->entry_list[pd->cmd_list_length].icon_name =
800 g_key_file_get_locale_string(kf, DRUN_GROUP_NAME, "Icon", NULL, NULL);
801 pd->entry_list[pd->cmd_list_length].icon = NULL;
802
803 // Keep keyfile around.
804 pd->entry_list[pd->cmd_list_length].key_file = kf;
805 // We don't want to parse items with this id anymore.
806 g_hash_table_add(pd->disabled_entries, g_strdup(id));
807 g_debug("[%s] Using file %s.", id, path);
808 (pd->cmd_list_length)++;
809
810 if (!parse_action) {
811 gsize actions_length = 0;
812 char **actions = g_key_file_get_string_list(kf, DRUN_GROUP_NAME, "Actions",
813 &actions_length, NULL);
814 for (gsize iter = 0; iter < actions_length; iter++) {
815 char *new_action = g_strdup_printf("Desktop Action %s", actions[iter]);
816 read_desktop_file(pd, root, path, basename, new_action);
817 g_free(new_action);
818 }
819 g_strfreev(actions);
820 }
821 return;
822}
823
827static void walk_dir(DRunModePrivateData *pd, const char *root,
828 const char *dirname, const gboolean recursive) {
829 DIR *dir;
830
831 g_debug("Checking directory %s for desktop files.", dirname);
832 dir = opendir(dirname);
833 if (dir == NULL) {
834 return;
835 }
836
837 struct dirent *file;
838 gchar *filename = NULL;
839 struct stat st;
840 while ((file = readdir(dir)) != NULL) {
841 if (file->d_name[0] == '.') {
842 continue;
843 }
844 switch (file->d_type) {
845 case DT_LNK:
846 case DT_REG:
847 case DT_DIR:
848 case DT_UNKNOWN:
849 filename = g_build_filename(dirname, file->d_name, NULL);
850 break;
851 default:
852 continue;
853 }
854
855 // On a link, or if FS does not support providing this information
856 // Fallback to stat method.
857 if (file->d_type == DT_LNK || file->d_type == DT_UNKNOWN) {
858 file->d_type = DT_UNKNOWN;
859 if (stat(filename, &st) == 0) {
860 if (S_ISDIR(st.st_mode)) {
861 file->d_type = DT_DIR;
862 } else if (S_ISREG(st.st_mode)) {
863 file->d_type = DT_REG;
864 }
865 }
866 }
867
868 switch (file->d_type) {
869 case DT_REG:
870 // Skip files not ending on .desktop.
871 if (g_str_has_suffix(file->d_name, ".desktop")) {
872 read_desktop_file(pd, root, filename, file->d_name, DRUN_GROUP_NAME);
873 }
874 break;
875 case DT_DIR:
876 if (recursive) {
877 walk_dir(pd, root, filename, recursive);
878 }
879 break;
880 default:
881 break;
882 }
883 g_free(filename);
884 }
885 closedir(dir);
886}
892static void delete_entry_history(const DRunModeEntry *entry) {
893 char *path = g_build_filename(cache_dir, DRUN_CACHE_FILE, NULL);
894 history_remove(path, entry->desktop_id);
895 g_free(path);
896}
897
898static void get_apps_history(DRunModePrivateData *pd) {
899 TICK_N("Start drun history");
900 unsigned int length = 0;
901 gchar *path = g_build_filename(cache_dir, DRUN_CACHE_FILE, NULL);
902 gchar **retv = history_get_list(path, &length);
903 for (unsigned int index = 0; index < length; index++) {
904 for (size_t i = 0; i < pd->cmd_list_length; i++) {
905 if (g_strcmp0(pd->entry_list[i].desktop_id, retv[index]) == 0) {
906 unsigned int sort_index = length - index;
907 if (G_LIKELY(sort_index < INT_MAX)) {
908 pd->entry_list[i].sort_index = sort_index;
909 } else {
910 // This won't sort right anymore, but never gonna hit it anyway.
911 pd->entry_list[i].sort_index = INT_MAX;
912 }
913 }
914 }
915 }
916 g_strfreev(retv);
917 g_free(path);
918 TICK_N("Stop drun history");
919}
920
921static gint drun_int_sort_list(gconstpointer a, gconstpointer b,
922 G_GNUC_UNUSED gpointer user_data) {
923 DRunModeEntry *da = (DRunModeEntry *)a;
924 DRunModeEntry *db = (DRunModeEntry *)b;
925
926 if (da->sort_index < 0 && db->sort_index < 0) {
927 if (da->name == NULL && db->name == NULL) {
928 return 0;
929 }
930 if (da->name == NULL) {
931 return -1;
932 }
933 if (db->name == NULL) {
934 return 1;
935 }
936 return g_utf8_collate(da->name, db->name);
937 }
938 return db->sort_index - da->sort_index;
939}
940
941/*******************************************
942 * Cache voodoo *
943 *******************************************/
944
946#define CACHE_VERSION 3
947static void drun_write_str(FILE *fd, const char *str) {
948 size_t l = (str == NULL ? 0 : strlen(str));
949 fwrite(&l, sizeof(l), 1, fd);
950 // Only write string if it is not NULL or empty.
951 if (l > 0) {
952 // Also writeout terminating '\0'
953 fwrite(str, 1, l + 1, fd);
954 }
955}
956static void drun_write_integer(FILE *fd, int32_t val) {
957 fwrite(&val, sizeof(val), 1, fd);
958}
959static void drun_read_integer(FILE *fd, int32_t *type) {
960 if (fread(type, sizeof(int32_t), 1, fd) != 1) {
961 g_warning("Failed to read entry, cache corrupt?");
962 return;
963 }
964}
965static void drun_read_string(FILE *fd, char **str) {
966 size_t l = 0;
967
968 if (fread(&l, sizeof(l), 1, fd) != 1) {
969 g_warning("Failed to read entry, cache corrupt?");
970 return;
971 }
972 (*str) = NULL;
973 if (l > 0) {
974 // Include \0
975 l++;
976 (*str) = g_malloc(l);
977 if (fread((*str), 1, l, fd) != l) {
978 g_warning("Failed to read entry, cache corrupt?");
979 }
980 }
981}
982static void drun_write_strv(FILE *fd, char **str) {
983 guint vl = (str == NULL ? 0 : g_strv_length(str));
984 fwrite(&vl, sizeof(vl), 1, fd);
985 for (guint index = 0; index < vl; index++) {
986 drun_write_str(fd, str[index]);
987 }
988}
989static void drun_read_stringv(FILE *fd, char ***str) {
990 guint vl = 0;
991 (*str) = NULL;
992 if (fread(&vl, sizeof(vl), 1, fd) != 1) {
993 g_warning("Failed to read entry, cache corrupt?");
994 return;
995 }
996 if (vl > 0) {
997 // Include terminating NULL entry.
998 (*str) = g_malloc0((vl + 1) * sizeof(**str));
999 for (guint index = 0; index < vl; index++) {
1000 drun_read_string(fd, &((*str)[index]));
1001 }
1002 }
1003}
1004
1005static void write_cache(DRunModePrivateData *pd, const char *cache_file) {
1006 if (cache_file == NULL || config.drun_use_desktop_cache == FALSE) {
1007 return;
1008 }
1009 TICK_N("DRUN Write CACHE: start");
1010
1011 FILE *fd = fopen(cache_file, "w");
1012 if (fd == NULL) {
1013 g_warning("Failed to write to cache file");
1014 return;
1015 }
1016 uint8_t version = CACHE_VERSION;
1017 fwrite(&version, sizeof(version), 1, fd);
1018
1019 fwrite(&(pd->cmd_list_length), sizeof(pd->cmd_list_length), 1, fd);
1020 for (unsigned int index = 0; index < pd->cmd_list_length; index++) {
1021 DRunModeEntry *entry = &(pd->entry_list[index]);
1022
1023 drun_write_str(fd, entry->action);
1024 drun_write_str(fd, entry->root);
1025 drun_write_str(fd, entry->path);
1026 drun_write_str(fd, entry->app_id);
1027 drun_write_str(fd, entry->desktop_id);
1028 drun_write_str(fd, entry->icon_name);
1029 drun_write_str(fd, entry->exec);
1030 drun_write_str(fd, entry->name);
1031 drun_write_str(fd, entry->generic_name);
1032
1033 drun_write_strv(fd, entry->categories);
1034 drun_write_strv(fd, entry->keywords);
1035
1036 drun_write_str(fd, entry->comment);
1037 drun_write_str(fd, entry->url);
1038 drun_write_integer(fd, (int32_t)entry->type);
1039 }
1040
1041 fclose(fd);
1042 TICK_N("DRUN Write CACHE: end");
1043}
1044
1048static gboolean drun_read_cache(DRunModePrivateData *pd,
1049 const char *cache_file) {
1050 if (cache_file == NULL || config.drun_use_desktop_cache == FALSE) {
1051 return TRUE;
1052 }
1053
1054 if (config.drun_reload_desktop_cache) {
1055 return TRUE;
1056 }
1057 TICK_N("DRUN Read CACHE: start");
1058 FILE *fd = fopen(cache_file, "r");
1059 if (fd == NULL) {
1060 TICK_N("DRUN Read CACHE: stop");
1061 return TRUE;
1062 }
1063
1064 // Read version.
1065 uint8_t version = 0;
1066
1067 if (fread(&version, sizeof(version), 1, fd) != 1) {
1068 fclose(fd);
1069 g_warning("Cache corrupt, ignoring.");
1070 TICK_N("DRUN Read CACHE: stop");
1071 return TRUE;
1072 }
1073
1074 if (version != CACHE_VERSION) {
1075 fclose(fd);
1076 g_warning("Cache file wrong version, ignoring.");
1077 TICK_N("DRUN Read CACHE: stop");
1078 return TRUE;
1079 }
1080
1081 if (fread(&(pd->cmd_list_length), sizeof(pd->cmd_list_length), 1, fd) != 1) {
1082 fclose(fd);
1083 g_warning("Cache corrupt, ignoring.");
1084 TICK_N("DRUN Read CACHE: stop");
1085 return TRUE;
1086 }
1087 // set actual length to length;
1088 pd->cmd_list_length_actual = pd->cmd_list_length;
1089
1090 pd->entry_list =
1091 g_malloc0(pd->cmd_list_length_actual * sizeof(*(pd->entry_list)));
1092
1093 for (unsigned int index = 0; index < pd->cmd_list_length; index++) {
1094 DRunModeEntry *entry = &(pd->entry_list[index]);
1095
1096 drun_read_string(fd, &(entry->action));
1097 drun_read_string(fd, &(entry->root));
1098 drun_read_string(fd, &(entry->path));
1099 drun_read_string(fd, &(entry->app_id));
1100 drun_read_string(fd, &(entry->desktop_id));
1101 drun_read_string(fd, &(entry->icon_name));
1102 drun_read_string(fd, &(entry->exec));
1103 drun_read_string(fd, &(entry->name));
1104 drun_read_string(fd, &(entry->generic_name));
1105
1106 drun_read_stringv(fd, &(entry->categories));
1107 drun_read_stringv(fd, &(entry->keywords));
1108
1109 drun_read_string(fd, &(entry->comment));
1110 drun_read_string(fd, &(entry->url));
1111 int32_t type = 0;
1112 drun_read_integer(fd, &(type));
1113 entry->type = type;
1114 }
1115
1116 fclose(fd);
1117 TICK_N("DRUN Read CACHE: stop");
1118 return FALSE;
1119}
1120
1121static void get_apps(DRunModePrivateData *pd) {
1122 char *cache_file = g_build_filename(cache_dir, DRUN_DESKTOP_CACHE_FILE, NULL);
1123 TICK_N("Get Desktop apps (start)");
1124 if (drun_read_cache(pd, cache_file)) {
1125 ThemeWidget *wid = rofi_config_find_widget(drun_mode.name, NULL, TRUE);
1126
1128 Property *p =
1129 rofi_theme_find_property(wid, P_BOOLEAN, "scan-desktop", FALSE);
1130 if (p != NULL && (p->type == P_BOOLEAN && p->value.b)) {
1131 const gchar *dir;
1132 // First read the user directory.
1133 dir = g_get_user_special_dir(G_USER_DIRECTORY_DESKTOP);
1134 walk_dir(pd, dir, dir, FALSE);
1135 TICK_N("Get Desktop dir apps");
1136 }
1138 p = rofi_theme_find_property(wid, P_BOOLEAN, "parse-user", TRUE);
1139 if (p == NULL || (p->type == P_BOOLEAN && p->value.b)) {
1140 gchar *dir;
1141 // First read the user directory.
1142 dir = g_build_filename(g_get_user_data_dir(), "applications", NULL);
1143 walk_dir(pd, dir, dir, TRUE);
1144 g_free(dir);
1145 TICK_N("Get Desktop apps (user dir)");
1146 }
1147
1149 p = rofi_theme_find_property(wid, P_BOOLEAN, "parse-system", TRUE);
1150 if (p == NULL || (p->type == P_BOOLEAN && p->value.b)) {
1151 // Then read thee system data dirs.
1152 const gchar *const *sys = g_get_system_data_dirs();
1153 for (const gchar *const *iter = sys; *iter != NULL; ++iter) {
1154 gboolean unique = TRUE;
1155 // Stupid duplicate detection, better then walking dir.
1156 for (const gchar *const *iterd = sys; iterd != iter; ++iterd) {
1157 if (g_strcmp0(*iter, *iterd) == 0) {
1158 unique = FALSE;
1159 }
1160 }
1161 // Check, we seem to be getting empty string...
1162 if (unique && (**iter) != '\0') {
1163 char *dir = g_build_filename(*iter, "applications", NULL);
1164 walk_dir(pd, dir, dir, TRUE);
1165 g_free(dir);
1166 }
1167 }
1168 TICK_N("Get Desktop apps (system dirs)");
1169 }
1170 get_apps_history(pd);
1171
1172 g_qsort_with_data(pd->entry_list, pd->cmd_list_length,
1173 sizeof(DRunModeEntry), drun_int_sort_list, NULL);
1174
1175 TICK_N("Sorting done.");
1176
1177 write_cache(pd, cache_file);
1178 }
1179 g_free(cache_file);
1180}
1181
1182static void drun_mode_parse_entry_fields(void) {
1183 char *savept = NULL;
1184 // Make a copy, as strtok will modify it.
1185 char *switcher_str = g_strdup(config.drun_match_fields);
1186 const char *const sep = ",#";
1187 // Split token on ','. This modifies switcher_str.
1188 for (unsigned int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++) {
1189 matching_entry_fields[i].enabled_match = FALSE;
1190 matching_entry_fields[i].enabled_display = FALSE;
1191 }
1192 for (char *token = strtok_r(switcher_str, sep, &savept); token != NULL;
1193 token = strtok_r(NULL, sep, &savept)) {
1194 if (strcmp(token, "all") == 0) {
1195 for (unsigned int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++) {
1196 matching_entry_fields[i].enabled_match = TRUE;
1197 matching_entry_fields[i].enabled_display = TRUE;
1198 }
1199 break;
1200 }
1201 gboolean matched = FALSE;
1202 for (unsigned int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++) {
1203 const char *entry_name = matching_entry_fields[i].entry_field_name;
1204 if (g_ascii_strcasecmp(token, entry_name) == 0) {
1205 matching_entry_fields[i].enabled_match = TRUE;
1206 matching_entry_fields[i].enabled_display = TRUE;
1207 matched = TRUE;
1208 }
1209 }
1210 if (!matched) {
1211 g_warning("Invalid entry name :%s", token);
1212 }
1213 }
1214 // Free string that was modified by strtok_r
1215 g_free(switcher_str);
1216}
1217
1218static void drun_mode_parse_display_format(void) {
1219 for (int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++) {
1220 if (matching_entry_fields[i].enabled_display)
1221 continue;
1222
1223 gchar *search_term =
1224 g_strdup_printf("{%s}", matching_entry_fields[i].entry_field_name);
1225 if (strstr(config.drun_display_format, search_term)) {
1226 matching_entry_fields[i].enabled_match = TRUE;
1227 }
1228 g_free(search_term);
1229 }
1230}
1231
1232static int drun_mode_init(Mode *sw) {
1233 if (mode_get_private_data(sw) != NULL) {
1234 return TRUE;
1235 }
1236 DRunModePrivateData *pd = g_malloc0(sizeof(*pd));
1237 pd->disabled_entries =
1238 g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
1239 mode_set_private_data(sw, (void *)pd);
1240 // current desktop
1241 const char *current_desktop = g_getenv("XDG_CURRENT_DESKTOP");
1242 pd->current_desktop_list =
1243 current_desktop ? g_strsplit(current_desktop, ":", 0) : NULL;
1244
1245 if (config.drun_categories && *(config.drun_categories)) {
1246 pd->show_categories = g_strsplit(config.drun_categories, ",", 0);
1247 }
1248
1249 drun_mode_parse_entry_fields();
1250 drun_mode_parse_display_format();
1251 get_apps(pd);
1252
1253 pd->completer = NULL;
1254 return TRUE;
1255}
1256static void drun_entry_clear(DRunModeEntry *e) {
1257 g_free(e->root);
1258 g_free(e->path);
1259 g_free(e->app_id);
1260 g_free(e->desktop_id);
1261 if (e->icon != NULL) {
1262 cairo_surface_destroy(e->icon);
1263 }
1264 g_free(e->icon_name);
1265 g_free(e->exec);
1266 g_free(e->name);
1267 g_free(e->generic_name);
1268 g_free(e->comment);
1269 if (e->action != DRUN_GROUP_NAME) {
1270 g_free(e->action);
1271 }
1272 g_strfreev(e->categories);
1273 g_strfreev(e->keywords);
1274 if (e->key_file) {
1275 g_key_file_free(e->key_file);
1276 }
1277}
1278
1279static ModeMode drun_mode_result(Mode *sw, int mretv, char **input,
1280 unsigned int selected_line) {
1281 DRunModePrivateData *rmpd = (DRunModePrivateData *)mode_get_private_data(sw);
1282 ModeMode retv = MODE_EXIT;
1283
1284 if (rmpd->file_complete == TRUE) {
1285
1286 retv = RELOAD_DIALOG;
1287
1288 if ((mretv & (MENU_COMPLETE))) {
1289 g_free(rmpd->old_completer_input);
1290 rmpd->old_completer_input = *input;
1291 *input = NULL;
1292 if (rmpd->selected_line < rmpd->cmd_list_length) {
1293 (*input) = g_strdup(rmpd->old_input);
1294 }
1295 rmpd->file_complete = FALSE;
1296 } else if ((mretv & MENU_CANCEL)) {
1297 retv = MODE_EXIT;
1298 } else {
1299 char *path = NULL;
1300 retv = mode_completer_result(rmpd->completer, mretv, input, selected_line,
1301 &path);
1302 if (retv == MODE_EXIT) {
1303 exec_cmd_entry(&(rmpd->entry_list[rmpd->selected_line]), path);
1304 }
1305 g_free(path);
1306 }
1307 return retv;
1308 }
1309 if ((mretv & MENU_OK)) {
1310 switch (rmpd->entry_list[selected_line].type) {
1311 case DRUN_DESKTOP_ENTRY_TYPE_SERVICE:
1312 case DRUN_DESKTOP_ENTRY_TYPE_APPLICATION:
1313 exec_cmd_entry(&(rmpd->entry_list[selected_line]), NULL);
1314 break;
1315 case DRUN_DESKTOP_ENTRY_TYPE_LINK:
1316 launch_link_entry(&(rmpd->entry_list[selected_line]));
1317 break;
1318 default:
1319 g_assert_not_reached();
1320 }
1321 } else if ((mretv & MENU_CUSTOM_INPUT) && *input != NULL &&
1322 *input[0] != '\0') {
1323 RofiHelperExecuteContext context = {.name = NULL};
1324 gboolean run_in_term = ((mretv & MENU_CUSTOM_ACTION) == MENU_CUSTOM_ACTION);
1325 // FIXME: We assume startup notification in terminals, not in others
1326 if (!helper_execute_command(NULL, *input, run_in_term,
1327 run_in_term ? &context : NULL)) {
1328 retv = RELOAD_DIALOG;
1329 }
1330 } else if ((mretv & MENU_ENTRY_DELETE) &&
1331 selected_line < rmpd->cmd_list_length) {
1332 // Positive sort index means it is in history.
1333 if (rmpd->entry_list[selected_line].sort_index >= 0) {
1334 delete_entry_history(&(rmpd->entry_list[selected_line]));
1335 drun_entry_clear(&(rmpd->entry_list[selected_line]));
1336 memmove(&(rmpd->entry_list[selected_line]),
1337 &rmpd->entry_list[selected_line + 1],
1338 sizeof(DRunModeEntry) *
1339 (rmpd->cmd_list_length - selected_line - 1));
1340 rmpd->cmd_list_length--;
1341 }
1342 retv = RELOAD_DIALOG;
1343 } else if (mretv & MENU_CUSTOM_COMMAND) {
1344 retv = (mretv & MENU_LOWER_MASK);
1345 } else if ((mretv & MENU_COMPLETE)) {
1346 retv = RELOAD_DIALOG;
1347 if (selected_line < rmpd->cmd_list_length) {
1348 switch (rmpd->entry_list[selected_line].type) {
1349 case DRUN_DESKTOP_ENTRY_TYPE_SERVICE:
1350 case DRUN_DESKTOP_ENTRY_TYPE_APPLICATION: {
1351 GRegex *regex = g_regex_new("%[fFuU]", 0, 0, NULL);
1352
1353 if (g_regex_match(regex, rmpd->entry_list[selected_line].exec, 0,
1354 NULL)) {
1355 rmpd->selected_line = selected_line;
1356 // TODO add check if it supports passing file.
1357
1358 g_free(rmpd->old_input);
1359 rmpd->old_input = g_strdup(*input);
1360
1361 if (*input)
1362 g_free(*input);
1363 *input = g_strdup(rmpd->old_completer_input);
1364
1365 const Mode *comp = rofi_get_completer();
1366 if (comp) {
1367 rmpd->completer = mode_create(comp);
1368 mode_init(rmpd->completer);
1369 rmpd->file_complete = TRUE;
1370 }
1371 }
1372 g_regex_unref(regex);
1373 }
1374 default:
1375 break;
1376 }
1377 }
1378 }
1379 return retv;
1380}
1381static void drun_mode_destroy(Mode *sw) {
1382 DRunModePrivateData *rmpd = (DRunModePrivateData *)mode_get_private_data(sw);
1383 if (rmpd != NULL) {
1384 for (size_t i = 0; i < rmpd->cmd_list_length; i++) {
1385 drun_entry_clear(&(rmpd->entry_list[i]));
1386 }
1387 g_hash_table_destroy(rmpd->disabled_entries);
1388 g_free(rmpd->entry_list);
1389
1390 g_free(rmpd->old_completer_input);
1391 g_free(rmpd->old_input);
1392 if (rmpd->completer != NULL) {
1393 mode_destroy(rmpd->completer);
1394 g_free(rmpd->completer);
1395 }
1396
1397 g_strfreev(rmpd->current_desktop_list);
1398 g_strfreev(rmpd->show_categories);
1399 g_free(rmpd);
1400 mode_set_private_data(sw, NULL);
1401 }
1402}
1403
1404static char *_get_display_value(const Mode *sw, unsigned int selected_line,
1405 int *state, G_GNUC_UNUSED GList **list,
1406 int get_entry) {
1407 DRunModePrivateData *pd = (DRunModePrivateData *)mode_get_private_data(sw);
1408
1409 if (pd->file_complete) {
1410 return pd->completer->_get_display_value(pd->completer, selected_line,
1411 state, list, get_entry);
1412 }
1413 *state |= MARKUP;
1414 if (!get_entry) {
1415 return NULL;
1416 }
1417 if (pd->entry_list == NULL) {
1418 // Should never get here.
1419 return g_strdup("Failed");
1420 }
1421 /* Free temp storage. */
1422 DRunModeEntry *dr = &(pd->entry_list[selected_line]);
1423 gchar *cats = NULL;
1424 if (dr->categories) {
1425 char *tcats = g_strjoinv(",", dr->categories);
1426 if (tcats) {
1427 cats = g_markup_escape_text(tcats, -1);
1428 g_free(tcats);
1429 }
1430 }
1431 gchar *keywords = NULL;
1432 if (dr->keywords) {
1433 char *tkeyw = g_strjoinv(",", dr->keywords);
1434 if (tkeyw) {
1435 keywords = g_markup_escape_text(tkeyw, -1);
1436 g_free(tkeyw);
1437 }
1438 }
1439 // Needed for display.
1440 char *egn = NULL;
1441 char *en = NULL;
1442 char *ec = NULL;
1443 char *ee = NULL;
1444 char *eu = NULL;
1445 if (dr->generic_name) {
1446 egn = g_markup_escape_text(dr->generic_name, -1);
1447 }
1448 if (dr->name) {
1449 en = g_markup_escape_text(dr->name, -1);
1450 }
1451 if (dr->comment) {
1452 ec = g_markup_escape_text(dr->comment, -1);
1453 }
1454 if (dr->url) {
1455 eu = g_markup_escape_text(dr->url, -1);
1456 }
1457 if (dr->exec) {
1458 ee = g_markup_escape_text(dr->exec, -1);
1459 }
1460
1462 config.drun_display_format, "{generic}", egn, "{name}", en, "{comment}",
1463 ec, "{exec}", ee, "{categories}", cats, "{keywords}", keywords, "{url}",
1464 eu, (char *)0);
1465 g_free(egn);
1466 g_free(en);
1467 g_free(ec);
1468 g_free(eu);
1469 g_free(ee);
1470 g_free(cats);
1471 g_free(keywords);
1472 return retv;
1473}
1474
1475static cairo_surface_t *_get_icon(const Mode *sw, unsigned int selected_line,
1476 unsigned int height) {
1477 DRunModePrivateData *pd = (DRunModePrivateData *)mode_get_private_data(sw);
1478 if (pd->file_complete) {
1479 return pd->completer->_get_icon(pd->completer, selected_line, height);
1480 }
1481 g_return_val_if_fail(pd->entry_list != NULL, NULL);
1482 DRunModeEntry *dr = &(pd->entry_list[selected_line]);
1483 if (dr->icon_name != NULL) {
1484 if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height) {
1485 cairo_surface_t *icon = rofi_icon_fetcher_get(dr->icon_fetch_uid);
1486 return icon;
1487 }
1488 dr->icon_fetch_uid = rofi_icon_fetcher_query(dr->icon_name, height);
1489 dr->icon_fetch_size = height;
1490 cairo_surface_t *icon = rofi_icon_fetcher_get(dr->icon_fetch_uid);
1491 return icon;
1492 }
1493 return NULL;
1494}
1495
1496static char *drun_get_completion(const Mode *sw, unsigned int index) {
1497 DRunModePrivateData *pd = (DRunModePrivateData *)mode_get_private_data(sw);
1498 /* Free temp storage. */
1499 DRunModeEntry *dr = &(pd->entry_list[index]);
1500 if (dr->generic_name == NULL) {
1501 return g_strdup(dr->name);
1502 }
1503 return g_strdup_printf("%s", dr->name);
1504}
1505
1506static int drun_token_match(const Mode *data, rofi_int_matcher **tokens,
1507 unsigned int index) {
1508 DRunModePrivateData *rmpd =
1509 (DRunModePrivateData *)mode_get_private_data(data);
1510 if (rmpd->file_complete) {
1511 return rmpd->completer->_token_match(rmpd->completer, tokens, index);
1512 }
1513 int match = 1;
1514 if (tokens) {
1515 for (int j = 0; match && tokens[j] != NULL; j++) {
1516 int test = 0;
1517 rofi_int_matcher *ftokens[2] = {tokens[j], NULL};
1518 // Match name
1519 if (matching_entry_fields[DRUN_MATCH_FIELD_NAME].enabled_match) {
1520 if (rmpd->entry_list[index].name) {
1521 test = helper_token_match(ftokens, rmpd->entry_list[index].name);
1522 }
1523 }
1524 if (matching_entry_fields[DRUN_MATCH_FIELD_GENERIC].enabled_match) {
1525 // Match generic name
1526 if (test == tokens[j]->invert && rmpd->entry_list[index].generic_name) {
1527 test =
1528 helper_token_match(ftokens, rmpd->entry_list[index].generic_name);
1529 }
1530 }
1531 if (matching_entry_fields[DRUN_MATCH_FIELD_EXEC].enabled_match) {
1532 // Match executable name.
1533 if (test == tokens[j]->invert && rmpd->entry_list[index].exec) {
1534 test = helper_token_match(ftokens, rmpd->entry_list[index].exec);
1535 }
1536 }
1537 if (matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_match) {
1538 // Match against category.
1539 if (test == tokens[j]->invert) {
1540 gchar **list = rmpd->entry_list[index].categories;
1541 for (int iter = 0; test == tokens[j]->invert && list && list[iter];
1542 iter++) {
1543 test = helper_token_match(ftokens, list[iter]);
1544 }
1545 }
1546 }
1547 if (matching_entry_fields[DRUN_MATCH_FIELD_KEYWORDS].enabled_match) {
1548 // Match against category.
1549 if (test == tokens[j]->invert) {
1550 gchar **list = rmpd->entry_list[index].keywords;
1551 for (int iter = 0; test == tokens[j]->invert && list && list[iter];
1552 iter++) {
1553 test = helper_token_match(ftokens, list[iter]);
1554 }
1555 }
1556 }
1557 if (matching_entry_fields[DRUN_MATCH_FIELD_URL].enabled_match) {
1558
1559 // Match executable name.
1560 if (test == tokens[j]->invert && rmpd->entry_list[index].url) {
1561 test = helper_token_match(ftokens, rmpd->entry_list[index].url);
1562 }
1563 }
1564 if (matching_entry_fields[DRUN_MATCH_FIELD_COMMENT].enabled_match) {
1565
1566 // Match executable name.
1567 if (test == tokens[j]->invert && rmpd->entry_list[index].comment) {
1568 test = helper_token_match(ftokens, rmpd->entry_list[index].comment);
1569 }
1570 }
1571 if (test == 0) {
1572 match = 0;
1573 }
1574 }
1575 }
1576
1577 return match;
1578}
1579
1580static unsigned int drun_mode_get_num_entries(const Mode *sw) {
1581 const DRunModePrivateData *pd =
1582 (const DRunModePrivateData *)mode_get_private_data(sw);
1583 if (pd->file_complete) {
1584 return pd->completer->_get_num_entries(pd->completer);
1585 }
1586 return pd->cmd_list_length;
1587}
1588static char *drun_get_message(const Mode *sw) {
1589 DRunModePrivateData *pd = sw->private_data;
1590 if (pd->file_complete) {
1591 if (pd->selected_line < pd->cmd_list_length) {
1592 char *msg = mode_get_message(pd->completer);
1593 if (msg) {
1594 char *retv =
1595 g_strdup_printf("File complete for: %s\n%s",
1596 pd->entry_list[pd->selected_line].name, msg);
1597 g_free(msg);
1598 return retv;
1599 }
1600 return g_strdup_printf("File complete for: %s",
1601 pd->entry_list[pd->selected_line].name);
1602 }
1603 }
1604 return NULL;
1605}
1606#include "mode-private.h"
1608Mode drun_mode = {.name = "drun",
1609 .cfg_name_key = "display-drun",
1610 ._init = drun_mode_init,
1611 ._get_num_entries = drun_mode_get_num_entries,
1612 ._result = drun_mode_result,
1613 ._destroy = drun_mode_destroy,
1614 ._token_match = drun_token_match,
1615 ._get_message = drun_get_message,
1616 ._get_completion = drun_get_completion,
1617 ._get_display_value = _get_display_value,
1618 ._get_icon = _get_icon,
1619 ._preprocess_input = NULL,
1620 .private_data = NULL,
1621 .free = NULL,
1622 .type = MODE_TYPE_SWITCHER};
1623
1624#endif // ENABLE_DRUN
static cairo_surface_t * _get_icon(const Mode *sw, unsigned int selected_line, unsigned int height)
static char * _get_display_value(const Mode *sw, unsigned int selected_line, G_GNUC_UNUSED int *state, G_GNUC_UNUSED GList **attr_list, int get_entry)
const char * icon_name[NUM_FILE_TYPES]
Definition filebrowser.c:90
Property * rofi_theme_find_property(ThemeWidget *wid, PropertyType type, const char *property, gboolean exact)
Definition theme.c:743
gboolean helper_execute_command(const char *wd, const char *cmd, gboolean run_in_term, RofiHelperExecuteContext *context)
Definition helper.c:1028
char * helper_string_replace_if_exists(char *string,...)
Definition helper.c:1336
ThemeWidget * rofi_config_find_widget(const char *name, const char *state, gboolean exact)
Definition theme.c:780
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
Definition helper.c:515
void history_set(const char *filename, const char *entry)
Definition history.c:179
void history_remove(const char *filename, const char *entry)
Definition history.c:260
char ** history_get_list(const char *filename, unsigned int *length)
Definition history.c:324
cairo_surface_t * rofi_icon_fetcher_get(const uint32_t uid)
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:44
Mode * mode_create(const Mode *mode)
Definition mode.c:220
ModeMode mode_completer_result(Mode *mode, int menu_retv, char **input, unsigned int selected_line, char **path)
Definition mode.c:227
void * mode_get_private_data(const Mode *mode)
Definition mode.c:171
char * mode_get_message(const Mode *mode)
Definition mode.c:213
void mode_set_private_data(Mode *mode, void *pd)
Definition mode.c:176
ModeMode
Definition mode.h:49
@ MENU_CUSTOM_COMMAND
Definition mode.h:79
@ MENU_COMPLETE
Definition mode.h:83
@ MENU_LOWER_MASK
Definition mode.h:87
@ MENU_CANCEL
Definition mode.h:69
@ MENU_ENTRY_DELETE
Definition mode.h:75
@ MENU_CUSTOM_ACTION
Definition mode.h:85
@ MENU_OK
Definition mode.h:67
@ MENU_CUSTOM_INPUT
Definition mode.h:73
@ MODE_EXIT
Definition mode.h:51
@ RELOAD_DIALOG
Definition mode.h:55
const Mode * rofi_get_completer(void)
Definition rofi.c:1266
const char * cache_dir
Definition rofi.c:84
#define TICK_N(a)
Definition timings.h:69
@ MARKUP
Definition textbox.h:112
struct _icon icon
Definition icon.h:44
static void get_apps(KeysHelpModePrivateData *pd)
Definition help-keys.c:55
@ MODE_TYPE_SWITCHER
@ P_BOOLEAN
Definition rofi-types.h:18
struct rofi_int_matcher_t rofi_int_matcher
Settings config
PropertyValue value
Definition rofi-types.h:293
PropertyType type
Definition rofi-types.h:291
const gchar * wmclass
Definition helper.h:298
void * private_data