frameworks/base
修订版 | 62b644ec4a2f4165b47d203a153c78f45fbb72f5 (tree) |
---|---|
时间 | 2009-12-02 14:45:28 |
作者 | Bryan Hundven <bryanhundven@gmai...> |
Commiter | Bryan Hundven |
Patch InputDevice.java to read calibration settings from pointercal. Patch
from git.koolu.org by Sean McNeals (applied in koolu freerunner by Brian
Code)
@@ -22,10 +22,15 @@ import android.view.MotionEvent; | ||
22 | 22 | import android.view.Surface; |
23 | 23 | import android.view.WindowManagerPolicy; |
24 | 24 | |
25 | +import java.io.FileInputStream; | |
26 | +import java.util.StringTokenizer; | |
27 | + | |
25 | 28 | public class InputDevice { |
26 | 29 | /** Amount that trackball needs to move in order to generate a key event. */ |
27 | 30 | static final int TRACKBALL_MOVEMENT_THRESHOLD = 6; |
28 | 31 | |
32 | + static final String CALIBRATION_FILE = "/system/etc/pointercal"; | |
33 | + | |
29 | 34 | final int id; |
30 | 35 | final int classes; |
31 | 36 | final String name; |
@@ -33,6 +38,7 @@ public class InputDevice { | ||
33 | 38 | final AbsoluteInfo absY; |
34 | 39 | final AbsoluteInfo absPressure; |
35 | 40 | final AbsoluteInfo absSize; |
41 | + final TransformInfo tInfo; | |
36 | 42 | |
37 | 43 | long mDownTime = 0; |
38 | 44 | int mMetaKeysState = 0; |
@@ -40,7 +46,7 @@ public class InputDevice { | ||
40 | 46 | final MotionState mAbs = new MotionState(0, 0); |
41 | 47 | final MotionState mRel = new MotionState(TRACKBALL_MOVEMENT_THRESHOLD, |
42 | 48 | TRACKBALL_MOVEMENT_THRESHOLD); |
43 | - | |
49 | + | |
44 | 50 | static class MotionState { |
45 | 51 | int xPrecision; |
46 | 52 | int yPrecision; |
@@ -55,7 +61,7 @@ public class InputDevice { | ||
55 | 61 | int y = 0; |
56 | 62 | int pressure = 1; |
57 | 63 | int size = 0; |
58 | - | |
64 | + | |
59 | 65 | MotionState(int mx, int my) { |
60 | 66 | xPrecision = mx; |
61 | 67 | yPrecision = my; |
@@ -69,7 +75,7 @@ public class InputDevice { | ||
69 | 75 | if (!changed) { |
70 | 76 | return null; |
71 | 77 | } |
72 | - | |
78 | + | |
73 | 79 | float scaledX = x; |
74 | 80 | float scaledY = y; |
75 | 81 | float temp; |
@@ -86,11 +92,21 @@ public class InputDevice { | ||
86 | 92 | h = tmp; |
87 | 93 | } |
88 | 94 | if (device.absX != null) { |
89 | - scaledX = ((scaledX-device.absX.minValue) | |
95 | + if (device.tInfo != null) | |
96 | + scaledX = (device.tInfo.x1 * x + | |
97 | + device.tInfo.y1 * y + | |
98 | + device.tInfo.z1) / device.tInfo.s; | |
99 | + else | |
100 | + scaledX = ((scaledX-device.absX.minValue) | |
90 | 101 | / device.absX.range) * w; |
91 | 102 | } |
92 | 103 | if (device.absY != null) { |
93 | - scaledY = ((scaledY-device.absY.minValue) | |
104 | + if (device.tInfo != null) | |
105 | + scaledY = (device.tInfo.x2 * x + | |
106 | + device.tInfo.y2 * y + | |
107 | + device.tInfo.z2) / device.tInfo.s; | |
108 | + else | |
109 | + scaledY = ((scaledY-device.absY.minValue) | |
94 | 110 | / device.absY.range) * h; |
95 | 111 | } |
96 | 112 | if (device.absPressure != null) { |
@@ -198,7 +214,17 @@ public class InputDevice { | ||
198 | 214 | int flat; |
199 | 215 | int fuzz; |
200 | 216 | }; |
201 | - | |
217 | + | |
218 | + static class TransformInfo { | |
219 | + float x1; | |
220 | + float y1; | |
221 | + float z1; | |
222 | + float x2; | |
223 | + float y2; | |
224 | + float z2; | |
225 | + float s; | |
226 | + }; | |
227 | + | |
202 | 228 | InputDevice(int _id, int _classes, String _name, |
203 | 229 | AbsoluteInfo _absX, AbsoluteInfo _absY, |
204 | 230 | AbsoluteInfo _absPressure, AbsoluteInfo _absSize) { |
@@ -209,5 +235,34 @@ public class InputDevice { | ||
209 | 235 | absY = _absY; |
210 | 236 | absPressure = _absPressure; |
211 | 237 | absSize = _absSize; |
238 | + TransformInfo t = null; | |
239 | + try { | |
240 | + FileInputStream is = new FileInputStream(CALIBRATION_FILE); | |
241 | + byte[] mBuffer = new byte[64]; | |
242 | + int len = is.read(mBuffer); | |
243 | + is.close(); | |
244 | + if (len > 0) { | |
245 | + int i; | |
246 | + for (i = 0 ; i < len ; i++) { | |
247 | + if (mBuffer[i] == '\n' || mBuffer[i] == 0) { | |
248 | + break; | |
249 | + } | |
250 | + } | |
251 | + len = i; | |
252 | + } | |
253 | + StringTokenizer st = new StringTokenizer( new String(mBuffer, 0, 0, len) ); | |
254 | + | |
255 | + t = new TransformInfo (); | |
256 | + t.x1 = Integer.parseInt( st.nextToken() ); | |
257 | + t.y1 = Integer.parseInt( st.nextToken() ); | |
258 | + t.z1 = Integer.parseInt( st.nextToken() ); | |
259 | + t.x2 = Integer.parseInt( st.nextToken() ); | |
260 | + t.y2 = Integer.parseInt( st.nextToken() ); | |
261 | + t.z2 = Integer.parseInt( st.nextToken() ); | |
262 | + t.s = Integer.parseInt( st.nextToken() ); | |
263 | + } catch (java.io.FileNotFoundException e) { | |
264 | + } catch (java.io.IOException e) { | |
265 | + } | |
266 | + tInfo = t; | |
212 | 267 | } |
213 | 268 | }; |
@@ -0,0 +1,213 @@ | ||
1 | +/* | |
2 | + * Copyright (C) 2007 The Android Open Source Project | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | + | |
17 | +package com.android.server; | |
18 | + | |
19 | +import android.util.Log; | |
20 | +import android.view.Display; | |
21 | +import android.view.MotionEvent; | |
22 | +import android.view.Surface; | |
23 | +import android.view.WindowManagerPolicy; | |
24 | + | |
25 | +public class InputDevice { | |
26 | + /** Amount that trackball needs to move in order to generate a key event. */ | |
27 | + static final int TRACKBALL_MOVEMENT_THRESHOLD = 6; | |
28 | + | |
29 | + final int id; | |
30 | + final int classes; | |
31 | + final String name; | |
32 | + final AbsoluteInfo absX; | |
33 | + final AbsoluteInfo absY; | |
34 | + final AbsoluteInfo absPressure; | |
35 | + final AbsoluteInfo absSize; | |
36 | + | |
37 | + long mDownTime = 0; | |
38 | + int mMetaKeysState = 0; | |
39 | + | |
40 | + final MotionState mAbs = new MotionState(0, 0); | |
41 | + final MotionState mRel = new MotionState(TRACKBALL_MOVEMENT_THRESHOLD, | |
42 | + TRACKBALL_MOVEMENT_THRESHOLD); | |
43 | + | |
44 | + static class MotionState { | |
45 | + int xPrecision; | |
46 | + int yPrecision; | |
47 | + float xMoveScale; | |
48 | + float yMoveScale; | |
49 | + MotionEvent currentMove = null; | |
50 | + boolean changed = false; | |
51 | + boolean down = false; | |
52 | + boolean lastDown = false; | |
53 | + long downTime = 0; | |
54 | + int x = 0; | |
55 | + int y = 0; | |
56 | + int pressure = 1; | |
57 | + int size = 0; | |
58 | + | |
59 | + MotionState(int mx, int my) { | |
60 | + xPrecision = mx; | |
61 | + yPrecision = my; | |
62 | + xMoveScale = mx != 0 ? (1.0f/mx) : 1.0f; | |
63 | + yMoveScale = my != 0 ? (1.0f/my) : 1.0f; | |
64 | + } | |
65 | + | |
66 | + MotionEvent generateMotion(InputDevice device, long curTime, | |
67 | + boolean isAbs, Display display, int orientation, | |
68 | + int metaState) { | |
69 | + if (!changed) { | |
70 | + return null; | |
71 | + } | |
72 | + | |
73 | + float scaledX = x; | |
74 | + float scaledY = y; | |
75 | + float temp; | |
76 | + float scaledPressure = 1.0f; | |
77 | + float scaledSize = 0; | |
78 | + int edgeFlags = 0; | |
79 | + if (isAbs) { | |
80 | + int w = display.getWidth()-1; | |
81 | + int h = display.getHeight()-1; | |
82 | + if (orientation == Surface.ROTATION_90 | |
83 | + || orientation == Surface.ROTATION_270) { | |
84 | + int tmp = w; | |
85 | + w = h; | |
86 | + h = tmp; | |
87 | + } | |
88 | + if (device.absX != null) { | |
89 | + scaledX = ((scaledX-device.absX.minValue) | |
90 | + / device.absX.range) * w; | |
91 | + } | |
92 | + if (device.absY != null) { | |
93 | + scaledY = ((scaledY-device.absY.minValue) | |
94 | + / device.absY.range) * h; | |
95 | + } | |
96 | + if (device.absPressure != null) { | |
97 | + scaledPressure = | |
98 | + ((pressure-device.absPressure.minValue) | |
99 | + / (float)device.absPressure.range); | |
100 | + } | |
101 | + if (device.absSize != null) { | |
102 | + scaledSize = | |
103 | + ((size-device.absSize.minValue) | |
104 | + / (float)device.absSize.range); | |
105 | + } | |
106 | + switch (orientation) { | |
107 | + case Surface.ROTATION_90: | |
108 | + temp = scaledX; | |
109 | + scaledX = scaledY; | |
110 | + scaledY = w-temp; | |
111 | + break; | |
112 | + case Surface.ROTATION_180: | |
113 | + scaledX = w-scaledX; | |
114 | + scaledY = h-scaledY; | |
115 | + break; | |
116 | + case Surface.ROTATION_270: | |
117 | + temp = scaledX; | |
118 | + scaledX = h-scaledY; | |
119 | + scaledY = temp; | |
120 | + break; | |
121 | + } | |
122 | + | |
123 | + if (scaledX == 0) { | |
124 | + edgeFlags += MotionEvent.EDGE_LEFT; | |
125 | + } else if (scaledX == display.getWidth() - 1.0f) { | |
126 | + edgeFlags += MotionEvent.EDGE_RIGHT; | |
127 | + } | |
128 | + | |
129 | + if (scaledY == 0) { | |
130 | + edgeFlags += MotionEvent.EDGE_TOP; | |
131 | + } else if (scaledY == display.getHeight() - 1.0f) { | |
132 | + edgeFlags += MotionEvent.EDGE_BOTTOM; | |
133 | + } | |
134 | + | |
135 | + } else { | |
136 | + scaledX *= xMoveScale; | |
137 | + scaledY *= yMoveScale; | |
138 | + switch (orientation) { | |
139 | + case Surface.ROTATION_90: | |
140 | + temp = scaledX; | |
141 | + scaledX = scaledY; | |
142 | + scaledY = -temp; | |
143 | + break; | |
144 | + case Surface.ROTATION_180: | |
145 | + scaledX = -scaledX; | |
146 | + scaledY = -scaledY; | |
147 | + break; | |
148 | + case Surface.ROTATION_270: | |
149 | + temp = scaledX; | |
150 | + scaledX = -scaledY; | |
151 | + scaledY = temp; | |
152 | + break; | |
153 | + } | |
154 | + } | |
155 | + | |
156 | + changed = false; | |
157 | + if (down != lastDown) { | |
158 | + int action; | |
159 | + lastDown = down; | |
160 | + if (down) { | |
161 | + action = MotionEvent.ACTION_DOWN; | |
162 | + downTime = curTime; | |
163 | + } else { | |
164 | + action = MotionEvent.ACTION_UP; | |
165 | + } | |
166 | + currentMove = null; | |
167 | + if (!isAbs) { | |
168 | + x = y = 0; | |
169 | + } | |
170 | + return MotionEvent.obtain(downTime, curTime, action, | |
171 | + scaledX, scaledY, scaledPressure, scaledSize, metaState, | |
172 | + xPrecision, yPrecision, device.id, edgeFlags); | |
173 | + } else { | |
174 | + if (currentMove != null) { | |
175 | + if (false) Log.i("InputDevice", "Adding batch x=" + scaledX | |
176 | + + " y=" + scaledY + " to " + currentMove); | |
177 | + currentMove.addBatch(curTime, scaledX, scaledY, | |
178 | + scaledPressure, scaledSize, metaState); | |
179 | + if (WindowManagerPolicy.WATCH_POINTER) { | |
180 | + Log.i("KeyInputQueue", "Updating: " + currentMove); | |
181 | + } | |
182 | + return null; | |
183 | + } | |
184 | + MotionEvent me = MotionEvent.obtain(downTime, curTime, | |
185 | + MotionEvent.ACTION_MOVE, scaledX, scaledY, | |
186 | + scaledPressure, scaledSize, metaState, | |
187 | + xPrecision, yPrecision, device.id, edgeFlags); | |
188 | + currentMove = me; | |
189 | + return me; | |
190 | + } | |
191 | + } | |
192 | + } | |
193 | + | |
194 | + static class AbsoluteInfo { | |
195 | + int minValue; | |
196 | + int maxValue; | |
197 | + int range; | |
198 | + int flat; | |
199 | + int fuzz; | |
200 | + }; | |
201 | + | |
202 | + InputDevice(int _id, int _classes, String _name, | |
203 | + AbsoluteInfo _absX, AbsoluteInfo _absY, | |
204 | + AbsoluteInfo _absPressure, AbsoluteInfo _absSize) { | |
205 | + id = _id; | |
206 | + classes = _classes; | |
207 | + name = _name; | |
208 | + absX = _absX; | |
209 | + absY = _absY; | |
210 | + absPressure = _absPressure; | |
211 | + absSize = _absSize; | |
212 | + } | |
213 | +}; |