NonUiNotifier.cpp 4.9 KB

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