summaryrefslogtreecommitdiff
path: root/stmfts.c
blob: 5c2634e305e1ed35f48cc435b6cb8e4fcbd7b5a3 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * ST Microelectronics Finger Tip S stmfts testing program
 * Copyright (C) 2017  Andi Shyti <andi.shyti@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <dirent.h>
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <linux/input.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

enum stmfts_ev_type {
	STMFTS_EV_TYPE_MT_TOUCH,
	STMFTS_EV_TYPE_HOVER,
	STMFTS_EV_TYPE_KEY
};

#define STMFTS_INPUT_PATH	"/dev/input/"
#define STMFTS_INPUT_NAME_SIZE	256

struct stmfts_event {
	struct input_event ev;

	enum stmfts_ev_type type;

	int id;
	int x;
	int y;
	int z; /* distance, for hovering */
	int orientation;
	int major;
	int minor;
	int area;

	/* key buttons */
	int key_menu;
	int key_back;
};

static int stmfts_select(const struct dirent *ep)
{
	if(ep->d_type != DT_CHR)
		return 0;

	if(!strstr(ep->d_name, "event"))
		return 0;

	return 1;
}

int stmfts_read_event(int fd)
{
	int ret;
	struct stmfts_event sev;
	int slot;

	memset(&sev, 0, sizeof(sev));

	while(1) {
		ret = read(fd, &sev.ev, sizeof(sev.ev));

		if (ret < 0) {
			perror("unable to read from device");
			return -1;
		}
		if (ret != sizeof(sev.ev)) {
			error(0, EIO, "unable to read properly");
			return -1;
		}

		switch(sev.ev.type) {
		case EV_SYN:
			switch (sev.type) {
			case STMFTS_EV_TYPE_MT_TOUCH:
				printf("touch: [%d] id = %d, x = %d, y = %d, "
					"major = %d, minor = %d, orientation = %d, "
					"area = %d\n", slot, sev.id, sev.x, sev.y,
					sev.major, sev.minor, sev.orientation, sev.area);
				break;
			case STMFTS_EV_TYPE_HOVER:
				printf("hover: x = %d, y = %d, z = %d\n",
					sev.x, sev.y, sev.z);
				break;
			case STMFTS_EV_TYPE_KEY:
				if (sev.key_menu < 0)
					printf("key: back %s\n", sev.key_back ? "pressed" : "released");
				else
					printf("key: menu %s\n", sev.key_menu ? "pressed" : "released");
				break;
			}
			memset(&sev, 0, sizeof(sev));
			break;
		case EV_ABS:
			switch(sev.ev.code) {
			case ABS_MT_POSITION_X:
				sev.type = STMFTS_EV_TYPE_MT_TOUCH;
				sev.x = sev.ev.value;
				break;
			case ABS_MT_POSITION_Y:
				sev.type = STMFTS_EV_TYPE_MT_TOUCH;
				sev.y = sev.ev.value;
				break;
			case ABS_MT_TOUCH_MAJOR:
				sev.type = STMFTS_EV_TYPE_MT_TOUCH;
				sev.major = sev.ev.value;
				break;
			case ABS_MT_TOUCH_MINOR:
				sev.type = STMFTS_EV_TYPE_MT_TOUCH;
				sev.minor = sev.ev.value;
				break;
			case ABS_MT_ORIENTATION:
				sev.type = STMFTS_EV_TYPE_MT_TOUCH;
				sev.orientation = sev.ev.value;
				break;
			case ABS_MT_PRESSURE:
				sev.type = STMFTS_EV_TYPE_MT_TOUCH;
				sev.area = sev.ev.value;
				break;
			case ABS_MT_TRACKING_ID:
				sev.id = sev.ev.value;
				break;
			case ABS_MT_SLOT:
				sev.type = STMFTS_EV_TYPE_MT_TOUCH;
				slot = sev.ev.value;
				break;
			case ABS_X:
				sev.type = STMFTS_EV_TYPE_HOVER;
				sev.x = sev.ev.value;
				break;
			case ABS_Y:
				sev.type = STMFTS_EV_TYPE_HOVER;
				sev.y = sev.ev.value;
				break;
			case ABS_DISTANCE:
				sev.type = STMFTS_EV_TYPE_HOVER;
				sev.z = sev.ev.value;
				break;
			default:
				fprintf(stderr, "*** unhandled event code"
					"(type = %x, code = %x, value = %d)\n",
					sev.ev.type, sev.ev.code, sev.ev.value);
			}
			break;
		case EV_KEY:
			switch(sev.ev.code) {
			case KEY_MENU:
				sev.type = STMFTS_EV_TYPE_KEY;
				sev.key_back = -1;
				sev.key_menu = sev.ev.value;
				break;
			case KEY_BACK:
				sev.type = STMFTS_EV_TYPE_KEY;
				sev.key_menu = -1;
				sev.key_back = sev.ev.value;
				break;
			default:
				fprintf(stderr, "*** unhandled event code"
					"(type = %x, code = %x, value = %d)\n",
					sev.ev.type, sev.ev.code, sev.ev.value);
			}
			break;
		default:
			fprintf(stderr, "*** unhandled event type"
				"(type = %x, code = %x, value = %d)\n",
				sev.ev.type, sev.ev.code, sev.ev.value);
		}
	}

	return -1;
}

int stmfts_open_event(char *fname)
{
	struct dirent **eps;
	int i;
	int n;
	int ret = 0;

	n = scandir(STMFTS_INPUT_PATH, &eps, stmfts_select, alphasort);
	if (!n) {
		perror ("couldn't open the directory");
		return -1;
	}

	for (i = 0; i < n; i++) {
		char path[64];
		int fd;
		char dev_name[STMFTS_INPUT_NAME_SIZE];

		sprintf(path, STMFTS_INPUT_PATH "%s", eps[i]->d_name);

		fd = open(path, O_RDONLY);
		if (fd < 0) {
			perror("cannot open file");
			return -1;
		}

		ret = ioctl(fd, EVIOCGNAME(STMFTS_INPUT_NAME_SIZE), dev_name);
		if (ret < 0) {
			perror("unable to get the name");
			close(fd);
			continue;
		}

		ret = strncmp(fname, dev_name, STMFTS_INPUT_NAME_SIZE);
		if (ret) {
			close(fd);
			continue;
		}

		return fd;
	}

	error(0, ENODEV, "input device not found");
	return -1;
}

int stmfts_close_event(int fd)
{
	return close(fd);
}

void stmfts_print_usage(char *argv)
{
	printf("usage: %s <input dev name>\n", argv);
	printf("\n");
	printf("\'input dev name\' is the name given by the device driver "
		"to the /dev/inputX interface\n");
}

int main(int argc, char *argv[])
{
	int fd, err;
	struct input_mask mask;

	if (argc != 2) {
		error(0, EPERM, "missing event name");
		stmfts_print_usage(argv[0]);
		return -1;
	}

	fd = stmfts_open_event(argv[1]);
	if (fd < 0) {
		error(0, ENODEV, "input event not found");
		return -1;
	}

	mask.type = EV_ABS;
	mask.codes_size = 0;
	mask.codes_ptr = 0;

	err = ioctl(fd, EVIOCSMASK, &mask);
	if (err < 0) {
		error(0, EFAULT, "ioctl failed");
		return -1;
	}

	fd = stmfts_read_event(fd);
	if (fd) {
		fprintf(stderr, "something went wrong\n");
		return -1;
	}

	return 0;
}