#include #include #include #include #include #include #include #include #include #include static int read_configured_level(void) { char path[512]; const char* cfg = getenv("XDG_CONFIG_HOME"); const char* home = getenv("HOME"); if (cfg && *cfg) { snprintf(path, sizeof(path), "%s/sxmo/ringsettings", cfg); } else if (home && *home) { snprintf(path, sizeof(path), "%s/.config/sxmo/ringsettings", home); } else { snprintf(path, sizeof(path), "/home/user/.config/sxmo/ringsettings"); } 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; } } fclose(fp); return level; } static int resolve_magnitude(int raw_arg, int has_arg) { int val = raw_arg; if (!has_arg || (val == 1 && !getenv("SXMO_VIBRATE_STRENGTH"))) { int cfg = read_configured_level(); if (cfg >= 1 && cfg <= 3) val = cfg; } if (val <= 1) return 8000; if (val == 2) return 32000; if (val == 3) return 65535; if (val < 256) return val * 257; if (val > 65535) return 65535; return val; } 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; } 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; } 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 magnitude = resolve_magnitude(raw_strength, has_strength); 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; play.value = 3; if (write(fd, &play, sizeof(play)) < 0) { ioctl(fd, EVIOCRMFF, effect.id); close(fd); return 1; } struct timespec req; req.tv_sec = duration_ms / 1000; req.tv_nsec = (duration_ms % 1000) * 1000000L; while (nanosleep(&req, &req) == -1 && errno == EINTR) { } play.value = 0; (void)write(fd, &play, sizeof(play)); (void)ioctl(fd, EVIOCRMFF, effect.id); close(fd); return 0; }