NonUiNotifier.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2024 The LineageOS Project
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #define LOG_TAG "NonUiNotifier"
  7. #include <android-base/logging.h>
  8. #include <android/frameworks/sensorservice/1.0/ISensorManager.h>
  9. #include <fcntl.h>
  10. #include <poll.h>
  11. #include <sys/ioctl.h>
  12. #include <linux/xiaomi_touch.h>
  13. #define SENSOR_NAME_XIAOMI_SENSOR_NONUI "xiaomi.sensor.nonui"
  14. using android::sp;
  15. using android::frameworks::sensorservice::V1_0::IEventQueue;
  16. using android::frameworks::sensorservice::V1_0::IEventQueueCallback;
  17. using android::frameworks::sensorservice::V1_0::ISensorManager;
  18. using android::frameworks::sensorservice::V1_0::Result;
  19. using android::hardware::Return;
  20. using android::hardware::Void;
  21. using android::hardware::sensors::V1_0::Event;
  22. using android::hardware::sensors::V1_0::SensorFlagBits;
  23. using android::hardware::sensors::V1_0::SensorInfo;
  24. using android::hardware::sensors::V1_0::SensorType;
  25. #define TOUCH_DEV_PATH "/dev/xiaomi-touch"
  26. namespace {
  27. static bool readBool(int fd) {
  28. char c;
  29. int rc;
  30. rc = lseek(fd, 0, SEEK_SET);
  31. if (rc) {
  32. LOG(ERROR) << "failed to seek fd, err: " << rc;
  33. return false;
  34. }
  35. rc = read(fd, &c, sizeof(char));
  36. if (rc != 1) {
  37. LOG(ERROR) << "failed to read bool from fd, err: " << rc;
  38. return false;
  39. }
  40. return c != '0';
  41. }
  42. struct NonUiSensorCallback : IEventQueueCallback {
  43. Return<void> onEvent(const Event& e) {
  44. int buf[MAX_BUF_SIZE] = {0, Touch_Nonui_Mode, static_cast<int>(e.u.scalar)};
  45. ioctl(open(TOUCH_DEV_PATH, O_RDWR), TOUCH_IOC_SET_CUR_VALUE, &buf);
  46. return Void();
  47. }
  48. };
  49. } // namespace
  50. int main() {
  51. Result res;
  52. sp<IEventQueue> queue;
  53. sp<ISensorManager> manager = ISensorManager::getService();
  54. if (manager == nullptr) {
  55. LOG(ERROR) << "failed to get ISensorManager";
  56. return EXIT_FAILURE;
  57. }
  58. std::vector<SensorInfo> sensorList;
  59. manager->getSensorList([&sensorList, &res](const auto& l, auto r) {
  60. sensorList = l;
  61. res = r;
  62. });
  63. if (res != Result::OK) {
  64. LOG(ERROR) << "failed to get getSensorList";
  65. return EXIT_FAILURE;
  66. }
  67. auto it = std::find_if(sensorList.begin(), sensorList.end(), [](const SensorInfo& sensor) {
  68. return (sensor.typeAsString == SENSOR_NAME_XIAOMI_SENSOR_NONUI) &&
  69. (sensor.flags & SensorFlagBits::WAKE_UP);
  70. });
  71. int32_t sensorHandle = -1;
  72. if (it != sensorList.end()) {
  73. sensorHandle = it->sensorHandle;
  74. } else {
  75. LOG(ERROR) << "failed to get wake-up version of nonui sensor";
  76. return EXIT_FAILURE;
  77. }
  78. sensorList.clear();
  79. manager->createEventQueue(new NonUiSensorCallback(), [&queue, &res](const auto& q, auto r) {
  80. queue = q;
  81. res = r;
  82. });
  83. if (res != Result::OK) {
  84. LOG(ERROR) << "failed to create event queue";
  85. return EXIT_FAILURE;
  86. }
  87. // Enable states of touchscreen sensors
  88. const std::vector<const char*> paths = {
  89. "/sys/class/touch/touch_dev/fod_longpress_gesture_enabled",
  90. "/sys/class/touch/touch_dev/gesture_single_tap_enabled",
  91. "/sys/class/touch/touch_dev/gesture_double_tap_enabled"};
  92. pollfd* pollfds = new pollfd[paths.size()];
  93. for (size_t i = 0; i < paths.size(); ++i) {
  94. int fd = open(paths[i], O_RDONLY);
  95. if (fd < 0) {
  96. LOG(ERROR) << "failed to open " << paths[i] << " , err: " << fd;
  97. return EXIT_FAILURE;
  98. }
  99. pollfds[i].fd = fd;
  100. pollfds[i].events = POLLERR | POLLPRI;
  101. pollfds[i].revents = 0;
  102. }
  103. while (true) {
  104. int rc = poll(pollfds, paths.size(), -1);
  105. if (rc < 0) {
  106. LOG(ERROR) << "failed to poll, err: " << rc;
  107. continue;
  108. }
  109. for (size_t i = 0; i < paths.size(); ++i) {
  110. if (pollfds[i].revents & (POLLERR | POLLPRI)) {
  111. LOG(VERBOSE) << "polled change on " << paths[i];
  112. }
  113. }
  114. bool enabled = false;
  115. for (size_t i = 0; i < paths.size(); ++i) {
  116. enabled = enabled || readBool(pollfds[i].fd);
  117. }
  118. if (enabled) {
  119. res = queue->enableSensor(sensorHandle, 20000 /* sample period */, 0 /* latency */);
  120. if (res != Result::OK) {
  121. LOG(ERROR) << "failed to enable sensor";
  122. }
  123. } else {
  124. res = queue->disableSensor(sensorHandle);
  125. if (res != Result::OK) {
  126. LOG(ERROR) << "failed to disable sensor";
  127. }
  128. }
  129. }
  130. /*
  131. * Free the event queue.
  132. * kernel calls decStrong() on server side implementation of IEventQueue,
  133. * hence resources (including the callback) are freed as well.
  134. */
  135. queue = nullptr;
  136. // Should never reach this
  137. return EXIT_SUCCESS;
  138. }