Implemented Reconfigure action

This commit is contained in:
Keith Bowes 2022-02-18 19:25:45 -05:00
parent 740c8c3393
commit 141a941bed
3 changed files with 49 additions and 28 deletions

View file

@ -91,19 +91,27 @@ static bool parse_key_bindings(struct wb_config *config, xmlXPathContextPtr ctxt
{ {
attr = attr->next; attr = attr->next;
} }
key_bind->action = malloc(sizeof(char) * 64); char *action = (char *) attr->children->content;
strcpy(key_bind->action, (char *) attr->children->content); if (strcmp(action, "Execute") == 0)
wlr_log(WLR_INFO, "Registering action %s", (char *) key_bind->action); key_bind->action = ACTION_EXECUTE;
if (strcmp((char *) key_bind->action, "Execute") != 0) else if (strcmp(action, "NextWindow") == 0)
key_bind->action = ACTION_NEXT_WINDOW;
else if (strcmp(action, "PreviousWindow") == 0)
key_bind->action = ACTION_PREVIOUS_WINDOW;
else if (strcmp(action, "Close") == 0)
key_bind->action = ACTION_CLOSE;
else if (strcmp(action, "Exit") == 0)
key_bind->action = ACTION_EXIT;
else if (strcmp(action, "Reconfigure") == 0)
key_bind->action = ACTION_RECONFIGURE;
if (key_bind->action != ACTION_EXECUTE)
break; break;
cur_node = cur_node->children; cur_node = cur_node->children;
} }
if (strcmp((char *) cur_node->name, "execute") == 0) if (strcmp((char *) cur_node->name, "execute") == 0)
{ {
/* Bad things can happen if the command is greater than 1024 characters */ /* Bad things can happen if the command is greater than 1024 characters */
key_bind->cmd = malloc(sizeof(char) * 1024); key_bind->cmd = (char *) xmlStrdup(cur_node->children->content);
strncpy(key_bind->cmd, (char *) cur_node->children->content, 1023);
key_bind->cmd[1023] = '\0';
if (key_bind->action) if (key_bind->action)
break; break;
} }
@ -175,7 +183,6 @@ void deinit_config(struct wb_config *config) {
/* Free everything allocated in init_config */ /* Free everything allocated in init_config */
struct wb_key_binding *key_binding; struct wb_key_binding *key_binding;
wl_list_for_each(key_binding, &config->key_bindings, link) { wl_list_for_each(key_binding, &config->key_bindings, link) {
free(key_binding->action);
free(key_binding->cmd); free(key_binding->cmd);
free(key_binding); free(key_binding);
} }

View file

@ -3,6 +3,16 @@
#include "waybox/server.h" #include "waybox/server.h"
enum action_type {
ACTION_FIRST,
ACTION_CLOSE,
ACTION_EXECUTE,
ACTION_EXIT,
ACTION_NEXT_WINDOW,
ACTION_PREVIOUS_WINDOW,
ACTION_RECONFIGURE
};
struct wb_config { struct wb_config {
struct wb_server *server; struct wb_server *server;
struct { struct {
@ -20,7 +30,7 @@ struct wb_config {
struct wb_key_binding { struct wb_key_binding {
xkb_keysym_t sym; xkb_keysym_t sym;
uint32_t modifiers; uint32_t modifiers;
char *action; enum action_type action;
char *cmd; char *cmd;
struct wl_list link; struct wl_list link;
}; };

View file

@ -62,25 +62,29 @@ static bool handle_keybinding(struct wb_server *server, xkb_keysym_t sym, uint32
wl_list_for_each(key_binding, &server->config->key_bindings, link) { wl_list_for_each(key_binding, &server->config->key_bindings, link) {
if (sym == key_binding->sym && modifiers == key_binding->modifiers) if (sym == key_binding->sym && modifiers == key_binding->modifiers)
{ {
if ((strcmp("NextWindow", key_binding->action) == 0)) { switch (key_binding->action)
return cycle_views(server); {
} case ACTION_NEXT_WINDOW:
else if ((strcmp("PreviousWindow", key_binding->action) == 0)) { return cycle_views(server);
return cycle_views_reverse(server); case ACTION_PREVIOUS_WINDOW:
} return cycle_views_reverse(server);
else if ((strcmp("Close", key_binding->action) == 0)) { case ACTION_CLOSE:
struct wb_view *current_view = wl_container_of( {
server->views.next, current_view, link); struct wb_view *current_view = wl_container_of(
wlr_xdg_toplevel_send_close(current_view->xdg_toplevel); server->views.next, current_view, link);
return true; wlr_xdg_toplevel_send_close(current_view->xdg_toplevel);
} return true;
else if ((strcmp("Execute", key_binding->action) == 0)) { }
if (fork() == 0) { case ACTION_EXECUTE:
execl("/bin/sh", "/bin/sh", "-c", key_binding->cmd, (char *) NULL); if (fork() == 0) {
} execl("/bin/sh", "/bin/sh", "-c", key_binding->cmd, (char *) NULL);
return true; }
} return true;
else if ((strcmp("Exit", key_binding->action) == 0)) { case ACTION_RECONFIGURE:
deinit_config(server->config);
init_config(server);
return true;
case ACTION_EXIT:
wl_display_terminate(server->wl_display); wl_display_terminate(server->wl_display);
return true; return true;
} }