blob: ab354a5da11caef1ebb105af3293b77e7ce1f4f7 [file] [log] [blame]
Jeff Browne839a582010-04-22 18:58:52 -07001//
2// Copyright 2010 The Android Open Source Project
3//
4// The input manager.
5//
6#define LOG_TAG "InputManager"
7
8//#define LOG_NDEBUG 0
9
10#include <cutils/log.h>
11#include <ui/InputManager.h>
12#include <ui/InputReader.h>
13#include <ui/InputDispatcher.h>
14
15namespace android {
16
17InputManager::InputManager(const sp<EventHubInterface>& eventHub,
18 const sp<InputDispatchPolicyInterface>& policy) :
19 mEventHub(eventHub), mPolicy(policy) {
20 mDispatcher = new InputDispatcher(policy);
21 mReader = new InputReader(eventHub, policy, mDispatcher);
22
23 mDispatcherThread = new InputDispatcherThread(mDispatcher);
24 mReaderThread = new InputReaderThread(mReader);
25
26 configureExcludedDevices();
27}
28
29InputManager::~InputManager() {
30 stop();
31}
32
33void InputManager::configureExcludedDevices() {
34 Vector<String8> excludedDeviceNames;
35 mPolicy->getExcludedDeviceNames(excludedDeviceNames);
36
37 for (size_t i = 0; i < excludedDeviceNames.size(); i++) {
38 mEventHub->addExcludedDevice(excludedDeviceNames[i]);
39 }
40}
41
42status_t InputManager::start() {
43 status_t result = mDispatcherThread->run("InputDispatcher", PRIORITY_URGENT_DISPLAY);
44 if (result) {
45 LOGE("Could not start InputDispatcher thread due to error %d.", result);
46 return result;
47 }
48
49 result = mReaderThread->run("InputReader", PRIORITY_URGENT_DISPLAY);
50 if (result) {
51 LOGE("Could not start InputReader thread due to error %d.", result);
52
53 mDispatcherThread->requestExit();
54 return result;
55 }
56
57 return OK;
58}
59
60status_t InputManager::stop() {
61 status_t result = mReaderThread->requestExitAndWait();
62 if (result) {
63 LOGW("Could not stop InputReader thread due to error %d.", result);
64 }
65
66 result = mDispatcherThread->requestExitAndWait();
67 if (result) {
68 LOGW("Could not stop InputDispatcher thread due to error %d.", result);
69 }
70
71 return OK;
72}
73
74status_t InputManager::registerInputChannel(const sp<InputChannel>& inputChannel) {
75 return mDispatcher->registerInputChannel(inputChannel);
76}
77
78status_t InputManager::unregisterInputChannel(const sp<InputChannel>& inputChannel) {
79 return mDispatcher->unregisterInputChannel(inputChannel);
80}
81
82int32_t InputManager::getScanCodeState(int32_t deviceId, int32_t deviceClasses, int32_t scanCode)
83 const {
84 int32_t vkKeyCode, vkScanCode;
85 if (mReader->getCurrentVirtualKey(& vkKeyCode, & vkScanCode)) {
86 if (vkScanCode == scanCode) {
87 return KEY_STATE_VIRTUAL;
88 }
89 }
90
91 return mEventHub->getScanCodeState(deviceId, deviceClasses, scanCode);
92}
93
94int32_t InputManager::getKeyCodeState(int32_t deviceId, int32_t deviceClasses, int32_t keyCode)
95 const {
96 int32_t vkKeyCode, vkScanCode;
97 if (mReader->getCurrentVirtualKey(& vkKeyCode, & vkScanCode)) {
98 if (vkKeyCode == keyCode) {
99 return KEY_STATE_VIRTUAL;
100 }
101 }
102
103 return mEventHub->getKeyCodeState(deviceId, deviceClasses, keyCode);
104}
105
106int32_t InputManager::getSwitchState(int32_t deviceId, int32_t deviceClasses, int32_t sw) const {
107 return mEventHub->getSwitchState(deviceId, deviceClasses, sw);
108}
109
110bool InputManager::hasKeys(size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) const {
111 return mEventHub->hasKeys(numCodes, keyCodes, outFlags);
112}
113
114} // namespace android