aidl: vibrator: properly implement effects
authorTim Zimmermann <tim@linux4.de>
Mon, 31 Jan 2022 11:01:42 +0000 (12:01 +0100)
committerJan Altensen <info@stricted.net>
Tue, 29 Mar 2022 22:44:18 +0000 (00:44 +0200)
Change-Id: I947c99d05b7227839089cfd5fc5e337033b8e5ba

aidl/vibrator/Vibrator.cpp
aidl/vibrator/Vibrator.h
aidl/vibrator/android.hardware.vibrator-service.samsung.rc

index f21eee3119e6570d2ae925519d2a64a6fb1bde5e..95d5aec6b8fcdc818a70ba74510de8e3a08c3070 100644 (file)
@@ -11,6 +11,7 @@
 #include <cmath>
 #include <fstream>
 #include <iostream>
+#include <map>
 #include <thread>
 
 namespace aidl {
@@ -18,6 +19,14 @@ namespace android {
 namespace hardware {
 namespace vibrator {
 
+static std::map<Effect, int> CP_TRIGGER_EFFECTS {
+    { Effect::CLICK, 10 },
+    { Effect::DOUBLE_CLICK, 14 },
+    { Effect::HEAVY_CLICK, 23 },
+    { Effect::TEXTURE_TICK, 50 },
+    { Effect::TICK, 50 }
+};
+
 /*
  * Write value to path and close file.
  */
@@ -48,6 +57,7 @@ static bool nodeExists(const std::string& path) {
 Vibrator::Vibrator() {
     mIsTimedOutVibrator = nodeExists(VIBRATOR_TIMEOUT_PATH);
     mHasTimedOutIntensity = nodeExists(VIBRATOR_INTENSITY_PATH);
+    mHasTimedOutEffect = nodeExists(VIBRATOR_CP_TRIGGER_PATH);
 }
 
 ndk::ScopedAStatus Vibrator::getCapabilities(int32_t* _aidl_return) {
@@ -68,13 +78,18 @@ ndk::ScopedAStatus Vibrator::off() {
 }
 
 ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs, const std::shared_ptr<IVibratorCallback>& callback) {
-    ndk::ScopedAStatus status = activate(timeoutMs);
+    ndk::ScopedAStatus status;
+
+    if (mHasTimedOutEffect)
+        writeNode(VIBRATOR_CP_TRIGGER_PATH, 0); // Clear all effects
+
+    status = activate(timeoutMs);
 
     if (callback != nullptr) {
         std::thread([=] {
-            LOG(INFO) << "Starting on on another thread";
+            LOG(DEBUG) << "Starting on on another thread";
             usleep(timeoutMs * 1000);
-            LOG(INFO) << "Notifying on complete";
+            LOG(DEBUG) << "Notifying on complete";
             if (!callback->onComplete().isOk()) {
                 LOG(ERROR) << "Failed to call onComplete";
             }
@@ -86,26 +101,34 @@ ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs, const std::shared_ptr<IVibrat
 
 ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength, const std::shared_ptr<IVibratorCallback>& callback, int32_t* _aidl_return) {
     ndk::ScopedAStatus status;
-    uint8_t amplitude;
-    uint32_t ms;
+    uint32_t amplitude = strengthToAmplitude(strength, &status);
+    uint32_t ms = 1000;
 
-    amplitude = strengthToAmplitude(strength, &status);
-    if (!status.isOk()) {
+    if (!status.isOk())
         return status;
-    }
+
+    activate(0);
     setAmplitude(amplitude);
 
-    ms = effectToMs(effect, &status);
-    if (!status.isOk()) {
-        return status;
+    if (mHasTimedOutEffect && CP_TRIGGER_EFFECTS.find(effect) != CP_TRIGGER_EFFECTS.end()) {
+        writeNode(VIBRATOR_CP_TRIGGER_PATH, CP_TRIGGER_EFFECTS[effect]);
+    } else {
+        if (mHasTimedOutEffect)
+            writeNode(VIBRATOR_CP_TRIGGER_PATH, 0); // Clear previous effect
+
+        ms = effectToMs(effect, &status);
+
+        if (!status.isOk())
+            return status;
     }
+
     status = activate(ms);
 
     if (callback != nullptr) {
         std::thread([=] {
-            LOG(INFO) << "Starting perform on another thread";
+            LOG(DEBUG) << "Starting perform on another thread";
             usleep(ms * 1000);
-            LOG(INFO) << "Notifying perform complete";
+            LOG(DEBUG) << "Notifying perform complete";
             callback->onComplete();
         }).detach();
     }
@@ -255,19 +278,18 @@ uint8_t Vibrator::strengthToAmplitude(EffectStrength strength, ndk::ScopedAStatu
 
 uint32_t Vibrator::effectToMs(Effect effect, ndk::ScopedAStatus* status) {
     *status = ndk::ScopedAStatus::ok();
-
     switch (effect) {
         case Effect::CLICK:
-            return 20;
+            return 10;
         case Effect::DOUBLE_CLICK:
-            return 25;
-        case Effect::HEAVY_CLICK:
-            return 30;
+            return 15;
         case Effect::TICK:
         case Effect::TEXTURE_TICK:
         case Effect::THUD:
         case Effect::POP:
-            return 15;
+            return 5;
+        case Effect::HEAVY_CLICK:
+            return 10;
         case Effect::RINGTONE_1:
         case Effect::RINGTONE_2:
         case Effect::RINGTONE_3:
@@ -283,9 +305,8 @@ uint32_t Vibrator::effectToMs(Effect effect, ndk::ScopedAStatus* status) {
         case Effect::RINGTONE_13:
         case Effect::RINGTONE_14:
         case Effect::RINGTONE_15:
-            return 300;
+            return 30000;
     }
-
     *status = ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
     return 0;
 }
index 6770527538ee017d542ed411ec3911ed1be399b0..785ad9d864234ccbcdbe6f348dafbd5e6a2465db 100644 (file)
@@ -14,6 +14,7 @@
 
 #define VIBRATOR_TIMEOUT_PATH "/sys/class/timed_output/vibrator/enable"
 #define VIBRATOR_INTENSITY_PATH "/sys/class/timed_output/vibrator/intensity"
+#define VIBRATOR_CP_TRIGGER_PATH "/sys/class/timed_output/vibrator/cp_trigger_index"
 
 using ::aidl::android::hardware::vibrator::IVibratorCallback;
 using ::aidl::android::hardware::vibrator::Braking;
@@ -67,6 +68,7 @@ private:
 
     bool mIsTimedOutVibrator;
     bool mHasTimedOutIntensity;
+    bool mHasTimedOutEffect;
 };
 
 } // namespace vibrator
index 3f439057d21369ecc10e56276334ade08d22281e..f211456f8f107df73d6bf5d9dd42d83488531f39 100644 (file)
@@ -1,3 +1,8 @@
+on init
+    chown system system /sys/class/timed_output/vibrator/cp_trigger_index
+    chown system system /sys/class/timed_output/vibrator/enable
+    chown system system /sys/class/timed_output/vibrator/intensity
+
 service vendor.vibrator-default /vendor/bin/hw/android.hardware.vibrator-service.samsung
     class hal
     user system