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;
230 gboolean disable_dbusactivate;
239static gboolean drun_helper_eval_cb(
const GMatchInfo *info, GString *res,
242 struct RegexEvalArg *e = (
struct RegexEvalArg *)data;
246 match = g_match_info_fetch(info, 0);
254 g_string_append(res, e->path);
260 if (e->e && e->e->icon) {
261 g_string_append_printf(res,
"--icon %s", e->e->icon_name);
273 g_string_append(res,
"%");
277 char *esc = g_shell_quote(e->e->path);
278 g_string_append(res, esc);
284 char *esc = g_shell_quote(e->e->name);
285 g_string_append(res, esc);
300static void launch_link_entry(DRunModeEntry *e) {
301 if (e->key_file == NULL) {
302 GKeyFile *kf = g_key_file_new();
303 GError *error = NULL;
304 gboolean res = g_key_file_load_from_file(kf, e->path, 0, &error);
308 g_warning(
"[%s] [%s] Failed to parse desktop file because: %s.",
309 e->app_id, e->path, error->message);
316 gchar *url = g_key_file_get_string(e->key_file, e->action,
"URL", NULL);
317 if (url == NULL || strlen(url) == 0) {
318 g_warning(
"[%s] [%s] No URL found.", e->app_id, e->path);
323 gsize command_len = strlen(
config.drun_url_launcher) + strlen(url) +
325 gchar *command = g_newa(gchar, command_len);
326 g_snprintf(command, command_len,
"%s %s",
config.drun_url_launcher, url);
329 g_debug(
"Link launch command: |%s|", command);
331 char *path = g_build_filename(
cache_dir, DRUN_CACHE_FILE, NULL);
337static gchar *app_path_for_id(
const gchar *app_id) {
341 path = g_strconcat(
"/", app_id, NULL);
342 for (i = 0; path[i]; i++) {
351static GVariant *app_get_platform_data(
void) {
352 GVariantBuilder builder;
353 const gchar *startup_id;
355 g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
357 if ((startup_id = g_getenv(
"DESKTOP_STARTUP_ID")))
358 g_variant_builder_add(&builder,
"{sv}",
"desktop-startup-id",
359 g_variant_new_string(startup_id));
361 if ((startup_id = g_getenv(
"XDG_ACTIVATION_TOKEN")))
362 g_variant_builder_add(&builder,
"{sv}",
"activation-token",
363 g_variant_new_string(startup_id));
365 return g_variant_builder_end(&builder);
368static gboolean exec_dbus_entry(DRunModeEntry *e,
const char *path) {
369 GVariantBuilder files;
370 GDBusConnection *session;
371 GError *error = NULL;
374 GVariant *params = NULL;
375 const char *method =
"Activate";
376 g_debug(
"Trying to launch desktop file using dbus activation.");
378 session = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
380 g_warning(
"unable to connect to D-Bus: %s\n", error->message);
385 object_path = app_path_for_id(e->app_id);
387 g_variant_builder_init(&files, G_VARIANT_TYPE_STRING_ARRAY);
391 params = g_variant_new(
"(as@a{sv})", &files, app_get_platform_data());
393 params = g_variant_new(
"(@a{sv})", app_get_platform_data());
396 GFile *file = g_file_new_for_commandline_arg(path);
397 g_variant_builder_add_value(
398 &files, g_variant_new_take_string(g_file_get_uri(file)));
399 g_object_unref(file);
402 result = g_dbus_connection_call_sync(
403 session, e->app_id, object_path,
"org.freedesktop.Application", method,
404 params, G_VARIANT_TYPE_UNIT, G_DBUS_CALL_FLAGS_NONE, 1500, NULL, &error);
409 g_variant_unref(result);
411 g_warning(
"error sending %s message to application: %s\n",
"Open",
414 g_object_unref(session);
417 g_object_unref(session);
421static void exec_cmd_entry(DRunModePrivateData *pd, DRunModeEntry *e,
423 GError *error = NULL;
424 GRegex *reg = g_regex_new(
"%[a-zA-Z%]", 0, 0, &error);
426 g_warning(
"Internal error, failed to create regex: %s.", error->message);
430 struct RegexEvalArg earg = {.e = e, .path = path, .success = TRUE};
431 char *str = g_regex_replace_eval(reg, e->exec, -1, 0, 0, drun_helper_eval_cb,
434 g_warning(
"Internal error, failed replace field codes: %s.",
440 if (earg.success == FALSE) {
441 g_warning(
"Invalid field code in Exec line: %s.", e->exec);
446 g_warning(
"Nothing to execute after processing: %s.", e->exec);
450 g_debug(
"Parsed command: |%s| into |%s|.", e->exec, str);
452 if (e->key_file == NULL) {
453 GKeyFile *kf = g_key_file_new();
454 GError *key_error = NULL;
455 gboolean res = g_key_file_load_from_file(kf, e->path, 0, &key_error);
459 g_warning(
"[%s] [%s] Failed to parse desktop file because: %s.",
460 e->app_id, e->path, key_error->message);
461 g_error_free(key_error);
468 const gchar *fp = g_strstrip(str);
470 g_key_file_get_string(e->key_file, e->action,
"Path", NULL);
471 if (exec_path != NULL && strlen(exec_path) == 0) {
479 .icon = e->icon_name,
483 g_key_file_get_boolean(e->key_file, e->action,
"StartupNotify", NULL);
484 gchar *wmclass = NULL;
486 g_key_file_has_key(e->key_file, e->action,
"StartupWMClass", NULL)) {
488 g_key_file_get_string(e->key_file, e->action,
"StartupWMClass", NULL);
494 gboolean launched = FALSE;
495 if (!(pd->disable_dbusactivate)) {
496 if (g_key_file_get_boolean(e->key_file, e->action,
"DBusActivatable",
498 printf(
"DBus launch\n");
499 launched = exec_dbus_entry(e, path);
502 if (launched == FALSE) {
508 g_key_file_get_boolean(e->key_file, e->action,
"Terminal", NULL);
510 char *drun_cach_path = g_build_filename(
cache_dir, DRUN_CACHE_FILE, NULL);
513 g_free(drun_cach_path);
521static gboolean rofi_strv_contains(
const char *
const *categories,
522 const char *
const *field) {
523 for (
int i = 0; categories && categories[i]; i++) {
524 for (
int j = 0; field[j]; j++) {
525 if (g_str_equal(categories[i], field[j])) {
535static void read_desktop_file(DRunModePrivateData *pd,
const char *root,
536 const char *path,
const gchar *basename,
537 const char *action) {
538 DRunDesktopEntryType desktop_entry_type =
539 DRUN_DESKTOP_ENTRY_TYPE_UNDETERMINED;
540 int parse_action = (
config.drun_show_actions && action != DRUN_GROUP_NAME);
543 const ssize_t id_len = strlen(path) - strlen(root);
545 g_strlcpy(
id, &(path[strlen(root) + 1]), id_len);
546 for (
int index = 0; index < id_len; index++) {
547 if (
id[index] ==
'/') {
553 if (g_hash_table_contains(pd->disabled_entries,
id) && !parse_action) {
554 g_debug(
"[%s] [%s] Skipping, was previously seen.",
id, path);
557 GKeyFile *kf = g_key_file_new();
558 GError *error = NULL;
559 gboolean res = g_key_file_load_from_file(kf, path, 0, &error);
562 g_debug(
"[%s] [%s] Failed to parse desktop file because: %s.",
id, path,
569 if (g_key_file_has_group(kf, action) == FALSE) {
571 g_debug(
"[%s] [%s] Invalid desktop file: No %s group",
id, path, action);
576 gchar *key = g_key_file_get_string(kf, DRUN_GROUP_NAME,
"Type", NULL);
579 g_debug(
"[%s] [%s] Invalid desktop file: No type indicated",
id, path);
583 if (!g_strcmp0(key,
"Application")) {
584 desktop_entry_type = DRUN_DESKTOP_ENTRY_TYPE_APPLICATION;
585 }
else if (!g_strcmp0(key,
"Link")) {
586 desktop_entry_type = DRUN_DESKTOP_ENTRY_TYPE_LINK;
587 }
else if (!g_strcmp0(key,
"Service")) {
588 desktop_entry_type = DRUN_DESKTOP_ENTRY_TYPE_SERVICE;
589 g_debug(
"Service file detected.");
592 "[%s] [%s] Skipping desktop file: Not of type Application or Link (%s)",
601 if (!g_key_file_has_key(kf, DRUN_GROUP_NAME,
"Name", NULL)) {
602 g_debug(
"[%s] [%s] Invalid desktop file: no 'Name' key present.",
id, path);
608 if (g_key_file_get_boolean(kf, DRUN_GROUP_NAME,
"Hidden", NULL)) {
610 "[%s] [%s] Adding desktop file to disabled list: 'Hidden' key is true",
613 g_hash_table_add(pd->disabled_entries, g_strdup(
id));
616 if (pd->current_desktop_list) {
617 gboolean show = TRUE;
619 if (g_key_file_has_key(kf, DRUN_GROUP_NAME,
"OnlyShowIn", NULL)) {
622 gchar **list = g_key_file_get_string_list(kf, DRUN_GROUP_NAME,
623 "OnlyShowIn", &llength, NULL);
625 for (gsize lcd = 0; !show && pd->current_desktop_list[lcd]; lcd++) {
626 for (gsize lle = 0; !show && lle < llength; lle++) {
627 show = (g_strcmp0(pd->current_desktop_list[lcd], list[lle]) == 0);
633 if (show && g_key_file_has_key(kf, DRUN_GROUP_NAME,
"NotShowIn", NULL)) {
635 gchar **list = g_key_file_get_string_list(kf, DRUN_GROUP_NAME,
636 "NotShowIn", &llength, NULL);
638 for (gsize lcd = 0; show && pd->current_desktop_list[lcd]; lcd++) {
639 for (gsize lle = 0; show && lle < llength; lle++) {
640 show = !(g_strcmp0(pd->current_desktop_list[lcd], list[lle]) == 0);
648 g_debug(
"[%s] [%s] Adding desktop file to disabled list: "
649 "'OnlyShowIn'/'NotShowIn' keys don't match current desktop",
652 g_hash_table_add(pd->disabled_entries, g_strdup(
id));
657 if (g_key_file_get_boolean(kf, DRUN_GROUP_NAME,
"NoDisplay", NULL)) {
658 g_debug(
"[%s] [%s] Adding desktop file to disabled list: 'NoDisplay' key "
662 g_hash_table_add(pd->disabled_entries, g_strdup(
id));
667 if (desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_APPLICATION &&
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_SERVICE &&
676 !g_key_file_has_key(kf, DRUN_GROUP_NAME,
"Exec", NULL)) {
677 g_debug(
"[%s] [%s] Unsupported desktop file: no 'Exec' key present for "
683 if (desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_LINK &&
684 !g_key_file_has_key(kf, DRUN_GROUP_NAME,
"URL", NULL)) {
685 g_debug(
"[%s] [%s] Unsupported desktop file: no 'URL' key present for type "
692 if (g_key_file_has_key(kf, DRUN_GROUP_NAME,
"TryExec", NULL)) {
693 char *te = g_key_file_get_string(kf, DRUN_GROUP_NAME,
"TryExec", NULL);
694 if (!g_path_is_absolute(te)) {
695 char *fp = g_find_program_in_path(te);
703 if (g_file_test(te, G_FILE_TEST_IS_EXECUTABLE) == FALSE) {
712 char **categories = NULL;
713 if (pd->show_categories) {
714 categories = g_key_file_get_locale_string_list(
715 kf, DRUN_GROUP_NAME,
"Categories", NULL, NULL, NULL);
716 if (!rofi_strv_contains((
const char *
const *)categories,
717 (
const char *
const *)pd->show_categories)) {
718 g_strfreev(categories);
724 size_t nl = ((pd->cmd_list_length) + 1);
725 if (nl >= pd->cmd_list_length_actual) {
726 pd->cmd_list_length_actual += 256;
727 pd->entry_list = g_realloc(pd->entry_list, pd->cmd_list_length_actual *
728 sizeof(*(pd->entry_list)));
732 if (G_UNLIKELY(pd->cmd_list_length > INT_MAX)) {
734 pd->entry_list[pd->cmd_list_length].sort_index = INT_MIN;
736 pd->entry_list[pd->cmd_list_length].sort_index = -nl;
738 pd->entry_list[pd->cmd_list_length].icon_size = 0;
739 pd->entry_list[pd->cmd_list_length].icon_fetch_uid = 0;
740 pd->entry_list[pd->cmd_list_length].icon_fetch_size = 0;
741 pd->entry_list[pd->cmd_list_length].root = g_strdup(root);
742 pd->entry_list[pd->cmd_list_length].path = g_strdup(path);
743 pd->entry_list[pd->cmd_list_length].desktop_id = g_strdup(
id);
744 pd->entry_list[pd->cmd_list_length].app_id =
745 g_strndup(basename, strlen(basename) - strlen(
".desktop"));
747 g_key_file_get_locale_string(kf, DRUN_GROUP_NAME,
"Name", NULL, NULL);
749 if (action != DRUN_GROUP_NAME) {
750 gchar *na = g_key_file_get_locale_string(kf, action,
"Name", NULL, NULL);
751 gchar *l = g_strdup_printf(
"%s - %s", n, na);
755 pd->entry_list[pd->cmd_list_length].name = n;
756 pd->entry_list[pd->cmd_list_length].action = DRUN_GROUP_NAME;
757 gchar *gn = g_key_file_get_locale_string(kf, DRUN_GROUP_NAME,
"GenericName",
759 pd->entry_list[pd->cmd_list_length].generic_name = gn;
760 if (matching_entry_fields[DRUN_MATCH_FIELD_KEYWORDS].enabled_match ||
761 matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_display) {
762 pd->entry_list[pd->cmd_list_length].keywords =
763 g_key_file_get_locale_string_list(kf, DRUN_GROUP_NAME,
"Keywords", NULL,
766 pd->entry_list[pd->cmd_list_length].keywords = NULL;
769 if (matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_match ||
770 matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_display) {
772 pd->entry_list[pd->cmd_list_length].categories = categories;
775 pd->entry_list[pd->cmd_list_length].categories =
776 g_key_file_get_locale_string_list(kf, DRUN_GROUP_NAME,
"Categories",
780 pd->entry_list[pd->cmd_list_length].categories = NULL;
782 g_strfreev(categories);
784 pd->entry_list[pd->cmd_list_length].type = desktop_entry_type;
785 if (desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_APPLICATION ||
786 desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_SERVICE) {
787 pd->entry_list[pd->cmd_list_length].exec =
788 g_key_file_get_string(kf, action,
"Exec", NULL);
790 pd->entry_list[pd->cmd_list_length].exec = NULL;
793 if (matching_entry_fields[DRUN_MATCH_FIELD_COMMENT].enabled_match ||
794 matching_entry_fields[DRUN_MATCH_FIELD_COMMENT].enabled_display) {
795 pd->entry_list[pd->cmd_list_length].comment = g_key_file_get_locale_string(
796 kf, DRUN_GROUP_NAME,
"Comment", NULL, NULL);
798 pd->entry_list[pd->cmd_list_length].comment = NULL;
800 if (matching_entry_fields[DRUN_MATCH_FIELD_URL].enabled_match ||
801 matching_entry_fields[DRUN_MATCH_FIELD_URL].enabled_display) {
802 pd->entry_list[pd->cmd_list_length].url =
803 g_key_file_get_locale_string(kf, DRUN_GROUP_NAME,
"URL", NULL, NULL);
805 pd->entry_list[pd->cmd_list_length].url = NULL;
807 pd->entry_list[pd->cmd_list_length].icon_name =
808 g_key_file_get_locale_string(kf, DRUN_GROUP_NAME,
"Icon", NULL, NULL);
809 pd->entry_list[pd->cmd_list_length].icon = NULL;
812 pd->entry_list[pd->cmd_list_length].key_file = kf;
814 g_hash_table_add(pd->disabled_entries, g_strdup(
id));
815 g_debug(
"[%s] Using file %s.",
id, path);
816 (pd->cmd_list_length)++;
819 gsize actions_length = 0;
820 char **actions = g_key_file_get_string_list(kf, DRUN_GROUP_NAME,
"Actions",
821 &actions_length, NULL);
822 for (gsize iter = 0; iter < actions_length; iter++) {
823 char *new_action = g_strdup_printf(
"Desktop Action %s", actions[iter]);
824 read_desktop_file(pd, root, path, basename, new_action);
835static void walk_dir(DRunModePrivateData *pd,
const char *root,
836 const char *dirname,
const gboolean recursive) {
839 g_debug(
"Checking directory %s for desktop files.", dirname);
840 dir = opendir(dirname);
846 gchar *filename = NULL;
848 while ((file = readdir(dir)) != NULL) {
849 if (file->d_name[0] ==
'.') {
852 switch (file->d_type) {
857 filename = g_build_filename(dirname, file->d_name, NULL);
865 if (file->d_type == DT_LNK || file->d_type == DT_UNKNOWN) {
866 file->d_type = DT_UNKNOWN;
867 if (stat(filename, &st) == 0) {
868 if (S_ISDIR(st.st_mode)) {
869 file->d_type = DT_DIR;
870 }
else if (S_ISREG(st.st_mode)) {
871 file->d_type = DT_REG;
876 switch (file->d_type) {
879 if (g_str_has_suffix(file->d_name,
".desktop")) {
880 read_desktop_file(pd, root, filename, file->d_name, DRUN_GROUP_NAME);
885 walk_dir(pd, root, filename, recursive);
900static void delete_entry_history(
const DRunModeEntry *entry) {
901 char *path = g_build_filename(
cache_dir, DRUN_CACHE_FILE, NULL);
906static void get_apps_history(DRunModePrivateData *pd) {
907 TICK_N(
"Start drun history");
908 unsigned int length = 0;
909 gchar *path = g_build_filename(
cache_dir, DRUN_CACHE_FILE, NULL);
911 for (
unsigned int index = 0; index < length; index++) {
912 for (
size_t i = 0; i < pd->cmd_list_length; i++) {
913 if (g_strcmp0(pd->entry_list[i].desktop_id, retv[index]) == 0) {
914 unsigned int sort_index = length - index;
915 if (G_LIKELY(sort_index < INT_MAX)) {
916 pd->entry_list[i].sort_index = sort_index;
919 pd->entry_list[i].sort_index = INT_MAX;
926 TICK_N(
"Stop drun history");
929static gint drun_int_sort_list(gconstpointer a, gconstpointer b,
930 G_GNUC_UNUSED gpointer user_data) {
931 DRunModeEntry *da = (DRunModeEntry *)a;
932 DRunModeEntry *db = (DRunModeEntry *)b;
934 if (da->sort_index < 0 && db->sort_index < 0) {
935 if (da->name == NULL && db->name == NULL) {
938 if (da->name == NULL) {
941 if (db->name == NULL) {
944 return g_utf8_collate(da->name, db->name);
946 return db->sort_index - da->sort_index;
954#define CACHE_VERSION 3
955static void drun_write_str(FILE *fd,
const char *str) {
956 size_t l = (str == NULL ? 0 : strlen(str));
957 fwrite(&l,
sizeof(l), 1, fd);
961 fwrite(str, 1, l + 1, fd);
964static void drun_write_integer(FILE *fd, int32_t val) {
965 fwrite(&val,
sizeof(val), 1, fd);
967static void drun_read_integer(FILE *fd, int32_t *type) {
968 if (fread(type,
sizeof(int32_t), 1, fd) != 1) {
969 g_warning(
"Failed to read entry, cache corrupt?");
973static void drun_read_string(FILE *fd,
char **str) {
976 if (fread(&l,
sizeof(l), 1, fd) != 1) {
977 g_warning(
"Failed to read entry, cache corrupt?");
984 (*str) = g_malloc(l);
985 if (fread((*str), 1, l, fd) != l) {
986 g_warning(
"Failed to read entry, cache corrupt?");
990static void drun_write_strv(FILE *fd,
char **str) {
991 guint vl = (str == NULL ? 0 : g_strv_length(str));
992 fwrite(&vl,
sizeof(vl), 1, fd);
993 for (guint index = 0; index < vl; index++) {
994 drun_write_str(fd, str[index]);
997static void drun_read_stringv(FILE *fd,
char ***str) {
1000 if (fread(&vl,
sizeof(vl), 1, fd) != 1) {
1001 g_warning(
"Failed to read entry, cache corrupt?");
1006 (*str) = g_malloc0((vl + 1) *
sizeof(**str));
1007 for (guint index = 0; index < vl; index++) {
1008 drun_read_string(fd, &((*str)[index]));
1013static void write_cache(DRunModePrivateData *pd,
const char *cache_file) {
1014 if (cache_file == NULL ||
config.drun_use_desktop_cache == FALSE) {
1017 TICK_N(
"DRUN Write CACHE: start");
1019 FILE *fd = fopen(cache_file,
"w");
1021 g_warning(
"Failed to write to cache file");
1024 uint8_t version = CACHE_VERSION;
1025 fwrite(&version,
sizeof(version), 1, fd);
1027 fwrite(&(pd->cmd_list_length),
sizeof(pd->cmd_list_length), 1, fd);
1028 for (
unsigned int index = 0; index < pd->cmd_list_length; index++) {
1029 DRunModeEntry *entry = &(pd->entry_list[index]);
1031 drun_write_str(fd, entry->action);
1032 drun_write_str(fd, entry->root);
1033 drun_write_str(fd, entry->path);
1034 drun_write_str(fd, entry->app_id);
1035 drun_write_str(fd, entry->desktop_id);
1036 drun_write_str(fd, entry->icon_name);
1037 drun_write_str(fd, entry->exec);
1038 drun_write_str(fd, entry->name);
1039 drun_write_str(fd, entry->generic_name);
1041 drun_write_strv(fd, entry->categories);
1042 drun_write_strv(fd, entry->keywords);
1044 drun_write_str(fd, entry->comment);
1045 drun_write_str(fd, entry->url);
1046 drun_write_integer(fd, (int32_t)entry->type);
1050 TICK_N(
"DRUN Write CACHE: end");
1056static gboolean drun_read_cache(DRunModePrivateData *pd,
1057 const char *cache_file) {
1058 if (cache_file == NULL ||
config.drun_use_desktop_cache == FALSE) {
1062 if (
config.drun_reload_desktop_cache) {
1065 TICK_N(
"DRUN Read CACHE: start");
1066 FILE *fd = fopen(cache_file,
"r");
1068 TICK_N(
"DRUN Read CACHE: stop");
1073 uint8_t version = 0;
1075 if (fread(&version,
sizeof(version), 1, fd) != 1) {
1077 g_warning(
"Cache corrupt, ignoring.");
1078 TICK_N(
"DRUN Read CACHE: stop");
1082 if (version != CACHE_VERSION) {
1084 g_warning(
"Cache file wrong version, ignoring.");
1085 TICK_N(
"DRUN Read CACHE: stop");
1089 if (fread(&(pd->cmd_list_length),
sizeof(pd->cmd_list_length), 1, fd) != 1) {
1091 g_warning(
"Cache corrupt, ignoring.");
1092 TICK_N(
"DRUN Read CACHE: stop");
1096 pd->cmd_list_length_actual = pd->cmd_list_length;
1099 g_malloc0(pd->cmd_list_length_actual *
sizeof(*(pd->entry_list)));
1101 for (
unsigned int index = 0; index < pd->cmd_list_length; index++) {
1102 DRunModeEntry *entry = &(pd->entry_list[index]);
1104 drun_read_string(fd, &(entry->action));
1105 drun_read_string(fd, &(entry->root));
1106 drun_read_string(fd, &(entry->path));
1107 drun_read_string(fd, &(entry->app_id));
1108 drun_read_string(fd, &(entry->desktop_id));
1109 drun_read_string(fd, &(entry->icon_name));
1110 drun_read_string(fd, &(entry->exec));
1111 drun_read_string(fd, &(entry->name));
1112 drun_read_string(fd, &(entry->generic_name));
1114 drun_read_stringv(fd, &(entry->categories));
1115 drun_read_stringv(fd, &(entry->keywords));
1117 drun_read_string(fd, &(entry->comment));
1118 drun_read_string(fd, &(entry->url));
1120 drun_read_integer(fd, &(type));
1125 TICK_N(
"DRUN Read CACHE: stop");
1129static void get_apps(DRunModePrivateData *pd) {
1130 char *cache_file = g_build_filename(
cache_dir, DRUN_DESKTOP_CACHE_FILE, NULL);
1131 TICK_N(
"Get Desktop apps (start)");
1132 if (drun_read_cache(pd, cache_file)) {
1141 dir = g_get_user_special_dir(G_USER_DIRECTORY_DESKTOP);
1142 walk_dir(pd, dir, dir, FALSE);
1143 TICK_N(
"Get Desktop dir apps");
1150 dir = g_build_filename(g_get_user_data_dir(),
"applications", NULL);
1151 walk_dir(pd, dir, dir, TRUE);
1153 TICK_N(
"Get Desktop apps (user dir)");
1160 const gchar *
const *sys = g_get_system_data_dirs();
1161 for (
const gchar *
const *iter = sys; *iter != NULL; ++iter) {
1162 gboolean unique = TRUE;
1164 for (
const gchar *
const *iterd = sys; iterd != iter; ++iterd) {
1165 if (g_strcmp0(*iter, *iterd) == 0) {
1170 if (unique && (**iter) !=
'\0') {
1171 char *dir = g_build_filename(*iter,
"applications", NULL);
1172 walk_dir(pd, dir, dir, TRUE);
1176 TICK_N(
"Get Desktop apps (system dirs)");
1178 pd->disable_dbusactivate = FALSE;
1181 pd->disable_dbusactivate = TRUE;
1183 get_apps_history(pd);
1185 g_qsort_with_data(pd->entry_list, pd->cmd_list_length,
1186 sizeof(DRunModeEntry), drun_int_sort_list, NULL);
1190 write_cache(pd, cache_file);
1195static void drun_mode_parse_entry_fields(
void) {
1196 char *savept = NULL;
1198 char *switcher_str = g_strdup(
config.drun_match_fields);
1199 const char *
const sep =
",#";
1201 for (
unsigned int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++) {
1202 matching_entry_fields[i].enabled_match = FALSE;
1203 matching_entry_fields[i].enabled_display = FALSE;
1205 for (
char *token = strtok_r(switcher_str, sep, &savept); token != NULL;
1206 token = strtok_r(NULL, sep, &savept)) {
1207 if (strcmp(token,
"all") == 0) {
1208 for (
unsigned int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++) {
1209 matching_entry_fields[i].enabled_match = TRUE;
1210 matching_entry_fields[i].enabled_display = TRUE;
1214 gboolean matched = FALSE;
1215 for (
unsigned int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++) {
1216 const char *entry_name = matching_entry_fields[i].entry_field_name;
1217 if (g_ascii_strcasecmp(token, entry_name) == 0) {
1218 matching_entry_fields[i].enabled_match = TRUE;
1219 matching_entry_fields[i].enabled_display = TRUE;
1224 g_warning(
"Invalid entry name :%s", token);
1228 g_free(switcher_str);
1231static void drun_mode_parse_display_format(
void) {
1232 for (
int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++) {
1233 if (matching_entry_fields[i].enabled_display)
1236 gchar *search_term =
1237 g_strdup_printf(
"{%s}", matching_entry_fields[i].entry_field_name);
1238 if (strstr(
config.drun_display_format, search_term)) {
1239 matching_entry_fields[i].enabled_match = TRUE;
1241 g_free(search_term);
1245static int drun_mode_init(
Mode *sw) {
1249 DRunModePrivateData *pd = g_malloc0(
sizeof(*pd));
1250 pd->disabled_entries =
1251 g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
1254 const char *current_desktop = g_getenv(
"XDG_CURRENT_DESKTOP");
1255 pd->current_desktop_list =
1256 current_desktop ? g_strsplit(current_desktop,
":", 0) : NULL;
1258 if (
config.drun_categories && *(
config.drun_categories)) {
1259 pd->show_categories = g_strsplit(
config.drun_categories,
",", 0);
1262 drun_mode_parse_entry_fields();
1263 drun_mode_parse_display_format();
1266 pd->completer = NULL;
1269static void drun_entry_clear(DRunModeEntry *e) {
1273 g_free(e->desktop_id);
1274 if (e->icon != NULL) {
1275 cairo_surface_destroy(e->icon);
1277 g_free(e->icon_name);
1280 g_free(e->generic_name);
1282 if (e->action != DRUN_GROUP_NAME) {
1285 g_strfreev(e->categories);
1286 g_strfreev(e->keywords);
1288 g_key_file_free(e->key_file);
1292static ModeMode drun_mode_result(
Mode *sw,
int mretv,
char **input,
1293 unsigned int selected_line) {
1297 if (rmpd->file_complete == TRUE) {
1302 g_free(rmpd->old_completer_input);
1303 rmpd->old_completer_input = *input;
1305 if (rmpd->selected_line < rmpd->cmd_list_length) {
1306 (*input) = g_strdup(rmpd->old_input);
1308 rmpd->file_complete = FALSE;
1316 exec_cmd_entry(rmpd, &(rmpd->entry_list[rmpd->selected_line]), path);
1323 switch (rmpd->entry_list[selected_line].type) {
1324 case DRUN_DESKTOP_ENTRY_TYPE_SERVICE:
1325 case DRUN_DESKTOP_ENTRY_TYPE_APPLICATION:
1326 exec_cmd_entry(rmpd, &(rmpd->entry_list[selected_line]), NULL);
1328 case DRUN_DESKTOP_ENTRY_TYPE_LINK:
1329 launch_link_entry(&(rmpd->entry_list[selected_line]));
1332 g_assert_not_reached();
1335 *input[0] !=
'\0') {
1340 run_in_term ? &context : NULL)) {
1344 selected_line < rmpd->cmd_list_length) {
1346 if (rmpd->entry_list[selected_line].sort_index >= 0) {
1347 delete_entry_history(&(rmpd->entry_list[selected_line]));
1348 drun_entry_clear(&(rmpd->entry_list[selected_line]));
1349 memmove(&(rmpd->entry_list[selected_line]),
1350 &rmpd->entry_list[selected_line + 1],
1351 sizeof(DRunModeEntry) *
1352 (rmpd->cmd_list_length - selected_line - 1));
1353 rmpd->cmd_list_length--;
1360 if (selected_line < rmpd->cmd_list_length) {
1361 switch (rmpd->entry_list[selected_line].type) {
1362 case DRUN_DESKTOP_ENTRY_TYPE_SERVICE:
1363 case DRUN_DESKTOP_ENTRY_TYPE_APPLICATION: {
1364 GRegex *regex = g_regex_new(
"%[fFuU]", 0, 0, NULL);
1366 if (g_regex_match(regex, rmpd->entry_list[selected_line].exec, 0,
1368 rmpd->selected_line = selected_line;
1371 g_free(rmpd->old_input);
1372 rmpd->old_input = g_strdup(*input);
1376 *input = g_strdup(rmpd->old_completer_input);
1382 rmpd->file_complete = TRUE;
1385 g_regex_unref(regex);
1394static void drun_mode_destroy(
Mode *sw) {
1397 for (
size_t i = 0; i < rmpd->cmd_list_length; i++) {
1398 drun_entry_clear(&(rmpd->entry_list[i]));
1400 g_hash_table_destroy(rmpd->disabled_entries);
1401 g_free(rmpd->entry_list);
1403 g_free(rmpd->old_completer_input);
1404 g_free(rmpd->old_input);
1405 if (rmpd->completer != NULL) {
1407 g_free(rmpd->completer);
1410 g_strfreev(rmpd->current_desktop_list);
1411 g_strfreev(rmpd->show_categories);
1418 int *state, G_GNUC_UNUSED GList **list,
1422 if (pd->file_complete) {
1423 return pd->completer->_get_display_value(pd->completer, selected_line,
1424 state, list, get_entry);
1430 if (pd->entry_list == NULL) {
1432 return g_strdup(
"Failed");
1435 DRunModeEntry *dr = &(pd->entry_list[selected_line]);
1437 if (dr->categories) {
1438 char *tcats = g_strjoinv(
",", dr->categories);
1440 cats = g_markup_escape_text(tcats, -1);
1444 gchar *keywords = NULL;
1446 char *tkeyw = g_strjoinv(
",", dr->keywords);
1448 keywords = g_markup_escape_text(tkeyw, -1);
1458 if (dr->generic_name) {
1459 egn = g_markup_escape_text(dr->generic_name, -1);
1462 en = g_markup_escape_text(dr->name, -1);
1465 ec = g_markup_escape_text(dr->comment, -1);
1468 eu = g_markup_escape_text(dr->url, -1);
1471 ee = g_markup_escape_text(dr->exec, -1);
1475 config.drun_display_format,
"{generic}", egn,
"{name}", en,
"{comment}",
1476 ec,
"{exec}", ee,
"{categories}", cats,
"{keywords}", keywords,
"{url}",
1488static cairo_surface_t *
_get_icon(
const Mode *sw,
unsigned int selected_line,
1489 unsigned int height) {
1491 if (pd->file_complete) {
1492 return pd->completer->_get_icon(pd->completer, selected_line, height);
1494 g_return_val_if_fail(pd->entry_list != NULL, NULL);
1495 DRunModeEntry *dr = &(pd->entry_list[selected_line]);
1496 if (dr->icon_name != NULL) {
1497 if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height) {
1502 dr->icon_fetch_size = height;
1509static char *drun_get_completion(
const Mode *sw,
unsigned int index) {
1512 DRunModeEntry *dr = &(pd->entry_list[index]);
1513 if (dr->generic_name == NULL) {
1514 return g_strdup(dr->name);
1516 return g_strdup_printf(
"%s", dr->name);
1520 unsigned int index) {
1521 DRunModePrivateData *rmpd =
1523 if (rmpd->file_complete) {
1524 return rmpd->completer->_token_match(rmpd->completer, tokens, index);
1528 for (
int j = 0; match && tokens[j] != NULL; j++) {
1532 if (matching_entry_fields[DRUN_MATCH_FIELD_NAME].enabled_match) {
1533 if (rmpd->entry_list[index].name) {
1537 if (matching_entry_fields[DRUN_MATCH_FIELD_GENERIC].enabled_match) {
1539 if (test == tokens[j]->invert && rmpd->entry_list[index].generic_name) {
1544 if (matching_entry_fields[DRUN_MATCH_FIELD_EXEC].enabled_match) {
1546 if (test == tokens[j]->invert && rmpd->entry_list[index].exec) {
1550 if (matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_match) {
1552 if (test == tokens[j]->invert) {
1553 gchar **list = rmpd->entry_list[index].categories;
1554 for (
int iter = 0; test == tokens[j]->
invert && list && list[iter];
1560 if (matching_entry_fields[DRUN_MATCH_FIELD_KEYWORDS].enabled_match) {
1562 if (test == tokens[j]->invert) {
1563 gchar **list = rmpd->entry_list[index].keywords;
1564 for (
int iter = 0; test == tokens[j]->
invert && list && list[iter];
1570 if (matching_entry_fields[DRUN_MATCH_FIELD_URL].enabled_match) {
1573 if (test == tokens[j]->invert && rmpd->entry_list[index].url) {
1577 if (matching_entry_fields[DRUN_MATCH_FIELD_COMMENT].enabled_match) {
1580 if (test == tokens[j]->invert && rmpd->entry_list[index].comment) {
1593static unsigned int drun_mode_get_num_entries(
const Mode *sw) {
1594 const DRunModePrivateData *pd =
1596 if (pd->file_complete) {
1597 return pd->completer->_get_num_entries(pd->completer);
1599 return pd->cmd_list_length;
1601static char *drun_get_message(
const Mode *sw) {
1603 if (pd->file_complete) {
1604 if (pd->selected_line < pd->cmd_list_length) {
1608 g_strdup_printf(
"File complete for: %s\n%s",
1609 pd->entry_list[pd->selected_line].name, msg);
1613 return g_strdup_printf(
"File complete for: %s",
1614 pd->entry_list[pd->selected_line].name);
1621Mode drun_mode = {.name =
"drun",
1622 .cfg_name_key =
"display-drun",
1623 ._init = drun_mode_init,
1624 ._get_num_entries = drun_mode_get_num_entries,
1625 ._result = drun_mode_result,
1626 ._destroy = drun_mode_destroy,
1627 ._token_match = drun_token_match,
1628 ._get_message = drun_get_message,
1629 ._get_completion = drun_get_completion,
1632 ._preprocess_input = NULL,
1633 .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