diff options
-rw-r--r-- | include/sway/config.h | 3 | ||||
-rw-r--r-- | include/sway/input/cursor.h | 8 | ||||
-rw-r--r-- | sway/commands/bind.c | 130 | ||||
-rw-r--r-- | sway/commands/move.c | 2 | ||||
-rw-r--r-- | sway/commands/seat/cursor.c | 4 | ||||
-rw-r--r-- | sway/config.c | 4 | ||||
-rw-r--r-- | sway/input/cursor.c | 77 | ||||
-rw-r--r-- | sway/input/seat.c | 4 | ||||
-rw-r--r-- | sway/ipc-server.c | 12 | ||||
-rw-r--r-- | sway/sway.5.scd | 2 | ||||
-rw-r--r-- | sway/tree/output.c | 2 | ||||
-rw-r--r-- | swaybar/input.c | 4 | ||||
-rw-r--r-- | swaybar/render.c | 86 |
13 files changed, 212 insertions, 126 deletions
diff --git a/include/sway/config.h b/include/sway/config.h index ebf16e6a..29c21afe 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -27,7 +27,8 @@ struct sway_variable { enum binding_input_type { BINDING_KEYCODE, BINDING_KEYSYM, - BINDING_MOUSE, + BINDING_MOUSECODE, + BINDING_MOUSESYM, }; enum binding_flags { diff --git a/include/sway/input/cursor.h b/include/sway/input/cursor.h index 22e278b0..4636bf6b 100644 --- a/include/sway/input/cursor.h +++ b/include/sway/input/cursor.h @@ -86,4 +86,12 @@ void cursor_warp_to_container(struct sway_cursor *cursor, void cursor_warp_to_workspace(struct sway_cursor *cursor, struct sway_workspace *workspace); + +uint32_t get_mouse_bindsym(const char *name, char **error); + +uint32_t get_mouse_bindcode(const char *name, char **error); + +// Considers both bindsym and bindcode +uint32_t get_mouse_button(const char *name, char **error); + #endif diff --git a/sway/commands/bind.c b/sway/commands/bind.c index 7994ab2f..be47d412 100644 --- a/sway/commands/bind.c +++ b/sway/commands/bind.c @@ -1,10 +1,6 @@ #define _POSIX_C_SOURCE 200809L #include <libevdev/libevdev.h> -#ifdef __linux__ #include <linux/input-event-codes.h> -#elif __FreeBSD__ -#include <dev/evdev/input-event-codes.h> -#endif #include <xkbcommon/xkbcommon.h> #include <xkbcommon/xkbcommon-names.h> #include <string.h> @@ -89,69 +85,91 @@ static int key_qsort_cmp(const void *keyp_a, const void *keyp_b) { */ static struct cmd_results *identify_key(const char* name, bool first_key, uint32_t* key_val, enum binding_input_type* type) { - if (*type == BINDING_KEYCODE) { - // check for keycode - xkb_keycode_t keycode = strtol(name, NULL, 10); - if (!xkb_keycode_is_legal_ext(keycode)) { - return cmd_results_new(CMD_INVALID, "bindcode", - "Invalid keycode '%s'", name); + if (*type == BINDING_MOUSECODE) { + // check for mouse bindcodes + char *message = NULL; + uint32_t button = get_mouse_bindcode(name, &message); + if (!button) { + if (message) { + struct cmd_results *error = + cmd_results_new(CMD_INVALID, "bindcode", message); + free(message); + return error; + } else { + return cmd_results_new(CMD_INVALID, "bindcode", + "Unknown button code %s", name); + } } - *key_val = keycode; - } else { - // check for keysym - xkb_keysym_t keysym = xkb_keysym_from_name(name, - XKB_KEYSYM_CASE_INSENSITIVE); - - // Check for mouse binding - uint32_t button = 0; - if (strncasecmp(name, "button", strlen("button")) == 0) { - // Map to x11 mouse buttons - button = name[strlen("button")] - '0'; - if (button < 1 || button > 9 || strlen(name) > strlen("button0")) { + *key_val = button; + } else if (*type == BINDING_MOUSESYM) { + // check for mouse bindsyms (x11 buttons or event names) + char *message = NULL; + uint32_t button = get_mouse_bindsym(name, &message); + if (!button) { + if (message) { + struct cmd_results *error = + cmd_results_new(CMD_INVALID, "bindsym", message); + free(message); + return error; + } else if (!button) { return cmd_results_new(CMD_INVALID, "bindsym", - "Only buttons 1-9 are supported. For other mouse " - "buttons, use the name of the event code."); + "Unknown button %s", name); } - uint32_t buttons[] = {BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, - SWAY_SCROLL_UP, SWAY_SCROLL_DOWN, SWAY_SCROLL_LEFT, - SWAY_SCROLL_RIGHT, BTN_SIDE, BTN_EXTRA}; - button = buttons[button - 1]; - } else if (strncmp(name, "BTN_", strlen("BTN_")) == 0) { - // Get event code - int code = libevdev_event_code_from_name(EV_KEY, name); - if (code == -1) { - return cmd_results_new(CMD_INVALID, "bindsym", - "Invalid event code name %s", name); + } + *key_val = button; + } else if (*type == BINDING_KEYCODE) { + // check for keycode. If it is the first key, allow mouse bindcodes + if (first_key) { + char *message = NULL; + uint32_t button = get_mouse_bindcode(name, &message); + free(message); + if (button) { + *type = BINDING_MOUSECODE; + *key_val = button; + return NULL; } - button = code; } - if (*type == BINDING_KEYSYM) { - if (button) { - if (first_key) { - *type = BINDING_MOUSE; - *key_val = button; - } else { - return cmd_results_new(CMD_INVALID, "bindsym", - "Mixed button '%s' into key sequence", name); - } - } else if (keysym) { - *key_val = keysym; + xkb_keycode_t keycode = strtol(name, NULL, 10); + if (!xkb_keycode_is_legal_ext(keycode)) { + if (first_key) { + return cmd_results_new(CMD_INVALID, "bindcode", + "Invalid keycode or button code '%s'", name); } else { - return cmd_results_new(CMD_INVALID, "bindsym", - "Unknown key '%s'", name); + return cmd_results_new(CMD_INVALID, "bindcode", + "Invalid keycode '%s'", name); } - } else { - if (button) { + } + *key_val = keycode; + } else { + // check for keysym. If it is the first key, allow mouse bindsyms + if (first_key) { + char *message = NULL; + uint32_t button = get_mouse_bindsym(name, &message); + if (message) { + struct cmd_results *error = + cmd_results_new(CMD_INVALID, "bindsym", message); + free(message); + return error; + } else if (button) { + *type = BINDING_MOUSESYM; *key_val = button; - } else if (keysym) { + return NULL; + } + } + + xkb_keysym_t keysym = xkb_keysym_from_name(name, + XKB_KEYSYM_CASE_INSENSITIVE); + if (!keysym) { + if (first_key) { return cmd_results_new(CMD_INVALID, "bindsym", - "Mixed keysym '%s' into button sequence", name); + "Unknown key or button '%s'", name); } else { return cmd_results_new(CMD_INVALID, "bindsym", - "Unknown button '%s'", name); + "Unknown key '%s'", name); } } + *key_val = keysym; } return NULL; } @@ -205,7 +223,8 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv, } if (binding->flags & (BINDING_BORDER | BINDING_CONTENTS | BINDING_TITLEBAR) || exclude_titlebar) { - binding->type = BINDING_MOUSE; + binding->type = binding->type == BINDING_KEYCODE ? + BINDING_MOUSECODE : BINDING_MOUSESYM; } if (argc < 2) { @@ -253,7 +272,8 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv, // that this is one if (exclude_titlebar) { binding->flags &= ~BINDING_TITLEBAR; - } else if (binding->type == BINDING_MOUSE) { + } else if (binding->type == BINDING_MOUSECODE + || binding->type == BINDING_MOUSESYM) { binding->flags |= BINDING_TITLEBAR; } diff --git a/sway/commands/move.c b/sway/commands/move.c index 09f19c3f..72e177e8 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -154,6 +154,8 @@ static void container_move_to_container_from_direction( static void container_move_to_workspace_from_direction( struct sway_container *container, struct sway_workspace *workspace, enum wlr_direction move_dir) { + container->width = container->height = 0; + if (is_parallel(workspace->layout, move_dir)) { wlr_log(WLR_DEBUG, "Reparenting container (parallel)"); int index = diff --git a/sway/commands/seat/cursor.c b/sway/commands/seat/cursor.c index b4728543..1fbc68a1 100644 --- a/sway/commands/seat/cursor.c +++ b/sway/commands/seat/cursor.c @@ -1,9 +1,5 @@ #define _POSIX_C_SOURCE 200809L -#ifdef __linux__ #include <linux/input-event-codes.h> -#elif __FreeBSD__ -#include <dev/evdev/input-event-codes.h> -#endif #include <strings.h> #include <wlr/types/wlr_cursor.h> diff --git a/sway/config.c b/sway/config.c index b2742327..ea9f23dd 100644 --- a/sway/config.c +++ b/sway/config.c @@ -13,11 +13,7 @@ #include <limits.h> #include <dirent.h> #include <strings.h> -#ifdef __linux__ #include <linux/input-event-codes.h> -#elif __FreeBSD__ -#include <dev/evdev/input-event-codes.h> -#endif #include <wlr/types/wlr_output.h> #include "sway/input/input-manager.h" #include "sway/input/seat.h" diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 510030ae..9af7ef57 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -1,13 +1,11 @@ #define _POSIX_C_SOURCE 200809L #include <math.h> #include <libevdev/libevdev.h> -#ifdef __linux__ #include <linux/input-event-codes.h> -#elif __FreeBSD__ -#include <dev/evdev/input-event-codes.h> -#endif +#include <errno.h> #include <float.h> #include <limits.h> +#include <strings.h> #include <wlr/types/wlr_cursor.h> #include <wlr/types/wlr_xcursor_manager.h> #include <wlr/types/wlr_idle.h> @@ -89,6 +87,10 @@ static struct sway_node *node_at_coords( return NULL; } struct sway_output *output = wlr_output->data; + if (!output) { + // output is being destroyed + return NULL; + } double ox = lx, oy = ly; wlr_output_layout_output_coords(root->output_layout, wlr_output, &ox, &oy); @@ -1096,6 +1098,8 @@ static void dispatch_cursor_axis(struct sway_cursor *cursor, enum wlr_edges edge = cont ? find_edge(cont, cursor) : WLR_EDGE_NONE; bool on_border = edge != WLR_EDGE_NONE; bool on_titlebar = cont && !on_border && !surface; + bool on_titlebar_border = cont && on_border && + cursor->cursor->y < cont->content_y; bool on_contents = cont && !on_border && surface; float scroll_factor = (ic == NULL || ic->scroll_factor == FLT_MIN) ? 1.0f : ic->scroll_factor; @@ -1121,7 +1125,7 @@ static void dispatch_cursor_axis(struct sway_cursor *cursor, } // Scrolling on a tabbed or stacked title bar (handled as press event) - if (!handled && on_titlebar) { + if (!handled && (on_titlebar || on_titlebar_border)) { enum sway_container_layout layout = container_parent_layout(cont); if (layout == L_TABBED || layout == L_STACKED) { struct sway_node *tabcontainer = node_get_parent(node); @@ -1531,3 +1535,66 @@ void cursor_warp_to_workspace(struct sway_cursor *cursor, wlr_cursor_warp(cursor->cursor, NULL, x, y); } + +uint32_t get_mouse_bindsym(const char *name, char **error) { + if (strncasecmp(name, "button", strlen("button")) == 0) { + // Map to x11 mouse buttons + int number = name[strlen("button")] - '0'; + if (number < 1 || number > 9 || strlen(name) > strlen("button0")) { + *error = strdup("Only buttons 1-9 are supported. For other mouse " + "buttons, use the name of the event code."); + return 0; + } + static const uint32_t buttons[] = {BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, + SWAY_SCROLL_UP, SWAY_SCROLL_DOWN, SWAY_SCROLL_LEFT, + SWAY_SCROLL_RIGHT, BTN_SIDE, BTN_EXTRA}; + return buttons[number - 1]; + } else if (strncmp(name, "BTN_", strlen("BTN_")) == 0) { + // Get event code from name + int code = libevdev_event_code_from_name(EV_KEY, name); + if (code == -1) { + size_t len = snprintf(NULL, 0, "Unknown event %s", name) + 1; + *error = malloc(len); + if (*error) { + snprintf(*error, len, "Unknown event %s", name); + } + return 0; + } + return code; + } + return 0; +} + +uint32_t get_mouse_bindcode(const char *name, char **error) { + // Validate event code + errno = 0; + char *endptr; + int code = strtol(name, &endptr, 10); + if (endptr == name && code <= 0) { + *error = strdup("Button event code must be a positive integer."); + return 0; + } else if (errno == ERANGE) { + *error = strdup("Button event code out of range."); + return 0; + } + const char *event = libevdev_event_code_get_name(EV_KEY, code); + if (!event || strncmp(event, "BTN_", strlen("BTN_")) != 0) { + size_t len = snprintf(NULL, 0, "Event code %d (%s) is not a button", + code, event) + 1; + *error = malloc(len); + if (*error) { + snprintf(*error, len, "Event code %d (%s) is not a button", + code, event); + } + return 0; + } + return code; +} + +uint32_t get_mouse_button(const char *name, char **error) { + uint32_t button = get_mouse_bindsym(name, error); + if (!button && !error) { + button = get_mouse_bindcode(name, error); + } + return button; +} diff --git a/sway/input/seat.c b/sway/input/seat.c index 41a3a4df..a8df5b99 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -1,11 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include <assert.h> #include <errno.h> -#ifdef __linux__ #include <linux/input-event-codes.h> -#elif __FreeBSD__ -#include <dev/evdev/input-event-codes.h> -#endif #include <strings.h> #include <time.h> #include <wlr/types/wlr_cursor.h> diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 28546d41..ff1bc89f 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -1,10 +1,6 @@ // See https://i3wm.org/docs/ipc.html for protocol information #define _POSIX_C_SOURCE 200112L -#ifdef __linux__ #include <linux/input-event-codes.h> -#elif __FreeBSD__ -#include <dev/evdev/input-event-codes.h> -#endif #include <assert.h> #include <errno.h> #include <fcntl.h> @@ -449,8 +445,12 @@ void ipc_event_binding(struct sway_binding *binding) { json_object_object_add(json_binding, "input_code", json_object_new_int(input_code)); json_object_object_add(json_binding, "symbols", symbols); json_object_object_add(json_binding, "symbol", symbol); - json_object_object_add(json_binding, "input_type", binding->type == BINDING_MOUSE ? - json_object_new_string("mouse") : json_object_new_string("keyboard")); + + bool mouse = binding->type == BINDING_MOUSECODE || + binding->type == BINDING_MOUSESYM; + json_object_object_add(json_binding, "input_type", mouse + ? json_object_new_string("mouse") + : json_object_new_string("keyboard")); json_object *json = json_object_new_object(); json_object_object_add(json, "change", json_object_new_string("run")); diff --git a/sway/sway.5.scd b/sway/sway.5.scd index 3757a097..3398ad58 100644 --- a/sway/sway.5.scd +++ b/sway/sway.5.scd @@ -302,7 +302,7 @@ runtime. ``` *bindcode* [--release|--locked] [--input-device=<device>] [--no-warn] <code> <command> - is also available for binding with key codes instead of key names. + is also available for binding with key/button codes instead of key/button names. *client.<class>* <border> <background> <text> <indicator> <child\_border> Configures the color of window borders and title bars. All 5 colors are diff --git a/sway/tree/output.c b/sway/tree/output.c index 95ab9378..f24be010 100644 --- a/sway/tree/output.c +++ b/sway/tree/output.c @@ -58,6 +58,7 @@ struct sway_output *output_create(struct wlr_output *wlr_output) { wlr_output->data = output; wl_signal_add(&wlr_output->events.destroy, &output->destroy); + wl_signal_init(&output->events.destroy); wl_list_insert(&root->all_outputs, &output->link); @@ -76,7 +77,6 @@ void output_enable(struct sway_output *output, struct output_config *oc) { for (size_t i = 0; i < len; ++i) { wl_list_init(&output->layers[i]); } - wl_signal_init(&output->events.destroy); output->enabled = true; list_add(root->outputs, output); diff --git a/swaybar/input.c b/swaybar/input.c index 7d219be3..620da977 100644 --- a/swaybar/input.c +++ b/swaybar/input.c @@ -1,9 +1,5 @@ #include <assert.h> -#ifdef __FreeBSD__ -#include <dev/evdev/input-event-codes.h> -#else #include <linux/input-event-codes.h> -#endif #include <stdlib.h> #include <wayland-client.h> #include <wayland-cursor.h> diff --git a/swaybar/render.c b/swaybar/render.c index 9fe4ee9c..7cbcea07 100644 --- a/swaybar/render.c +++ b/swaybar/render.c @@ -90,13 +90,24 @@ static uint32_t render_status_line_text(cairo_t *cairo, return output->height; } -static void render_sharp_line(cairo_t *cairo, uint32_t color, +static void render_sharp_rectangle(cairo_t *cairo, uint32_t color, double x, double y, double width, double height) { + cairo_save(cairo); cairo_set_source_u32(cairo, color); + cairo_set_antialias(cairo, CAIRO_ANTIALIAS_NONE); + cairo_rectangle(cairo, x, y, width, height); + cairo_fill(cairo); + cairo_restore(cairo); +} + +static void render_sharp_line(cairo_t *cairo, uint32_t color, + double x, double y, double width, double height) { if (width > 1 && height > 1) { - cairo_rectangle(cairo, x, y, width, height); - cairo_fill(cairo); + render_sharp_rectangle(cairo, color, x, y, width, height); } else { + cairo_save(cairo); + cairo_set_source_u32(cairo, color); + cairo_set_antialias(cairo, CAIRO_ANTIALIAS_NONE); if (width == 1) { x += 0.5; height += y; @@ -111,6 +122,7 @@ static void render_sharp_line(cairo_t *cairo, uint32_t color, cairo_set_line_width(cairo, 1.0); cairo_line_to(cairo, width, height); cairo_stroke(cairo); + cairo_restore(cairo); } } @@ -141,7 +153,7 @@ static uint32_t render_status_block(cairo_t *cairo, output->scale, block->markup, "%s", block->full_text); int margin = 3 * output->scale; - int ws_vertical_padding = WS_VERTICAL_PADDING * 2; + double ws_vertical_padding = WS_VERTICAL_PADDING * 2 * output->scale; int width = text_width; if (width < block->min_width) { @@ -157,12 +169,12 @@ static uint32_t render_status_block(cairo_t *cairo, *x -= width; if ((block->border || block->urgent) && block->border_left > 0) { - *x -= (block->border_left + margin); - block_width += block->border_left + margin; + *x -= (block->border_left * output->scale + margin); + block_width += block->border_left * output->scale + margin; } if ((block->border || block->urgent) && block->border_right > 0) { - *x -= (block->border_right + margin); - block_width += block->border_right + margin; + *x -= (block->border_right * output->scale + margin); + block_width += block->border_right * output->scale + margin; } int sep_width, sep_height; @@ -199,48 +211,41 @@ static uint32_t render_status_block(cairo_t *cairo, wl_list_insert(&output->hotspots, &hotspot->link); } - double pos = *x; + double x_pos = *x; + double y_pos = WS_VERTICAL_PADDING * output->scale; + double render_height = height - ws_vertical_padding + output->scale; uint32_t bg_color = block->urgent ? config->colors.urgent_workspace.background : block->background; if (bg_color) { - cairo_set_source_u32(cairo, bg_color); - cairo_rectangle(cairo, pos - 0.5 * output->scale, - output->scale, width, height); - cairo_fill(cairo); + render_sharp_rectangle(cairo, bg_color, x_pos, y_pos, + block_width, render_height); } uint32_t border_color = block->urgent ? config->colors.urgent_workspace.border : block->border; if (border_color && block->border_top > 0) { - render_sharp_line(cairo, border_color, - pos - 0.5 * output->scale, output->scale, - text_width, block->border_top); + render_sharp_line(cairo, border_color, x_pos, y_pos, + block_width, block->border_top * output->scale); } if (border_color && block->border_bottom > 0) { - render_sharp_line(cairo, border_color, - pos - 0.5 * output->scale, - height - output->scale - block->border_bottom, - text_width, block->border_bottom); - } - if (border_color != 0 && block->border_left > 0) { - render_sharp_line(cairo, border_color, - pos - 0.5 * output->scale, output->scale, - block->border_left, height); + render_sharp_line(cairo, border_color, x_pos, + y_pos + render_height - block->border_bottom * output->scale, + block_width, block->border_bottom * output->scale); } - if (border_color != 0 && block->border_right > 0) { - render_sharp_line(cairo, border_color, - pos - 0.5 * output->scale + text_width, output->scale, - block->border_right, height); + if (border_color && block->border_left > 0) { + render_sharp_line(cairo, border_color, x_pos, y_pos, + block->border_left * output->scale, render_height); + x_pos += block->border_left * output->scale + margin; } double offset = 0; if (strncmp(block->align, "left", 5) == 0) { - offset = pos; + offset = x_pos; } else if (strncmp(block->align, "right", 5) == 0) { - offset = pos + width - text_width; + offset = x_pos + width - text_width; } else if (strncmp(block->align, "center", 6) == 0) { - offset = pos + (width - text_width) / 2; + offset = x_pos + (width - text_width) / 2; } cairo_move_to(cairo, offset, height / 2.0 - text_height / 2.0); uint32_t color = block->color ? *block->color : config->colors.statusline; @@ -248,14 +253,13 @@ static uint32_t render_status_block(cairo_t *cairo, cairo_set_source_u32(cairo, color); pango_printf(cairo, config->font, output->scale, block->markup, "%s", block->full_text); - pos += width; + x_pos += width; if (block->border && block->border_right > 0) { - pos += margin; - render_sharp_line(cairo, block->border, - pos - 0.5 * output->scale, output->scale, - block->border_right, height); - pos += block->border_right; + x_pos += margin; + render_sharp_line(cairo, border_color, x_pos, y_pos, + block->border_right * output->scale, render_height); + x_pos += block->border_right * output->scale; } if (!edge && block->separator) { @@ -265,14 +269,14 @@ static uint32_t render_status_block(cairo_t *cairo, cairo_set_source_u32(cairo, config->colors.separator); } if (config->sep_symbol) { - offset = pos + (sep_block_width - sep_width) / 2; + offset = x_pos + (sep_block_width - sep_width) / 2; cairo_move_to(cairo, offset, height / 2.0 - sep_height / 2.0); pango_printf(cairo, config->font, output->scale, false, "%s", config->sep_symbol); } else { cairo_set_line_width(cairo, 1); - cairo_move_to(cairo, pos + sep_block_width / 2, margin); - cairo_line_to(cairo, pos + sep_block_width / 2, height - margin); + cairo_move_to(cairo, x_pos + sep_block_width / 2, margin); + cairo_line_to(cairo, x_pos + sep_block_width / 2, height - margin); cairo_stroke(cairo); } } |