NonUiNotifier.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. int buf[MAX_BUF_SIZE] = {0, Touch_Nonui_Mode, static_cast<int>(e.u.scalar)};
  29. ioctl(touch_fd_.get(), TOUCH_IOC_SET_CUR_VALUE, &buf);
  30. return Void();
  31. }
  32. private:
  33. android::base::unique_fd touch_fd_;
  34. };
  35. } // namespace
  36. NonUiNotifier::NonUiNotifier(sp<ISensorManager> manager) : SensorNotifier(manager) {
  37. initializeSensorQueue("xiaomi.sensor.nonui", true, new NonUiSensorCallback());
  38. }
  39. NonUiNotifier::~NonUiNotifier() {
  40. deactivate();
  41. }
  42. void NonUiNotifier::notify() {
  43. Result res;
  44. // Enable states of touchscreen sensors
  45. const std::vector<const char*> paths = {
  46. "/sys/class/touch/touch_dev/fod_longpress_gesture_enabled",
  47. "/sys/class/touch/touch_dev/gesture_single_tap_enabled",
  48. "/sys/class/touch/touch_dev/gesture_double_tap_enabled"};
  49. pollfd* pollfds = new pollfd[paths.size()];
  50. for (size_t i = 0; i < paths.size(); ++i) {
  51. int fd = open(paths[i], O_RDONLY);
  52. if (fd < 0) {
  53. LOG(ERROR) << "failed to open " << paths[i] << " , err: " << fd;
  54. mActive = false;
  55. return;
  56. }
  57. pollfds[i].fd = fd;
  58. pollfds[i].events = POLLPRI;
  59. }
  60. while (mActive) {
  61. int rc = poll(pollfds, paths.size(), -1);
  62. if (rc < 0) {
  63. LOG(ERROR) << "failed to poll, err: " << rc;
  64. continue;
  65. }
  66. bool enabled = false;
  67. for (size_t i = 0; i < paths.size(); ++i) {
  68. enabled = enabled || readBool(pollfds[i].fd);
  69. }
  70. if (enabled) {
  71. res = mQueue->enableSensor(mSensorHandle, 20000 /* sample period */, 0 /* latency */);
  72. if (res != Result::OK) {
  73. LOG(ERROR) << "failed to enable sensor";
  74. }
  75. } else {
  76. res = mQueue->disableSensor(mSensorHandle);
  77. if (res != Result::OK) {
  78. LOG(DEBUG) << "failed to disable sensor";
  79. }
  80. }
  81. }
  82. }