From 9c24576540ba57449c31bafcb8f4aab41b8623b7 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Mon, 29 Mar 2010 23:50:05 +0200 Subject: Rename State to HWState Rename the hardware state struct State to HWState, to make room for additional state structures. Signed-off-by: Henrik Rydberg --- Makefile | 2 +- src/gestures.c | 22 ++++---- src/hwstate.c | 162 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/hwstate.h | 51 ++++++++++++++++++ src/mtouch.c | 6 +-- src/mtouch.h | 4 +- src/state.c | 162 --------------------------------------------------------- src/state.h | 51 ------------------ 8 files changed, 230 insertions(+), 230 deletions(-) create mode 100644 src/hwstate.c create mode 100644 src/hwstate.h delete mode 100644 src/state.c delete mode 100644 src/state.h diff --git a/Makefile b/Makefile index 7779580..2e202b1 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ o_match = match o_src = capabilities \ iobuffer \ hwdata \ - state \ + hwstate \ mtouch \ gestures \ multitouch diff --git a/src/gestures.c b/src/gestures.c index 01e7b10..5dd6861 100644 --- a/src/gestures.c +++ b/src/gestures.c @@ -25,16 +25,16 @@ void extract_gestures(struct Gestures *gs, struct MTouch* mt) { - const struct FingerState *b = mt->ns.finger; - const struct FingerState *e = b + mt->ns.nfinger; + const struct FingerState *b = mt->nhs.finger; + const struct FingerState *e = b + mt->nhs.nfinger; const struct FingerState *p, *fs; - int nof = count_fingers(&mt->os); - int nsf = count_fingers(&mt->ns); + int nof = count_fingers(&mt->ohs); + int nsf = count_fingers(&mt->nhs); int dn = 0, i; memset(gs, 0, sizeof(struct Gestures)); if (nof == nsf) { for (p = b; p != e; p++) { - fs = find_finger(&mt->os, p->id); + fs = find_finger(&mt->ohs, p->id); if (fs) { gs->dx += p->hw.position_x - fs->hw.position_x; gs->dy += p->hw.position_y - fs->hw.position_y; @@ -52,14 +52,14 @@ void extract_gestures(struct Gestures *gs, struct MTouch* mt) if (nsf == 3) SETBIT(gs->type, GS_HSCROLL); } - if (mt->ns.button == BITMASK(MT_BUTTON_LEFT)) { + if (mt->nhs.button == BITMASK(MT_BUTTON_LEFT)) { if (nsf == 2) - mt->ns.button = BITMASK(MT_BUTTON_RIGHT); + mt->nhs.button = BITMASK(MT_BUTTON_RIGHT); if (nsf == 3) - mt->ns.button = BITMASK(MT_BUTTON_MIDDLE); + mt->nhs.button = BITMASK(MT_BUTTON_MIDDLE); } - gs->btmask = (mt->ns.button ^ mt->os.button) & BITONES(DIM_BUTTON); - gs->btdata = mt->ns.button & BITONES(DIM_BUTTON); - mt->os = mt->ns; + gs->btmask = (mt->nhs.button ^ mt->ohs.button) & BITONES(DIM_BUTTON); + gs->btdata = mt->nhs.button & BITONES(DIM_BUTTON); + mt->ohs = mt->nhs; } diff --git a/src/hwstate.c b/src/hwstate.c new file mode 100644 index 0000000..7e40b50 --- /dev/null +++ b/src/hwstate.c @@ -0,0 +1,162 @@ +/*************************************************************************** + * + * Multitouch X driver + * Copyright (C) 2008 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + **************************************************************************/ + +#include "hwstate.h" +#include +#include + +const double FTW = 0.05; +const double FTS = 0.05; +const int XMAX = 32767; + +void init_hwstate(struct HWState *s) +{ + memset(s, 0, sizeof(struct HWState)); +} + +static int fincmp(const void *a, const void *b) +{ + return ((struct FingerState *)a)->id - ((struct FingerState *)b)->id; +} + +static inline int clamp15(int x) +{ + return x < -XMAX ? -XMAX : x > XMAX ? XMAX : x; +} + +/* abslute scale is assumed to fit in 15 bits */ +inline int dist2(const struct FingerData *a, const struct FingerData *b) +{ + int dx = clamp15(a->position_x - b->position_x); + int dy = clamp15(a->position_y - b->position_y); + + return dx * dx + dy * dy; +} + +static void set_finger(struct FingerState *fs, + const struct FingerData *hw, int id, + const struct Capabilities *caps) +{ + fs->hw = *hw; + fs->id = id; + if (!caps->has_touch_minor) + fs->hw.touch_minor = hw->touch_major; + if (!caps->has_width_minor) + fs->hw.width_minor = hw->width_major; +} + +static int touching_finger(const struct FingerData *hw, + const struct Capabilities *caps) +{ + if (caps->has_touch_major && caps->has_width_major) + return hw->width_major > 0 && + hw->touch_major > FTW * hw->width_major; + if (caps->has_touch_major) + return hw->touch_major > FTS * caps->abs_touch_major.maximum; + return 1; +} + +void modify_hwstate(struct HWState *s, + const struct HWData *hw, + const struct Capabilities *caps) +{ + int A[DIM2_FINGER], *row; + int sid[DIM_FINGER], hw2s[DIM_FINGER]; + int id, sk, hwk; + + /* setup distance matrix for finger id matching */ + for (sk = 0; sk < s->nfinger; sk++) { + sid[sk] = s->finger[sk].id; + row = A + hw->nfinger * sk; + for (hwk = 0; hwk < hw->nfinger; hwk++) + row[hwk] = dist2(&hw->finger[hwk], &s->finger[sk].hw); + } + + match_fingers(hw2s, A, hw->nfinger, s->nfinger); + + /* update matched fingers and create new ones */ + for (hwk = 0; hwk < hw->nfinger; hwk++) { + sk = hw2s[hwk]; + id = sk >= 0 ? sid[sk] : 0; + if (!touching_finger(&hw->finger[hwk], caps)) + id = 0; + else + while (!id) + id = ++s->lastid; + set_finger(&s->finger[hwk], &hw->finger[hwk], id, caps); + } + + s->button = hw->button; + s->nfinger = hw->nfinger; + s->evtime = hw->evtime; + + /* sort fingers in touching order */ + qsort(s->finger, s->nfinger, sizeof(struct FingerState), fincmp); +} + +const struct FingerState *find_finger(const struct HWState *s, int id) +{ + int i; + + if (!id) + return NULL; + for (i = 0; i < s->nfinger; i++) + if (s->finger[i].id == id) + return s->finger + i; + + return NULL; +} + +int count_fingers(const struct HWState *s) +{ + int i, n = 0; + for (i = 0; i < s->nfinger; i++) + if (s->finger[i].id) + n++; + return n; +} + +void output_hwstate(const struct HWState *s) +{ + int i; + xf86Msg(X_INFO, "buttons: %d%d%d\n", + GETBIT(s->button, MT_BUTTON_LEFT), + GETBIT(s->button, MT_BUTTON_MIDDLE), + GETBIT(s->button, MT_BUTTON_RIGHT)); + xf86Msg(X_INFO, "fingers: %d\n", + s->nfinger); + xf86Msg(X_INFO, "evtime: %lld\n", + s->evtime); + for (i = 0; i < s->nfinger; i++) { + xf86Msg(X_INFO, + " %+02d %+05d:%+05d +%05d:%+05d " + "%+06d %+06d %+05d:%+05d\n", + s->finger[i].id, + s->finger[i].hw.touch_major, + s->finger[i].hw.touch_minor, + s->finger[i].hw.width_major, + s->finger[i].hw.width_minor, + s->finger[i].hw.orientation, + s->finger[i].hw.pressure, + s->finger[i].hw.position_x, + s->finger[i].hw.position_y); + } +} diff --git a/src/hwstate.h b/src/hwstate.h new file mode 100644 index 0000000..fd53e71 --- /dev/null +++ b/src/hwstate.h @@ -0,0 +1,51 @@ +/*************************************************************************** + * + * Multitouch X driver + * Copyright (C) 2008 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + **************************************************************************/ + +#ifndef MTEVENT_H +#define MTEVENT_H + +#include "capabilities.h" +#include "hwdata.h" + +/* zero id means not mapped (not touching) */ +struct FingerState { + struct FingerData hw; + int id; +}; + +struct HWState { + struct FingerState finger[DIM_FINGER]; + unsigned button; + int nfinger; + mstime_t evtime; + int lastid; +}; + +void init_hwstate(struct HWState *s); +void modify_hwstate(struct HWState *s, + const struct HWData *hw, + const struct Capabilities *caps); +void output_hwstate(const struct HWState *s); + +const struct FingerState *find_finger(const struct HWState *s, int id); +int count_fingers(const struct HWState *s); + +#endif diff --git a/src/mtouch.c b/src/mtouch.c index f7ffb12..5bf72ec 100644 --- a/src/mtouch.c +++ b/src/mtouch.c @@ -35,8 +35,8 @@ int open_mtouch(struct MTouch *mt, int fd) int rc; init_iobuf(&mt->buf); init_hwdata(&mt->hw); - init_state(&mt->os); - init_state(&mt->ns); + init_hwstate(&mt->ohs); + init_hwstate(&mt->nhs); SYSCALL(rc = ioctl(fd, EVIOCGRAB, (pointer)1)); return rc; } @@ -59,5 +59,5 @@ int read_synchronized_event(struct MTouch *mt, int fd) void parse_event(struct MTouch *mt) { - modify_state(&mt->ns, &mt->hw, &mt->caps); + modify_hwstate(&mt->nhs, &mt->hw, &mt->caps); } diff --git a/src/mtouch.h b/src/mtouch.h index 753a400..e4a59fb 100644 --- a/src/mtouch.h +++ b/src/mtouch.h @@ -25,13 +25,13 @@ #include "capabilities.h" #include "iobuffer.h" #include "hwdata.h" -#include "state.h" +#include "hwstate.h" struct MTouch { struct Capabilities caps; struct IOBuffer buf; struct HWData hw; - struct State os, ns; + struct HWState ohs, nhs; }; int configure_mtouch(struct MTouch *mt, int fd); diff --git a/src/state.c b/src/state.c deleted file mode 100644 index 72bb759..0000000 --- a/src/state.c +++ /dev/null @@ -1,162 +0,0 @@ -/*************************************************************************** - * - * Multitouch X driver - * Copyright (C) 2008 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 2 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, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - **************************************************************************/ - -#include "state.h" -#include -#include - -const double FTW = 0.05; -const double FTS = 0.05; -const int XMAX = 32767; - -void init_state(struct State *s) -{ - memset(s, 0, sizeof(struct State)); -} - -static int fincmp(const void *a, const void *b) -{ - return ((struct FingerState *)a)->id - ((struct FingerState *)b)->id; -} - -static inline int clamp15(int x) -{ - return x < -XMAX ? -XMAX : x > XMAX ? XMAX : x; -} - -/* abslute scale is assumed to fit in 15 bits */ -inline int dist2(const struct FingerData *a, const struct FingerData *b) -{ - int dx = clamp15(a->position_x - b->position_x); - int dy = clamp15(a->position_y - b->position_y); - - return dx * dx + dy * dy; -} - -static void set_finger(struct FingerState *fs, - const struct FingerData *hw, int id, - const struct Capabilities *caps) -{ - fs->hw = *hw; - fs->id = id; - if (!caps->has_touch_minor) - fs->hw.touch_minor = hw->touch_major; - if (!caps->has_width_minor) - fs->hw.width_minor = hw->width_major; -} - -static int touching_finger(const struct FingerData *hw, - const struct Capabilities *caps) -{ - if (caps->has_touch_major && caps->has_width_major) - return hw->width_major > 0 && - hw->touch_major > FTW * hw->width_major; - if (caps->has_touch_major) - return hw->touch_major > FTS * caps->abs_touch_major.maximum; - return 1; -} - -void modify_state(struct State *s, - const struct HWData *hw, - const struct Capabilities *caps) -{ - int A[DIM2_FINGER], *row; - int sid[DIM_FINGER], hw2s[DIM_FINGER]; - int id, sk, hwk; - - /* setup distance matrix for finger id matching */ - for (sk = 0; sk < s->nfinger; sk++) { - sid[sk] = s->finger[sk].id; - row = A + hw->nfinger * sk; - for (hwk = 0; hwk < hw->nfinger; hwk++) - row[hwk] = dist2(&hw->finger[hwk], &s->finger[sk].hw); - } - - match_fingers(hw2s, A, hw->nfinger, s->nfinger); - - /* update matched fingers and create new ones */ - for (hwk = 0; hwk < hw->nfinger; hwk++) { - sk = hw2s[hwk]; - id = sk >= 0 ? sid[sk] : 0; - if (!touching_finger(&hw->finger[hwk], caps)) - id = 0; - else - while (!id) - id = ++s->lastid; - set_finger(&s->finger[hwk], &hw->finger[hwk], id, caps); - } - - s->button = hw->button; - s->nfinger = hw->nfinger; - s->evtime = hw->evtime; - - /* sort fingers in touching order */ - qsort(s->finger, s->nfinger, sizeof(struct FingerState), fincmp); -} - -const struct FingerState *find_finger(const struct State *s, int id) -{ - int i; - - if (!id) - return NULL; - for (i = 0; i < s->nfinger; i++) - if (s->finger[i].id == id) - return s->finger + i; - - return NULL; -} - -int count_fingers(const struct State *s) -{ - int i, n = 0; - for (i = 0; i < s->nfinger; i++) - if (s->finger[i].id) - n++; - return n; -} - -void output_state(const struct State *s) -{ - int i; - xf86Msg(X_INFO, "buttons: %d%d%d\n", - GETBIT(s->button, MT_BUTTON_LEFT), - GETBIT(s->button, MT_BUTTON_MIDDLE), - GETBIT(s->button, MT_BUTTON_RIGHT)); - xf86Msg(X_INFO, "fingers: %d\n", - s->nfinger); - xf86Msg(X_INFO, "evtime: %lld\n", - s->evtime); - for (i = 0; i < s->nfinger; i++) { - xf86Msg(X_INFO, - " %+02d %+05d:%+05d +%05d:%+05d " - "%+06d %+06d %+05d:%+05d\n", - s->finger[i].id, - s->finger[i].hw.touch_major, - s->finger[i].hw.touch_minor, - s->finger[i].hw.width_major, - s->finger[i].hw.width_minor, - s->finger[i].hw.orientation, - s->finger[i].hw.pressure, - s->finger[i].hw.position_x, - s->finger[i].hw.position_y); - } -} diff --git a/src/state.h b/src/state.h deleted file mode 100644 index 2c2d2b7..0000000 --- a/src/state.h +++ /dev/null @@ -1,51 +0,0 @@ -/*************************************************************************** - * - * Multitouch X driver - * Copyright (C) 2008 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 2 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, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - **************************************************************************/ - -#ifndef MTEVENT_H -#define MTEVENT_H - -#include "capabilities.h" -#include "hwdata.h" - -/* zero id means not mapped (not touching) */ -struct FingerState { - struct FingerData hw; - int id; -}; - -struct State { - struct FingerState finger[DIM_FINGER]; - unsigned button; - int nfinger; - mstime_t evtime; - int lastid; -}; - -void init_state(struct State *s); -void modify_state(struct State *s, - const struct HWData *hw, - const struct Capabilities *caps); -void output_state(const struct State *s); - -const struct FingerState *find_finger(const struct State *s, int id); -int count_fingers(const struct State *s); - -#endif -- cgit v1.2.3