2019-09-13 03:25:25 -04:00
|
|
|
/*
|
2024-02-07 21:16:11 -05:00
|
|
|
* Copyright (C) 2019-2024 Scoopta
|
2019-09-13 03:25:25 -04:00
|
|
|
* This file is part of Wofi
|
|
|
|
* Wofi is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Wofi is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Wofi. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-03-26 02:05:46 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <libgen.h>
|
2019-09-13 03:25:25 -04:00
|
|
|
|
2020-03-26 02:05:46 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <map.h>
|
|
|
|
#include <utils.h>
|
|
|
|
#include <config.h>
|
2020-07-11 20:39:09 -04:00
|
|
|
#include <utils_g.h>
|
2020-06-30 00:23:36 -04:00
|
|
|
#include <widget_builder_api.h>
|
2020-03-26 02:05:46 -04:00
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
2020-01-28 16:56:43 -05:00
|
|
|
#include <gio/gdesktopappinfo.h>
|
|
|
|
|
2024-01-19 16:50:37 -05:00
|
|
|
static const char* arg_names[] = {"print_command", "display_generic", "disable_prime", "print_desktop_file"};
|
2020-02-25 19:04:40 -05:00
|
|
|
|
2020-01-20 23:09:40 -05:00
|
|
|
static struct mode* mode;
|
2019-11-16 17:52:20 -05:00
|
|
|
|
2020-06-23 03:33:57 -04:00
|
|
|
struct desktop_entry {
|
|
|
|
char* full_path;
|
2020-01-16 19:35:51 -05:00
|
|
|
struct wl_list link;
|
|
|
|
};
|
|
|
|
|
2020-06-23 03:33:57 -04:00
|
|
|
static struct map* entries;
|
|
|
|
static struct wl_list desktop_entries;
|
2020-01-16 19:35:51 -05:00
|
|
|
|
2020-02-25 19:04:40 -05:00
|
|
|
static bool print_command;
|
2020-05-15 20:20:56 -04:00
|
|
|
static bool display_generic;
|
2022-12-25 18:54:50 -05:00
|
|
|
static bool disable_prime;
|
2024-01-19 16:50:37 -05:00
|
|
|
static bool print_desktop_file;
|
2020-02-25 19:04:40 -05:00
|
|
|
|
2020-06-30 00:23:36 -04:00
|
|
|
static char* get_search_text(char* file) {
|
|
|
|
GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(file);
|
|
|
|
const char* name = g_app_info_get_display_name(G_APP_INFO(info));
|
|
|
|
const char* exec = g_app_info_get_executable(G_APP_INFO(info));
|
|
|
|
const char* description = g_app_info_get_description(G_APP_INFO(info));
|
|
|
|
const char* categories = g_desktop_app_info_get_categories(info);
|
|
|
|
const char* const* keywords = g_desktop_app_info_get_keywords(info);
|
|
|
|
const char* generic_name = g_desktop_app_info_get_generic_name(info);
|
|
|
|
|
|
|
|
char* keywords_str = strdup("");
|
|
|
|
|
|
|
|
if(keywords != NULL) {
|
|
|
|
for(size_t count = 0; keywords[count] != NULL; ++count) {
|
|
|
|
char* tmp = utils_concat(2, keywords_str, keywords[count]);
|
|
|
|
free(keywords_str);
|
|
|
|
keywords_str = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char* ret = utils_concat(7, name, file,
|
|
|
|
exec == NULL ? "" : exec,
|
|
|
|
description == NULL ? "" : description,
|
|
|
|
categories == NULL ? "" : categories,
|
|
|
|
keywords_str,
|
|
|
|
generic_name == NULL ? "" : generic_name);
|
|
|
|
free(keywords_str);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool populate_widget(char* file, char* action, struct widget_builder* builder) {
|
2019-09-13 03:25:25 -04:00
|
|
|
GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(file);
|
2023-07-05 01:51:29 -04:00
|
|
|
if(info == NULL || !g_app_info_should_show(G_APP_INFO(info)) ||
|
|
|
|
g_desktop_app_info_get_is_hidden(info)) {
|
2020-06-30 00:23:36 -04:00
|
|
|
return false;
|
2019-09-13 03:25:25 -04:00
|
|
|
}
|
2019-11-13 04:17:45 -05:00
|
|
|
const char* name;
|
2020-05-15 20:20:56 -04:00
|
|
|
char* generic_name = strdup("");
|
2019-11-13 04:17:45 -05:00
|
|
|
if(action == NULL) {
|
|
|
|
name = g_app_info_get_display_name(G_APP_INFO(info));
|
2020-05-15 20:20:56 -04:00
|
|
|
if(display_generic) {
|
|
|
|
const char* gname = g_desktop_app_info_get_generic_name(info);
|
|
|
|
if(gname != NULL) {
|
|
|
|
free(generic_name);
|
|
|
|
generic_name = utils_concat(3, " (", gname, ")");
|
|
|
|
}
|
|
|
|
}
|
2019-11-13 04:17:45 -05:00
|
|
|
} else {
|
|
|
|
name = g_desktop_app_info_get_action_name(info, action);
|
|
|
|
}
|
2019-09-13 03:25:25 -04:00
|
|
|
if(name == NULL) {
|
2020-06-30 00:23:36 -04:00
|
|
|
free(generic_name);
|
|
|
|
return false;
|
2019-09-13 03:25:25 -04:00
|
|
|
}
|
2020-06-30 00:23:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-09-13 03:25:25 -04:00
|
|
|
if(wofi_allow_images()) {
|
|
|
|
GIcon* icon = g_app_info_get_icon(G_APP_INFO(info));
|
2020-06-30 00:23:36 -04:00
|
|
|
GdkPixbuf* pixbuf;
|
2019-11-24 06:46:14 -05:00
|
|
|
if(G_IS_FILE_ICON(icon)) {
|
2019-10-27 15:14:18 -04:00
|
|
|
GFile* file = g_file_icon_get_file(G_FILE_ICON(icon));
|
|
|
|
char* path = g_file_get_path(file);
|
2020-06-30 00:23:36 -04:00
|
|
|
pixbuf = gdk_pixbuf_new_from_file(path, NULL);
|
2019-09-13 03:25:25 -04:00
|
|
|
} else {
|
2019-11-24 06:46:14 -05:00
|
|
|
GtkIconTheme* theme = gtk_icon_theme_get_default();
|
|
|
|
GtkIconInfo* info = NULL;
|
|
|
|
if(icon != NULL) {
|
|
|
|
const gchar* const* icon_names = g_themed_icon_get_names(G_THEMED_ICON(icon));
|
2020-07-18 04:44:29 -04:00
|
|
|
info = gtk_icon_theme_choose_icon_for_scale(theme, (const gchar**) icon_names, wofi_get_image_size(), wofi_get_window_scale(), 0);
|
2019-11-24 06:46:14 -05:00
|
|
|
}
|
|
|
|
if(info == NULL) {
|
2020-07-18 04:44:29 -04:00
|
|
|
info = gtk_icon_theme_lookup_icon_for_scale(theme, "application-x-executable", wofi_get_image_size(), wofi_get_window_scale(), 0);
|
2019-11-24 06:46:14 -05:00
|
|
|
}
|
2020-06-30 00:23:36 -04:00
|
|
|
pixbuf = gtk_icon_info_load_icon(info, NULL);
|
2019-09-13 03:25:25 -04:00
|
|
|
}
|
|
|
|
|
2022-06-29 22:53:29 -04:00
|
|
|
if(pixbuf == NULL) {
|
|
|
|
goto img_fail;
|
|
|
|
}
|
|
|
|
|
2020-07-18 04:44:29 -04:00
|
|
|
pixbuf = utils_g_resize_pixbuf(pixbuf, wofi_get_image_size() * wofi_get_window_scale(), GDK_INTERP_BILINEAR);
|
2020-06-30 00:23:36 -04:00
|
|
|
|
2020-07-28 19:18:44 -04:00
|
|
|
wofi_widget_builder_insert_image(builder, pixbuf, "icon", NULL);
|
2020-06-30 00:23:36 -04:00
|
|
|
g_object_unref(pixbuf);
|
2020-01-07 21:08:55 -05:00
|
|
|
}
|
2022-06-29 22:53:29 -04:00
|
|
|
|
|
|
|
img_fail:
|
2020-07-28 19:18:44 -04:00
|
|
|
wofi_widget_builder_insert_text(builder, name, "name", NULL);
|
|
|
|
wofi_widget_builder_insert_text(builder, generic_name, "generic-name", NULL);
|
2020-06-30 00:23:36 -04:00
|
|
|
free(generic_name);
|
2020-01-07 21:08:55 -05:00
|
|
|
|
2020-06-30 00:23:36 -04:00
|
|
|
if(action == NULL) {
|
|
|
|
wofi_widget_builder_set_action(builder, file);
|
|
|
|
} else {
|
|
|
|
char* action_txt = utils_concat(3, file, " ", action);
|
|
|
|
wofi_widget_builder_set_action(builder, action_txt);
|
|
|
|
free(action_txt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char* search_txt = get_search_text(file);
|
|
|
|
wofi_widget_builder_set_search_text(builder, search_txt);
|
|
|
|
free(search_txt);
|
|
|
|
|
|
|
|
return true;
|
2019-10-28 17:17:06 -04:00
|
|
|
}
|
|
|
|
|
2019-11-13 04:17:45 -05:00
|
|
|
static const gchar* const* get_actions(char* file, size_t* action_count) {
|
|
|
|
*action_count = 0;
|
|
|
|
GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(file);
|
|
|
|
if(info == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
const gchar* const* actions = g_desktop_app_info_list_actions(info);
|
|
|
|
if(actions[0] == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(; actions[*action_count] != NULL; ++*action_count);
|
|
|
|
|
|
|
|
return actions;
|
|
|
|
}
|
|
|
|
|
2020-06-30 00:23:36 -04:00
|
|
|
static struct widget_builder* populate_actions(char* file, size_t* text_count) {
|
2019-11-13 04:17:45 -05:00
|
|
|
const gchar* const* action_names = get_actions(file, text_count);
|
|
|
|
|
|
|
|
++*text_count;
|
|
|
|
|
|
|
|
|
2020-07-11 19:39:09 -04:00
|
|
|
struct widget_builder* builder = wofi_widget_builder_init(mode, *text_count);
|
2020-06-30 00:23:36 -04:00
|
|
|
if(!populate_widget(file, NULL, builder)) {
|
2020-07-11 19:39:09 -04:00
|
|
|
wofi_widget_builder_free(builder);
|
2019-11-13 04:17:45 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-30 00:23:36 -04:00
|
|
|
for(size_t count = 1; count < *text_count; ++count) {
|
|
|
|
populate_widget(file, (gchar*) action_names[count - 1], wofi_widget_builder_get_idx(builder, count));
|
2019-11-13 04:17:45 -05:00
|
|
|
}
|
2020-06-30 00:23:36 -04:00
|
|
|
return builder;
|
2019-11-13 04:17:45 -05:00
|
|
|
}
|
|
|
|
|
2020-01-11 19:03:02 -05:00
|
|
|
static char* get_id(char* path) {
|
|
|
|
char* applications = strstr(path, "applications/");
|
2020-12-20 03:54:17 -05:00
|
|
|
if(applications == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-01-11 19:03:02 -05:00
|
|
|
char* name = strchr(applications, '/') + 1;
|
|
|
|
char* id = strdup(name);
|
|
|
|
|
|
|
|
char* slash;
|
|
|
|
while((slash = strchr(id, '/')) != NULL) {
|
|
|
|
*slash = '-';
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2020-06-23 03:33:57 -04:00
|
|
|
static struct widget* create_widget(char* full_path) {
|
|
|
|
char* id = get_id(full_path);
|
2020-12-20 03:54:17 -05:00
|
|
|
if(id == NULL) {
|
|
|
|
free(full_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-06-23 03:33:57 -04:00
|
|
|
|
|
|
|
if(map_contains(entries, id)) {
|
|
|
|
free(id);
|
|
|
|
free(full_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
map_put(entries, id, "true");
|
2020-06-30 00:23:36 -04:00
|
|
|
|
|
|
|
size_t action_count;
|
|
|
|
|
|
|
|
struct widget_builder* builder = populate_actions(full_path, &action_count);
|
|
|
|
if(builder == NULL) {
|
2020-06-23 03:33:57 -04:00
|
|
|
wofi_remove_cache(mode, full_path);
|
|
|
|
free(id);
|
|
|
|
free(full_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-30 00:23:36 -04:00
|
|
|
struct widget* ret = wofi_widget_builder_get_widget(builder);
|
2020-06-23 03:33:57 -04:00
|
|
|
|
|
|
|
free(id);
|
|
|
|
free(full_path);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void insert_dir(char* app_dir) {
|
2019-11-02 14:58:51 -04:00
|
|
|
DIR* dir = opendir(app_dir);
|
|
|
|
if(dir == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct dirent* entry;
|
|
|
|
while((entry = readdir(dir)) != NULL) {
|
|
|
|
if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
char* full_path = utils_concat(3, app_dir, "/", entry->d_name);
|
2020-01-11 19:03:02 -05:00
|
|
|
char* id = get_id(full_path);
|
2020-12-20 03:54:17 -05:00
|
|
|
if(id == NULL) {
|
|
|
|
free(full_path);
|
|
|
|
continue;
|
|
|
|
}
|
2020-01-11 19:03:02 -05:00
|
|
|
|
2019-11-02 14:58:51 -04:00
|
|
|
struct stat info;
|
|
|
|
stat(full_path, &info);
|
|
|
|
if(S_ISDIR(info.st_mode)) {
|
2020-06-23 03:33:57 -04:00
|
|
|
insert_dir(full_path);
|
2020-01-11 19:03:02 -05:00
|
|
|
free(id);
|
2019-11-02 14:58:51 -04:00
|
|
|
free(full_path);
|
|
|
|
continue;
|
|
|
|
}
|
2020-01-11 19:03:02 -05:00
|
|
|
if(map_contains(entries, id)) {
|
|
|
|
free(id);
|
2019-11-02 14:58:51 -04:00
|
|
|
free(full_path);
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-13 04:17:45 -05:00
|
|
|
|
2020-06-23 03:33:57 -04:00
|
|
|
struct desktop_entry* entry = malloc(sizeof(struct desktop_entry));
|
|
|
|
entry->full_path = full_path;
|
|
|
|
wl_list_insert(&desktop_entries, &entry->link);
|
2019-11-13 04:17:45 -05:00
|
|
|
|
2020-01-11 19:03:02 -05:00
|
|
|
free(id);
|
2019-11-02 14:58:51 -04:00
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
2020-03-24 21:54:44 -04:00
|
|
|
static char* get_data_dirs(void) {
|
2020-03-24 21:37:23 -04:00
|
|
|
char* data_dirs = getenv("XDG_DATA_DIRS");
|
|
|
|
if(data_dirs == NULL) {
|
|
|
|
data_dirs = "/usr/local/share:/usr/share";
|
|
|
|
}
|
2020-03-24 21:54:44 -04:00
|
|
|
return strdup(data_dirs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* get_data_home(void) {
|
|
|
|
char* data_home = getenv("XDG_DATA_HOME");
|
|
|
|
if(data_home == NULL) {
|
|
|
|
data_home = utils_concat(2, getenv("HOME"), "/.local/share");
|
|
|
|
} else {
|
|
|
|
data_home = strdup(data_home);
|
|
|
|
}
|
|
|
|
return data_home;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool starts_with_data_dirs(char* path) {
|
|
|
|
char* data_dirs = get_data_dirs();
|
2020-03-24 21:37:23 -04:00
|
|
|
char* save_ptr;
|
2020-03-24 21:54:44 -04:00
|
|
|
char* str = strtok_r(data_dirs, ":", &save_ptr);
|
2020-03-24 21:37:23 -04:00
|
|
|
do {
|
|
|
|
char* tmpstr = utils_concat(2, str, "/applications");
|
|
|
|
char* tmp = strdup(path);
|
|
|
|
char* dir = dirname(tmp);
|
|
|
|
if(strcmp(dir, tmpstr) == 0) {
|
|
|
|
free(tmp);
|
2020-03-24 21:54:44 -04:00
|
|
|
free(data_dirs);
|
2020-03-24 21:37:23 -04:00
|
|
|
free(tmpstr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
free(tmp);
|
|
|
|
free(tmpstr);
|
|
|
|
} while((str = strtok_r(NULL, ":", &save_ptr)) != NULL);
|
|
|
|
|
2020-03-24 21:54:44 -04:00
|
|
|
free(data_dirs);
|
2020-03-24 21:37:23 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool should_invalidate_cache(char* path) {
|
|
|
|
if(starts_with_data_dirs(path)) {
|
2020-03-24 21:54:44 -04:00
|
|
|
char* data_home = get_data_home();
|
2020-03-24 21:37:23 -04:00
|
|
|
char* tmp = strdup(path);
|
|
|
|
char* file = basename(tmp);
|
|
|
|
char* full_path = utils_concat(3, data_home, "/applications/", file);
|
|
|
|
free(data_home);
|
|
|
|
if(access(full_path, F_OK) == 0) {
|
|
|
|
free(full_path);
|
|
|
|
free(tmp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
free(full_path);
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-20 23:09:40 -05:00
|
|
|
void wofi_drun_init(struct mode* this, struct map* config) {
|
|
|
|
mode = this;
|
|
|
|
|
2020-02-25 19:04:40 -05:00
|
|
|
print_command = strcmp(config_get(config, "print_command", "false"), "true") == 0;
|
2020-05-15 20:20:56 -04:00
|
|
|
display_generic = strcmp(config_get(config, "display_generic", "false"), "true") == 0;
|
2022-12-25 18:54:50 -05:00
|
|
|
disable_prime = strcmp(config_get(config, "disable_prime", "false"), "true") == 0;
|
2024-01-19 16:50:37 -05:00
|
|
|
print_desktop_file = strcmp(config_get(config, "print_desktop_file", "false"), "true") == 0;
|
2020-02-25 19:04:40 -05:00
|
|
|
|
2020-06-23 03:33:57 -04:00
|
|
|
entries = map_init();
|
2020-01-20 23:09:40 -05:00
|
|
|
struct wl_list* cache = wofi_read_cache(mode);
|
2019-09-13 03:25:25 -04:00
|
|
|
|
2020-06-23 03:33:57 -04:00
|
|
|
wl_list_init(&desktop_entries);
|
2020-01-16 19:35:51 -05:00
|
|
|
|
2019-09-13 03:25:25 -04:00
|
|
|
struct cache_line* node, *tmp;
|
|
|
|
wl_list_for_each_safe(node, tmp, cache, link) {
|
2020-03-24 21:37:23 -04:00
|
|
|
if(should_invalidate_cache(node->line)) {
|
|
|
|
wofi_remove_cache(mode, node->line);
|
2020-06-23 03:33:57 -04:00
|
|
|
free(node->line);
|
2020-03-24 21:37:23 -04:00
|
|
|
goto cache_cont;
|
|
|
|
}
|
|
|
|
|
2020-06-23 03:33:57 -04:00
|
|
|
struct desktop_entry* entry = malloc(sizeof(struct desktop_entry));
|
|
|
|
entry->full_path = node->line;
|
|
|
|
wl_list_insert(&desktop_entries, &entry->link);
|
2019-09-13 03:25:25 -04:00
|
|
|
|
|
|
|
cache_cont:
|
|
|
|
wl_list_remove(&node->link);
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(cache);
|
|
|
|
|
2020-03-24 21:54:44 -04:00
|
|
|
char* data_home = get_data_home();
|
|
|
|
char* data_dirs = get_data_dirs();
|
2019-09-13 03:25:25 -04:00
|
|
|
char* dirs = utils_concat(3, data_home, ":", data_dirs);
|
|
|
|
free(data_home);
|
2020-03-24 21:54:44 -04:00
|
|
|
free(data_dirs);
|
2019-09-13 03:25:25 -04:00
|
|
|
|
2019-11-05 01:41:53 -05:00
|
|
|
char* save_ptr;
|
|
|
|
char* str = strtok_r(dirs, ":", &save_ptr);
|
|
|
|
do {
|
|
|
|
char* app_dir = utils_concat(2, str, "/applications");
|
2020-06-23 03:33:57 -04:00
|
|
|
insert_dir(app_dir);
|
2019-09-13 03:25:25 -04:00
|
|
|
free(app_dir);
|
2019-11-05 01:41:53 -05:00
|
|
|
} while((str = strtok_r(NULL, ":", &save_ptr)) != NULL);
|
|
|
|
free(dirs);
|
2019-09-13 03:25:25 -04:00
|
|
|
}
|
|
|
|
|
2020-01-31 16:29:00 -05:00
|
|
|
struct widget* wofi_drun_get_widget(void) {
|
2020-06-23 03:33:57 -04:00
|
|
|
struct desktop_entry* node, *tmp;
|
|
|
|
wl_list_for_each_reverse_safe(node, tmp, &desktop_entries, link) {
|
|
|
|
struct widget* widget = create_widget(node->full_path);
|
2020-01-16 19:35:51 -05:00
|
|
|
wl_list_remove(&node->link);
|
|
|
|
free(node);
|
2020-06-23 03:33:57 -04:00
|
|
|
if(widget == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-01-16 19:35:51 -05:00
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-11-12 20:50:42 -05:00
|
|
|
static void launch_done(GObject* obj, GAsyncResult* result, gpointer data) {
|
|
|
|
GError* err = NULL;
|
|
|
|
if(g_app_info_launch_uris_finish(G_APP_INFO(obj), result, &err)) {
|
2024-02-07 21:16:11 -05:00
|
|
|
wofi_exit(0);
|
2020-11-12 20:50:42 -05:00
|
|
|
} else if(err != NULL) {
|
|
|
|
char* cmd = data;
|
|
|
|
fprintf(stderr, "%s cannot be executed: %s\n", cmd, err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
} else {
|
|
|
|
char* cmd = data;
|
|
|
|
fprintf(stderr, "%s cannot be executed\n", cmd);
|
|
|
|
}
|
2024-02-07 21:16:11 -05:00
|
|
|
wofi_exit(1);
|
2020-11-12 20:50:42 -05:00
|
|
|
}
|
|
|
|
|
2020-05-21 18:18:26 -04:00
|
|
|
static void set_dri_prime(GDesktopAppInfo* info) {
|
|
|
|
bool dri_prime = g_desktop_app_info_get_boolean(info, "PrefersNonDefaultGPU");
|
2022-12-25 18:54:50 -05:00
|
|
|
if(dri_prime && !disable_prime) {
|
2020-05-21 18:18:26 -04:00
|
|
|
setenv("DRI_PRIME", "1", true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 20:50:42 -05:00
|
|
|
static bool uses_dbus(GDesktopAppInfo* info) {
|
|
|
|
return g_desktop_app_info_get_boolean(info, "DBusActivatable");
|
|
|
|
}
|
|
|
|
|
2020-12-20 03:48:09 -05:00
|
|
|
static char* get_cmd(GAppInfo* info) {
|
|
|
|
const char* cmd = g_app_info_get_commandline(info);
|
|
|
|
size_t cmd_size = strlen(cmd);
|
|
|
|
char* new_cmd = calloc(1, cmd_size + 1);
|
|
|
|
size_t new_cmd_count = 0;
|
|
|
|
for(size_t count = 0; count < cmd_size; ++count) {
|
|
|
|
if(cmd[count] == '%') {
|
|
|
|
if(cmd[++count] == '%') {
|
|
|
|
new_cmd[new_cmd_count++] = cmd[count];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
new_cmd[new_cmd_count++] = cmd[count];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(new_cmd[--new_cmd_count] == ' ') {
|
|
|
|
new_cmd[new_cmd_count] = 0;
|
|
|
|
}
|
|
|
|
return new_cmd;
|
|
|
|
}
|
|
|
|
|
2019-11-04 16:25:44 -05:00
|
|
|
void wofi_drun_exec(const gchar* cmd) {
|
2019-09-13 03:25:25 -04:00
|
|
|
GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(cmd);
|
|
|
|
if(G_IS_DESKTOP_APP_INFO(info)) {
|
2020-01-20 23:09:40 -05:00
|
|
|
wofi_write_cache(mode, cmd);
|
2020-02-25 19:04:40 -05:00
|
|
|
if(print_command) {
|
2020-12-20 03:48:09 -05:00
|
|
|
char* cmd = get_cmd(G_APP_INFO(info));
|
|
|
|
printf("%s\n", cmd);
|
|
|
|
free(cmd);
|
2024-02-07 21:16:11 -05:00
|
|
|
wofi_exit(0);
|
2024-01-19 16:50:37 -05:00
|
|
|
} else if(print_desktop_file) {
|
|
|
|
printf("%s\n", cmd);
|
2024-02-07 21:16:11 -05:00
|
|
|
wofi_exit(0);
|
2020-02-25 19:04:40 -05:00
|
|
|
} else {
|
2020-05-21 18:18:26 -04:00
|
|
|
set_dri_prime(info);
|
2020-11-12 20:50:42 -05:00
|
|
|
if(uses_dbus(info)) {
|
|
|
|
g_app_info_launch_uris_async(G_APP_INFO(info), NULL, NULL, NULL, launch_done, (gchar*) cmd);
|
|
|
|
} else {
|
|
|
|
g_app_info_launch_uris(G_APP_INFO(info), NULL, NULL, NULL);
|
2024-02-07 21:16:11 -05:00
|
|
|
wofi_exit(0);
|
2020-11-12 20:50:42 -05:00
|
|
|
}
|
2020-02-25 19:04:40 -05:00
|
|
|
}
|
2019-11-13 04:17:45 -05:00
|
|
|
} else if(strrchr(cmd, ' ') != NULL) {
|
|
|
|
char* space = strrchr(cmd, ' ');
|
|
|
|
*space = 0;
|
2020-01-20 23:09:40 -05:00
|
|
|
wofi_write_cache(mode, cmd);
|
2019-11-13 04:17:45 -05:00
|
|
|
info = g_desktop_app_info_new_from_filename(cmd);
|
|
|
|
char* action = space + 1;
|
2020-02-25 19:04:40 -05:00
|
|
|
if(print_command) {
|
2020-12-20 03:48:09 -05:00
|
|
|
char* cmd = get_cmd(G_APP_INFO(info));
|
|
|
|
printf("%s\n", cmd);
|
|
|
|
free(cmd);
|
2020-02-25 21:59:16 -05:00
|
|
|
fprintf(stderr, "Printing the command line for an action is not supported\n");
|
2024-01-19 16:50:37 -05:00
|
|
|
} else if(print_desktop_file) {
|
|
|
|
printf("%s %s\n", cmd, action);
|
2024-02-07 21:16:11 -05:00
|
|
|
wofi_exit(0);
|
2020-02-25 19:04:40 -05:00
|
|
|
} else {
|
2020-05-21 18:18:26 -04:00
|
|
|
set_dri_prime(info);
|
2020-02-25 19:04:40 -05:00
|
|
|
g_desktop_app_info_launch_action(info, action, NULL);
|
|
|
|
}
|
2024-02-07 21:16:11 -05:00
|
|
|
wofi_exit(0);
|
2019-09-13 03:25:25 -04:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s cannot be executed\n", cmd);
|
2024-02-07 21:16:11 -05:00
|
|
|
wofi_exit(1);
|
2019-09-13 03:25:25 -04:00
|
|
|
}
|
|
|
|
}
|
2020-02-25 19:04:40 -05:00
|
|
|
|
|
|
|
const char** wofi_drun_get_arg_names(void) {
|
|
|
|
return arg_names;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t wofi_drun_get_arg_count(void) {
|
|
|
|
return sizeof(arg_names) / sizeof(char*);
|
|
|
|
}
|