aboutsummaryrefslogtreecommitdiffstats
path: root/src/capabilities.c
blob: 7f8a8f76187c6a820ce970c8ccbe92292cfa7899 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "capabilities.h"

////////////////////////////////////////////////////////

#define SETABS(c, x, map, key, fd)					\
	c->has_##x = getbit(map, key) && getabs(&c->abs_##x, key, fd)

#define ADDCAP(s, c, x) strcat(s, c->has_##x ? " " #x : "")

////////////////////////////////////////////////////////

static const int bits_per_long = 8 * sizeof(long);

static inline int nlongs(int nbit)
{
	return (nbit + bits_per_long - 1) / bits_per_long;
}

static inline bool getbit(const unsigned long* map, int key)
{
	return (map[key / bits_per_long] >> (key % bits_per_long)) & 0x01;
}

static bool getabs(struct input_absinfo *abs, int key, int fd)
{
	int rc;
	SYSCALL(rc = ioctl(fd, EVIOCGABS(key), abs));
	return rc >= 0;
}

////////////////////////////////////////////////////////

int read_capabilities(struct Capabilities *cap, int fd)
{
	unsigned long evbits[nlongs(EV_MAX)];
	unsigned long absbits[nlongs(ABS_MAX)];
	unsigned long keybits[nlongs(KEY_MAX)];
	int rc;

	memset(cap, 0, sizeof(struct Capabilities));

	SYSCALL(rc = ioctl(fd, EVIOCGBIT(EV_SYN, sizeof(evbits)), evbits));
	if (rc < 0)
		return rc;
	SYSCALL(rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits));
	if (rc < 0)
		return rc;
	SYSCALL(rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits));
	if (rc < 0)
		return rc;

	cap->has_left = getbit(keybits, BTN_LEFT);
	cap->has_middle = getbit(keybits, BTN_MIDDLE);
	cap->has_right = getbit(keybits, BTN_RIGHT);

	SETABS(cap, touch_major, absbits, ABS_MT_TOUCH_MAJOR, fd);
	SETABS(cap, touch_minor, absbits, ABS_MT_TOUCH_MINOR, fd);
	SETABS(cap, width_major, absbits, ABS_MT_WIDTH_MAJOR, fd);
	SETABS(cap, width_minor, absbits, ABS_MT_WIDTH_MINOR, fd);
	SETABS(cap, orientation, absbits, ABS_MT_ORIENTATION, fd);
	SETABS(cap, position_x, absbits, ABS_MT_POSITION_X, fd);
	SETABS(cap, position_y, absbits, ABS_MT_POSITION_Y, fd);

	cap->has_mtdata = cap->has_position_x && cap->has_position_y;

	return 0;
}

////////////////////////////////////////////////////////

void output_capabilities(const struct Capabilities *cap)
{
	char line[1024];
	memset(line, 0, sizeof(line));
	ADDCAP(line, cap, left);
	ADDCAP(line, cap, middle);
	ADDCAP(line, cap, right);
	ADDCAP(line, cap, mtdata);
	ADDCAP(line, cap, touch_major);
	ADDCAP(line, cap, touch_minor);
	ADDCAP(line, cap, width_major);
	ADDCAP(line, cap, width_minor);
	ADDCAP(line, cap, orientation);
	ADDCAP(line, cap, position_x);
	ADDCAP(line, cap, position_y);
	xf86Msg(X_INFO, "multitouch: caps:%s\n", line);
	if (cap->has_touch_major)
		xf86Msg(X_INFO, "multitouch: touch: %d %d\n",
			cap->abs_touch_major.minimum,
			cap->abs_touch_major.maximum);
	if (cap->has_width_major)
		xf86Msg(X_INFO, "multitouch: width: %d %d\n",
			cap->abs_width_major.minimum,
			cap->abs_width_major.maximum);
	if (cap->has_orientation)
		xf86Msg(X_INFO, "multitouch: orientation: %d %d\n",
			cap->abs_orientation.minimum,
			cap->abs_orientation.maximum);
	if (cap->has_position_x)
		xf86Msg(X_INFO, "multitouch: position_x: %d %d\n",
			cap->abs_position_x.minimum,
			cap->abs_position_x.maximum);
	if (cap->has_position_y)
		xf86Msg(X_INFO, "multitouch: position_y: %d %d\n",
			cap->abs_position_y.minimum,
			cap->abs_position_y.maximum);
}

////////////////////////////////////////////////////////