/***************************************************************************** * * grail - Gesture Recognition And Instantiation Library * * Copyright (C) 2010 Canonical Ltd. * Copyright (C) 2010 Henrik Rydberg * * 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 . * ****************************************************************************/ #include "grail-inserter.h" #include "grail-recognizer.h" #include "grail-impl.h" #include #include #include #include #include #include #define DIM_FRAMES 100 #define FRAME_RATE 100 void grail_filter_abs_events(struct grail *ge, int usage) { struct grail_impl *x = ge->impl; x->filter_abs = usage; } static void init_impl(struct grail_impl *x) { int i; if (evemu_has_event(x->evemu, EV_ABS, ABS_X)) { x->emin_x = evemu_get_abs_minimum(x->evemu, ABS_X); x->emin_y = evemu_get_abs_minimum(x->evemu, ABS_Y); x->emax_x = evemu_get_abs_maximum(x->evemu, ABS_X); x->emax_y = evemu_get_abs_maximum(x->evemu, ABS_Y); } else { struct utouch_surface *s = utouch_frame_get_surface(x->fh); x->emin_x = s->min_x; x->emin_y = s->min_y; x->emax_x = s->max_x; x->emax_y = s->max_y; } } int grail_open(struct grail *ge, int fd) { struct grail_impl *x; int ret; x = calloc(1, sizeof(*x)); if (!x) return -ENOMEM; x->evemu = evemu_new(0); if (!x->evemu) { ret = -ENOMEM; goto freemem; } ret = evemu_extract(x->evemu, fd); if (ret) goto freemem; if (!utouch_frame_is_supported_mtdev(x->evemu)) { ret = -ENODEV; goto freemem; } x->mtdev = mtdev_new_open(fd); if (!x->mtdev) { ret = -ENOMEM; goto freemem; } x->fh = utouch_frame_new_engine(DIM_FRAMES, DIM_TOUCH, FRAME_RATE); if (!x->fh) { ret = -ENOMEM; goto freedev; } ret = utouch_frame_init_mtdev(x->fh, x->evemu); if (ret) goto freeframe; init_impl(x); ge->impl = x; ret = gin_init(ge); if (ret) goto freeframe; ret = gru_init(ge); if (ret) goto freegin; return 0; freegin: gin_destroy(ge); freeframe: utouch_frame_delete_engine(x->fh); freedev: mtdev_close_delete(x->mtdev); freemem: evemu_delete(x->evemu); free(x); ge->impl = 0; return ret; } void grail_close(struct grail *ge, int fd) { struct grail_impl *x = ge->impl; void *status; gru_destroy(ge); gin_destroy(ge); utouch_frame_delete_engine(x->fh); mtdev_close_delete(x->mtdev); evemu_delete(x->evemu); free(x); ge->impl = 0; } int grail_idle(struct grail *ge, int fd, int ms) { struct grail_impl *x = ge->impl; return mtdev_idle(x->mtdev, fd, ms); } void grail_get_units(const struct grail *ge, struct grail_coord *min, struct grail_coord *max) { struct utouch_surface *s = utouch_frame_get_surface(ge->impl->fh); min->x = s->min_x; min->y = s->min_y; max->x = s->max_x; max->y = s->max_y; } // legacy glue below static void evput(struct grail_impl *x, struct timeval time, unsigned type, unsigned code, int value) { struct input_event ev = { time, type, code, value }; evbuf_put(&x->evbuf, &ev); } static void report_down(struct grail_impl *impl) { if (impl->report_status) return; evput(impl, impl->report_time, EV_KEY, BTN_TOUCH, 1); evput(impl, impl->report_time, EV_ABS, ABS_X, impl->report_x); evput(impl, impl->report_time, EV_ABS, ABS_Y, impl->report_y); impl->report_status = 1; } static void report_up(struct grail_impl *impl, const struct input_event *syn) { if (!impl->report_status) return; evput(impl, syn->time, EV_KEY, BTN_TOUCH, 0); impl->report_status = 0; } static void set_pointer(struct grail_impl *impl) { struct utouch_surface *s = utouch_frame_get_surface(impl->fh); const struct utouch_frame *frame = impl->frame; int best_x, best_y, best_d = -1; int i; for (i = 0; i < frame->num_active; i++) { const struct utouch_contact *t = frame->active[i]; float u = (t->x - s->min_x) / (s->max_x - s->min_x); float v = (t->y - s->min_y) / (s->max_y - s->min_y); int x = impl->emin_x + u * (impl->emax_x - impl->emin_x); int y = impl->emin_y + v * (impl->emax_y - impl->emin_y); int d = abs(x - impl->pointer_x) + abs(y - impl->pointer_y); if (best_d < 0 || d < best_d) { best_x = x; best_y = y; best_d = d; } } if (best_d >= 0) { impl->pointer_x = best_x; impl->pointer_y = best_y; } } static void handle_abs_events(struct grail *ge, const struct input_event *syn) { static const int fm_mask = 0x03; struct grail_impl *impl = ge->impl; struct gesture_inserter *gin = ge->gin; struct gesture_recognizer *gru = ge->gru; struct move_model *move = &gru->move; struct tapping_model *tap = &gru->tapping; int used_move = grail_mask_count(gin->types, sizeof(gin->types)); int used_tap = grail_mask_get(gin->types, GRAIL_TYPE_TAP1); int pointer = move->ntouch == 1; int ismove = pointer && (move->active & fm_mask) && !used_move; int ishold = pointer && tap->active && !used_move; int istap = gru->tapping.tap == 1 && !used_tap; set_pointer(impl); if (!impl->pointer_status && pointer) { impl->report_x = impl->pointer_x; impl->report_y = impl->pointer_y; impl->report_time = syn->time; impl->pointer_status = 1; if (!used_move) report_down(impl); return; } if (ishold) return; if (istap) { report_down(impl); evput(impl, impl->report_time, EV_KEY, BTN_LEFT, 1); evput(impl, impl->report_time, EV_SYN, SYN_REPORT, 0); evput(impl, syn->time, EV_KEY, BTN_LEFT, 0); } else if (ismove) { report_down(impl); if (impl->report_x != impl->pointer_x) { evput(impl, syn->time, EV_ABS, ABS_X, impl->pointer_x); impl->report_x = impl->pointer_x; } if (impl->report_y != impl->pointer_y) { evput(impl, syn->time, EV_ABS, ABS_Y, impl->pointer_y); impl->report_y = impl->pointer_y; } } if (impl->pointer_status && !pointer) { impl->pointer_status = 0; report_up(impl, syn); } } static void report_frame(struct grail *ge, const struct utouch_frame *frame, const struct input_event *syn) { struct grail_impl *impl = ge->impl; int head = impl->evbuf.head; struct input_event iev; struct grail_event gev; ge->impl->frame = frame; gin_frame_begin(ge, frame); gru_recognize(ge, frame); gin_frame_end(ge, frame); if (!impl->filter_abs) handle_abs_events(ge, syn); if (impl->evbuf.head != head) evbuf_put(&impl->evbuf, syn); while (!grailbuf_empty(&impl->gbuf)) { grailbuf_get(&impl->gbuf, &gev); if (ge->gesture) ge->gesture(ge, &gev); } while (!evbuf_empty(&impl->evbuf)) { evbuf_get(&impl->evbuf, &iev); if (ge->event) ge->event(ge, &iev); } } static void grail_pump_mtdev(struct grail *ge, const struct input_event *ev) { struct grail_impl *impl = ge->impl; const struct utouch_frame *frame; if (ev->type == EV_SYN || ev->type == EV_ABS) { frame = utouch_frame_pump_mtdev(impl->fh, ev); if (frame) report_frame(ge, frame, ev); } else if (ev->type == EV_KEY) { switch (ev->code) { case BTN_TOUCH: case BTN_TOOL_FINGER: case BTN_TOOL_DOUBLETAP: case BTN_TOOL_TRIPLETAP: case BTN_TOOL_QUADTAP: break; default: evbuf_put(&impl->evbuf, ev); break; } } else { evbuf_put(&impl->evbuf, ev); } } int grail_pull(struct grail *ge, int fd) { struct grail_impl *impl = ge->impl; struct input_event ev; int ret, count = 0; while ((ret = mtdev_get(impl->mtdev, fd, &ev, 1)) > 0) { grail_pump_mtdev(ge, &ev); count++; } return count > 0 ? count : ret; }