diff options
-rw-r--r-- | include/sway/config.h | 16 | ||||
-rw-r--r-- | include/swaybar/config.h | 8 | ||||
-rw-r--r-- | include/swaybar/ipc.h | 1 | ||||
-rw-r--r-- | sway/commands/bar/bindsym.c | 60 | ||||
-rw-r--r-- | sway/commands/mouse_warping.c | 8 | ||||
-rw-r--r-- | sway/config.c | 2 | ||||
-rw-r--r-- | sway/config/bar.c | 13 | ||||
-rw-r--r-- | sway/input/cursor.c | 1 | ||||
-rw-r--r-- | sway/input/seat.c | 21 | ||||
-rw-r--r-- | sway/ipc-json.c | 16 | ||||
-rw-r--r-- | sway/sway-bar.5.scd | 5 | ||||
-rw-r--r-- | sway/sway.5.scd | 7 | ||||
-rw-r--r-- | sway/tree/container.c | 12 | ||||
-rw-r--r-- | sway/tree/view.c | 4 | ||||
-rw-r--r-- | swaybar/bar.c | 31 | ||||
-rw-r--r-- | swaybar/config.c | 16 | ||||
-rw-r--r-- | swaybar/ipc.c | 26 | ||||
-rw-r--r-- | swaybar/main.c | 2 | ||||
-rw-r--r-- | swaymsg/main.c | 2 |
19 files changed, 226 insertions, 25 deletions
diff --git a/include/sway/config.h b/include/sway/config.h index 00b5f25b..bc02c0fd 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -239,6 +239,12 @@ struct bar_config { } colors; }; +struct bar_binding { + uint32_t button; + bool release; + char *command; +}; + struct border_colors { float border[4]; float background[4]; @@ -325,6 +331,12 @@ enum focus_wrapping_mode { WRAP_FORCE }; +enum mouse_warping_mode { + WARP_NO, + WARP_OUTPUT, + WARP_CONTAINER +}; + /** * The configuration struct. The result of loading a config file. */ @@ -366,7 +378,7 @@ struct sway_config { // Flags bool focus_follows_mouse; bool raise_floating; - bool mouse_warping; + enum mouse_warping_mode mouse_warping; enum focus_wrapping_mode focus_wrapping; bool active; bool failed; @@ -527,6 +539,8 @@ struct bar_config *default_bar_config(void); void free_bar_config(struct bar_config *bar); +void free_bar_binding(struct bar_binding *binding); + void free_workspace_config(struct workspace_config *wsc); /** diff --git a/include/swaybar/config.h b/include/swaybar/config.h index 5f5688cf..d0336c27 100644 --- a/include/swaybar/config.h +++ b/include/swaybar/config.h @@ -3,6 +3,7 @@ #include <stdbool.h> #include <stdint.h> #include <wayland-client.h> +#include "list.h" #include "util.h" struct box_colors { @@ -17,6 +18,12 @@ struct config_output { size_t index; }; +struct swaybar_binding { + uint32_t button; + char *command; + bool release; +}; + struct swaybar_config { char *status_command; bool pango_markup; @@ -29,6 +36,7 @@ struct swaybar_config { bool binding_mode_indicator; bool wrap_scroll; bool workspace_buttons; + list_t *bindings; struct wl_list outputs; // config_output::link bool all_outputs; int height; diff --git a/include/swaybar/ipc.h b/include/swaybar/ipc.h index 81e48a6b..8731dac2 100644 --- a/include/swaybar/ipc.h +++ b/include/swaybar/ipc.h @@ -7,5 +7,6 @@ bool ipc_initialize(struct swaybar *bar, const char *bar_id); bool handle_ipc_readable(struct swaybar *bar); void ipc_get_workspaces(struct swaybar *bar); void ipc_send_workspace_command(struct swaybar *bar, const char *ws); +void ipc_execute_binding(struct swaybar *bar, struct swaybar_binding *bind); #endif diff --git a/sway/commands/bar/bindsym.c b/sway/commands/bar/bindsym.c index ac09a03f..4eea3e6a 100644 --- a/sway/commands/bar/bindsym.c +++ b/sway/commands/bar/bindsym.c @@ -1,5 +1,7 @@ +#define _XOPEN_SOURCE 500 #include <stdlib.h> #include <string.h> +#include <strings.h> #include "sway/commands.h" #include "sway/config.h" #include "list.h" @@ -7,5 +9,61 @@ #include "stringop.h" struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { - return cmd_results_new(CMD_FAILURE, "bindsym", "TODO"); // TODO + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "bar bindsym", EXPECTED_MORE_THAN, 1))) { + return error; + } + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "bar bindsym", "No bar defined."); + } + + struct bar_binding *binding = calloc(1, sizeof(struct bar_binding)); + if (!binding) { + return cmd_results_new(CMD_FAILURE, "bar bindsym", + "Unable to allocate bar binding"); + } + + binding->release = false; + if (strcmp("--release", argv[0]) == 0) { + binding->release = true; + argv++; + argc--; + } + + binding->button = 0; + if (strncasecmp(argv[0], "button", strlen("button")) == 0 && + strlen(argv[0]) == strlen("button0")) { + binding->button = argv[0][strlen("button")] - '0'; + } + if (binding->button < 1 || binding->button > 9) { + free_bar_binding(binding); + return cmd_results_new(CMD_FAILURE, "bar bindsym", + "Only button<1-9> is supported"); + } + + binding->command = join_args(argv + 1, argc - 1); + + list_t *bindings = config->current_bar->bindings; + bool overwritten = false; + for (int i = 0; i < bindings->length; i++) { + struct bar_binding *other = bindings->items[i]; + if (other->button == binding->button && + other->release == binding->release) { + overwritten = true; + bindings->items[i] = binding; + free_bar_binding(other); + wlr_log(WLR_DEBUG, "[bar %s] Updated binding for button%u%s", + config->current_bar->id, binding->button, + binding->release ? " (release)" : ""); + break; + } + } + if (!overwritten) { + list_add(bindings, binding); + wlr_log(WLR_DEBUG, "[bar %s] Added binding for button%u%s", + config->current_bar->id, binding->button, + binding->release ? " (release)" : ""); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/mouse_warping.c b/sway/commands/mouse_warping.c index eef32ce7..d067bc65 100644 --- a/sway/commands/mouse_warping.c +++ b/sway/commands/mouse_warping.c @@ -6,13 +6,15 @@ struct cmd_results *cmd_mouse_warping(int argc, char **argv) { struct cmd_results *error = NULL; if ((error = checkarg(argc, "mouse_warping", EXPECTED_EQUAL_TO, 1))) { return error; + } else if (strcasecmp(argv[0], "container") == 0) { + config->mouse_warping = WARP_CONTAINER; } else if (strcasecmp(argv[0], "output") == 0) { - config->mouse_warping = true; + config->mouse_warping = WARP_OUTPUT; } else if (strcasecmp(argv[0], "none") == 0) { - config->mouse_warping = false; + config->mouse_warping = WARP_NO; } else { return cmd_results_new(CMD_FAILURE, "mouse_warping", - "Expected 'mouse_warping output|none'"); + "Expected 'mouse_warping output|container|none'"); } return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/config.c b/sway/config.c index 1926bc08..f239ba1d 100644 --- a/sway/config.c +++ b/sway/config.c @@ -223,7 +223,7 @@ static void config_defaults(struct sway_config *config) { // Flags config->focus_follows_mouse = true; config->raise_floating = true; - config->mouse_warping = true; + config->mouse_warping = WARP_OUTPUT; config->focus_wrapping = WRAP_YES; config->validating = false; config->reloading = false; diff --git a/sway/config/bar.c b/sway/config/bar.c index b8695798..c6899f57 100644 --- a/sway/config/bar.c +++ b/sway/config/bar.c @@ -28,6 +28,14 @@ static void terminate_swaybar(pid_t pid) { } } +void free_bar_binding(struct bar_binding *binding) { + if (!binding) { + return; + } + free(binding->command); + free(binding); +} + void free_bar_config(struct bar_config *bar) { if (!bar) { return; @@ -39,7 +47,10 @@ void free_bar_config(struct bar_config *bar) { free(bar->status_command); free(bar->font); free(bar->separator_symbol); - // TODO: Free mouse bindings + for (int i = 0; i < bar->bindings->length; i++) { + struct bar_binding *binding = bar->bindings->items[i]; + free_bar_binding(binding); + } list_free(bar->bindings); if (bar->outputs) { free_flat_list(bar->outputs); diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 6d57c45f..5c446299 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -877,6 +877,7 @@ void dispatch_cursor_button(struct sway_cursor *cursor, while (cont->parent) { cont = cont->parent; } + seat_set_focus_container(seat, cont); seat_begin_move_floating(seat, cont, button); return; } diff --git a/sway/input/seat.c b/sway/input/seat.c index f418785d..03ed638e 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -772,7 +772,9 @@ void seat_set_focus_warp(struct sway_seat *seat, struct sway_node *node, } if (last_focus) { - if (config->mouse_warping && warp && new_output != last_output) { + if (config->mouse_warping && warp && + (new_output != last_output || + config->mouse_warping == WARP_CONTAINER)) { double x = 0; double y = 0; if (container) { @@ -782,9 +784,11 @@ void seat_set_focus_warp(struct sway_seat *seat, struct sway_node *node, x = new_workspace->x + new_workspace->width / 2.0; y = new_workspace->y + new_workspace->height / 2.0; } + if (!wlr_output_layout_contains_point(root->output_layout, new_output->wlr_output, seat->cursor->cursor->x, - seat->cursor->cursor->y)) { + seat->cursor->cursor->y) + || config->mouse_warping == WARP_CONTAINER) { wlr_cursor_warp(seat->cursor->cursor, NULL, x, y); cursor_send_pointer_motion(seat->cursor, 0, true); } @@ -1038,7 +1042,7 @@ void seat_begin_down(struct sway_seat *seat, struct sway_container *con, seat->op_moved = false; // In case the container was not raised by gaining focus, raise on click - if (con && !config->raise_floating) { + if (!config->raise_floating) { container_raise_floating(con); } } @@ -1052,6 +1056,12 @@ void seat_begin_move_floating(struct sway_seat *seat, seat->operation = OP_MOVE_FLOATING; seat->op_container = con; seat->op_button = button; + + // In case the container was not raised by gaining focus, raise on click + if (!config->raise_floating) { + container_raise_floating(con); + } + cursor_set_image(seat->cursor, "grab", NULL); } @@ -1085,6 +1095,11 @@ void seat_begin_resize_floating(struct sway_seat *seat, seat->op_ref_con_ly = con->y; seat->op_ref_width = con->width; seat->op_ref_height = con->height; + // + // In case the container was not raised by gaining focus, raise on click + if (!config->raise_floating) { + container_raise_floating(con); + } const char *image = edge == WLR_EDGE_NONE ? "se-resize" : wlr_xcursor_get_resize_name(edge); diff --git a/sway/ipc-json.c b/sway/ipc-json.c index f02f370b..54d611f2 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -623,6 +623,22 @@ json_object *ipc_json_describe_bar_config(struct bar_config *bar) { json_object_object_add(json, "colors", colors); + if (bar->bindings->length > 0) { + json_object *bindings = json_object_new_array(); + for (int i = 0; i < bar->bindings->length; ++i) { + struct bar_binding *binding = bar->bindings->items[i]; + json_object *bind = json_object_new_object(); + json_object_object_add(bind, "input_code", + json_object_new_int(binding->button)); + json_object_object_add(bind, "command", + json_object_new_string(binding->command)); + json_object_object_add(bind, "release", + json_object_new_boolean(binding->release)); + json_object_array_add(bindings, bind); + } + json_object_object_add(json, "bindings", bindings); + } + // Add outputs if defined if (bar->outputs && bar->outputs->length > 0) { json_object *outputs = json_object_new_array(); diff --git a/sway/sway-bar.5.scd b/sway/sway-bar.5.scd index 8c7be8e7..6729c9ac 100644 --- a/sway/sway-bar.5.scd +++ b/sway/sway-bar.5.scd @@ -60,6 +60,11 @@ Sway allows configuring swaybar in the sway configuration file. *height* <height> Sets the height of the bar. Default height will match the font size. +*bindsym* [--release] button<n> <command> + Executes _command_ when mouse button _n_ has been pressed (or if _released_ + is given, when mouse button _n_ has been released). To disable the default + behavior for a button, use the command _nop_. + ## TRAY Swaybar provides a system tray where third-party applications may place icons. diff --git a/sway/sway.5.scd b/sway/sway.5.scd index 28ab15df..240e0731 100644 --- a/sway/sway.5.scd +++ b/sway/sway.5.scd @@ -492,9 +492,10 @@ The default colors are: If _--pango\_markup_ is given, then _mode_ will be interpreted as pango markup. -*mouse\_warping* output|none +*mouse\_warping* output|container|none If _output_ is specified, the mouse will be moved to new outputs as you - move focus between them. Default is _output_. + move focus between them. If _container_ is specified, the mouse will be + moved to the middle of the container on switch. Default is _output_. *no\_focus* <criteria> Prevents windows matching <criteria> from being focused automatically when @@ -598,7 +599,7 @@ match any output by using the output name "\*". *workspace* prev\_on\_output|next\_on\_output Switches to the next workspace on the current output. -*workspace* back_and_forth +*workspace* back\_and\_forth Switches to the previously focused workspace. *workspace* <name> gaps inner|outer <amount> diff --git a/sway/tree/container.c b/sway/tree/container.c index 1664514a..f36fe4b0 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -358,7 +358,6 @@ struct sway_container *container_at(struct sway_workspace *workspace, struct wlr_surface **surface, double *sx, double *sy) { struct sway_container *c; - // Focused view's popups struct sway_seat *seat = input_manager_current_seat(input_manager); struct sway_container *focus = seat_get_focused_container(seat); bool is_floating = focus && container_is_floating_or_child(focus); @@ -370,14 +369,11 @@ struct sway_container *container_at(struct sway_workspace *workspace, } *surface = NULL; } - // Cast a ray to handle floating windows - for (int i = workspace->floating->length - 1; i >= 0; --i) { - struct sway_container *cn = workspace->floating->items[i]; - if (cn->view && (c = surface_at_view(cn, lx, ly, surface, sx, sy))) { - return c; - } + // Floating + if ((c = floating_container_at(lx, ly, surface ,sx ,sy))) { + return c; } - // If focused is tiling, focused view's non-popups + // Tiling (focused) if (focus && focus->view && !is_floating) { if ((c = surface_at_view(focus, lx, ly, surface, sx, sy))) { return c; diff --git a/sway/tree/view.c b/sway/tree/view.c index 1f00452d..e613ac0b 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -243,10 +243,10 @@ void view_autoconfigure(struct sway_view *view) { // title area. We have to offset the surface y by the height of the title, // bar, and disable any top border because we'll always have the title bar. enum sway_container_layout layout = container_parent_layout(con); - if (layout == L_TABBED) { + if (layout == L_TABBED && !container_is_floating(con)) { y_offset = container_titlebar_height(); view->border_top = false; - } else if (layout == L_STACKED) { + } else if (layout == L_STACKED && !container_is_floating(con)) { list_t *siblings = container_get_siblings(con); y_offset = container_titlebar_height() * siblings->length; view->border_top = false; diff --git a/swaybar/bar.c b/swaybar/bar.c index 3990f1ca..5b7fea71 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -144,6 +144,19 @@ static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, bar->pointer.y = wl_fixed_to_int(surface_y); } +static bool check_bindings(struct swaybar *bar, uint32_t x11_button, + uint32_t state) { + bool released = state == WL_POINTER_BUTTON_STATE_RELEASED; + for (int i = 0; i < bar->config->bindings->length; i++) { + struct swaybar_binding *binding = bar->config->bindings->items[i]; + if (binding->button == x11_button && binding->release == released) { + ipc_execute_binding(bar, binding); + return true; + } + } + return false; +} + static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { struct swaybar *bar = data; @@ -152,6 +165,11 @@ static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, if (!sway_assert(output, "button with no active output")) { return; } + + if (check_bindings(bar, wl_button_to_x11_button(button), state)) { + return; + } + if (state != WL_POINTER_BUTTON_STATE_PRESSED) { return; } @@ -180,6 +198,15 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, return; } + // If there is a button press binding, execute it, skip default behavior, + // and check button release bindings + if (check_bindings(bar, wl_axis_to_x11_button(axis, value), + WL_POINTER_BUTTON_STATE_PRESSED)) { + check_bindings(bar, wl_axis_to_x11_button(axis, value), + WL_POINTER_BUTTON_STATE_RELEASED); + return; + } + struct swaybar_hotspot *hotspot; wl_list_for_each(hotspot, &output->hotspots, link) { double x = pointer->x * output->scale; @@ -247,6 +274,10 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, } ipc_send_workspace_command(bar, new->name); + + // Check button release bindings + check_bindings(bar, wl_axis_to_x11_button(axis, value), + WL_POINTER_BUTTON_STATE_RELEASED); } static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) { diff --git a/swaybar/config.c b/swaybar/config.c index 4e851cca..09d40c24 100644 --- a/swaybar/config.c +++ b/swaybar/config.c @@ -3,6 +3,8 @@ #include <string.h> #include "swaybar/config.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" +#include "stringop.h" +#include "list.h" uint32_t parse_position(const char *position) { uint32_t horiz = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | @@ -34,6 +36,7 @@ struct swaybar_config *init_config(void) { config->binding_mode_indicator = true; config->wrap_scroll = false; config->workspace_buttons = true; + config->bindings = create_list(); wl_list_init(&config->outputs); /* height */ @@ -69,11 +72,24 @@ struct swaybar_config *init_config(void) { return config; } +static void free_binding(struct swaybar_binding *binding) { + if (!binding) { + return; + } + free(binding->command); + free(binding); +} + void free_config(struct swaybar_config *config) { free(config->status_command); free(config->font); free(config->mode); free(config->sep_symbol); + for (int i = 0; i < config->bindings->length; i++) { + struct swaybar_binding *binding = config->bindings->items[i]; + free_binding(binding); + } + list_free(config->bindings); struct config_output *coutput, *tmp; wl_list_for_each_safe(coutput, tmp, &config->outputs, link) { wl_list_remove(&coutput->link); diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 7c53a44f..a67814c1 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -7,6 +7,7 @@ #include "swaybar/config.h" #include "swaybar/ipc.h" #include "ipc-client.h" +#include "list.h" void ipc_send_workspace_command(struct swaybar *bar, const char *ws) { const char *fmt = "workspace \"%s\""; @@ -154,6 +155,7 @@ static bool ipc_parse_config( json_object *markup, *mode, *hidden_bar, *position, *status_command; json_object *font, *bar_height, *wrap_scroll, *workspace_buttons, *strip_workspace_numbers; json_object *binding_mode_indicator, *verbose, *colors, *sep_symbol, *outputs; + json_object *bindings; json_object_object_get_ex(bar_config, "mode", &mode); json_object_object_get_ex(bar_config, "hidden_bar", &hidden_bar); json_object_object_get_ex(bar_config, "position", &position); @@ -169,6 +171,7 @@ static bool ipc_parse_config( json_object_object_get_ex(bar_config, "colors", &colors); json_object_object_get_ex(bar_config, "outputs", &outputs); json_object_object_get_ex(bar_config, "pango_markup", &markup); + json_object_object_get_ex(bar_config, "bindings", &bindings); if (status_command) { free(config->status_command); config->status_command = strdup(json_object_get_string(status_command)); @@ -202,6 +205,21 @@ static bool ipc_parse_config( if (markup) { config->pango_markup = json_object_get_boolean(markup); } + if (bindings) { + int length = json_object_array_length(bindings); + for (int i = 0; i < length; ++i) { + json_object *bindobj = json_object_array_get_idx(bindings, i); + struct swaybar_binding *binding = + calloc(1, sizeof(struct swaybar_binding)); + binding->button = json_object_get_int( + json_object_object_get(bindobj, "input_code")); + binding->command = strdup(json_object_get_string( + json_object_object_get(bindobj, "command"))); + binding->release = json_object_get_boolean( + json_object_object_get(bindobj, "release")); + list_add(config->bindings, binding); + } + } struct config_output *output, *tmp; wl_list_for_each_safe(output, tmp, &config->outputs, link) { @@ -319,6 +337,14 @@ static void ipc_get_outputs(struct swaybar *bar) { free(res); } +void ipc_execute_binding(struct swaybar *bar, struct swaybar_binding *bind) { + wlr_log(WLR_DEBUG, "Executing binding for button %u (release=%d): `%s`", + bind->button, bind->release, bind->command); + uint32_t len = strlen(bind->command); + free(ipc_single_command(bar->ipc_socketfd, + IPC_COMMAND, bind->command, &len)); +} + bool ipc_initialize(struct swaybar *bar, const char *bar_id) { uint32_t len = strlen(bar_id); char *res = ipc_single_command(bar->ipc_socketfd, diff --git a/swaybar/main.c b/swaybar/main.c index d2c579db..db204f4a 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -62,7 +62,7 @@ int main(int argc, char **argv) { bar_id = strdup(optarg); break; case 'v': - fprintf(stdout, "sway version " SWAY_VERSION "\n"); + fprintf(stdout, "swaybar version " SWAY_VERSION "\n"); exit(EXIT_SUCCESS); break; case 'd': // Debug diff --git a/swaymsg/main.c b/swaymsg/main.c index 3767daf3..4688737c 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -345,7 +345,7 @@ int main(int argc, char **argv) { cmdtype = strdup(optarg); break; case 'v': - fprintf(stdout, "sway version " SWAY_VERSION "\n"); + fprintf(stdout, "swaymsg version " SWAY_VERSION "\n"); exit(EXIT_SUCCESS); break; default: |