NonUiNotifier.cpp 4.8 KB

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