From be3d7618ff047716575bb0f1329f882c996130e1 Mon Sep 17 00:00:00 2001 From: Keith Bowes Date: Wed, 2 Feb 2022 20:19:52 -0500 Subject: [PATCH] Fixed some things I overlooked --- .build.yml | 1 + waybox/config.c | 21 ++++++------ waybox/seat.c | 86 +++++++++++++++++++++++++++++++------------------ 3 files changed, 65 insertions(+), 43 deletions(-) diff --git a/.build.yml b/.build.yml index ebb7c4e..254defc 100644 --- a/.build.yml +++ b/.build.yml @@ -7,6 +7,7 @@ packages: - xcb-util-image - libinput - libxkbcommon + - libxml2 sources: - https://github.com/wizbright/waybox - https://gitlab.freedesktop.org/wlroots/wlroots diff --git a/waybox/config.c b/waybox/config.c index eb7a017..01712ce 100644 --- a/waybox/config.c +++ b/waybox/config.c @@ -21,8 +21,7 @@ static char *parse_xpath_expr(char *expr, xmlXPathContextPtr ctxt) { return (char *) ret; } -static bool parse_key_bindings(struct wb_config *config, xmlXPathContextPtr ctxt) -{ +static bool parse_key_bindings(struct wb_config *config, xmlXPathContextPtr ctxt) { /* Get the key bindings */ wl_list_init(&config->key_bindings); xmlXPathObjectPtr object = xmlXPathEvalExpression((xmlChar *) "//ob:keyboard/ob:keybind", ctxt); @@ -92,7 +91,7 @@ static bool parse_key_bindings(struct wb_config *config, xmlXPathContextPtr ctxt { attr = attr->next; } - key_bind->action = malloc(sizeof (char) * 64); + key_bind->action = malloc(sizeof(char) * 64); strcpy(key_bind->action, (char *) attr->children->content); wlr_log(WLR_INFO, "Registering action %s", (char *) key_bind->action); if (strcmp((char *) key_bind->action, "Execute") != 0) @@ -104,7 +103,7 @@ static bool parse_key_bindings(struct wb_config *config, xmlXPathContextPtr ctxt /* Bad things can happen if the command is greater than 1024 characters */ key_bind->cmd = malloc(sizeof(char) * 1024); strncpy(key_bind->cmd, (char *) cur_node->children->content, 1023); - key_bind->cmd[strlen(key_bind->cmd)] = '\0'; + key_bind->cmd[1023] = '\0'; if (key_bind->action) break; } @@ -118,8 +117,7 @@ static bool parse_key_bindings(struct wb_config *config, xmlXPathContextPtr ctxt return true; } -bool init_config(struct wb_server *server) -{ +bool init_config(struct wb_server *server) { xmlDocPtr doc; if (server->config_file == NULL) { char *xdg_config = getenv("XDG_CONFIG_HOME"); @@ -134,7 +132,7 @@ bool init_config(struct wb_server *server) free(rc_file); } else { wlr_log(WLR_INFO, "Using config file %s", server->config_file); - doc = xmlParseFile(server->config_file); + doc = xmlReadFile(server->config_file, NULL, XML_PARSE_RECOVER); } if (doc == NULL) { @@ -157,8 +155,7 @@ bool init_config(struct wb_server *server) config->keyboard_layout.options = parse_xpath_expr("//ob:keyboard//ob:layout//ob:options", ctxt); config->keyboard_layout.rules = parse_xpath_expr("//ob:keyboard//ob:layout//ob:rules", ctxt); config->keyboard_layout.variant = parse_xpath_expr("//ob:keyboard//ob:layout//ob:variant", ctxt); - if (!parse_key_bindings(config, ctxt)) - { + if (!parse_key_bindings(config, ctxt)) { xmlFreeDoc(doc); return false; } @@ -171,8 +168,10 @@ bool init_config(struct wb_server *server) return true; } -void deinit_config(struct wb_config *config) -{ +void deinit_config(struct wb_config *config) { + if (!config) + return; + /* Free everything allocated in init_config */ struct wb_key_binding *key_binding; wl_list_for_each(key_binding, &config->key_bindings, link) { diff --git a/waybox/seat.c b/waybox/seat.c index ce3cf26..e6ab292 100644 --- a/waybox/seat.c +++ b/waybox/seat.c @@ -3,6 +3,38 @@ #include "waybox/seat.h" #include "waybox/xdg_shell.h" +static bool cycle_views(struct wb_server *server) { + /* Cycle to the next view */ + if (wl_list_length(&server->views) < 2) { + return false; + } + struct wb_view *current_view = wl_container_of( + server->views.prev, current_view, link); + struct wb_view *prev_view = wl_container_of( + server->views.next, prev_view, link); + focus_view(current_view, current_view->xdg_surface->surface); + /* Move the current view to the beginning of the list */ + wl_list_remove(¤t_view->link); + wl_list_insert(&server->views, ¤t_view->link); + return true; +} + +static bool cycle_views_reverse(struct wb_server *server) { + /* Cycle to the previous view */ + if (wl_list_length(&server->views) < 2) { + return false; + } + struct wb_view *current_view = wl_container_of( + server->views.next, current_view, link); + struct wb_view *next_view = wl_container_of( + current_view->link.next, next_view, link); + focus_view(next_view, next_view->xdg_surface->surface); + /* Move the previous view to the end of the list */ + wl_list_remove(¤t_view->link); + wl_list_insert(server->views.prev, ¤t_view->link); + return true; +} + static bool handle_keybinding(struct wb_server *server, xkb_keysym_t sym, uint32_t modifiers) { /* * Here we handle compositor keybindings. This is when the compositor is @@ -14,43 +46,33 @@ static bool handle_keybinding(struct wb_server *server, xkb_keysym_t sym, uint32 */ struct wb_key_binding *key_binding; + if (!server->config) + { + if (modifiers & WLR_MODIFIER_ALT && sym == XKB_KEY_Tab) + cycle_views(server); + else if (modifiers & (WLR_MODIFIER_ALT|WLR_MODIFIER_SHIFT) && + sym == XKB_KEY_Tab) + cycle_views_reverse(server); + else if (sym == XKB_KEY_Escape && modifiers & WLR_MODIFIER_CTRL) + wl_display_terminate(server->wl_display); + else + return false; + return true; + } wl_list_for_each(key_binding, &server->config->key_bindings, link) { if (sym == key_binding->sym && modifiers == key_binding->modifiers) { if ((strcmp("NextWindow", key_binding->action) == 0)) { - /* Cycle to the next view */ - if (wl_list_length(&server->views) < 2) { - return false; - } - struct wb_view *current_view = wl_container_of( - server->views.prev, current_view, link); - struct wb_view *prev_view = wl_container_of( - server->views.next, prev_view, link); - focus_view(prev_view, prev_view->xdg_surface->surface); - /* Move the current view to the beginning of the list */ - wl_list_remove(¤t_view->link); - wl_list_insert(&server->views, ¤t_view->link); - return true; + return cycle_views(server); } else if ((strcmp("PreviousWindow", key_binding->action) == 0)) { - /* Cycle to the previous view */ - if (wl_list_length(&server->views) < 2) { - return false; - } - struct wb_view *current_view = wl_container_of( - server->views.next, current_view, link); - struct wb_view *next_view = wl_container_of( - current_view->link.next, next_view, link); - focus_view(next_view, next_view->xdg_surface->surface); - /* Move the previous view to the end of the list */ - wl_list_remove(¤t_view->link); - wl_list_insert(server->views.prev, ¤t_view->link); - return true; + return cycle_views_reverse(server); } else if ((strcmp("Close", key_binding->action) == 0)) { struct wb_view *current_view = wl_container_of( server->views.next, current_view, link); wlr_xdg_toplevel_send_close(current_view->xdg_surface); + return true; } else if ((strcmp("Execute", key_binding->action) == 0)) { if (fork() == 0) { @@ -126,23 +148,23 @@ static void handle_new_keyboard(struct wb_server *server, /* We need to prepare an XKB keymap and assign it to the keyboard. */ struct xkb_rule_names rules = {0}; - if (server->config->keyboard_layout.layout) + if (server->config && server->config->keyboard_layout.layout) rules.layout = server->config->keyboard_layout.layout; else rules.layout = getenv("XKB_DEFAULT_LAYOUT"); - if (server->config->keyboard_layout.model) + if (server->config && server->config->keyboard_layout.model) rules.model = server->config->keyboard_layout.model; else rules.model = getenv("XKB_DEFAULT_MODEL"); - if (server->config->keyboard_layout.options) + if (server->config && server->config->keyboard_layout.options) rules.options = server->config->keyboard_layout.options; else rules.options = getenv("XKB_DEFAULT_OPTIONS"); - if (server->config->keyboard_layout.rules) + if (server->config && server->config->keyboard_layout.rules) rules.rules = server->config->keyboard_layout.rules; else - rules.variant = getenv("XKB_DEFAULT_RULES"); - if (server->config->keyboard_layout.variant) + rules.rules = getenv("XKB_DEFAULT_RULES"); + if (server->config && server->config->keyboard_layout.variant) rules.variant = server->config->keyboard_layout.variant; else rules.variant = getenv("XKB_DEFAULT_VARIANT");