blob: 91951233fca138f62bb2bfebd20f3ccaff500f27 [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 Brown6ec402b2010-07-28 15:48:59 -070032import android.view.InputEvent;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070033import android.view.KeyEvent;
34import android.view.MotionEvent;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070035import android.view.Surface;
36import android.view.WindowManagerPolicy;
37
38import java.io.BufferedReader;
39import java.io.File;
40import java.io.FileInputStream;
41import java.io.FileNotFoundException;
42import java.io.FileReader;
43import java.io.IOException;
44import java.io.InputStreamReader;
45import java.io.PrintWriter;
46import java.util.ArrayList;
47
48/*
49 * Wraps the C++ InputManager and provides its callbacks.
50 *
51 * XXX Tempted to promote this to a first-class service, ie. InputManagerService, to
52 * improve separation of concerns with respect to the window manager.
53 */
54public class InputManager {
55 static final String TAG = "InputManager";
56
57 private final Callbacks mCallbacks;
58 private final Context mContext;
59 private final WindowManagerService mWindowManagerService;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070060
61 private int mTouchScreenConfig;
62 private int mKeyboardConfig;
63 private int mNavigationConfig;
64
65 private static native void nativeInit(Callbacks callbacks);
66 private static native void nativeStart();
67 private static native void nativeSetDisplaySize(int displayId, int width, int height);
68 private static native void nativeSetDisplayOrientation(int displayId, int rotation);
69
Jeff Brown6d0fec22010-07-23 21:28:06 -070070 private static native int nativeGetScanCodeState(int deviceId, int sourceMask,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070071 int scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -070072 private static native int nativeGetKeyCodeState(int deviceId, int sourceMask,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070073 int keyCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -070074 private static native int nativeGetSwitchState(int deviceId, int sourceMask,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070075 int sw);
Jeff Brown6d0fec22010-07-23 21:28:06 -070076 private static native boolean nativeHasKeys(int deviceId, int sourceMask,
77 int[] keyCodes, boolean[] keyExists);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070078 private static native void nativeRegisterInputChannel(InputChannel inputChannel);
79 private static native void nativeUnregisterInputChannel(InputChannel inputChannel);
Jeff Brown6ec402b2010-07-28 15:48:59 -070080 private static native int nativeInjectInputEvent(InputEvent event,
81 int injectorPid, int injectorUid, int syncMode, int timeoutMillis);
Jeff Brown349703e2010-06-22 01:27:15 -070082 private static native void nativeSetInputWindows(InputWindow[] windows);
83 private static native void nativeSetInputDispatchMode(boolean enabled, boolean frozen);
84 private static native void nativeSetFocusedApplication(InputApplication application);
85 private static native void nativePreemptInputDispatch();
Jeff Browne33348b2010-07-15 23:54:05 -070086 private static native String nativeDump();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070087
Jeff Brown7fbdc842010-06-17 20:52:56 -070088 // Input event injection constants defined in InputDispatcher.h.
89 static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0;
90 static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1;
91 static final int INPUT_EVENT_INJECTION_FAILED = 2;
92 static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3;
93
Jeff Brown6ec402b2010-07-28 15:48:59 -070094 // Input event injection synchronization modes defined in InputDispatcher.h
95 static final int INPUT_EVENT_INJECTION_SYNC_NONE = 0;
96 static final int INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1;
97 static final int INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISH = 2;
98
Jeff Brown6d0fec22010-07-23 21:28:06 -070099 // Key states (may be returned by queries about the current state of a
100 // particular key code, scan code or switch).
101
102 /** The key state is unknown or the requested key itself is not supported. */
103 public static final int KEY_STATE_UNKNOWN = -1;
104
105 /** The key is up. /*/
106 public static final int KEY_STATE_UP = 0;
107
108 /** The key is down. */
109 public static final int KEY_STATE_DOWN = 1;
110
111 /** The key is down but is a virtual key press that is being emulated by the system. */
112 public static final int KEY_STATE_VIRTUAL = 2;
113
Jeff Browne33348b2010-07-15 23:54:05 -0700114 public InputManager(Context context, WindowManagerService windowManagerService) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700115 this.mContext = context;
116 this.mWindowManagerService = windowManagerService;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700117
118 this.mCallbacks = new Callbacks();
119
120 mTouchScreenConfig = Configuration.TOUCHSCREEN_NOTOUCH;
121 mKeyboardConfig = Configuration.KEYBOARD_NOKEYS;
122 mNavigationConfig = Configuration.NAVIGATION_NONAV;
123
124 init();
125 }
126
127 private void init() {
128 Slog.i(TAG, "Initializing input manager");
129 nativeInit(mCallbacks);
130 }
131
132 public void start() {
133 Slog.i(TAG, "Starting input manager");
134 nativeStart();
135 }
136
137 public void setDisplaySize(int displayId, int width, int height) {
138 if (width <= 0 || height <= 0) {
139 throw new IllegalArgumentException("Invalid display id or dimensions.");
140 }
141
142 Slog.i(TAG, "Setting display #" + displayId + " size to " + width + "x" + height);
143 nativeSetDisplaySize(displayId, width, height);
144 }
145
146 public void setDisplayOrientation(int displayId, int rotation) {
147 if (rotation < Surface.ROTATION_0 || rotation > Surface.ROTATION_270) {
148 throw new IllegalArgumentException("Invalid rotation.");
149 }
150
151 Slog.i(TAG, "Setting display #" + displayId + " orientation to " + rotation);
152 nativeSetDisplayOrientation(displayId, rotation);
153 }
154
155 public void getInputConfiguration(Configuration config) {
156 if (config == null) {
157 throw new IllegalArgumentException("config must not be null.");
158 }
159
160 config.touchscreen = mTouchScreenConfig;
161 config.keyboard = mKeyboardConfig;
162 config.navigation = mNavigationConfig;
163 }
164
Jeff Brown6d0fec22010-07-23 21:28:06 -0700165 /**
166 * Gets the current state of a key or button by key code.
167 * @param deviceId The input device id, or -1 to consult all devices.
168 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
169 * consider all input sources. An input device is consulted if at least one of its
170 * non-class input source bits matches the specified source mask.
171 * @param keyCode The key code to check.
172 * @return The key state.
173 */
174 public int getKeyCodeState(int deviceId, int sourceMask, int keyCode) {
175 return nativeGetKeyCodeState(deviceId, sourceMask, keyCode);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700176 }
177
Jeff Brown6d0fec22010-07-23 21:28:06 -0700178 /**
179 * Gets the current state of a key or button by scan code.
180 * @param deviceId The input device id, or -1 to consult all devices.
181 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
182 * consider all input sources. An input device is consulted if at least one of its
183 * non-class input source bits matches the specified source mask.
184 * @param scanCode The scan code to check.
185 * @return The key state.
186 */
187 public int getScanCodeState(int deviceId, int sourceMask, int scanCode) {
188 return nativeGetScanCodeState(deviceId, sourceMask, scanCode);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700189 }
190
Jeff Brown6d0fec22010-07-23 21:28:06 -0700191 /**
192 * Gets the current state of a switch by switch code.
193 * @param deviceId The input device id, or -1 to consult all devices.
194 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
195 * consider all input sources. An input device is consulted if at least one of its
196 * non-class input source bits matches the specified source mask.
197 * @param switchCode The switch code to check.
198 * @return The switch state.
199 */
200 public int getSwitchState(int deviceId, int sourceMask, int switchCode) {
201 return nativeGetSwitchState(deviceId, sourceMask, switchCode);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700202 }
203
Jeff Brown6d0fec22010-07-23 21:28:06 -0700204 /**
205 * Determines whether the specified key codes are supported by a particular device.
206 * @param deviceId The input device id, or -1 to consult all devices.
207 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
208 * consider all input sources. An input device is consulted if at least one of its
209 * non-class input source bits matches the specified source mask.
210 * @param keyCodes The array of key codes to check.
211 * @param keyExists An array at least as large as keyCodes whose entries will be set
212 * to true or false based on the presence or absence of support for the corresponding
213 * key codes.
214 * @return True if the lookup was successful, false otherwise.
215 */
216 public boolean hasKeys(int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700217 if (keyCodes == null) {
218 throw new IllegalArgumentException("keyCodes must not be null.");
219 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700220 if (keyExists == null || keyExists.length < keyCodes.length) {
221 throw new IllegalArgumentException("keyExists must not be null and must be at "
222 + "least as large as keyCodes.");
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700223 }
224
Jeff Brown6d0fec22010-07-23 21:28:06 -0700225 return nativeHasKeys(deviceId, sourceMask, keyCodes, keyExists);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700226 }
227
228 public void registerInputChannel(InputChannel inputChannel) {
229 if (inputChannel == null) {
230 throw new IllegalArgumentException("inputChannel must not be null.");
231 }
232
233 nativeRegisterInputChannel(inputChannel);
234 }
235
236 public void unregisterInputChannel(InputChannel inputChannel) {
237 if (inputChannel == null) {
238 throw new IllegalArgumentException("inputChannel must not be null.");
239 }
240
241 nativeUnregisterInputChannel(inputChannel);
242 }
243
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700244 /**
Jeff Brown6ec402b2010-07-28 15:48:59 -0700245 * Injects an input event into the event system on behalf of an application.
246 * The synchronization mode determines whether the method blocks while waiting for
247 * input injection to proceed.
248 *
249 * {@link #INPUT_EVENT_INJECTION_SYNC_NONE} never blocks. Injection is asynchronous and
250 * is assumed always to be successful.
251 *
252 * {@link #INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT} waits for previous events to be
253 * dispatched so that the input dispatcher can determine whether input event injection will
254 * be permitted based on the current input focus. Does not wait for the input event to
255 * finish processing.
256 *
257 * {@link #INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISH} waits for the input event to
258 * be completely processed.
259 *
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700260 * @param event The event to inject.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700261 * @param injectorPid The pid of the injecting application.
262 * @param injectorUid The uid of the injecting application.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700263 * @param syncMode The synchronization mode.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700264 * @param timeoutMillis The injection timeout in milliseconds.
265 * @return One of the INPUT_EVENT_INJECTION_XXX constants.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700266 */
Jeff Brown6ec402b2010-07-28 15:48:59 -0700267 public int injectInputEvent(InputEvent event, int injectorPid, int injectorUid,
268 int syncMode, int timeoutMillis) {
Jeff Brown7fbdc842010-06-17 20:52:56 -0700269 if (event == null) {
270 throw new IllegalArgumentException("event must not be null");
271 }
272 if (injectorPid < 0 || injectorUid < 0) {
273 throw new IllegalArgumentException("injectorPid and injectorUid must not be negative.");
274 }
275 if (timeoutMillis <= 0) {
276 throw new IllegalArgumentException("timeoutMillis must be positive");
277 }
Jeff Brown6ec402b2010-07-28 15:48:59 -0700278
279 return nativeInjectInputEvent(event, injectorPid, injectorUid, syncMode, timeoutMillis);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700280 }
281
Jeff Brown349703e2010-06-22 01:27:15 -0700282 public void setInputWindows(InputWindow[] windows) {
283 nativeSetInputWindows(windows);
284 }
285
286 public void setFocusedApplication(InputApplication application) {
287 nativeSetFocusedApplication(application);
288 }
289
290 public void preemptInputDispatch() {
291 nativePreemptInputDispatch();
292 }
293
294 public void setInputDispatchMode(boolean enabled, boolean frozen) {
295 nativeSetInputDispatchMode(enabled, frozen);
296 }
297
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700298 public void dump(PrintWriter pw) {
Jeff Browne33348b2010-07-15 23:54:05 -0700299 String dumpStr = nativeDump();
300 if (dumpStr != null) {
301 pw.println(dumpStr);
302 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700303 }
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}