Streamline menu callbacks
This commit is contained in:
parent
a0df7959f9
commit
7d717b3696
5 changed files with 42 additions and 36 deletions
42
wmenu-run.c
42
wmenu-run.c
|
|
@ -1,6 +1,6 @@
|
|||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
|
@ -27,46 +27,48 @@ static void read_items(struct menu *menu) {
|
|||
free(path);
|
||||
}
|
||||
|
||||
struct executable {
|
||||
struct command {
|
||||
struct menu *menu;
|
||||
char *name;
|
||||
char *text;
|
||||
bool exit;
|
||||
};
|
||||
|
||||
static void activation_token_done(void *data, struct xdg_activation_token_v1 *activation_token,
|
||||
const char *token) {
|
||||
struct executable *exe = data;
|
||||
struct command *cmd = data;
|
||||
xdg_activation_token_v1_destroy(activation_token);
|
||||
menu_destroy(exe->menu);
|
||||
|
||||
setenv("XDG_ACTIVATION_TOKEN", token, true);
|
||||
char* cmd[] = {"/bin/sh", "-c", exe->name, NULL};
|
||||
execvp(cmd[0], (char**)cmd);
|
||||
|
||||
fprintf(stderr, "Failed to execute selection: %s\n", strerror(errno));
|
||||
free(exe->name);
|
||||
free(exe);
|
||||
exit(EXIT_FAILURE);
|
||||
int pid = fork();
|
||||
if (pid == 0) {
|
||||
setenv("XDG_ACTIVATION_TOKEN", token, true);
|
||||
char *argv[] = {"/bin/sh", "-c", cmd->text, NULL};
|
||||
execvp(argv[0], (char**)argv);
|
||||
} else {
|
||||
if (cmd->exit) {
|
||||
cmd->menu->exit = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static const struct xdg_activation_token_v1_listener activation_token_listener = {
|
||||
.done = activation_token_done,
|
||||
};
|
||||
|
||||
static void exec(struct menu *menu) {
|
||||
struct executable *exe = calloc(1, sizeof(struct executable));
|
||||
exe->menu = menu;
|
||||
exe->name = strdup(menu->input);
|
||||
static void exec_item(struct menu *menu, char *text, bool exit) {
|
||||
struct command *cmd = calloc(1, sizeof(struct command));
|
||||
cmd->menu = menu;
|
||||
cmd->text = strdup(text);
|
||||
cmd->exit = exit;
|
||||
|
||||
struct xdg_activation_v1 *activation = context_get_xdg_activation(menu->context);
|
||||
struct xdg_activation_token_v1 *activation_token = xdg_activation_v1_get_activation_token(activation);
|
||||
xdg_activation_token_v1_set_surface(activation_token, context_get_surface(menu->context));
|
||||
xdg_activation_token_v1_add_listener(activation_token, &activation_token_listener, exe);
|
||||
xdg_activation_token_v1_add_listener(activation_token, &activation_token_listener, cmd);
|
||||
xdg_activation_token_v1_commit(activation_token);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
struct menu *menu = menu_create();
|
||||
menu->callback = exec;
|
||||
struct menu *menu = menu_create(exec_item);
|
||||
menu_getopts(menu, argc, argv);
|
||||
read_items(menu);
|
||||
int status = menu_run(menu);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue