summaryrefslogtreecommitdiff
path: root/tools/utouch-frame-test-mtdev.c
blob: 4838f84f19a7ef22de6830ea32937c7bf932fb6f (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
/*****************************************************************************
 *
 * utouch-frame - Touch Frame Library
 *
 * Copyright (C) 2010-2011 Canonical Ltd.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *      Henrik Rydberg <rydberg@bitmath.org>
 *
 ****************************************************************************/

#define MTDEV_NO_LEGACY_API

#include <utouch/frame-mtdev.h>
#include <mtdev.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "common-defs.h"

struct frame_test {
	struct evemu_device *evemu;
	struct mtdev *mtdev;
	utouch_frame_handle fh;
};

static int init_evemu(struct frame_test *test, int fd)
{
	test->evemu = evemu_new(NULL);
	if (!test->evemu)
		return -1;
	return evemu_extract(test->evemu, fd);
}

static int init_mtdev(struct frame_test *test, int fd)
{
	test->mtdev = mtdev_new_open(fd);
	if (!test->mtdev)
		return -1;
	return 0;
}

static int init_frame(struct frame_test *test, int fd)
{
	test->fh = utouch_frame_new_engine(100, 32, 100);
	if (!test->fh)
		return -1;
	return utouch_frame_init_mtdev(test->fh, test->evemu);
}

static void destroy_all(struct frame_test *test)
{
	utouch_frame_delete_engine(test->fh);
	mtdev_close_delete(test->mtdev);
	evemu_delete(test->evemu);
	memset(test, 0, sizeof(*test));
}

static void loop_device(struct frame_test *test, int fd)
{
	const struct utouch_frame *frame;
	struct input_event ev;

	while (!mtdev_idle(test->mtdev, fd, 5000)) {
		while (mtdev_get(test->mtdev, fd, &ev, 1) > 0) {
			frame = utouch_frame_pump_mtdev(test->fh, &ev);
			if (frame)
				report_frame(frame);
		}
	}
}

int main(int argc, char *argv[])
{
	struct frame_test test;
	int fd;

	if (argc < 2) {
		fprintf(stderr, "Usage: %s <device>\n", argv[0]);
		return -1;
	}

	fd = open(argv[1], O_RDONLY | O_NONBLOCK);
	if (fd < 0) {
		fprintf(stderr, "error: could not open device\n");
		return -1;
	}
	if (ioctl(fd, EVIOCGRAB, 1)) {
		fprintf(stderr, "error: could not grab the device\n");
		return -1;
	}

	if (init_evemu(&test, fd)) {
		fprintf(stderr, "error: could not describe device\n");
		return -1;
	}
	if (!utouch_frame_is_supported_mtdev(test.evemu)) {
		fprintf(stderr, "error: unsupported device\n");
		return -1;
	}

	fprintf(stderr, "device: %s\n", evemu_get_name(test.evemu));

	if (init_mtdev(&test, fd)) {
		fprintf(stderr, "error: could not init mtdev\n");
		return -1;
	}
	if (init_frame(&test, fd)) {
		fprintf(stderr, "error: could not init frame\n");
		return -1;
	}

	report_device_caps(test.fh);

	loop_device(&test, fd);

	destroy_all(&test);
	return 0;
}