NonUiNotifier.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C) 2024 The LineageOS Project
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #define LOG_TAG "NonUiNotifier"
  7. #include "NonUiNotifier.h"
  8. #include <android-base/logging.h>
  9. #include <android-base/unique_fd.h>
  10. #include <linux/xiaomi_touch.h>
  11. #include <poll.h>
  12. #include <sys/ioctl.h>
  13. #include "SensorNotifierUtils.h"
  14. static const std::string kTouchDevice = "/dev/xiaomi-touch";
  15. using android::hardware::Return;
  16. using android::hardware::Void;
  17. using android::hardware::sensors::V1_0::Event;
  18. namespace {
  19. class NonUiSensorCallback : public IEventQueueCallback {
  20. public:
  21. NonUiSensorCallback() {
  22. touch_fd_ = android::base::unique_fd(open(kTouchDevice.c_str(), O_RDWR));
  23. if (touch_fd_.get() == -1) {
  24. LOG(ERROR) << "failed to open " << kTouchDevice;
  25. }
  26. }
  27. Return<void> onEvent(const Event& e) {
  28. struct touch_mode_request request = {
  29. .mode = TOUCH_MODE_NONUI_MODE,
  30. .value = static_cast<int>(e.u.scalar),
  31. };
  32. ioctl(touch_fd_.get(), TOUCH_IOC_SET_CUR_VALUE, &request);
  33. return Void();
  34. }
  35. private:
  36. android::base::unique_fd touch_fd_;
  37. };
  38. } // namespace
  39. NonUiNotifier::NonUiNotifier(sp<ISensorManager> manager) : SensorNotifier(manager) {
  40. initializeSensorQueue("xiaomi.sensor.nonui", true, new NonUiSensorCallback());
  41. }
  42. NonUiNotifier::~NonUiNotifier() {
  43. deactivate();
  44. }
  45. void NonUiNotifier::notify() {
  46. Result res;
  47. // Enable states of touchscreen sensors
  48. const std::vector<const char*> paths = {
  49. "/sys/class/touch/touch_dev/fod_longpress_gesture_enabled",
  50. "/sys/class/touch/touch_dev/gesture_single_tap_enabled",
  51. "/sys/class/touch/touch_dev/gesture_double_tap_enabled"};
  52. pollfd* pollfds = new pollfd[paths.size()];
  53. for (size_t i = 0; i < paths.size(); ++i) {
  54. int fd = open(paths[i], O_RDONLY);
  55. if (fd < 0) {
  56. LOG(ERROR) << "failed to open " << paths[i] << " , err: " << fd;
  57. mActive = false;
  58. return;
  59. }
  60. pollfds[i].fd = fd;
  61. pollfds[i].events = POLLPRI;
  62. }
  63. while (mActive) {
  64. int rc = poll(pollfds, paths.size(), -1);
  65. if (rc < 0) {
  66. LOG(ERROR) << "failed to poll, err: " << rc;
  67. continue;
  68. }
  69. bool enabled = false;
  70. for (size_t i = 0; i < paths.size(); ++i) {
  71. enabled = enabled || readBool(pollfds[i].fd);
  72. }
  73. if (enabled) {
  74. res = mQueue->enableSensor(mSensorHandle, 20000 /* sample period */, 0 /* latency */);
  75. if (res != Result::OK) {
  76. LOG(ERROR) << "failed to enable sensor";
  77. }
  78. } else {
  79. res = mQueue->disableSensor(mSensorHandle);
  80. if (res != Result::OK) {
  81. LOG(DEBUG) << "failed to disable sensor";
  82. }
  83. }
  84. }
  85. }