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;
218 char **exclude_categories;
221 const gchar *icon_theme;
223 gchar **current_desktop_list;
225 gboolean file_complete;
227 char *old_completer_input;
228 uint32_t selected_line;
231 gboolean disable_dbusactivate;
239static void drun_entry_clear(DRunModeEntry *e);
241static gboolean drun_helper_eval_cb(
const GMatchInfo *info, GString *res,
244 struct RegexEvalArg *e = (
struct RegexEvalArg *)data;
248 match = g_match_info_fetch(info, 0);
256 g_string_append(res, e->path);
262 if (e->e && e->e->icon) {
263 g_string_append_printf(res,
"--icon %s", e->e->icon_name);
275 g_string_append(res,
"%");
279 char *esc = g_shell_quote(e->e->path);
280 g_string_append(res, esc);
286 char *esc = g_shell_quote(e->e->name);
287 g_string_append(res, esc);
302static void launch_link_entry(DRunModeEntry *e) {
303 if (e->key_file == NULL) {
304 GKeyFile *kf = g_key_file_new();
305 GError *error = NULL;
306 gboolean res = g_key_file_load_from_file(kf, e->path, 0, &error);
310 g_warning(
"[%s] [%s] Failed to parse desktop file because: %s.",
311 e->app_id, e->path, error->message);
318 gchar *url = g_key_file_get_string(e->key_file, e->action,
"URL", NULL);
319 if (url == NULL || strlen(url) == 0) {
320 g_warning(
"[%s] [%s] No URL found.", e->app_id, e->path);
325 gsize command_len = strlen(
config.drun_url_launcher) + strlen(url) +
327 gchar *command = g_newa(gchar, command_len);
328 g_snprintf(command, command_len,
"%s %s",
config.drun_url_launcher, url);
331 g_debug(
"Link launch command: |%s|", command);
333 char *path = g_build_filename(
cache_dir, DRUN_CACHE_FILE, NULL);
339static gchar *app_path_for_id(
const gchar *app_id) {
343 path = g_strconcat(
"/", app_id, NULL);
344 for (i = 0; path[i]; i++) {
353static GVariant *app_get_platform_data(
void) {
354 GVariantBuilder builder;
355 const gchar *startup_id;
357 g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
359 if ((startup_id = g_getenv(
"DESKTOP_STARTUP_ID")))
360 g_variant_builder_add(&builder,
"{sv}",
"desktop-startup-id",
361 g_variant_new_string(startup_id));
363 if ((startup_id = g_getenv(
"XDG_ACTIVATION_TOKEN")))
364 g_variant_builder_add(&builder,
"{sv}",
"activation-token",
365 g_variant_new_string(startup_id));
367 return g_variant_builder_end(&builder);
370static gboolean exec_dbus_entry(DRunModeEntry *e,
const char *path) {
371 GVariantBuilder files;
372 GDBusConnection *session;
373 GError *error = NULL;
376 GVariant *params = NULL;
377 const char *method =
"Activate";
378 g_debug(
"Trying to launch desktop file using dbus activation.");
380 session = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
382 g_warning(
"unable to connect to D-Bus: %s\n", error->message);
387 object_path = app_path_for_id(e->app_id);
389 g_variant_builder_init(&files, G_VARIANT_TYPE_STRING_ARRAY);
393 params = g_variant_new(
"(as@a{sv})", &files, app_get_platform_data());
395 params = g_variant_new(
"(@a{sv})", app_get_platform_data());
398 GFile *file = g_file_new_for_commandline_arg(path);
399 g_variant_builder_add_value(
400 &files, g_variant_new_take_string(g_file_get_uri(file)));
401 g_object_unref(file);
404 result = g_dbus_connection_call_sync(
405 session, e->app_id, object_path,
"org.freedesktop.Application", method,
406 params, G_VARIANT_TYPE_UNIT, G_DBUS_CALL_FLAGS_NONE, 1500, NULL, &error);
411 g_variant_unref(result);
413 g_warning(
"error sending %s message to application: %s\n",
"Open",
416 g_object_unref(session);
419 g_object_unref(session);
423static void exec_cmd_entry(DRunModePrivateData *pd, DRunModeEntry *e,
425 GError *error = NULL;
426 GRegex *reg = g_regex_new(
"%[a-zA-Z%]", 0, 0, &error);
428 g_warning(
"Internal error, failed to create regex: %s.", error->message);
432 struct RegexEvalArg earg = {.e = e, .path = path, .success = TRUE};
433 char *str = g_regex_replace_eval(reg, e->exec, -1, 0, 0, drun_helper_eval_cb,
436 g_warning(
"Internal error, failed replace field codes: %s.",
442 if (earg.success == FALSE) {
443 g_warning(
"Invalid field code in Exec line: %s.", e->exec);
448 g_warning(
"Nothing to execute after processing: %s.", e->exec);
452 g_debug(
"Parsed command: |%s| into |%s|.", e->exec, str);
454 if (e->key_file == NULL) {
455 GKeyFile *kf = g_key_file_new();
456 GError *key_error = NULL;
457 gboolean res = g_key_file_load_from_file(kf, e->path, 0, &key_error);
461 g_warning(
"[%s] [%s] Failed to parse desktop file because: %s.",
462 e->app_id, e->path, key_error->message);
463 g_error_free(key_error);
470 const gchar *fp = g_strstrip(str);
472 g_key_file_get_string(e->key_file, e->action,
"Path", NULL);
473 if (exec_path != NULL && strlen(exec_path) == 0) {
481 .icon = e->icon_name,
485 g_key_file_get_boolean(e->key_file, e->action,
"StartupNotify", NULL);
486 gchar *wmclass = NULL;
488 g_key_file_has_key(e->key_file, e->action,
"StartupWMClass", NULL)) {
490 g_key_file_get_string(e->key_file, e->action,
"StartupWMClass", NULL);
496 gboolean launched = FALSE;
497 if (!(pd->disable_dbusactivate)) {
498 if (g_key_file_get_boolean(e->key_file, e->action,
"DBusActivatable",
500 printf(
"DBus launch\n");
501 launched = exec_dbus_entry(e, path);
504 if (launched == FALSE) {
510 g_key_file_get_boolean(e->key_file, e->action,
"Terminal", NULL);
512 char *drun_cach_path = g_build_filename(
cache_dir, DRUN_CACHE_FILE, NULL);
515 g_free(drun_cach_path);
523static gboolean rofi_strv_contains(
const char *
const *categories,
524 const char *
const *field) {
525 for (
int i = 0; categories && categories[i]; i++) {
526 for (
int j = 0; field[j]; j++) {
527 if (g_str_equal(categories[i], field[j])) {
537static void read_desktop_file(DRunModePrivateData *pd,
const char *root,
538 const char *path,
const gchar *basename,
539 const char *action) {
540 DRunDesktopEntryType desktop_entry_type =
541 DRUN_DESKTOP_ENTRY_TYPE_UNDETERMINED;
542 int parse_action = (
config.drun_show_actions && action != DRUN_GROUP_NAME);
545 const ssize_t id_len = strlen(path) - strlen(root);
547 g_strlcpy(
id, &(path[strlen(root) + 1]), id_len);
548 for (
int index = 0; index < id_len; index++) {
549 if (
id[index] ==
'/') {
555 if (g_hash_table_contains(pd->disabled_entries,
id) && !parse_action) {
556 g_debug(
"[%s] [%s] Skipping, was previously seen.",
id, path);
559 GKeyFile *kf = g_key_file_new();
560 GError *error = NULL;
561 gboolean res = g_key_file_load_from_file(kf, path, 0, &error);
564 g_debug(
"[%s] [%s] Failed to parse desktop file because: %s.",
id, path,
571 if (g_key_file_has_group(kf, action) == FALSE) {
573 g_debug(
"[%s] [%s] Invalid desktop file: No %s group",
id, path, action);
578 gchar *key = g_key_file_get_string(kf, DRUN_GROUP_NAME,
"Type", NULL);
581 g_debug(
"[%s] [%s] Invalid desktop file: No type indicated",
id, path);
585 if (!g_strcmp0(key,
"Application")) {
586 desktop_entry_type = DRUN_DESKTOP_ENTRY_TYPE_APPLICATION;
587 }
else if (!g_strcmp0(key,
"Link")) {
588 desktop_entry_type = DRUN_DESKTOP_ENTRY_TYPE_LINK;
589 }
else if (!g_strcmp0(key,
"Service")) {
590 desktop_entry_type = DRUN_DESKTOP_ENTRY_TYPE_SERVICE;
591 g_debug(
"Service file detected.");
594 "[%s] [%s] Skipping desktop file: Not of type Application or Link (%s)",
603 if (!g_key_file_has_key(kf, DRUN_GROUP_NAME,
"Name", NULL)) {
604 g_debug(
"[%s] [%s] Invalid desktop file: no 'Name' key present.",
id, path);
610 if (g_key_file_get_boolean(kf, DRUN_GROUP_NAME,
"Hidden", NULL)) {
612 "[%s] [%s] Adding desktop file to disabled list: 'Hidden' key is true",
615 g_hash_table_add(pd->disabled_entries, g_strdup(
id));
618 if (pd->current_desktop_list) {
619 gboolean show = TRUE;
621 if (g_key_file_has_key(kf, DRUN_GROUP_NAME,
"OnlyShowIn", NULL)) {
624 gchar **list = g_key_file_get_string_list(kf, DRUN_GROUP_NAME,
625 "OnlyShowIn", &llength, NULL);
627 for (gsize lcd = 0; !show && pd->current_desktop_list[lcd]; lcd++) {
628 for (gsize lle = 0; !show && lle < llength; lle++) {
629 show = (g_strcmp0(pd->current_desktop_list[lcd], list[lle]) == 0);
635 if (show && g_key_file_has_key(kf, DRUN_GROUP_NAME,
"NotShowIn", NULL)) {
637 gchar **list = g_key_file_get_string_list(kf, DRUN_GROUP_NAME,
638 "NotShowIn", &llength, NULL);
640 for (gsize lcd = 0; show && pd->current_desktop_list[lcd]; lcd++) {
641 for (gsize lle = 0; show && lle < llength; lle++) {
642 show = !(g_strcmp0(pd->current_desktop_list[lcd], list[lle]) == 0);
650 g_debug(
"[%s] [%s] Adding desktop file to disabled list: "
651 "'OnlyShowIn'/'NotShowIn' keys don't match current desktop",
654 g_hash_table_add(pd->disabled_entries, g_strdup(
id));
659 if (g_key_file_get_boolean(kf, DRUN_GROUP_NAME,
"NoDisplay", NULL)) {
660 g_debug(
"[%s] [%s] Adding desktop file to disabled list: 'NoDisplay' key "
664 g_hash_table_add(pd->disabled_entries, g_strdup(
id));
669 if (desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_APPLICATION &&
670 !g_key_file_has_key(kf, DRUN_GROUP_NAME,
"Exec", NULL)) {
671 g_debug(
"[%s] [%s] Unsupported desktop file: no 'Exec' key present for "
677 if (desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_SERVICE &&
678 !g_key_file_has_key(kf, DRUN_GROUP_NAME,
"Exec", NULL)) {
679 g_debug(
"[%s] [%s] Unsupported desktop file: no 'Exec' key present for "
685 if (desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_LINK &&
686 !g_key_file_has_key(kf, DRUN_GROUP_NAME,
"URL", NULL)) {
687 g_debug(
"[%s] [%s] Unsupported desktop file: no 'URL' key present for type "
694 if (g_key_file_has_key(kf, DRUN_GROUP_NAME,
"TryExec", NULL)) {
695 char *te = g_key_file_get_string(kf, DRUN_GROUP_NAME,
"TryExec", NULL);
696 if (!g_path_is_absolute(te)) {
697 char *fp = g_find_program_in_path(te);
705 if (g_file_test(te, G_FILE_TEST_IS_EXECUTABLE) == FALSE) {
714 char **categories = NULL;
715 if (pd->show_categories) {
716 categories = g_key_file_get_locale_string_list(
717 kf, DRUN_GROUP_NAME,
"Categories", NULL, NULL, NULL);
718 if (!rofi_strv_contains((
const char *
const *)categories,
719 (
const char *
const *)pd->show_categories)) {
720 g_strfreev(categories);
726 if (pd->exclude_categories) {
727 if (categories == NULL) {
728 categories = g_key_file_get_locale_string_list(
729 kf, DRUN_GROUP_NAME,
"Categories", NULL, NULL, NULL);
731 if (rofi_strv_contains((
const char *
const *)categories,
732 (
const char *
const *)pd->exclude_categories)) {
733 g_strfreev(categories);
739 size_t nl = ((pd->cmd_list_length) + 1);
740 if (nl >= pd->cmd_list_length_actual) {
741 pd->cmd_list_length_actual += 256;
742 pd->entry_list = g_realloc(pd->entry_list, pd->cmd_list_length_actual *
743 sizeof(*(pd->entry_list)));
747 if (G_UNLIKELY(pd->cmd_list_length > INT_MAX)) {
749 pd->entry_list[pd->cmd_list_length].sort_index = INT_MIN;
751 pd->entry_list[pd->cmd_list_length].sort_index = -nl;
753 pd->entry_list[pd->cmd_list_length].icon_size = 0;
754 pd->entry_list[pd->cmd_list_length].icon_fetch_uid = 0;
755 pd->entry_list[pd->cmd_list_length].icon_fetch_size = 0;
756 pd->entry_list[pd->cmd_list_length].root = g_strdup(root);
757 pd->entry_list[pd->cmd_list_length].path = g_strdup(path);
758 pd->entry_list[pd->cmd_list_length].desktop_id = g_strdup(
id);
759 pd->entry_list[pd->cmd_list_length].app_id =
760 g_strndup(basename, strlen(basename) - strlen(
".desktop"));
762 g_key_file_get_locale_string(kf, DRUN_GROUP_NAME,
"Name", NULL, NULL);
764 if (action != DRUN_GROUP_NAME) {
765 gchar *na = g_key_file_get_locale_string(kf, action,
"Name", NULL, NULL);
766 gchar *l = g_strdup_printf(
"%s - %s", n, na);
770 pd->entry_list[pd->cmd_list_length].name = n;
771 pd->entry_list[pd->cmd_list_length].action = DRUN_GROUP_NAME;
772 gchar *gn = g_key_file_get_locale_string(kf, DRUN_GROUP_NAME,
"GenericName",
774 pd->entry_list[pd->cmd_list_length].generic_name = gn;
775 if (matching_entry_fields[DRUN_MATCH_FIELD_KEYWORDS].enabled_match ||
776 matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_display) {
777 pd->entry_list[pd->cmd_list_length].keywords =
778 g_key_file_get_locale_string_list(kf, DRUN_GROUP_NAME,
"Keywords", NULL,
781 pd->entry_list[pd->cmd_list_length].keywords = NULL;
784 if (matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_match ||
785 matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_display) {
787 pd->entry_list[pd->cmd_list_length].categories = categories;
790 pd->entry_list[pd->cmd_list_length].categories =
791 g_key_file_get_locale_string_list(kf, DRUN_GROUP_NAME,
"Categories",
795 pd->entry_list[pd->cmd_list_length].categories = NULL;
797 g_strfreev(categories);
799 pd->entry_list[pd->cmd_list_length].type = desktop_entry_type;
800 if (desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_APPLICATION ||
801 desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_SERVICE) {
802 pd->entry_list[pd->cmd_list_length].exec =
803 g_key_file_get_string(kf, action,
"Exec", NULL);
805 pd->entry_list[pd->cmd_list_length].exec = NULL;
808 if (matching_entry_fields[DRUN_MATCH_FIELD_COMMENT].enabled_match ||
809 matching_entry_fields[DRUN_MATCH_FIELD_COMMENT].enabled_display) {
810 pd->entry_list[pd->cmd_list_length].comment = g_key_file_get_locale_string(
811 kf, DRUN_GROUP_NAME,
"Comment", NULL, NULL);
813 pd->entry_list[pd->cmd_list_length].comment = NULL;
815 if (matching_entry_fields[DRUN_MATCH_FIELD_URL].enabled_match ||
816 matching_entry_fields[DRUN_MATCH_FIELD_URL].enabled_display) {
817 pd->entry_list[pd->cmd_list_length].url =
818 g_key_file_get_locale_string(kf, DRUN_GROUP_NAME,
"URL", NULL, NULL);
820 pd->entry_list[pd->cmd_list_length].url = NULL;
822 pd->entry_list[pd->cmd_list_length].icon_name =
823 g_key_file_get_locale_string(kf, DRUN_GROUP_NAME,
"Icon", NULL, NULL);
824 pd->entry_list[pd->cmd_list_length].icon = NULL;
827 pd->entry_list[pd->cmd_list_length].key_file = kf;
829 g_hash_table_add(pd->disabled_entries, g_strdup(
id));
830 g_debug(
"[%s] Using file %s.",
id, path);
831 (pd->cmd_list_length)++;
834 gsize actions_length = 0;
835 char **actions = g_key_file_get_string_list(kf, DRUN_GROUP_NAME,
"Actions",
836 &actions_length, NULL);
837 for (gsize iter = 0; iter < actions_length; iter++) {
838 char *new_action = g_strdup_printf(
"Desktop Action %s", actions[iter]);
839 read_desktop_file(pd, root, path, basename, new_action);
850static void walk_dir(DRunModePrivateData *pd,
const char *root,
851 const char *dirname,
const gboolean recursive) {
854 g_debug(
"Checking directory %s for desktop files.", dirname);
855 dir = opendir(dirname);
861 gchar *filename = NULL;
863 while ((file = readdir(dir)) != NULL) {
864 if (file->d_name[0] ==
'.') {
867 switch (file->d_type) {
872 filename = g_build_filename(dirname, file->d_name, NULL);
880 if (file->d_type == DT_LNK || file->d_type == DT_UNKNOWN) {
881 file->d_type = DT_UNKNOWN;
882 if (stat(filename, &st) == 0) {
883 if (S_ISDIR(st.st_mode)) {
884 file->d_type = DT_DIR;
885 }
else if (S_ISREG(st.st_mode)) {
886 file->d_type = DT_REG;
891 switch (file->d_type) {
894 if (g_str_has_suffix(file->d_name,
".desktop")) {
895 read_desktop_file(pd, root, filename, file->d_name, DRUN_GROUP_NAME);
900 walk_dir(pd, root, filename, recursive);
915static void delete_entry_history(
const DRunModeEntry *entry) {
916 char *path = g_build_filename(
cache_dir, DRUN_CACHE_FILE, NULL);
921static void get_apps_history(DRunModePrivateData *pd) {
922 TICK_N(
"Start drun history");
923 unsigned int length = 0;
924 gchar *path = g_build_filename(
cache_dir, DRUN_CACHE_FILE, NULL);
926 for (
unsigned int index = 0; index < length; index++) {
927 for (
size_t i = 0; i < pd->cmd_list_length; i++) {
928 if (g_strcmp0(pd->entry_list[i].desktop_id, retv[index]) == 0) {
929 unsigned int sort_index = length - index;
930 if (G_LIKELY(sort_index < INT_MAX)) {
931 pd->entry_list[i].sort_index = sort_index;
934 pd->entry_list[i].sort_index = INT_MAX;
941 TICK_N(
"Stop drun history");
944static gint drun_int_sort_list(gconstpointer a, gconstpointer b,
945 G_GNUC_UNUSED gpointer user_data) {
946 DRunModeEntry *da = (DRunModeEntry *)a;
947 DRunModeEntry *db = (DRunModeEntry *)b;
949 if (da->sort_index < 0 && db->sort_index < 0) {
950 if (da->name == NULL && db->name == NULL) {
953 if (da->name == NULL) {
956 if (db->name == NULL) {
959 return g_utf8_collate(da->name, db->name);
961 return db->sort_index - da->sort_index;
969#define CACHE_VERSION 3
970static void drun_write_str(FILE *fd,
const char *str) {
971 size_t l = (str == NULL ? 0 : strlen(str));
972 fwrite(&l,
sizeof(l), 1, fd);
976 fwrite(str, 1, l + 1, fd);
979static void drun_write_integer(FILE *fd, int32_t val) {
980 fwrite(&val,
sizeof(val), 1, fd);
982static gboolean drun_read_integer(FILE *fd, int32_t *type) {
983 if (fread(type,
sizeof(int32_t), 1, fd) != 1) {
984 g_warning(
"Failed to read entry, cache corrupt?");
989static gboolean drun_read_string(FILE *fd,
char **str) {
992 if (fread(&l,
sizeof(l), 1, fd) != 1) {
993 g_warning(
"Failed to read entry, cache corrupt?");
1000 (*str) = g_malloc(l);
1001 if (fread((*str), 1, l, fd) != l) {
1002 g_warning(
"Failed to read entry, cache corrupt?");
1008static void drun_write_strv(FILE *fd,
char **str) {
1009 guint vl = (str == NULL ? 0 : g_strv_length(str));
1010 fwrite(&vl,
sizeof(vl), 1, fd);
1011 for (guint index = 0; index < vl; index++) {
1012 drun_write_str(fd, str[index]);
1015static gboolean drun_read_stringv(FILE *fd,
char ***str) {
1018 if (fread(&vl,
sizeof(vl), 1, fd) != 1) {
1019 g_warning(
"Failed to read entry, cache corrupt?");
1024 (*str) = g_malloc0((vl + 1) *
sizeof(**str));
1025 for (guint index = 0; index < vl; index++) {
1026 if (drun_read_string(fd, &((*str)[index]))) {
1034static void write_cache(DRunModePrivateData *pd,
const char *cache_file) {
1035 if (cache_file == NULL ||
config.drun_use_desktop_cache == FALSE) {
1038 TICK_N(
"DRUN Write CACHE: start");
1040 FILE *fd = fopen(cache_file,
"w");
1042 g_warning(
"Failed to write to cache file");
1045 uint8_t version = CACHE_VERSION;
1046 fwrite(&version,
sizeof(version), 1, fd);
1048 fwrite(&(pd->cmd_list_length),
sizeof(pd->cmd_list_length), 1, fd);
1049 for (
unsigned int index = 0; index < pd->cmd_list_length; index++) {
1050 DRunModeEntry *entry = &(pd->entry_list[index]);
1052 drun_write_str(fd, entry->action);
1053 drun_write_str(fd, entry->root);
1054 drun_write_str(fd, entry->path);
1055 drun_write_str(fd, entry->app_id);
1056 drun_write_str(fd, entry->desktop_id);
1057 drun_write_str(fd, entry->icon_name);
1058 drun_write_str(fd, entry->exec);
1059 drun_write_str(fd, entry->name);
1060 drun_write_str(fd, entry->generic_name);
1062 drun_write_strv(fd, entry->categories);
1063 drun_write_strv(fd, entry->keywords);
1065 drun_write_str(fd, entry->comment);
1066 drun_write_str(fd, entry->url);
1067 drun_write_integer(fd, (int32_t)entry->type);
1071 TICK_N(
"DRUN Write CACHE: end");
1077static gboolean drun_read_cache(DRunModePrivateData *pd,
1078 const char *cache_file) {
1079 if (cache_file == NULL ||
config.drun_use_desktop_cache == FALSE) {
1083 if (
config.drun_reload_desktop_cache) {
1086 TICK_N(
"DRUN Read CACHE: start");
1087 FILE *fd = fopen(cache_file,
"r");
1089 TICK_N(
"DRUN Read CACHE: stop");
1094 uint8_t version = 0;
1096 if (fread(&version,
sizeof(version), 1, fd) != 1) {
1098 g_warning(
"Cache corrupt, ignoring.");
1099 TICK_N(
"DRUN Read CACHE: stop");
1103 if (version != CACHE_VERSION) {
1105 g_warning(
"Cache file wrong version, ignoring.");
1106 TICK_N(
"DRUN Read CACHE: stop");
1110 if (fread(&(pd->cmd_list_length),
sizeof(pd->cmd_list_length), 1, fd) != 1) {
1112 g_warning(
"Cache corrupt, ignoring.");
1113 TICK_N(
"DRUN Read CACHE: stop");
1117 pd->cmd_list_length_actual = pd->cmd_list_length;
1120 g_malloc0(pd->cmd_list_length_actual *
sizeof(*(pd->entry_list)));
1123 for (
unsigned int index = 0; !error && index < pd->cmd_list_length; index++) {
1124 DRunModeEntry *entry = &(pd->entry_list[index]);
1126 if (drun_read_string(fd, &(entry->action))) {
1130 if (drun_read_string(fd, &(entry->root))) {
1134 if (drun_read_string(fd, &(entry->path))) {
1138 if (drun_read_string(fd, &(entry->app_id))) {
1142 if (drun_read_string(fd, &(entry->desktop_id))) {
1146 if (drun_read_string(fd, &(entry->icon_name))) {
1150 if (drun_read_string(fd, &(entry->exec))) {
1154 if (drun_read_string(fd, &(entry->name))) {
1158 if (drun_read_string(fd, &(entry->generic_name))) {
1163 if (drun_read_stringv(fd, &(entry->categories))) {
1167 if (drun_read_stringv(fd, &(entry->keywords))) {
1172 if (drun_read_string(fd, &(entry->comment))) {
1176 if (drun_read_string(fd, &(entry->url))) {
1181 if (drun_read_integer(fd, &(type))) {
1190 for (
size_t i = 0; i < pd->cmd_list_length; i++) {
1191 drun_entry_clear(&(pd->entry_list[i]));
1193 g_free(pd->entry_list);
1194 pd->cmd_list_length = 0;
1195 pd->cmd_list_length_actual = 0;
1198 TICK_N(
"DRUN Read CACHE: stop");
1202static void get_apps(DRunModePrivateData *pd) {
1203 char *cache_file = g_build_filename(
cache_dir, DRUN_DESKTOP_CACHE_FILE, NULL);
1204 TICK_N(
"Get Desktop apps (start)");
1205 if (drun_read_cache(pd, cache_file)) {
1214 dir = g_get_user_special_dir(G_USER_DIRECTORY_DESKTOP);
1215 walk_dir(pd, dir, dir, FALSE);
1216 TICK_N(
"Get Desktop dir apps");
1223 dir = g_build_filename(g_get_user_data_dir(),
"applications", NULL);
1224 walk_dir(pd, dir, dir, TRUE);
1226 TICK_N(
"Get Desktop apps (user dir)");
1233 const gchar *
const *sys = g_get_system_data_dirs();
1234 for (
const gchar *
const *iter = sys; *iter != NULL; ++iter) {
1235 gboolean unique = TRUE;
1237 for (
const gchar *
const *iterd = sys; iterd != iter; ++iterd) {
1238 if (g_strcmp0(*iter, *iterd) == 0) {
1243 if (unique && (**iter) !=
'\0') {
1244 char *dir = g_build_filename(*iter,
"applications", NULL);
1245 walk_dir(pd, dir, dir, TRUE);
1249 TICK_N(
"Get Desktop apps (system dirs)");
1251 pd->disable_dbusactivate = FALSE;
1254 pd->disable_dbusactivate = TRUE;
1256 get_apps_history(pd);
1258 g_qsort_with_data(pd->entry_list, pd->cmd_list_length,
1259 sizeof(DRunModeEntry), drun_int_sort_list, NULL);
1263 write_cache(pd, cache_file);
1265 g_debug(
"Read drun entries from cache.");
1270static void drun_mode_parse_entry_fields(
void) {
1271 char *savept = NULL;
1273 char *switcher_str = g_strdup(
config.drun_match_fields);
1274 const char *
const sep =
",#";
1276 for (
unsigned int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++) {
1277 matching_entry_fields[i].enabled_match = FALSE;
1278 matching_entry_fields[i].enabled_display = FALSE;
1280 for (
char *token = strtok_r(switcher_str, sep, &savept); token != NULL;
1281 token = strtok_r(NULL, sep, &savept)) {
1282 if (strcmp(token,
"all") == 0) {
1283 for (
unsigned int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++) {
1284 matching_entry_fields[i].enabled_match = TRUE;
1285 matching_entry_fields[i].enabled_display = TRUE;
1289 gboolean matched = FALSE;
1290 for (
unsigned int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++) {
1291 const char *entry_name = matching_entry_fields[i].entry_field_name;
1292 if (g_ascii_strcasecmp(token, entry_name) == 0) {
1293 matching_entry_fields[i].enabled_match = TRUE;
1294 matching_entry_fields[i].enabled_display = TRUE;
1299 g_warning(
"Invalid entry name :%s", token);
1303 g_free(switcher_str);
1306static void drun_mode_parse_display_format(
void) {
1307 for (
int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++) {
1308 if (matching_entry_fields[i].enabled_display)
1311 gchar *search_term =
1312 g_strdup_printf(
"{%s}", matching_entry_fields[i].entry_field_name);
1313 if (strstr(
config.drun_display_format, search_term)) {
1314 matching_entry_fields[i].enabled_match = TRUE;
1316 g_free(search_term);
1320static int drun_mode_init(
Mode *sw) {
1324 DRunModePrivateData *pd = g_malloc0(
sizeof(*pd));
1325 pd->disabled_entries =
1326 g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
1329 const char *current_desktop = g_getenv(
"XDG_CURRENT_DESKTOP");
1330 pd->current_desktop_list =
1331 current_desktop ? g_strsplit(current_desktop,
":", 0) : NULL;
1333 if (
config.drun_categories && *(
config.drun_categories)) {
1334 pd->show_categories = g_strsplit(
config.drun_categories,
",", 0);
1337 if (
config.drun_exclude_categories && *(
config.drun_exclude_categories)) {
1338 pd->exclude_categories = g_strsplit(
config.drun_exclude_categories,
",", 0);
1341 drun_mode_parse_entry_fields();
1342 drun_mode_parse_display_format();
1345 pd->completer = NULL;
1348static void drun_entry_clear(DRunModeEntry *e) {
1355 g_free(e->desktop_id);
1356 if (e->icon != NULL) {
1357 cairo_surface_destroy(e->icon);
1359 g_free(e->icon_name);
1362 g_free(e->generic_name);
1364 if (e->action != DRUN_GROUP_NAME) {
1367 g_strfreev(e->categories);
1368 g_strfreev(e->keywords);
1370 g_key_file_free(e->key_file);
1374static ModeMode drun_mode_result(
Mode *sw,
int mretv,
char **input,
1375 unsigned int selected_line) {
1379 if (rmpd->file_complete == TRUE) {
1384 g_free(rmpd->old_completer_input);
1385 rmpd->old_completer_input = *input;
1387 if (rmpd->selected_line < rmpd->cmd_list_length) {
1388 (*input) = g_strdup(rmpd->old_input);
1390 rmpd->file_complete = FALSE;
1398 exec_cmd_entry(rmpd, &(rmpd->entry_list[rmpd->selected_line]), path);
1405 switch (rmpd->entry_list[selected_line].type) {
1406 case DRUN_DESKTOP_ENTRY_TYPE_SERVICE:
1407 case DRUN_DESKTOP_ENTRY_TYPE_APPLICATION:
1408 exec_cmd_entry(rmpd, &(rmpd->entry_list[selected_line]), NULL);
1410 case DRUN_DESKTOP_ENTRY_TYPE_LINK:
1411 launch_link_entry(&(rmpd->entry_list[selected_line]));
1414 g_assert_not_reached();
1417 *input[0] !=
'\0') {
1422 run_in_term ? &context : NULL)) {
1426 selected_line < rmpd->cmd_list_length) {
1428 if (rmpd->entry_list[selected_line].sort_index >= 0) {
1429 delete_entry_history(&(rmpd->entry_list[selected_line]));
1430 drun_entry_clear(&(rmpd->entry_list[selected_line]));
1431 memmove(&(rmpd->entry_list[selected_line]),
1432 &rmpd->entry_list[selected_line + 1],
1433 sizeof(DRunModeEntry) *
1434 (rmpd->cmd_list_length - selected_line - 1));
1435 rmpd->cmd_list_length--;
1442 if (selected_line < rmpd->cmd_list_length) {
1443 switch (rmpd->entry_list[selected_line].type) {
1444 case DRUN_DESKTOP_ENTRY_TYPE_SERVICE:
1445 case DRUN_DESKTOP_ENTRY_TYPE_APPLICATION: {
1446 GRegex *regex = g_regex_new(
"%[fFuU]", 0, 0, NULL);
1448 if (g_regex_match(regex, rmpd->entry_list[selected_line].exec, 0,
1450 rmpd->selected_line = selected_line;
1453 g_free(rmpd->old_input);
1454 rmpd->old_input = g_strdup(*input);
1458 *input = g_strdup(rmpd->old_completer_input);
1464 rmpd->file_complete = TRUE;
1467 g_regex_unref(regex);
1476static void drun_mode_destroy(
Mode *sw) {
1479 for (
size_t i = 0; i < rmpd->cmd_list_length; i++) {
1480 drun_entry_clear(&(rmpd->entry_list[i]));
1482 g_hash_table_destroy(rmpd->disabled_entries);
1483 g_free(rmpd->entry_list);
1485 g_free(rmpd->old_completer_input);
1486 g_free(rmpd->old_input);
1487 if (rmpd->completer != NULL) {
1489 g_free(rmpd->completer);
1492 g_strfreev(rmpd->current_desktop_list);
1493 g_strfreev(rmpd->show_categories);
1494 g_strfreev(rmpd->exclude_categories);
1501 int *state, G_GNUC_UNUSED GList **list,
1505 if (pd->file_complete) {
1506 return pd->completer->_get_display_value(pd->completer, selected_line,
1507 state, list, get_entry);
1513 if (pd->entry_list == NULL) {
1515 return g_strdup(
"Failed");
1518 DRunModeEntry *dr = &(pd->entry_list[selected_line]);
1520 if (dr->categories) {
1521 char *tcats = g_strjoinv(
",", dr->categories);
1523 cats = g_markup_escape_text(tcats, -1);
1527 gchar *keywords = NULL;
1529 char *tkeyw = g_strjoinv(
",", dr->keywords);
1531 keywords = g_markup_escape_text(tkeyw, -1);
1541 if (dr->generic_name) {
1542 egn = g_markup_escape_text(dr->generic_name, -1);
1545 en = g_markup_escape_text(dr->name, -1);
1548 ec = g_markup_escape_text(dr->comment, -1);
1551 eu = g_markup_escape_text(dr->url, -1);
1554 ee = g_markup_escape_text(dr->exec, -1);
1558 config.drun_display_format,
"{generic}", egn,
"{name}", en,
"{comment}",
1559 ec,
"{exec}", ee,
"{categories}", cats,
"{keywords}", keywords,
"{url}",
1571static cairo_surface_t *
_get_icon(
const Mode *sw,
unsigned int selected_line,
1572 unsigned int height) {
1574 if (pd->file_complete) {
1575 return pd->completer->_get_icon(pd->completer, selected_line, height);
1577 g_return_val_if_fail(pd->entry_list != NULL, NULL);
1578 DRunModeEntry *dr = &(pd->entry_list[selected_line]);
1579 if (dr->icon_name != NULL) {
1580 if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height) {
1585 dr->icon_fetch_size = height;
1592static char *drun_get_completion(
const Mode *sw,
unsigned int index) {
1595 DRunModeEntry *dr = &(pd->entry_list[index]);
1596 if (dr->generic_name == NULL) {
1597 return g_strdup(dr->name);
1599 return g_strdup_printf(
"%s", dr->name);
1603 unsigned int index) {
1604 DRunModePrivateData *rmpd =
1606 if (rmpd->file_complete) {
1607 return rmpd->completer->_token_match(rmpd->completer, tokens, index);
1611 for (
int j = 0; match && tokens[j] != NULL; j++) {
1615 if (matching_entry_fields[DRUN_MATCH_FIELD_NAME].enabled_match) {
1616 if (rmpd->entry_list[index].name) {
1620 if (matching_entry_fields[DRUN_MATCH_FIELD_GENERIC].enabled_match) {
1622 if (test == tokens[j]->invert && rmpd->entry_list[index].generic_name) {
1627 if (matching_entry_fields[DRUN_MATCH_FIELD_EXEC].enabled_match) {
1629 if (test == tokens[j]->invert && rmpd->entry_list[index].exec) {
1633 if (matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_match) {
1635 if (test == tokens[j]->invert) {
1636 gchar **list = rmpd->entry_list[index].categories;
1637 for (
int iter = 0; test == tokens[j]->
invert && list && list[iter];
1643 if (matching_entry_fields[DRUN_MATCH_FIELD_KEYWORDS].enabled_match) {
1645 if (test == tokens[j]->invert) {
1646 gchar **list = rmpd->entry_list[index].keywords;
1647 for (
int iter = 0; test == tokens[j]->
invert && list && list[iter];
1653 if (matching_entry_fields[DRUN_MATCH_FIELD_URL].enabled_match) {
1656 if (test == tokens[j]->invert && rmpd->entry_list[index].url) {
1660 if (matching_entry_fields[DRUN_MATCH_FIELD_COMMENT].enabled_match) {
1663 if (test == tokens[j]->invert && rmpd->entry_list[index].comment) {
1676static unsigned int drun_mode_get_num_entries(
const Mode *sw) {
1677 const DRunModePrivateData *pd =
1679 if (pd->file_complete) {
1680 return pd->completer->_get_num_entries(pd->completer);
1682 return pd->cmd_list_length;
1684static char *drun_get_message(
const Mode *sw) {
1686 if (pd->file_complete) {
1687 if (pd->selected_line < pd->cmd_list_length) {
1691 g_strdup_printf(
"File complete for: %s\n%s",
1692 pd->entry_list[pd->selected_line].name, msg);
1696 return g_strdup_printf(
"File complete for: %s",
1697 pd->entry_list[pd->selected_line].name);
1704Mode drun_mode = {.name =
"drun",
1705 .cfg_name_key =
"display-drun",
1706 ._init = drun_mode_init,
1707 ._get_num_entries = drun_mode_get_num_entries,
1708 ._result = drun_mode_result,
1709 ._destroy = drun_mode_destroy,
1710 ._token_match = drun_token_match,
1711 ._get_message = drun_get_message,
1712 ._get_completion = drun_get_completion,
1715 ._preprocess_input = NULL,
1716 .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