1 /*****************************************************************************
3 * mtdev - Multitouch Protocol Translation Library (MIT license)
5 * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
6 * Copyright (C) 2010 Canonical Ltd.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
27 ****************************************************************************/
31 #define SETABS(c, x, map, key, fd) \
32 (c->has_##x = getbit(map, key) && getabs(&c->x, key, fd))
34 static const int SN_COORD = 250; /* coordinate signal-to-noise ratio */
35 static const int SN_WIDTH = 100; /* width signal-to-noise ratio */
36 static const int SN_ORIENT = 10; /* orientation signal-to-noise ratio */
38 static const int bits_per_long = 8 * sizeof(long);
40 static inline int nlongs(int nbit)
42 return (nbit + bits_per_long - 1) / bits_per_long;
45 static inline int getbit(const unsigned long *map, int key)
47 return (map[key / bits_per_long] >> (key % bits_per_long)) & 0x01;
50 static int getabs(struct input_absinfo *abs, int key, int fd)
53 SYSCALL(rc = ioctl(fd, EVIOCGABS(key), abs));
57 static int has_mt_data(const struct mtdev_caps *cap)
59 return cap->has_abs[MTDEV_POSITION_X] && cap->has_abs[MTDEV_POSITION_Y];
62 static void default_fuzz(struct mtdev_caps *cap, int bit, int sn)
64 if (cap->has_abs[bit] && cap->abs[bit].fuzz == 0)
66 (cap->abs[bit].maximum - cap->abs[bit].minimum) / sn;
69 static int read_caps(struct mtdev_caps *cap, int fd)
71 unsigned long absbits[nlongs(ABS_MAX)];
74 memset(cap, 0, sizeof(struct mtdev_caps));
76 SYSCALL(rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits));
80 SETABS(cap, slot, absbits, ABS_MT_SLOT, fd);
81 for (i = 0; i < MT_ABS_SIZE; i++)
82 SETABS(cap, abs[i], absbits, mtdev_mt2abs(i), fd);
84 cap->has_mtdata = has_mt_data(cap);
86 if (!cap->has_abs[MTDEV_POSITION_X])
87 getabs(&cap->abs[MTDEV_POSITION_X], ABS_X, fd);
88 if (!cap->has_abs[MTDEV_POSITION_Y])
89 getabs(&cap->abs[MTDEV_POSITION_Y], ABS_Y, fd);
90 if (!cap->has_abs[MTDEV_PRESSURE])
91 getabs(&cap->abs[MTDEV_PRESSURE], ABS_PRESSURE, fd);
93 if (!cap->has_abs[MTDEV_TRACKING_ID]) {
94 cap->abs[MTDEV_TRACKING_ID].minimum = MT_ID_MIN;
95 cap->abs[MTDEV_TRACKING_ID].maximum = MT_ID_MAX;
98 default_fuzz(cap, MTDEV_POSITION_X, SN_COORD);
99 default_fuzz(cap, MTDEV_POSITION_Y, SN_COORD);
100 default_fuzz(cap, MTDEV_TOUCH_MAJOR, SN_WIDTH);
101 default_fuzz(cap, MTDEV_TOUCH_MINOR, SN_WIDTH);
102 default_fuzz(cap, MTDEV_WIDTH_MAJOR, SN_WIDTH);
103 default_fuzz(cap, MTDEV_WIDTH_MINOR, SN_WIDTH);
104 default_fuzz(cap, MTDEV_ORIENTATION, SN_ORIENT);
109 int mtdev_configure(struct mtdev *dev, int fd)
111 return read_caps(&dev->caps, fd);