1
/*****************************************************************************
3
* grail - Gesture Recognition And Instantiation Library
5
* Copyright (C) 2010 Canonical Ltd.
6
* Copyright (C) 2010 Henrik Rydberg <rydberg@bitmath.org>
8
* This program is free software: you can redistribute it and/or modify it
9
* under the terms of the GNU General Public License as published by the
10
* Free Software Foundation, either version 3 of the License, or (at your
11
* option) any later version.
13
* This program is distributed in the hope that it will be useful, but
14
* WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* General Public License for more details.
18
* You should have received a copy of the GNU General Public License along
19
* with this program. If not, see <http://www.gnu.org/licenses/>.
21
****************************************************************************/
23
#include <grail-touch.h>
27
/* see mtdev-mapping.h */
28
#define MTDEV_POSITION_X 5
29
#define MTDEV_POSITION_Y 6
30
#define MTDEV_ORIENTATION 4
31
#define MTDEV_PRESSURE 10
33
#define SYSCALL(call) while (((call) == -1) && (errno == EINTR))
35
static const int bits_per_long = 8 * sizeof(long);
37
static inline int nlongs(int nbit)
39
return (nbit + bits_per_long - 1) / bits_per_long;
42
static inline int getbit(const unsigned long *map, int key)
44
return (map[key / bits_per_long] >> (key % bits_per_long)) & 0x01;
47
int touch_caps_is_supported(struct touch_dev *dev, int fd)
49
unsigned long keybits[nlongs(KEY_MAX)];
50
unsigned long absbits[nlongs(ABS_MAX)];
51
unsigned long relbits[nlongs(REL_CNT)];
54
SYSCALL(rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits));
57
SYSCALL(rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits));
60
SYSCALL(rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbits)), relbits));
64
/* only support pure absolute devices at this time */
65
if (getbit(relbits, REL_X) || getbit(relbits, REL_Y))
68
if (dev->mtdev.caps.has_mtdata)
71
return getbit(absbits, ABS_X) &&
72
getbit(absbits, ABS_Y) &&
73
getbit(keybits, BTN_TOUCH) &&
74
getbit(keybits, BTN_TOOL_DOUBLETAP);
77
void touch_caps_init(struct touch_dev *dev)
79
const struct mtdev_caps *mt = &dev->mtdev.caps;
80
struct touch_caps *caps = &dev->caps;
81
caps->min_x = mt->abs[MTDEV_POSITION_X].minimum;
82
caps->max_x = mt->abs[MTDEV_POSITION_X].maximum;
83
caps->min_y = mt->abs[MTDEV_POSITION_Y].minimum;
84
caps->max_y = mt->abs[MTDEV_POSITION_Y].maximum;
85
caps->min_orient = mt->abs[MTDEV_ORIENTATION].minimum;
86
caps->max_orient = mt->abs[MTDEV_ORIENTATION].maximum;
87
caps->min_press = mt->abs[MTDEV_PRESSURE].minimum;
88
caps->max_press = mt->abs[MTDEV_PRESSURE].maximum;
89
if (caps->min_x == caps->max_x) {
93
if (caps->min_y == caps->max_y) {
97
if (caps->min_orient == caps->max_orient)
99
if (caps->min_orient + caps->max_orient != 0)
100
caps->min_orient = -caps->max_orient;
101
if (caps->min_press == caps->max_press) {
103
caps->max_press = 256;
107
float touch_angle(const struct touch_dev *dev, int orient)
109
const struct touch_caps *caps = &dev->caps;
110
return orient / caps->max_orient * M_PI_2;
113
float touch_pressure(const struct touch_dev *dev, int press)
115
const struct touch_caps *caps = &dev->caps;
116
return (press - caps->min_press) / (caps->max_press - caps->min_press);