232 lines
5.7 KiB
C
232 lines
5.7 KiB
C
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <glob.h>
|
|
#include <linux/input.h>
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/ioctl.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
static volatile sig_atomic_t g_stop = 0;
|
|
|
|
static void handle_signal(int sig) {
|
|
(void)sig;
|
|
g_stop = 1;
|
|
}
|
|
|
|
static void get_sxmo_config_dir(char* out, size_t out_sz) {
|
|
const char* cfg = getenv("XDG_CONFIG_HOME");
|
|
const char* home = getenv("HOME");
|
|
if (cfg && *cfg) {
|
|
snprintf(out, out_sz, "%s/sxmo", cfg);
|
|
} else if (home && *home) {
|
|
snprintf(out, out_sz, "%s/.config/sxmo", home);
|
|
} else {
|
|
snprintf(out, out_sz, "/home/user/.config/sxmo");
|
|
}
|
|
}
|
|
|
|
static int is_vibrate_muted(void) {
|
|
const char* force = getenv("SXMO_VIBRATE_FORCE");
|
|
if (force && *force == '1') {
|
|
return 0;
|
|
}
|
|
char dir[448];
|
|
char path[512];
|
|
get_sxmo_config_dir(dir, sizeof(dir));
|
|
snprintf(path, sizeof(path), "%s/.novibrate", dir);
|
|
return access(path, F_OK) == 0;
|
|
}
|
|
|
|
static int read_configured_level(void) {
|
|
char dir[448];
|
|
char path[512];
|
|
get_sxmo_config_dir(dir, sizeof(dir));
|
|
snprintf(path, sizeof(path), "%s/ringsettings", dir);
|
|
|
|
FILE* fp = fopen(path, "r");
|
|
if (!fp) return 0;
|
|
|
|
int level = 0;
|
|
char line[256];
|
|
while (fgets(line, sizeof(line), fp)) {
|
|
char* p = line;
|
|
while (*p == ' ' || *p == '\t') ++p;
|
|
if (strncmp(p, "SXMO_VIBRATE_STRENGTH=", 22) == 0) {
|
|
int v = atoi(p + 22);
|
|
if (v >= 1 && v <= 3) level = v;
|
|
else if (v > 3 && v <= 16000) level = 1;
|
|
else if (v > 16000 && v <= 45000) level = 2;
|
|
else if (v > 45000) level = 3;
|
|
}
|
|
}
|
|
fclose(fp);
|
|
return level;
|
|
}
|
|
|
|
static int resolve_level_and_magnitude(int raw_arg, int has_arg, int* level_out) {
|
|
int val = raw_arg;
|
|
const char* env_str = getenv("SXMO_VIBRATE_STRENGTH");
|
|
if (!has_arg || (val == 1 && (!env_str || !*env_str))) {
|
|
int cfg = read_configured_level();
|
|
if (cfg >= 1 && cfg <= 3) {
|
|
val = cfg;
|
|
} else if (env_str && *env_str) {
|
|
int ev = atoi(env_str);
|
|
if (ev >= 1) val = ev;
|
|
} else if (!has_arg) {
|
|
val = 2;
|
|
}
|
|
}
|
|
|
|
int level = 2;
|
|
int mag = 32000;
|
|
if (val <= 1 || (val >= 256 && val <= 16000)) {
|
|
level = 1;
|
|
mag = 14000;
|
|
} else if (val == 2 || (val > 16000 && val <= 45000)) {
|
|
level = 2;
|
|
mag = 32000;
|
|
} else {
|
|
level = 3;
|
|
mag = 65535;
|
|
}
|
|
|
|
if (level_out) *level_out = level;
|
|
return mag;
|
|
}
|
|
|
|
static int open_vibrator_device(void) {
|
|
const char* env_dev = getenv("SXMO_VIBRATE_DEV");
|
|
if (env_dev && *env_dev) {
|
|
int fd = open(env_dev, O_RDWR | O_CLOEXEC);
|
|
if (fd >= 0) return fd;
|
|
}
|
|
|
|
int fd = open("/dev/input/by-path/platform-vibrator-event", O_RDWR | O_CLOEXEC);
|
|
if (fd >= 0) return fd;
|
|
|
|
glob_t g;
|
|
memset(&g, 0, sizeof(g));
|
|
if (glob("/dev/input/by-path/*vibrator*-event", 0, NULL, &g) == 0) {
|
|
for (size_t i = 0; i < g.gl_pathc; ++i) {
|
|
fd = open(g.gl_pathv[i], O_RDWR | O_CLOEXEC);
|
|
if (fd >= 0) {
|
|
globfree(&g);
|
|
return fd;
|
|
}
|
|
}
|
|
globfree(&g);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static void sleep_ms_interruptible(long ms) {
|
|
if (ms <= 0 || g_stop) return;
|
|
struct timespec req;
|
|
req.tv_sec = ms / 1000;
|
|
req.tv_nsec = (ms % 1000) * 1000000L;
|
|
while (!g_stop && nanosleep(&req, &req) == -1 && errno == EINTR) {
|
|
}
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 2 || argc > 3) {
|
|
fprintf(stderr, "Usage: sxmo_vibrate duration_ms [strength_number]\n");
|
|
return 1;
|
|
}
|
|
|
|
char* endp = NULL;
|
|
errno = 0;
|
|
long duration_ms = strtol(argv[1], &endp, 10);
|
|
if (errno != 0 || !endp || *endp != '\0' || duration_ms <= 0) {
|
|
fprintf(stderr, "sxmo_vibrate: invalid duration\n");
|
|
return 1;
|
|
}
|
|
|
|
if (is_vibrate_muted()) {
|
|
return 0;
|
|
}
|
|
|
|
int raw_strength = 2;
|
|
int has_strength = 0;
|
|
if (argc == 3) {
|
|
endp = NULL;
|
|
errno = 0;
|
|
long s = strtol(argv[2], &endp, 10);
|
|
if (errno == 0 && endp && *endp == '\0' && s >= 1 && s <= 65535) {
|
|
raw_strength = (int)s;
|
|
has_strength = 1;
|
|
}
|
|
}
|
|
|
|
int level = 2;
|
|
int magnitude = resolve_level_and_magnitude(raw_strength, has_strength, &level);
|
|
|
|
struct sigaction sa;
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.sa_handler = handle_signal;
|
|
sigemptyset(&sa.sa_mask);
|
|
sigaction(SIGINT, &sa, NULL);
|
|
sigaction(SIGTERM, &sa, NULL);
|
|
sigaction(SIGHUP, &sa, NULL);
|
|
|
|
int fd = open_vibrator_device();
|
|
if (fd < 0) {
|
|
fprintf(stderr, "sxmo_vibrate: cannot open vibrator device: %s\n",
|
|
strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
struct ff_effect effect;
|
|
memset(&effect, 0, sizeof(effect));
|
|
effect.type = FF_RUMBLE;
|
|
effect.id = -1;
|
|
effect.u.rumble.strong_magnitude = (unsigned short)magnitude;
|
|
effect.u.rumble.weak_magnitude = (unsigned short)magnitude;
|
|
|
|
if (ioctl(fd, EVIOCSFF, &effect) < 0) {
|
|
close(fd);
|
|
return 1;
|
|
}
|
|
|
|
struct input_event play;
|
|
memset(&play, 0, sizeof(play));
|
|
play.type = EV_FF;
|
|
play.code = (unsigned short)effect.id;
|
|
|
|
if (level >= 3 || duration_ms <= 90) {
|
|
long actual_ms = (level == 1 && duration_ms > 55) ? 55 : duration_ms;
|
|
play.value = 1;
|
|
if (write(fd, &play, sizeof(play)) >= 0) {
|
|
sleep_ms_interruptible(actual_ms);
|
|
}
|
|
} else {
|
|
long on_ms = (level == 1) ? 75 : 160;
|
|
long off_ms = (level == 1) ? 125 : 80;
|
|
long rem = duration_ms;
|
|
while (rem > 0 && !g_stop) {
|
|
long cur_on = (rem < on_ms) ? rem : on_ms;
|
|
play.value = 1;
|
|
if (write(fd, &play, sizeof(play)) < 0) break;
|
|
sleep_ms_interruptible(cur_on);
|
|
rem -= cur_on;
|
|
play.value = 0;
|
|
(void)write(fd, &play, sizeof(play));
|
|
if (rem <= 0 || g_stop) break;
|
|
long cur_off = (rem < off_ms) ? rem : off_ms;
|
|
sleep_ms_interruptible(cur_off);
|
|
rem -= cur_off;
|
|
}
|
|
}
|
|
|
|
play.value = 0;
|
|
(void)write(fd, &play, sizeof(play));
|
|
(void)ioctl(fd, EVIOCRMFF, effect.id);
|
|
close(fd);
|
|
return 0;
|
|
}
|