NonUiNotifier.cpp 4.9 KB

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