28#define G_LOG_DOMAIN "Modes.DRun"
64#define DRUN_CACHE_FILE "rofi3.druncache"
67#define DRUN_DESKTOP_CACHE_FILE "rofi-drun-desktop.cache"
70char *DRUN_GROUP_NAME =
"Desktop Entry";
75typedef struct _DRunModePrivateData DRunModePrivateData;
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;
98 DRunModePrivateData *pd;
117 cairo_surface_t *
icon;
137 uint32_t icon_fetch_uid;
138 uint32_t icon_fetch_size;
140 DRunDesktopEntryType type;
144 const char *entry_field_name;
145 gboolean enabled_match;
146 gboolean enabled_display;
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,
171static DRunEntryField matching_entry_fields[DRUN_MATCH_NUM_FIELDS] = {
173 .entry_field_name =
"name",
174 .enabled_match = TRUE,
175 .enabled_display = TRUE,
178 .entry_field_name =
"generic",
179 .enabled_match = TRUE,
180 .enabled_display = TRUE,
183 .entry_field_name =
"exec",
184 .enabled_match = TRUE,
185 .enabled_display = TRUE,
188 .entry_field_name =
"categories",
189 .enabled_match = TRUE,
190 .enabled_display = TRUE,
193 .entry_field_name =
"keywords",
194 .enabled_match = TRUE,
195 .enabled_display = TRUE,
198 .entry_field_name =
"comment",
199 .enabled_match = FALSE,
200 .enabled_display = FALSE,
203 .entry_field_name =
"url",
204 .enabled_match = FALSE,
205 .enabled_display = FALSE,
208struct _DRunModePrivateData {
209 DRunModeEntry *entry_list;
210 unsigned int cmd_list_length;
211 unsigned int cmd_list_length_actual;
213 GHashTable *disabled_entries;
214 unsigned int disabled_entries_length;
215 unsigned int expected_line_height;
217 char **show_categories;
220 const gchar *icon_theme;
222 gchar **current_desktop_list;
224 gboolean file_complete;
226 char *old_completer_input;
227 uint32_t selected_line;
237static gboolean drun_helper_eval_cb(
const GMatchInfo *info, GString *res,
240 struct RegexEvalArg *e = (
struct RegexEvalArg *)data;
244 match = g_match_info_fetch(info, 0);
252 g_string_append(res, e->path);
258 if (e->e && e->e->icon) {
259 g_string_append_printf(res,
"--icon %s", e->e->icon_name);
271 g_string_append(res,
"%");
275 char *esc = g_shell_quote(e->e->path);
276 g_string_append(res, esc);
282 char *esc = g_shell_quote(e->e->name);
283 g_string_append(res, esc);
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);
306 g_warning(
"[%s] [%s] Failed to parse desktop file because: %s.",
307 e->app_id, e->path, error->message);
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);
321 gsize command_len = strlen(
config.drun_url_launcher) + strlen(url) +
323 gchar *command = g_newa(gchar, command_len);
324 g_snprintf(command, command_len,
"%s %s",
config.drun_url_launcher, url);
327 g_debug(
"Link launch command: |%s|", command);
329 char *path = g_build_filename(
cache_dir, DRUN_CACHE_FILE, NULL);
335static gchar *app_path_for_id(
const gchar *app_id) {
339 path = g_strconcat(
"/", app_id, NULL);
340 for (i = 0; path[i]; i++) {
349static GVariant *app_get_platform_data(
void) {
350 GVariantBuilder builder;
351 const gchar *startup_id;
353 g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
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));
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));
363 return g_variant_builder_end(&builder);
366static gboolean exec_dbus_entry(DRunModeEntry *e,
const char *path) {
367 GVariantBuilder files;
368 GDBusConnection *session;
369 GError *error = NULL;
372 GVariant *params = NULL;
373 const char *method =
"Activate";
374 g_debug(
"Trying to launch desktop file using dbus activation.");
376 session = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
378 g_warning(
"unable to connect to D-Bus: %s\n", error->message);
383 object_path = app_path_for_id(e->app_id);
385 g_variant_builder_init(&files, G_VARIANT_TYPE_STRING_ARRAY);
389 params = g_variant_new(
"(as@a{sv})", &files, app_get_platform_data());
391 params = g_variant_new(
"(@a{sv})", app_get_platform_data());
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);
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);
406 g_variant_unref(result);
408 g_warning(
"error sending %s message to application: %s\n",
"Open",
411 g_object_unref(session);
414 g_object_unref(session);
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);
422 g_warning(
"Internal error, failed to create regex: %s.", error->message);
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,
430 g_warning(
"Internal error, failed replace field codes: %s.",
436 if (earg.success == FALSE) {
437 g_warning(
"Invalid field code in Exec line: %s.", e->exec);
442 g_warning(
"Nothing to execute after processing: %s.", e->exec);
446 g_debug(
"Parsed command: |%s| into |%s|.", e->exec, str);
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);
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);
464 const gchar *fp = g_strstrip(str);
466 g_key_file_get_string(e->key_file, e->action,
"Path", NULL);
467 if (exec_path != NULL && strlen(exec_path) == 0) {
475 .icon = e->icon_name,
479 g_key_file_get_boolean(e->key_file, e->action,
"StartupNotify", NULL);
480 gchar *wmclass = NULL;
482 g_key_file_has_key(e->key_file, e->action,
"StartupWMClass", NULL)) {
484 g_key_file_get_string(e->key_file, e->action,
"StartupWMClass", NULL);
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);
494 if (launched == FALSE) {
500 g_key_file_get_boolean(e->key_file, e->action,
"Terminal", NULL);
502 char *drun_cach_path = g_build_filename(
cache_dir, DRUN_CACHE_FILE, NULL);
505 g_free(drun_cach_path);
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])) {
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);
535 const ssize_t id_len = strlen(path) - strlen(root);
537 g_strlcpy(
id, &(path[strlen(root) + 1]), id_len);
538 for (
int index = 0; index < id_len; index++) {
539 if (
id[index] ==
'/') {
545 if (g_hash_table_contains(pd->disabled_entries,
id) && !parse_action) {
546 g_debug(
"[%s] [%s] Skipping, was previously seen.",
id, path);
549 GKeyFile *kf = g_key_file_new();
550 GError *error = NULL;
551 gboolean res = g_key_file_load_from_file(kf, path, 0, &error);
554 g_debug(
"[%s] [%s] Failed to parse desktop file because: %s.",
id, path,
561 if (g_key_file_has_group(kf, action) == FALSE) {
563 g_debug(
"[%s] [%s] Invalid desktop file: No %s group",
id, path, action);
568 gchar *key = g_key_file_get_string(kf, DRUN_GROUP_NAME,
"Type", NULL);
571 g_debug(
"[%s] [%s] Invalid desktop file: No type indicated",
id, path);
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.");
584 "[%s] [%s] Skipping desktop file: Not of type Application or Link (%s)",
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);
600 if (g_key_file_get_boolean(kf, DRUN_GROUP_NAME,
"Hidden", NULL)) {
602 "[%s] [%s] Adding desktop file to disabled list: 'Hidden' key is true",
605 g_hash_table_add(pd->disabled_entries, g_strdup(
id));
608 if (pd->current_desktop_list) {
609 gboolean show = TRUE;
611 if (g_key_file_has_key(kf, DRUN_GROUP_NAME,
"OnlyShowIn", NULL)) {
614 gchar **list = g_key_file_get_string_list(kf, DRUN_GROUP_NAME,
615 "OnlyShowIn", &llength, NULL);
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);
625 if (show && g_key_file_has_key(kf, DRUN_GROUP_NAME,
"NotShowIn", NULL)) {
627 gchar **list = g_key_file_get_string_list(kf, DRUN_GROUP_NAME,
628 "NotShowIn", &llength, NULL);
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);
640 g_debug(
"[%s] [%s] Adding desktop file to disabled list: "
641 "'OnlyShowIn'/'NotShowIn' keys don't match current desktop",
644 g_hash_table_add(pd->disabled_entries, g_strdup(
id));
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 "
654 g_hash_table_add(pd->disabled_entries, g_strdup(
id));
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 "
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 "
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 "
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);
695 if (g_file_test(te, G_FILE_TEST_IS_EXECUTABLE) == FALSE) {
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);
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)));
724 if (G_UNLIKELY(pd->cmd_list_length > INT_MAX)) {
726 pd->entry_list[pd->cmd_list_length].sort_index = INT_MIN;
728 pd->entry_list[pd->cmd_list_length].sort_index = -nl;
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"));
739 g_key_file_get_locale_string(kf, DRUN_GROUP_NAME,
"Name", NULL, NULL);
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);
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",
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,
758 pd->entry_list[pd->cmd_list_length].keywords = NULL;
761 if (matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_match ||
762 matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_display) {
764 pd->entry_list[pd->cmd_list_length].categories = categories;
767 pd->entry_list[pd->cmd_list_length].categories =
768 g_key_file_get_locale_string_list(kf, DRUN_GROUP_NAME,
"Categories",
772 pd->entry_list[pd->cmd_list_length].categories = NULL;
774 g_strfreev(categories);
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);
782 pd->entry_list[pd->cmd_list_length].exec = NULL;
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);
790 pd->entry_list[pd->cmd_list_length].comment = NULL;
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);
797 pd->entry_list[pd->cmd_list_length].url = NULL;
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;
804 pd->entry_list[pd->cmd_list_length].key_file = kf;
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)++;
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);
827static void walk_dir(DRunModePrivateData *pd,
const char *root,
828 const char *dirname,
const gboolean recursive) {
831 g_debug(
"Checking directory %s for desktop files.", dirname);
832 dir = opendir(dirname);
838 gchar *filename = NULL;
840 while ((file = readdir(dir)) != NULL) {
841 if (file->d_name[0] ==
'.') {
844 switch (file->d_type) {
849 filename = g_build_filename(dirname, file->d_name, NULL);
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;
868 switch (file->d_type) {
871 if (g_str_has_suffix(file->d_name,
".desktop")) {
872 read_desktop_file(pd, root, filename, file->d_name, DRUN_GROUP_NAME);
877 walk_dir(pd, root, filename, recursive);
892static void delete_entry_history(
const DRunModeEntry *entry) {
893 char *path = g_build_filename(
cache_dir, DRUN_CACHE_FILE, NULL);
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);
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;
911 pd->entry_list[i].sort_index = INT_MAX;
918 TICK_N(
"Stop drun history");
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;
926 if (da->sort_index < 0 && db->sort_index < 0) {
927 if (da->name == NULL && db->name == NULL) {
930 if (da->name == NULL) {
933 if (db->name == NULL) {
936 return g_utf8_collate(da->name, db->name);
938 return db->sort_index - da->sort_index;
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);
953 fwrite(str, 1, l + 1, fd);
956static void drun_write_integer(FILE *fd, int32_t val) {
957 fwrite(&val,
sizeof(val), 1, fd);
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?");
965static void drun_read_string(FILE *fd,
char **str) {
968 if (fread(&l,
sizeof(l), 1, fd) != 1) {
969 g_warning(
"Failed to read entry, cache corrupt?");
976 (*str) = g_malloc(l);
977 if (fread((*str), 1, l, fd) != l) {
978 g_warning(
"Failed to read entry, cache corrupt?");
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]);
989static void drun_read_stringv(FILE *fd,
char ***str) {
992 if (fread(&vl,
sizeof(vl), 1, fd) != 1) {
993 g_warning(
"Failed to read entry, cache corrupt?");
998 (*str) = g_malloc0((vl + 1) *
sizeof(**str));
999 for (guint index = 0; index < vl; index++) {
1000 drun_read_string(fd, &((*str)[index]));
1005static void write_cache(DRunModePrivateData *pd,
const char *cache_file) {
1006 if (cache_file == NULL ||
config.drun_use_desktop_cache == FALSE) {
1009 TICK_N(
"DRUN Write CACHE: start");
1011 FILE *fd = fopen(cache_file,
"w");
1013 g_warning(
"Failed to write to cache file");
1016 uint8_t version = CACHE_VERSION;
1017 fwrite(&version,
sizeof(version), 1, fd);
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]);
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);
1033 drun_write_strv(fd, entry->categories);
1034 drun_write_strv(fd, entry->keywords);
1036 drun_write_str(fd, entry->comment);
1037 drun_write_str(fd, entry->url);
1038 drun_write_integer(fd, (int32_t)entry->type);
1042 TICK_N(
"DRUN Write CACHE: end");
1048static gboolean drun_read_cache(DRunModePrivateData *pd,
1049 const char *cache_file) {
1050 if (cache_file == NULL ||
config.drun_use_desktop_cache == FALSE) {
1054 if (
config.drun_reload_desktop_cache) {
1057 TICK_N(
"DRUN Read CACHE: start");
1058 FILE *fd = fopen(cache_file,
"r");
1060 TICK_N(
"DRUN Read CACHE: stop");
1065 uint8_t version = 0;
1067 if (fread(&version,
sizeof(version), 1, fd) != 1) {
1069 g_warning(
"Cache corrupt, ignoring.");
1070 TICK_N(
"DRUN Read CACHE: stop");
1074 if (version != CACHE_VERSION) {
1076 g_warning(
"Cache file wrong version, ignoring.");
1077 TICK_N(
"DRUN Read CACHE: stop");
1081 if (fread(&(pd->cmd_list_length),
sizeof(pd->cmd_list_length), 1, fd) != 1) {
1083 g_warning(
"Cache corrupt, ignoring.");
1084 TICK_N(
"DRUN Read CACHE: stop");
1088 pd->cmd_list_length_actual = pd->cmd_list_length;
1091 g_malloc0(pd->cmd_list_length_actual *
sizeof(*(pd->entry_list)));
1093 for (
unsigned int index = 0; index < pd->cmd_list_length; index++) {
1094 DRunModeEntry *entry = &(pd->entry_list[index]);
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));
1106 drun_read_stringv(fd, &(entry->categories));
1107 drun_read_stringv(fd, &(entry->keywords));
1109 drun_read_string(fd, &(entry->comment));
1110 drun_read_string(fd, &(entry->url));
1112 drun_read_integer(fd, &(type));
1117 TICK_N(
"DRUN Read CACHE: stop");
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)) {
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");
1142 dir = g_build_filename(g_get_user_data_dir(),
"applications", NULL);
1143 walk_dir(pd, dir, dir, TRUE);
1145 TICK_N(
"Get Desktop apps (user dir)");
1152 const gchar *
const *sys = g_get_system_data_dirs();
1153 for (
const gchar *
const *iter = sys; *iter != NULL; ++iter) {
1154 gboolean unique = TRUE;
1156 for (
const gchar *
const *iterd = sys; iterd != iter; ++iterd) {
1157 if (g_strcmp0(*iter, *iterd) == 0) {
1162 if (unique && (**iter) !=
'\0') {
1163 char *dir = g_build_filename(*iter,
"applications", NULL);
1164 walk_dir(pd, dir, dir, TRUE);
1168 TICK_N(
"Get Desktop apps (system dirs)");
1170 get_apps_history(pd);
1172 g_qsort_with_data(pd->entry_list, pd->cmd_list_length,
1173 sizeof(DRunModeEntry), drun_int_sort_list, NULL);
1177 write_cache(pd, cache_file);
1182static void drun_mode_parse_entry_fields(
void) {
1183 char *savept = NULL;
1185 char *switcher_str = g_strdup(
config.drun_match_fields);
1186 const char *
const sep =
",#";
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;
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;
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;
1211 g_warning(
"Invalid entry name :%s", token);
1215 g_free(switcher_str);
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)
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;
1228 g_free(search_term);
1232static int drun_mode_init(
Mode *sw) {
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);
1241 const char *current_desktop = g_getenv(
"XDG_CURRENT_DESKTOP");
1242 pd->current_desktop_list =
1243 current_desktop ? g_strsplit(current_desktop,
":", 0) : NULL;
1245 if (
config.drun_categories && *(
config.drun_categories)) {
1246 pd->show_categories = g_strsplit(
config.drun_categories,
",", 0);
1249 drun_mode_parse_entry_fields();
1250 drun_mode_parse_display_format();
1253 pd->completer = NULL;
1256static void drun_entry_clear(DRunModeEntry *e) {
1260 g_free(e->desktop_id);
1261 if (e->icon != NULL) {
1262 cairo_surface_destroy(e->icon);
1264 g_free(e->icon_name);
1267 g_free(e->generic_name);
1269 if (e->action != DRUN_GROUP_NAME) {
1272 g_strfreev(e->categories);
1273 g_strfreev(e->keywords);
1275 g_key_file_free(e->key_file);
1279static ModeMode drun_mode_result(
Mode *sw,
int mretv,
char **input,
1280 unsigned int selected_line) {
1284 if (rmpd->file_complete == TRUE) {
1289 g_free(rmpd->old_completer_input);
1290 rmpd->old_completer_input = *input;
1292 if (rmpd->selected_line < rmpd->cmd_list_length) {
1293 (*input) = g_strdup(rmpd->old_input);
1295 rmpd->file_complete = FALSE;
1303 exec_cmd_entry(&(rmpd->entry_list[rmpd->selected_line]), path);
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);
1315 case DRUN_DESKTOP_ENTRY_TYPE_LINK:
1316 launch_link_entry(&(rmpd->entry_list[selected_line]));
1319 g_assert_not_reached();
1322 *input[0] !=
'\0') {
1327 run_in_term ? &context : NULL)) {
1331 selected_line < rmpd->cmd_list_length) {
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--;
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);
1353 if (g_regex_match(regex, rmpd->entry_list[selected_line].exec, 0,
1355 rmpd->selected_line = selected_line;
1358 g_free(rmpd->old_input);
1359 rmpd->old_input = g_strdup(*input);
1363 *input = g_strdup(rmpd->old_completer_input);
1369 rmpd->file_complete = TRUE;
1372 g_regex_unref(regex);
1381static void drun_mode_destroy(
Mode *sw) {
1384 for (
size_t i = 0; i < rmpd->cmd_list_length; i++) {
1385 drun_entry_clear(&(rmpd->entry_list[i]));
1387 g_hash_table_destroy(rmpd->disabled_entries);
1388 g_free(rmpd->entry_list);
1390 g_free(rmpd->old_completer_input);
1391 g_free(rmpd->old_input);
1392 if (rmpd->completer != NULL) {
1394 g_free(rmpd->completer);
1397 g_strfreev(rmpd->current_desktop_list);
1398 g_strfreev(rmpd->show_categories);
1405 int *state, G_GNUC_UNUSED GList **list,
1409 if (pd->file_complete) {
1410 return pd->completer->_get_display_value(pd->completer, selected_line,
1411 state, list, get_entry);
1417 if (pd->entry_list == NULL) {
1419 return g_strdup(
"Failed");
1422 DRunModeEntry *dr = &(pd->entry_list[selected_line]);
1424 if (dr->categories) {
1425 char *tcats = g_strjoinv(
",", dr->categories);
1427 cats = g_markup_escape_text(tcats, -1);
1431 gchar *keywords = NULL;
1433 char *tkeyw = g_strjoinv(
",", dr->keywords);
1435 keywords = g_markup_escape_text(tkeyw, -1);
1445 if (dr->generic_name) {
1446 egn = g_markup_escape_text(dr->generic_name, -1);
1449 en = g_markup_escape_text(dr->name, -1);
1452 ec = g_markup_escape_text(dr->comment, -1);
1455 eu = g_markup_escape_text(dr->url, -1);
1458 ee = g_markup_escape_text(dr->exec, -1);
1462 config.drun_display_format,
"{generic}", egn,
"{name}", en,
"{comment}",
1463 ec,
"{exec}", ee,
"{categories}", cats,
"{keywords}", keywords,
"{url}",
1475static cairo_surface_t *
_get_icon(
const Mode *sw,
unsigned int selected_line,
1476 unsigned int height) {
1478 if (pd->file_complete) {
1479 return pd->completer->_get_icon(pd->completer, selected_line, height);
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) {
1489 dr->icon_fetch_size = height;
1496static char *drun_get_completion(
const Mode *sw,
unsigned int index) {
1499 DRunModeEntry *dr = &(pd->entry_list[index]);
1500 if (dr->generic_name == NULL) {
1501 return g_strdup(dr->name);
1503 return g_strdup_printf(
"%s", dr->name);
1507 unsigned int index) {
1508 DRunModePrivateData *rmpd =
1510 if (rmpd->file_complete) {
1511 return rmpd->completer->_token_match(rmpd->completer, tokens, index);
1515 for (
int j = 0; match && tokens[j] != NULL; j++) {
1519 if (matching_entry_fields[DRUN_MATCH_FIELD_NAME].enabled_match) {
1520 if (rmpd->entry_list[index].name) {
1524 if (matching_entry_fields[DRUN_MATCH_FIELD_GENERIC].enabled_match) {
1526 if (test == tokens[j]->invert && rmpd->entry_list[index].generic_name) {
1531 if (matching_entry_fields[DRUN_MATCH_FIELD_EXEC].enabled_match) {
1533 if (test == tokens[j]->invert && rmpd->entry_list[index].exec) {
1537 if (matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_match) {
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];
1547 if (matching_entry_fields[DRUN_MATCH_FIELD_KEYWORDS].enabled_match) {
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];
1557 if (matching_entry_fields[DRUN_MATCH_FIELD_URL].enabled_match) {
1560 if (test == tokens[j]->invert && rmpd->entry_list[index].url) {
1564 if (matching_entry_fields[DRUN_MATCH_FIELD_COMMENT].enabled_match) {
1567 if (test == tokens[j]->invert && rmpd->entry_list[index].comment) {
1580static unsigned int drun_mode_get_num_entries(
const Mode *sw) {
1581 const DRunModePrivateData *pd =
1583 if (pd->file_complete) {
1584 return pd->completer->_get_num_entries(pd->completer);
1586 return pd->cmd_list_length;
1588static char *drun_get_message(
const Mode *sw) {
1590 if (pd->file_complete) {
1591 if (pd->selected_line < pd->cmd_list_length) {
1595 g_strdup_printf(
"File complete for: %s\n%s",
1596 pd->entry_list[pd->selected_line].name, msg);
1600 return g_strdup_printf(
"File complete for: %s",
1601 pd->entry_list[pd->selected_line].name);
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,
1619 ._preprocess_input = NULL,
1620 .private_data = NULL,
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]
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)
static void get_apps(KeysHelpModePrivateData *pd)
struct rofi_int_matcher_t rofi_int_matcher