mirror of
https://github.com/Tha14/toxic.git
synced 2025-07-01 06:06:45 +02:00
Added VAD, changed device i/o, mute option, dynamic device changing and more
This commit is contained in:
146
src/settings.c
146
src/settings.c
@ -20,95 +20,162 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "toxic.h"
|
||||
#include "windows.h"
|
||||
#include "configdir.h"
|
||||
|
||||
#ifdef _SUPPORT_AUDIO
|
||||
#include "device.h"
|
||||
#endif /* _SUPPORT_AUDIO */
|
||||
|
||||
#include "toxic_windows.h"
|
||||
#include "configdir.h"
|
||||
#include "settings.h"
|
||||
#include "line_info.h"
|
||||
|
||||
static void uset_autolog(struct user_settings *s, const char *val);
|
||||
static void uset_time(struct user_settings *s, const char *val);
|
||||
static void uset_alerts(struct user_settings *s, const char *val);
|
||||
static void uset_colours(struct user_settings *s, const char *val);
|
||||
static void uset_hst_size(struct user_settings *s, const char *val);
|
||||
static void uset_dwnld_path(struct user_settings *s, const char *val);
|
||||
|
||||
static void uset_autolog(struct user_settings *s, int val);
|
||||
static void uset_time(struct user_settings *s, int val);
|
||||
static void uset_alerts(struct user_settings *s, int val);
|
||||
static void uset_colours(struct user_settings *s, int val);
|
||||
static void uset_ain_dev(struct user_settings *s, int val);
|
||||
static void uset_aout_dev(struct user_settings *s, int val);
|
||||
#ifdef _SUPPORT_AUDIO
|
||||
static void uset_ain_dev(struct user_settings *s, const char *val);
|
||||
static void uset_aout_dev(struct user_settings *s, const char *val);
|
||||
#endif
|
||||
|
||||
struct {
|
||||
const char *name;
|
||||
void (*func)(struct user_settings *s, int val);
|
||||
const char *key;
|
||||
void (*func)(struct user_settings *s, const char *val);
|
||||
} user_settings_list[] = {
|
||||
{ "autolog", uset_autolog },
|
||||
{ "time", uset_time },
|
||||
{ "disable_alerts", uset_alerts },
|
||||
{ "colour_theme", uset_colours },
|
||||
|
||||
{ "history_size", uset_hst_size },
|
||||
{ "download_path", uset_dwnld_path },
|
||||
|
||||
#ifdef _SUPPORT_AUDIO
|
||||
{ "audio_in_dev", uset_ain_dev },
|
||||
{ "audio_out_dev", uset_aout_dev },
|
||||
#endif
|
||||
};
|
||||
|
||||
static void uset_autolog(struct user_settings *s, int val)
|
||||
static void uset_autolog(struct user_settings *s, const char *val)
|
||||
{
|
||||
int n = atoi(val);
|
||||
|
||||
/* default off if invalid value */
|
||||
s->autolog = val == AUTOLOG_ON ? AUTOLOG_ON : AUTOLOG_OFF;
|
||||
s->autolog = n == AUTOLOG_ON ? AUTOLOG_ON : AUTOLOG_OFF;
|
||||
}
|
||||
|
||||
static void uset_time(struct user_settings *s, int val)
|
||||
static void uset_time(struct user_settings *s, const char *val)
|
||||
{
|
||||
int n = atoi(val);
|
||||
|
||||
/* default to 24 hour time if invalid value */
|
||||
s->time = val == TIME_12 ? TIME_12 : TIME_24;
|
||||
s->time = n == TIME_12 ? TIME_12 : TIME_24;
|
||||
}
|
||||
|
||||
static void uset_alerts(struct user_settings *s, int val)
|
||||
static void uset_alerts(struct user_settings *s, const char *val)
|
||||
{
|
||||
int n = atoi(val);
|
||||
|
||||
/* alerts default on if invalid value */
|
||||
s->alerts = val == ALERTS_DISABLED ? ALERTS_DISABLED : ALERTS_ENABLED;
|
||||
s->alerts = n == ALERTS_DISABLED ? ALERTS_DISABLED : ALERTS_ENABLED;
|
||||
}
|
||||
|
||||
static void uset_colours(struct user_settings *s, int val)
|
||||
static void uset_colours(struct user_settings *s, const char *val)
|
||||
{
|
||||
int n = atoi(val);
|
||||
|
||||
/* use default toxic colours if invalid value */
|
||||
s->colour_theme = val == NATIVE_COLS ? NATIVE_COLS : DFLT_COLS;
|
||||
s->colour_theme = n == NATIVE_COLS ? NATIVE_COLS : DFLT_COLS;
|
||||
}
|
||||
|
||||
#ifdef _SUPPORT_AUDIO
|
||||
|
||||
static void uset_ain_dev(struct user_settings *s, int val)
|
||||
static void uset_ain_dev(struct user_settings *s, const char *val)
|
||||
{
|
||||
if (val < 0 || val > MAX_DEVICES)
|
||||
val = 0;
|
||||
s->audio_in_dev = val;
|
||||
int n = atoi(val);
|
||||
|
||||
if (n < 0 || n > MAX_DEVICES)
|
||||
n = (long int) 0;
|
||||
|
||||
s->audio_in_dev = (long int) n;
|
||||
}
|
||||
|
||||
static void uset_aout_dev(struct user_settings *s, int val)
|
||||
static void uset_aout_dev(struct user_settings *s, const char *val)
|
||||
{
|
||||
if (val < 0 || val > MAX_DEVICES)
|
||||
val = 0;
|
||||
int n = atoi(val);
|
||||
|
||||
s->audio_out_dev = val;
|
||||
if (n < 0 || n > MAX_DEVICES)
|
||||
n = (long int) 0;
|
||||
|
||||
s->audio_out_dev = (long int) n;
|
||||
}
|
||||
|
||||
#endif /* _SUPPORT_AUDIO */
|
||||
|
||||
static void uset_hst_size(struct user_settings *s, const char *val)
|
||||
{
|
||||
int n = atoi(val);
|
||||
|
||||
/* if val is out of range use default history size */
|
||||
s->history_size = (n > MAX_HISTORY || n < MIN_HISTORY) ? DFLT_HST_SIZE : n;
|
||||
}
|
||||
|
||||
static void uset_dwnld_path(struct user_settings *s, const char *val)
|
||||
{
|
||||
memset(s->download_path, 0, sizeof(s->download_path));
|
||||
|
||||
if (val == NULL)
|
||||
return;
|
||||
|
||||
int len = strlen(val);
|
||||
|
||||
if (len >= sizeof(s->download_path) - 2) /* leave room for null and '/' */
|
||||
return;
|
||||
|
||||
FILE *fp = fopen(val, "r");
|
||||
|
||||
if (fp == NULL)
|
||||
return;
|
||||
|
||||
strcpy(s->download_path, val);
|
||||
|
||||
if (val[len - 1] != '/')
|
||||
strcat(s->download_path, "/");
|
||||
}
|
||||
|
||||
static void set_default_settings(struct user_settings *s)
|
||||
{
|
||||
/* see settings_values enum in settings.h for defaults */
|
||||
uset_autolog(s, "0");
|
||||
uset_time(s, "24");
|
||||
uset_alerts(s, "0");
|
||||
uset_colours(s, "0");
|
||||
uset_hst_size(s, "700");
|
||||
uset_dwnld_path(s, NULL);
|
||||
|
||||
#ifdef _SUPPORT_AUDIO
|
||||
uset_ain_dev(s, "0");
|
||||
uset_aout_dev(s, "0");
|
||||
#endif
|
||||
}
|
||||
|
||||
int settings_load(struct user_settings *s, char *path)
|
||||
{
|
||||
char *user_config_dir = get_user_config_dir();
|
||||
FILE *fp = NULL;
|
||||
char dflt_path[MAX_STR_SIZE];
|
||||
|
||||
|
||||
if (path) {
|
||||
fp = fopen(path, "r");
|
||||
} else {
|
||||
@ -118,34 +185,37 @@ int settings_load(struct user_settings *s, char *path)
|
||||
|
||||
free(user_config_dir);
|
||||
|
||||
set_default_settings(s);
|
||||
|
||||
if (fp == NULL && !path) {
|
||||
if ((fp = fopen(dflt_path, "w")) == NULL)
|
||||
return -1;
|
||||
} else if (fp == NULL && path) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
char line[MAX_STR_SIZE];
|
||||
|
||||
while (fgets(line, sizeof(line), fp)) {
|
||||
if (line[0] == '#' || !line[0])
|
||||
continue;
|
||||
|
||||
|
||||
const char *key = strtok(line, ":");
|
||||
const char *val = strtok(NULL, ";");
|
||||
|
||||
|
||||
if (key == NULL || val == NULL)
|
||||
continue;
|
||||
|
||||
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NUM_SETTINGS; ++i) {
|
||||
if (strcmp(user_settings_list[i].name, key) == 0) {
|
||||
(user_settings_list[i].func)(s, atoi(val));
|
||||
if (strcmp(user_settings_list[i].key, key) == 0) {
|
||||
(user_settings_list[i].func)(s, val);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user