From 4643996c3af09ec3af86c01ac0f91fc9f9632499 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 8 Apr 2014 17:20:21 -0400 Subject: [PATCH] add audio settings for conf file --- misc/toxic.conf | 6 ++++++ src/audio_call.c | 34 +++++++++++++++++++--------------- src/audio_call.h | 9 +++++++++ src/main.c | 9 +++++++-- src/settings.c | 23 ++++++++++++++++++++++- src/settings.h | 10 ++++++---- 6 files changed, 69 insertions(+), 22 deletions(-) diff --git a/misc/toxic.conf b/misc/toxic.conf index e3966d3..3c763db 100644 --- a/misc/toxic.conf +++ b/misc/toxic.conf @@ -6,3 +6,9 @@ autolog:0; # 1 to use native terminal colours, 0 to use toxic default colour theme colour_theme:0; + +# preferred audio input device; numbers correspond to /lsdev in +audio_in_dev:0; + +# preferred audio output device; numbers correspond to /lsdev out +audio_out_dev:0; diff --git a/src/audio_call.c b/src/audio_call.c index 3e9e3ab..f38fdca 100644 --- a/src/audio_call.c +++ b/src/audio_call.c @@ -21,7 +21,6 @@ #include #include -#define MAX_DEVICES 32 #define _cbend pthread_exit(NULL) #define AUDIO_FRAME_SIZE (av_DefaultSettings.audio_sample_rate * av_DefaultSettings.audio_frame_duration / 1000) @@ -36,12 +35,6 @@ typedef struct _DeviceIx { int index; /* Current index */ } DeviceIx; -typedef enum _Devices -{ - input, - output, -} _Devices; - struct _ASettings { DeviceIx device[2]; @@ -72,6 +65,21 @@ static void print_err (ToxWindow *self, uint8_t *error_str) line_info_add(self, NULL, NULL, NULL, error_str, SYS_MSG, 0, 0); } +int device_set(ToxWindow *self, _Devices type, long int selection) +{ + uint8_t error_str[MAX_STR_SIZE]; + uint8_t *s_type = type == input ? "input" : "output"; + + if ( selection < 0 || selection >= ASettins.device[type].size ) { + snprintf(error_str, sizeof(error_str), "Cannot set audio %s device: Invalid index", s_type); + line_info_add(self, NULL, NULL, NULL, error_str, SYS_MSG, 0, 0); + return -1; + } + + ASettins.device[type].index = selection; + return 0; +} + /* Opens device under current index */ int device_open (ToxWindow *self, _Devices type) @@ -709,16 +717,12 @@ void cmd_change_device(WINDOW *window, ToxWindow *self, Tox *m, int argc, char ( error_str = "Invalid input"; goto on_error; } - - if ( selection < 0 || selection >= ASettins.device[type].size ) { - error_str = "No device with such index"; - goto on_error; + + if ( device_set(self, type, selection ) == 0) { + snprintf(msg, sizeof(msg), "Selected: %s", ASettins.device[type].devices[selection]); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } - ASettins.device[type].index = selection; - snprintf(msg, sizeof(msg), "Selected: %s", ASettins.device[type].devices[selection]); - line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); - return; on_error: print_err (self, error_str); diff --git a/src/audio_call.h b/src/audio_call.h index 49cdd68..e55d1df 100644 --- a/src/audio_call.h +++ b/src/audio_call.h @@ -7,6 +7,8 @@ #include +#define MAX_DEVICES 32 + typedef struct ToxWindow ToxWindow; typedef enum _AudioError @@ -17,6 +19,12 @@ typedef enum _AudioError ErrorStartingCoreAudio = 1 << 2 } AudioError; +typedef enum _Devices +{ + input, + output, +} _Devices; + /* You will have to pass pointer to first member of 'windows' * declared in windows.c otherwise undefined behaviour will */ @@ -26,5 +34,6 @@ void terminate_audio(); int errors(); int start_transmission(ToxWindow *self); +int device_set(ToxWindow *self, _Devices type, long int selection); #endif /* _audio_h */ \ No newline at end of file diff --git a/src/main.c b/src/main.c index 007e30b..613c988 100644 --- a/src/main.c +++ b/src/main.c @@ -505,6 +505,8 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } + memset(user_settings, 0, sizeof(struct user_settings)); + int settings_err = settings_load(user_settings, NULL); init_term(); @@ -538,12 +540,15 @@ int main(int argc, char *argv[]) av = init_audio(prompt, m); if ( errors() == NoError ) - msg = "Audio started with no problems."; + msg = "Audio initiated with no problems."; else /* Get error code and stuff */ - msg = "Error starting audio!"; + msg = "Error initiating audio!"; line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); + device_set(prompt, input, user_settings->audio_in_dev); + device_set(prompt, output, user_settings->audio_out_dev); + #endif /* _SUPPORT_AUDIO */ if (f_loadfromfile) diff --git a/src/settings.c b/src/settings.c index 7833485..62b6e4e 100644 --- a/src/settings.c +++ b/src/settings.c @@ -25,11 +25,14 @@ #include "toxic_windows.h" #include "configdir.h" +#include "audio_call.h" #include "settings.h" static void uset_autolog(struct user_settings *s, int val); static void uset_time(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); struct { const char *name; @@ -38,10 +41,12 @@ struct { { "autolog", uset_autolog }, { "time", uset_time }, { "colour_theme", uset_colours }, + { "audio_in_dev", uset_ain_dev }, + { "audio_out_dev", uset_aout_dev }, }; static void uset_autolog(struct user_settings *s, int val) -{ +{ /* default off if invalid value */ s->autolog = val == AUTOLOG_ON ? AUTOLOG_ON : AUTOLOG_OFF; } @@ -58,6 +63,22 @@ static void uset_colours(struct user_settings *s, int val) s->colour_theme = val == NATIVE_COLS ? NATIVE_COLS : DFLT_COLS; } +static void uset_ain_dev(struct user_settings *s, int val) +{ + if (val < 0 || val > MAX_DEVICES) + val = (long int) 0; + + s->audio_in_dev = (long int) val; +} + +static void uset_aout_dev(struct user_settings *s, int val) +{ + if (val < 0 || val > MAX_DEVICES) + val = (long int) 0; + + s->audio_out_dev = (long int) val; +} + int settings_load(struct user_settings *s, char *path) { char *user_config_dir = get_user_config_dir(); diff --git a/src/settings.h b/src/settings.h index 66eaabc..47ea552 100644 --- a/src/settings.h +++ b/src/settings.h @@ -20,13 +20,15 @@ * */ -#define NUM_SETTINGS 3 +#define NUM_SETTINGS 5 /* holds user setting values */ struct user_settings { - int autolog; /* boolean */ - int time; /* 12 or 24 */ - int colour_theme; /* boolean (0 for default toxic colours) */ + int autolog; /* boolean */ + int time; /* 12 or 24 */ + int colour_theme; /* boolean (0 for default toxic colours) */ + long int audio_in_dev; + long int audio_out_dev; }; enum {