NonUiNotifier.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. int32_t sensorHandle = -1;
  63. manager->getDefaultSensor(static_cast<SensorType>(SENSOR_TYPE_XIAOMI_SENSOR_NONUI),
  64. [&sensorHandle](const auto& info, auto r) {
  65. sensorHandle = info.sensorHandle;
  66. });
  67. manager->createEventQueue(new NonUiSensorCallback(), [&queue, &res](const auto& q, auto r) {
  68. queue = q;
  69. res = r;
  70. });
  71. if (res != Result::OK) {
  72. LOG(ERROR) << "failed to create event queue";
  73. return EXIT_FAILURE;
  74. }
  75. // Enable states of touchscreen sensors
  76. const std::vector<const char*> paths = {
  77. "/sys/class/touch/touch_dev/fod_longpress_gesture_enabled",
  78. "/sys/class/touch/touch_dev/gesture_single_tap_enabled",
  79. "/sys/class/touch/touch_dev/gesture_double_tap_enabled"};
  80. pollfd* pollfds = new pollfd[paths.size()];
  81. for (size_t i = 0; i < paths.size(); ++i) {
  82. int fd = open(paths[i], O_RDONLY);
  83. if (fd < 0) {
  84. LOG(ERROR) << "failed to open " << paths[i] << " , err: " << fd;
  85. return EXIT_FAILURE;
  86. }
  87. pollfds[i].fd = fd;
  88. pollfds[i].events = POLLERR | POLLPRI;
  89. pollfds[i].revents = 0;
  90. }
  91. while (true) {
  92. int rc = poll(pollfds, paths.size(), -1);
  93. if (rc < 0) {
  94. LOG(ERROR) << "failed to poll, err: " << rc;
  95. continue;
  96. }
  97. for (size_t i = 0; i < paths.size(); ++i) {
  98. if (pollfds[i].revents & (POLLERR | POLLPRI)) {
  99. LOG(VERBOSE) << "polled change on " << paths[i];
  100. }
  101. }
  102. bool enabled = false;
  103. for (size_t i = 0; i < paths.size(); ++i) {
  104. enabled = enabled || readBool(pollfds[i].fd);
  105. }
  106. if (enabled) {
  107. res = queue->enableSensor(sensorHandle, 20000 /* sample period */, 0 /* latency */);
  108. if (res != Result::OK) {
  109. LOG(ERROR) << "failed to enable sensor";
  110. }
  111. } else {
  112. res = queue->disableSensor(sensorHandle);
  113. if (res != Result::OK) {
  114. LOG(ERROR) << "failed to disable sensor";
  115. }
  116. }
  117. }
  118. /*
  119. * Free the event queue.
  120. * kernel calls decStrong() on server side implementation of IEventQueue,
  121. * hence resources (including the callback) are freed as well.
  122. */
  123. queue = nullptr;
  124. // Should never reach this
  125. return EXIT_SUCCESS;
  126. }