oops many changes

This commit is contained in:
sankalp committed 2025-10-14 23:32:58 +05:45
1 parent 347d3048f0
commit 65dc7da800
12 files changed
+219 -330

No files matched your search

+4 -4
View File
@@ -52,8 +52,8 @@ test: all
install:
@echo "Installing KeyVibe to $(DESTDIR)$(BINDIR) and $(DESTDIR)$(SHAREDIR)..."
install -Dm755 $(KeyVibe_TARGET) $(DESTDIR)$(BINDIR)/$(KeyVibe_TARGET)
install -Dm755 $(SOUND_TARGET) $(DESTDIR)$(BINDIR)/$(SOUND_TARGET)
install -Dm755 $(KEYBOARD_TARGET) $(DESTDIR)$(BINDIR)/$(KEYBOARD_TARGET)
install -Dm755 $(SOUND_TARGET) $(DESTDIR)$(BINDIR)/keyvibe-audio
install -Dm755 $(KEYBOARD_TARGET) $(DESTDIR)$(BINDIR)/keyvibe-input
install -d $(DESTDIR)$(SHAREDIR)
cp -r soundpacks $(DESTDIR)$(SHAREDIR)/
@echo "Installing udev rule for non-root keyboard access..."
@@ -72,8 +72,8 @@ install:
uninstall:
@echo "Uninstalling KeyVibe from $(DESTDIR)$(BINDIR) and $(DESTDIR)$(SHAREDIR)..."
rm -f $(DESTDIR)$(BINDIR)/$(KeyVibe_TARGET)
rm -f $(DESTDIR)$(BINDIR)/$(SOUND_TARGET)
rm -f $(DESTDIR)$(BINDIR)/$(KEYBOARD_TARGET)
rm -f $(DESTDIR)$(BINDIR)/keyvibe-audio
rm -f $(DESTDIR)$(BINDIR)/keyvibe-input
rm -rf $(DESTDIR)$(SHAREDIR)
@echo "Removing udev rule..."
@sudo rm -f $(UDEV_RULE) || true
+1 -1
View File
@@ -17,7 +17,7 @@ void stop_children(void);
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_mute, int mouse_mute, int keyboard_enabled, int mouse_enabled);
void handle_sighup(int sig);
#endif // KEYVIBE_PROCESS_H
+5
View File
@@ -28,4 +28,9 @@ void write_runtime_mouse_mute_file(int mute);
int write_runtime_keyboard_enabled_file(int enabled);
int write_runtime_mouse_enabled_file(int enabled);
// Helper functions
void int_to_str(char *buffer, size_t size, int value);
char *xstrdup(const char *s);
int validate_volume(int volume);
#endif // KEYVIBE_UTILS_H
+18 -14
View File
@@ -87,10 +87,12 @@ void stop_children(void) {
}
}
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_mute, int mouse_mute, int keyboard_enabled,
int mouse_enabled) {
(void)config_path;
(void)mouse_sound_dir;
int pipefd[2];
@@ -116,21 +118,23 @@ int start_children(const char *sound_dir, const char *config_path, int volume,
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);
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];
int_to_str(volume_str, sizeof(volume_str), volume);
int_to_str(mouse_volume_str, sizeof(mouse_volume_str), mouse_volume);
int_to_str(verbose_str, sizeof(verbose_str), verbose);
int_to_str(mute_str, sizeof(mute_str), mute);
int_to_str(keyboard_mute_str, sizeof(keyboard_mute_str), keyboard_mute);
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);
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);
keyboard_mute_str, mouse_mute_str, keyboard_enabled_str,
mouse_enabled_str, (char *)NULL);
perror("execl keyvibe-audio");
exit(1);
}
+19 -7
View File
@@ -1,5 +1,6 @@
#include "app/reload.h"
#include "app/process.h"
#include "common/utils.h"
#include "soundpacks.h"
#include "user_config.h"
#include <stdio.h>
@@ -21,13 +22,16 @@ int handle_reload(const char *user_cfg_path, char *current_sound_name,
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, &cfg_keyboard_enabled, &cfg_mouse_enabled)) {
printf("Read config - keyboard:%s mouse:%s kvol:%d mvol:%d kenabled:%d menabled:%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, 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_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;
@@ -58,11 +62,18 @@ 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",
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, keyboard_enabled_changed, mouse_enabled_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_volume_changed || keyboard_enabled_changed ||
mouse_enabled_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);
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) {
@@ -77,7 +88,8 @@ int handle_reload(const char *user_cfg_path, char *current_sound_name,
*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);
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_sound_name, current_mouse_sound_name,
+1 -9
View File
@@ -1,12 +1,12 @@
#include "audio/types.h"
#include "common/utils.h"
#include <errno.h>
#include <json-c/json.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
SoundPack g_sound_pack = {0};
SoundPack g_mouse_sound_pack = {0};
@@ -33,14 +33,6 @@ static void get_full_path(char *buffer, size_t buffer_size,
}
}
static char *xstrdup(const char *s) {
if (!s) return NULL;
size_t n = strlen(s) + 1;
char *p = (char *)malloc(n);
if (!p) return NULL;
memcpy(p, s, n);
return p;
}
int load_sound_config(const char *config_path) {
FILE *file = fopen(config_path, "r");
+39 -106
View File
@@ -10,132 +10,61 @@
#include <sys/time.h>
#include <unistd.h>
// 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() {
char mute_file[1024];
// Generic function to read runtime state files
static int read_runtime_state(const char *filename_suffix, int default_value) {
char state_file[1024];
const char *rd = getenv("XDG_RUNTIME_DIR");
if (!rd || strlen(rd) == 0) {
rd = "/tmp";
}
if (!safe_snprintf(mute_file, sizeof(mute_file), "%s/keyvibe-mute-%d", rd,
(int)getuid())) {
return 0;
if (!safe_snprintf(state_file, sizeof(state_file), "%s/keyvibe-%s-%d", rd,
filename_suffix, (int)getuid())) {
return default_value;
}
FILE *f = fopen(mute_file, "r");
FILE *f = fopen(state_file, "r");
if (!f) {
return 0;
return default_value;
}
int mute = 0;
if (fscanf(f, "%d", &mute) == 1) {
int state = default_value;
if (fscanf(f, "%d", &state) == 1) {
fclose(f);
return mute;
return state;
}
fclose(f);
return 0;
return default_value;
}
static int read_mute_state() {
return read_runtime_state("mute", 0);
}
static int read_keyboard_mute_state() {
char mute_file[1024];
const char *rd = getenv("XDG_RUNTIME_DIR");
if (!rd || strlen(rd) == 0) {
rd = "/tmp";
}
if (!safe_snprintf(mute_file, sizeof(mute_file), "%s/keyvibe-kbd-mute-%d", rd,
(int)getuid())) {
return 0;
}
FILE *f = fopen(mute_file, "r");
if (!f) {
return 0;
}
int mute = 0;
if (fscanf(f, "%d", &mute) == 1) {
fclose(f);
return mute;
}
fclose(f);
return 0;
return read_runtime_state("kbd-mute", 0);
}
static int read_mouse_mute_state() {
char mute_file[1024];
const char *rd = getenv("XDG_RUNTIME_DIR");
if (!rd || strlen(rd) == 0) {
rd = "/tmp";
}
if (!safe_snprintf(mute_file, sizeof(mute_file), "%s/keyvibe-mouse-mute-%d",
rd, (int)getuid())) {
return 0;
}
FILE *f = fopen(mute_file, "r");
if (!f) {
return 0;
}
int mute = 0;
if (fscanf(f, "%d", &mute) == 1) {
fclose(f);
return mute;
}
fclose(f);
return 0;
return read_runtime_state("mouse-mute", 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
return read_runtime_state("kbd-enabled", 1);
}
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
return read_runtime_state("mouse-enabled", 1);
}
int main(int argc, char *argv[]) {
if (argc < 2 || argc > 9) {
if (argc < 2 || argc > 11) {
fprintf(stderr,
"Usage: %s <config.json> [volume] [verbose] [mute] [mouse_config] "
"[mouse_volume] [keyboard_mute] [mouse_mute]\n",
"[mouse_volume] [keyboard_mute] [mouse_mute] [keyboard_enabled] "
"[mouse_enabled]\n",
argv[0]);
fprintf(stderr, " volume: 0-100 (default: 50)\n");
fprintf(stderr, " verbose: 1 to enable verbose output (default: 0)\n");
@@ -144,14 +73,13 @@ int main(int argc, char *argv[]) {
fprintf(stderr, " mouse_volume: 0-100 for mouse volume (optional)\n");
fprintf(stderr, " keyboard_mute: 1 to mute keyboard sounds (optional)\n");
fprintf(stderr, " mouse_mute: 1 to mute mouse sounds (optional)\n");
fprintf(stderr,
" keyboard_enabled: 1 to enable keyboard sounds (optional)\n");
fprintf(stderr, " mouse_enabled: 1 to enable mouse sounds (optional)\n");
return 1;
}
if (argc >= 3) {
int volume_percent = atoi(argv[2]);
if (volume_percent < 0)
volume_percent = 0;
if (volume_percent > 100)
volume_percent = 100;
int volume_percent = validate_volume(atoi(argv[2]));
g_volume = volume_percent / 100.0f;
if (g_verbose)
printf("Volume set to: %d%%\n", volume_percent);
@@ -182,11 +110,7 @@ int main(int argc, char *argv[]) {
}
}
if (argc >= 7) {
int mouse_volume_percent = atoi(argv[6]);
if (mouse_volume_percent < 0)
mouse_volume_percent = 0;
if (mouse_volume_percent > 100)
mouse_volume_percent = 100;
int mouse_volume_percent = validate_volume(atoi(argv[6]));
g_mouse_volume = mouse_volume_percent / 100.0f;
if (g_verbose)
printf("Mouse volume set to: %d%%\n", mouse_volume_percent);
@@ -201,6 +125,16 @@ int main(int argc, char *argv[]) {
if (g_verbose)
printf("Mouse mute: %s\n", g_mouse_mute ? "enabled" : "disabled");
}
if (argc >= 10) {
g_keyboard_enabled = atoi(argv[9]);
if (g_verbose)
printf("Keyboard enabled: %s\n", g_keyboard_enabled ? "yes" : "no");
}
if (argc >= 11) {
g_mouse_enabled = atoi(argv[10]);
if (g_verbose)
printf("Mouse enabled: %s\n", g_mouse_enabled ? "yes" : "no");
}
if (load_sound_config(argv[1]) != 0) {
fprintf(stderr, "Failed to load keyboard sound configuration\n");
return 1;
@@ -216,8 +150,7 @@ 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();
+3 -12
View File
@@ -1,5 +1,6 @@
#include "audio/playback.h"
#include "audio/types.h"
#include "common/utils.h"
#include <json-c/json.h>
#include <pthread.h>
#include <pulse/error.h>
@@ -212,7 +213,7 @@ 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) {
@@ -268,20 +269,10 @@ void play_sound_segment(int key_code, int is_pressed) {
pthread_mutex_unlock(&thread_mutex);
}
static char *xstrdup2(const char *s) {
if (!s)
return NULL;
size_t n = strlen(s) + 1;
char *p = (char *)malloc(n);
if (!p)
return NULL;
memcpy(p, s, n);
return p;
}
int parse_keyboard_event(const char *json_line, int *key_code,
int *is_pressed) {
char *line_copy = xstrdup2(json_line);
char *line_copy = xstrdup(json_line);
if (!line_copy)
return -1;
char *newline = strchr(line_copy, '\n');
+69 -90
View File
@@ -1,52 +1,86 @@
#include "app/cli.h"
#include "common/utils.h"
#include "config.h"
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Helper function to parse device-specific arguments
static int parse_device_argument(const char *optarg, const char *action,
int *keyboard_value, int *mouse_value, int value) {
if (optarg == NULL || strcmp(optarg, "both") == 0) {
*keyboard_value = value;
*mouse_value = value;
} else if (strcmp(optarg, "keyboard") == 0) {
*keyboard_value = value;
} else if (strcmp(optarg, "mouse") == 0) {
*mouse_value = value;
} else {
fprintf(stderr, "Error: Invalid %s device '%s'\n", action, optarg);
fprintf(stderr, "Use: keyboard, mouse, or both\n");
return 1;
}
return 0;
}
// Helper function to validate and set volume
static void set_volume(int *volume_ptr, int value) {
*volume_ptr = validate_volume(value);
}
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 --disable mouse # Disable mouse sounds\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) {
@@ -115,96 +149,41 @@ int parse_cli(int argc, char **argv, CliOptions *out) {
out->mouse_sound = optarg;
break;
case 'V':
out->volume = atoi(optarg);
if (out->volume < 0)
out->volume = 0;
if (out->volume > 100)
out->volume = 100;
set_volume(&out->volume, atoi(optarg));
break;
case 'K':
out->keyboard_volume = atoi(optarg);
if (out->keyboard_volume < 0)
out->keyboard_volume = 0;
if (out->keyboard_volume > 100)
out->keyboard_volume = 100;
set_volume(&out->keyboard_volume, atoi(optarg));
break;
case 'O':
out->mouse_volume = atoi(optarg);
if (out->mouse_volume < 0)
out->mouse_volume = 0;
if (out->mouse_volume > 100)
out->mouse_volume = 100;
set_volume(&out->mouse_volume, atoi(optarg));
break;
case 'm':
if (optarg == NULL || strcmp(optarg, "both") == 0) {
out->keyboard_mute = 1;
out->mouse_mute = 1;
} else if (strcmp(optarg, "keyboard") == 0) {
out->keyboard_mute = 1;
} else if (strcmp(optarg, "mouse") == 0) {
out->mouse_mute = 1;
} else {
fprintf(stderr, "Error: Invalid mute device '%s'\n", optarg);
fprintf(stderr, "Use: keyboard, mouse, or both\n");
if (parse_device_argument(optarg, "mute", &out->keyboard_mute, &out->mouse_mute, 1) != 0) {
return 1;
}
break;
case 'u':
if (optarg == NULL || strcmp(optarg, "both") == 0) {
out->keyboard_mute = 0;
out->mouse_mute = 0;
} else if (strcmp(optarg, "keyboard") == 0) {
out->keyboard_mute = 0;
} else if (strcmp(optarg, "mouse") == 0) {
out->mouse_mute = 0;
} else {
fprintf(stderr, "Error: Invalid unmute device '%s'\n", optarg);
fprintf(stderr, "Use: keyboard, mouse, or both\n");
if (parse_device_argument(optarg, "unmute", &out->keyboard_mute, &out->mouse_mute, 0) != 0) {
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
{
int enable_value = (opt == 1000) ? 1 : 0;
const char *action = (opt == 1000) ? "enable" : "disable";
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 (parse_device_argument(optarg, action, &out->keyboard_enabled, &out->mouse_enabled, enable_value) != 0) {
return 1;
}
}
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':
+39 -60
View File
@@ -28,49 +28,32 @@ int read_runtime_mute_file() {
return 0;
}
void write_runtime_mute_file(int mute) {
char mute_file[1024];
// Generic helper to write runtime state files
static void write_runtime_state_file(const char *filename_suffix, int value) {
char state_file[1024];
const char *rd = getenv("XDG_RUNTIME_DIR");
if (!rd || strlen(rd) == 0)
rd = "/tmp";
if (!safe_snprintf(mute_file, sizeof(mute_file), "%s/keyvibe-mute-%d", rd,
(int)getuid()))
if (!safe_snprintf(state_file, sizeof(state_file), "%s/keyvibe-%s-%d", rd,
filename_suffix, (int)getuid()))
return;
FILE *f = fopen(mute_file, "w");
FILE *f = fopen(state_file, "w");
if (!f)
return;
fprintf(f, "%d\n", mute ? 1 : 0);
fprintf(f, "%d\n", value);
fclose(f);
}
void write_runtime_mute_file(int mute) {
write_runtime_state_file("mute", mute ? 1 : 0);
}
void write_runtime_keyboard_mute_file(int mute) {
char mute_file[1024];
const char *rd = getenv("XDG_RUNTIME_DIR");
if (!rd || strlen(rd) == 0)
rd = "/tmp";
if (!safe_snprintf(mute_file, sizeof(mute_file), "%s/keyvibe-kbd-mute-%d", rd,
(int)getuid()))
return;
FILE *f = fopen(mute_file, "w");
if (!f)
return;
fprintf(f, "%d\n", mute ? 1 : 0);
fclose(f);
write_runtime_state_file("kbd-mute", mute ? 1 : 0);
}
void write_runtime_mouse_mute_file(int mute) {
char mute_file[1024];
const char *rd = getenv("XDG_RUNTIME_DIR");
if (!rd || strlen(rd) == 0)
rd = "/tmp";
if (!safe_snprintf(mute_file, sizeof(mute_file), "%s/keyvibe-mouse-mute-%d",
rd, (int)getuid()))
return;
FILE *f = fopen(mute_file, "w");
if (!f)
return;
fprintf(f, "%d\n", mute ? 1 : 0);
fclose(f);
write_runtime_state_file("mouse-mute", mute ? 1 : 0);
}
int safe_snprintf(char *dst, size_t dst_sz, const char *fmt, ...) {
@@ -133,39 +116,35 @@ int process_is_running(pid_t pid) {
}
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);
write_runtime_state_file("kbd-enabled", enabled);
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);
write_runtime_state_file("mouse-enabled", enabled);
return 1;
}
void int_to_str(char *buffer, size_t size, int value) {
snprintf(buffer, size, "%d", value);
}
// Safe string duplication
char *xstrdup(const char *s) {
if (!s)
return NULL;
size_t n = strlen(s) + 1;
char *p = (char *)malloc(n);
if (!p)
return NULL;
memcpy(p, s, n);
return p;
}
// Validate and clamp volume to 0-100 range
int validate_volume(int volume) {
if (volume < 0) return 0;
if (volume > 100) return 100;
return volume;
}
+4 -12
View File
@@ -8,16 +8,6 @@
#include <string.h>
#include <unistd.h>
static char *xstrdup(const char *s) {
if (!s)
return NULL;
size_t n = strlen(s) + 1;
char *p = (char *)malloc(n);
if (!p)
return NULL;
memcpy(p, s, n);
return p;
}
char *get_user_config_path(char *buffer, size_t buflen) {
const char *home = getenv("HOME");
@@ -35,7 +25,8 @@ 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) {
int mouse_volume, int keyboard_enabled,
int mouse_enabled) {
json_object *root = json_object_new_object();
if (!root)
return 0;
@@ -72,7 +63,8 @@ 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_volume, int *out_keyboard_enabled,
int *out_mouse_enabled) {
FILE *f = fopen(path, "r");
if (!f)
return 0;
+17 -15
View File
@@ -7,8 +7,8 @@
#include "common/mute.h"
#include "common/utils.h"
#include "config.h"
#include "user_config.h"
#include "soundpacks.h"
#include "user_config.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
@@ -77,7 +77,8 @@ int main(int argc, char *argv[]) {
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_enabled, &cfg_mouse_enabled)) {
&cfg_keyboard_volume, &cfg_mouse_volume,
&cfg_keyboard_enabled, &cfg_mouse_enabled)) {
if (cfg_keyboard_sound) {
if (sound_name_owned) {
free(sound_name);
@@ -98,27 +99,26 @@ int main(int argc, char *argv[]) {
}
}
}
// Parse CLI arguments
int parse_result = parse_cli(argc, argv, &cli_opts);
if (parse_result == 1) {
if (sound_name_owned) {
free(sound_name);
}
if (sound_name_owned) {
free(sound_name);
}
if (mouse_sound_name_owned) {
free(mouse_sound_name);
}
return 1;
} else if (parse_result == 2) {
if (sound_name_owned) {
free(sound_name);
}
if (sound_name_owned) {
free(sound_name);
}
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;
@@ -204,11 +204,11 @@ int main(int argc, char *argv[]) {
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)) {
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) {
@@ -230,7 +230,8 @@ int main(int argc, char *argv[]) {
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_enabled, current_mouse_enabled)) {
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);
@@ -342,7 +343,8 @@ int main(int argc, char *argv[]) {
}
if (!start_children(sound_dir, config_path, volume, verbose, current_mute,
mouse_sound_dir, mouse_config_path, current_mouse_volume,
current_keyboard_mute, current_mouse_mute)) {
current_keyboard_mute, current_mouse_mute,
current_keyboard_enabled, current_mouse_enabled)) {
if (sound_name_owned) {
free(sound_name);
}