NonUiNotifier.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 touchFd = open(TOUCH_DEV_PATH, O_RDWR);
  45. if (touchFd == -1) {
  46. LOG(ERROR) << "failed to open " << TOUCH_DEV_PATH;
  47. return Void();
  48. }
  49. int buf[MAX_BUF_SIZE] = {0, Touch_Nonui_Mode, static_cast<int>(e.u.scalar)};
  50. ioctl(touchFd, TOUCH_IOC_SET_CUR_VALUE, &buf);
  51. close(touchFd);
  52. return Void();
  53. }
  54. };
  55. } // namespace
  56. int main() {
  57. Result res;
  58. sp<IEventQueue> queue;
  59. sp<ISensorManager> manager = ISensorManager::getService();
  60. if (manager == nullptr) {
  61. LOG(ERROR) << "failed to get ISensorManager";
  62. return EXIT_FAILURE;
  63. }
  64. std::vector<SensorInfo> sensorList;
  65. manager->getSensorList([&sensorList, &res](const auto& l, auto r) {
  66. sensorList = l;
  67. res = r;
  68. });
  69. if (res != Result::OK) {
  70. LOG(ERROR) << "failed to get getSensorList";
  71. return EXIT_FAILURE;
  72. }
  73. auto it = std::find_if(sensorList.begin(), sensorList.end(), [](const SensorInfo& sensor) {
  74. return (sensor.typeAsString == SENSOR_NAME_XIAOMI_SENSOR_NONUI) &&
  75. (sensor.flags & SensorFlagBits::WAKE_UP);
  76. });
  77. int32_t sensorHandle = -1;
  78. if (it != sensorList.end()) {
  79. sensorHandle = it->sensorHandle;
  80. } else {
  81. LOG(ERROR) << "failed to get wake-up version of nonui sensor";
  82. return EXIT_FAILURE;
  83. }
  84. sensorList.clear();
  85. manager->createEventQueue(new NonUiSensorCallback(), [&queue, &res](const auto& q, auto r) {
  86. queue = q;
  87. res = r;
  88. });
  89. if (res != Result::OK) {
  90. LOG(ERROR) << "failed to create event queue";
  91. return EXIT_FAILURE;
  92. }
  93. // Enable states of touchscreen sensors
  94. const std::vector<const char*> paths = {
  95. "/sys/class/touch/touch_dev/fod_longpress_gesture_enabled",
  96. "/sys/class/touch/touch_dev/gesture_single_tap_enabled",
  97. "/sys/class/touch/touch_dev/gesture_double_tap_enabled"};
  98. pollfd* pollfds = new pollfd[paths.size()];
  99. for (size_t i = 0; i < paths.size(); ++i) {
  100. int fd = open(paths[i], O_RDONLY);
  101. if (fd < 0) {
  102. LOG(ERROR) << "failed to open " << paths[i] << " , err: " << fd;
  103. return EXIT_FAILURE;
  104. }
  105. pollfds[i].fd = fd;
  106. pollfds[i].events = POLLERR | POLLPRI;
  107. pollfds[i].revents = 0;
  108. }
  109. while (true) {
  110. int rc = poll(pollfds, paths.size(), -1);
  111. if (rc < 0) {
  112. LOG(ERROR) << "failed to poll, err: " << rc;
  113. continue;
  114. }
  115. for (size_t i = 0; i < paths.size(); ++i) {
  116. if (pollfds[i].revents & (POLLERR | POLLPRI)) {
  117. LOG(VERBOSE) << "polled change on " << paths[i];
  118. }
  119. }
  120. bool enabled = false;
  121. for (size_t i = 0; i < paths.size(); ++i) {
  122. enabled = enabled || readBool(pollfds[i].fd);
  123. }
  124. if (enabled) {
  125. res = queue->enableSensor(sensorHandle, 20000 /* sample period */, 0 /* latency */);
  126. if (res != Result::OK) {
  127. LOG(ERROR) << "failed to enable sensor";
  128. }
  129. } else {
  130. res = queue->disableSensor(sensorHandle);
  131. if (res != Result::OK) {
  132. LOG(ERROR) << "failed to disable sensor";
  133. }
  134. }
  135. }
  136. /*
  137. * Free the event queue.
  138. * kernel calls decStrong() on server side implementation of IEventQueue,
  139. * hence resources (including the callback) are freed as well.
  140. */
  141. queue = nullptr;
  142. // Should never reach this
  143. return EXIT_SUCCESS;
  144. }