summaryrefslogtreecommitdiff
path: root/tools/mtview.c
blob: 0420c05265a948098e674648dae27d9aae2c45bf (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <utouch/frame-xi2.h>
#include <utouch/frame-mtdev.h>
#include <linux/input.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>

#define XMARG 16
#define YMARG 16
#define DEF_FRAC 0.15
#define DEF_WIDTH 0.05

#define DIM_TOUCH 32

static int opcode;
struct mtdev;

struct windata {
	Display *dsp;
	Window root, win;
	GC gc;
	int screen, width, height;
	unsigned long white, black;
	unsigned long color[DIM_TOUCH];
	int id[DIM_TOUCH];
};

static inline float max(float a, float b)
{
	return b > a ? b : a;
}

static unsigned long new_color(struct windata *w)
{
	return lrand48() & 0xffffff;
}

static void clear_screen(struct windata *w)
{
	XSetForeground(w->dsp, w->gc, w->black);
	XFillRectangle(w->dsp, w->win, w->gc, 0, 0, w->width, w->height);
}

static void output_touch(utouch_frame_handle fh, struct windata *w,
			 const struct utouch_contact *t)
{
	const struct utouch_surface *s = utouch_frame_get_surface(fh);

	float x1 = s->min_x, y1 = s->min_y;
	float x2 = s->max_x, y2 = s->max_y;
	float dx = x2 - x1, dy = y2 - y1;
	float major = 0, minor = 0, angle = 0;

	if (s->use_pressure) {
		float p = DEF_FRAC / s->max_pressure;
		major = t->pressure * p * dx;
		minor = t->pressure * p * dx;
		angle = 0;
	}
	if (s->use_touch_major) {
		major = t->touch_major;
		minor = t->touch_minor;
		angle = t->orientation;
	}
	if (major == 0 && minor == 0) {
		major = DEF_WIDTH * dy;
		minor = DEF_WIDTH * dx;
	}

	float ac = fabs(cos(angle));
	float as = fabs(sin(angle));
	float mx = max(minor * ac, major * as);
	float my = max(major * ac, minor * as);

	if (w->id[t->slot] != t->id) {
		w->id[t->slot] = t->id;
		w->color[t->slot] = new_color(w);
	}

	XSetForeground(w->dsp, w->gc, w->color[t->slot]);
	XFillArc(w->dsp, w->win, w->gc, t->x - mx / 2, t->y - my / 2, mx, my, 0,
		 360 * 64);
	XFlush(w->dsp);
}

static void report_frame(utouch_frame_handle fh,
			 const struct utouch_frame *frame,
			 struct windata *w)
{
	int i;

	for (i = 0; i < frame->num_active; i++)
		output_touch(fh, w, frame->active[i]);
}

static int init_window(struct windata *w)
{
	int event, err;
	int i;
	memset(w, 0, sizeof(w));
	for (i = 0; i < DIM_TOUCH; i++)
		w->id[i] = -1;

	w->dsp = XOpenDisplay(NULL);
	if (!w->dsp)
		return -1;
	if (!XQueryExtension(w->dsp, "XInputExtension", &opcode, &event, &err))
		return -1;

	w->screen = DefaultScreen(w->dsp);
	w->white = WhitePixel(w->dsp, w->screen);
	w->black = BlackPixel(w->dsp, w->screen);
	w->width = DisplayWidth(w->dsp, w->screen) - XMARG;
	w->height = DisplayHeight(w->dsp, w->screen) - YMARG;

	w->root = DefaultRootWindow(w->dsp);
	w->win = XCreateSimpleWindow(w->dsp, w->root,
				    0, 0, w->width, w->height,
				    0, w->black, w->white);


	w->gc = XCreateGC(w->dsp, w->win, 0, NULL);

	XMapWindow(w->dsp, w->win);
	XFlush(w->dsp);

	return 0;
}

static void term_window(struct windata *w)
{
	XDestroyWindow(w->dsp, w->win);
	XCloseDisplay(w->dsp);
}

static void run_window_mtdev(utouch_frame_handle fh, struct mtdev *dev, int fd)
{
	long eventMask = StructureNotifyMask;
	const struct utouch_frame *frame;
	struct input_event iev;
	struct windata w;
	XWindowAttributes attrs;
	XEvent xev;

	if (init_window(&w))
		return;

	XSelectInput(w.dsp, w.win, eventMask);

	do {
		XNextEvent(w.dsp, &xev);
	} while (xev.type != MapNotify);


	XSelectInput(w.dsp, w.win, StructureNotifyMask);

	clear_screen(&w);

	XGetWindowAttributes(w.dsp, w.win, &attrs);
	utouch_frame_mtdev_set_dimensions(fh, attrs.width, attrs.height);

	while (1) {
		while (!mtdev_idle(dev, fd, 100)) {
			while (mtdev_get(dev, fd, &iev, 1) > 0) {
				frame = utouch_frame_pump_mtdev(fh, &iev);
				if (frame)
					report_frame(fh, frame, &w);
			}
		}
		while (XPending(w.dsp)) {
			XNextEvent(w.dsp, &xev);
			if (xev.type == ConfigureNotify) {
				XConfigureEvent *xce = (XConfigureEvent *)&xev;
				utouch_frame_mtdev_set_dimensions(fh,
								  xce->width,
								  xce->height);
			}
		}
	}

	term_window(&w);
}

static void run_window_xi2(struct windata *w,
			   utouch_frame_handle fh,
			   XIDeviceInfo *dev)
{
	const struct utouch_frame *frame;
	XIEventMask mask;

	fprintf(stderr, "xi2 running\n");

	XSelectInput(w->dsp, w->win, StructureNotifyMask);
	XSelectInput(w->dsp, XDefaultRootWindow(w->dsp), StructureNotifyMask);

	mask.deviceid = dev->deviceid;
	mask.mask_len = XIMaskLen(XI_TouchMotion);
	mask.mask = calloc(mask.mask_len, sizeof(char));

	XISetMask(mask.mask, XI_PropertyEvent);
	XISetMask(mask.mask, XI_TouchBegin);
	XISetMask(mask.mask, XI_TouchMotion);
	XISetMask(mask.mask, XI_TouchEnd);
	XISelectEvents(w->dsp, w->win, &mask, 1);

	

	while (1) {
		XEvent ev;
		XNextEvent(w->dsp, &ev);
		XConfigureEvent *xce = &ev.xconfigure;

		if (ev.type == ConfigureNotify && (xce->width != w->width ||
						   xce->height != w->height)) {
			w->width = xce->width;
			w->height = xce->height;
			clear_screen(w);
		}

		frame = utouch_frame_pump_xi2(fh, &ev);
		if (frame)
			report_frame(fh, frame, w);
	}
}

static int run_mtdev(const char *name)
{
	struct evemu_device *evemu;
	struct mtdev *mtdev;
	utouch_frame_handle fh;
	int fd;

	fd = open(name, O_RDONLY | O_NONBLOCK);
	if (fd < 0) {
		fprintf(stderr, "error: could not open device\n");
		return -1;
	}

	evemu = evemu_new(0);
	if (!evemu || evemu_extract(evemu, fd)) {
		fprintf(stderr, "error: could not describe device\n");
		return -1;
	}
	if (!utouch_frame_is_supported_mtdev(evemu)) {
		fprintf(stderr, "error: unsupported device\n");
		return -1;
	}
	mtdev = mtdev_new_open(fd);
	if (!mtdev) {
		fprintf(stderr, "error: could not open mtdev\n");
		return -1;
	}
	fh = utouch_frame_new_engine(100, 32, 100);
	if (!fh || utouch_frame_init_mtdev(fh, evemu)) {
		fprintf(stderr, "error: could not init frame\n");
		return -1;
	}

	run_window_mtdev(fh, mtdev, fd);

	utouch_frame_delete_engine(fh);
	mtdev_close_delete(mtdev);
	evemu_delete(evemu);

	ioctl(fd, EVIOCGRAB, 0);
	close(fd);

	return 0;
}

static int run_xi2(int id)
{
	struct windata w;
	XIDeviceInfo *info, *dev;
	utouch_frame_handle fh;
	int ndevice;
	int i;

	if (init_window(&w)) {
		fprintf(stderr, "error: could not init window\n");
		return -1;
	}

	info = XIQueryDevice(w.dsp, XIAllDevices, &ndevice);
	dev = 0;
	for (i = 0; i < ndevice; i++)
		if (info[i].deviceid == id)
			dev = &info[i];
	if (!dev)
		return -1;

	if (!utouch_frame_is_supported_xi2(w.dsp, dev)) {
		fprintf(stderr, "error: unsupported device\n");
		return -1;
	}

	fh = utouch_frame_new_engine(100, 32, 100);
	if (!fh || utouch_frame_init_xi2(fh, w.dsp, dev)) {
		fprintf(stderr, "error: could not init frame\n");
		return -1;
	}

	run_window_xi2(&w, fh, dev);

	utouch_frame_delete_engine(fh);
	XIFreeDeviceInfo(info);
	term_window(&w);

	return 0;
}

int main(int argc, char *argv[])
{
	int id, ret;

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

	id = atoi(argv[1]);
	if (id)
		ret = run_xi2(id);
	else
		ret = run_mtdev(argv[1]);

	return ret;
}