blob: cdae27c5bda90850fd9809edcd55e1c1e6b68631 [file] [log] [blame]
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 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
17package com.android.server;
18
19import com.android.internal.util.XmlUtils;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070020
21import org.xmlpull.v1.XmlPullParser;
22
23import android.content.Context;
Jeff Brown349703e2010-06-22 01:27:15 -070024import android.content.pm.PackageManager;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070025import android.content.res.Configuration;
26import android.os.Environment;
27import android.os.LocalPowerManager;
28import android.os.PowerManager;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070029import android.util.Slog;
30import android.util.Xml;
31import android.view.InputChannel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070032import android.view.KeyEvent;
33import android.view.MotionEvent;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070034import android.view.Surface;
35import android.view.WindowManagerPolicy;
36
37import java.io.BufferedReader;
38import java.io.File;
39import java.io.FileInputStream;
40import java.io.FileNotFoundException;
41import java.io.FileReader;
42import java.io.IOException;
43import java.io.InputStreamReader;
44import java.io.PrintWriter;
45import java.util.ArrayList;
46
47/*
48 * Wraps the C++ InputManager and provides its callbacks.
49 *
50 * XXX Tempted to promote this to a first-class service, ie. InputManagerService, to
51 * improve separation of concerns with respect to the window manager.
52 */
53public class InputManager {
54 static final String TAG = "InputManager";
55
56 private final Callbacks mCallbacks;
57 private final Context mContext;
58 private final WindowManagerService mWindowManagerService;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070059 private final PowerManager mPowerManager;
60 private final PowerManagerService mPowerManagerService;
61
62 private int mTouchScreenConfig;
63 private int mKeyboardConfig;
64 private int mNavigationConfig;
65
66 private static native void nativeInit(Callbacks callbacks);
67 private static native void nativeStart();
68 private static native void nativeSetDisplaySize(int displayId, int width, int height);
69 private static native void nativeSetDisplayOrientation(int displayId, int rotation);
70
71 private static native int nativeGetScanCodeState(int deviceId, int deviceClasses,
72 int scanCode);
73 private static native int nativeGetKeyCodeState(int deviceId, int deviceClasses,
74 int keyCode);
75 private static native int nativeGetSwitchState(int deviceId, int deviceClasses,
76 int sw);
77 private static native boolean nativeHasKeys(int[] keyCodes, boolean[] keyExists);
78 private static native void nativeRegisterInputChannel(InputChannel inputChannel);
79 private static native void nativeUnregisterInputChannel(InputChannel inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -070080 private static native int nativeInjectKeyEvent(KeyEvent event, int nature,
81 int injectorPid, int injectorUid, boolean sync, int timeoutMillis);
82 private static native int nativeInjectMotionEvent(MotionEvent event, int nature,
83 int injectorPid, int injectorUid, boolean sync, int timeoutMillis);
Jeff Brown349703e2010-06-22 01:27:15 -070084 private static native void nativeSetInputWindows(InputWindow[] windows);
85 private static native void nativeSetInputDispatchMode(boolean enabled, boolean frozen);
86 private static native void nativeSetFocusedApplication(InputApplication application);
87 private static native void nativePreemptInputDispatch();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070088
89 // Device class as defined by EventHub.
90 private static final int CLASS_KEYBOARD = 0x00000001;
91 private static final int CLASS_ALPHAKEY = 0x00000002;
92 private static final int CLASS_TOUCHSCREEN = 0x00000004;
93 private static final int CLASS_TRACKBALL = 0x00000008;
94 private static final int CLASS_TOUCHSCREEN_MT = 0x00000010;
95 private static final int CLASS_DPAD = 0x00000020;
96
Jeff Brown7fbdc842010-06-17 20:52:56 -070097 // Input event injection constants defined in InputDispatcher.h.
98 static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0;
99 static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1;
100 static final int INPUT_EVENT_INJECTION_FAILED = 2;
101 static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3;
102
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700103 public InputManager(Context context,
104 WindowManagerService windowManagerService,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700105 PowerManager powerManager,
106 PowerManagerService powerManagerService) {
107 this.mContext = context;
108 this.mWindowManagerService = windowManagerService;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700109 this.mPowerManager = powerManager;
110 this.mPowerManagerService = powerManagerService;
111
112 this.mCallbacks = new Callbacks();
113
114 mTouchScreenConfig = Configuration.TOUCHSCREEN_NOTOUCH;
115 mKeyboardConfig = Configuration.KEYBOARD_NOKEYS;
116 mNavigationConfig = Configuration.NAVIGATION_NONAV;
117
118 init();
119 }
120
121 private void init() {
122 Slog.i(TAG, "Initializing input manager");
123 nativeInit(mCallbacks);
124 }
125
126 public void start() {
127 Slog.i(TAG, "Starting input manager");
128 nativeStart();
129 }
130
131 public void setDisplaySize(int displayId, int width, int height) {
132 if (width <= 0 || height <= 0) {
133 throw new IllegalArgumentException("Invalid display id or dimensions.");
134 }
135
136 Slog.i(TAG, "Setting display #" + displayId + " size to " + width + "x" + height);
137 nativeSetDisplaySize(displayId, width, height);
138 }
139
140 public void setDisplayOrientation(int displayId, int rotation) {
141 if (rotation < Surface.ROTATION_0 || rotation > Surface.ROTATION_270) {
142 throw new IllegalArgumentException("Invalid rotation.");
143 }
144
145 Slog.i(TAG, "Setting display #" + displayId + " orientation to " + rotation);
146 nativeSetDisplayOrientation(displayId, rotation);
147 }
148
149 public void getInputConfiguration(Configuration config) {
150 if (config == null) {
151 throw new IllegalArgumentException("config must not be null.");
152 }
153
154 config.touchscreen = mTouchScreenConfig;
155 config.keyboard = mKeyboardConfig;
156 config.navigation = mNavigationConfig;
157 }
158
159 public int getScancodeState(int code) {
160 return nativeGetScanCodeState(0, -1, code);
161 }
162
163 public int getScancodeState(int deviceId, int code) {
164 return nativeGetScanCodeState(deviceId, -1, code);
165 }
166
167 public int getTrackballScancodeState(int code) {
168 return nativeGetScanCodeState(-1, CLASS_TRACKBALL, code);
169 }
170
171 public int getDPadScancodeState(int code) {
172 return nativeGetScanCodeState(-1, CLASS_DPAD, code);
173 }
174
175 public int getKeycodeState(int code) {
176 return nativeGetKeyCodeState(0, -1, code);
177 }
178
179 public int getKeycodeState(int deviceId, int code) {
180 return nativeGetKeyCodeState(deviceId, -1, code);
181 }
182
183 public int getTrackballKeycodeState(int code) {
184 return nativeGetKeyCodeState(-1, CLASS_TRACKBALL, code);
185 }
186
187 public int getDPadKeycodeState(int code) {
188 return nativeGetKeyCodeState(-1, CLASS_DPAD, code);
189 }
190
191 public int getSwitchState(int sw) {
192 return nativeGetSwitchState(-1, -1, sw);
193 }
194
195 public int getSwitchState(int deviceId, int sw) {
196 return nativeGetSwitchState(deviceId, -1, sw);
197 }
198
199 public boolean hasKeys(int[] keyCodes, boolean[] keyExists) {
200 if (keyCodes == null) {
201 throw new IllegalArgumentException("keyCodes must not be null.");
202 }
203 if (keyExists == null) {
204 throw new IllegalArgumentException("keyExists must not be null.");
205 }
206
207 return nativeHasKeys(keyCodes, keyExists);
208 }
209
210 public void registerInputChannel(InputChannel inputChannel) {
211 if (inputChannel == null) {
212 throw new IllegalArgumentException("inputChannel must not be null.");
213 }
214
215 nativeRegisterInputChannel(inputChannel);
216 }
217
218 public void unregisterInputChannel(InputChannel inputChannel) {
219 if (inputChannel == null) {
220 throw new IllegalArgumentException("inputChannel must not be null.");
221 }
222
223 nativeUnregisterInputChannel(inputChannel);
224 }
225
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700226 /**
227 * Injects a key event into the event system on behalf of an application.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700228 * This method may block even if sync is false because it must wait for previous events
229 * to be dispatched before it can determine whether input event injection will be
230 * permitted based on the current input focus.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700231 * @param event The event to inject.
232 * @param nature The nature of the event.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700233 * @param injectorPid The pid of the injecting application.
234 * @param injectorUid The uid of the injecting application.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700235 * @param sync If true, waits for the event to be completed before returning.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700236 * @param timeoutMillis The injection timeout in milliseconds.
237 * @return One of the INPUT_EVENT_INJECTION_XXX constants.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700238 */
Jeff Brown7fbdc842010-06-17 20:52:56 -0700239 public int injectKeyEvent(KeyEvent event, int nature, int injectorPid, int injectorUid,
240 boolean sync, int timeoutMillis) {
241 if (event == null) {
242 throw new IllegalArgumentException("event must not be null");
243 }
244 if (injectorPid < 0 || injectorUid < 0) {
245 throw new IllegalArgumentException("injectorPid and injectorUid must not be negative.");
246 }
247 if (timeoutMillis <= 0) {
248 throw new IllegalArgumentException("timeoutMillis must be positive");
249 }
250
251 return nativeInjectKeyEvent(event, nature, injectorPid, injectorUid,
252 sync, timeoutMillis);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700253 }
254
255 /**
256 * Injects a motion event into the event system on behalf of an application.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700257 * This method may block even if sync is false because it must wait for previous events
258 * to be dispatched before it can determine whether input event injection will be
259 * permitted based on the current input focus.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700260 * @param event The event to inject.
261 * @param nature The nature of the event.
262 * @param sync If true, waits for the event to be completed before returning.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700263 * @param injectorPid The pid of the injecting application.
264 * @param injectorUid The uid of the injecting application.
265 * @param sync If true, waits for the event to be completed before returning.
266 * @param timeoutMillis The injection timeout in milliseconds.
267 * @return One of the INPUT_EVENT_INJECTION_XXX constants.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700268 */
Jeff Brown7fbdc842010-06-17 20:52:56 -0700269 public int injectMotionEvent(MotionEvent event, int nature, int injectorPid, int injectorUid,
270 boolean sync, int timeoutMillis) {
271 if (event == null) {
272 throw new IllegalArgumentException("event must not be null");
273 }
274 if (injectorPid < 0 || injectorUid < 0) {
275 throw new IllegalArgumentException("injectorPid and injectorUid must not be negative.");
276 }
277 if (timeoutMillis <= 0) {
278 throw new IllegalArgumentException("timeoutMillis must be positive");
279 }
280
281 return nativeInjectMotionEvent(event, nature, injectorPid, injectorUid,
282 sync, timeoutMillis);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700283 }
284
Jeff Brown349703e2010-06-22 01:27:15 -0700285 public void setInputWindows(InputWindow[] windows) {
286 nativeSetInputWindows(windows);
287 }
288
289 public void setFocusedApplication(InputApplication application) {
290 nativeSetFocusedApplication(application);
291 }
292
293 public void preemptInputDispatch() {
294 nativePreemptInputDispatch();
295 }
296
297 public void setInputDispatchMode(boolean enabled, boolean frozen) {
298 nativeSetInputDispatchMode(enabled, frozen);
299 }
300
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700301 public void dump(PrintWriter pw) {
302 // TODO
303 }
304
305 private static final class VirtualKeyDefinition {
306 public int scanCode;
307
308 // configured position data, specified in display coords
309 public int centerX;
310 public int centerY;
311 public int width;
312 public int height;
313 }
314
315 /*
316 * Callbacks from native.
317 */
318 private class Callbacks {
319 static final String TAG = "InputManager-Callbacks";
320
321 private static final boolean DEBUG_VIRTUAL_KEYS = false;
322 private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml";
323
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700324 @SuppressWarnings("unused")
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700325 public void virtualKeyDownFeedback() {
326 mWindowManagerService.mInputMonitor.virtualKeyDownFeedback();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700327 }
328
329 @SuppressWarnings("unused")
330 public void notifyConfigurationChanged(long whenNanos,
331 int touchScreenConfig, int keyboardConfig, int navigationConfig) {
332 mTouchScreenConfig = touchScreenConfig;
333 mKeyboardConfig = keyboardConfig;
334 mNavigationConfig = navigationConfig;
335
336 mWindowManagerService.sendNewConfiguration();
337 }
338
339 @SuppressWarnings("unused")
340 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700341 mWindowManagerService.mInputMonitor.notifyLidSwitchChanged(whenNanos, lidOpen);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700342 }
343
344 @SuppressWarnings("unused")
Jeff Brown7fbdc842010-06-17 20:52:56 -0700345 public void notifyInputChannelBroken(InputChannel inputChannel) {
Jeff Brown349703e2010-06-22 01:27:15 -0700346 mWindowManagerService.mInputMonitor.notifyInputChannelBroken(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700347 }
348
349 @SuppressWarnings("unused")
350 public long notifyInputChannelANR(InputChannel inputChannel) {
Jeff Brown349703e2010-06-22 01:27:15 -0700351 return mWindowManagerService.mInputMonitor.notifyInputChannelANR(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700352 }
353
354 @SuppressWarnings("unused")
355 public void notifyInputChannelRecoveredFromANR(InputChannel inputChannel) {
Jeff Brown349703e2010-06-22 01:27:15 -0700356 mWindowManagerService.mInputMonitor.notifyInputChannelRecoveredFromANR(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700357 }
358
359 @SuppressWarnings("unused")
Jeff Brown349703e2010-06-22 01:27:15 -0700360 public long notifyANR(Object token) {
361 return mWindowManagerService.mInputMonitor.notifyANR(token);
362 }
363
364 @SuppressWarnings("unused")
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700365 public int interceptKeyBeforeQueueing(long whenNanos, int keyCode, boolean down,
366 int policyFlags, boolean isScreenOn) {
367 return mWindowManagerService.mInputMonitor.interceptKeyBeforeQueueing(
368 whenNanos, keyCode, down, policyFlags, isScreenOn);
Jeff Brown349703e2010-06-22 01:27:15 -0700369 }
370
371 @SuppressWarnings("unused")
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700372 public boolean interceptKeyBeforeDispatching(InputChannel focus, int action,
373 int flags, int keyCode, int metaState, int repeatCount, int policyFlags) {
Jeff Brown349703e2010-06-22 01:27:15 -0700374 return mWindowManagerService.mInputMonitor.interceptKeyBeforeDispatching(focus,
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700375 action, flags, keyCode, metaState, repeatCount, policyFlags);
Jeff Brown349703e2010-06-22 01:27:15 -0700376 }
377
378 @SuppressWarnings("unused")
379 public boolean checkInjectEventsPermission(int injectorPid, int injectorUid) {
380 return mContext.checkPermission(
381 android.Manifest.permission.INJECT_EVENTS, injectorPid, injectorUid)
382 == PackageManager.PERMISSION_GRANTED;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700383 }
384
385 @SuppressWarnings("unused")
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700386 public void notifyAppSwitchComing() {
Jeff Brown349703e2010-06-22 01:27:15 -0700387 mWindowManagerService.mInputMonitor.notifyAppSwitchComing();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700388 }
389
390 @SuppressWarnings("unused")
391 public boolean filterTouchEvents() {
392 return mContext.getResources().getBoolean(
393 com.android.internal.R.bool.config_filterTouchEvents);
394 }
395
396 @SuppressWarnings("unused")
397 public boolean filterJumpyTouchEvents() {
398 return mContext.getResources().getBoolean(
399 com.android.internal.R.bool.config_filterJumpyTouchEvents);
400 }
401
402 @SuppressWarnings("unused")
403 public VirtualKeyDefinition[] getVirtualKeyDefinitions(String deviceName) {
404 ArrayList<VirtualKeyDefinition> keys = new ArrayList<VirtualKeyDefinition>();
405
406 try {
407 FileInputStream fis = new FileInputStream(
408 "/sys/board_properties/virtualkeys." + deviceName);
409 InputStreamReader isr = new InputStreamReader(fis);
410 BufferedReader br = new BufferedReader(isr, 2048);
411 String str = br.readLine();
412 if (str != null) {
413 String[] it = str.split(":");
414 if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "***** VIRTUAL KEYS: " + it);
415 final int N = it.length-6;
416 for (int i=0; i<=N; i+=6) {
417 if (!"0x01".equals(it[i])) {
418 Slog.w(TAG, "Unknown virtual key type at elem #" + i
419 + ": " + it[i]);
420 continue;
421 }
422 try {
423 VirtualKeyDefinition key = new VirtualKeyDefinition();
424 key.scanCode = Integer.parseInt(it[i+1]);
425 key.centerX = Integer.parseInt(it[i+2]);
426 key.centerY = Integer.parseInt(it[i+3]);
427 key.width = Integer.parseInt(it[i+4]);
428 key.height = Integer.parseInt(it[i+5]);
429 if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "Virtual key "
430 + key.scanCode + ": center=" + key.centerX + ","
431 + key.centerY + " size=" + key.width + "x"
432 + key.height);
433 keys.add(key);
434 } catch (NumberFormatException e) {
435 Slog.w(TAG, "Bad number at region " + i + " in: "
436 + str, e);
437 }
438 }
439 }
440 br.close();
441 } catch (FileNotFoundException e) {
442 Slog.i(TAG, "No virtual keys found");
443 } catch (IOException e) {
444 Slog.w(TAG, "Error reading virtual keys", e);
445 }
446
447 return keys.toArray(new VirtualKeyDefinition[keys.size()]);
448 }
449
450 @SuppressWarnings("unused")
451 public String[] getExcludedDeviceNames() {
452 ArrayList<String> names = new ArrayList<String>();
453
454 // Read partner-provided list of excluded input devices
455 XmlPullParser parser = null;
456 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
457 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
458 FileReader confreader = null;
459 try {
460 confreader = new FileReader(confFile);
461 parser = Xml.newPullParser();
462 parser.setInput(confreader);
463 XmlUtils.beginDocument(parser, "devices");
464
465 while (true) {
466 XmlUtils.nextElement(parser);
467 if (!"device".equals(parser.getName())) {
468 break;
469 }
470 String name = parser.getAttributeValue(null, "name");
471 if (name != null) {
472 names.add(name);
473 }
474 }
475 } catch (FileNotFoundException e) {
476 // It's ok if the file does not exist.
477 } catch (Exception e) {
478 Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
479 } finally {
480 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
481 }
482
483 return names.toArray(new String[names.size()]);
484 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700485 }
486}