blob: 8d9bb2940ab22a45f00cef4fb05930c199dd9b2c [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;
20import com.android.server.KeyInputQueue.VirtualKey;
21
22import org.xmlpull.v1.XmlPullParser;
23
24import android.content.Context;
25import android.content.res.Configuration;
26import android.os.Environment;
27import android.os.LocalPowerManager;
28import android.os.PowerManager;
29import android.util.Log;
30import android.util.Slog;
31import android.util.Xml;
32import android.view.InputChannel;
33import android.view.InputTarget;
34import android.view.KeyEvent;
35import android.view.MotionEvent;
36import android.view.RawInputEvent;
37import android.view.Surface;
38import android.view.WindowManagerPolicy;
39
40import java.io.BufferedReader;
41import java.io.File;
42import java.io.FileInputStream;
43import java.io.FileNotFoundException;
44import java.io.FileReader;
45import java.io.IOException;
46import java.io.InputStreamReader;
47import java.io.PrintWriter;
48import java.util.ArrayList;
49
50/*
51 * Wraps the C++ InputManager and provides its callbacks.
52 *
53 * XXX Tempted to promote this to a first-class service, ie. InputManagerService, to
54 * improve separation of concerns with respect to the window manager.
55 */
56public class InputManager {
57 static final String TAG = "InputManager";
58
59 private final Callbacks mCallbacks;
60 private final Context mContext;
61 private final WindowManagerService mWindowManagerService;
62 private final WindowManagerPolicy mWindowManagerPolicy;
63 private final PowerManager mPowerManager;
64 private final PowerManagerService mPowerManagerService;
65
66 private int mTouchScreenConfig;
67 private int mKeyboardConfig;
68 private int mNavigationConfig;
69
70 private static native void nativeInit(Callbacks callbacks);
71 private static native void nativeStart();
72 private static native void nativeSetDisplaySize(int displayId, int width, int height);
73 private static native void nativeSetDisplayOrientation(int displayId, int rotation);
74
75 private static native int nativeGetScanCodeState(int deviceId, int deviceClasses,
76 int scanCode);
77 private static native int nativeGetKeyCodeState(int deviceId, int deviceClasses,
78 int keyCode);
79 private static native int nativeGetSwitchState(int deviceId, int deviceClasses,
80 int sw);
81 private static native boolean nativeHasKeys(int[] keyCodes, boolean[] keyExists);
82 private static native void nativeRegisterInputChannel(InputChannel inputChannel);
83 private static native void nativeUnregisterInputChannel(InputChannel inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -070084 private static native int nativeInjectKeyEvent(KeyEvent event, int nature,
85 int injectorPid, int injectorUid, boolean sync, int timeoutMillis);
86 private static native int nativeInjectMotionEvent(MotionEvent event, int nature,
87 int injectorPid, int injectorUid, boolean sync, int timeoutMillis);
Jeff Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -0700103 public InputManager(Context context,
104 WindowManagerService windowManagerService,
105 WindowManagerPolicy windowManagerPolicy,
106 PowerManager powerManager,
107 PowerManagerService powerManagerService) {
108 this.mContext = context;
109 this.mWindowManagerService = windowManagerService;
110 this.mWindowManagerPolicy = windowManagerPolicy;
111 this.mPowerManager = powerManager;
112 this.mPowerManagerService = powerManagerService;
113
114 this.mCallbacks = new Callbacks();
115
116 mTouchScreenConfig = Configuration.TOUCHSCREEN_NOTOUCH;
117 mKeyboardConfig = Configuration.KEYBOARD_NOKEYS;
118 mNavigationConfig = Configuration.NAVIGATION_NONAV;
119
120 init();
121 }
122
123 private void init() {
124 Slog.i(TAG, "Initializing input manager");
125 nativeInit(mCallbacks);
126 }
127
128 public void start() {
129 Slog.i(TAG, "Starting input manager");
130 nativeStart();
131 }
132
133 public void setDisplaySize(int displayId, int width, int height) {
134 if (width <= 0 || height <= 0) {
135 throw new IllegalArgumentException("Invalid display id or dimensions.");
136 }
137
138 Slog.i(TAG, "Setting display #" + displayId + " size to " + width + "x" + height);
139 nativeSetDisplaySize(displayId, width, height);
140 }
141
142 public void setDisplayOrientation(int displayId, int rotation) {
143 if (rotation < Surface.ROTATION_0 || rotation > Surface.ROTATION_270) {
144 throw new IllegalArgumentException("Invalid rotation.");
145 }
146
147 Slog.i(TAG, "Setting display #" + displayId + " orientation to " + rotation);
148 nativeSetDisplayOrientation(displayId, rotation);
149 }
150
151 public void getInputConfiguration(Configuration config) {
152 if (config == null) {
153 throw new IllegalArgumentException("config must not be null.");
154 }
155
156 config.touchscreen = mTouchScreenConfig;
157 config.keyboard = mKeyboardConfig;
158 config.navigation = mNavigationConfig;
159 }
160
161 public int getScancodeState(int code) {
162 return nativeGetScanCodeState(0, -1, code);
163 }
164
165 public int getScancodeState(int deviceId, int code) {
166 return nativeGetScanCodeState(deviceId, -1, code);
167 }
168
169 public int getTrackballScancodeState(int code) {
170 return nativeGetScanCodeState(-1, CLASS_TRACKBALL, code);
171 }
172
173 public int getDPadScancodeState(int code) {
174 return nativeGetScanCodeState(-1, CLASS_DPAD, code);
175 }
176
177 public int getKeycodeState(int code) {
178 return nativeGetKeyCodeState(0, -1, code);
179 }
180
181 public int getKeycodeState(int deviceId, int code) {
182 return nativeGetKeyCodeState(deviceId, -1, code);
183 }
184
185 public int getTrackballKeycodeState(int code) {
186 return nativeGetKeyCodeState(-1, CLASS_TRACKBALL, code);
187 }
188
189 public int getDPadKeycodeState(int code) {
190 return nativeGetKeyCodeState(-1, CLASS_DPAD, code);
191 }
192
193 public int getSwitchState(int sw) {
194 return nativeGetSwitchState(-1, -1, sw);
195 }
196
197 public int getSwitchState(int deviceId, int sw) {
198 return nativeGetSwitchState(deviceId, -1, sw);
199 }
200
201 public boolean hasKeys(int[] keyCodes, boolean[] keyExists) {
202 if (keyCodes == null) {
203 throw new IllegalArgumentException("keyCodes must not be null.");
204 }
205 if (keyExists == null) {
206 throw new IllegalArgumentException("keyExists must not be null.");
207 }
208
209 return nativeHasKeys(keyCodes, keyExists);
210 }
211
212 public void registerInputChannel(InputChannel inputChannel) {
213 if (inputChannel == null) {
214 throw new IllegalArgumentException("inputChannel must not be null.");
215 }
216
217 nativeRegisterInputChannel(inputChannel);
218 }
219
220 public void unregisterInputChannel(InputChannel inputChannel) {
221 if (inputChannel == null) {
222 throw new IllegalArgumentException("inputChannel must not be null.");
223 }
224
225 nativeUnregisterInputChannel(inputChannel);
226 }
227
Jeff Brown46b9ac02010-04-22 18:58:52 -0700228 /**
229 * Injects a key event into the event system on behalf of an application.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700230 * This method may block even if sync is false because it must wait for previous events
231 * to be dispatched before it can determine whether input event injection will be
232 * permitted based on the current input focus.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700233 * @param event The event to inject.
234 * @param nature The nature of the event.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700235 * @param injectorPid The pid of the injecting application.
236 * @param injectorUid The uid of the injecting application.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700237 * @param sync If true, waits for the event to be completed before returning.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700238 * @param timeoutMillis The injection timeout in milliseconds.
239 * @return One of the INPUT_EVENT_INJECTION_XXX constants.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700240 */
Jeff Brown7fbdc842010-06-17 20:52:56 -0700241 public int injectKeyEvent(KeyEvent event, int nature, int injectorPid, int injectorUid,
242 boolean sync, int timeoutMillis) {
243 if (event == null) {
244 throw new IllegalArgumentException("event must not be null");
245 }
246 if (injectorPid < 0 || injectorUid < 0) {
247 throw new IllegalArgumentException("injectorPid and injectorUid must not be negative.");
248 }
249 if (timeoutMillis <= 0) {
250 throw new IllegalArgumentException("timeoutMillis must be positive");
251 }
252
253 return nativeInjectKeyEvent(event, nature, injectorPid, injectorUid,
254 sync, timeoutMillis);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700255 }
256
257 /**
258 * Injects a motion event into the event system on behalf of an application.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700259 * This method may block even if sync is false because it must wait for previous events
260 * to be dispatched before it can determine whether input event injection will be
261 * permitted based on the current input focus.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700262 * @param event The event to inject.
263 * @param nature The nature of the event.
264 * @param sync If true, waits for the event to be completed before returning.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700265 * @param injectorPid The pid of the injecting application.
266 * @param injectorUid The uid of the injecting application.
267 * @param sync If true, waits for the event to be completed before returning.
268 * @param timeoutMillis The injection timeout in milliseconds.
269 * @return One of the INPUT_EVENT_INJECTION_XXX constants.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700270 */
Jeff Brown7fbdc842010-06-17 20:52:56 -0700271 public int injectMotionEvent(MotionEvent event, int nature, int injectorPid, int injectorUid,
272 boolean sync, int timeoutMillis) {
273 if (event == null) {
274 throw new IllegalArgumentException("event must not be null");
275 }
276 if (injectorPid < 0 || injectorUid < 0) {
277 throw new IllegalArgumentException("injectorPid and injectorUid must not be negative.");
278 }
279 if (timeoutMillis <= 0) {
280 throw new IllegalArgumentException("timeoutMillis must be positive");
281 }
282
283 return nativeInjectMotionEvent(event, nature, injectorPid, injectorUid,
284 sync, timeoutMillis);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700285 }
286
287 public void dump(PrintWriter pw) {
288 // TODO
289 }
290
291 private static final class VirtualKeyDefinition {
292 public int scanCode;
293
294 // configured position data, specified in display coords
295 public int centerX;
296 public int centerY;
297 public int width;
298 public int height;
299 }
300
301 /*
302 * Callbacks from native.
303 */
304 private class Callbacks {
305 static final String TAG = "InputManager-Callbacks";
306
307 private static final boolean DEBUG_VIRTUAL_KEYS = false;
308 private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml";
309
Jeff Brown46b9ac02010-04-22 18:58:52 -0700310 @SuppressWarnings("unused")
311 public boolean isScreenOn() {
312 return mPowerManagerService.isScreenOn();
313 }
314
315 @SuppressWarnings("unused")
316 public boolean isScreenBright() {
317 return mPowerManagerService.isScreenBright();
318 }
319
320 @SuppressWarnings("unused")
321 public void virtualKeyFeedback(long whenNanos, int deviceId, int action, int flags,
322 int keyCode, int scanCode, int metaState, long downTimeNanos) {
323 KeyEvent keyEvent = new KeyEvent(downTimeNanos / 1000000,
324 whenNanos / 1000000, action, keyCode, 0, metaState, scanCode, deviceId,
325 flags);
326
327 mWindowManagerService.virtualKeyFeedback(keyEvent);
328 }
329
330 @SuppressWarnings("unused")
331 public void notifyConfigurationChanged(long whenNanos,
332 int touchScreenConfig, int keyboardConfig, int navigationConfig) {
333 mTouchScreenConfig = touchScreenConfig;
334 mKeyboardConfig = keyboardConfig;
335 mNavigationConfig = navigationConfig;
336
337 mWindowManagerService.sendNewConfiguration();
338 }
339
340 @SuppressWarnings("unused")
341 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
342 mWindowManagerPolicy.notifyLidSwitchChanged(whenNanos, lidOpen);
343 }
344
345 @SuppressWarnings("unused")
Jeff Brown7fbdc842010-06-17 20:52:56 -0700346 public void notifyInputChannelBroken(InputChannel inputChannel) {
347 mWindowManagerService.notifyInputChannelBroken(inputChannel);
348 }
349
350 @SuppressWarnings("unused")
351 public long notifyInputChannelANR(InputChannel inputChannel) {
352 return mWindowManagerService.notifyInputChannelANR(inputChannel);
353 }
354
355 @SuppressWarnings("unused")
356 public void notifyInputChannelRecoveredFromANR(InputChannel inputChannel) {
357 mWindowManagerService.notifyInputChannelRecoveredFromANR(inputChannel);
358 }
359
360 @SuppressWarnings("unused")
Jeff Brown46b9ac02010-04-22 18:58:52 -0700361 public int hackInterceptKey(int deviceId, int type, int scanCode,
362 int keyCode, int policyFlags, int value, long whenNanos, boolean isScreenOn) {
363 RawInputEvent event = new RawInputEvent();
364 event.deviceId = deviceId;
365 event.type = type;
366 event.scancode = scanCode;
367 event.keycode = keyCode;
368 event.flags = policyFlags;
369 event.value = value;
370 event.when = whenNanos / 1000000;
371
372 return mWindowManagerPolicy.interceptKeyTq(event, isScreenOn);
373 }
374
375 @SuppressWarnings("unused")
376 public void goToSleep(long whenNanos) {
377 long when = whenNanos / 1000000;
378 mPowerManager.goToSleep(when);
379 }
380
381 @SuppressWarnings("unused")
382 public void pokeUserActivityForKey(long whenNanos) {
383 long when = whenNanos / 1000000;
384 mPowerManagerService.userActivity(when, false,
385 LocalPowerManager.BUTTON_EVENT, false);
386 }
387
388 @SuppressWarnings("unused")
389 public void notifyAppSwitchComing() {
390 mWindowManagerService.mKeyWaiter.appSwitchComing();
391 }
392
393 @SuppressWarnings("unused")
394 public boolean filterTouchEvents() {
395 return mContext.getResources().getBoolean(
396 com.android.internal.R.bool.config_filterTouchEvents);
397 }
398
399 @SuppressWarnings("unused")
400 public boolean filterJumpyTouchEvents() {
401 return mContext.getResources().getBoolean(
402 com.android.internal.R.bool.config_filterJumpyTouchEvents);
403 }
404
405 @SuppressWarnings("unused")
406 public VirtualKeyDefinition[] getVirtualKeyDefinitions(String deviceName) {
407 ArrayList<VirtualKeyDefinition> keys = new ArrayList<VirtualKeyDefinition>();
408
409 try {
410 FileInputStream fis = new FileInputStream(
411 "/sys/board_properties/virtualkeys." + deviceName);
412 InputStreamReader isr = new InputStreamReader(fis);
413 BufferedReader br = new BufferedReader(isr, 2048);
414 String str = br.readLine();
415 if (str != null) {
416 String[] it = str.split(":");
417 if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "***** VIRTUAL KEYS: " + it);
418 final int N = it.length-6;
419 for (int i=0; i<=N; i+=6) {
420 if (!"0x01".equals(it[i])) {
421 Slog.w(TAG, "Unknown virtual key type at elem #" + i
422 + ": " + it[i]);
423 continue;
424 }
425 try {
426 VirtualKeyDefinition key = new VirtualKeyDefinition();
427 key.scanCode = Integer.parseInt(it[i+1]);
428 key.centerX = Integer.parseInt(it[i+2]);
429 key.centerY = Integer.parseInt(it[i+3]);
430 key.width = Integer.parseInt(it[i+4]);
431 key.height = Integer.parseInt(it[i+5]);
432 if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "Virtual key "
433 + key.scanCode + ": center=" + key.centerX + ","
434 + key.centerY + " size=" + key.width + "x"
435 + key.height);
436 keys.add(key);
437 } catch (NumberFormatException e) {
438 Slog.w(TAG, "Bad number at region " + i + " in: "
439 + str, e);
440 }
441 }
442 }
443 br.close();
444 } catch (FileNotFoundException e) {
445 Slog.i(TAG, "No virtual keys found");
446 } catch (IOException e) {
447 Slog.w(TAG, "Error reading virtual keys", e);
448 }
449
450 return keys.toArray(new VirtualKeyDefinition[keys.size()]);
451 }
452
453 @SuppressWarnings("unused")
454 public String[] getExcludedDeviceNames() {
455 ArrayList<String> names = new ArrayList<String>();
456
457 // Read partner-provided list of excluded input devices
458 XmlPullParser parser = null;
459 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
460 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
461 FileReader confreader = null;
462 try {
463 confreader = new FileReader(confFile);
464 parser = Xml.newPullParser();
465 parser.setInput(confreader);
466 XmlUtils.beginDocument(parser, "devices");
467
468 while (true) {
469 XmlUtils.nextElement(parser);
470 if (!"device".equals(parser.getName())) {
471 break;
472 }
473 String name = parser.getAttributeValue(null, "name");
474 if (name != null) {
475 names.add(name);
476 }
477 }
478 } catch (FileNotFoundException e) {
479 // It's ok if the file does not exist.
480 } catch (Exception e) {
481 Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
482 } finally {
483 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
484 }
485
486 return names.toArray(new String[names.size()]);
487 }
488
Jeff Brown7fbdc842010-06-17 20:52:56 -0700489 // TODO All code related to target identification should be moved down into native.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700490 @SuppressWarnings("unused")
Jeff Brown7fbdc842010-06-17 20:52:56 -0700491 public int getKeyEventTargets(InputTargetList inputTargets,
492 KeyEvent event, int nature, int policyFlags,
493 int injectorPid, int injectorUid) {
494 inputTargets.clear();
495 return mWindowManagerService.getKeyEventTargetsTd(
496 inputTargets, event, nature, policyFlags, injectorPid, injectorUid);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700497 }
498
499 @SuppressWarnings("unused")
Jeff Brown7fbdc842010-06-17 20:52:56 -0700500 public int getMotionEventTargets(InputTargetList inputTargets,
501 MotionEvent event, int nature, int policyFlags,
502 int injectorPid, int injectorUid) {
503 inputTargets.clear();
504 return mWindowManagerService.getMotionEventTargetsTd(
505 inputTargets, event, nature, policyFlags, injectorPid, injectorUid);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700506 }
507 }
508}