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
diff --git a/minuitwrp/events.cpp b/minuitwrp/events.cpp
index 94b800e..bd29f71 100644
--- a/minuitwrp/events.cpp
+++ b/minuitwrp/events.cpp
@@ -26,7 +26,7 @@
#include <unistd.h>
#include <stdio.h>
#include <string.h>
-
+#include <fstream>
#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 @@
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;
}