From 7e81e58e7d1f540e448f3827751f75bf54b1fe9f Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sat, 1 Sep 2018 11:45:48 +1000 Subject: Allow reload command to exist anywhere in the command string This fixes a crash if you have commands where reload appears in the middle or at the end, such as `bindsym r mode default, reload`. --- include/stringop.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include') diff --git a/include/stringop.h b/include/stringop.h index e7f58011..01bbdaa9 100644 --- a/include/stringop.h +++ b/include/stringop.h @@ -46,4 +46,6 @@ char *cmdsep(char **stringp, const char *delim); // Split string into 2 by delim, handle quotes char *argsep(char **stringp, const char *delim); +const char *strcasestr(const char *haystack, const char *needle); + #endif -- cgit v1.2.3-70-g09d2 From f4ec3083766280a5197cf40680bf991cc9afa41b Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sat, 1 Sep 2018 23:04:49 +1000 Subject: Implement window_type criteria token --- include/sway/criteria.h | 2 +- include/sway/xwayland.h | 1 + sway/criteria.c | 45 +++++++++++++++++++++++++++++++++++++++------ sway/desktop/xwayland.c | 1 + 4 files changed, 42 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/sway/criteria.h b/include/sway/criteria.h index 7a1e547b..af12ffd7 100644 --- a/include/sway/criteria.h +++ b/include/sway/criteria.h @@ -31,7 +31,7 @@ struct criteria { uint32_t id; // X11 window ID #endif pcre *window_role; - uint32_t window_type; + enum atom_name window_type; bool floating; bool tiling; char urgent; // 'l' for latest or 'o' for oldest diff --git a/include/sway/xwayland.h b/include/sway/xwayland.h index 78d1053b..121edad3 100644 --- a/include/sway/xwayland.h +++ b/include/sway/xwayland.h @@ -5,6 +5,7 @@ #include enum atom_name { + NET_WM_WINDOW_TYPE_NORMAL, NET_WM_WINDOW_TYPE_DIALOG, NET_WM_WINDOW_TYPE_UTILITY, NET_WM_WINDOW_TYPE_TOOLBAR, diff --git a/sway/criteria.c b/sway/criteria.c index 5452c4ee..13176fa1 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "sway/criteria.h" #include "sway/tree/container.h" @@ -25,7 +26,7 @@ bool criteria_is_empty(struct criteria *criteria) { && !criteria->id #endif && !criteria->window_role - && !criteria->window_type + && criteria->window_type == ATOM_LAST && !criteria->floating && !criteria->tiling && !criteria->urgent @@ -50,6 +51,23 @@ static int regex_cmp(const char *item, const pcre *regex) { return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0); } +static bool view_has_window_type(struct sway_view *view, enum atom_name name) { +#ifdef HAVE_XWAYLAND + if (view->type != SWAY_VIEW_XWAYLAND) { + return false; + } + struct wlr_xwayland_surface *surface = view->wlr_xwayland_surface; + struct sway_xwayland *xwayland = &server.xwayland; + xcb_atom_t desired_atom = xwayland->atoms[name]; + for (size_t i = 0; i < surface->window_type_len; ++i) { + if (surface->window_type[i] == desired_atom) { + return true; + } + } +#endif + return false; +} + static int cmp_urgent(const void *_a, const void *_b) { struct sway_view *a = *(void **)_a; struct sway_view *b = *(void **)_b; @@ -144,9 +162,8 @@ static bool criteria_matches_view(struct criteria *criteria, // TODO } - if (criteria->window_type) { - uint32_t type = view_get_window_type(view); - if (!type || type != criteria->window_type) { + if (criteria->window_type != ATOM_LAST) { + if (!view_has_window_type(view, criteria->window_type)) { return false; } } @@ -254,6 +271,21 @@ static bool generate_regex(pcre **regex, char *value) { return true; } +static enum atom_name parse_window_type(const char *type) { + if (strcasecmp(type, "normal") == 0) { + return NET_WM_WINDOW_TYPE_NORMAL; + } else if (strcasecmp(type, "dialog") == 0) { + return NET_WM_WINDOW_TYPE_DIALOG; + } else if (strcasecmp(type, "utility") == 0) { + return NET_WM_WINDOW_TYPE_UTILITY; + } else if (strcasecmp(type, "toolbar") == 0) { + return NET_WM_WINDOW_TYPE_TOOLBAR; + } else if (strcasecmp(type, "splash") == 0) { + return NET_WM_WINDOW_TYPE_SPLASH; + } + return ATOM_LAST; // ie. invalid +} + enum criteria_token { T_APP_ID, T_CLASS, @@ -434,7 +466,7 @@ static bool parse_token(struct criteria *criteria, char *name, char *value) { generate_regex(&criteria->window_role, effective_value); break; case T_WINDOW_TYPE: - // TODO: This is a string but will be stored as an enum or integer + criteria->window_type = parse_window_type(effective_value); break; #ifdef HAVE_XWAYLAND case T_ID: @@ -526,7 +558,8 @@ struct criteria *criteria_parse(char *raw, char **error_arg) { } ++head; - struct criteria *criteria = calloc(sizeof(struct criteria), 1); + struct criteria *criteria = calloc(1, sizeof(struct criteria)); + criteria->window_type = ATOM_LAST; // default value char *name = NULL, *value = NULL; bool in_quotes = false; diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index 4e401008..2adc28c5 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -17,6 +17,7 @@ #include "sway/tree/view.h" static const char *atom_map[ATOM_LAST] = { + "_NET_WM_WINDOW_TYPE_NORMAL", "_NET_WM_WINDOW_TYPE_DIALOG", "_NET_WM_WINDOW_TYPE_UTILITY", "_NET_WM_WINDOW_TYPE_TOOLBAR", -- cgit v1.2.3-70-g09d2 From f057a0195ee79dfcaeddbcab026c06e310998c75 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sun, 2 Sep 2018 15:03:58 +1000 Subject: Implement focus_on_window_activation Depends on https://github.com/swaywm/wlroots/pull/1223 --- include/sway/commands.h | 1 + include/sway/config.h | 11 +++++++++++ include/sway/tree/view.h | 6 ++++++ sway/commands.c | 1 + sway/commands/focus_on_window_activation.c | 25 +++++++++++++++++++++++++ sway/desktop/xwayland.c | 18 ++++++++++++++++++ sway/meson.build | 1 + sway/tree/view.c | 23 +++++++++++++++++++++++ 8 files changed, 86 insertions(+) create mode 100644 sway/commands/focus_on_window_activation.c (limited to 'include') diff --git a/include/sway/commands.h b/include/sway/commands.h index 8e91c158..b0b5ed0f 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -117,6 +117,7 @@ sway_cmd cmd_floating_modifier; sway_cmd cmd_floating_scroll; sway_cmd cmd_focus; sway_cmd cmd_focus_follows_mouse; +sway_cmd cmd_focus_on_window_activation; sway_cmd cmd_focus_wrapping; sway_cmd cmd_font; sway_cmd cmd_for_window; diff --git a/include/sway/config.h b/include/sway/config.h index 18d10faa..45fa73c4 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -57,6 +57,16 @@ struct sway_mouse_binding { char *command; }; +/** + * Focus on window activation. + */ +enum fowa { + FOWA_SMART, + FOWA_URGENT, + FOWA_FOCUS, + FOWA_NONE, +}; + /** * A "mode" of keybindings created via the `mode` command. */ @@ -340,6 +350,7 @@ struct sway_config { size_t font_height; bool pango_markup; size_t urgent_timeout; + enum fowa focus_on_window_activation; // Flags bool focus_follows_mouse; diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h index f73ce571..382ab6b9 100644 --- a/include/sway/tree/view.h +++ b/include/sway/tree/view.h @@ -167,6 +167,7 @@ struct sway_xwayland_view { struct wl_listener request_maximize; struct wl_listener request_configure; struct wl_listener request_fullscreen; + struct wl_listener request_activate; struct wl_listener set_title; struct wl_listener set_class; struct wl_listener set_window_type; @@ -259,6 +260,11 @@ void view_autoconfigure(struct sway_view *view); void view_set_activated(struct sway_view *view, bool activated); +/** + * Called when the view requests to be focused. + */ +void view_request_activate(struct sway_view *view); + void view_set_tiled(struct sway_view *view, bool tiled); void view_close(struct sway_view *view); diff --git a/sway/commands.c b/sway/commands.c index 13f5983e..359856cc 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -106,6 +106,7 @@ static struct cmd_handler handlers[] = { { "floating_modifier", cmd_floating_modifier }, { "focus", cmd_focus }, { "focus_follows_mouse", cmd_focus_follows_mouse }, + { "focus_on_window_activation", cmd_focus_on_window_activation }, { "focus_wrapping", cmd_focus_wrapping }, { "font", cmd_font }, { "for_window", cmd_for_window }, diff --git a/sway/commands/focus_on_window_activation.c b/sway/commands/focus_on_window_activation.c new file mode 100644 index 00000000..1fb07918 --- /dev/null +++ b/sway/commands/focus_on_window_activation.c @@ -0,0 +1,25 @@ +#include "sway/commands.h" + +struct cmd_results *cmd_focus_on_window_activation(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "focus_on_window_activation", + EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (strcmp(argv[0], "smart") == 0) { + config->focus_on_window_activation = FOWA_SMART; + } else if (strcmp(argv[0], "urgent") == 0) { + config->focus_on_window_activation = FOWA_URGENT; + } else if (strcmp(argv[0], "focus") == 0) { + config->focus_on_window_activation = FOWA_FOCUS; + } else if (strcmp(argv[0], "none") == 0) { + config->focus_on_window_activation = FOWA_NONE; + } else { + return cmd_results_new(CMD_INVALID, "focus_on_window_activation", + "Expected " + "'focus_on_window_activation smart|urgent|focus|none'"); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index 68d70b64..10faf91d 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -337,6 +337,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_list_remove(&xwayland_view->request_fullscreen.link); wl_list_remove(&xwayland_view->request_move.link); wl_list_remove(&xwayland_view->request_resize.link); + wl_list_remove(&xwayland_view->request_activate.link); wl_list_remove(&xwayland_view->set_title.link); wl_list_remove(&xwayland_view->set_class.link); wl_list_remove(&xwayland_view->set_window_type.link); @@ -463,6 +464,19 @@ static void handle_request_resize(struct wl_listener *listener, void *data) { seat_begin_resize_floating(seat, view->swayc, seat->last_button, e->edges); } +static void handle_request_activate(struct wl_listener *listener, void *data) { + struct sway_xwayland_view *xwayland_view = + wl_container_of(listener, xwayland_view, request_activate); + struct sway_view *view = &xwayland_view->view; + struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface; + if (!xsurface->mapped) { + return; + } + view_request_activate(view); + + transaction_commit_dirty(); +} + static void handle_set_title(struct wl_listener *listener, void *data) { struct sway_xwayland_view *xwayland_view = wl_container_of(listener, xwayland_view, set_title); @@ -555,6 +569,10 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) { &xwayland_view->request_fullscreen); xwayland_view->request_fullscreen.notify = handle_request_fullscreen; + wl_signal_add(&xsurface->events.request_activate, + &xwayland_view->request_activate); + xwayland_view->request_activate.notify = handle_request_activate; + wl_signal_add(&xsurface->events.request_move, &xwayland_view->request_move); xwayland_view->request_move.notify = handle_request_move; diff --git a/sway/meson.build b/sway/meson.build index bcb44e8b..c14e58dd 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -46,6 +46,7 @@ sway_sources = files( 'commands/floating_modifier.c', 'commands/focus.c', 'commands/focus_follows_mouse.c', + 'commands/focus_on_window_activation.c', 'commands/focus_wrapping.c', 'commands/font.c', 'commands/for_window.c', diff --git a/sway/tree/view.c b/sway/tree/view.c index 1a98c5f2..c6ed68f6 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -280,6 +280,29 @@ void view_set_activated(struct sway_view *view, bool activated) { } } +void view_request_activate(struct sway_view *view) { + if (config->focus_on_window_activation == FOWA_NONE) { + return; + } + if (config->focus_on_window_activation == FOWA_FOCUS) { + struct sway_seat *seat = input_manager_current_seat(input_manager); + seat_set_focus(seat, view->swayc); + return; + } + if (config->focus_on_window_activation == FOWA_URGENT) { + view_set_urgent(view, true); + return; + } + // FOWA_SMART + struct sway_container *ws = container_parent(view->swayc, C_WORKSPACE); + if (workspace_is_visible(ws)) { + struct sway_seat *seat = input_manager_current_seat(input_manager); + seat_set_focus(seat, view->swayc); + } else { + view_set_urgent(view, true); + } +} + void view_set_tiled(struct sway_view *view, bool tiled) { if (!tiled) { view->using_csd = true; -- cgit v1.2.3-70-g09d2 From 6fb03817c9d2bd29697a91f92d680b0c6a2c5996 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sun, 2 Sep 2018 18:25:45 +1000 Subject: Rename fowa enum and use switch in view_request_activate --- include/sway/config.h | 4 ++-- sway/tree/view.c | 34 +++++++++++++++++----------------- 2 files changed, 19 insertions(+), 19 deletions(-) (limited to 'include') diff --git a/include/sway/config.h b/include/sway/config.h index 45fa73c4..4ee8c3c2 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -60,7 +60,7 @@ struct sway_mouse_binding { /** * Focus on window activation. */ -enum fowa { +enum sway_fowa { FOWA_SMART, FOWA_URGENT, FOWA_FOCUS, @@ -350,7 +350,7 @@ struct sway_config { size_t font_height; bool pango_markup; size_t urgent_timeout; - enum fowa focus_on_window_activation; + enum sway_fowa focus_on_window_activation; // Flags bool focus_follows_mouse; diff --git a/sway/tree/view.c b/sway/tree/view.c index c6ed68f6..6bd0ef67 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -281,25 +281,25 @@ void view_set_activated(struct sway_view *view, bool activated) { } void view_request_activate(struct sway_view *view) { - if (config->focus_on_window_activation == FOWA_NONE) { - return; - } - if (config->focus_on_window_activation == FOWA_FOCUS) { - struct sway_seat *seat = input_manager_current_seat(input_manager); - seat_set_focus(seat, view->swayc); - return; - } - if (config->focus_on_window_activation == FOWA_URGENT) { - view_set_urgent(view, true); - return; - } - // FOWA_SMART struct sway_container *ws = container_parent(view->swayc, C_WORKSPACE); - if (workspace_is_visible(ws)) { - struct sway_seat *seat = input_manager_current_seat(input_manager); - seat_set_focus(seat, view->swayc); - } else { + struct sway_seat *seat = input_manager_current_seat(input_manager); + + switch (config->focus_on_window_activation) { + case FOWA_SMART: + if (workspace_is_visible(ws)) { + seat_set_focus(seat, view->swayc); + } else { + view_set_urgent(view, true); + } + break; + case FOWA_URGENT: view_set_urgent(view, true); + break; + case FOWA_FOCUS: + seat_set_focus(seat, view->swayc); + break; + case FOWA_NONE: + break; } } -- cgit v1.2.3-70-g09d2