From b76660d71e2ea77752025089bd71deffad64e325 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Mon, 26 Jul 2010 13:51:34 +0200 Subject: Test implementation of grail api This patch introduces the grail API, and a program to test it (evlink). Signed-off-by: Henrik Rydberg --- .gitignore | 1 + include/grail-gesture.h | 90 +++++++++++++++++++++++++++++ include/grail-touch.h | 8 ++- include/grail.h | 80 ++++++++++--------------- src/Makefile.am | 4 +- src/gesture-client.c | 2 +- src/gesture-dev.c | 2 +- src/grail-api.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++++ src/touch-dev.c | 6 +- src/touch-engine.c | 9 ++- test/Makefile.am | 5 +- test/evlink.c | 96 ++++++++++++++++++++++++++++++ test/grail.c | 7 ++- test/touch.c | 7 ++- 14 files changed, 398 insertions(+), 70 deletions(-) create mode 100644 include/grail-gesture.h create mode 100644 src/grail-api.c create mode 100644 test/evlink.c diff --git a/.gitignore b/.gitignore index 904dc45..deec709 100644 --- a/.gitignore +++ b/.gitignore @@ -78,6 +78,7 @@ core # test/touch test/grail +test/evlink patches .bzr .bzrignore diff --git a/include/grail-gesture.h b/include/grail-gesture.h new file mode 100644 index 0000000..fe62b95 --- /dev/null +++ b/include/grail-gesture.h @@ -0,0 +1,90 @@ +/***************************************************************************** + * + * 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-touch.h b/include/grail-touch.h index 9e8572b..e4bfd7b 100644 --- a/include/grail-touch.h +++ b/include/grail-touch.h @@ -67,7 +67,7 @@ struct touch_dev { void (*modify)(struct touch_dev *dev, int slot, const touch_prop_mask_t *mask, const touch_prop_t *prop); void (*destroy)(struct touch_dev *dev, int slot); - void (*sync)(struct touch_dev *dev, touch_time_t time); + void (*sync)(struct touch_dev *dev, const struct input_event *syn); struct touch_dev_impl *impl; void *priv; }; @@ -75,7 +75,8 @@ struct touch_dev { struct touch_engine { void (*event)(struct touch_engine *engine, const struct input_event *ev); - void (*sync)(struct touch_engine *engine); + void (*sync)(struct touch_engine *engine, + const struct input_event *syn); struct touch_frame frame; void *priv; }; @@ -98,7 +99,8 @@ void touch_dev_close(struct touch_dev *dev, int fd); void touch_engine_init(struct touch_engine *engine, void (*event)(struct touch_engine *engine, const struct input_event *ev), - void (*sync)(struct touch_engine *engine), + void (*sync)(struct touch_engine *engine, + const struct input_event *ev), void *priv); void touch_engine_attach(struct touch_engine *engine, struct touch_dev *dev); void touch_engine_detach(struct touch_engine *engine, struct touch_dev *dev); diff --git a/include/grail.h b/include/grail.h index 087cf0c..c390e22 100644 --- a/include/grail.h +++ b/include/grail.h @@ -25,66 +25,44 @@ #ifndef _GRAIL_H #define _GRAIL_H -#include +#include -#define DIM_GESTURE_PROP 8 -typedef int gesture_prop_t; +#define DIM_GRAIL_PROP 8 +#define DIM_GRAIL_TYPE 64 -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; -}; +typedef int grail_prop_t; +typedef __u64 grail_time_t; +typedef unsigned long grail_clientid_t; -struct gesture_event { - enum gesture_type type; - touch_time_t time; - gesture_prop_t prop[DIM_GESTURE_PROP]; +struct grail_event { + int type; + int id; + int x; + int y; + int status; + grail_time_t time; + grail_prop_t prop[DIM_GRAIL_PROP]; }; -struct gesture_dev { - struct gedev_impl *impl; +struct grail { + int (*get_clients)(int x, int y, int screen, + grail_clientid_t *clients, int nclients); + void (*event)(struct grail *ge, + const struct input_event *ev); + void (*gesture)(struct grail *ge, + const struct grail_event *ev); + struct grail_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); +int grail_open(struct grail *ge, int fd); +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 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 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); -void gedev_detach_client(struct gesture_dev *dev, - struct gesture_client *client); -void gedev_destroy(struct gesture_dev *dev); #endif diff --git a/src/Makefile.am b/src/Makefile.am index 7ccacae..50a5afd 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,7 +8,8 @@ libgrail_la_SOURCES = \ touch-dev.c \ touch-engine.c \ gesture-client.c \ - gesture-dev.c + gesture-dev.c \ + grail-api.c AM_CFLAGS = $(CWARNFLAGS) -std=c99 @@ -17,4 +18,5 @@ 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/gesture-client.c b/src/gesture-client.c index 78328b7..91f1530 100644 --- a/src/gesture-client.c +++ b/src/gesture-client.c @@ -22,7 +22,7 @@ * ****************************************************************************/ -#include +#include #include #include #include diff --git a/src/gesture-dev.c b/src/gesture-dev.c index 8dd11a9..46c77d7 100644 --- a/src/gesture-dev.c +++ b/src/gesture-dev.c @@ -22,7 +22,7 @@ * ****************************************************************************/ -#include +#include #include #include #include diff --git a/src/grail-api.c b/src/grail-api.c new file mode 100644 index 0000000..dbe4977 --- /dev/null +++ b/src/grail-api.c @@ -0,0 +1,151 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +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; +}; + +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); +} + +static void tp_sync(struct touch_engine *engine, + const struct input_event *syn) +{ + struct grail *ge = engine->priv; +#if 0 + struct touch_frame *frame = &engine->frame; + gh_handle(ge, frame, frame->time); +#endif + if (ge->event) + ge->event(ge, syn); +} + +int grail_open(struct grail *ge, int fd) +{ + struct grail_impl *x; + int ret; + x = calloc(1, sizeof(*x)); + if (!x) + return -ENOMEM; + ret = touch_dev_open(&x->dev, fd); + if (ret) + goto error; + touch_engine_init(&x->engine, tp_event, tp_sync, ge); + touch_engine_attach(&x->engine, &x->dev); + ge->impl = x; + return 0; + error: + 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); + 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) +{ + struct grail_impl *x = ge->impl; + //gedev_attach_client(&x->dev, &x->client); +} + +void grail_remove_client(struct grail *ge, grail_clientid_t id) +{ + struct grail_impl *x = ge->impl; + //gedev_detach_client(&x->dev, &x->client); +} + diff --git a/src/touch-dev.c b/src/touch-dev.c index 80cc82f..dc23baa 100644 --- a/src/touch-dev.c +++ b/src/touch-dev.c @@ -65,11 +65,9 @@ static void finish_touch(struct touch_dev *dev, static void finish_packet(struct touch_dev *dev, const struct input_event *syn) { - static const touch_time_t ms = 1000; - touch_time_t time = syn->time.tv_usec / ms + syn->time.tv_sec * ms; finish_touch(dev, dev->impl); if (dev->sync) - dev->sync(dev, time); + dev->sync(dev, syn); } static int handle_abs_event(struct touch_dev *dev, @@ -133,6 +131,8 @@ int touch_dev_open(struct touch_dev *dev, int fd) if (!x) return -ENOMEM; ret = mtdev_open(&x->mtdev, fd); + if (!ret && !x->mtdev.caps.has_mtdata) + ret = -ENODEV; if (ret) goto error; for (i = 0; i < DIM_TOUCH; i++) diff --git a/src/touch-engine.c b/src/touch-engine.c index 6174e0d..8e43faf 100644 --- a/src/touch-engine.c +++ b/src/touch-engine.c @@ -94,13 +94,15 @@ static void tp_destroy(struct touch_dev *dev, int slot) } } -static void tp_sync(struct touch_dev *dev, touch_time_t time) +static void tp_sync(struct touch_dev *dev, const struct input_event *syn) { + static const touch_time_t ms = 1000; struct touch_engine *engine = dev->priv; + touch_time_t time = syn->time.tv_usec / ms + syn->time.tv_sec * ms; if (engine) { finalize_frame(&engine->frame, time); if (engine->sync) - engine->sync(engine); + engine->sync(engine, syn); engine->frame.ncreate = 0; engine->frame.nmodify = 0; engine->frame.ndestroy = 0; @@ -110,7 +112,8 @@ static void tp_sync(struct touch_dev *dev, touch_time_t time) void touch_engine_init(struct touch_engine *engine, void (*event)(struct touch_engine *engine, const struct input_event *ev), - void (*sync)(struct touch_engine *engine), + void (*sync)(struct touch_engine *engine, + const struct input_event *ev), void *priv) { memset(engine, 0, sizeof(*engine)); diff --git a/test/Makefile.am b/test/Makefile.am index c8404f8..a24f615 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,4 +1,4 @@ -noinst_PROGRAMS = touch grail +noinst_PROGRAMS = touch grail evlink INCLUDES=-I$(top_srcdir)/include/ @@ -7,3 +7,6 @@ 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 new file mode 100644 index 0000000..bef5e22 --- /dev/null +++ b/test/evlink.c @@ -0,0 +1,96 @@ +/***************************************************************************** + * + * 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 82de65f..e22ffd8 100644 --- a/test/grail.c +++ b/test/grail.c @@ -22,7 +22,7 @@ * ****************************************************************************/ -#include +#include #include #include #include @@ -133,7 +133,8 @@ static void tp_event(struct touch_engine *engine, { } -static void tp_sync(struct touch_engine *engine) +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; @@ -158,7 +159,7 @@ int main(int argc, char *argv[]) struct touch_dev dev; int fd; if (argc < 2) { - fprintf(stderr, "Usage: mtdev \n"); + fprintf(stderr, "Usage: %s \n", argv[0]); return -1; } fd = open(argv[1], O_RDONLY | O_NONBLOCK); diff --git a/test/touch.c b/test/touch.c index 1f9213d..bc10b54 100644 --- a/test/touch.c +++ b/test/touch.c @@ -22,7 +22,7 @@ * ****************************************************************************/ -#include +#include #include #include #include @@ -34,7 +34,8 @@ static void tp_event(struct touch_engine *engine, fprintf(stderr, "event %d %d %d\n", ev->type, ev->code, ev->value); } -static void tp_sync(struct touch_engine *engine) +static void tp_sync(struct touch_engine *engine, + const struct input_event *syn) { struct touch_frame *frame = &engine->frame; int slot, i; @@ -62,7 +63,7 @@ int main(int argc, char *argv[]) struct touch_dev dev; int fd; if (argc < 2) { - fprintf(stderr, "Usage: mtdev \n"); + fprintf(stderr, "Usage: %s \n", argv[0]); return -1; } fd = open(argv[1], O_RDONLY | O_NONBLOCK); -- cgit v1.2.3