From e0eb1015bf30fd0913db571b000ba6a83ea22660 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Thu, 29 Jul 2010 15:54:31 +0200 Subject: Split the code into recognizer and inserter Refactor code to make more room for gesture recognition. Also simplify public headers. Signed-off-by: Henrik Rydberg --- src/Makefile.am | 5 +- src/evbuf.h | 64 +++++++++++++++++ src/gebuf.h | 5 ++ src/gesture-client.c | 113 ------------------------------ src/gesture-dev.c | 105 ---------------------------- src/grail-api.c | 133 ++++++++++++----------------------- src/grail-inserter.c | 184 +++++++++++++++++++++++++++++++++++++++++++++++++ src/grail-inserter.h | 69 +++++++++++++++++++ src/grail-recognizer.c | 97 ++++++++++++++++++++++++++ src/grail-recognizer.h | 34 +++++++++ 10 files changed, 498 insertions(+), 311 deletions(-) create mode 100644 src/evbuf.h delete mode 100644 src/gesture-client.c delete mode 100644 src/gesture-dev.c create mode 100644 src/grail-inserter.c create mode 100644 src/grail-inserter.h create mode 100644 src/grail-recognizer.c create mode 100644 src/grail-recognizer.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 50a5afd..67f930a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,8 +7,8 @@ libgrail_la_LDFLAGS = \ libgrail_la_SOURCES = \ touch-dev.c \ touch-engine.c \ - gesture-client.c \ - gesture-dev.c \ + grail-inserter.c \ + grail-recognizer.c \ grail-api.c AM_CFLAGS = $(CWARNFLAGS) -std=c99 @@ -18,5 +18,4 @@ INCLUDES = -I$(top_srcdir)/include/ libgrailincludedir = $(includedir) libgrailinclude_HEADERS = \ $(top_srcdir)/include/grail-touch.h \ - $(top_srcdir)/include/grail-gesture.h \ $(top_srcdir)/include/grail.h diff --git a/src/evbuf.h b/src/evbuf.h new file mode 100644 index 0000000..dc5ad1e --- /dev/null +++ b/src/evbuf.h @@ -0,0 +1,64 @@ +/***************************************************************************** + * + * mtdev - Multitouch Protocol Translation Library (MIT license) + * + * Copyright (C) 2010 Henrik Rydberg + * Copyright (C) 2010 Canonical Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + ****************************************************************************/ + +#ifndef GRAIL_EVBUF_H +#define GRAIL_EVBUF_H + +#define DIM_EVENTS 512 + +struct evbuf { + int head; + int tail; + struct input_event buffer[DIM_EVENTS]; +}; + +static inline void evbuf_clear(struct evbuf *evbuf) +{ + evbuf->head = evbuf->tail = 0; +} + +static inline int evbuf_empty(const struct evbuf *evbuf) +{ + return evbuf->head == evbuf->tail; +} + +static inline void evbuf_put(struct evbuf *evbuf, + const struct input_event *ev) +{ + evbuf->buffer[evbuf->head++] = *ev; + evbuf->head &= DIM_EVENTS - 1; +} + +static inline void evbuf_get(struct evbuf *evbuf, + struct input_event *ev) +{ + *ev = evbuf->buffer[evbuf->tail++]; + evbuf->tail &= DIM_EVENTS - 1; +} + +#endif diff --git a/src/gebuf.h b/src/gebuf.h index e92431a..37e4a5c 100644 --- a/src/gebuf.h +++ b/src/gebuf.h @@ -29,6 +29,11 @@ #define DIM_GESTURE_EVENTS 512 +struct gesture_event { + touch_time_t time; + grail_prop_t prop[DIM_GRAIL_PROP]; +}; + struct gebuf { int head; int tail; diff --git a/src/gesture-client.c b/src/gesture-client.c deleted file mode 100644 index 91f1530..0000000 --- a/src/gesture-client.c +++ /dev/null @@ -1,113 +0,0 @@ -/***************************************************************************** - * - * grail - Gesture Recognition And Instantiation Library - * - * Copyright (C) 2010 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 . - * - * Authors: - * Henrik Rydberg - * - ****************************************************************************/ - -#include -#include -#include -#include -#include "gebuf.h" - -#define GESTURE_GAP (1000 * 60 * 60) - -struct client_impl { - enum gesture_state state; - touch_time_t begin, end; - struct gebuf buf; -}; - -int client_init(struct gesture_client *client, void *priv) -{ - struct client_impl *x; - memset(client, 0, sizeof(*client)); - x = calloc(DIM_GESTURE_TYPE, sizeof(*x)); - if (!x) - return -ENOMEM; - client->impl = x; - client->priv = priv; - return 0; -} - -void client_flag(struct gesture_client *client, - const struct gesture_flag *flag) -{ - if (client->priority[flag->type]) { - struct client_impl *x = &client->impl[flag->type]; - switch(flag->state) { - case GS_BEGIN: - x->begin = flag->time; - x->end = flag->time + GESTURE_GAP; - break; - case GS_CANCEL: - x->end = x->begin; - break; - case GS_END: - x->end = flag->time; - break; - } - x->state = flag->state; - } -} - -void client_event(struct gesture_client *client, - const struct gesture_event *ev) -{ - if (client->priority[ev->type]) { - struct client_impl *x = &client->impl[ev->type]; - gebuf_put(&x->buf, ev); - } -} - -void client_sync(struct gesture_client *client) -{ - struct gesture_event ev; - struct client_impl *x = client->impl; - int i, prio = 0; - for (i = 0; i < DIM_GESTURE_TYPE; i++) { - if (x[i].begin != x[i].end) { - if (client->priority[i] > prio) - prio = client->priority[i]; - } - } - for (i = 0; i < DIM_GESTURE_TYPE; i++) { - if (client->priority[i] < prio) { - gebuf_clear(&x[i].buf); - } else if (prio && client->gesture) { - if (x[i].state == GS_END) { - while (!gebuf_empty(&x[i].buf)) { - gebuf_get(&x[i].buf, &ev); - client->gesture(client, &ev); - } - } - } - } - for (i = 0; i < DIM_GESTURE_TYPE; i++) - if (x[i].state == GS_END) - x[i].begin = x[i].end; -} - -void client_destroy(struct gesture_client *client) -{ - free(client->impl); - memset(client, 0, sizeof(*client)); -} diff --git a/src/gesture-dev.c b/src/gesture-dev.c deleted file mode 100644 index 46c77d7..0000000 --- a/src/gesture-dev.c +++ /dev/null @@ -1,105 +0,0 @@ -/***************************************************************************** - * - * grail - Gesture Recognition And Instantiation Library - * - * Copyright (C) 2010 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 . - * - * Authors: - * Henrik Rydberg - * - ****************************************************************************/ - -#include -#include -#include -#include - -#define DIM_CLIENT 32 - -struct gedev_impl { - int nclient; - struct gesture_client *client[DIM_CLIENT]; -}; - -int gedev_init(struct gesture_dev *dev, void *priv) -{ - struct gedev_impl *x; - x = calloc(1, sizeof(*x)); - if (!x) - return -ENOMEM; - dev->impl = x; - dev->priv = priv; - return 0; -} - -int gedev_attach_client(struct gesture_dev *dev, - struct gesture_client *client) -{ - struct gedev_impl *x = dev->impl; - if (x->nclient >= DIM_CLIENT) - return -ENOMEM; - x->client[x->nclient++] = client; - return 0; -} - -void gedev_flag(struct gesture_dev *dev, - const struct gesture_flag *flag) -{ - struct gedev_impl *x = dev->impl; - int i; - for (i = 0; i < x->nclient; i++) - client_flag(x->client[i], flag); -} - -void gedev_event(struct gesture_dev *dev, - const struct gesture_event *ev) -{ - struct gedev_impl *x = dev->impl; - int i; - for (i = 0; i < x->nclient; i++) - client_event(x->client[i], ev); -} - -void gedev_sync(struct gesture_dev *dev) -{ - struct gedev_impl *x = dev->impl; - int i; - for (i = 0; i < x->nclient; i++) - client_sync(x->client[i]); -} - -void gedev_detach_client(struct gesture_dev *dev, - struct gesture_client *client) -{ - struct gedev_impl *x = dev->impl; - int i, j; - for (i = 0; i < x->nclient; i++) - if (x->client[i] == client) - break; - if (i >= x->nclient) - return; - x->nclient--; - for (j = i; j < x->nclient; j++) - x->client[j] = x->client[j + 1]; - for (j = x->nclient; j < DIM_CLIENT; j++) - x->client[j] = 0; -} - -void gedev_destroy(struct gesture_dev *dev) -{ - free(dev->impl); - memset(dev, 0, sizeof(*dev)); -} diff --git a/src/grail-api.c b/src/grail-api.c index dbe4977..19b66aa 100644 --- a/src/grail-api.c +++ b/src/grail-api.c @@ -1,4 +1,5 @@ -#include +#include "grail-inserter.h" +#include "grail-recognizer.h" #include #include #include @@ -6,93 +7,46 @@ #include #include #include +#include "evbuf.h" struct grail_impl { struct touch_dev dev; struct touch_engine engine; - struct touch_frame frame; - struct gesture_dev gedev; - struct gesture_client client; - touch_time_t last_scroll; + struct evbuf evbuf; }; -static void gh_handle(struct grail_impl *gh, - const struct touch_frame *frame, - touch_time_t time) -{ -#if 0 - struct gesture_flag flag; - struct grail_event ev; - if (frame->ntouch == 1 || frame->ntouch == 2) { - flag.type = GE_MOVE; - flag.time = time; - flag.state = GS_BEGIN; - gedev_flag(&gh->dev, &flag); - - ev.type = GE_MOVE; - ev.time = time; - ev.prop[0] = - frame->touch[0].prop[TP_POS_X] - - gh->frame.touch[0].prop[TP_POS_X]; - ev.prop[1] = - frame->touch[0].prop[TP_POS_Y] - - gh->frame.touch[0].prop[TP_POS_Y]; - if (ev.prop[0] || ev.prop[1]) - gedev_event(&gh->dev, &ev); - - flag.time = time + 1; - flag.state = GS_END; - gedev_flag(&gh->dev, &flag); - } - if (frame->ntouch == 2) { - flag.type = GE_VSCROLL; - flag.time = time; - flag.state = GS_BEGIN; - gedev_flag(&gh->dev, &flag); - - ev.type = GE_VSCROLL; - ev.time = time; - ev.prop[0] = - frame->touch[0].prop[TP_POS_Y] - - gh->frame.touch[0].prop[TP_POS_Y]; - - if (ev.prop[0]) - gedev_event(&gh->dev, &ev); - - if (time > gh->last_scroll + 100) { - flag.time = time + 1; - flag.state = GS_END; - gedev_flag(&gh->dev, &flag); - gh->last_scroll = time; - } - } else { - flag.type = GE_VSCROLL; - flag.time = time; - flag.state = GS_CANCEL; - gedev_flag(&gh->dev, &flag); - } - gedev_sync(&gh->dev); - gh->frame = *frame; -#endif -} - static void tp_event(struct touch_engine *engine, const struct input_event *ev) { struct grail *ge = engine->priv; - if (ge->event) - ge->event(ge, ev); + struct grail_impl *x = ge->impl; + evbuf_put(&x->evbuf, ev); } static void tp_sync(struct touch_engine *engine, const struct input_event *syn) { + struct input_event ev; struct grail *ge = engine->priv; -#if 0 + struct grail_impl *x = ge->impl; struct touch_frame *frame = &engine->frame; - gh_handle(ge, frame, frame->time); -#endif - if (ge->event) + grail_mask_t filtered[DIM_EV_TYPE_BYTES]; + int nevent = 0; + gin_frame_begin(ge, frame->time); + gru_recognize(ge, frame); + gin_frame_end(ge, filtered); + if (!ge->event) { + evbuf_clear(&x->evbuf); + return; + } + while (!evbuf_empty(&x->evbuf)) { + evbuf_get(&x->evbuf, &ev); + if (grail_get_mask(filtered, ev.type)) + continue; + ge->event(ge, &ev); + nevent++; + } + if (nevent) ge->event(ge, syn); } @@ -103,49 +57,48 @@ int grail_open(struct grail *ge, int fd) x = calloc(1, sizeof(*x)); if (!x) return -ENOMEM; + ret = gin_init(ge); + if (ret) + goto freemem; + ret = gru_init(ge); + if (ret) + goto freedev; ret = touch_dev_open(&x->dev, fd); if (ret) - goto error; + goto freegru; touch_engine_init(&x->engine, tp_event, tp_sync, ge); touch_engine_attach(&x->engine, &x->dev); ge->impl = x; return 0; - error: + freegru: + gru_destroy(ge); + freedev: + gin_destroy(ge); + freemem: free(x); return ret; } -int grail_idle(struct grail *ge, int fd, int ms) -{ - struct grail_impl *x = ge->impl; - return touch_dev_idle(&x->dev, fd, ms); -} - -int grail_pull(struct grail *ge, int fd) -{ - struct grail_impl *x = ge->impl; - return touch_dev_pull(&x->dev, fd); -} - void grail_close(struct grail *ge, int fd) { struct grail_impl *x = ge->impl; touch_engine_detach(&x->engine, &x->dev); touch_dev_close(&x->dev, fd); + gru_destroy(ge); + gin_destroy(ge); free(ge->impl); ge->impl = 0; } -void grail_add_client(struct grail *ge, grail_clientid_t id, - const int *types, const int *priority, int ntypes) +int grail_idle(struct grail *ge, int fd, int ms) { struct grail_impl *x = ge->impl; - //gedev_attach_client(&x->dev, &x->client); + return touch_dev_idle(&x->dev, fd, ms); } -void grail_remove_client(struct grail *ge, grail_clientid_t id) +int grail_pull(struct grail *ge, int fd) { struct grail_impl *x = ge->impl; - //gedev_detach_client(&x->dev, &x->client); + return touch_dev_pull(&x->dev, fd); } diff --git a/src/grail-inserter.c b/src/grail-inserter.c new file mode 100644 index 0000000..b357e29 --- /dev/null +++ b/src/grail-inserter.c @@ -0,0 +1,184 @@ +/***************************************************************************** + * + * grail - Gesture Recognition And Instantiation Library + * + * Copyright (C) 2010 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 . + * + * Authors: + * Henrik Rydberg + * + ****************************************************************************/ + +#include "grail-inserter.h" +#include +#include +#include + +static const int MAX_GESTURE_ID = 0xfff; + +int gin_init(struct grail *ge) +{ + struct gesture_inserter *x; + x = calloc(1, sizeof(*x)); + if (!x) + return -ENOMEM; + ge->gin = x; + return 0; +} + +void gin_destroy(struct grail *ge) +{ + free(ge->gin); + ge->gin = NULL; +} + +void gin_frame_begin(struct grail *ge, grail_time_t time) +{ + struct gesture_inserter *gin = ge->gin; + gin->time = time; +} + +static void send_event(struct grail *ge, struct slot_state *s, + const struct gesture_event *ev) +{ + struct grail_event gev; + int i; + if (!ge->gesture) + return; + gev.type = s->type; + gev.id = s->id; + gev.status = s->status; + gev.pos.x = s->x; + gev.pos.y = s->x; + gev.time = ev->time; + memcpy(gev.prop, ev->prop, DIM_GRAIL_PROP * sizeof(gev.prop[0])); + for (i = 0; i < s->nclient; i++) { + gev.client_id = s->client_id[i]; + ge->gesture(ge, &gev); + } +} + +void gin_frame_end(struct grail *ge, grail_mask_t *filtered) +{ + struct gesture_inserter *gin = ge->gin; + struct gesture_event ev; + grail_mask_t span[DIM_SLOT_BYTES]; + int i, slot, spanned = 0; + memset(filtered, 0, DIM_EV_TYPE_BYTES); + memset(span, 0, sizeof(span)); + for (slot = 0; slot < DIM_SLOT; slot++) { + struct slot_state *s = &gin->state[slot]; + if (!grail_get_mask(gin->used, slot)) + continue; + if (!s->nclient) + gin_gesture_discard(gin, slot); + } + for (slot = 0; slot < DIM_SLOT; slot++) { + struct slot_state *s = &gin->state[slot]; + if (!grail_get_mask(gin->used, slot)) + continue; + for (i = 0; i < DIM_SLOT_BYTES; i++) { + span[i] |= s->span[i]; + spanned += !!span[i]; + } + while (!gebuf_empty(&s->buf)) { + gebuf_get(&s->buf, &ev); + send_event(ge, s, &ev); + } + if (s->status == GRAIL_STATUS_BEGIN) + s->status = GRAIL_STATUS_UPDATE; + } + if (spanned) + grail_set_mask(filtered, EV_ABS); +} + +int gin_gesture_begin(struct grail *ge, const struct touch_frame *frame, + int type, int x, int y, const grail_mask_t *span) +{ + struct grail_client_info info[DIM_CLIENT]; + struct grail_coord coord[DIM_CLIENT]; + struct gesture_inserter *gin = ge->gin; + struct slot_state *s; + int i, slot, nc, ncoord = 0; + for (slot = 0; slot < DIM_SLOT; slot++) + if (!grail_get_mask(gin->used, slot)) + break; + if (slot >= DIM_SLOT) + return -1; + s = &gin->state[slot]; + s->type = type; + s->id = gin->gestureid++ & MAX_GESTURE_ID; + s->status = GRAIL_STATUS_BEGIN; + s->x = x; + s->y = y; + s->nclient = 0; + if (ge->get_clients) { + grail_mask_t tps[DIM_GRAIL_TYPE_BYTES]; + memset(tps, 0, sizeof(tps)); + grail_set_mask(tps, type); + for (i = 0; i < DIM_SLOT; i++) { + const struct touch *t = &frame->touch[i]; + if (!grail_get_mask(span, i)) + continue; + coord[ncoord].x = t->prop[TP_POS_X]; + coord[ncoord].y = t->prop[TP_POS_Y]; + ncoord++; + } + nc = ge->get_clients(ge, info, DIM_CLIENT, coord, ncoord, tps); + for (i = 0; i < nc; i++) { + if (!grail_get_mask(info[i].mask, type)) + continue; + s->client_id[s->nclient] = info[i].id; + s->nclient++; + } + } + memcpy(s->span, span, sizeof(s->span)); + gebuf_clear(&s->buf); + grail_set_mask(gin->used, slot); + return slot; +} + +void gin_gesture_end(struct gesture_inserter *gin, int slot) +{ + struct slot_state *s = &gin->state[slot]; + s->status = GRAIL_STATUS_END; + grail_clear_mask(gin->used, slot); +} + +void gin_gesture_discard(struct gesture_inserter *gin, int slot) +{ + struct slot_state *s = &gin->state[slot]; + gebuf_clear(&s->buf); + gin_gesture_end(gin, slot); +} + +void gin_gesture_end_all(struct gesture_inserter *gin) +{ + int slot; + for (slot = 0; slot < DIM_SLOT; slot++) + gin_gesture_end(gin, slot); +} + +void gin_gesture_event(struct gesture_inserter *gin, int slot, + const grail_prop_t *prop) +{ + struct slot_state *s = &gin->state[slot]; + struct gesture_event ev; + ev.time = gin->time; + memcpy(ev.prop, prop, sizeof(ev.prop)); + gebuf_put(&s->buf, &ev); +} + diff --git a/src/grail-inserter.h b/src/grail-inserter.h new file mode 100644 index 0000000..1e02d5f --- /dev/null +++ b/src/grail-inserter.h @@ -0,0 +1,69 @@ +/***************************************************************************** + * + * grail - Gesture Recognition And Instantiation Library + * + * Copyright (C) 2010 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 . + * + * Authors: + * Henrik Rydberg + * + ****************************************************************************/ + +#ifndef _GRAIL_GESTURE_H +#define _GRAIL_GESTURE_H + +#include +#include +#include "gebuf.h" + +#define DIM_EV_TYPE EV_CNT +#define DIM_EV_TYPE_BYTES (DIM_EV_TYPE >> 3) + +#define DIM_SLOT 32 +#define DIM_SLOT_BYTES (DIM_SLOT >> 3) + +#define DIM_CLIENT 32 + +struct slot_state { + int type, id, status, x, y, nclient; + struct grail_client_id client_id[DIM_CLIENT]; + grail_mask_t span[DIM_SLOT_BYTES]; + struct gebuf buf; +}; + +struct gesture_inserter { + struct slot_state state[DIM_SLOT]; + grail_mask_t used[DIM_SLOT_BYTES]; + grail_time_t time; + int gestureid; +}; + +int gin_init(struct grail *ge); +void gin_destroy(struct grail *ge); + +void gin_frame_begin(struct grail *ge, grail_time_t time); +void gin_frame_end(struct grail *ge, grail_mask_t *filtered); + +int gin_gesture_begin(struct grail *ge, const struct touch_frame *frame, + int type, int x, int y, const grail_mask_t *span); +void gin_gesture_discard(struct gesture_inserter *gin, int slot); +void gin_gesture_end(struct gesture_inserter *gin, int slot); +void gin_gesture_end_all(struct gesture_inserter *gin); + +void gin_gesture_event(struct gesture_inserter *gin, int slot, + const grail_prop_t *prop); + +#endif diff --git a/src/grail-recognizer.c b/src/grail-recognizer.c new file mode 100644 index 0000000..9ede059 --- /dev/null +++ b/src/grail-recognizer.c @@ -0,0 +1,97 @@ +/***************************************************************************** + * + * grail - Gesture Recognition And Instantiation Library + * + * Copyright (C) 2010 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 . + * + * Authors: + * Henrik Rydberg + * + ****************************************************************************/ + +#include "grail-inserter.h" +#include +#include +#include + +struct gesture_recognizer { + struct touch_frame frame; + touch_time_t scroll_time; + int scroll_active; + int scroll_gslot; + int scroll_slots[2]; +}; + +int gru_init(struct grail *ge) +{ + ge->gru = calloc(1, sizeof(struct gesture_recognizer)); + if (!ge->gru) + return -ENOMEM; + return 0; +} + +void gru_destroy(struct grail *ge) +{ + free(ge->gru); + ge->gru = NULL; +} + +void gru_recognize(struct grail *ge, const struct touch_frame *frame) +{ + struct gesture_inserter *gin = ge->gin; + struct gesture_recognizer *gru = ge->gru; + grail_prop_t prop[DIM_GRAIL_PROP]; + grail_mask_t span[16]; + int slot, nslot, x, y, dx, dy; + if (!gru) + return; + if (frame->ncreate || frame->ndestroy) { + gin_gesture_end_all(gin); + gru->scroll_active = 0; + return; + } + if (frame->ntouch != 2) + return; + memset(span, 0, sizeof(span)); + nslot = 0; + x = y = dx = dy = 0; + for (slot = frame->slot; slot >= 0; slot = next_slot(frame, slot)) { + struct touch *ot = &gru->frame.touch[slot]; + const struct touch *t = &frame->touch[slot]; + gru->scroll_slots[nslot] = slot; + grail_set_mask(span, slot); + x += t->prop[TP_POS_X]; + y += t->prop[TP_POS_Y]; + dx += t->prop[TP_POS_X] - ot->prop[TP_POS_X]; + dy += t->prop[TP_POS_Y] - ot->prop[TP_POS_Y]; + nslot++; + } + x /= nslot; + y /= nslot; + dx /= nslot; + dy /= nslot; + if (!dy) + return; + if (!gru->scroll_active) { + gru->scroll_gslot = gin_gesture_begin(ge, frame, + GRAIL_TYPE_VSCROLL, + x, y, span); + gru->scroll_active = 1; + } + prop[0] = dy; + gin_gesture_event(gin, gru->scroll_gslot, prop); + gru->frame = *frame; +} diff --git a/src/grail-recognizer.h b/src/grail-recognizer.h new file mode 100644 index 0000000..8c47162 --- /dev/null +++ b/src/grail-recognizer.h @@ -0,0 +1,34 @@ +/***************************************************************************** + * + * grail - Gesture Recognition And Instantiation Library + * + * Copyright (C) 2010 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 . + * + * Authors: + * Henrik Rydberg + * + ****************************************************************************/ + +#ifndef _GRAIL_RECOGNIZER_H +#define _GRAIL_RECOGNIZER_H + +#include "grail-inserter.h" + +int gru_init(struct grail *ge); +void gru_recognize(struct grail *ge, const struct touch_frame *frame); +void gru_destroy(struct grail *ge); + +#endif -- cgit v1.2.3