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 <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <wofi_api.h>
|
|
|
|
|
|
|
|
#include <pango/pango.h>
|
2019-09-13 03:25:25 -04:00
|
|
|
|
2020-02-11 13:44:50 -05:00
|
|
|
static const char* arg_names[] = {"parse_action", "separator", "print_line_num"};
|
2019-12-16 22:15:16 -05:00
|
|
|
|
|
|
|
static bool parse_action;
|
2020-01-25 17:29:52 -05:00
|
|
|
static char* separator;
|
2020-02-11 13:44:50 -05:00
|
|
|
static bool print_line_num;
|
2020-01-20 23:09:40 -05:00
|
|
|
static struct mode* mode;
|
2019-12-16 22:15:16 -05:00
|
|
|
|
2020-01-16 19:35:51 -05:00
|
|
|
struct node {
|
|
|
|
struct widget* widget;
|
|
|
|
struct wl_list link;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct wl_list widgets;
|
|
|
|
|
2020-01-20 23:09:40 -05:00
|
|
|
void wofi_dmenu_init(struct mode* this, struct map* config) {
|
|
|
|
mode = this;
|
2019-12-16 22:15:16 -05:00
|
|
|
parse_action = strcmp(config_get(config, "parse_action", "false"), "true") == 0;
|
2020-01-25 17:29:52 -05:00
|
|
|
separator = config_get(config, "separator", "\n");
|
2020-02-11 13:44:50 -05:00
|
|
|
print_line_num = strcmp(config_get(config, "print_line_num", "false"), "true") == 0;
|
2019-12-16 22:15:16 -05:00
|
|
|
|
2020-01-26 03:22:23 -05:00
|
|
|
if(strcmp(separator, "\\n") == 0) {
|
|
|
|
separator = "\n";
|
2020-02-01 01:37:59 -05:00
|
|
|
} else if(strcmp(separator, "\\0") == 0) {
|
|
|
|
separator = "\0";
|
2020-03-08 00:12:14 -05:00
|
|
|
} else if(strcmp(separator, "\\t") == 0) {
|
|
|
|
separator = "\t";
|
2020-01-26 03:22:23 -05:00
|
|
|
}
|
|
|
|
|
2020-01-16 19:35:51 -05:00
|
|
|
wl_list_init(&widgets);
|
|
|
|
|
2019-09-13 03:25:25 -04:00
|
|
|
struct map* cached = map_init();
|
|
|
|
|
2020-01-02 17:36:54 -05:00
|
|
|
struct wl_list entries;
|
|
|
|
wl_list_init(&entries);
|
2019-09-13 03:25:25 -04:00
|
|
|
|
2020-01-02 17:36:54 -05:00
|
|
|
struct map* entry_map = map_init();
|
2019-09-13 03:25:25 -04:00
|
|
|
|
2020-03-07 22:55:30 -05:00
|
|
|
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");
|
2019-09-13 03:25:25 -04:00
|
|
|
}
|
2020-03-07 22:55:30 -05:00
|
|
|
free(line);
|
2020-02-01 01:37:59 -05:00
|
|
|
}
|
2020-01-25 17:29:52 -05:00
|
|
|
|
2020-02-11 13:44:50 -05:00
|
|
|
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);
|
2020-01-02 17:36:54 -05:00
|
|
|
}
|
2020-02-11 13:44:50 -05:00
|
|
|
free(cache);
|
2020-01-02 17:36:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
map_free(entry_map);
|
|
|
|
|
2020-02-11 13:44:50 -05:00
|
|
|
uint16_t line_num = 0;
|
|
|
|
|
|
|
|
struct cache_line* node, *tmp;
|
2020-01-02 17:36:54 -05:00
|
|
|
wl_list_for_each_reverse_safe(node, tmp, &entries, link) {
|
|
|
|
if(!map_contains(cached, node->line)) {
|
2020-02-11 13:44:50 -05:00
|
|
|
|
|
|
|
char* action;
|
|
|
|
if(print_line_num) {
|
|
|
|
action = malloc(6);
|
|
|
|
snprintf(action, 6, "%u", line_num++);
|
|
|
|
} else {
|
|
|
|
action = strdup(node->line);
|
|
|
|
}
|
|
|
|
|
2020-01-16 19:35:51 -05:00
|
|
|
struct node* widget = malloc(sizeof(struct node));
|
2020-02-11 13:44:50 -05:00
|
|
|
widget->widget = wofi_create_widget(mode, &node->line, node->line, &action, 1);
|
2020-01-16 19:35:51 -05:00
|
|
|
wl_list_insert(&widgets, &widget->link);
|
2020-02-11 13:44:50 -05:00
|
|
|
free(action);
|
2020-01-02 17:36:54 -05:00
|
|
|
}
|
|
|
|
free(node->line);
|
|
|
|
wl_list_remove(&node->link);
|
|
|
|
free(node);
|
|
|
|
}
|
2019-09-13 03:25:25 -04:00
|
|
|
map_free(cached);
|
|
|
|
}
|
|
|
|
|
2020-01-31 16:29:00 -05:00
|
|
|
struct widget* wofi_dmenu_get_widget(void) {
|
2020-01-16 19:35:51 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-11-04 16:25:44 -05:00
|
|
|
void wofi_dmenu_exec(const gchar* cmd) {
|
2019-12-16 22:15:16 -05:00
|
|
|
char* action = strdup(cmd);
|
|
|
|
if(parse_action) {
|
2019-12-16 22:38:12 -05:00
|
|
|
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;
|
|
|
|
}
|
2019-12-16 22:15:16 -05:00
|
|
|
}
|
2020-02-11 13:44:50 -05:00
|
|
|
if(!print_line_num) {
|
|
|
|
wofi_write_cache(mode, cmd);
|
|
|
|
}
|
2019-12-16 22:15:16 -05:00
|
|
|
printf("%s\n", action);
|
|
|
|
free(action);
|
2024-02-07 21:16:11 -05:00
|
|
|
wofi_exit(0);
|
2019-09-13 03:25:25 -04:00
|
|
|
}
|
2019-12-16 22:15:16 -05:00
|
|
|
|
2020-01-06 03:39:47 -05:00
|
|
|
const char** wofi_dmenu_get_arg_names(void) {
|
2019-12-16 22:15:16 -05:00
|
|
|
return arg_names;
|
|
|
|
}
|
|
|
|
|
2020-01-06 03:39:47 -05:00
|
|
|
size_t wofi_dmenu_get_arg_count(void) {
|
2019-12-16 22:15:16 -05:00
|
|
|
return sizeof(arg_names) / sizeof(char*);
|
|
|
|
}
|
2020-01-23 23:26:33 -05:00
|
|
|
|
|
|
|
bool wofi_dmenu_no_entry(void) {
|
|
|
|
return true;
|
|
|
|
}
|