blob: 2ba29141e18aa413259824c7ac69dd8dd1a05846 [file] [log] [blame]
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -070025import android.content.res.Configuration;
26import android.os.Environment;
27import android.os.LocalPowerManager;
28import android.os.PowerManager;
Jeff Brown46b9ac02010-04-22 18:58:52 -070029import android.util.Slog;
30import android.util.Xml;
31import android.view.InputChannel;
Jeff Brown46b9ac02010-04-22 18:58:52 -070032import android.view.KeyEvent;
33import android.view.MotionEvent;
Jeff Brown46b9ac02010-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;
59 private final WindowManagerPolicy mWindowManagerPolicy;
60 private final PowerManager mPowerManager;
61 private final PowerManagerService mPowerManagerService;
62
63 private int mTouchScreenConfig;
64 private int mKeyboardConfig;
65 private int mNavigationConfig;
66
67 private static native void nativeInit(Callbacks callbacks);
68 private static native void nativeStart();
69 private static native void nativeSetDisplaySize(int displayId, int width, int height);
70 private static native void nativeSetDisplayOrientation(int displayId, int rotation);
71
72 private static native int nativeGetScanCodeState(int deviceId, int deviceClasses,
73 int scanCode);
74 private static native int nativeGetKeyCodeState(int deviceId, int deviceClasses,
75 int keyCode);
76 private static native int nativeGetSwitchState(int deviceId, int deviceClasses,
77 int sw);
78 private static native boolean nativeHasKeys(int[] keyCodes, boolean[] keyExists);
79 private static native void nativeRegisterInputChannel(InputChannel inputChannel);
80 private static native void nativeUnregisterInputChannel(InputChannel inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -070081 private static native int nativeInjectKeyEvent(KeyEvent event, int nature,
82 int injectorPid, int injectorUid, boolean sync, int timeoutMillis);
83 private static native int nativeInjectMotionEvent(MotionEvent event, int nature,
84 int injectorPid, int injectorUid, boolean sync, int timeoutMillis);
Jeff Brown349703e2010-06-22 01:27:15 -070085 private static native void nativeSetInputWindows(InputWindow[] windows);
86 private static native void nativeSetInputDispatchMode(boolean enabled, boolean frozen);
87 private static native void nativeSetFocusedApplication(InputApplication application);
88 private static native void nativePreemptInputDispatch();
Jeff Brown46b9ac02010-04-22 18:58:52 -070089
90 // Device class as defined by EventHub.
91 private static final int CLASS_KEYBOARD = 0x00000001;
92 private static final int CLASS_ALPHAKEY = 0x00000002;
93 private static final int CLASS_TOUCHSCREEN = 0x00000004;
94 private static final int CLASS_TRACKBALL = 0x00000008;
95 private static final int CLASS_TOUCHSCREEN_MT = 0x00000010;
96 private static final int CLASS_DPAD = 0x00000020;
97
Jeff Brown7fbdc842010-06-17 20:52:56 -070098 // Input event injection constants defined in InputDispatcher.h.
99 static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0;
100 static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1;
101 static final int INPUT_EVENT_INJECTION_FAILED = 2;
102 static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3;
103
Jeff Brown46b9ac02010-04-22 18:58:52 -0700104 public InputManager(Context context,
105 WindowManagerService windowManagerService,
106 WindowManagerPolicy windowManagerPolicy,
107 PowerManager powerManager,
108 PowerManagerService powerManagerService) {
109 this.mContext = context;
110 this.mWindowManagerService = windowManagerService;
111 this.mWindowManagerPolicy = windowManagerPolicy;
112 this.mPowerManager = powerManager;
113 this.mPowerManagerService = powerManagerService;
114
115 this.mCallbacks = new Callbacks();
116
117 mTouchScreenConfig = Configuration.TOUCHSCREEN_NOTOUCH;
118 mKeyboardConfig = Configuration.KEYBOARD_NOKEYS;
119 mNavigationConfig = Configuration.NAVIGATION_NONAV;
120
121 init();
122 }
123
124 private void init() {
125 Slog.i(TAG, "Initializing input manager");
126 nativeInit(mCallbacks);
127 }
128
129 public void start() {
130 Slog.i(TAG, "Starting input manager");
131 nativeStart();
132 }
133
134 public void setDisplaySize(int displayId, int width, int height) {
135 if (width <= 0 || height <= 0) {
136 throw new IllegalArgumentException("Invalid display id or dimensions.");
137 }
138
139 Slog.i(TAG, "Setting display #" + displayId + " size to " + width + "x" + height);
140 nativeSetDisplaySize(displayId, width, height);
141 }
142
143 public void setDisplayOrientation(int displayId, int rotation) {
144 if (rotation < Surface.ROTATION_0 || rotation > Surface.ROTATION_270) {
145 throw new IllegalArgumentException("Invalid rotation.");
146 }
147
148 Slog.i(TAG, "Setting display #" + displayId + " orientation to " + rotation);
149 nativeSetDisplayOrientation(displayId, rotation);
150 }
151
152 public void getInputConfiguration(Configuration config) {
153 if (config == null) {
154 throw new IllegalArgumentException("config must not be null.");
155 }
156
157 config.touchscreen = mTouchScreenConfig;
158 config.keyboard = mKeyboardConfig;
159 config.navigation = mNavigationConfig;
160 }
161
162 public int getScancodeState(int code) {
163 return nativeGetScanCodeState(0, -1, code);
164 }
165
166 public int getScancodeState(int deviceId, int code) {
167 return nativeGetScanCodeState(deviceId, -1, code);
168 }
169
170 public int getTrackballScancodeState(int code) {
171 return nativeGetScanCodeState(-1, CLASS_TRACKBALL, code);
172 }
173
174 public int getDPadScancodeState(int code) {
175 return nativeGetScanCodeState(-1, CLASS_DPAD, code);
176 }
177
178 public int getKeycodeState(int code) {
179 return nativeGetKeyCodeState(0, -1, code);
180 }
181
182 public int getKeycodeState(int deviceId, int code) {
183 return nativeGetKeyCodeState(deviceId, -1, code);
184 }
185
186 public int getTrackballKeycodeState(int code) {
187 return nativeGetKeyCodeState(-1, CLASS_TRACKBALL, code);
188 }
189
190 public int getDPadKeycodeState(int code) {
191 return nativeGetKeyCodeState(-1, CLASS_DPAD, code);
192 }
193
194 public int getSwitchState(int sw) {
195 return nativeGetSwitchState(-1, -1, sw);
196 }
197
198 public int getSwitchState(int deviceId, int sw) {
199 return nativeGetSwitchState(deviceId, -1, sw);
200 }
201
202 public boolean hasKeys(int[] keyCodes, boolean[] keyExists) {
203 if (keyCodes == null) {
204 throw new IllegalArgumentException("keyCodes must not be null.");
205 }
206 if (keyExists == null) {
207 throw new IllegalArgumentException("keyExists must not be null.");
208 }
209
210 return nativeHasKeys(keyCodes, keyExists);
211 }
212
213 public void registerInputChannel(InputChannel inputChannel) {
214 if (inputChannel == null) {
215 throw new IllegalArgumentException("inputChannel must not be null.");
216 }
217
218 nativeRegisterInputChannel(inputChannel);
219 }
220
221 public void unregisterInputChannel(InputChannel inputChannel) {
222 if (inputChannel == null) {
223 throw new IllegalArgumentException("inputChannel must not be null.");
224 }
225
226 nativeUnregisterInputChannel(inputChannel);
227 }
228
Jeff Brown46b9ac02010-04-22 18:58:52 -0700229 /**
230 * Injects a key event into the event system on behalf of an application.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700231 * This method may block even if sync is false because it must wait for previous events
232 * to be dispatched before it can determine whether input event injection will be
233 * permitted based on the current input focus.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700234 * @param event The event to inject.
235 * @param nature The nature of the event.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700236 * @param injectorPid The pid of the injecting application.
237 * @param injectorUid The uid of the injecting application.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700238 * @param sync If true, waits for the event to be completed before returning.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700239 * @param timeoutMillis The injection timeout in milliseconds.
240 * @return One of the INPUT_EVENT_INJECTION_XXX constants.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700241 */
Jeff Brown7fbdc842010-06-17 20:52:56 -0700242 public int injectKeyEvent(KeyEvent event, int nature, int injectorPid, int injectorUid,
243 boolean sync, int timeoutMillis) {
244 if (event == null) {
245 throw new IllegalArgumentException("event must not be null");
246 }
247 if (injectorPid < 0 || injectorUid < 0) {
248 throw new IllegalArgumentException("injectorPid and injectorUid must not be negative.");
249 }
250 if (timeoutMillis <= 0) {
251 throw new IllegalArgumentException("timeoutMillis must be positive");
252 }
253
254 return nativeInjectKeyEvent(event, nature, injectorPid, injectorUid,
255 sync, timeoutMillis);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700256 }
257
258 /**
259 * Injects a motion event into the event system on behalf of an application.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700260 * This method may block even if sync is false because it must wait for previous events
261 * to be dispatched before it can determine whether input event injection will be
262 * permitted based on the current input focus.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700263 * @param event The event to inject.
264 * @param nature The nature of the event.
265 * @param sync If true, waits for the event to be completed before returning.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700266 * @param injectorPid The pid of the injecting application.
267 * @param injectorUid The uid of the injecting application.
268 * @param sync If true, waits for the event to be completed before returning.
269 * @param timeoutMillis The injection timeout in milliseconds.
270 * @return One of the INPUT_EVENT_INJECTION_XXX constants.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700271 */
Jeff Brown7fbdc842010-06-17 20:52:56 -0700272 public int injectMotionEvent(MotionEvent event, int nature, int injectorPid, int injectorUid,
273 boolean sync, int timeoutMillis) {
274 if (event == null) {
275 throw new IllegalArgumentException("event must not be null");
276 }
277 if (injectorPid < 0 || injectorUid < 0) {
278 throw new IllegalArgumentException("injectorPid and injectorUid must not be negative.");
279 }
280 if (timeoutMillis <= 0) {
281 throw new IllegalArgumentException("timeoutMillis must be positive");
282 }
283
284 return nativeInjectMotionEvent(event, nature, injectorPid, injectorUid,
285 sync, timeoutMillis);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700286 }
287
Jeff Brown349703e2010-06-22 01:27:15 -0700288 public void setInputWindows(InputWindow[] windows) {
289 nativeSetInputWindows(windows);
290 }
291
292 public void setFocusedApplication(InputApplication application) {
293 nativeSetFocusedApplication(application);
294 }
295
296 public void preemptInputDispatch() {
297 nativePreemptInputDispatch();
298 }
299
300 public void setInputDispatchMode(boolean enabled, boolean frozen) {
301 nativeSetInputDispatchMode(enabled, frozen);
302 }
303
Jeff Brown46b9ac02010-04-22 18:58:52 -0700304 public void dump(PrintWriter pw) {
305 // TODO
306 }
307
308 private static final class VirtualKeyDefinition {
309 public int scanCode;
310
311 // configured position data, specified in display coords
312 public int centerX;
313 public int centerY;
314 public int width;
315 public int height;
316 }
317
318 /*
319 * Callbacks from native.
320 */
321 private class Callbacks {
322 static final String TAG = "InputManager-Callbacks";
323
324 private static final boolean DEBUG_VIRTUAL_KEYS = false;
325 private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml";
326
Jeff Brown46b9ac02010-04-22 18:58:52 -0700327 @SuppressWarnings("unused")
328 public boolean isScreenOn() {
329 return mPowerManagerService.isScreenOn();
330 }
331
332 @SuppressWarnings("unused")
333 public boolean isScreenBright() {
334 return mPowerManagerService.isScreenBright();
335 }
336
337 @SuppressWarnings("unused")
338 public void virtualKeyFeedback(long whenNanos, int deviceId, int action, int flags,
339 int keyCode, int scanCode, int metaState, long downTimeNanos) {
340 KeyEvent keyEvent = new KeyEvent(downTimeNanos / 1000000,
341 whenNanos / 1000000, action, keyCode, 0, metaState, scanCode, deviceId,
342 flags);
343
344 mWindowManagerService.virtualKeyFeedback(keyEvent);
345 }
346
347 @SuppressWarnings("unused")
348 public void notifyConfigurationChanged(long whenNanos,
349 int touchScreenConfig, int keyboardConfig, int navigationConfig) {
350 mTouchScreenConfig = touchScreenConfig;
351 mKeyboardConfig = keyboardConfig;
352 mNavigationConfig = navigationConfig;
353
354 mWindowManagerService.sendNewConfiguration();
355 }
356
357 @SuppressWarnings("unused")
358 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
359 mWindowManagerPolicy.notifyLidSwitchChanged(whenNanos, lidOpen);
360 }
361
362 @SuppressWarnings("unused")
Jeff Brown7fbdc842010-06-17 20:52:56 -0700363 public void notifyInputChannelBroken(InputChannel inputChannel) {
Jeff Brown349703e2010-06-22 01:27:15 -0700364 mWindowManagerService.mInputMonitor.notifyInputChannelBroken(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700365 }
366
367 @SuppressWarnings("unused")
368 public long notifyInputChannelANR(InputChannel inputChannel) {
Jeff Brown349703e2010-06-22 01:27:15 -0700369 return mWindowManagerService.mInputMonitor.notifyInputChannelANR(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700370 }
371
372 @SuppressWarnings("unused")
373 public void notifyInputChannelRecoveredFromANR(InputChannel inputChannel) {
Jeff Brown349703e2010-06-22 01:27:15 -0700374 mWindowManagerService.mInputMonitor.notifyInputChannelRecoveredFromANR(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700375 }
376
377 @SuppressWarnings("unused")
Jeff Brown349703e2010-06-22 01:27:15 -0700378 public long notifyANR(Object token) {
379 return mWindowManagerService.mInputMonitor.notifyANR(token);
380 }
381
382 @SuppressWarnings("unused")
383 public int interceptKeyBeforeQueueing(int deviceId, int type, int scanCode,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700384 int keyCode, int policyFlags, int value, long whenNanos, boolean isScreenOn) {
Jeff Brown349703e2010-06-22 01:27:15 -0700385 return mWindowManagerService.mInputMonitor.interceptKeyBeforeQueueing(deviceId, type,
386 scanCode, keyCode, policyFlags, value, whenNanos, isScreenOn);
387 }
388
389 @SuppressWarnings("unused")
390 public boolean interceptKeyBeforeDispatching(InputChannel focus, int keyCode,
391 int metaState, boolean down, int repeatCount, int policyFlags) {
392 return mWindowManagerService.mInputMonitor.interceptKeyBeforeDispatching(focus,
393 keyCode, metaState, down, repeatCount, policyFlags);
394 }
395
396 @SuppressWarnings("unused")
397 public boolean checkInjectEventsPermission(int injectorPid, int injectorUid) {
398 return mContext.checkPermission(
399 android.Manifest.permission.INJECT_EVENTS, injectorPid, injectorUid)
400 == PackageManager.PERMISSION_GRANTED;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700401 }
402
403 @SuppressWarnings("unused")
404 public void goToSleep(long whenNanos) {
405 long when = whenNanos / 1000000;
406 mPowerManager.goToSleep(when);
407 }
408
409 @SuppressWarnings("unused")
Jeff Brown349703e2010-06-22 01:27:15 -0700410 public void pokeUserActivity(long eventTimeNanos, int eventType) {
411 long eventTime = eventTimeNanos / 1000000;
412 mPowerManagerService.userActivity(eventTime, false, eventType, false);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700413 }
414
415 @SuppressWarnings("unused")
416 public void notifyAppSwitchComing() {
Jeff Brown349703e2010-06-22 01:27:15 -0700417 mWindowManagerService.mInputMonitor.notifyAppSwitchComing();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700418 }
419
420 @SuppressWarnings("unused")
421 public boolean filterTouchEvents() {
422 return mContext.getResources().getBoolean(
423 com.android.internal.R.bool.config_filterTouchEvents);
424 }
425
426 @SuppressWarnings("unused")
427 public boolean filterJumpyTouchEvents() {
428 return mContext.getResources().getBoolean(
429 com.android.internal.R.bool.config_filterJumpyTouchEvents);
430 }
431
432 @SuppressWarnings("unused")
433 public VirtualKeyDefinition[] getVirtualKeyDefinitions(String deviceName) {
434 ArrayList<VirtualKeyDefinition> keys = new ArrayList<VirtualKeyDefinition>();
435
436 try {
437 FileInputStream fis = new FileInputStream(
438 "/sys/board_properties/virtualkeys." + deviceName);
439 InputStreamReader isr = new InputStreamReader(fis);
440 BufferedReader br = new BufferedReader(isr, 2048);
441 String str = br.readLine();
442 if (str != null) {
443 String[] it = str.split(":");
444 if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "***** VIRTUAL KEYS: " + it);
445 final int N = it.length-6;
446 for (int i=0; i<=N; i+=6) {
447 if (!"0x01".equals(it[i])) {
448 Slog.w(TAG, "Unknown virtual key type at elem #" + i
449 + ": " + it[i]);
450 continue;
451 }
452 try {
453 VirtualKeyDefinition key = new VirtualKeyDefinition();
454 key.scanCode = Integer.parseInt(it[i+1]);
455 key.centerX = Integer.parseInt(it[i+2]);
456 key.centerY = Integer.parseInt(it[i+3]);
457 key.width = Integer.parseInt(it[i+4]);
458 key.height = Integer.parseInt(it[i+5]);
459 if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "Virtual key "
460 + key.scanCode + ": center=" + key.centerX + ","
461 + key.centerY + " size=" + key.width + "x"
462 + key.height);
463 keys.add(key);
464 } catch (NumberFormatException e) {
465 Slog.w(TAG, "Bad number at region " + i + " in: "
466 + str, e);
467 }
468 }
469 }
470 br.close();
471 } catch (FileNotFoundException e) {
472 Slog.i(TAG, "No virtual keys found");
473 } catch (IOException e) {
474 Slog.w(TAG, "Error reading virtual keys", e);
475 }
476
477 return keys.toArray(new VirtualKeyDefinition[keys.size()]);
478 }
479
480 @SuppressWarnings("unused")
481 public String[] getExcludedDeviceNames() {
482 ArrayList<String> names = new ArrayList<String>();
483
484 // Read partner-provided list of excluded input devices
485 XmlPullParser parser = null;
486 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
487 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
488 FileReader confreader = null;
489 try {
490 confreader = new FileReader(confFile);
491 parser = Xml.newPullParser();
492 parser.setInput(confreader);
493 XmlUtils.beginDocument(parser, "devices");
494
495 while (true) {
496 XmlUtils.nextElement(parser);
497 if (!"device".equals(parser.getName())) {
498 break;
499 }
500 String name = parser.getAttributeValue(null, "name");
501 if (name != null) {
502 names.add(name);
503 }
504 }
505 } catch (FileNotFoundException e) {
506 // It's ok if the file does not exist.
507 } catch (Exception e) {
508 Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
509 } finally {
510 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
511 }
512
513 return names.toArray(new String[names.size()]);
514 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700515 }
516}