diff --git a/common/util.c b/common/util.c index f4588b573..0caafb39e 100644 --- a/common/util.c +++ b/common/util.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -140,6 +141,17 @@ bool parse_boolean(const char *boolean, bool current) { return false; } +float parse_float(const char *value) { + errno = 0; + char *end; + float flt = strtof(value, &end); + if (*end || errno) { + wlr_log(WLR_DEBUG, "Invalid float value '%s', defaulting to NAN", value); + return NAN; + } + return flt; +} + enum wlr_direction opposite_direction(enum wlr_direction d) { switch (d) { case WLR_DIRECTION_UP: diff --git a/include/sway/commands.h b/include/sway/commands.h index 6606775aa..2fe8a631d 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -229,6 +229,7 @@ sway_cmd input_cmd_map_to_output; sway_cmd input_cmd_middle_emulation; sway_cmd input_cmd_natural_scroll; sway_cmd input_cmd_pointer_accel; +sway_cmd input_cmd_scroll_factor; sway_cmd input_cmd_repeat_delay; sway_cmd input_cmd_repeat_rate; sway_cmd input_cmd_scroll_button; diff --git a/include/sway/config.h b/include/sway/config.h index 79c4359b8..658b4a018 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -100,6 +100,7 @@ struct input_config { int middle_emulation; int natural_scroll; float pointer_accel; + float scroll_factor; int repeat_delay; int repeat_rate; int scroll_button; diff --git a/include/util.h b/include/util.h index f143d0c0d..84318fe72 100644 --- a/include/util.h +++ b/include/util.h @@ -59,6 +59,12 @@ uint32_t parse_color(const char *color); */ bool parse_boolean(const char *boolean, bool current); +/** + * Given a string that represents a floating point value, return a float. + * Returns NAN on error. + */ +float parse_float(const char *value); + enum wlr_direction opposite_direction(enum wlr_direction d); #endif diff --git a/sway/commands/input.c b/sway/commands/input.c index c50926a85..b5765c38d 100644 --- a/sway/commands/input.c +++ b/sway/commands/input.c @@ -22,6 +22,7 @@ static struct cmd_handler input_handlers[] = { { "repeat_delay", input_cmd_repeat_delay }, { "repeat_rate", input_cmd_repeat_rate }, { "scroll_button", input_cmd_scroll_button }, + { "scroll_factor", input_cmd_scroll_factor }, { "scroll_method", input_cmd_scroll_method }, { "tap", input_cmd_tap }, { "tap_button_map", input_cmd_tap_button_map }, diff --git a/sway/commands/input/pointer_accel.c b/sway/commands/input/pointer_accel.c index df487b1cf..efd81ee64 100644 --- a/sway/commands/input/pointer_accel.c +++ b/sway/commands/input/pointer_accel.c @@ -1,8 +1,10 @@ +#include #include #include #include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" +#include "util.h" struct cmd_results *input_cmd_pointer_accel(int argc, char **argv) { struct cmd_results *error = NULL; @@ -15,8 +17,11 @@ struct cmd_results *input_cmd_pointer_accel(int argc, char **argv) { "pointer_accel", "No input device defined."); } - float pointer_accel = atof(argv[0]); - if (pointer_accel < -1 || pointer_accel > 1) { + float pointer_accel = parse_float(argv[0]); + if (isnan(pointer_accel)) { + return cmd_results_new(CMD_INVALID, "pointer_accel", + "Invalid pointer accel; expected float."); + } if (pointer_accel < -1 || pointer_accel > 1) { return cmd_results_new(CMD_INVALID, "pointer_accel", "Input out of range [-1, 1]"); } diff --git a/sway/commands/input/scroll_factor.c b/sway/commands/input/scroll_factor.c new file mode 100644 index 000000000..52d943b06 --- /dev/null +++ b/sway/commands/input/scroll_factor.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include "sway/config.h" +#include "sway/commands.h" +#include "sway/input/input-manager.h" +#include "util.h" + +struct cmd_results *input_cmd_scroll_factor(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "scroll_factor", EXPECTED_AT_LEAST, 1))) { + return error; + } + struct input_config *ic = config->handler_context.input_config; + if (!ic) { + return cmd_results_new(CMD_FAILURE, + "scroll_factor", "No input device defined."); + } + + float scroll_factor = parse_float(argv[0]); + if (isnan(scroll_factor)) { + return cmd_results_new(CMD_INVALID, "scroll_factor", + "Invalid scroll factor; expected float."); + } else if (scroll_factor < 0) { + return cmd_results_new(CMD_INVALID, "scroll_factor", + "Scroll factor cannot be negative."); + } + ic->scroll_factor = scroll_factor; + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config/input.c b/sway/config/input.c index 794d51946..d5d2d90b4 100644 --- a/sway/config/input.c +++ b/sway/config/input.c @@ -29,6 +29,7 @@ struct input_config *new_input_config(const char* identifier) { input->natural_scroll = INT_MIN; input->accel_profile = INT_MIN; input->pointer_accel = FLT_MIN; + input->scroll_factor = FLT_MIN; input->scroll_button = INT_MIN; input->scroll_method = INT_MIN; input->left_handed = INT_MIN; @@ -68,6 +69,9 @@ void merge_input_config(struct input_config *dst, struct input_config *src) { if (src->pointer_accel != FLT_MIN) { dst->pointer_accel = src->pointer_accel; } + if (src->scroll_factor != FLT_MIN) { + dst->scroll_factor = src->scroll_factor; + } if (src->repeat_delay != INT_MIN) { dst->repeat_delay = src->repeat_delay; } diff --git a/sway/input/cursor.c b/sway/input/cursor.c index c6b7414cf..d89f64d87 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -5,6 +5,7 @@ #elif __FreeBSD__ #include #endif +#include #include #include #include @@ -979,6 +980,8 @@ static void handle_cursor_button(struct wl_listener *listener, void *data) { static void dispatch_cursor_axis(struct sway_cursor *cursor, struct wlr_event_pointer_axis *event) { struct sway_seat *seat = cursor->seat; + struct sway_input_device *input_device = event->device->data; + struct input_config *ic = input_device_get_config(input_device); // Determine what's under the cursor struct wlr_surface *surface = NULL; @@ -990,6 +993,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; + float scroll_factor = + (ic == NULL || ic->scroll_factor == FLT_MIN) ? 1.0f : ic->scroll_factor; // Scrolling on a tabbed or stacked title bar if (on_titlebar) { @@ -1000,7 +1005,7 @@ static void dispatch_cursor_axis(struct sway_cursor *cursor, seat_get_active_tiling_child(seat, tabcontainer); list_t *siblings = container_get_siblings(cont); int desired = list_find(siblings, active->sway_container) + - event->delta_discrete; + round(scroll_factor * event->delta_discrete); if (desired < 0) { desired = 0; } else if (desired >= siblings->length) { @@ -1024,7 +1029,8 @@ static void dispatch_cursor_axis(struct sway_cursor *cursor, } wlr_seat_pointer_notify_axis(cursor->seat->wlr_seat, event->time_msec, - event->orientation, event->delta, event->delta_discrete, event->source); + event->orientation, scroll_factor * event->delta, + round(scroll_factor * event->delta_discrete), event->source); } static void handle_cursor_axis(struct wl_listener *listener, void *data) { diff --git a/sway/meson.build b/sway/meson.build index cde09a020..d9bc08f38 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -135,6 +135,7 @@ sway_sources = files( 'commands/input/repeat_delay.c', 'commands/input/repeat_rate.c', 'commands/input/scroll_button.c', + 'commands/input/scroll_factor.c', 'commands/input/scroll_method.c', 'commands/input/tap.c', 'commands/input/tap_button_map.c', diff --git a/sway/sway-input.5.scd b/sway/sway-input.5.scd index 82273ef3d..459946442 100644 --- a/sway/sway-input.5.scd +++ b/sway/sway-input.5.scd @@ -105,14 +105,18 @@ The following commands may only be used in the configuration file. *input* repeat\_rate Sets the frequency of key repeats once the repeat\_delay has passed. -*input* scroll\_method none|two\_finger|edge|on\_button\_down - Changes the scroll method for the specified input device. - *input* scroll\_button Sets button used for scroll\_method on\_button\_down. The button identifier can be obtained from `libinput debug-events`. If set to 0, it disables the scroll\_button on\_button\_down. +*input* scroll\_factor + Changes the scroll factor for the specified input device. Scroll speed will + be scaled by the given value, which must be non-negative. + +*input* scroll\_method none|two\_finger|edge|on\_button\_down + Changes the scroll method for the specified input device. + *input* tap enabled|disabled Enables or disables tap for specified input device.