+static int ts_fd = -1;
+
+/* touscreen. Could just use tsblib, but it's LGPL... */
+static int tsc[7];
+
+static int host_ts_init(void)
+{
+ static const char name_def[] = "/dev/input/touchscreen0";
+ const char *name;
+ FILE *f;
+ int ret;
+
+ f = fopen("/etc/pointercal", "r");
+ if (f == NULL) {
+ perror("fopen pointercal");
+ return -1;
+ }
+ ret = fscanf(f, "%d %d %d %d %d %d %d", &tsc[0], &tsc[1],
+ &tsc[2], &tsc[3], &tsc[4], &tsc[5], &tsc[6]);
+ fclose(f);
+ if (ret != 7) {
+ fprintf(stderr, "could not parse pointercal\n");
+ return -1;
+ }
+
+ name = getenv("TSLIB_TSDEVICE");
+ if (name == NULL)
+ name = name_def;
+ ts_fd = open(name, O_RDONLY | O_NONBLOCK);
+ if (ts_fd == -1) {
+ fprintf(stderr, "open %s", name);
+ perror("");
+ return -1;
+ }
+ return 0;
+}
+
+// returns ranges 0-1023
+int host_read_ts(int *pressure, int *x1024, int *y1024)
+{
+ static int raw_x, raw_y, raw_p;
+ struct input_event ev;
+ ssize_t ret;
+
+ if (ts_fd == -1)
+ return -1;
+
+ for (;;) {
+ errno = 0;
+ ret = read(ts_fd, &ev, sizeof(ev));
+ if (ret != sizeof(ev)) {
+ if (errno == EAGAIN)
+ break;
+ perror("ts read");
+ return -1;
+ }
+ if (ev.type == EV_ABS) {
+ switch (ev.code) {
+ case ABS_X: raw_x = ev.value; break;
+ case ABS_Y: raw_y = ev.value; break;
+ case ABS_PRESSURE: raw_p = ev.value; break;
+ }
+ }
+ }
+
+ *pressure = raw_p;
+ *x1024 = (tsc[0] * raw_x + tsc[1] * raw_y + tsc[2]) / tsc[6];
+ *y1024 = (tsc[3] * raw_x + tsc[4] * raw_y + tsc[5]) / tsc[6];
+
+ host_video_normalize_ts(x1024, y1024);
+
+ return 0;
+}