29#define G_LOG_DOMAIN "Modes.DRun"
64#ifdef HAVE_GDESKTOPAPPINFO_H
65#include <gio/gdesktopappinfo.h>
69#define DRUN_CACHE_FILE "rofi3.druncache"
72#define DRUN_DESKTOP_CACHE_FILE "rofi-drun-desktop.cache"
75#define DRUN_MAX_STRING_LENGTH (10 * 1024 * 1024)
77#define DRUN_MAX_NUM_ENTRIES (256 * 1024)
80char *DRUN_GROUP_NAME =
"Desktop Entry";
85typedef struct _DRunModePrivateData DRunModePrivateData;
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;
126 cairo_surface_t *
icon;
146 uint32_t icon_fetch_uid;
147 uint32_t icon_fetch_size;
148 guint icon_fetch_scale;
152 DRunDesktopEntryType type;
156 const char *entry_field_name;
157 gboolean enabled_match;
158 gboolean enabled_display;
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,
183static DRunEntryField matching_entry_fields[DRUN_MATCH_NUM_FIELDS] = {
185 .entry_field_name =
"name",
186 .enabled_match = TRUE,
187 .enabled_display = TRUE,
190 .entry_field_name =
"generic",
191 .enabled_match = TRUE,
192 .enabled_display = TRUE,
195 .entry_field_name =
"exec",
196 .enabled_match = TRUE,
197 .enabled_display = TRUE,
200 .entry_field_name =
"categories",
201 .enabled_match = TRUE,
202 .enabled_display = TRUE,
205 .entry_field_name =
"keywords",
206 .enabled_match = TRUE,
207 .enabled_display = TRUE,
210 .entry_field_name =
"comment",
211 .enabled_match = FALSE,
212 .enabled_display = FALSE,
215 .entry_field_name =
"url",
216 .enabled_match = FALSE,
217 .enabled_display = FALSE,
220struct _DRunModePrivateData {
221 DRunModeEntry *entry_list;
222 unsigned int cmd_list_length;
223 unsigned int cmd_list_length_actual;
225 GHashTable *disabled_entries;
226 unsigned int disabled_entries_length;
227 unsigned int expected_line_height;
229 char **show_categories;
230 char **exclude_categories;
233 const gchar *icon_theme;
235 gchar **current_desktop_list;
237 gboolean file_complete;
239 char *old_completer_input;
240 uint32_t selected_line;
244 gboolean disable_giolaunch;
252static void drun_entry_clear(DRunModeEntry *e);
254static gboolean drun_helper_eval_cb(
const GMatchInfo *info, GString *res,
257 struct RegexEvalArg *e = (
struct RegexEvalArg *)data;
261 match = g_match_info_fetch(info, 0);
269 g_string_append(res, e->path);
275 if (e->e && e->e->icon) {
276 g_string_append_printf(res,
"--icon %s", e->e->icon_name);
288 g_string_append(res,
"%");
292 char *esc = g_shell_quote(e->e->path);
293 g_string_append(res, esc);
299 char *esc = g_shell_quote(e->e->name);
300 g_string_append(res, esc);
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);
323 g_warning(
"[%s] [%s] Failed to parse desktop file because: %s.",
324 e->app_id, e->path, error->message);
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);
340 gchar *command = g_newa(gchar, command_len);
344 g_debug(
"Link launch command: |%s|", command);
346 char *path = g_build_filename(
cache_dir, DRUN_CACHE_FILE, NULL);
353static void exec_cmd_entry(DRunModePrivateData *pd, DRunModeEntry *e,
355 GError *error = NULL;
356 GRegex *reg = g_regex_new(
"%[a-zA-Z%]", 0, 0, &error);
358 g_warning(
"Internal error, failed to create regex: %s.", error->message);
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,
366 g_warning(
"Internal error, failed replace field codes: %s.",
372 if (earg.success == FALSE) {
373 g_warning(
"Invalid field code in Exec line: %s.", e->exec);
378 g_warning(
"Nothing to execute after processing: %s.", e->exec);
382 g_debug(
"Parsed command: |%s| into |%s|.", e->exec, str);
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);
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);
400 const gchar *fp = g_strstrip(str);
402 g_key_file_get_string(e->key_file, e->action,
"Path", NULL);
403 if (exec_path != NULL && strlen(exec_path) == 0) {
411 .icon = e->icon_name,
412 .app_id = e->desktop_id,
415 g_key_file_get_boolean(e->key_file, e->action,
"StartupNotify", NULL);
416 gchar *wmclass = NULL;
418 g_key_file_has_key(e->key_file, e->action,
"StartupWMClass", NULL)) {
420 g_key_file_get_string(e->key_file, e->action,
"StartupWMClass", NULL);
423 gboolean launched = FALSE;
425#ifdef HAVE_GDESKTOPAPPINFO_H
426 if (pd->disable_giolaunch == FALSE) {
427 GDesktopAppInfo *gdai = g_desktop_app_info_new_from_keyfile(e->key_file);
430 GError *ai_error = NULL;
433 GFile *file = g_file_new_for_path(path);
434 files = g_list_append(files, (
void *)file);
437 g_desktop_app_info_launch_action(gdai, e->action_name, NULL);
440 launched = g_app_info_launch(G_APP_INFO(gdai), files, NULL, &ai_error);
443 g_warning(
"Failed to launch application using GAppInfo::Launch: %s",
445 g_error_free(ai_error);
447 g_object_unref(gdai);
449 g_list_free_full(files, g_object_unref);
452 g_warning(
"Failed to create GDesktopAppInfo for: %s", e->path);
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);
466 if (e->generic_name) {
468 g_strdup_printf(
"DESKTOP_ENTRY_GENERICNAME_L=%s", e->generic_name);
470 envp[envp_l++] = NULL;
475 g_key_file_get_boolean(e->key_file, e->action,
"Terminal", NULL);
477 sn ? &context : NULL, envp);
481 if (launched == TRUE) {
482 char *drun_cach_path = g_build_filename(
cache_dir, DRUN_CACHE_FILE, NULL);
485 g_free(drun_cach_path);
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])) {
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;
514 const ssize_t id_len = strlen(path) - strlen(root);
516 g_strlcpy(
id, &(path[strlen(root) + 1]), id_len);
517 for (
int index = 0; index < id_len; index++) {
518 if (
id[index] ==
'/') {
524 if (g_hash_table_contains(pd->disabled_entries,
id) && !parse_action) {
525 g_debug(
"[%s] [%s] Skipping, was previously seen.",
id, path);
528 GKeyFile *kf = g_key_file_new();
529 GError *error = NULL;
530 gboolean res = g_key_file_load_from_file(kf, path, 0, &error);
533 g_debug(
"[%s] [%s] Failed to parse desktop file because: %s.",
id, path,
540 if (g_key_file_has_group(kf, action) == FALSE) {
542 g_debug(
"[%s] [%s] Invalid desktop file: No %s group",
id, path, action);
547 gchar *key = g_key_file_get_string(kf, DRUN_GROUP_NAME,
"Type", NULL);
550 g_debug(
"[%s] [%s] Invalid desktop file: No type indicated",
id, path);
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.");
563 "[%s] [%s] Skipping desktop file: Not of type Application or Link (%s)",
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);
579 if (g_key_file_get_boolean(kf, DRUN_GROUP_NAME,
"Hidden", NULL)) {
581 "[%s] [%s] Adding desktop file to disabled list: 'Hidden' key is true",
584 g_hash_table_add(pd->disabled_entries, g_strdup(
id));
587 if (pd->current_desktop_list) {
588 gboolean show = TRUE;
590 if (g_key_file_has_key(kf, DRUN_GROUP_NAME,
"OnlyShowIn", NULL)) {
593 gchar **list = g_key_file_get_string_list(kf, DRUN_GROUP_NAME,
594 "OnlyShowIn", &llength, NULL);
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);
604 if (show && g_key_file_has_key(kf, DRUN_GROUP_NAME,
"NotShowIn", NULL)) {
606 gchar **list = g_key_file_get_string_list(kf, DRUN_GROUP_NAME,
607 "NotShowIn", &llength, NULL);
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);
619 g_debug(
"[%s] [%s] Adding desktop file to disabled list: "
620 "'OnlyShowIn'/'NotShowIn' keys don't match current desktop",
623 g_hash_table_add(pd->disabled_entries, g_strdup(
id));
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 "
633 g_hash_table_add(pd->disabled_entries, g_strdup(
id));
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 "
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 "
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 "
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);
674 if (g_file_test(te, G_FILE_TEST_IS_EXECUTABLE) == FALSE) {
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);
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);
700 if (rofi_strv_contains((
const char *
const *)categories,
701 (
const char *
const *)pd->exclude_categories)) {
702 g_strfreev(categories);
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)));
716 if (G_UNLIKELY(pd->cmd_list_length > INT_MAX)) {
718 pd->entry_list[pd->cmd_list_length].sort_index = INT_MIN;
720 pd->entry_list[pd->cmd_list_length].sort_index = -nl;
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"));
734 g_key_file_get_locale_string(kf, DRUN_GROUP_NAME,
"Name", NULL, NULL);
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);
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",
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,
753 pd->entry_list[pd->cmd_list_length].keywords = NULL;
756 if (matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_match ||
757 matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_display) {
759 pd->entry_list[pd->cmd_list_length].categories = categories;
762 pd->entry_list[pd->cmd_list_length].categories =
763 g_key_file_get_locale_string_list(kf, DRUN_GROUP_NAME,
"Categories",
767 pd->entry_list[pd->cmd_list_length].categories = NULL;
769 g_strfreev(categories);
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);
777 pd->entry_list[pd->cmd_list_length].exec = NULL;
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);
785 pd->entry_list[pd->cmd_list_length].comment = NULL;
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);
792 pd->entry_list[pd->cmd_list_length].url = NULL;
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;
799 pd->entry_list[pd->cmd_list_length].key_file = kf;
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)++;
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]);
822static void walk_dir(DRunModePrivateData *pd,
const char *root,
823 const char *dirname,
const gboolean recursive) {
826 g_debug(
"Checking directory %s for desktop files.", dirname);
827 dir = opendir(dirname);
833 gchar *filename = NULL;
835 while ((file = readdir(dir)) != NULL) {
836 if (file->d_name[0] ==
'.') {
839 switch (file->d_type) {
844 filename = g_build_filename(dirname, file->d_name, NULL);
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;
863 switch (file->d_type) {
866 if (g_str_has_suffix(file->d_name,
".desktop")) {
867 read_desktop_file(pd, root, filename, file->d_name, DRUN_GROUP_NAME,
873 walk_dir(pd, root, filename, recursive);
888static void delete_entry_history(
const DRunModeEntry *entry) {
889 char *path = g_build_filename(
cache_dir, DRUN_CACHE_FILE, NULL);
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);
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;
907 pd->entry_list[i].sort_index = INT_MAX;
914 TICK_N(
"Stop drun history");
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;
922 if (da->sort_index < 0 && db->sort_index < 0) {
923 if (da->name == NULL && db->name == NULL) {
926 if (da->name == NULL) {
929 if (db->name == NULL) {
932 return g_utf8_collate(da->name, db->name);
934 return db->sort_index - da->sort_index;
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);
949 fwrite(str, 1, l + 1, fd);
952static void drun_write_integer(FILE *fd, int32_t val) {
953 fwrite(&val,
sizeof(val), 1, fd);
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?");
962static gboolean drun_read_string(FILE *fd,
char **str) {
965 if (fread(&l,
sizeof(l), 1, fd) != 1) {
966 g_warning(
"Failed to read entry, cache corrupt?");
971 if (l > DRUN_MAX_STRING_LENGTH) {
976 (*str) = g_malloc(l);
977 if (fread((*str), 1, l, fd) != l) {
978 g_warning(
"Failed to read entry, cache corrupt?");
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]);
991static gboolean drun_read_stringv(FILE *fd,
char ***str) {
994 if (fread(&vl,
sizeof(vl), 1, fd) != 1) {
995 g_warning(
"Failed to read entry, cache corrupt?");
1001 if (vl >= UINT16_MAX) {
1002 g_warning(
"Array of strings vector is to long.");
1005 size_t ss =
sizeof(
char **) * (vl + 1);
1007 (*str) = g_malloc0(ss);
1008 for (guint index = 0; index < vl; index++) {
1009 if (drun_read_string(fd, &((*str)[index]))) {
1016static void write_cache(DRunModePrivateData *pd,
const char *cache_file) {
1020 TICK_N(
"DRUN Write CACHE: start");
1022 FILE *fd = fopen(cache_file,
"w");
1024 g_warning(
"Failed to write to cache file");
1027 uint8_t version = CACHE_VERSION;
1028 fwrite(&version,
sizeof(version), 1, fd);
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]);
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);
1044 drun_write_strv(fd, entry->categories);
1045 drun_write_strv(fd, entry->keywords);
1047 drun_write_str(fd, entry->comment);
1048 drun_write_str(fd, entry->url);
1049 drun_write_integer(fd, (int32_t)entry->type);
1053 TICK_N(
"DRUN Write CACHE: end");
1059static gboolean drun_read_cache(DRunModePrivateData *pd,
1060 const char *cache_file) {
1068 TICK_N(
"DRUN Read CACHE: start");
1069 FILE *fd = fopen(cache_file,
"r");
1071 TICK_N(
"DRUN Read CACHE: stop");
1076 uint8_t version = 0;
1078 if (fread(&version,
sizeof(version), 1, fd) != 1) {
1080 g_warning(
"Cache corrupt, ignoring.");
1081 TICK_N(
"DRUN Read CACHE: stop");
1085 if (version != CACHE_VERSION) {
1087 g_warning(
"Cache file wrong version, ignoring.");
1088 TICK_N(
"DRUN Read CACHE: stop");
1092 if (fread(&(pd->cmd_list_length),
sizeof(pd->cmd_list_length), 1, fd) != 1) {
1094 g_warning(
"Cache corrupt, ignoring.");
1095 TICK_N(
"DRUN Read CACHE: stop");
1099 pd->cmd_list_length_actual = pd->cmd_list_length;
1102 gsize newsize =
sizeof(DRunModeEntry) * pd->cmd_list_length;
1103 if ((DRUN_MAX_NUM_ENTRIES *
sizeof(DRunModeEntry) < newsize)) {
1105 g_warning(
"Cache has to many entries.");
1106 TICK_N(
"DRUN Read CACHE: stop");
1109 pd->entry_list = g_malloc0(newsize);
1112 for (
unsigned int index = 0; !error && index < pd->cmd_list_length; index++) {
1113 DRunModeEntry *entry = &(pd->entry_list[index]);
1115 if (drun_read_string(fd, &(entry->action))) {
1119 if (drun_read_string(fd, &(entry->root))) {
1123 if (drun_read_string(fd, &(entry->path))) {
1127 if (drun_read_string(fd, &(entry->app_id))) {
1131 if (drun_read_string(fd, &(entry->desktop_id))) {
1135 if (drun_read_string(fd, &(entry->icon_name))) {
1139 if (drun_read_string(fd, &(entry->exec))) {
1143 if (drun_read_string(fd, &(entry->name))) {
1147 if (drun_read_string(fd, &(entry->generic_name))) {
1152 if (drun_read_stringv(fd, &(entry->categories))) {
1156 if (drun_read_stringv(fd, &(entry->keywords))) {
1161 if (drun_read_string(fd, &(entry->comment))) {
1165 if (drun_read_string(fd, &(entry->url))) {
1170 if (drun_read_integer(fd, &(type))) {
1179 for (
size_t i = 0; i < pd->cmd_list_length; i++) {
1180 drun_entry_clear(&(pd->entry_list[i]));
1182 g_free(pd->entry_list);
1183 pd->cmd_list_length = 0;
1184 pd->cmd_list_length_actual = 0;
1187 TICK_N(
"DRUN Read CACHE: stop");
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)) {
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");
1212 dir = g_build_filename(g_get_user_data_dir(),
"applications", NULL);
1213 walk_dir(pd, dir, dir, TRUE);
1215 TICK_N(
"Get Desktop apps (user dir)");
1222 const gchar *
const *sys = g_get_system_data_dirs();
1223 for (
const gchar *
const *iter = sys; *iter != NULL; ++iter) {
1224 gboolean unique = TRUE;
1226 for (
const gchar *
const *iterd = sys; iterd != iter; ++iterd) {
1227 if (g_strcmp0(*iter, *iterd) == 0) {
1232 if (unique && (**iter) !=
'\0') {
1233 char *dir = g_build_filename(*iter,
"applications", NULL);
1234 walk_dir(pd, dir, dir, TRUE);
1238 TICK_N(
"Get Desktop apps (system dirs)");
1240 pd->disable_giolaunch = FALSE;
1243 pd->disable_giolaunch = TRUE;
1246 get_apps_history(pd);
1248 g_qsort_with_data(pd->entry_list, pd->cmd_list_length,
1249 sizeof(DRunModeEntry), drun_int_sort_list, NULL);
1253 write_cache(pd, cache_file);
1255 g_debug(
"Read drun entries from cache.");
1260static void drun_mode_parse_entry_fields(
void) {
1261 char *savept = NULL;
1264 const char *
const sep =
",#";
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;
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;
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;
1289 g_warning(
"Invalid entry name :%s", token);
1293 g_free(switcher_str);
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)
1301 gchar *search_term =
1302 g_strdup_printf(
"{%s}", matching_entry_fields[i].entry_field_name);
1304 matching_entry_fields[i].enabled_match = TRUE;
1306 g_free(search_term);
1310static int drun_mode_init(
Mode *sw) {
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);
1319 const char *current_desktop = g_getenv(
"XDG_CURRENT_DESKTOP");
1320 pd->current_desktop_list =
1321 current_desktop ? g_strsplit(current_desktop,
":", 0) : NULL;
1331 drun_mode_parse_entry_fields();
1332 drun_mode_parse_display_format();
1335 pd->completer = NULL;
1338static void drun_entry_clear(DRunModeEntry *e) {
1345 g_free(e->desktop_id);
1346 if (e->icon != NULL) {
1347 cairo_surface_destroy(e->icon);
1349 g_free(e->icon_name);
1352 g_free(e->generic_name);
1354 if (e->action != DRUN_GROUP_NAME) {
1357 g_free(e->action_name);
1358 g_strfreev(e->categories);
1359 g_strfreev(e->keywords);
1361 g_key_file_free(e->key_file);
1365static ModeMode drun_mode_result(
Mode *sw,
int mretv,
char **input,
1366 unsigned int selected_line) {
1370 if (rmpd->file_complete == TRUE) {
1375 g_free(rmpd->old_completer_input);
1376 rmpd->old_completer_input = *input;
1378 if (rmpd->selected_line < rmpd->cmd_list_length) {
1379 (*input) = g_strdup(rmpd->old_input);
1381 rmpd->file_complete = FALSE;
1389 exec_cmd_entry(rmpd, &(rmpd->entry_list[rmpd->selected_line]), path);
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);
1401 case DRUN_DESKTOP_ENTRY_TYPE_LINK:
1402 launch_link_entry(&(rmpd->entry_list[selected_line]));
1405 g_assert_not_reached();
1408 *input[0] !=
'\0') {
1413 run_in_term ? &context : NULL)) {
1417 selected_line < rmpd->cmd_list_length) {
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--;
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);
1439 if (g_regex_match(regex, rmpd->entry_list[selected_line].exec, 0,
1441 rmpd->selected_line = selected_line;
1444 g_free(rmpd->old_input);
1445 rmpd->old_input = g_strdup(*input);
1449 *input = g_strdup(rmpd->old_completer_input);
1455 rmpd->file_complete = TRUE;
1458 g_regex_unref(regex);
1467static void drun_mode_destroy(
Mode *sw) {
1470 for (
size_t i = 0; i < rmpd->cmd_list_length; i++) {
1471 drun_entry_clear(&(rmpd->entry_list[i]));
1473 g_hash_table_destroy(rmpd->disabled_entries);
1474 g_free(rmpd->entry_list);
1476 g_free(rmpd->old_completer_input);
1477 g_free(rmpd->old_input);
1478 if (rmpd->completer != NULL) {
1480 g_free(rmpd->completer);
1483 g_strfreev(rmpd->current_desktop_list);
1484 g_strfreev(rmpd->show_categories);
1485 g_strfreev(rmpd->exclude_categories);
1491static char *_get_display_value(
const Mode *sw,
unsigned int selected_line,
1492 int *state, G_GNUC_UNUSED GList **list,
1496 if (pd->file_complete) {
1497 return pd->completer->_get_display_value(pd->completer, selected_line,
1498 state, list, get_entry);
1504 if (pd->entry_list == NULL) {
1506 return g_strdup(
"Failed");
1509 DRunModeEntry *dr = &(pd->entry_list[selected_line]);
1511 if (dr->categories) {
1512 char *tcats = g_strjoinv(
",", dr->categories);
1514 cats = g_markup_escape_text(tcats, -1);
1518 gchar *keywords = NULL;
1520 char *tkeyw = g_strjoinv(
",", dr->keywords);
1522 keywords = g_markup_escape_text(tkeyw, -1);
1532 if (dr->generic_name) {
1533 egn = g_markup_escape_text(dr->generic_name, -1);
1536 en = g_markup_escape_text(dr->name, -1);
1539 ec = g_markup_escape_text(dr->comment, -1);
1542 eu = g_markup_escape_text(dr->url, -1);
1545 ee = g_markup_escape_text(dr->exec, -1);
1550 ec,
"{exec}", ee,
"{categories}", cats,
"{keywords}", keywords,
"{url}",
1562static cairo_surface_t *_get_icon(
const Mode *sw,
unsigned int selected_line,
1563 unsigned int height) {
1566 if (pd->file_complete) {
1567 return pd->completer->_get_icon(pd->completer, selected_line, height);
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) {
1578 dr->icon_fetch_size = height;
1579 dr->icon_fetch_scale = scale;
1586static char *drun_get_completion(
const Mode *sw,
unsigned int index) {
1589 DRunModeEntry *dr = &(pd->entry_list[index]);
1590 if (dr->generic_name == NULL) {
1591 return g_strdup(dr->name);
1593 return g_strdup_printf(
"%s", dr->name);
1597 unsigned int index) {
1598 DRunModePrivateData *rmpd =
1600 if (rmpd->file_complete) {
1601 return rmpd->completer->_token_match(rmpd->completer, tokens, index);
1605 for (
int j = 0; match && tokens[j] != NULL; j++) {
1609 if (matching_entry_fields[DRUN_MATCH_FIELD_NAME].enabled_match) {
1610 if (rmpd->entry_list[index].name) {
1614 if (matching_entry_fields[DRUN_MATCH_FIELD_GENERIC].enabled_match) {
1616 if (test == tokens[j]->invert && rmpd->entry_list[index].generic_name) {
1621 if (matching_entry_fields[DRUN_MATCH_FIELD_EXEC].enabled_match) {
1623 if (test == tokens[j]->invert && rmpd->entry_list[index].exec) {
1627 if (matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_match) {
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];
1637 if (matching_entry_fields[DRUN_MATCH_FIELD_KEYWORDS].enabled_match) {
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];
1647 if (matching_entry_fields[DRUN_MATCH_FIELD_URL].enabled_match) {
1650 if (test == tokens[j]->invert && rmpd->entry_list[index].url) {
1654 if (matching_entry_fields[DRUN_MATCH_FIELD_COMMENT].enabled_match) {
1657 if (test == tokens[j]->invert && rmpd->entry_list[index].comment) {
1670static unsigned int drun_mode_get_num_entries(
const Mode *sw) {
1671 const DRunModePrivateData *pd =
1673 if (pd->file_complete) {
1674 return pd->completer->_get_num_entries(pd->completer);
1676 return pd->cmd_list_length;
1678static char *drun_get_message(
const Mode *sw) {
1680 if (pd->file_complete) {
1681 if (pd->selected_line < pd->cmd_list_length) {
1685 g_strdup_printf(
"File complete for: %s\n%s",
1686 pd->entry_list[pd->selected_line].name, msg);
1690 return g_strdup_printf(
"File complete for: %s",
1691 pd->entry_list[pd->selected_line].name);
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,
guint display_scale(void)
const char * icon_name[NUM_FILE_TYPES]
gboolean helper_execute_command_env(const char *wd, const char *cmd, gboolean run_in_term, RofiHelperExecuteContext *context, char **envp)
Property * rofi_theme_find_property(ThemeWidget *wid, PropertyType type, const char *property, gboolean exact)
gboolean helper_execute_command(const char *wd, const char *cmd, gboolean run_in_term, RofiHelperExecuteContext *context)
char * helper_string_replace_if_exists(char *string,...)
ThemeWidget * rofi_config_find_widget(const char *name, const char *state, gboolean exact)
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
void history_set(const char *filename, const char *entry)
void history_remove(const char *filename, const char *entry)
char ** history_get_list(const char *filename, unsigned int *length)
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)
int mode_init(Mode *mode)
Mode * mode_create(const Mode *mode)
ModeMode mode_completer_result(Mode *mode, int menu_retv, char **input, unsigned int selected_line, char **path)
void * mode_get_private_data(const Mode *mode)
char * mode_get_message(const Mode *mode)
void mode_set_private_data(Mode *mode, void *pd)
const Mode * rofi_get_completer(void)
struct rofi_int_matcher_t rofi_int_matcher
gboolean drun_reload_desktop_cache
unsigned int drun_show_actions
char * drun_display_format
char * drun_exclude_categories
gboolean drun_use_desktop_cache