summaryrefslogtreecommitdiff
path: root/src/hwstate.c
diff options
context:
space:
mode:
authorHenrik Rydberg <rydberg@euromail.se>2010-03-29 23:50:05 +0200
committerHenrik Rydberg <rydberg@euromail.se>2010-04-15 06:10:07 +0200
commit9c24576540ba57449c31bafcb8f4aab41b8623b7 (patch)
tree8a9001c10f2598b74d141ea783114575c91fd39c /src/hwstate.c
parent173d728d812e78b299cbc9475fc237b9023eee2b (diff)
Rename State to HWState
Rename the hardware state struct State to HWState, to make room for additional state structures. Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/hwstate.c')
-rw-r--r--src/hwstate.c162
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
26const double FTW = 0.05;
27const double FTS = 0.05;
28const int XMAX = 32767;
29
30void init_hwstate(struct HWState *s)
31{
32 memset(s, 0, sizeof(struct HWState));
33}
34
35static int fincmp(const void *a, const void *b)
36{
37 return ((struct FingerState *)a)->id - ((struct FingerState *)b)->id;
38}
39
40static 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 */
46inline 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
54static 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
66static 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
77void 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
115const 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
128int 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
137void 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}