blob: b4f46ab55a979997245815aa0a80d6cbdf530c40 [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
60 private int mTouchScreenConfig;
61 private int mKeyboardConfig;
62 private int mNavigationConfig;
63
64 private static native void nativeInit(Callbacks callbacks);
65 private static native void nativeStart();
66 private static native void nativeSetDisplaySize(int displayId, int width, int height);
67 private static native void nativeSetDisplayOrientation(int displayId, int rotation);
68
69 private static native int nativeGetScanCodeState(int deviceId, int deviceClasses,
70 int scanCode);
71 private static native int nativeGetKeyCodeState(int deviceId, int deviceClasses,
72 int keyCode);
73 private static native int nativeGetSwitchState(int deviceId, int deviceClasses,
74 int sw);
75 private static native boolean nativeHasKeys(int[] keyCodes, boolean[] keyExists);
76 private static native void nativeRegisterInputChannel(InputChannel inputChannel);
77 private static native void nativeUnregisterInputChannel(InputChannel inputChannel);
Jeff Brownc5ed5912010-07-14 18:48:53 -070078 private static native int nativeInjectKeyEvent(KeyEvent event,
Jeff Brown7fbdc842010-06-17 20:52:56 -070079 int injectorPid, int injectorUid, boolean sync, int timeoutMillis);
Jeff Brownc5ed5912010-07-14 18:48:53 -070080 private static native int nativeInjectMotionEvent(MotionEvent event,
Jeff Brown7fbdc842010-06-17 20:52:56 -070081 int injectorPid, int injectorUid, boolean sync, 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
88 // Device class as defined by EventHub.
89 private static final int CLASS_KEYBOARD = 0x00000001;
90 private static final int CLASS_ALPHAKEY = 0x00000002;
91 private static final int CLASS_TOUCHSCREEN = 0x00000004;
92 private static final int CLASS_TRACKBALL = 0x00000008;
93 private static final int CLASS_TOUCHSCREEN_MT = 0x00000010;
94 private static final int CLASS_DPAD = 0x00000020;
95
Jeff Brown7fbdc842010-06-17 20:52:56 -070096 // Input event injection constants defined in InputDispatcher.h.
97 static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0;
98 static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1;
99 static final int INPUT_EVENT_INJECTION_FAILED = 2;
100 static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3;
101
Jeff Browne33348b2010-07-15 23:54:05 -0700102 public InputManager(Context context, WindowManagerService windowManagerService) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700103 this.mContext = context;
104 this.mWindowManagerService = windowManagerService;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700105
106 this.mCallbacks = new Callbacks();
107
108 mTouchScreenConfig = Configuration.TOUCHSCREEN_NOTOUCH;
109 mKeyboardConfig = Configuration.KEYBOARD_NOKEYS;
110 mNavigationConfig = Configuration.NAVIGATION_NONAV;
111
112 init();
113 }
114
115 private void init() {
116 Slog.i(TAG, "Initializing input manager");
117 nativeInit(mCallbacks);
118 }
119
120 public void start() {
121 Slog.i(TAG, "Starting input manager");
122 nativeStart();
123 }
124
125 public void setDisplaySize(int displayId, int width, int height) {
126 if (width <= 0 || height <= 0) {
127 throw new IllegalArgumentException("Invalid display id or dimensions.");
128 }
129
130 Slog.i(TAG, "Setting display #" + displayId + " size to " + width + "x" + height);
131 nativeSetDisplaySize(displayId, width, height);
132 }
133
134 public void setDisplayOrientation(int displayId, int rotation) {
135 if (rotation < Surface.ROTATION_0 || rotation > Surface.ROTATION_270) {
136 throw new IllegalArgumentException("Invalid rotation.");
137 }
138
139 Slog.i(TAG, "Setting display #" + displayId + " orientation to " + rotation);
140 nativeSetDisplayOrientation(displayId, rotation);
141 }
142
143 public void getInputConfiguration(Configuration config) {
144 if (config == null) {
145 throw new IllegalArgumentException("config must not be null.");
146 }
147
148 config.touchscreen = mTouchScreenConfig;
149 config.keyboard = mKeyboardConfig;
150 config.navigation = mNavigationConfig;
151 }
152
153 public int getScancodeState(int code) {
154 return nativeGetScanCodeState(0, -1, code);
155 }
156
157 public int getScancodeState(int deviceId, int code) {
158 return nativeGetScanCodeState(deviceId, -1, code);
159 }
160
161 public int getTrackballScancodeState(int code) {
162 return nativeGetScanCodeState(-1, CLASS_TRACKBALL, code);
163 }
164
165 public int getDPadScancodeState(int code) {
166 return nativeGetScanCodeState(-1, CLASS_DPAD, code);
167 }
168
169 public int getKeycodeState(int code) {
170 return nativeGetKeyCodeState(0, -1, code);
171 }
172
173 public int getKeycodeState(int deviceId, int code) {
174 return nativeGetKeyCodeState(deviceId, -1, code);
175 }
176
177 public int getTrackballKeycodeState(int code) {
178 return nativeGetKeyCodeState(-1, CLASS_TRACKBALL, code);
179 }
180
181 public int getDPadKeycodeState(int code) {
182 return nativeGetKeyCodeState(-1, CLASS_DPAD, code);
183 }
184
185 public int getSwitchState(int sw) {
186 return nativeGetSwitchState(-1, -1, sw);
187 }
188
189 public int getSwitchState(int deviceId, int sw) {
190 return nativeGetSwitchState(deviceId, -1, sw);
191 }
192
193 public boolean hasKeys(int[] keyCodes, boolean[] keyExists) {
194 if (keyCodes == null) {
195 throw new IllegalArgumentException("keyCodes must not be null.");
196 }
197 if (keyExists == null) {
198 throw new IllegalArgumentException("keyExists must not be null.");
199 }
200
201 return nativeHasKeys(keyCodes, keyExists);
202 }
203
204 public void registerInputChannel(InputChannel inputChannel) {
205 if (inputChannel == null) {
206 throw new IllegalArgumentException("inputChannel must not be null.");
207 }
208
209 nativeRegisterInputChannel(inputChannel);
210 }
211
212 public void unregisterInputChannel(InputChannel inputChannel) {
213 if (inputChannel == null) {
214 throw new IllegalArgumentException("inputChannel must not be null.");
215 }
216
217 nativeUnregisterInputChannel(inputChannel);
218 }
219
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700220 /**
221 * Injects a key event into the event system on behalf of an application.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700222 * This method may block even if sync is false because it must wait for previous events
223 * to be dispatched before it can determine whether input event injection will be
224 * permitted based on the current input focus.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700225 * @param event The event to inject.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700226 * @param injectorPid The pid of the injecting application.
227 * @param injectorUid The uid of the injecting application.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700228 * @param sync If true, waits for the event to be completed before returning.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700229 * @param timeoutMillis The injection timeout in milliseconds.
230 * @return One of the INPUT_EVENT_INJECTION_XXX constants.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700231 */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700232 public int injectKeyEvent(KeyEvent event, int injectorPid, int injectorUid,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700233 boolean sync, int timeoutMillis) {
234 if (event == null) {
235 throw new IllegalArgumentException("event must not be null");
236 }
237 if (injectorPid < 0 || injectorUid < 0) {
238 throw new IllegalArgumentException("injectorPid and injectorUid must not be negative.");
239 }
240 if (timeoutMillis <= 0) {
241 throw new IllegalArgumentException("timeoutMillis must be positive");
242 }
243
Jeff Brownc5ed5912010-07-14 18:48:53 -0700244 return nativeInjectKeyEvent(event, injectorPid, injectorUid,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700245 sync, timeoutMillis);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700246 }
247
248 /**
249 * Injects a motion event into the event system on behalf of an application.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700250 * This method may block even if sync is false because it must wait for previous events
251 * to be dispatched before it can determine whether input event injection will be
252 * permitted based on the current input focus.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700253 * @param event The event to inject.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700254 * @param sync If true, waits for the event to be completed before returning.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700255 * @param injectorPid The pid of the injecting application.
256 * @param injectorUid The uid of the injecting application.
257 * @param sync If true, waits for the event to be completed before returning.
258 * @param timeoutMillis The injection timeout in milliseconds.
259 * @return One of the INPUT_EVENT_INJECTION_XXX constants.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700260 */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700261 public int injectMotionEvent(MotionEvent event, int injectorPid, int injectorUid,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700262 boolean sync, int timeoutMillis) {
263 if (event == null) {
264 throw new IllegalArgumentException("event must not be null");
265 }
266 if (injectorPid < 0 || injectorUid < 0) {
267 throw new IllegalArgumentException("injectorPid and injectorUid must not be negative.");
268 }
269 if (timeoutMillis <= 0) {
270 throw new IllegalArgumentException("timeoutMillis must be positive");
271 }
272
Jeff Brownc5ed5912010-07-14 18:48:53 -0700273 return nativeInjectMotionEvent(event, injectorPid, injectorUid,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700274 sync, timeoutMillis);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700275 }
276
Jeff Brown349703e2010-06-22 01:27:15 -0700277 public void setInputWindows(InputWindow[] windows) {
278 nativeSetInputWindows(windows);
279 }
280
281 public void setFocusedApplication(InputApplication application) {
282 nativeSetFocusedApplication(application);
283 }
284
285 public void preemptInputDispatch() {
286 nativePreemptInputDispatch();
287 }
288
289 public void setInputDispatchMode(boolean enabled, boolean frozen) {
290 nativeSetInputDispatchMode(enabled, frozen);
291 }
292
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700293 public void dump(PrintWriter pw) {
Jeff Browne33348b2010-07-15 23:54:05 -0700294 String dumpStr = nativeDump();
295 if (dumpStr != null) {
296 pw.println(dumpStr);
297 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700298 }
299
300 private static final class VirtualKeyDefinition {
301 public int scanCode;
302
303 // configured position data, specified in display coords
304 public int centerX;
305 public int centerY;
306 public int width;
307 public int height;
308 }
309
310 /*
311 * Callbacks from native.
312 */
313 private class Callbacks {
314 static final String TAG = "InputManager-Callbacks";
315
316 private static final boolean DEBUG_VIRTUAL_KEYS = false;
317 private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml";
318
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700319 @SuppressWarnings("unused")
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700320 public void virtualKeyDownFeedback() {
321 mWindowManagerService.mInputMonitor.virtualKeyDownFeedback();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700322 }
323
324 @SuppressWarnings("unused")
325 public void notifyConfigurationChanged(long whenNanos,
326 int touchScreenConfig, int keyboardConfig, int navigationConfig) {
327 mTouchScreenConfig = touchScreenConfig;
328 mKeyboardConfig = keyboardConfig;
329 mNavigationConfig = navigationConfig;
330
331 mWindowManagerService.sendNewConfiguration();
332 }
333
334 @SuppressWarnings("unused")
335 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700336 mWindowManagerService.mInputMonitor.notifyLidSwitchChanged(whenNanos, lidOpen);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700337 }
338
339 @SuppressWarnings("unused")
Jeff Brown7fbdc842010-06-17 20:52:56 -0700340 public void notifyInputChannelBroken(InputChannel inputChannel) {
Jeff Brown349703e2010-06-22 01:27:15 -0700341 mWindowManagerService.mInputMonitor.notifyInputChannelBroken(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700342 }
343
344 @SuppressWarnings("unused")
345 public long notifyInputChannelANR(InputChannel inputChannel) {
Jeff Brown349703e2010-06-22 01:27:15 -0700346 return mWindowManagerService.mInputMonitor.notifyInputChannelANR(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700347 }
348
349 @SuppressWarnings("unused")
350 public void notifyInputChannelRecoveredFromANR(InputChannel inputChannel) {
Jeff Brown349703e2010-06-22 01:27:15 -0700351 mWindowManagerService.mInputMonitor.notifyInputChannelRecoveredFromANR(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700352 }
353
354 @SuppressWarnings("unused")
Jeff Brown349703e2010-06-22 01:27:15 -0700355 public long notifyANR(Object token) {
356 return mWindowManagerService.mInputMonitor.notifyANR(token);
357 }
358
359 @SuppressWarnings("unused")
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700360 public int interceptKeyBeforeQueueing(long whenNanos, int keyCode, boolean down,
361 int policyFlags, boolean isScreenOn) {
362 return mWindowManagerService.mInputMonitor.interceptKeyBeforeQueueing(
363 whenNanos, keyCode, down, policyFlags, isScreenOn);
Jeff Brown349703e2010-06-22 01:27:15 -0700364 }
365
366 @SuppressWarnings("unused")
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700367 public boolean interceptKeyBeforeDispatching(InputChannel focus, int action,
368 int flags, int keyCode, int metaState, int repeatCount, int policyFlags) {
Jeff Brown349703e2010-06-22 01:27:15 -0700369 return mWindowManagerService.mInputMonitor.interceptKeyBeforeDispatching(focus,
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700370 action, flags, keyCode, metaState, repeatCount, policyFlags);
Jeff Brown349703e2010-06-22 01:27:15 -0700371 }
372
373 @SuppressWarnings("unused")
374 public boolean checkInjectEventsPermission(int injectorPid, int injectorUid) {
375 return mContext.checkPermission(
376 android.Manifest.permission.INJECT_EVENTS, injectorPid, injectorUid)
377 == PackageManager.PERMISSION_GRANTED;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700378 }
379
380 @SuppressWarnings("unused")
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700381 public void notifyAppSwitchComing() {
Jeff Brown349703e2010-06-22 01:27:15 -0700382 mWindowManagerService.mInputMonitor.notifyAppSwitchComing();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700383 }
384
385 @SuppressWarnings("unused")
386 public boolean filterTouchEvents() {
387 return mContext.getResources().getBoolean(
388 com.android.internal.R.bool.config_filterTouchEvents);
389 }
390
391 @SuppressWarnings("unused")
392 public boolean filterJumpyTouchEvents() {
393 return mContext.getResources().getBoolean(
394 com.android.internal.R.bool.config_filterJumpyTouchEvents);
395 }
396
397 @SuppressWarnings("unused")
398 public VirtualKeyDefinition[] getVirtualKeyDefinitions(String deviceName) {
399 ArrayList<VirtualKeyDefinition> keys = new ArrayList<VirtualKeyDefinition>();
400
401 try {
402 FileInputStream fis = new FileInputStream(
403 "/sys/board_properties/virtualkeys." + deviceName);
404 InputStreamReader isr = new InputStreamReader(fis);
405 BufferedReader br = new BufferedReader(isr, 2048);
406 String str = br.readLine();
407 if (str != null) {
408 String[] it = str.split(":");
409 if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "***** VIRTUAL KEYS: " + it);
410 final int N = it.length-6;
411 for (int i=0; i<=N; i+=6) {
412 if (!"0x01".equals(it[i])) {
413 Slog.w(TAG, "Unknown virtual key type at elem #" + i
414 + ": " + it[i]);
415 continue;
416 }
417 try {
418 VirtualKeyDefinition key = new VirtualKeyDefinition();
419 key.scanCode = Integer.parseInt(it[i+1]);
420 key.centerX = Integer.parseInt(it[i+2]);
421 key.centerY = Integer.parseInt(it[i+3]);
422 key.width = Integer.parseInt(it[i+4]);
423 key.height = Integer.parseInt(it[i+5]);
424 if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "Virtual key "
425 + key.scanCode + ": center=" + key.centerX + ","
426 + key.centerY + " size=" + key.width + "x"
427 + key.height);
428 keys.add(key);
429 } catch (NumberFormatException e) {
430 Slog.w(TAG, "Bad number at region " + i + " in: "
431 + str, e);
432 }
433 }
434 }
435 br.close();
436 } catch (FileNotFoundException e) {
437 Slog.i(TAG, "No virtual keys found");
438 } catch (IOException e) {
439 Slog.w(TAG, "Error reading virtual keys", e);
440 }
441
442 return keys.toArray(new VirtualKeyDefinition[keys.size()]);
443 }
444
445 @SuppressWarnings("unused")
446 public String[] getExcludedDeviceNames() {
447 ArrayList<String> names = new ArrayList<String>();
448
449 // Read partner-provided list of excluded input devices
450 XmlPullParser parser = null;
451 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
452 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
453 FileReader confreader = null;
454 try {
455 confreader = new FileReader(confFile);
456 parser = Xml.newPullParser();
457 parser.setInput(confreader);
458 XmlUtils.beginDocument(parser, "devices");
459
460 while (true) {
461 XmlUtils.nextElement(parser);
462 if (!"device".equals(parser.getName())) {
463 break;
464 }
465 String name = parser.getAttributeValue(null, "name");
466 if (name != null) {
467 names.add(name);
468 }
469 }
470 } catch (FileNotFoundException e) {
471 // It's ok if the file does not exist.
472 } catch (Exception e) {
473 Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
474 } finally {
475 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
476 }
477
478 return names.toArray(new String[names.size()]);
479 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700480 }
481}