2019-09-13 03:25:25 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 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 <wofi.h>
|
|
|
|
|
2019-11-16 17:52:20 -05:00
|
|
|
#define MODE "dmenu"
|
|
|
|
|
2019-12-16 22:15:16 -05:00
|
|
|
static const char* arg_names[] = {"parse_action"};
|
|
|
|
|
|
|
|
static bool parse_action;
|
|
|
|
|
|
|
|
void wofi_dmenu_init(struct map* config) {
|
|
|
|
parse_action = strcmp(config_get(config, "parse_action", "false"), "true") == 0;
|
|
|
|
|
2019-09-13 03:25:25 -04:00
|
|
|
struct map* cached = map_init();
|
2019-11-16 17:52:20 -05:00
|
|
|
struct wl_list* cache = wofi_read_cache(MODE);
|
2019-09-13 03:25:25 -04:00
|
|
|
|
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
|
|
|
|
2019-11-25 07:58:16 -05:00
|
|
|
char* line = NULL;
|
2019-09-13 03:25:25 -04:00
|
|
|
size_t size = 0;
|
|
|
|
while(getline(&line, &size, stdin) != -1) {
|
|
|
|
char* lf = strchr(line, '\n');
|
|
|
|
if(lf != NULL) {
|
|
|
|
*lf = 0;
|
|
|
|
}
|
2020-01-02 17:36:54 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
free(line);
|
2020-01-02 17:36:54 -05:00
|
|
|
|
|
|
|
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");
|
|
|
|
wofi_insert_widget(MODE, &node->line, node->line, &node->line, 1);
|
|
|
|
} else {
|
|
|
|
wofi_remove_cache(MODE, node->line);
|
|
|
|
}
|
|
|
|
free(node->line);
|
|
|
|
wl_list_remove(&node->link);
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(cache);
|
|
|
|
map_free(entry_map);
|
|
|
|
|
|
|
|
wl_list_for_each_reverse_safe(node, tmp, &entries, link) {
|
|
|
|
if(!map_contains(cached, node->line)) {
|
|
|
|
wofi_insert_widget(MODE, &node->line, node->line, &node->line, 1);
|
|
|
|
}
|
|
|
|
free(node->line);
|
|
|
|
wl_list_remove(&node->link);
|
|
|
|
free(node);
|
|
|
|
}
|
2019-09-13 03:25:25 -04:00
|
|
|
map_free(cached);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2019-11-16 17:52:20 -05:00
|
|
|
wofi_write_cache(MODE, cmd);
|
2019-12-16 22:15:16 -05:00
|
|
|
printf("%s\n", action);
|
|
|
|
free(action);
|
2019-09-13 03:25:25 -04:00
|
|
|
exit(0);
|
|
|
|
}
|
2019-12-16 22:15:16 -05:00
|
|
|
|
|
|
|
const char** wofi_dmenu_get_arg_names() {
|
|
|
|
return arg_names;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t wofi_dmenu_get_arg_count() {
|
|
|
|
return sizeof(arg_names) / sizeof(char*);
|
|
|
|
}
|