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 --- .gitignore | 1 - include/grail-gesture.h | 90 ----------------------- include/grail.h | 80 ++++++++++++++++----- 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 +++++++++ test/Makefile.am | 5 +- test/evlink.c | 96 ------------------------- test/grail.c | 154 +++++++++------------------------------- 16 files changed, 597 insertions(+), 638 deletions(-) delete mode 100644 include/grail-gesture.h 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 delete mode 100644 test/evlink.c diff --git a/.gitignore b/.gitignore index deec709..904dc45 100644 --- a/.gitignore +++ b/.gitignore @@ -78,7 +78,6 @@ core # test/touch test/grail -test/evlink patches .bzr .bzrignore diff --git a/include/grail-gesture.h b/include/grail-gesture.h deleted file mode 100644 index fe62b95..0000000 --- a/include/grail-gesture.h +++ /dev/null @@ -1,90 +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 - * - ****************************************************************************/ - -#ifndef _GRAIL_GESTURE_H -#define _GRAIL_GESTURE_H - -#include - -#define DIM_GESTURE_PROP 8 -typedef int gesture_prop_t; - -enum gesture_type { - GE_MOVE, - GE_VSCROLL, - DIM_GESTURE_TYPE -}; - -enum gesture_state { - GS_CANCEL, - GS_BEGIN, - GS_END -}; - -struct gesture_flag { - enum gesture_type type; - touch_time_t time; - enum gesture_state state; -}; - -struct gesture_event { - enum gesture_type type; - touch_time_t time; - gesture_prop_t prop[DIM_GESTURE_PROP]; -}; - -struct gesture_dev { - struct gedev_impl *impl; - void *priv; -}; - -struct gesture_client { - void (*gesture)(struct gesture_client *client, - const struct gesture_event *ev); - int priority[DIM_GESTURE_TYPE]; - struct client_impl *impl; - void *priv; -}; - -int client_init(struct gesture_client *client, void *priv); -void client_flag(struct gesture_client *client, - const struct gesture_flag *flag); -void client_event(struct gesture_client *client, - const struct gesture_event *ev); -void client_sync(struct gesture_client *client); -void client_destroy(struct gesture_client *client); - -int gedev_init(struct gesture_dev *dev, void *priv); -int gedev_attach_client(struct gesture_dev *dev, - struct gesture_client *client); - -void gedev_flag(struct gesture_dev *dev, const struct gesture_flag *flag); -void gedev_event(struct gesture_dev *dev, const struct gesture_event *ev); -void gedev_sync(struct gesture_dev *dev); - -void gedev_detach_client(struct gesture_dev *dev, - struct gesture_client *client); -void gedev_destroy(struct gesture_dev *dev); - -#endif diff --git a/include/grail.h b/include/grail.h index c390e22..de202b5 100644 --- a/include/grail.h +++ b/include/grail.h @@ -27,31 +27,82 @@ #include -#define DIM_GRAIL_PROP 8 -#define DIM_GRAIL_TYPE 64 +#define GRAIL_STATUS_BEGIN 0 +#define GRAIL_STATUS_UPDATE 1 +#define GRAIL_STATUS_END 2 +#define DIM_GRAIL_TYPE 64 +#define DIM_GRAIL_TYPE_BYTES (DIM_GRAIL_TYPE >> 3) + +#define GRAIL_TYPE_MOVE 0 +#define GRAIL_TYPE_VSCROLL 1 +#define GRAIL_TYPE_HSCROLL 2 +#define GRAIL_TYPE_SCROLL 3 + +#define DIM_GRAIL_PROP 8 + +static const int GRAIL_NPROP[] = { + 2, /* MOVE */ + 1, /* VSCROLL */ + 1, /* HSCROLL */ + 2, /* SCROLL */ +}; + +typedef unsigned char grail_mask_t; typedef int grail_prop_t; typedef __u64 grail_time_t; -typedef unsigned long grail_clientid_t; + +static inline void grail_set_mask(grail_mask_t *mask, int i) +{ + mask[i >> 3] |= (1 << (i & 7)); +} + +static inline void grail_clear_mask(grail_mask_t *mask, int i) +{ + mask[i >> 3] &= ~(1 << (i & 7)); +} + +static inline int grail_get_mask(const grail_mask_t *mask, int i) +{ + return (mask[i >> 3] >> (i & 7)) & 1; +} + +struct grail_coord { + int x, y; +}; + +struct grail_client_id { + int client; + int root, event, child; +}; + +struct grail_client_info { + struct grail_client_id id; + grail_mask_t mask[DIM_GRAIL_TYPE_BYTES]; +}; struct grail_event { - int type; - int id; - int x; - int y; - int status; - grail_time_t time; - grail_prop_t prop[DIM_GRAIL_PROP]; + int type; + int id; + int status; + struct grail_coord pos; + struct grail_client_id client_id; + grail_time_t time; + grail_prop_t prop[DIM_GRAIL_PROP]; }; struct grail { - int (*get_clients)(int x, int y, int screen, - grail_clientid_t *clients, int nclients); + int (*get_clients)(struct grail *ge, + struct grail_client_info *client, int max_clients, + const struct grail_coord *coords, int num_coords, + const grail_mask_t *types); void (*event)(struct grail *ge, const struct input_event *ev); void (*gesture)(struct grail *ge, const struct grail_event *ev); struct grail_impl *impl; + struct gesture_inserter *gin; + struct gesture_recognizer *gru; void *priv; }; @@ -60,9 +111,4 @@ int grail_idle(struct grail *ge, int fd, int ms); int grail_pull(struct grail *ge, int fd); void grail_close(struct grail *ge, int fd); -void grail_add_client(struct grail *ge, grail_clientid_t id, - const int *types, const int *priority, int ntypes); -void grail_remove_client(struct grail *ge, grail_clientid_t id); - - #endif 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 diff --git a/test/Makefile.am b/test/Makefile.am index a24f615..c8404f8 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,4 +1,4 @@ -noinst_PROGRAMS = touch grail evlink +noinst_PROGRAMS = touch grail INCLUDES=-I$(top_srcdir)/include/ @@ -7,6 +7,3 @@ touch_LDFLAGS = -L$(top_builddir)/src/.libs/ -lgrail -lmtdev grail_SOURCES = grail.c grail_LDFLAGS = -L$(top_builddir)/src/.libs/ -lgrail -lmtdev - -evlink_SOURCES = evlink.c -evlink_LDFLAGS = -L$(top_builddir)/src/.libs/ -lgrail -lmtdev diff --git a/test/evlink.c b/test/evlink.c deleted file mode 100644 index bef5e22..0000000 --- a/test/evlink.c +++ /dev/null @@ -1,96 +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 - -static int tp_get_clients(int x, int y, int screen, - grail_clientid_t *clients, int nclients) -{ - return 0; -} - -static void tp_event(struct grail *ge, const struct input_event *ev) -{ - fprintf(stderr, "event %d %d %d\n", ev->type, ev->code, ev->value); -} - -static void tp_gesture(struct grail *ge, const struct grail_event *ev) -{ - fprintf(stderr, "gesture %d %d\n", ev->type, ev->id); -} - -static void loop_device(struct grail *ge, int fd) -{ - while (!grail_idle(ge, fd, 5000)) - grail_pull(ge, fd); -} - -#if 0 -void grail_add_client(struct grail *ge, grail_clientid_t id, - const int *types, const int *priority, int ntypes); -void grail_remove_client(struct grail *ge, grail_clientid_t id); - -#endif - - -int main(int argc, char *argv[]) -{ - struct grail ge; - int fd; - - if (argc < 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); - return -1; - } - - memset(&ge, 0, sizeof(ge)); - ge.get_clients = tp_get_clients; - ge.event = tp_event; - ge.gesture = tp_gesture; - - 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 (grail_open(&ge, fd)) { - fprintf(stderr, "error: could not open touch device\n"); - return -1; - } - loop_device(&ge, fd); - grail_close(&ge, fd); - - ioctl(fd, EVIOCGRAB, 0); - close(fd); - return 0; -} diff --git a/test/grail.c b/test/grail.c index e22ffd8..85c5bd4 100644 --- a/test/grail.c +++ b/test/grail.c @@ -22,146 +22,60 @@ * ****************************************************************************/ -#include +#include #include #include #include #include -struct gesture_handler { - struct touch_frame frame; - struct gesture_client client; - struct gesture_dev dev; - touch_time_t last_scroll; -}; - -static void gh_gesture(struct gesture_client *client, - const struct gesture_event *ev) -{ - int i; - fprintf(stderr, "gesture %d %lld\n", - ev->type, ev->time); - switch (ev->type) { - case GE_MOVE: - fprintf(stderr, " %d\n", ev->prop[0]); - fprintf(stderr, " %d\n", ev->prop[1]); - break; - case GE_VSCROLL: - fprintf(stderr, " %d\n", ev->prop[0]); - break; - } -} - -static void gh_init(struct gesture_handler *gh) +static int tp_get_clients(struct grail *ge, + struct grail_client_info *clients, int max_clients, + const struct grail_coord *coords, int num_coords, + const grail_mask_t *types) { - memset(gh, 0, sizeof(*gh)); - gedev_init(&gh->dev, 0); - client_init(&gh->client, gh); - gh->client.gesture = gh_gesture; - gh->client.priority[GE_MOVE] = 1; - gh->client.priority[GE_VSCROLL] = 2; - gedev_attach_client(&gh->dev, &gh->client); + memset(&clients[0], 0, sizeof(clients[0])); + clients[0].id.client = 345; + clients[0].id.root = 1; + clients[0].id.event = 2; + clients[0].id.child = 3; + clients[0].mask[0] = 3; + return 1; } -static void gh_handle(struct gesture_handler *gh, - const struct touch_frame *frame, - touch_time_t time) +static void tp_event(struct grail *ge, const struct input_event *ev) { - struct gesture_flag flag; - struct gesture_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; + fprintf(stderr, "event %d %d %d\n", ev->type, ev->code, ev->value); } -static void gh_destroy(struct gesture_handler *gh) +static void tp_gesture(struct grail *ge, const struct grail_event *ev) { - gedev_detach_client(&gh->dev, &gh->client); - client_destroy(&gh->client); - gedev_destroy(&gh->dev); + fprintf(stderr, "gesture %d %d %d %d: %d %d\n", + ev->type, ev->id, + ev->client_id.client, ev->client_id.event, + ev->prop[0], ev->prop[1]); } -static void tp_event(struct touch_engine *engine, - const struct input_event *ev) +static void loop_device(struct grail *ge, int fd) { -} - -static void tp_sync(struct touch_engine *engine, - const struct input_event *syn) -{ - struct gesture_handler *gh = engine->priv; - struct touch_frame *frame = &engine->frame; - gh_handle(gh, frame, frame->time); -} - -static void loop_device(struct touch_dev *dev, int fd) -{ - struct gesture_handler gh; - struct touch_engine engine; - gh_init(&gh); - touch_engine_init(&engine, tp_event, tp_sync, &gh); - touch_engine_attach(&engine, dev); - while (!touch_dev_idle(dev, fd, 5000)) - touch_dev_pull(dev, fd); - touch_engine_detach(&engine, dev); - gh_destroy(&gh); + while (!grail_idle(ge, fd, 5000)) + grail_pull(ge, fd); } int main(int argc, char *argv[]) { - struct touch_dev dev; + struct grail ge; int fd; + if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return -1; } + + memset(&ge, 0, sizeof(ge)); + ge.get_clients = tp_get_clients; + ge.event = tp_event; + ge.gesture = tp_gesture; + fd = open(argv[1], O_RDONLY | O_NONBLOCK); if (fd < 0) { fprintf(stderr, "error: could not open device\n"); @@ -171,12 +85,14 @@ int main(int argc, char *argv[]) fprintf(stderr, "error: could not grab the device\n"); return -1; } - if (touch_dev_open(&dev, fd)) { + + if (grail_open(&ge, fd)) { fprintf(stderr, "error: could not open touch device\n"); return -1; } - loop_device(&dev, fd); - touch_dev_close(&dev, fd); + loop_device(&ge, fd); + grail_close(&ge, fd); + ioctl(fd, EVIOCGRAB, 0); close(fd); return 0; -- cgit v1.2.3