init commit
This commit is contained in:
172
wofi/modes/dmenu.c
Normal file
172
wofi/modes/dmenu.c
Normal file
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright (C) 2019-2024 Scoopta
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <config.h>
|
||||
#include <wofi_api.h>
|
||||
|
||||
#include <pango/pango.h>
|
||||
|
||||
static const char* arg_names[] = {"parse_action", "separator", "print_line_num"};
|
||||
|
||||
static bool parse_action;
|
||||
static char* separator;
|
||||
static bool print_line_num;
|
||||
static struct mode* mode;
|
||||
|
||||
struct node {
|
||||
struct widget* widget;
|
||||
struct wl_list link;
|
||||
};
|
||||
|
||||
static struct wl_list widgets;
|
||||
|
||||
void wofi_dmenu_init(struct mode* this, struct map* config) {
|
||||
mode = this;
|
||||
parse_action = strcmp(config_get(config, "parse_action", "false"), "true") == 0;
|
||||
separator = config_get(config, "separator", "\n");
|
||||
print_line_num = strcmp(config_get(config, "print_line_num", "false"), "true") == 0;
|
||||
|
||||
if(strcmp(separator, "\\n") == 0) {
|
||||
separator = "\n";
|
||||
} else if(strcmp(separator, "\\0") == 0) {
|
||||
separator = "\0";
|
||||
} else if(strcmp(separator, "\\t") == 0) {
|
||||
separator = "\t";
|
||||
}
|
||||
|
||||
wl_list_init(&widgets);
|
||||
|
||||
struct map* cached = map_init();
|
||||
|
||||
struct wl_list entries;
|
||||
wl_list_init(&entries);
|
||||
|
||||
struct map* entry_map = map_init();
|
||||
|
||||
if(!isatty(STDIN_FILENO)) {
|
||||
char* line = NULL;
|
||||
size_t size = 0;
|
||||
while(getdelim(&line, &size, separator[0], stdin) != -1) {
|
||||
char* delim = strchr(line, separator[0]);
|
||||
if(delim != NULL) {
|
||||
*delim = 0;
|
||||
}
|
||||
struct cache_line* node = malloc(sizeof(struct cache_line));
|
||||
node->line = strdup(line);
|
||||
wl_list_insert(&entries, &node->link);
|
||||
map_put(entry_map, line, "true");
|
||||
}
|
||||
free(line);
|
||||
}
|
||||
|
||||
if(!print_line_num) {
|
||||
struct wl_list* cache = wofi_read_cache(mode);
|
||||
|
||||
struct cache_line* node, *tmp;
|
||||
wl_list_for_each_safe(node, tmp, cache, link) {
|
||||
if(map_contains(entry_map, node->line)) {
|
||||
map_put(cached, node->line, "true");
|
||||
struct node* widget = malloc(sizeof(struct node));
|
||||
widget->widget = wofi_create_widget(mode, &node->line, node->line, &node->line, 1);
|
||||
wl_list_insert(&widgets, &widget->link);
|
||||
} else {
|
||||
wofi_remove_cache(mode, node->line);
|
||||
}
|
||||
free(node->line);
|
||||
wl_list_remove(&node->link);
|
||||
free(node);
|
||||
}
|
||||
free(cache);
|
||||
}
|
||||
|
||||
map_free(entry_map);
|
||||
|
||||
uint16_t line_num = 0;
|
||||
|
||||
struct cache_line* node, *tmp;
|
||||
wl_list_for_each_reverse_safe(node, tmp, &entries, link) {
|
||||
if(!map_contains(cached, node->line)) {
|
||||
|
||||
char* action;
|
||||
if(print_line_num) {
|
||||
action = malloc(6);
|
||||
snprintf(action, 6, "%u", line_num++);
|
||||
} else {
|
||||
action = strdup(node->line);
|
||||
}
|
||||
|
||||
struct node* widget = malloc(sizeof(struct node));
|
||||
widget->widget = wofi_create_widget(mode, &node->line, node->line, &action, 1);
|
||||
wl_list_insert(&widgets, &widget->link);
|
||||
free(action);
|
||||
}
|
||||
free(node->line);
|
||||
wl_list_remove(&node->link);
|
||||
free(node);
|
||||
}
|
||||
map_free(cached);
|
||||
}
|
||||
|
||||
struct widget* wofi_dmenu_get_widget(void) {
|
||||
struct node* node, *tmp;
|
||||
wl_list_for_each_reverse_safe(node, tmp, &widgets, link) {
|
||||
struct widget* widget = node->widget;
|
||||
wl_list_remove(&node->link);
|
||||
free(node);
|
||||
return widget;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void wofi_dmenu_exec(const gchar* cmd) {
|
||||
char* action = strdup(cmd);
|
||||
if(parse_action) {
|
||||
if(wofi_allow_images()) {
|
||||
free(action);
|
||||
action = wofi_parse_image_escapes(cmd);
|
||||
}
|
||||
if(wofi_allow_markup()) {
|
||||
char* out;
|
||||
pango_parse_markup(action, -1, 0, NULL, &out, NULL, NULL);
|
||||
free(action);
|
||||
action = out;
|
||||
}
|
||||
}
|
||||
if(!print_line_num) {
|
||||
wofi_write_cache(mode, cmd);
|
||||
}
|
||||
printf("%s\n", action);
|
||||
free(action);
|
||||
wofi_exit(0);
|
||||
}
|
||||
|
||||
const char** wofi_dmenu_get_arg_names(void) {
|
||||
return arg_names;
|
||||
}
|
||||
|
||||
size_t wofi_dmenu_get_arg_count(void) {
|
||||
return sizeof(arg_names) / sizeof(char*);
|
||||
}
|
||||
|
||||
bool wofi_dmenu_no_entry(void) {
|
||||
return true;
|
||||
}
|
496
wofi/modes/drun.c
Normal file
496
wofi/modes/drun.c
Normal file
@@ -0,0 +1,496 @@
|
||||
/*
|
||||
* Copyright (C) 2019-2024 Scoopta
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <libgen.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <map.h>
|
||||
#include <utils.h>
|
||||
#include <config.h>
|
||||
#include <utils_g.h>
|
||||
#include <widget_builder_api.h>
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
#include <gio/gdesktopappinfo.h>
|
||||
|
||||
static const char* arg_names[] = {"print_command", "display_generic", "disable_prime", "print_desktop_file"};
|
||||
|
||||
static struct mode* mode;
|
||||
|
||||
struct desktop_entry {
|
||||
char* full_path;
|
||||
struct wl_list link;
|
||||
};
|
||||
|
||||
static struct map* entries;
|
||||
static struct wl_list desktop_entries;
|
||||
|
||||
static bool print_command;
|
||||
static bool display_generic;
|
||||
static bool disable_prime;
|
||||
static bool print_desktop_file;
|
||||
|
||||
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) {
|
||||
GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(file);
|
||||
if(info == NULL || !g_app_info_should_show(G_APP_INFO(info)) ||
|
||||
g_desktop_app_info_get_is_hidden(info)) {
|
||||
return false;
|
||||
}
|
||||
const char* name;
|
||||
char* generic_name = strdup("");
|
||||
if(action == NULL) {
|
||||
name = g_app_info_get_display_name(G_APP_INFO(info));
|
||||
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, ")");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
name = g_desktop_app_info_get_action_name(info, action);
|
||||
}
|
||||
if(name == NULL) {
|
||||
free(generic_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(wofi_allow_images()) {
|
||||
GIcon* icon = g_app_info_get_icon(G_APP_INFO(info));
|
||||
GdkPixbuf* pixbuf;
|
||||
if(G_IS_FILE_ICON(icon)) {
|
||||
GFile* file = g_file_icon_get_file(G_FILE_ICON(icon));
|
||||
char* path = g_file_get_path(file);
|
||||
pixbuf = gdk_pixbuf_new_from_file(path, NULL);
|
||||
} else {
|
||||
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));
|
||||
info = gtk_icon_theme_choose_icon_for_scale(theme, (const gchar**) icon_names, wofi_get_image_size(), wofi_get_window_scale(), 0);
|
||||
}
|
||||
if(info == NULL) {
|
||||
info = gtk_icon_theme_lookup_icon_for_scale(theme, "application-x-executable", wofi_get_image_size(), wofi_get_window_scale(), 0);
|
||||
}
|
||||
pixbuf = gtk_icon_info_load_icon(info, NULL);
|
||||
}
|
||||
|
||||
if(pixbuf == NULL) {
|
||||
goto img_fail;
|
||||
}
|
||||
|
||||
pixbuf = utils_g_resize_pixbuf(pixbuf, wofi_get_image_size() * wofi_get_window_scale(), GDK_INTERP_BILINEAR);
|
||||
|
||||
wofi_widget_builder_insert_image(builder, pixbuf, "icon", NULL);
|
||||
g_object_unref(pixbuf);
|
||||
}
|
||||
|
||||
img_fail:
|
||||
wofi_widget_builder_insert_text(builder, name, "name", NULL);
|
||||
wofi_widget_builder_insert_text(builder, generic_name, "generic-name", NULL);
|
||||
free(generic_name);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static struct widget_builder* populate_actions(char* file, size_t* text_count) {
|
||||
const gchar* const* action_names = get_actions(file, text_count);
|
||||
|
||||
++*text_count;
|
||||
|
||||
|
||||
struct widget_builder* builder = wofi_widget_builder_init(mode, *text_count);
|
||||
if(!populate_widget(file, NULL, builder)) {
|
||||
wofi_widget_builder_free(builder);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for(size_t count = 1; count < *text_count; ++count) {
|
||||
populate_widget(file, (gchar*) action_names[count - 1], wofi_widget_builder_get_idx(builder, count));
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
static char* get_id(char* path) {
|
||||
char* applications = strstr(path, "applications/");
|
||||
if(applications == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
char* name = strchr(applications, '/') + 1;
|
||||
char* id = strdup(name);
|
||||
|
||||
char* slash;
|
||||
while((slash = strchr(id, '/')) != NULL) {
|
||||
*slash = '-';
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
static struct widget* create_widget(char* full_path) {
|
||||
char* id = get_id(full_path);
|
||||
if(id == NULL) {
|
||||
free(full_path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(map_contains(entries, id)) {
|
||||
free(id);
|
||||
free(full_path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
map_put(entries, id, "true");
|
||||
|
||||
size_t action_count;
|
||||
|
||||
struct widget_builder* builder = populate_actions(full_path, &action_count);
|
||||
if(builder == NULL) {
|
||||
wofi_remove_cache(mode, full_path);
|
||||
free(id);
|
||||
free(full_path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct widget* ret = wofi_widget_builder_get_widget(builder);
|
||||
|
||||
free(id);
|
||||
free(full_path);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void insert_dir(char* app_dir) {
|
||||
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);
|
||||
char* id = get_id(full_path);
|
||||
if(id == NULL) {
|
||||
free(full_path);
|
||||
continue;
|
||||
}
|
||||
|
||||
struct stat info;
|
||||
stat(full_path, &info);
|
||||
if(S_ISDIR(info.st_mode)) {
|
||||
insert_dir(full_path);
|
||||
free(id);
|
||||
free(full_path);
|
||||
continue;
|
||||
}
|
||||
if(map_contains(entries, id)) {
|
||||
free(id);
|
||||
free(full_path);
|
||||
continue;
|
||||
}
|
||||
|
||||
struct desktop_entry* entry = malloc(sizeof(struct desktop_entry));
|
||||
entry->full_path = full_path;
|
||||
wl_list_insert(&desktop_entries, &entry->link);
|
||||
|
||||
free(id);
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
static char* get_data_dirs(void) {
|
||||
char* data_dirs = getenv("XDG_DATA_DIRS");
|
||||
if(data_dirs == NULL) {
|
||||
data_dirs = "/usr/local/share:/usr/share";
|
||||
}
|
||||
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();
|
||||
char* save_ptr;
|
||||
char* str = strtok_r(data_dirs, ":", &save_ptr);
|
||||
do {
|
||||
char* tmpstr = utils_concat(2, str, "/applications");
|
||||
char* tmp = strdup(path);
|
||||
char* dir = dirname(tmp);
|
||||
if(strcmp(dir, tmpstr) == 0) {
|
||||
free(tmp);
|
||||
free(data_dirs);
|
||||
free(tmpstr);
|
||||
return true;
|
||||
}
|
||||
free(tmp);
|
||||
free(tmpstr);
|
||||
} while((str = strtok_r(NULL, ":", &save_ptr)) != NULL);
|
||||
|
||||
free(data_dirs);
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool should_invalidate_cache(char* path) {
|
||||
if(starts_with_data_dirs(path)) {
|
||||
char* data_home = get_data_home();
|
||||
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;
|
||||
}
|
||||
|
||||
void wofi_drun_init(struct mode* this, struct map* config) {
|
||||
mode = this;
|
||||
|
||||
print_command = strcmp(config_get(config, "print_command", "false"), "true") == 0;
|
||||
display_generic = strcmp(config_get(config, "display_generic", "false"), "true") == 0;
|
||||
disable_prime = strcmp(config_get(config, "disable_prime", "false"), "true") == 0;
|
||||
print_desktop_file = strcmp(config_get(config, "print_desktop_file", "false"), "true") == 0;
|
||||
|
||||
entries = map_init();
|
||||
struct wl_list* cache = wofi_read_cache(mode);
|
||||
|
||||
wl_list_init(&desktop_entries);
|
||||
|
||||
struct cache_line* node, *tmp;
|
||||
wl_list_for_each_safe(node, tmp, cache, link) {
|
||||
if(should_invalidate_cache(node->line)) {
|
||||
wofi_remove_cache(mode, node->line);
|
||||
free(node->line);
|
||||
goto cache_cont;
|
||||
}
|
||||
|
||||
struct desktop_entry* entry = malloc(sizeof(struct desktop_entry));
|
||||
entry->full_path = node->line;
|
||||
wl_list_insert(&desktop_entries, &entry->link);
|
||||
|
||||
cache_cont:
|
||||
wl_list_remove(&node->link);
|
||||
free(node);
|
||||
}
|
||||
|
||||
free(cache);
|
||||
|
||||
char* data_home = get_data_home();
|
||||
char* data_dirs = get_data_dirs();
|
||||
char* dirs = utils_concat(3, data_home, ":", data_dirs);
|
||||
free(data_home);
|
||||
free(data_dirs);
|
||||
|
||||
char* save_ptr;
|
||||
char* str = strtok_r(dirs, ":", &save_ptr);
|
||||
do {
|
||||
char* app_dir = utils_concat(2, str, "/applications");
|
||||
insert_dir(app_dir);
|
||||
free(app_dir);
|
||||
} while((str = strtok_r(NULL, ":", &save_ptr)) != NULL);
|
||||
free(dirs);
|
||||
}
|
||||
|
||||
struct widget* wofi_drun_get_widget(void) {
|
||||
struct desktop_entry* node, *tmp;
|
||||
wl_list_for_each_reverse_safe(node, tmp, &desktop_entries, link) {
|
||||
struct widget* widget = create_widget(node->full_path);
|
||||
wl_list_remove(&node->link);
|
||||
free(node);
|
||||
if(widget == NULL) {
|
||||
continue;
|
||||
}
|
||||
return widget;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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)) {
|
||||
wofi_exit(0);
|
||||
} 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);
|
||||
}
|
||||
wofi_exit(1);
|
||||
}
|
||||
|
||||
static void set_dri_prime(GDesktopAppInfo* info) {
|
||||
bool dri_prime = g_desktop_app_info_get_boolean(info, "PrefersNonDefaultGPU");
|
||||
if(dri_prime && !disable_prime) {
|
||||
setenv("DRI_PRIME", "1", true);
|
||||
}
|
||||
}
|
||||
|
||||
static bool uses_dbus(GDesktopAppInfo* info) {
|
||||
return g_desktop_app_info_get_boolean(info, "DBusActivatable");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void wofi_drun_exec(const gchar* cmd) {
|
||||
GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(cmd);
|
||||
if(G_IS_DESKTOP_APP_INFO(info)) {
|
||||
wofi_write_cache(mode, cmd);
|
||||
if(print_command) {
|
||||
char* cmd = get_cmd(G_APP_INFO(info));
|
||||
printf("%s\n", cmd);
|
||||
free(cmd);
|
||||
wofi_exit(0);
|
||||
} else if(print_desktop_file) {
|
||||
printf("%s\n", cmd);
|
||||
wofi_exit(0);
|
||||
} else {
|
||||
set_dri_prime(info);
|
||||
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);
|
||||
wofi_exit(0);
|
||||
}
|
||||
}
|
||||
} else if(strrchr(cmd, ' ') != NULL) {
|
||||
char* space = strrchr(cmd, ' ');
|
||||
*space = 0;
|
||||
wofi_write_cache(mode, cmd);
|
||||
info = g_desktop_app_info_new_from_filename(cmd);
|
||||
char* action = space + 1;
|
||||
if(print_command) {
|
||||
char* cmd = get_cmd(G_APP_INFO(info));
|
||||
printf("%s\n", cmd);
|
||||
free(cmd);
|
||||
fprintf(stderr, "Printing the command line for an action is not supported\n");
|
||||
} else if(print_desktop_file) {
|
||||
printf("%s %s\n", cmd, action);
|
||||
wofi_exit(0);
|
||||
} else {
|
||||
set_dri_prime(info);
|
||||
g_desktop_app_info_launch_action(info, action, NULL);
|
||||
}
|
||||
wofi_exit(0);
|
||||
} else {
|
||||
fprintf(stderr, "%s cannot be executed\n", cmd);
|
||||
wofi_exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
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*);
|
||||
}
|
231
wofi/modes/run.c
Normal file
231
wofi/modes/run.c
Normal file
@@ -0,0 +1,231 @@
|
||||
/*
|
||||
* Copyright (C) 2019-2024 Scoopta
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <utils.h>
|
||||
#include <config.h>
|
||||
#include <wofi_api.h>
|
||||
|
||||
static const char* arg_names[] = {"always_parse_args", "show_all", "print_command"};
|
||||
|
||||
static bool always_parse_args;
|
||||
static bool show_all;
|
||||
static bool print_command;
|
||||
static struct mode* mode;
|
||||
static const char* arg_str = "__args";
|
||||
|
||||
struct node {
|
||||
struct widget* widget;
|
||||
struct wl_list link;
|
||||
};
|
||||
|
||||
static struct wl_list widgets;
|
||||
|
||||
void wofi_run_init(struct mode* this, struct map* config) {
|
||||
mode = this;
|
||||
always_parse_args = strcmp(config_get(config, arg_names[0], "false"), "true") == 0;
|
||||
show_all = strcmp(config_get(config, arg_names[1], "true"), "true") == 0;
|
||||
print_command = strcmp(config_get(config, arg_names[2], "false"), "true") == 0;
|
||||
|
||||
wl_list_init(&widgets);
|
||||
|
||||
struct map* cached = map_init();
|
||||
struct wl_list* cache = wofi_read_cache(mode);
|
||||
|
||||
struct map* entries = map_init();
|
||||
|
||||
struct cache_line* node, *tmp;
|
||||
wl_list_for_each_safe(node, tmp, cache, link) {
|
||||
char* text = node->line;
|
||||
if(strncmp(node->line, arg_str, strlen(arg_str)) == 0) {
|
||||
text = strchr(text, ' ') + 1;
|
||||
}
|
||||
|
||||
char* full_path = text;
|
||||
|
||||
char* final_slash = strrchr(text, '/');
|
||||
if(final_slash != NULL) {
|
||||
text = final_slash + 1;
|
||||
}
|
||||
|
||||
struct stat info;
|
||||
stat(node->line, &info);
|
||||
if(((access(node->line, X_OK) == 0 && S_ISREG(info.st_mode)) ||
|
||||
strncmp(node->line, arg_str, strlen(arg_str)) == 0) && !map_contains(cached, full_path)) {
|
||||
struct node* widget = malloc(sizeof(struct node));
|
||||
widget->widget = wofi_create_widget(mode, &text, text, &node->line, 1);
|
||||
wl_list_insert(&widgets, &widget->link);
|
||||
map_put(cached, full_path, "true");
|
||||
map_put(entries, text, "true");
|
||||
} else {
|
||||
wofi_remove_cache(mode, node->line);
|
||||
}
|
||||
free(node->line);
|
||||
wl_list_remove(&node->link);
|
||||
free(node);
|
||||
}
|
||||
|
||||
free(cache);
|
||||
|
||||
char* path = strdup(getenv("PATH"));
|
||||
|
||||
struct map* paths = map_init();
|
||||
|
||||
char* save_ptr;
|
||||
char* str = strtok_r(path, ":", &save_ptr);
|
||||
do {
|
||||
|
||||
str = realpath(str, NULL);
|
||||
if(str == NULL) {
|
||||
continue;
|
||||
}
|
||||
if(map_contains(paths, str)) {
|
||||
free(str);
|
||||
continue;
|
||||
}
|
||||
|
||||
map_put(paths, str, "true");
|
||||
|
||||
DIR* dir = opendir(str);
|
||||
if(dir == NULL) {
|
||||
continue;
|
||||
}
|
||||
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, str, "/", entry->d_name);
|
||||
struct stat info;
|
||||
stat(full_path, &info);
|
||||
if(access(full_path, X_OK) == 0 && S_ISREG(info.st_mode) &&
|
||||
!map_contains(cached, full_path) &&
|
||||
(show_all || !map_contains(entries, entry->d_name))) {
|
||||
char* text = strdup(entry->d_name);
|
||||
map_put(entries, text, "true");
|
||||
struct node* widget = malloc(sizeof(struct node));
|
||||
widget->widget = wofi_create_widget(mode, &text, text, &full_path, 1);
|
||||
wl_list_insert(&widgets, &widget->link);
|
||||
free(text);
|
||||
}
|
||||
free(full_path);
|
||||
}
|
||||
free(str);
|
||||
closedir(dir);
|
||||
} while((str = strtok_r(NULL, ":", &save_ptr)) != NULL);
|
||||
free(path);
|
||||
map_free(paths);
|
||||
map_free(cached);
|
||||
map_free(entries);
|
||||
}
|
||||
|
||||
struct widget* wofi_run_get_widget(void) {
|
||||
struct node* node, *tmp;
|
||||
wl_list_for_each_reverse_safe(node, tmp, &widgets, link) {
|
||||
struct widget* widget = node->widget;
|
||||
wl_list_remove(&node->link);
|
||||
free(node);
|
||||
return widget;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static char* parse_args(const char* cmd, size_t* space_count) {
|
||||
size_t cmd_l = strlen(cmd);
|
||||
char* ret = calloc(1, cmd_l + 1);
|
||||
|
||||
bool in_quote = false;
|
||||
size_t ret_count = 0;
|
||||
for(size_t count = 0; count < cmd_l; ++count) {
|
||||
if(cmd[count] == ' ' && !in_quote) {
|
||||
++*space_count;
|
||||
ret[ret_count++] = '\n';
|
||||
continue;
|
||||
}
|
||||
|
||||
if(cmd[count] == '"') {
|
||||
in_quote = !in_quote;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(cmd[count] == '\\') {
|
||||
++count;
|
||||
}
|
||||
ret[ret_count++] = cmd[count];
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void wofi_run_exec(const char* cmd) {
|
||||
bool arg_run = wofi_mod_control() || always_parse_args;
|
||||
if(strncmp(cmd, arg_str, strlen(arg_str)) == 0) {
|
||||
arg_run = true;
|
||||
cmd = strchr(cmd, ' ') + 1;
|
||||
}
|
||||
|
||||
if(wofi_mod_shift()) {
|
||||
wofi_write_cache(mode, cmd);
|
||||
wofi_term_run(cmd);
|
||||
}
|
||||
if(print_command) {
|
||||
printf("%s\n", cmd);
|
||||
wofi_exit(0);
|
||||
}
|
||||
if(arg_run) {
|
||||
size_t space_count = 2;
|
||||
char* tmp = parse_args(cmd, &space_count);
|
||||
|
||||
char** args = malloc(space_count * sizeof(char*));
|
||||
char* save_ptr;
|
||||
char* str = strtok_r(tmp, "\n", &save_ptr);
|
||||
size_t count = 0;
|
||||
do {
|
||||
args[count++] = str;
|
||||
} while((str = strtok_r(NULL, "\n", &save_ptr)) != NULL);
|
||||
args[space_count - 1] = NULL;
|
||||
char* cache = utils_concat(2, "__args ", cmd);
|
||||
wofi_write_cache(mode, cache);
|
||||
free(cache);
|
||||
execvp(tmp, args);
|
||||
} else {
|
||||
wofi_write_cache(mode, cmd);
|
||||
execl(cmd, cmd, NULL);
|
||||
}
|
||||
fprintf(stderr, "%s cannot be executed %s\n", cmd, strerror(errno));
|
||||
wofi_exit(errno);
|
||||
}
|
||||
|
||||
const char** wofi_run_get_arg_names(void) {
|
||||
return arg_names;
|
||||
}
|
||||
|
||||
size_t wofi_run_get_arg_count(void) {
|
||||
return sizeof(arg_names) / sizeof(char*);
|
||||
}
|
||||
|
||||
bool wofi_run_no_entry(void) {
|
||||
return true;
|
||||
}
|
Reference in New Issue
Block a user