[PATCH libevdev 2/2] tools - tweak-device: add a --resolution command

Peter Hutterer peter.hutterer at who-t.net
Tue Jun 30 00:21:01 PDT 2015


So far, 100% of the usages for tweak-tool was to set the x/y resolution of a
device. Make --resolution a shortcut for this.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 tools/libevdev-tweak-device.1 | 10 +++++
 tools/libevdev-tweak-device.c | 96 ++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 104 insertions(+), 2 deletions(-)

diff --git a/tools/libevdev-tweak-device.1 b/tools/libevdev-tweak-device.1
index 29f9a71..bb33387 100644
--- a/tools/libevdev-tweak-device.1
+++ b/tools/libevdev-tweak-device.1
@@ -5,6 +5,8 @@ libevdev-tweak-device \- modify an evdev kernel device
 .B libevdev-tweak-device
 --abs ABS_X [--min a] [--max b] [--res c] [--fuzz d] [--flat e]
 /dev/input/eventX
+.B libevdev-tweak-device
+--resolution res[,yres] /dev/input/eventX
 .PP
 .B libevdev-tweak-device
 --led LED_NUML --on|--off /dev/input/eventX
@@ -41,6 +43,14 @@ Set the absinfo fuzz to the value v
 .B --flat v
 Set the absinfo flat to the value v
 .PP
+.SS Changing the x/y resolution
+.TP 8
+.B --resolution res[,yres]
+Changes the resolution of the ABS_X, ABS_MT_POSITION_X, ABS_Y, and
+ABS_MT_POSITION_Y axis to the given resolution. If only one resolution value
+is provided, both x and y axis are set to the same resolution, otherwise the
+first resolution value is applied to the x axes and the second value to the
+y axes.
 .SS Toggling LEDs
 .TP 8
 .B --led led
diff --git a/tools/libevdev-tweak-device.c b/tools/libevdev-tweak-device.c
index 3d1cd23..feb1bee 100644
--- a/tools/libevdev-tweak-device.c
+++ b/tools/libevdev-tweak-device.c
@@ -24,6 +24,7 @@
 #include <config.h>
 
 #include <getopt.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -42,15 +43,20 @@ usage(void)
 {
 	printf("%s --abs <axis> [--min min] [--max max] [--res res] [--fuzz fuzz] [--flat flat] /dev/input/eventXYZ\n"
 	       "\tChange the absinfo struct for the named axis\n"
+	       "%s --resolution res[,yres] /dev/input/eventXYZ\n"
+	       "\tChange the x/y resolution on the given device\n"
 	       "%s --led <led> --on|--off /dev/input/eventXYZ\n"
 	       "\tEnable or disable the named LED\n",
-	       program_invocation_short_name, program_invocation_short_name);
+	       program_invocation_short_name,
+	       program_invocation_short_name,
+	       program_invocation_short_name);
 }
 
 enum mode {
 	MODE_NONE = 0,
 	MODE_ABS,
 	MODE_LED,
+	MODE_RESOLUTION,
 	MODE_HELP,
 };
 
@@ -64,9 +70,30 @@ enum opts {
 	OPT_LED = 1 << 6,
 	OPT_ON = 1 << 7,
 	OPT_OFF = 1 << 8,
-	OPT_HELP = 1 << 9,
+	OPT_RESOLUTION = 1 << 9,
+	OPT_HELP = 1 << 10,
 };
 
+static bool
+parse_resolution_argument(const char *arg, int *xres, int *yres)
+{
+	int matched;
+
+	matched = sscanf(arg, "%d,%d", xres, yres);
+
+	switch(matched) {
+		case 2:
+			break;
+		case 1:
+			*yres = *xres;
+			break;
+		default:
+			return false;
+	}
+
+	return true;
+}
+
 static int
 parse_options_abs(int argc, char **argv, unsigned int *changes,
 		  int *axis, struct input_absinfo *absinfo)
@@ -172,6 +199,41 @@ error:
 	return rc;
 }
 
+static int
+parse_options_resolution(int argc, char **argv, int *xres, int *yres)
+{
+	int rc = 1;
+	int c;
+	int option_index = 0;
+	static struct option opts[] = {
+		{ "resolution", 1, 0, OPT_RESOLUTION },
+		{ NULL, 0, 0, 0 },
+	};
+
+	if (argc < 2)
+		goto error;
+
+	while (1) {
+		c = getopt_long(argc, argv, "h", opts, &option_index);
+		if (c == -1)
+			break;
+
+		switch (c) {
+			case OPT_RESOLUTION:
+				if (!parse_resolution_argument(optarg,
+							       xres, yres))
+					goto error;
+				break;
+			default:
+				goto error;
+		}
+	}
+
+	rc = 0;
+error:
+	return rc;
+}
+
 static enum mode
 parse_options_mode(int argc, char **argv, const char **path)
 {
@@ -180,6 +242,7 @@ parse_options_mode(int argc, char **argv, const char **path)
 	static const struct option opts[] = {
 		{ "abs", 0, 0, OPT_ABS },
 		{ "led", 0, 0, OPT_LED },
+		{ "resolution", 0, 0, OPT_RESOLUTION },
 		{ "help", 0, 0, OPT_HELP },
 		{ NULL, 0, 0, 0 },
 	};
@@ -204,6 +267,9 @@ parse_options_mode(int argc, char **argv, const char **path)
 			case OPT_LED:
 				mode = MODE_LED;
 				break;
+			case OPT_RESOLUTION:
+				mode = MODE_RESOLUTION;
+				break;
 			default:
 				break;
 		}
@@ -276,6 +342,24 @@ set_led(struct libevdev *dev, unsigned int led, int led_state)
 			strerror(-rc));
 }
 
+static void
+set_resolution(struct libevdev *dev, int xres, int yres)
+{
+	struct input_absinfo abs;
+
+	abs.resolution = xres;
+	if (libevdev_has_event_code(dev, EV_ABS, ABS_X))
+		set_abs(dev, OPT_RES, ABS_X, &abs);
+	if (libevdev_has_event_code(dev, EV_ABS, ABS_MT_POSITION_X))
+		set_abs(dev, OPT_RES, ABS_MT_POSITION_X, &abs);
+
+	abs.resolution = yres;
+	if (libevdev_has_event_code(dev, EV_ABS, ABS_Y))
+		set_abs(dev, OPT_RES, ABS_Y, &abs);
+	if (libevdev_has_event_code(dev, EV_ABS, ABS_MT_POSITION_Y))
+		set_abs(dev, OPT_RES, ABS_MT_POSITION_Y, &abs);
+}
+
 int
 main(int argc, char **argv)
 {
@@ -289,6 +373,7 @@ main(int argc, char **argv)
 	int led;
 	int led_state = -1;
 	unsigned int changes; /* bitmask of changes */
+	int xres, yres;
 
 	mode = parse_options_mode(argc, argv, &path);
 	switch (mode) {
@@ -305,6 +390,10 @@ main(int argc, char **argv)
 		case MODE_LED:
 			rc = parse_options_led(argc, argv, &led, &led_state);
 			break;
+		case MODE_RESOLUTION:
+			rc = parse_options_resolution(argc, argv, &xres,
+						      &yres);
+			break;
 		default:
 			fprintf(stderr,
 				"++?????++ Out of Cheese Error. Redo From Start.\n");
@@ -333,6 +422,9 @@ main(int argc, char **argv)
 		case MODE_LED:
 			set_led(dev, led, led_state);
 			break;
+		case MODE_RESOLUTION:
+			set_resolution(dev, xres, yres);
+			break;
 		default:
 			break;
 	}
-- 
2.4.3



More information about the Input-tools mailing list