InputManager.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  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. #define LOG_TAG "InputManager"
  17. //#define LOG_NDEBUG 0
  18. #include "InputManager.h"
  19. #include <cutils/log.h>
  20. #include <cutils/iosched_policy.h>
  21. namespace android {
  22. InputManager::InputManager(
  23. const sp<EventHubInterface>& eventHub,
  24. const sp<InputReaderPolicyInterface>& readerPolicy,
  25. const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
  26. mDispatcher = new InputDispatcher(dispatcherPolicy);
  27. mReader = new InputReader(eventHub, readerPolicy, mDispatcher);
  28. initialize();
  29. }
  30. InputManager::InputManager(
  31. const sp<InputReaderInterface>& reader,
  32. const sp<InputDispatcherInterface>& dispatcher) :
  33. mReader(reader),
  34. mDispatcher(dispatcher) {
  35. initialize();
  36. }
  37. InputManager::~InputManager() {
  38. stop();
  39. }
  40. void InputManager::initialize() {
  41. mReaderThread = new InputReaderThread(mReader);
  42. mDispatcherThread = new InputDispatcherThread(mDispatcher);
  43. }
  44. status_t InputManager::start() {
  45. status_t result = mDispatcherThread->run("InputDispatcher",
  46. PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
  47. if (result) {
  48. ALOGE("Could not start InputDispatcher thread due to error %d.", result);
  49. return result;
  50. }
  51. result = mReaderThread->run("InputReader",
  52. PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
  53. if (result) {
  54. ALOGE("Could not start InputReader thread due to error %d.", result);
  55. mDispatcherThread->requestExit();
  56. return result;
  57. }
  58. android_set_rt_ioprio(mDispatcherThread->getTid(), 1);
  59. android_set_rt_ioprio(mReaderThread->getTid(), 1);
  60. return OK;
  61. }
  62. status_t InputManager::stop() {
  63. status_t result = mReaderThread->requestExitAndWait();
  64. if (result) {
  65. ALOGW("Could not stop InputReader thread due to error %d.", result);
  66. }
  67. result = mDispatcherThread->requestExitAndWait();
  68. if (result) {
  69. ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
  70. }
  71. return OK;
  72. }
  73. sp<InputReaderInterface> InputManager::getReader() {
  74. return mReader;
  75. }
  76. sp<InputDispatcherInterface> InputManager::getDispatcher() {
  77. return mDispatcher;
  78. }
  79. } // namespace android