diff options
Diffstat (limited to 'src/hwstate.c')
| -rw-r--r-- | src/hwstate.c | 162 |
1 files changed, 162 insertions, 0 deletions
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 @@ | |||
| 1 | /*************************************************************************** | ||
| 2 | * | ||
| 3 | * Multitouch X driver | ||
| 4 | * Copyright (C) 2008 Henrik Rydberg <rydberg@euromail.se> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License as published by | ||
| 8 | * the Free Software Foundation; either version 2 of the License, or | ||
| 9 | * (at your option) any later version. | ||
| 10 | * | ||
| 11 | * This program is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | * GNU General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License | ||
| 17 | * along with this program; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | * | ||
| 20 | **************************************************************************/ | ||
| 21 | |||
| 22 | #include "hwstate.h" | ||
| 23 | #include <stdlib.h> | ||
| 24 | #include <limits.h> | ||
| 25 | |||
| 26 | const double FTW = 0.05; | ||
| 27 | const double FTS = 0.05; | ||
| 28 | const int XMAX = 32767; | ||
| 29 | |||
| 30 | void init_hwstate(struct HWState *s) | ||
| 31 | { | ||
| 32 | memset(s, 0, sizeof(struct HWState)); | ||
| 33 | } | ||
| 34 | |||
| 35 | static int fincmp(const void *a, const void *b) | ||
| 36 | { | ||
| 37 | return ((struct FingerState *)a)->id - ((struct FingerState *)b)->id; | ||
| 38 | } | ||
| 39 | |||
| 40 | static inline int clamp15(int x) | ||
| 41 | { | ||
| 42 | return x < -XMAX ? -XMAX : x > XMAX ? XMAX : x; | ||
| 43 | } | ||
| 44 | |||
| 45 | /* abslute scale is assumed to fit in 15 bits */ | ||
| 46 | inline int dist2(const struct FingerData *a, const struct FingerData *b) | ||
| 47 | { | ||
| 48 | int dx = clamp15(a->position_x - b->position_x); | ||
| 49 | int dy = clamp15(a->position_y - b->position_y); | ||
| 50 | |||
| 51 | return dx * dx + dy * dy; | ||
| 52 | } | ||
| 53 | |||
| 54 | static void set_finger(struct FingerState *fs, | ||
| 55 | const struct FingerData *hw, int id, | ||
| 56 | const struct Capabilities *caps) | ||
| 57 | { | ||
| 58 | fs->hw = *hw; | ||
| 59 | fs->id = id; | ||
| 60 | if (!caps->has_touch_minor) | ||
| 61 | fs->hw.touch_minor = hw->touch_major; | ||
| 62 | if (!caps->has_width_minor) | ||
| 63 | fs->hw.width_minor = hw->width_major; | ||
| 64 | } | ||
| 65 | |||
| 66 | static int touching_finger(const struct FingerData *hw, | ||
| 67 | const struct Capabilities *caps) | ||
| 68 | { | ||
| 69 | if (caps->has_touch_major && caps->has_width_major) | ||
| 70 | return hw->width_major > 0 && | ||
| 71 | hw->touch_major > FTW * hw->width_major; | ||
| 72 | if (caps->has_touch_major) | ||
| 73 | return hw->touch_major > FTS * caps->abs_touch_major.maximum; | ||
| 74 | return 1; | ||
| 75 | } | ||
| 76 | |||
| 77 | void modify_hwstate(struct HWState *s, | ||
| 78 | const struct HWData *hw, | ||
| 79 | const struct Capabilities *caps) | ||
| 80 | { | ||
| 81 | int A[DIM2_FINGER], *row; | ||
| 82 | int sid[DIM_FINGER], hw2s[DIM_FINGER]; | ||
| 83 | int id, sk, hwk; | ||
| 84 | |||
| 85 | /* setup distance matrix for finger id matching */ | ||
| 86 | for (sk = 0; sk < s->nfinger; sk++) { | ||
| 87 | sid[sk] = s->finger[sk].id; | ||
| 88 | row = A + hw->nfinger * sk; | ||
| 89 | for (hwk = 0; hwk < hw->nfinger; hwk++) | ||
| 90 | row[hwk] = dist2(&hw->finger[hwk], &s->finger[sk].hw); | ||
| 91 | } | ||
| 92 | |||
| 93 | match_fingers(hw2s, A, hw->nfinger, s->nfinger); | ||
| 94 | |||
| 95 | /* update matched fingers and create new ones */ | ||
| 96 | for (hwk = 0; hwk < hw->nfinger; hwk++) { | ||
| 97 | sk = hw2s[hwk]; | ||
| 98 | id = sk >= 0 ? sid[sk] : 0; | ||
| 99 | if (!touching_finger(&hw->finger[hwk], caps)) | ||
| 100 | id = 0; | ||
| 101 | else | ||
| 102 | while (!id) | ||
| 103 | id = ++s->lastid; | ||
| 104 | set_finger(&s->finger[hwk], &hw->finger[hwk], id, caps); | ||
| 105 | } | ||
| 106 | |||
| 107 | s->button = hw->button; | ||
| 108 | s->nfinger = hw->nfinger; | ||
| 109 | s->evtime = hw->evtime; | ||
| 110 | |||
| 111 | /* sort fingers in touching order */ | ||
| 112 | qsort(s->finger, s->nfinger, sizeof(struct FingerState), fincmp); | ||
| 113 | } | ||
| 114 | |||
| 115 | const struct FingerState *find_finger(const struct HWState *s, int id) | ||
| 116 | { | ||
| 117 | int i; | ||
| 118 | |||
| 119 | if (!id) | ||
| 120 | return NULL; | ||
| 121 | for (i = 0; i < s->nfinger; i++) | ||
| 122 | if (s->finger[i].id == id) | ||
| 123 | return s->finger + i; | ||
| 124 | |||
| 125 | return NULL; | ||
| 126 | } | ||
| 127 | |||
| 128 | int count_fingers(const struct HWState *s) | ||
| 129 | { | ||
| 130 | int i, n = 0; | ||
| 131 | for (i = 0; i < s->nfinger; i++) | ||
| 132 | if (s->finger[i].id) | ||
| 133 | n++; | ||
| 134 | return n; | ||
| 135 | } | ||
| 136 | |||
| 137 | void output_hwstate(const struct HWState *s) | ||
| 138 | { | ||
| 139 | int i; | ||
| 140 | xf86Msg(X_INFO, "buttons: %d%d%d\n", | ||
| 141 | GETBIT(s->button, MT_BUTTON_LEFT), | ||
| 142 | GETBIT(s->button, MT_BUTTON_MIDDLE), | ||
| 143 | GETBIT(s->button, MT_BUTTON_RIGHT)); | ||
| 144 | xf86Msg(X_INFO, "fingers: %d\n", | ||
| 145 | s->nfinger); | ||
| 146 | xf86Msg(X_INFO, "evtime: %lld\n", | ||
| 147 | s->evtime); | ||
| 148 | for (i = 0; i < s->nfinger; i++) { | ||
| 149 | xf86Msg(X_INFO, | ||
| 150 | " %+02d %+05d:%+05d +%05d:%+05d " | ||
| 151 | "%+06d %+06d %+05d:%+05d\n", | ||
| 152 | s->finger[i].id, | ||
| 153 | s->finger[i].hw.touch_major, | ||
| 154 | s->finger[i].hw.touch_minor, | ||
| 155 | s->finger[i].hw.width_major, | ||
| 156 | s->finger[i].hw.width_minor, | ||
| 157 | s->finger[i].hw.orientation, | ||
| 158 | s->finger[i].hw.pressure, | ||
| 159 | s->finger[i].hw.position_x, | ||
| 160 | s->finger[i].hw.position_y); | ||
| 161 | } | ||
| 162 | } | ||
