feat(src): Add comprehensive logging and parameterize system volume following

Replace numerous printf statements with LOG_* macros, introduce system_volume_following parameter, and improve error handling throughout the daemon.

New Features
- Introduce LOG_INFO, LOG_ERROR, LOG_WARN for unified diagnostics.
- Add support for system_volume_following when spawning child processes.
- Log detailed startup information instead of raw printf.

Enhancements
- Refactor start_children signature to accept the new parameter.
- Use strerror(errno) in error logs for clearer messages.
- Improve signal handling output and reload request logging.

Bug Fixes
- Correct error formatting for pipe, fork, chdir, and execl failures.
- Ensure proper handling of system_volume_following_str initialization.
- Replace daemon‑specific prints with appropriate log levels.
This commit is contained in:
sankalp committed 2026-02-22 02:08:31 +05:45
1 parent 93491af2e2
commit 6658d6561b
6 files changed
+159 -109

No files matched your search

+16 -10
View File
@@ -1,11 +1,14 @@
#define _POSIX_C_SOURCE 200809L
#include "app/process.h"
#include "common/log.h"
#include "common/utils.h"
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
@@ -21,7 +24,7 @@ volatile sig_atomic_t reload_requested = 0;
void cleanup_processes(int sig) {
(void)sig;
if (!is_daemon) {
printf("\nShutting down VBX daemon...\n");
LOG_INFO("Shutting down VBX daemon...");
}
if (sound_pid > 0) {
kill(sound_pid, SIGTERM);
@@ -42,7 +45,7 @@ int require_running_pid(pid_t *out_pid) {
pid_t running_pid = 0;
if (!read_pidfile(pidfile_path, &running_pid) ||
!process_is_running(running_pid)) {
fprintf(stderr, "VBX daemon is not running.\n");
LOG_ERROR("VBX daemon is not running.");
return 0;
}
*out_pid = running_pid;
@@ -92,17 +95,17 @@ 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, int keyboard_enabled,
int mouse_enabled) {
int mouse_enabled, int system_volume_following) {
(void)config_path;
(void)mouse_sound_dir;
int pipefd[2];
if (pipe(pipefd) == -1) {
perror("pipe");
LOG_ERROR("pipe: %s", strerror(errno));
return 0;
}
sound_pid = fork();
if (sound_pid == -1) {
perror("fork");
LOG_ERROR("fork: %s", strerror(errno));
close(pipefd[0]);
close(pipefd[1]);
return 0;
@@ -115,12 +118,13 @@ int start_children(const char *sound_dir, const char *config_path, int volume,
dup2(pipefd[0], STDIN_FILENO);
close(pipefd[0]);
if (chdir(sound_dir) != 0) {
perror("chdir");
LOG_ERROR("chdir: %s", strerror(errno));
exit(1);
}
char volume_str[32], mouse_volume_str[32], verbose_str[8], mute_str[8];
char keyboard_mute_str[8], mouse_mute_str[8];
char keyboard_enabled_str[8], mouse_enabled_str[8];
char system_volume_following_str[8];
int_to_str(volume_str, sizeof(volume_str), volume);
int_to_str(mouse_volume_str, sizeof(mouse_volume_str), mouse_volume);
@@ -130,17 +134,19 @@ int start_children(const char *sound_dir, const char *config_path, int volume,
int_to_str(mouse_mute_str, sizeof(mouse_mute_str), mouse_mute);
int_to_str(keyboard_enabled_str, sizeof(keyboard_enabled_str), keyboard_enabled);
int_to_str(mouse_enabled_str, sizeof(mouse_enabled_str), mouse_enabled);
int_to_str(system_volume_following_str, sizeof(system_volume_following_str),
system_volume_following);
execl(sound_player_path, "vbx-audio", "config.json", volume_str,
verbose_str, mute_str, mouse_config_path, mouse_volume_str,
keyboard_mute_str, mouse_mute_str, keyboard_enabled_str,
mouse_enabled_str, (char *)NULL);
perror("execl vbx-audio");
mouse_enabled_str, system_volume_following_str, (char *)NULL);
LOG_ERROR("execl vbx-audio: %s", strerror(errno));
exit(1);
}
keyboard_pid = fork();
if (keyboard_pid == -1) {
perror("fork");
LOG_ERROR("fork: %s", strerror(errno));
kill(sound_pid, SIGTERM);
close(pipefd[0]);
close(pipefd[1]);
@@ -154,7 +160,7 @@ int start_children(const char *sound_dir, const char *config_path, int volume,
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[1]);
execl(get_key_presses_path, "vbx-input", (char *)NULL);
perror("execl vbx-input");
LOG_ERROR("execl vbx-input: %s", strerror(errno));
exit(1);
}
close(pipefd[0]);
+28 -19
View File
@@ -1,5 +1,6 @@
#include "app/reload.h"
#include "app/process.h"
#include "common/log.h"
#include "common/utils.h"
#include "soundpacks.h"
#include "user_config.h"
@@ -12,26 +13,28 @@ int handle_reload(const char *user_cfg_path, char *current_sound_name,
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,
int *current_keyboard_enabled, int *current_mouse_enabled) {
int current_verbose, int current_keyboard_mute,
int current_mouse_mute, int *current_keyboard_enabled,
int *current_mouse_enabled, int *current_system_volume_following) {
int g_verbose = current_verbose;
char *cfg_keyboard_sound = NULL;
char *cfg_mouse_sound = NULL;
int cfg_keyboard_volume = *current_keyboard_volume;
int cfg_mouse_volume = *current_mouse_volume;
int cfg_keyboard_enabled = *current_keyboard_enabled;
int cfg_mouse_enabled = *current_mouse_enabled;
int cfg_system_volume_following = *current_system_volume_following;
if (read_user_config(user_cfg_path, &cfg_keyboard_sound, &cfg_mouse_sound,
&cfg_keyboard_volume, &cfg_mouse_volume,
&cfg_keyboard_enabled, &cfg_mouse_enabled)) {
printf("Read config - keyboard:%s mouse:%s kvol:%d mvol:%d kenabled:%d "
"menabled:%d\n",
&cfg_keyboard_enabled, &cfg_mouse_enabled, &cfg_system_volume_following)) {
LOG_DEBUG("Read config - keyboard:%s mouse:%s kvol:%d mvol:%d kenabled:%d "
"menabled:%d",
cfg_keyboard_sound ? cfg_keyboard_sound : "NULL",
cfg_mouse_sound ? cfg_mouse_sound : "NULL", cfg_keyboard_volume,
cfg_mouse_volume, cfg_keyboard_enabled, cfg_mouse_enabled);
int keyboard_changed = 0, mouse_changed = 0, keyboard_volume_changed = 0,
mouse_volume_changed = 0, keyboard_enabled_changed = 0,
mouse_enabled_changed = 0;
mouse_enabled_changed = 0, sysvol_following_changed = 0;
if (cfg_keyboard_sound &&
strcmp(current_sound_name, cfg_keyboard_sound) != 0) {
keyboard_changed = 1;
@@ -62,18 +65,23 @@ int handle_reload(const char *user_cfg_path, char *current_sound_name,
mouse_enabled_changed = 1;
*current_mouse_enabled = cfg_mouse_enabled;
}
printf("Config changed - keyboard:%d mouse:%d kvol:%d mvol:%d kenabled:%d "
"menabled:%d\n",
if (cfg_system_volume_following != *current_system_volume_following) {
sysvol_following_changed = 1;
*current_system_volume_following = cfg_system_volume_following;
}
LOG_DEBUG("Config changed - keyboard:%d mouse:%d kvol:%d mvol:%d kenabled:%d "
"menabled:%d",
keyboard_changed, mouse_changed, keyboard_volume_changed,
mouse_volume_changed, keyboard_enabled_changed,
mouse_enabled_changed);
if (keyboard_changed || mouse_changed || keyboard_volume_changed ||
mouse_volume_changed || keyboard_enabled_changed ||
mouse_enabled_changed) {
mouse_enabled_changed || sysvol_following_changed) {
// Persist enabled state to runtime files so the audio process picks it up
// immediately
write_runtime_keyboard_enabled_file(*current_keyboard_enabled);
write_runtime_mouse_enabled_file(*current_mouse_enabled);
write_runtime_system_volume_following_file(*current_system_volume_following);
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) {
@@ -87,31 +95,32 @@ int handle_reload(const char *user_cfg_path, char *current_sound_name,
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_mouse_config_path, *current_mouse_volume,
current_keyboard_mute, current_mouse_mute,
*current_keyboard_enabled, *current_mouse_enabled);
printf("Reloaded successfully: keyboard=%s, mouse=%s, kvol=%d, "
"mvol=%d\n",
*current_keyboard_enabled, *current_mouse_enabled,
*current_system_volume_following);
LOG_INFO("Reloaded successfully: keyboard=%s, mouse=%s, kvol=%d, "
"mvol=%d",
current_sound_name, current_mouse_sound_name,
*current_keyboard_volume, *current_mouse_volume);
return 1;
} else {
printf("Failed to build mouse sound paths\n");
LOG_ERROR("Failed to build mouse sound paths");
}
} else {
printf("Failed to build keyboard sound paths\n");
LOG_ERROR("Failed to build keyboard sound paths");
}
} else {
if (!keyboard_valid)
printf("Reload failed: invalid keyboard sound pack '%s'\n",
LOG_ERROR("Reload failed: invalid keyboard sound pack '%s'",
current_sound_name);
if (!mouse_valid)
printf("Reload failed: invalid mouse sound pack '%s'\n",
LOG_ERROR("Reload failed: invalid mouse sound pack '%s'",
current_mouse_sound_name);
}
}
} else {
printf("Failed to read user config\n");
LOG_ERROR("Failed to read user config");
}
return 0;
}
+10 -4
View File
@@ -59,7 +59,8 @@ void print_usage(const char *program_name) {
printf(" --enable[=DEVICE] Enable device sounds "
"(keyboard|mouse|both)\n");
printf(" --disable[=DEVICE] Disable device sounds "
"(keyboard|mouse|both)\n\n");
"(keyboard|mouse|both)\n");
printf(" --system-volume-following=0/1 Enable or disable tracking Linux system volume dynamically\n\n");
printf("DAEMON MODE:\n");
printf(" -d, --daemon Run in background with auto-reload\n");
@@ -102,6 +103,7 @@ int parse_cli(int argc, char **argv, CliOptions *out) {
out->mouse_mute = -1;
out->keyboard_enabled = -1;
out->mouse_enabled = -1;
out->system_volume_following = -1;
static struct option long_options[] = {
{"sound", required_argument, 0, 'S'},
{"mouse", required_argument, 0, 'M'},
@@ -117,6 +119,7 @@ int parse_cli(int argc, char **argv, CliOptions *out) {
{"stop", no_argument, 0, 's'},
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, 0, 'v'},
{"system-volume-following", required_argument, 0, 'f'},
{0, 0, 0, 0}};
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-' && argv[i][1] == '-') {
@@ -133,8 +136,8 @@ int parse_cli(int argc, char **argv, CliOptions *out) {
(n == 6 && strncmp(a, "unmute", 6) == 0) ||
(n == 6 && strncmp(a, "enable", 6) == 0) ||
(n == 7 && strncmp(a, "disable", 7) == 0) ||
(n == 4 && strncmp(a, "help", 4) == 0) ||
(n == 7 && strncmp(a, "verbose", 7) == 0))) {
(n == 7 && strncmp(a, "verbose", 7) == 0) ||
(n == 23 && strncmp(a, "system-volume-following", 23) == 0))) {
safe_fprintf(stderr, "Error: Unknown option '%s'\n", argv[i]);
safe_fprintf(stderr, "Run '%s --help' to see available options.\n",
argv[0]);
@@ -144,7 +147,7 @@ int parse_cli(int argc, char **argv, CliOptions *out) {
}
optind = 1;
int opt;
while ((opt = getopt_long(argc, argv, "S:M:V:K:O:lhdsm::u::v", long_options,
while ((opt = getopt_long(argc, argv, "S:M:V:K:O:f:lhdsm::u::v", long_options,
NULL)) != -1) {
switch (opt) {
case 'd':
@@ -180,6 +183,9 @@ int parse_cli(int argc, char **argv, CliOptions *out) {
return 1;
}
break;
case 'f':
out->system_volume_following = atoi(optarg);
break;
case 1000: // --enable
case 1001: // --disable
{
+30 -3
View File
@@ -21,8 +21,31 @@ char *get_user_config_path(char *buffer, size_t buflen) {
int write_user_config(const char *path, const char *keyboard_sound,
const char *mouse_sound, int keyboard_volume,
int mouse_volume, int keyboard_enabled,
int mouse_enabled) {
json_object *root = json_object_new_object();
int mouse_enabled, int system_volume_following) {
json_object *root = NULL;
FILE *in_f = fopen(path, "r");
if (in_f) {
fseek(in_f, 0, SEEK_END);
long sz = ftell(in_f);
if (sz > 0) {
rewind(in_f);
char *buf = malloc((size_t)sz + 1);
if (buf) {
if (fread(buf, 1, sz, in_f) == (size_t)sz) {
buf[sz] = '\0';
root = json_tokener_parse(buf);
}
free(buf);
}
}
fclose(in_f);
}
if (!root || !json_object_is_type(root, json_type_object)) {
if (root) json_object_put(root);
root = json_object_new_object();
}
if (!root)
return 0;
@@ -43,6 +66,9 @@ int write_user_config(const char *path, const char *keyboard_sound,
json_object_object_add(mouse, "volume", json_object_new_int(mouse_volume));
json_object_object_add(root, "mouse", mouse);
json_object_object_add(root, "system_volume_following",
json_object_new_boolean(system_volume_following));
const char *json_str =
json_object_to_json_string_ext(root, JSON_C_TO_STRING_PRETTY);
FILE *f = fopen(path, "w");
@@ -59,7 +85,7 @@ int write_user_config(const char *path, const char *keyboard_sound,
int read_user_config(const char *path, char **out_keyboard_sound,
char **out_mouse_sound, int *out_keyboard_volume,
int *out_mouse_volume, int *out_keyboard_enabled,
int *out_mouse_enabled) {
int *out_mouse_enabled, int *out_system_volume_following) {
FILE *f = fopen(path, "r");
if (!f)
return 0;
@@ -135,6 +161,7 @@ int read_user_config(const char *path, char **out_keyboard_sound,
*out_keyboard_volume = legacy_volume;
*out_mouse_volume = legacy_volume;
}
*out_system_volume_following = 0;
}
json_object_put(root);
return 1;
+29 -31
View File
@@ -1,4 +1,6 @@
#include "common/utils.h"
#include "common/ipc.h"
#include "common/log.h"
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
@@ -61,41 +63,27 @@ static void close_restricted(int fd, void *user_data) {
static const struct libinput_interface interface = {
.open_restricted = open_restricted, .close_restricted = close_restricted};
static int print_key_event(struct libinput_event *event) {
struct libinput_event_keyboard *keyboard =
libinput_event_get_keyboard_event(event);
enum libinput_event_type event_type = libinput_event_get_type(event);
uint32_t time_stamp = libinput_event_keyboard_get_time(keyboard);
uint32_t key_code = libinput_event_keyboard_get_key(keyboard);
const char *key_name = libevdev_event_code_get_name(EV_KEY, key_code);
key_name = key_name ? key_name : "null";
enum libinput_key_state state_code =
libinput_event_keyboard_get_key_state(keyboard);
const char *state_name =
state_code == LIBINPUT_KEY_STATE_PRESSED ? "PRESSED" : "RELEASED";
return printf("{\"event_name\": \"KEYBOARD_KEY\", \"event_type\": %d, "
"\"time_stamp\": %d, \"key_name\": \"%s\", \"key_code\": %d, "
"\"state_name\": \"%s\", \"state_code\": %d}\n",
event_type, time_stamp, key_name, key_code, state_name,
state_code);
static void send_event(uint32_t key_code, uint8_t is_pressed) {
VbxEvent ev = { .key_code = key_code, .is_pressed = is_pressed };
fwrite(&ev, sizeof(VbxEvent), 1, stdout);
}
static int print_button_event(struct libinput_event *event) {
static void send_key_event(struct libinput_event *event) {
struct libinput_event_keyboard *keyboard =
libinput_event_get_keyboard_event(event);
uint32_t key_code = libinput_event_keyboard_get_key(keyboard);
enum libinput_key_state state_code =
libinput_event_keyboard_get_key_state(keyboard);
send_event(key_code, state_code == LIBINPUT_KEY_STATE_PRESSED ? 1 : 0);
}
static void send_button_event(struct libinput_event *event) {
struct libinput_event_pointer *pointer =
libinput_event_get_pointer_event(event);
enum libinput_event_type event_type = libinput_event_get_type(event);
uint32_t time_stamp = libinput_event_pointer_get_time(pointer);
uint32_t button_code = libinput_event_pointer_get_button(pointer);
const char *button_name = libevdev_event_code_get_name(EV_KEY, button_code);
enum libinput_button_state state_code =
libinput_event_pointer_get_button_state(pointer);
const char *state_name =
state_code == LIBINPUT_BUTTON_STATE_PRESSED ? "PRESSED" : "RELEASED";
return printf("{\"event_name\": \"POINTER_BUTTON\", \"event_type\": %d, "
"\"time_stamp\": %d, \"key_name\": \"%s\", \"key_code\": %d, "
"\"state_name\": \"%s\", \"state_code\": %d}\n",
event_type, time_stamp, button_name, button_code, state_name,
state_code);
send_event(button_code, state_code == LIBINPUT_BUTTON_STATE_PRESSED ? 1 : 0);
}
static int handle_events(struct libinput *libinput) {
@@ -103,21 +91,28 @@ static int handle_events(struct libinput *libinput) {
struct libinput_event *event;
if (libinput_dispatch(libinput) < 0)
return result;
bool flushed = false;
while ((event = libinput_get_event(libinput)) != NULL) {
switch (libinput_event_get_type(event)) {
case LIBINPUT_EVENT_KEYBOARD_KEY:
print_key_event(event);
send_key_event(event);
flushed = true;
break;
case LIBINPUT_EVENT_POINTER_BUTTON:
print_button_event(event);
send_button_event(event);
flushed = true;
break;
default:
break;
}
fflush(stdout);
libinput_event_destroy(event);
result = 0;
}
if (flushed) {
fflush(stdout);
}
return result;
}
@@ -153,6 +148,9 @@ void print_help(char *program_name) {
"will run this.\n");
}
int g_verbose = 0;
int is_daemon = 0;
int main(int argc, char *argv[]) {
setvbuf(stdout, NULL, _IOLBF, 0);
const struct option long_options[] = {{"version", no_argument, 0, 'v'},
+46 -42
View File
@@ -4,6 +4,7 @@
#include "app/process.h"
#include "app/reload.h"
#include "app/watch.h"
#include "common/log.h"
#include "common/mute.h"
#include "common/utils.h"
#include "config.h"
@@ -50,6 +51,7 @@ static int current_keyboard_mute = 0;
static int current_mouse_mute = 0;
static int current_keyboard_enabled = 1;
static int current_mouse_enabled = 1;
static int current_system_volume_following = 0;
static char current_config_path[MAX_PATH_LENGTH] = {0};
static char current_sound_dir[MAX_PATH_LENGTH] = {0};
static char current_mouse_config_path[MAX_PATH_LENGTH] = {0};
@@ -65,23 +67,24 @@ int main(int argc, char *argv[]) {
int list_sounds = 0;
int flag_daemon = 0;
int flag_stop = 0;
int volume = 50;
CliOptions cli_opts;
char user_cfg_path[MAX_PATH_LENGTH];
if (get_user_config_path(user_cfg_path, sizeof(user_cfg_path))) {
if (access(user_cfg_path, R_OK) == 0) {
if (verbose) {
printf("Loading configuration from: %s\n", user_cfg_path);
LOG_INFO("Loading configuration from: %s", user_cfg_path);
}
char *cfg_keyboard_sound = NULL;
char *cfg_mouse_sound = NULL;
int cfg_keyboard_volume = volume;
int cfg_mouse_volume = volume;
int cfg_keyboard_volume = current_keyboard_volume;
int cfg_mouse_volume = current_mouse_volume;
int cfg_keyboard_enabled = 1;
int cfg_mouse_enabled = 1;
int cfg_system_volume_following = 0;
if (read_user_config(user_cfg_path, &cfg_keyboard_sound, &cfg_mouse_sound,
&cfg_keyboard_volume, &cfg_mouse_volume,
&cfg_keyboard_enabled, &cfg_mouse_enabled)) {
&cfg_keyboard_enabled, &cfg_mouse_enabled,
&cfg_system_volume_following)) {
if (cfg_keyboard_sound) {
if (sound_name_owned) {
free(sound_name);
@@ -96,9 +99,11 @@ int main(int argc, char *argv[]) {
mouse_sound_name = cfg_mouse_sound;
mouse_sound_name_owned = 1;
}
volume = cfg_keyboard_volume;
current_keyboard_volume = cfg_keyboard_volume;
current_mouse_volume = cfg_mouse_volume;
current_keyboard_enabled = cfg_keyboard_enabled;
current_mouse_enabled = cfg_mouse_enabled;
current_system_volume_following = cfg_system_volume_following;
}
}
}
@@ -168,7 +173,7 @@ int main(int argc, char *argv[]) {
for (int i = 0; i < 30; i++) {
if (!process_is_running(running_pid)) {
unlink(pidfile_path);
printf("VBX daemon stopped successfully.\n");
LOG_INFO("VBX daemon stopped successfully.");
if (sound_name_owned) {
free(sound_name);
}
@@ -211,7 +216,8 @@ int main(int argc, char *argv[]) {
config_updated = 1;
}
if (cli_opts.volume >= 0) {
volume = cli_opts.volume;
current_keyboard_volume = cli_opts.volume;
current_mouse_volume = cli_opts.volume;
config_updated = 1;
}
if (cli_opts.keyboard_volume >= 0) {
@@ -264,17 +270,18 @@ int main(int argc, char *argv[]) {
write_runtime_mouse_mute_file(current_mouse_mute);
write_runtime_keyboard_enabled_file(current_keyboard_enabled);
write_runtime_mouse_enabled_file(current_mouse_enabled);
write_runtime_system_volume_following_file(current_system_volume_following);
if (config_updated &&
get_user_config_path(user_cfg_path, sizeof(user_cfg_path))) {
if (!write_user_config(user_cfg_path, sound_name, mouse_sound_name,
current_keyboard_volume, current_mouse_volume,
current_keyboard_enabled, current_mouse_enabled)) {
safe_fprintf(stderr, "Warning: Failed to update config file %s\n",
user_cfg_path);
safe_fprintf(stderr, "Your settings will not be saved for next time.\n");
current_keyboard_enabled, current_mouse_enabled,
current_system_volume_following)) {
LOG_WARN("Failed to update config file %s", user_cfg_path);
LOG_WARN("Your settings will not be saved for next time.");
} else if (verbose) {
safe_fprintf(stderr, "Updated config file %s\n", user_cfg_path);
LOG_INFO("Updated config file %s", user_cfg_path);
}
}
@@ -293,10 +300,11 @@ int main(int argc, char *argv[]) {
if (access(user_cfg_path, F_OK) != 0) {
if (!write_user_config(user_cfg_path, sound_name, mouse_sound_name,
current_keyboard_volume, current_mouse_volume,
current_keyboard_enabled, current_mouse_enabled)) {
safe_fprintf(stderr, "Warning: Failed to write %s\n", user_cfg_path);
current_keyboard_enabled, current_mouse_enabled,
current_system_volume_following)) {
LOG_WARN("Failed to write %s", user_cfg_path);
} else if (verbose) {
printf("Created default configuration: %s\n", user_cfg_path);
LOG_INFO("Created default configuration: %s", user_cfg_path);
}
}
}
@@ -346,10 +354,7 @@ int main(int argc, char *argv[]) {
if (mouse_sound_name_owned) {
free(mouse_sound_name);
}
safe_fprintf(stderr,
"VBX daemon is already running (PID: %ld). Use 'vbx --stop' "
"to stop it.\n",
(long)existing);
LOG_WARN("VBX daemon is already running (PID: %ld). Use 'vbx --stop' to stop it.", (long)existing);
return 1;
}
daemonize_self();
@@ -382,8 +387,6 @@ int main(int argc, char *argv[]) {
}
return 1;
}
current_keyboard_volume = volume;
current_mouse_volume = volume;
current_verbose = verbose;
safe_strncpy(current_sound_name, sound_name, sizeof(current_sound_name));
safe_strncpy(current_mouse_sound_name, mouse_sound_name,
@@ -395,14 +398,14 @@ int main(int argc, char *argv[]) {
safe_strncpy(current_mouse_sound_dir, mouse_sound_dir,
sizeof(current_mouse_sound_dir));
if (verbose && !is_daemon) {
printf("Starting VBX daemon...\n");
printf("Keyboard sound pack: %s\n", sound_name);
printf("Mouse sound pack: %s\n", mouse_sound_name);
printf("Keyboard config file: %s\n", config_path);
printf("Mouse config file: %s\n", mouse_config_path);
printf("Keyboard working directory: %s\n", sound_dir);
printf("Mouse working directory: %s\n", mouse_sound_dir);
printf("Press Ctrl+C to exit.\n\n");
LOG_INFO("Starting VBX daemon...");
LOG_INFO("Keyboard sound pack: %s", sound_name);
LOG_INFO("Mouse sound pack: %s", mouse_sound_name);
LOG_INFO("Keyboard config file: %s", config_path);
LOG_INFO("Mouse config file: %s", mouse_config_path);
LOG_INFO("Keyboard working directory: %s", sound_dir);
LOG_INFO("Mouse working directory: %s", mouse_sound_dir);
LOG_INFO("Press Ctrl+C to exit.");
} else {
if (!is_daemon) {
printf("VBX daemon started successfully!\n");
@@ -415,10 +418,11 @@ int main(int argc, char *argv[]) {
printf("Press Ctrl+C to exit.\n");
}
}
if (!start_children(sound_dir, config_path, volume, verbose, current_mute,
if (!start_children(sound_dir, config_path, current_keyboard_volume, verbose, current_mute,
mouse_sound_dir, mouse_config_path, current_mouse_volume,
current_keyboard_mute, current_mouse_mute,
current_keyboard_enabled, current_mouse_enabled)) {
current_keyboard_enabled, current_mouse_enabled,
current_system_volume_following)) {
if (sound_name_owned) {
free(sound_name);
}
@@ -433,14 +437,14 @@ int main(int argc, char *argv[]) {
char user_cfg_path2[MAX_PATH_LENGTH];
if (get_user_config_path(user_cfg_path2, sizeof(user_cfg_path2))) {
printf("Setting up file watcher for: %s\n", user_cfg_path2);
LOG_INFO("Setting up file watcher for: %s", user_cfg_path2);
start_config_watcher(user_cfg_path2);
} else {
printf("Warning: Could not get user config path for file watching\n");
LOG_WARN("Could not get user config path for file watching");
}
while (1) {
if (reload_requested) {
printf("Reload requested, processing config changes...\n");
LOG_INFO("Reload requested, processing config changes...");
reload_requested = 0;
char user_cfg_path3[MAX_PATH_LENGTH];
if (get_user_config_path(user_cfg_path3, sizeof(user_cfg_path3))) {
@@ -449,10 +453,11 @@ int main(int argc, char *argv[]) {
&current_keyboard_volume, &current_mouse_volume,
current_config_path, current_sound_dir, current_mouse_config_path,
current_mouse_sound_dir, current_mute, current_verbose,
current_mouse_volume, current_keyboard_mute, current_mouse_mute,
&current_keyboard_enabled, &current_mouse_enabled);
current_keyboard_mute, current_mouse_mute,
&current_keyboard_enabled, &current_mouse_enabled,
&current_system_volume_following);
} else {
printf("Failed to get user config path\n");
LOG_ERROR("Failed to get user config path");
}
}
finished_pid = waitpid(-1, &status, WNOHANG);
@@ -465,14 +470,13 @@ int main(int argc, char *argv[]) {
} else if (finished_pid > 0) {
if (finished_pid == keyboard_pid) {
if (!is_daemon)
printf("Keyboard listener exited with status %d\n",
WEXITSTATUS(status));
LOG_WARN("Keyboard listener exited with status %d", WEXITSTATUS(status));
keyboard_pid = 0;
if (sound_pid > 0)
kill(sound_pid, SIGTERM);
} else if (finished_pid == sound_pid) {
if (!is_daemon)
printf("Sound player exited with status %d\n", WEXITSTATUS(status));
LOG_WARN("Sound player exited with status %d", WEXITSTATUS(status));
sound_pid = 0;
if (keyboard_pid > 0)
kill(keyboard_pid, SIGTERM);
@@ -484,7 +488,7 @@ int main(int argc, char *argv[]) {
}
}
if (!is_daemon) {
printf("VBX daemon exited.\n");
LOG_INFO("VBX daemon exited.");
}
if (sound_name_owned) {
free(sound_name);