From 347d3048f08d3550027b7f448210c8fccedb5088 Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Mon, 13 Oct 2025 23:43:50 +0545 Subject: [PATCH] feat: add enable/disable keyboard and mouse sounds with CLI options and config file --- include/app/cli.h | 2 + include/app/reload.h | 2 +- include/audio/types.h | 2 + include/common/utils.h | 4 + include/user_config.h | 4 +- src/app/reload.c | 28 +++-- src/audio/main.c | 54 ++++++++++ src/audio/playback.c | 24 +++++ src/cli.c | 91 +++++++++++++---- src/common/utils.c | 38 +++++++ src/config.c | 21 +++- src/main.c | 227 ++++++++++++----------------------------- 12 files changed, 298 insertions(+), 199 deletions(-) diff --git a/include/app/cli.h b/include/app/cli.h index 00c8e70..aec605a 100644 --- a/include/app/cli.h +++ b/include/app/cli.h @@ -13,6 +13,8 @@ typedef struct { int verbose; int keyboard_mute; // -1 unchanged, 0 unmute, 1 mute int mouse_mute; // -1 unchanged, 0 unmute, 1 mute + int keyboard_enabled; // -1 unchanged, 0 disabled, 1 enabled + int mouse_enabled; // -1 unchanged, 0 disabled, 1 enabled } CliOptions; void print_usage(const char *program_name); diff --git a/include/app/reload.h b/include/app/reload.h index 681b366..da71817 100644 --- a/include/app/reload.h +++ b/include/app/reload.h @@ -8,7 +8,7 @@ int handle_reload(const char *user_cfg_path, 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_mouse_mute, int *current_keyboard_enabled, int *current_mouse_enabled); #endif // KEYVIBE_RELOAD_H diff --git a/include/audio/types.h b/include/audio/types.h index 8e60206..bb9a080 100644 --- a/include/audio/types.h +++ b/include/audio/types.h @@ -39,5 +39,7 @@ extern int g_verbose; extern int g_mute; extern int g_keyboard_mute; extern int g_mouse_mute; +extern int g_keyboard_enabled; +extern int g_mouse_enabled; #endif // KEYVIBE_AUDIO_TYPES_H diff --git a/include/common/utils.h b/include/common/utils.h index 6d9bd9e..b18330a 100644 --- a/include/common/utils.h +++ b/include/common/utils.h @@ -24,4 +24,8 @@ void write_runtime_mute_file(int mute); void write_runtime_keyboard_mute_file(int mute); void write_runtime_mouse_mute_file(int mute); +// Runtime enabled helpers +int write_runtime_keyboard_enabled_file(int enabled); +int write_runtime_mouse_enabled_file(int enabled); + #endif // KEYVIBE_UTILS_H diff --git a/include/user_config.h b/include/user_config.h index f89a799..5d2d719 100644 --- a/include/user_config.h +++ b/include/user_config.h @@ -8,11 +8,11 @@ char *get_user_config_path(char *buffer, size_t buflen); // Write ~/.keyvibe.json in new format int write_user_config(const char *path, const char *keyboard_sound, const char *mouse_sound, int keyboard_volume, - int mouse_volume); + int mouse_volume, int keyboard_enabled, int mouse_enabled); // Read ~/.keyvibe.json supporting new and legacy formats 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_mouse_volume, int *out_keyboard_enabled, int *out_mouse_enabled); #endif // KEYVIBE_CONFIG_IO_H diff --git a/src/app/reload.c b/src/app/reload.c index 6a9b1a9..2be8cb6 100644 --- a/src/app/reload.c +++ b/src/app/reload.c @@ -12,20 +12,22 @@ int handle_reload(const char *user_cfg_path, char *current_sound_name, 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_mute, int current_mouse_mute, + int *current_keyboard_enabled, int *current_mouse_enabled) { 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; 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_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_sound ? cfg_keyboard_sound : "NULL", cfg_mouse_sound ? cfg_mouse_sound : "NULL", cfg_keyboard_volume, - cfg_mouse_volume, *current_keyboard_volume, *current_mouse_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; + mouse_volume_changed = 0, keyboard_enabled_changed = 0, mouse_enabled_changed = 0; if (cfg_keyboard_sound && strcmp(current_sound_name, cfg_keyboard_sound) != 0) { keyboard_changed = 1; @@ -48,11 +50,19 @@ int handle_reload(const char *user_cfg_path, char *current_sound_name, mouse_volume_changed = 1; *current_mouse_volume = cfg_mouse_volume; } - printf("Config changed - keyboard:%d mouse:%d kvol:%d mvol:%d\n", + if (cfg_keyboard_enabled != *current_keyboard_enabled) { + keyboard_enabled_changed = 1; + *current_keyboard_enabled = cfg_keyboard_enabled; + } + if (cfg_mouse_enabled != *current_mouse_enabled) { + 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", keyboard_changed, mouse_changed, keyboard_volume_changed, - mouse_volume_changed); + mouse_volume_changed, keyboard_enabled_changed, mouse_enabled_changed); if (keyboard_changed || mouse_changed || keyboard_volume_changed || - mouse_volume_changed) { + mouse_volume_changed || keyboard_enabled_changed || mouse_enabled_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) { diff --git a/src/audio/main.c b/src/audio/main.c index 07b4192..3f72373 100644 --- a/src/audio/main.c +++ b/src/audio/main.c @@ -10,6 +10,10 @@ #include #include +// Global variables for enabled state +int g_keyboard_enabled = 1; +int g_mouse_enabled = 1; + int load_sound_config(const char *config_path); static int read_mute_state() { @@ -81,6 +85,52 @@ static int read_mouse_mute_state() { return 0; } +static int read_keyboard_enabled_state() { + char enabled_file[1024]; + const char *rd = getenv("XDG_RUNTIME_DIR"); + if (!rd || strlen(rd) == 0) { + rd = "/tmp"; + } + if (!safe_snprintf(enabled_file, sizeof(enabled_file), "%s/keyvibe-kbd-enabled-%d", rd, + (int)getuid())) { + return 1; // default enabled + } + FILE *f = fopen(enabled_file, "r"); + if (!f) { + return 1; // default enabled + } + int enabled = 1; + if (fscanf(f, "%d", &enabled) == 1) { + fclose(f); + return enabled; + } + fclose(f); + return 1; // default enabled +} + +static int read_mouse_enabled_state() { + char enabled_file[1024]; + const char *rd = getenv("XDG_RUNTIME_DIR"); + if (!rd || strlen(rd) == 0) { + rd = "/tmp"; + } + if (!safe_snprintf(enabled_file, sizeof(enabled_file), "%s/keyvibe-mouse-enabled-%d", + rd, (int)getuid())) { + return 1; // default enabled + } + FILE *f = fopen(enabled_file, "r"); + if (!f) { + return 1; // default enabled + } + int enabled = 1; + if (fscanf(f, "%d", &enabled) == 1) { + fclose(f); + return enabled; + } + fclose(f); + return 1; // default enabled +} + int main(int argc, char *argv[]) { if (argc < 2 || argc > 9) { fprintf(stderr, @@ -166,6 +216,10 @@ int main(int argc, char *argv[]) { g_mute = read_mute_state(); g_keyboard_mute = read_keyboard_mute_state(); g_mouse_mute = read_mouse_mute_state(); + + // Check enabled state from runtime files + g_keyboard_enabled = read_keyboard_enabled_state(); + g_mouse_enabled = read_mouse_enabled_state(); FD_ZERO(&readfds); FD_SET(STDIN_FILENO, &readfds); diff --git a/src/audio/playback.c b/src/audio/playback.c index c8a4437..326fe80 100644 --- a/src/audio/playback.c +++ b/src/audio/playback.c @@ -212,6 +212,30 @@ void play_sound_segment(int key_code, int is_pressed) { } return; } + + // Check if this is a mouse event and if mouse is disabled + int is_mouse_event = (key_code == 272 || key_code == 273 || key_code == 274); + if (is_mouse_event) { + // For mouse events, we need to check if mouse is enabled + // This will be handled by the main loop passing enabled state + // For now, we'll add a simple check here + extern int g_mouse_enabled; + if (!g_mouse_enabled) { + if (g_verbose) { + printf("Mouse sounds disabled - ignoring mouse event %d\n", key_code); + } + return; + } + } else { + // For keyboard events, check if keyboard is enabled + extern int g_keyboard_enabled; + if (!g_keyboard_enabled) { + if (g_verbose) { + printf("Keyboard sounds disabled - ignoring key %d\n", key_code); + } + return; + } + } if (!g_sound_pack.is_multi && !is_pressed) { if (g_verbose) { printf("Single mode: Ignoring key release for key %d\n", key_code); diff --git a/src/cli.c b/src/cli.c index 9ea8bc4..6eb6f6f 100644 --- a/src/cli.c +++ b/src/cli.c @@ -8,48 +8,45 @@ void print_usage(const char *program_name) { printf("KeyVibe - Mechanical Keyboard Sound Simulator\n"); printf("Version: %s\n\n", PROJECT_VERSION); - + printf("USAGE:\n"); printf(" %s [OPTIONS]\n\n", program_name); - + printf("SOUND CONTROL:\n"); printf(" -S, --sound PACK Keyboard sound pack (default: eg-oreo)\n"); printf(" -M, --mouse PACK Mouse sound pack (default: ping)\n"); printf(" -l, --list List available sound packs\n\n"); - + printf("VOLUME CONTROL:\n"); - printf( - " -V, --volume LEVEL Set both keyboard & mouse volume [0-100]\n"); + printf(" -V, --volume LEVEL Set both keyboard & mouse volume [0-100]\n"); printf(" -K, --keyboard-volume Set keyboard volume [0-100]\n"); printf(" -O, --mouse-volume Set mouse volume [0-100]\n\n"); - + printf("MUTE CONTROL:\n"); printf(" -m, --mute[=DEVICE] Mute sounds (keyboard|mouse|both)\n"); printf(" -u, --unmute[=DEVICE] Unmute sounds (keyboard|mouse|both)\n\n"); - + + printf("ENABLE/DISABLE:\n"); + printf(" --enable[=DEVICE] Enable sounds (keyboard|mouse|both)\n"); + printf(" --disable[=DEVICE] Disable sounds (keyboard|mouse|both)\n\n"); + printf("DAEMON CONTROL:\n"); printf(" -d, --daemon Run in background\n"); printf(" -s, --stop Stop background daemon\n\n"); - + printf("OTHER:\n"); printf(" -v, --verbose Enable verbose output\n"); printf(" -h, --help Show this help\n\n"); - + printf("EXAMPLES:\n"); - printf(" %s --list # List sound packs\n", - program_name); - printf(" %s -S cherrymx-blue -V 75 # Cherry MX Blue at " - "75%%\n", - program_name); - printf(" %s --daemon # Run in background\n", - program_name); - printf(" %s --mute keyboard # Mute only keyboard\n", - program_name); - printf(" %s --keyboard-volume 80 --mouse-volume 60 # Different volumes\n", - program_name); + printf(" %s --list # List sound packs\n", program_name); + printf(" %s -S cherrymx-blue -V 75 # Cherry MX Blue at 75%%\n", program_name); + printf(" %s --daemon # Run in background\n", program_name); + printf(" %s --mute keyboard # Mute only keyboard\n", program_name); + printf(" %s --disable mouse # Disable mouse sounds\n", program_name); + printf(" %s --keyboard-volume 80 --mouse-volume 60 # Different volumes\n", program_name); printf("\n"); - printf("CONFIG: Settings are saved to ~/.keyvibe.json and auto-reload in " - "daemon mode.\n"); + printf("CONFIG: Settings are saved to ~/.keyvibe.json and auto-reload in daemon mode.\n"); } int parse_cli(int argc, char **argv, CliOptions *out) { @@ -59,6 +56,8 @@ int parse_cli(int argc, char **argv, CliOptions *out) { out->mouse_volume = -1; out->keyboard_mute = -1; out->mouse_mute = -1; + out->keyboard_enabled = -1; + out->mouse_enabled = -1; static struct option long_options[] = { {"sound", required_argument, 0, 'S'}, {"mouse", required_argument, 0, 'M'}, @@ -67,6 +66,8 @@ int parse_cli(int argc, char **argv, CliOptions *out) { {"mouse-volume", required_argument, 0, 'O'}, {"mute", optional_argument, 0, 'm'}, {"unmute", optional_argument, 0, 'u'}, + {"enable", optional_argument, 0, 1000}, + {"disable", optional_argument, 0, 1001}, {"list", no_argument, 0, 'l'}, {"daemon", no_argument, 0, 'd'}, {"stop", no_argument, 0, 's'}, @@ -86,6 +87,8 @@ int parse_cli(int argc, char **argv, CliOptions *out) { (n == 4 && strncmp(a, "stop", 4) == 0) || (n == 4 && strncmp(a, "mute", 4) == 0) || (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))) { fprintf(stderr, "Error: Unknown option '%s'\n", argv[i]); @@ -160,6 +163,50 @@ int parse_cli(int argc, char **argv, CliOptions *out) { return 1; } break; + case 1000: // --enable + if (optarg == NULL) { + // Handle space-separated argument: --enable mouse + if (optind < argc && argv[optind][0] != '-') { + optarg = argv[optind++]; + } else { + optarg = "both"; // default to both if no argument + } + } + if (strcmp(optarg, "both") == 0) { + out->keyboard_enabled = 1; + out->mouse_enabled = 1; + } else if (strcmp(optarg, "keyboard") == 0) { + out->keyboard_enabled = 1; + } else if (strcmp(optarg, "mouse") == 0) { + out->mouse_enabled = 1; + } else { + fprintf(stderr, "Error: Invalid enable device '%s'\n", optarg); + fprintf(stderr, "Use: keyboard, mouse, or both\n"); + return 1; + } + break; + case 1001: // --disable + if (optarg == NULL) { + // Handle space-separated argument: --disable mouse + if (optind < argc && argv[optind][0] != '-') { + optarg = argv[optind++]; + } else { + optarg = "both"; // default to both if no argument + } + } + if (strcmp(optarg, "both") == 0) { + out->keyboard_enabled = 0; + out->mouse_enabled = 0; + } else if (strcmp(optarg, "keyboard") == 0) { + out->keyboard_enabled = 0; + } else if (strcmp(optarg, "mouse") == 0) { + out->mouse_enabled = 0; + } else { + fprintf(stderr, "Error: Invalid disable device '%s'\n", optarg); + fprintf(stderr, "Use: keyboard, mouse, or both\n"); + return 1; + } + break; case 'l': out->list_flag = 1; break; diff --git a/src/common/utils.c b/src/common/utils.c index c66f192..3b13fe1 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -131,3 +131,41 @@ int process_is_running(pid_t pid) { return 0; return kill(pid, 0) == 0; } + +int write_runtime_keyboard_enabled_file(int enabled) { + char enabled_file[1024]; + const char *rd = getenv("XDG_RUNTIME_DIR"); + if (!rd || strlen(rd) == 0) { + rd = "/tmp"; + } + if (!safe_snprintf(enabled_file, sizeof(enabled_file), "%s/keyvibe-kbd-enabled-%d", rd, + (int)getuid())) { + return 0; + } + FILE *f = fopen(enabled_file, "w"); + if (!f) { + return 0; + } + fprintf(f, "%d\n", enabled); + fclose(f); + return 1; +} + +int write_runtime_mouse_enabled_file(int enabled) { + char enabled_file[1024]; + const char *rd = getenv("XDG_RUNTIME_DIR"); + if (!rd || strlen(rd) == 0) { + rd = "/tmp"; + } + if (!safe_snprintf(enabled_file, sizeof(enabled_file), "%s/keyvibe-mouse-enabled-%d", rd, + (int)getuid())) { + return 0; + } + FILE *f = fopen(enabled_file, "w"); + if (!f) { + return 0; + } + fprintf(f, "%d\n", enabled); + fclose(f); + return 1; +} diff --git a/src/config.c b/src/config.c index 96a0c07..d9e6ee5 100644 --- a/src/config.c +++ b/src/config.c @@ -35,12 +35,14 @@ 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 mouse_volume, int keyboard_enabled, int mouse_enabled) { json_object *root = json_object_new_object(); if (!root) return 0; json_object *keyboard = json_object_new_object(); + json_object_object_add(keyboard, "enabled", + json_object_new_boolean(keyboard_enabled)); json_object_object_add(keyboard, "keyboard_sound", json_object_new_string(keyboard_sound)); json_object_object_add(keyboard, "volume", @@ -48,6 +50,8 @@ int write_user_config(const char *path, const char *keyboard_sound, json_object_object_add(root, "keyboard", keyboard); json_object *mouse = json_object_new_object(); + json_object_object_add(mouse, "enabled", + json_object_new_boolean(mouse_enabled)); json_object_object_add(mouse, "mouse_sound", json_object_new_string(mouse_sound)); json_object_object_add(mouse, "volume", json_object_new_int(mouse_volume)); @@ -68,7 +72,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_mouse_volume, int *out_keyboard_enabled, int *out_mouse_enabled) { FILE *f = fopen(path, "r"); if (!f) return 0; @@ -101,6 +105,16 @@ int read_user_config(const char *path, char **out_keyboard_sound, if (json_object_object_get_ex(root, "keyboard", &keyboard_obj) && json_object_object_get_ex(root, "mouse", &mouse_obj)) { json_object *o; + if (json_object_object_get_ex(keyboard_obj, "enabled", &o)) { + *out_keyboard_enabled = json_object_get_boolean(o); + } else { + *out_keyboard_enabled = 1; // default enabled + } + if (json_object_object_get_ex(mouse_obj, "enabled", &o)) { + *out_mouse_enabled = json_object_get_boolean(o); + } else { + *out_mouse_enabled = 1; // default enabled + } if (json_object_object_get_ex(keyboard_obj, "keyboard_sound", &o)) { const char *s = json_object_get_string(o); if (s) @@ -118,6 +132,9 @@ int read_user_config(const char *path, char **out_keyboard_sound, *out_mouse_volume = json_object_get_int(o); } } else { + // Legacy format - default to enabled + *out_keyboard_enabled = 1; + *out_mouse_enabled = 1; json_object *o; if (json_object_object_get_ex(root, "sound", &o)) { const char *s = json_object_get_string(o); diff --git a/src/main.c b/src/main.c index 54ee093..4ed5101 100644 --- a/src/main.c +++ b/src/main.c @@ -48,6 +48,8 @@ static char current_mouse_sound_name[MAX_PATH_LENGTH] = {0}; static int current_verbose = 0; 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 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}; @@ -63,28 +65,8 @@ int main(int argc, char *argv[]) { int list_sounds = 0; int flag_daemon = 0; int flag_stop = 0; - static struct option long_options[] = { - {"sound", required_argument, 0, 'S'}, - {"mouse", required_argument, 0, 'M'}, - {"volume", required_argument, 0, 'V'}, - {"keyboard-volume", required_argument, 0, 'K'}, - {"mouse-volume", required_argument, 0, 'O'}, - {"mute", optional_argument, 0, 'm'}, - {"unmute", optional_argument, 0, 'u'}, - {"list", no_argument, 0, 'l'}, - {"daemon", no_argument, 0, 'd'}, - {"stop", no_argument, 0, 's'}, - {"help", no_argument, 0, 'h'}, - {"verbose", no_argument, 0, 'v'}, - {0, 0, 0, 0}}; int volume = 50; - char *cli_sound = NULL; - char *cli_mouse_sound = NULL; - int cli_volume = -1; - int cli_keyboard_volume = -1; - int cli_mouse_volume = -1; - int cli_keyboard_mute = -1; - int cli_mouse_mute = -1; + 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) { @@ -92,8 +74,10 @@ int main(int argc, char *argv[]) { char *cfg_mouse_sound = NULL; int cfg_keyboard_volume = volume; int cfg_mouse_volume = volume; + int cfg_keyboard_enabled = 1; + int cfg_mouse_enabled = 1; if (read_user_config(user_cfg_path, &cfg_keyboard_sound, &cfg_mouse_sound, - &cfg_keyboard_volume, &cfg_mouse_volume)) { + &cfg_keyboard_volume, &cfg_mouse_volume, &cfg_keyboard_enabled, &cfg_mouse_enabled)) { if (cfg_keyboard_sound) { if (sound_name_owned) { free(sound_name); @@ -109,131 +93,36 @@ int main(int argc, char *argv[]) { mouse_sound_name_owned = 1; } volume = cfg_keyboard_volume; + current_keyboard_enabled = cfg_keyboard_enabled; + current_mouse_enabled = cfg_mouse_enabled; } } } - for (int i = 1; i < argc; i++) { - if (argv[i][0] == '-' && argv[i][1] == '-') { - const char *a = argv[i] + 2; - const char *e = strchr(a, '='); - size_t n = e ? (size_t)(e - a) : strlen(a); - if (!((n == 5 && strncmp(a, "sound", 5) == 0) || - (n == 5 && strncmp(a, "mouse", 5) == 0) || - (n == 6 && strncmp(a, "volume", 6) == 0) || - (n == 4 && strncmp(a, "list", 4) == 0) || - (n == 6 && strncmp(a, "daemon", 6) == 0) || - (n == 4 && strncmp(a, "stop", 4) == 0) || - (n == 4 && strncmp(a, "mute", 4) == 0) || - (n == 6 && strncmp(a, "unmute", 6) == 0) || - (n == 4 && strncmp(a, "help", 4) == 0) || - (n == 7 && strncmp(a, "verbose", 7) == 0))) { - fprintf(stderr, "Unknown option: %s (use full option name)\n", argv[i]); - return 1; - } - } - } - int opt; - while ((opt = getopt_long(argc, argv, "S:M:V:K:O:lhdsm::u::v", long_options, - NULL)) != -1) { - switch (opt) { - case 'd': - flag_daemon = 1; - break; - case 's': - flag_stop = 1; - break; - case 'S': - cli_sound = optarg; - break; - case 'M': - cli_mouse_sound = optarg; - break; - case 'V': - cli_volume = atoi(optarg); - if (cli_volume < 0) - cli_volume = 0; - if (cli_volume > 100) - cli_volume = 100; - break; - case 'K': - cli_keyboard_volume = atoi(optarg); - if (cli_keyboard_volume < 0) - cli_keyboard_volume = 0; - if (cli_keyboard_volume > 100) - cli_keyboard_volume = 100; - break; - case 'O': - cli_mouse_volume = atoi(optarg); - if (cli_mouse_volume < 0) - cli_mouse_volume = 0; - if (cli_mouse_volume > 100) - cli_mouse_volume = 100; - break; - case 'm': - if (optarg == NULL) { - cli_keyboard_mute = 1; - cli_mouse_mute = 1; - current_mute = 1; // global mute - } else if (strcmp(optarg, "keyboard") == 0) { - cli_keyboard_mute = 1; - write_runtime_keyboard_mute_file(1); - } else if (strcmp(optarg, "mouse") == 0) { - cli_mouse_mute = 1; - write_runtime_mouse_mute_file(1); - } else if (strcmp(optarg, "both") == 0) { - cli_keyboard_mute = 1; - cli_mouse_mute = 1; - current_mute = 1; // global mute - } else { - fprintf(stderr, - "Invalid mute option: %s. Use 'keyboard', 'mouse', or 'both'\n", - optarg); - return 1; - } - break; - case 'u': - if (optarg == NULL) { - cli_keyboard_mute = 0; - cli_mouse_mute = 0; - current_mute = 0; // clear global mute - } else if (strcmp(optarg, "keyboard") == 0) { - cli_keyboard_mute = 0; - write_runtime_keyboard_mute_file(0); - } else if (strcmp(optarg, "mouse") == 0) { - cli_mouse_mute = 0; - write_runtime_mouse_mute_file(0); - } else if (strcmp(optarg, "both") == 0) { - cli_keyboard_mute = 0; - cli_mouse_mute = 0; - current_mute = 0; // clear global mute - } else { - fprintf( - stderr, - "Invalid unmute option: %s. Use 'keyboard', 'mouse', or 'both'\n", - optarg); - return 1; - } - break; - case 'l': - list_sounds = 1; - break; - case 'h': - print_usage(argv[0]); + + // Parse CLI arguments + int parse_result = parse_cli(argc, argv, &cli_opts); + if (parse_result == 1) { if (sound_name_owned) { free(sound_name); } - return 0; - case 'v': - verbose = 1; - break; - default: - print_usage(argv[0]); + if (mouse_sound_name_owned) { + free(mouse_sound_name); + } + return 1; + } else if (parse_result == 2) { if (sound_name_owned) { free(sound_name); } - return 1; + if (mouse_sound_name_owned) { + free(mouse_sound_name); } + return 0; } + + verbose = cli_opts.verbose; + list_sounds = cli_opts.list_flag; + flag_daemon = cli_opts.daemon_flag; + flag_stop = cli_opts.stop_flag; if (list_sounds) { int rc = list_sound_packs(); if (sound_name_owned) { @@ -264,46 +153,68 @@ int main(int argc, char *argv[]) { current_mute = read_runtime_mute_file(); int config_updated = 0; - if (cli_sound != NULL) { + if (cli_opts.sound != NULL) { if (sound_name_owned) { free(sound_name); } - sound_name = cli_sound; + sound_name = cli_opts.sound; sound_name_owned = 0; config_updated = 1; } - if (cli_mouse_sound != NULL) { + if (cli_opts.mouse_sound != NULL) { if (mouse_sound_name_owned) { free(mouse_sound_name); } - mouse_sound_name = cli_mouse_sound; + mouse_sound_name = cli_opts.mouse_sound; mouse_sound_name_owned = 0; config_updated = 1; } - if (cli_volume >= 0) { - volume = cli_volume; + if (cli_opts.volume >= 0) { + volume = cli_opts.volume; config_updated = 1; } - if (cli_keyboard_volume >= 0) { - current_keyboard_volume = cli_keyboard_volume; + if (cli_opts.keyboard_volume >= 0) { + current_keyboard_volume = cli_opts.keyboard_volume; config_updated = 1; } - if (cli_mouse_volume >= 0) { - current_mouse_volume = cli_mouse_volume; + if (cli_opts.mouse_volume >= 0) { + current_mouse_volume = cli_opts.mouse_volume; config_updated = 1; } - if (cli_keyboard_mute >= 0) { - current_keyboard_mute = cli_keyboard_mute; + if (cli_opts.keyboard_mute >= 0) { + current_keyboard_mute = cli_opts.keyboard_mute; config_updated = 1; } - if (cli_mouse_mute >= 0) { - current_mouse_mute = cli_mouse_mute; + if (cli_opts.mouse_mute >= 0) { + current_mouse_mute = cli_opts.mouse_mute; + config_updated = 1; + } + if (cli_opts.keyboard_enabled >= 0) { + current_keyboard_enabled = cli_opts.keyboard_enabled; + config_updated = 1; + } + if (cli_opts.mouse_enabled >= 0) { + current_mouse_enabled = cli_opts.mouse_enabled; config_updated = 1; } write_runtime_mute_file(current_mute); write_runtime_keyboard_mute_file(current_keyboard_mute); write_runtime_mouse_mute_file(current_mouse_mute); + write_runtime_keyboard_enabled_file(current_keyboard_enabled); + write_runtime_mouse_enabled_file(current_mouse_enabled); + + // Update config file if needed + 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)) { + fprintf(stderr, "Warning: Failed to update config file %s\n", + user_cfg_path); + } else if (verbose) { + fprintf(stderr, "Updated config file %s\n", user_cfg_path); + } + } if (config_updated && !flag_daemon && !list_sounds && !flag_stop) { if (sound_name_owned) { @@ -315,22 +226,11 @@ int main(int argc, char *argv[]) { return 0; } - 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)) { - fprintf(stderr, "Warning: Failed to update config file %s\n", - user_cfg_path); - } else if (verbose) { - fprintf(stderr, "Updated config file %s\n", user_cfg_path); - } - } - if (!config_updated && get_user_config_path(user_cfg_path, sizeof(user_cfg_path))) { 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_volume, current_mouse_volume, current_keyboard_enabled, current_mouse_enabled)) { fprintf(stderr, "Warning: Failed to write %s\n", user_cfg_path); } else if (verbose) { fprintf(stderr, "Created default config %s\n", user_cfg_path); @@ -470,7 +370,8 @@ int main(int argc, char *argv[]) { ¤t_keyboard_volume, ¤t_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_mouse_volume, current_keyboard_mute, current_mouse_mute, + ¤t_keyboard_enabled, ¤t_mouse_enabled); } else { printf("Failed to get user config path\n"); }