oops many changes

This commit is contained in:
sankalp committed 2025-10-13 23:03:22 +05:45
1 parent 028785ac27
commit 4343cf56bf
25 files changed
+1413 -1234

No files matched your search

+164
View File
@@ -0,0 +1,164 @@
#define _POSIX_C_SOURCE 200809L
#include "app/process.h"
#include "common/utils.h"
#include "config.h"
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#define MAX_PATH_LENGTH 1024
pid_t keyboard_pid = 0;
pid_t sound_pid = 0;
char pidfile_path[MAX_PATH_LENGTH] = {0};
int is_daemon = 0;
volatile sig_atomic_t reload_requested = 0;
void cleanup_processes(int sig) {
(void)sig;
if (!is_daemon) {
printf("\nShutting down KeyVibe...\n");
}
if (sound_pid > 0) {
kill(sound_pid, SIGTERM);
waitpid(sound_pid, NULL, 0);
}
if (keyboard_pid > 0) {
kill(keyboard_pid, SIGTERM);
waitpid(keyboard_pid, NULL, 0);
}
if (pidfile_path[0] != '\0') {
unlink(pidfile_path);
}
exit(0);
}
int require_running_pid(pid_t *out_pid) {
build_pidfile_path(pidfile_path, sizeof(pidfile_path));
pid_t running_pid = 0;
if (!read_pidfile(pidfile_path, &running_pid) ||
!process_is_running(running_pid)) {
fprintf(stderr, "KeyVibe: not running.\n");
return 0;
}
*out_pid = running_pid;
return 1;
}
void daemonize_self(void) {
pid_t pid = fork();
if (pid < 0)
exit(1);
if (pid > 0)
exit(0);
if (setsid() < 0)
exit(1);
pid = fork();
if (pid < 0)
exit(1);
if (pid > 0)
exit(0);
umask(0);
chdir("/");
int fd = open("/dev/null", O_RDWR);
if (fd >= 0) {
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd > 2)
close(fd);
}
}
void stop_children(void) {
if (sound_pid > 0) {
kill(sound_pid, SIGTERM);
waitpid(sound_pid, NULL, 0);
sound_pid = 0;
}
if (keyboard_pid > 0) {
kill(keyboard_pid, SIGTERM);
waitpid(keyboard_pid, NULL, 0);
keyboard_pid = 0;
}
}
int start_children(const char *sound_dir, const char *config_path, int volume,
int verbose, int mute, const char *mouse_sound_dir,
const char *mouse_config_path, int mouse_volume,
int keyboard_mute, int mouse_mute) {
(void)config_path;
(void)mouse_sound_dir;
int pipefd[2];
if (pipe(pipefd) == -1) {
perror("pipe");
return 0;
}
sound_pid = fork();
if (sound_pid == -1) {
perror("fork");
close(pipefd[0]);
close(pipefd[1]);
return 0;
}
char sound_player_path[MAX_PATH_LENGTH];
snprintf(sound_player_path, sizeof(sound_player_path), "%s/keyvibe-audio",
KeyVibe_BIN_DIR);
if (sound_pid == 0) {
close(pipefd[1]);
dup2(pipefd[0], STDIN_FILENO);
close(pipefd[0]);
if (chdir(sound_dir) != 0) {
perror("chdir");
exit(1);
}
char volume_str[32];
snprintf(volume_str, sizeof(volume_str), "%d", volume);
char mouse_volume_str[32];
snprintf(mouse_volume_str, sizeof(mouse_volume_str), "%d", mouse_volume);
char verbose_str[8];
snprintf(verbose_str, sizeof(verbose_str), "%d", verbose);
char mute_str[8];
snprintf(mute_str, sizeof(mute_str), "%d", mute);
char keyboard_mute_str[8];
snprintf(keyboard_mute_str, sizeof(keyboard_mute_str), "%d", keyboard_mute);
char mouse_mute_str[8];
snprintf(mouse_mute_str, sizeof(mouse_mute_str), "%d", mouse_mute);
execl(sound_player_path, "keyvibe-audio", "config.json", volume_str,
verbose_str, mute_str, mouse_config_path, mouse_volume_str,
keyboard_mute_str, mouse_mute_str, (char *)NULL);
perror("execl keyvibe-audio");
exit(1);
}
keyboard_pid = fork();
if (keyboard_pid == -1) {
perror("fork");
kill(sound_pid, SIGTERM);
close(pipefd[0]);
close(pipefd[1]);
return 0;
}
char get_key_presses_path[MAX_PATH_LENGTH];
snprintf(get_key_presses_path, sizeof(get_key_presses_path),
"%s/keyvibe-input", KeyVibe_BIN_DIR);
if (keyboard_pid == 0) {
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[1]);
execl(get_key_presses_path, "keyvibe-input", (char *)NULL);
perror("execl keyvibe-input");
exit(1);
}
close(pipefd[0]);
close(pipefd[1]);
return 1;
}
void handle_sighup(int sig) {
(void)sig;
reload_requested = 1;
}
+95
View File
@@ -0,0 +1,95 @@
#include "app/reload.h"
#include "app/process.h"
#include "soundpacks.h"
#include "user_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int handle_reload(const char *user_cfg_path, char *current_sound_name,
char *current_mouse_sound_name, int *current_keyboard_volume,
int *current_mouse_volume, char *current_config_path,
char *current_sound_dir, char *current_mouse_config_path,
char *current_mouse_sound_dir, int current_mute,
int current_verbose, int current_mouse_volume_arg,
int current_keyboard_mute, int current_mouse_mute) {
char *cfg_keyboard_sound = NULL;
char *cfg_mouse_sound = NULL;
int cfg_keyboard_volume = *current_keyboard_volume;
int cfg_mouse_volume = *current_mouse_volume;
if (read_user_config(user_cfg_path, &cfg_keyboard_sound, &cfg_mouse_sound,
&cfg_keyboard_volume, &cfg_mouse_volume)) {
printf("Read config - keyboard:%s mouse:%s kvol:%d mvol:%d (current "
"kvol:%d mvol:%d)\n",
cfg_keyboard_sound ? cfg_keyboard_sound : "NULL",
cfg_mouse_sound ? cfg_mouse_sound : "NULL", cfg_keyboard_volume,
cfg_mouse_volume, *current_keyboard_volume, *current_mouse_volume);
int keyboard_changed = 0, mouse_changed = 0, keyboard_volume_changed = 0,
mouse_volume_changed = 0;
if (cfg_keyboard_sound &&
strcmp(current_sound_name, cfg_keyboard_sound) != 0) {
keyboard_changed = 1;
strncpy(current_sound_name, cfg_keyboard_sound, 1023);
current_sound_name[1023] = '\0';
}
if (cfg_mouse_sound &&
strcmp(current_mouse_sound_name, cfg_mouse_sound) != 0) {
mouse_changed = 1;
strncpy(current_mouse_sound_name, cfg_mouse_sound, 1023);
current_mouse_sound_name[1023] = '\0';
}
free(cfg_keyboard_sound);
free(cfg_mouse_sound);
if (cfg_keyboard_volume != *current_keyboard_volume) {
keyboard_volume_changed = 1;
*current_keyboard_volume = cfg_keyboard_volume;
}
if (cfg_mouse_volume != *current_mouse_volume) {
mouse_volume_changed = 1;
*current_mouse_volume = cfg_mouse_volume;
}
printf("Config changed - keyboard:%d mouse:%d kvol:%d mvol:%d\n",
keyboard_changed, mouse_changed, keyboard_volume_changed,
mouse_volume_changed);
if (keyboard_changed || mouse_changed || keyboard_volume_changed ||
mouse_volume_changed) {
int keyboard_valid = validate_keyboard_sound_pack(current_sound_name);
int mouse_valid = validate_mouse_sound_pack(current_mouse_sound_name);
if (keyboard_valid && mouse_valid) {
if (build_paths_for_keyboard_sound(current_sound_name,
current_config_path, 1024,
current_sound_dir, 1024)) {
if (build_paths_for_mouse_sound(current_mouse_sound_name,
current_mouse_config_path, 1024,
current_mouse_sound_dir, 1024)) {
stop_children();
start_children(current_sound_dir, current_config_path,
*current_keyboard_volume, current_verbose,
current_mute, current_mouse_sound_dir,
current_mouse_config_path, current_mouse_volume_arg,
current_keyboard_mute, current_mouse_mute);
printf("Reloaded successfully: keyboard=%s, mouse=%s, kvol=%d, "
"mvol=%d\n",
current_sound_name, current_mouse_sound_name,
*current_keyboard_volume, *current_mouse_volume);
return 1;
} else {
printf("Failed to build mouse sound paths\n");
}
} else {
printf("Failed to build keyboard sound paths\n");
}
} else {
if (!keyboard_valid)
printf("Reload failed: invalid keyboard sound pack '%s'\n",
current_sound_name);
if (!mouse_valid)
printf("Reload failed: invalid mouse sound pack '%s'\n",
current_mouse_sound_name);
}
}
} else {
printf("Failed to read user config\n");
}
return 0;
}
+67
View File
@@ -0,0 +1,67 @@
#include "app/watch.h"
#include "app/process.h"
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <sys/inotify.h>
#include <time.h>
#include <unistd.h>
struct inotify_thread_args {
char path[1024];
};
static void *inotify_thread_fn(void *arg) {
struct inotify_thread_args *a = (struct inotify_thread_args *)arg;
int fd = inotify_init1(IN_NONBLOCK);
if (fd < 0) {
printf("Failed to initialize inotify: %s\n", strerror(errno));
return NULL;
}
int wd = inotify_add_watch(
fd, a->path, IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB | IN_MODIFY);
if (wd < 0) {
printf("Failed to add inotify watch for %s: %s\n", a->path,
strerror(errno));
close(fd);
return NULL;
}
printf("Watching config file: %s\n", a->path);
char buf[4096] __attribute__((aligned(__alignof__(struct inotify_event))));
while (1) {
ssize_t len = read(fd, buf, sizeof(buf));
if (len <= 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 100000000L;
nanosleep(&ts, NULL);
continue;
}
printf("inotify read error: %s\n", strerror(errno));
break;
}
printf("Config file changed, requesting reload...\n");
handle_sighup(0);
}
inotify_rm_watch(fd, wd);
close(fd);
return NULL;
}
int start_config_watcher(const char *path) {
static pthread_t inotify_thread;
static struct inotify_thread_args in_args;
strncpy(in_args.path, path, sizeof(in_args.path) - 1);
in_args.path[sizeof(in_args.path) - 1] = '\0';
if (access(in_args.path, F_OK) == 0) {
pthread_create(&inotify_thread, NULL, inotify_thread_fn, &in_args);
pthread_detach(inotify_thread);
return 1;
} else {
printf("Warning: Config file %s does not exist, file watching disabled\n",
in_args.path);
return 0;
}
}