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