1/*
2 * Copyright © 2015 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include "config.h"
25
26#include <fcntl.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <libudev.h>
30#include <linux/input.h>
31#include <libevdev/libevdev.h>
32
33#include "util-macros.h"
34
35static void
36reset_absfuzz_to_zero(struct udev_device *device)
37{
38	const char *devnode;
39	struct libevdev *evdev = NULL;
40	int fd = -1;
41	int rc;
42	unsigned int axes[] = {ABS_X,
43			       ABS_Y,
44			       ABS_MT_POSITION_X,
45			       ABS_MT_POSITION_Y};
46
47	devnode = udev_device_get_devnode(device);
48	if (!devnode)
49		goto out;
50
51	fd = open(devnode, O_RDWR);
52	if (fd < 0)
53		goto out;
54
55	rc = libevdev_new_from_fd(fd, &evdev);
56	if (rc != 0)
57		goto out;
58
59	if (!libevdev_has_event_type(evdev, EV_ABS))
60		goto out;
61
62	ARRAY_FOR_EACH(axes, code) {
63		struct input_absinfo abs;
64		int fuzz;
65
66		fuzz = libevdev_get_abs_fuzz(evdev, *code);
67		if (!fuzz)
68			continue;
69
70		abs = *libevdev_get_abs_info(evdev, *code);
71		abs.fuzz = 0;
72		libevdev_kernel_set_abs_info(evdev, *code, &abs);
73	}
74
75out:
76	close(fd);
77	libevdev_free(evdev);
78}
79
80int main(int argc, char **argv)
81{
82	int rc = 1;
83	struct udev *udev = NULL;
84	struct udev_device *device = NULL;
85	const char *syspath;
86
87	if (argc != 2)
88		return 1;
89
90	syspath = argv[1];
91
92	udev = udev_new();
93	if (!udev)
94		goto out;
95
96	device = udev_device_new_from_syspath(udev, syspath);
97	if (!device)
98		goto out;
99
100	reset_absfuzz_to_zero(device);
101
102	rc = 0;
103
104out:
105	if (device)
106		udev_device_unref(device);
107	if (udev)
108		udev_unref(udev);
109
110	return rc;
111}
112