blob: 3c60a98e2be8acdb132f4b3324e18f891bfe18d3 [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 Brownc5ed5912010-07-14 18:48:53 -070080 private static native int nativeInjectKeyEvent(KeyEvent event,
Jeff Brown7fbdc842010-06-17 20:52:56 -070081 int injectorPid, int injectorUid, boolean sync, int timeoutMillis);
Jeff Brownc5ed5912010-07-14 18:48:53 -070082 private static native int nativeInjectMotionEvent(MotionEvent event,
Jeff Brown7fbdc842010-06-17 20:52:56 -070083 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.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700232 * @param injectorPid The pid of the injecting application.
233 * @param injectorUid The uid of the injecting application.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700234 * @param sync If true, waits for the event to be completed before returning.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700235 * @param timeoutMillis The injection timeout in milliseconds.
236 * @return One of the INPUT_EVENT_INJECTION_XXX constants.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700237 */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700238 public int injectKeyEvent(KeyEvent event, int injectorPid, int injectorUid,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700239 boolean sync, int timeoutMillis) {
240 if (event == null) {
241 throw new IllegalArgumentException("event must not be null");
242 }
243 if (injectorPid < 0 || injectorUid < 0) {
244 throw new IllegalArgumentException("injectorPid and injectorUid must not be negative.");
245 }
246 if (timeoutMillis <= 0) {
247 throw new IllegalArgumentException("timeoutMillis must be positive");
248 }
249
Jeff Brownc5ed5912010-07-14 18:48:53 -0700250 return nativeInjectKeyEvent(event, injectorPid, injectorUid,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700251 sync, timeoutMillis);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700252 }
253
254 /**
255 * Injects a motion event into the event system on behalf of an application.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700256 * This method may block even if sync is false because it must wait for previous events
257 * to be dispatched before it can determine whether input event injection will be
258 * permitted based on the current input focus.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700259 * @param event The event to inject.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700260 * @param sync If true, waits for the event to be completed before returning.
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.
263 * @param sync If true, waits for the event to be completed before returning.
264 * @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 Brownc5ed5912010-07-14 18:48:53 -0700267 public int injectMotionEvent(MotionEvent event, int injectorPid, int injectorUid,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700268 boolean sync, int timeoutMillis) {
269 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 }
278
Jeff Brownc5ed5912010-07-14 18:48:53 -0700279 return nativeInjectMotionEvent(event, injectorPid, injectorUid,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700280 sync, timeoutMillis);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700281 }
282
Jeff Brown349703e2010-06-22 01:27:15 -0700283 public void setInputWindows(InputWindow[] windows) {
284 nativeSetInputWindows(windows);
285 }
286
287 public void setFocusedApplication(InputApplication application) {
288 nativeSetFocusedApplication(application);
289 }
290
291 public void preemptInputDispatch() {
292 nativePreemptInputDispatch();
293 }
294
295 public void setInputDispatchMode(boolean enabled, boolean frozen) {
296 nativeSetInputDispatchMode(enabled, frozen);
297 }
298
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700299 public void dump(PrintWriter pw) {
300 // TODO
301 }
302
303 private static final class VirtualKeyDefinition {
304 public int scanCode;
305
306 // configured position data, specified in display coords
307 public int centerX;
308 public int centerY;
309 public int width;
310 public int height;
311 }
312
313 /*
314 * Callbacks from native.
315 */
316 private class Callbacks {
317 static final String TAG = "InputManager-Callbacks";
318
319 private static final boolean DEBUG_VIRTUAL_KEYS = false;
320 private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml";
321
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700322 @SuppressWarnings("unused")
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700323 public void virtualKeyDownFeedback() {
324 mWindowManagerService.mInputMonitor.virtualKeyDownFeedback();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700325 }
326
327 @SuppressWarnings("unused")
328 public void notifyConfigurationChanged(long whenNanos,
329 int touchScreenConfig, int keyboardConfig, int navigationConfig) {
330 mTouchScreenConfig = touchScreenConfig;
331 mKeyboardConfig = keyboardConfig;
332 mNavigationConfig = navigationConfig;
333
334 mWindowManagerService.sendNewConfiguration();
335 }
336
337 @SuppressWarnings("unused")
338 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700339 mWindowManagerService.mInputMonitor.notifyLidSwitchChanged(whenNanos, lidOpen);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700340 }
341
342 @SuppressWarnings("unused")
Jeff Brown7fbdc842010-06-17 20:52:56 -0700343 public void notifyInputChannelBroken(InputChannel inputChannel) {
Jeff Brown349703e2010-06-22 01:27:15 -0700344 mWindowManagerService.mInputMonitor.notifyInputChannelBroken(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700345 }
346
347 @SuppressWarnings("unused")
348 public long notifyInputChannelANR(InputChannel inputChannel) {
Jeff Brown349703e2010-06-22 01:27:15 -0700349 return mWindowManagerService.mInputMonitor.notifyInputChannelANR(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700350 }
351
352 @SuppressWarnings("unused")
353 public void notifyInputChannelRecoveredFromANR(InputChannel inputChannel) {
Jeff Brown349703e2010-06-22 01:27:15 -0700354 mWindowManagerService.mInputMonitor.notifyInputChannelRecoveredFromANR(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700355 }
356
357 @SuppressWarnings("unused")
Jeff Brown349703e2010-06-22 01:27:15 -0700358 public long notifyANR(Object token) {
359 return mWindowManagerService.mInputMonitor.notifyANR(token);
360 }
361
362 @SuppressWarnings("unused")
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700363 public int interceptKeyBeforeQueueing(long whenNanos, int keyCode, boolean down,
364 int policyFlags, boolean isScreenOn) {
365 return mWindowManagerService.mInputMonitor.interceptKeyBeforeQueueing(
366 whenNanos, keyCode, down, policyFlags, isScreenOn);
Jeff Brown349703e2010-06-22 01:27:15 -0700367 }
368
369 @SuppressWarnings("unused")
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700370 public boolean interceptKeyBeforeDispatching(InputChannel focus, int action,
371 int flags, int keyCode, int metaState, int repeatCount, int policyFlags) {
Jeff Brown349703e2010-06-22 01:27:15 -0700372 return mWindowManagerService.mInputMonitor.interceptKeyBeforeDispatching(focus,
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700373 action, flags, keyCode, metaState, repeatCount, policyFlags);
Jeff Brown349703e2010-06-22 01:27:15 -0700374 }
375
376 @SuppressWarnings("unused")
377 public boolean checkInjectEventsPermission(int injectorPid, int injectorUid) {
378 return mContext.checkPermission(
379 android.Manifest.permission.INJECT_EVENTS, injectorPid, injectorUid)
380 == PackageManager.PERMISSION_GRANTED;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700381 }
382
383 @SuppressWarnings("unused")
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700384 public void notifyAppSwitchComing() {
Jeff Brown349703e2010-06-22 01:27:15 -0700385 mWindowManagerService.mInputMonitor.notifyAppSwitchComing();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700386 }
387
388 @SuppressWarnings("unused")
389 public boolean filterTouchEvents() {
390 return mContext.getResources().getBoolean(
391 com.android.internal.R.bool.config_filterTouchEvents);
392 }
393
394 @SuppressWarnings("unused")
395 public boolean filterJumpyTouchEvents() {
396 return mContext.getResources().getBoolean(
397 com.android.internal.R.bool.config_filterJumpyTouchEvents);
398 }
399
400 @SuppressWarnings("unused")
401 public VirtualKeyDefinition[] getVirtualKeyDefinitions(String deviceName) {
402 ArrayList<VirtualKeyDefinition> keys = new ArrayList<VirtualKeyDefinition>();
403
404 try {
405 FileInputStream fis = new FileInputStream(
406 "/sys/board_properties/virtualkeys." + deviceName);
407 InputStreamReader isr = new InputStreamReader(fis);
408 BufferedReader br = new BufferedReader(isr, 2048);
409 String str = br.readLine();
410 if (str != null) {
411 String[] it = str.split(":");
412 if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "***** VIRTUAL KEYS: " + it);
413 final int N = it.length-6;
414 for (int i=0; i<=N; i+=6) {
415 if (!"0x01".equals(it[i])) {
416 Slog.w(TAG, "Unknown virtual key type at elem #" + i
417 + ": " + it[i]);
418 continue;
419 }
420 try {
421 VirtualKeyDefinition key = new VirtualKeyDefinition();
422 key.scanCode = Integer.parseInt(it[i+1]);
423 key.centerX = Integer.parseInt(it[i+2]);
424 key.centerY = Integer.parseInt(it[i+3]);
425 key.width = Integer.parseInt(it[i+4]);
426 key.height = Integer.parseInt(it[i+5]);
427 if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "Virtual key "
428 + key.scanCode + ": center=" + key.centerX + ","
429 + key.centerY + " size=" + key.width + "x"
430 + key.height);
431 keys.add(key);
432 } catch (NumberFormatException e) {
433 Slog.w(TAG, "Bad number at region " + i + " in: "
434 + str, e);
435 }
436 }
437 }
438 br.close();
439 } catch (FileNotFoundException e) {
440 Slog.i(TAG, "No virtual keys found");
441 } catch (IOException e) {
442 Slog.w(TAG, "Error reading virtual keys", e);
443 }
444
445 return keys.toArray(new VirtualKeyDefinition[keys.size()]);
446 }
447
448 @SuppressWarnings("unused")
449 public String[] getExcludedDeviceNames() {
450 ArrayList<String> names = new ArrayList<String>();
451
452 // Read partner-provided list of excluded input devices
453 XmlPullParser parser = null;
454 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
455 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
456 FileReader confreader = null;
457 try {
458 confreader = new FileReader(confFile);
459 parser = Xml.newPullParser();
460 parser.setInput(confreader);
461 XmlUtils.beginDocument(parser, "devices");
462
463 while (true) {
464 XmlUtils.nextElement(parser);
465 if (!"device".equals(parser.getName())) {
466 break;
467 }
468 String name = parser.getAttributeValue(null, "name");
469 if (name != null) {
470 names.add(name);
471 }
472 }
473 } catch (FileNotFoundException e) {
474 // It's ok if the file does not exist.
475 } catch (Exception e) {
476 Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
477 } finally {
478 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
479 }
480
481 return names.toArray(new String[names.size()]);
482 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700483 }
484}