From c7c78d2fea4e9a53da09657ccb4155ec1344bd03 Mon Sep 17 00:00:00 2001 From: notsyncing Date: Wed, 6 Jun 2018 20:26:47 +0800 Subject: minuitwrp: Support haptics drivers registered with LED class framework This type of vibrator is found on newer kernel versions (4.9+) and registers with LED class framework (located at /sys/class/leds/vibrator). Change-Id: I85e93fdac17b3f4b6f2ae689bbbd490806b5c29b --- minuitwrp/events.cpp | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'minuitwrp') diff --git a/minuitwrp/events.cpp b/minuitwrp/events.cpp index 94b800ea6..bd29f718d 100644 --- a/minuitwrp/events.cpp +++ b/minuitwrp/events.cpp @@ -26,7 +26,7 @@ #include #include #include - +#include #include "../common.h" @@ -39,6 +39,9 @@ #define VIBRATOR_TIMEOUT_FILE "/sys/class/timed_output/vibrator/enable" #define VIBRATOR_TIME_MS 50 +#define LEDS_HAPTICS_DURATION_FILE "/sys/class/leds/vibrator/duration" +#define LEDS_HAPTICS_ACTIVATE_FILE "/sys/class/leds/vibrator/activate" + #ifndef SYN_REPORT #define SYN_REPORT 0x00 #endif @@ -108,24 +111,27 @@ static inline int ABS(int x) { return x<0?-x:x; } +int write_to_file(const std::string& fn, const std::string& line) { + FILE *file; + file = fopen(fn.c_str(), "w"); + if (file != NULL) { + fwrite(line.c_str(), line.size(), 1, file); + fclose(file); + return 0; + } + LOGI("Cannot find file %s\n", fn.c_str()); + return -1; +} + int vibrate(int timeout_ms) { - char str[20]; - int fd; - int ret; - if (timeout_ms > 10000) timeout_ms = 1000; - fd = open(VIBRATOR_TIMEOUT_FILE, O_WRONLY); - if (fd < 0) - return -1; - - ret = snprintf(str, sizeof(str), "%d", timeout_ms); - ret = write(fd, str, ret); - close(fd); - - if (ret < 0) - return -1; + if (std::ifstream(LEDS_HAPTICS_ACTIVATE_FILE).good()) { + write_to_file(LEDS_HAPTICS_DURATION_FILE, std::to_string(timeout_ms)); + write_to_file(LEDS_HAPTICS_ACTIVATE_FILE, "1"); + } else + write_to_file(VIBRATOR_TIMEOUT_FILE, std::to_string(timeout_ms)); return 0; } -- cgit v1.2.3