From c08762901e9840d8dca008dcc8d0b5602602fd0a Mon Sep 17 00:00:00 2001
From: Cezary Drożak <cezary@drozak.net>
Date: Fri, 16 Jun 2023 11:28:30 +0200
Subject: input/libinput: add scroll_button_lock method

Closes https://github.com/swaywm/sway/issues/6987

Co-authored-by: JJGadgets <git@jjgadgets.tech>
Co-authored-by: DeltaWhy <mike5713@gmail.com>
---
 sway/commands/input.c                    |  1 +
 sway/commands/input/scroll_button_lock.c | 26 ++++++++++++++++++++++++++
 sway/config/input.c                      |  4 ++++
 sway/input/libinput.c                    | 15 +++++++++++++++
 sway/ipc-json.c                          | 11 +++++++++++
 sway/meson.build                         |  1 +
 sway/sway-input.5.scd                    |  3 +++
 sway/sway-ipc.7.scd                      |  3 +++
 8 files changed, 64 insertions(+)
 create mode 100644 sway/commands/input/scroll_button_lock.c

(limited to 'sway')

diff --git a/sway/commands/input.c b/sway/commands/input.c
index 3075b5f4..306c40f7 100644
--- a/sway/commands/input.c
+++ b/sway/commands/input.c
@@ -27,6 +27,7 @@ static const struct cmd_handler input_handlers[] = {
 	{ "repeat_rate", input_cmd_repeat_rate },
 	{ "rotation_angle", input_cmd_rotation_angle },
 	{ "scroll_button", input_cmd_scroll_button },
+	{ "scroll_button_lock", input_cmd_scroll_button_lock },
 	{ "scroll_factor", input_cmd_scroll_factor },
 	{ "scroll_method", input_cmd_scroll_method },
 	{ "tap", input_cmd_tap },
diff --git a/sway/commands/input/scroll_button_lock.c b/sway/commands/input/scroll_button_lock.c
new file mode 100644
index 00000000..f96b6514
--- /dev/null
+++ b/sway/commands/input/scroll_button_lock.c
@@ -0,0 +1,26 @@
+#include <libinput.h>
+#include <string.h>
+#include <strings.h>
+#include "sway/config.h"
+#include "sway/commands.h"
+#include "sway/input/input-manager.h"
+#include "util.h"
+
+struct cmd_results *input_cmd_scroll_button_lock(int argc, char **argv) {
+	struct cmd_results *error = NULL;
+	if ((error = checkarg(argc, "scroll_button_lock", EXPECTED_AT_LEAST, 1))) {
+		return error;
+	}
+	struct input_config *ic = config->handler_context.input_config;
+	if (!ic) {
+		return cmd_results_new(CMD_FAILURE, "No input device defined.");
+	}
+
+	if (parse_boolean(argv[0], true)) {
+		ic->scroll_button_lock = LIBINPUT_CONFIG_SCROLL_BUTTON_LOCK_ENABLED;
+	} else {
+		ic->scroll_button_lock = LIBINPUT_CONFIG_SCROLL_BUTTON_LOCK_DISABLED;
+	}
+
+	return cmd_results_new(CMD_SUCCESS, NULL);
+}
diff --git a/sway/config/input.c b/sway/config/input.c
index 2ee165c9..44c2be28 100644
--- a/sway/config/input.c
+++ b/sway/config/input.c
@@ -35,6 +35,7 @@ struct input_config *new_input_config(const char* identifier) {
 	input->pointer_accel = FLT_MIN;
 	input->scroll_factor = FLT_MIN;
 	input->scroll_button = INT_MIN;
+	input->scroll_button_lock = INT_MIN;
 	input->scroll_method = INT_MIN;
 	input->left_handed = INT_MIN;
 	input->repeat_delay = INT_MIN;
@@ -96,6 +97,9 @@ void merge_input_config(struct input_config *dst, struct input_config *src) {
 	if (src->scroll_button != INT_MIN) {
 		dst->scroll_button = src->scroll_button;
 	}
+	if (src->scroll_button_lock != INT_MIN) {
+		dst->scroll_button_lock = src->scroll_button_lock;
+	}
 	if (src->send_events != INT_MIN) {
 		dst->send_events = src->send_events;
 	}
diff --git a/sway/input/libinput.c b/sway/input/libinput.c
index dd4fc0be..43875634 100644
--- a/sway/input/libinput.c
+++ b/sway/input/libinput.c
@@ -166,6 +166,18 @@ static bool set_scroll_button(struct libinput_device *dev, uint32_t button) {
 	return true;
 }
 
+static bool set_scroll_button_lock(struct libinput_device *dev,
+		enum libinput_config_scroll_button_lock_state lock) {
+	uint32_t scroll = libinput_device_config_scroll_get_methods(dev);
+	if ((scroll & ~LIBINPUT_CONFIG_SCROLL_NO_SCROLL) == 0 ||
+			libinput_device_config_scroll_get_button_lock(dev) == lock) {
+		return false;
+	}
+	sway_log(SWAY_DEBUG, "scroll_set_button_lock(%" PRIu32 ")", lock);
+	log_status(libinput_device_config_scroll_set_button_lock(dev, lock));
+	return true;
+}
+
 static bool set_dwt(struct libinput_device *device, bool dwt) {
 	if (!libinput_device_config_dwt_is_available(device) ||
 			libinput_device_config_dwt_get_enabled(device) == dwt) {
@@ -276,6 +288,9 @@ bool sway_input_configure_libinput_device(struct sway_input_device *input_device
 	if (ic->scroll_button != INT_MIN) {
 		changed |= set_scroll_button(device, ic->scroll_button);
 	}
+	if (ic->scroll_button_lock != INT_MIN) {
+		changed |= set_scroll_button_lock(device, ic->scroll_button_lock);
+	}
 	if (ic->dwt != INT_MIN) {
 		changed |= set_dwt(device, ic->dwt);
 	}
diff --git a/sway/ipc-json.c b/sway/ipc-json.c
index c7cbea01..58356d4e 100644
--- a/sway/ipc-json.c
+++ b/sway/ipc-json.c
@@ -1019,6 +1019,17 @@ static json_object *describe_libinput_device(struct libinput_device *device) {
 			uint32_t button = libinput_device_config_scroll_get_button(device);
 			json_object_object_add(object, "scroll_button",
 					json_object_new_int(button));
+			const char *lock = "unknown";
+			switch (libinput_device_config_scroll_get_button_lock(device)) {
+			case LIBINPUT_CONFIG_SCROLL_BUTTON_LOCK_ENABLED:
+				lock = "enabled";
+				break;
+			case LIBINPUT_CONFIG_SCROLL_BUTTON_LOCK_DISABLED:
+				lock = "disabled";
+				break;
+			}
+			json_object_object_add(object, "scroll_button_lock",
+					json_object_new_string(lock));
 		}
 	}
 
diff --git a/sway/meson.build b/sway/meson.build
index c6a27434..bc193bf9 100644
--- a/sway/meson.build
+++ b/sway/meson.build
@@ -171,6 +171,7 @@ sway_sources = files(
 	'commands/input/repeat_delay.c',
 	'commands/input/repeat_rate.c',
 	'commands/input/scroll_button.c',
+	'commands/input/scroll_button_lock.c',
 	'commands/input/scroll_factor.c',
 	'commands/input/scroll_method.c',
 	'commands/input/tap.c',
diff --git a/sway/sway-input.5.scd b/sway/sway-input.5.scd
index 1662d55a..082b68c2 100644
--- a/sway/sway-input.5.scd
+++ b/sway/sway-input.5.scd
@@ -185,6 +185,9 @@ The following commands may only be used in the configuration file.
 	debug-events*, or as a x11 mouse button (button[1-3,8,9]). If set to
 	_disable_, it disables the scroll_method on_button_down.
 
+*input* <identifier> scroll_button_lock enabled|disabled
+	Enables or disables scroll button lock for specified input device.
+
 *input* <identifier> scroll_factor <floating point value>
 	Changes the scroll factor for the specified input device. Scroll speed will
 	be scaled by the given value, which must be non-negative.
diff --git a/sway/sway-ipc.7.scd b/sway/sway-ipc.7.scd
index 42aaaab6..f4a5ccff 100644
--- a/sway/sway-ipc.7.scd
+++ b/sway/sway-ipc.7.scd
@@ -1195,6 +1195,9 @@ following properties will be included for devices that support them:
 :  int
 :  The scroll button to use when _scroll_method_ is _on_button_down_. This
    will be given as an input event code
+|- scroll_button_lock
+:  string
+:  Whether scroll button lock is enabled. It can be _enabled_ or _disabled_
 |- dwt
 :  string
 :  Whether disable-while-typing is enabled. It can be _enabled_ or _disabled_
-- 
cgit v1.2.3