Refactor input method magic numbers into enum

master
Tudor Brindus 6 years ago
parent cf7eff3310
commit f6d29db711
  1. 52
      cava.c
  2. 159
      config.c
  3. 50
      config.h

@ -268,7 +268,6 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
char ch = '\0'; char ch = '\0';
int bars = 25; int bars = 25;
char supportedInput[255] = "'fifo'";
int sourceIsAuto = 1; int sourceIsAuto = 1;
double smh; double smh;
@ -315,19 +314,6 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
n = 0; n = 0;
} }
#ifdef ALSA
strcat(supportedInput, ", 'alsa'");
#endif
#ifdef PULSE
strcat(supportedInput, ", 'pulse'");
#endif
#ifdef SNDIO
strcat(supportedInput, ", 'sndio'");
#endif
#ifdef SHMEM
strcat(supportedInput, ", 'shmem'");
#endif
// general: main loop // general: main loop
while (1) { while (1) {
@ -335,7 +321,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
// config: load // config: load
struct error_s error; struct error_s error;
error.length = 0; error.length = 0;
if (!load_config(configPath, supportedInput, (void *)&p, 0, &error)) { if (!load_config(configPath, &p, 0, &error)) {
fprintf(stderr, "Error loading config. %s", error.message); fprintf(stderr, "Error loading config. %s", error.message);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -388,9 +374,10 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
audio.treble_index = 0; audio.treble_index = 0;
debug("starting audio thread\n"); debug("starting audio thread\n");
switch (p.im) {
#ifdef ALSA #ifdef ALSA
// input_alsa: wait for the input to be ready case INPUT_ALSA:
if (p.im == 1) { // input_alsa: wait for the input to be ready
if (is_loop_device_for_sure(audio.source)) { if (is_loop_device_for_sure(audio.source)) {
if (directory_exists("/sys/")) { if (directory_exists("/sys/")) {
if (!directory_exists("/sys/module/snd_aloop/")) { if (!directory_exists("/sys/module/snd_aloop/")) {
@ -421,17 +408,15 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
} }
} }
debug("got format: %d and rate %d\n", audio.format, audio.rate); debug("got format: %d and rate %d\n", audio.format, audio.rate);
} break;
#endif #endif
case INPUT_FIFO:
if (p.im == 2) {
// starting fifomusic listener // starting fifomusic listener
thr_id = pthread_create(&p_thread, NULL, input_fifo, (void *)&audio); thr_id = pthread_create(&p_thread, NULL, input_fifo, (void *)&audio);
audio.rate = p.fifoSample; audio.rate = p.fifoSample;
} break;
#ifdef PULSE #ifdef PULSE
if (p.im == 3) { case INPUT_PULSE:
if (strcmp(audio.source, "auto") == 0) { if (strcmp(audio.source, "auto") == 0) {
getPulseDefaultSink((void *)&audio); getPulseDefaultSink((void *)&audio);
sourceIsAuto = 1; sourceIsAuto = 1;
@ -440,29 +425,30 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
// starting pulsemusic listener // starting pulsemusic listener
thr_id = pthread_create(&p_thread, NULL, input_pulse, (void *)&audio); thr_id = pthread_create(&p_thread, NULL, input_pulse, (void *)&audio);
audio.rate = 44100; audio.rate = 44100;
} break;
#endif #endif
#ifdef SNDIO #ifdef SNDIO
if (p.im == 4) { case INPUT_SNDIO:
thr_id = pthread_create(&p_thread, NULL, input_sndio, (void *)&audio); thr_id = pthread_create(&p_thread, NULL, input_sndio, (void *)&audio);
audio.rate = 44100; audio.rate = 44100;
} break;
#endif #endif
#ifdef SHMEM #ifdef SHMEM
if (p.im == 5) { case INPUT_SHMEM:
thr_id = pthread_create(&p_thread, NULL, input_shmem, (void *)&audio); thr_id = pthread_create(&p_thread, NULL, input_shmem, (void *)&audio);
// audio.rate = 44100; // audio.rate = 44100;
} break;
#endif #endif
#ifdef PORTAUDIO #ifdef PORTAUDIO
if (p.im == 6) { case INPUT_PORTAUDIO:
thr_id = pthread_create(&p_thread, NULL, input_portaudio, (void *)&audio); thr_id = pthread_create(&p_thread, NULL, input_portaudio, (void *)&audio);
audio.rate = 44100; audio.rate = 44100;
} break;
#endif #endif
default:
exit(EXIT_FAILURE); // Can't happen.
}
if (p.highcf > audio.rate / 2) { if (p.highcf > audio.rate / 2) {
cleanup(); cleanup();
@ -783,7 +769,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
if (reload_colors) { if (reload_colors) {
struct error_s error; struct error_s error;
error.length = 0; error.length = 0;
if (!load_config(configPath, supportedInput, (void *)&p, 1, &error)) { if (!load_config(configPath, (void *)&p, 1, &error)) {
cleanup(); cleanup();
fprintf(stderr, "Error loading config. %s", error.message); fprintf(stderr, "Error loading config. %s", error.message);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

@ -9,7 +9,26 @@
double smoothDef[5] = {1, 1, 1, 1, 1}; double smoothDef[5] = {1, 1, 1, 1, 1};
char *inputMethod, *outputMethod, *channels; char *outputMethod, *channels;
const char *input_method_names[] = {
"fifo", "portaudio", "alsa", "pulse", "sndio", "shmem",
};
const bool has_input_method[] = {
true, /** Always have at least FIFO input. */
HAS_PORTAUDIO, HAS_ALSA, HAS_PULSE, HAS_SNDIO, HAS_SHMEM,
};
enum input_method input_method_by_name(const char *str) {
for (int i = 0; i < INPUT_MAX; i++) {
if (!strcmp(str, input_method_names[i])) {
return (enum input_method)i;
}
}
return INPUT_MAX;
}
void write_errorf(void *err, const char *fmt, ...) { void write_errorf(void *err, const char *fmt, ...) {
struct error_s *error = (struct error_s *)err; struct error_s *error = (struct error_s *)err;
@ -127,55 +146,7 @@ bool validate_colors(void *params, void *err) {
return true; return true;
} }
bool validate_config(char supportedInput[255], struct config_params *p, struct error_s *error) { bool validate_config(struct config_params *p, struct error_s *error) {
// validate: input method
p->im = 0;
if (strcmp(inputMethod, "alsa") == 0) {
p->im = 1;
#ifndef ALSA
write_errorf(error, "cava was built without alsa support, install alsa dev files and run "
"make clean && ./configure && make again\n");
return false;
#endif
}
if (strcmp(inputMethod, "fifo") == 0) {
p->im = 2;
}
if (strcmp(inputMethod, "pulse") == 0) {
p->im = 3;
#ifndef PULSE
write_errorf(error, "cava was built without pulseaudio support, install pulseaudio dev "
"files and run make clean && ./configure && make again\n");
return false;
#endif
}
if (strcmp(inputMethod, "sndio") == 0) {
p->im = 4;
#ifndef SNDIO
write_errorf(error, "cava was built without sndio support\n");
return false;
#endif
}
if (strcmp(inputMethod, "shmem") == 0) {
p->im = 5;
#ifndef SHMEM
write_errorf(error, "cava was built without shmem support\n");
return false;
#endif
}
if (strcmp(inputMethod, "portaudio") == 0) {
p->im = 6;
#ifndef PORTAUDIO
write_errorf(error, "cava was built without portaudio support\n");
return false;
#endif
}
if (p->im == 0) {
write_errorf(error, "input method '%s' is not supported, supported methods are: %s\n",
inputMethod, supportedInput);
return false;
}
// validate: output method // validate: output method
p->om = 0; p->om = 0;
if (strcmp(outputMethod, "ncurses") == 0) { if (strcmp(outputMethod, "ncurses") == 0) {
@ -370,8 +341,8 @@ bool load_colors(struct config_params *p, dictionary *ini, void *err) {
return true; return true;
} }
bool load_config(char configPath[255], char supportedInput[255], struct config_params *p, bool load_config(char configPath[255], struct config_params *p, bool colorsOnly,
bool colorsOnly, struct error_s *error) { struct error_s *error) {
FILE *fp; FILE *fp;
// config: creating path to default config file // config: creating path to default config file
@ -428,24 +399,6 @@ bool load_config(char configPath[255], char supportedInput[255], struct config_p
return validate_colors(p, error); return validate_colors(p, error);
} }
// setting fifo to defaualt if no other input modes supported
inputMethod = (char *)iniparser_getstring(ini, "input:method", "fifo");
// setting portaudio to default if supported
#ifdef ALSA
inputMethod = (char *)iniparser_getstring(ini, "input:method", "portaudio");
#endif
// setting alsa to defaualt if supported
#ifdef ALSA
inputMethod = (char *)iniparser_getstring(ini, "input:method", "alsa");
#endif
// setting pulse to defaualt if supported
#ifdef PULSE
inputMethod = (char *)iniparser_getstring(ini, "input:method", "pulse");
#endif
#ifdef NCURSES #ifdef NCURSES
outputMethod = (char *)iniparser_getstring(ini, "output:method", "ncurses"); outputMethod = (char *)iniparser_getstring(ini, "output:method", "ncurses");
#endif #endif
@ -512,42 +465,66 @@ bool load_config(char configPath[255], char supportedInput[255], struct config_p
free(p->audio_source); free(p->audio_source);
// config: input char *input_method_name;
p->im = 0; for (int i = INPUT_MAX - 1; i >= 0; i--) {
if (strcmp(inputMethod, "alsa") == 0) { if (has_input_method[i]) {
p->im = 1; input_method_name =
p->audio_source = strdup(iniparser_getstring(ini, "input:source", "hw:Loopback,1")); (char *)iniparser_getstring(ini, "input:method", input_method_names[i]);
}
} }
if (strcmp(inputMethod, "fifo") == 0) {
p->im = 2; p->im = input_method_by_name(input_method_name);
switch (p->im) {
#ifdef ALSA
case INPUT_ALSA:
p->audio_source = strdup(iniparser_getstring(ini, "input:source", "hw:Loopback,1"));
break;
#endif
case INPUT_FIFO:
p->audio_source = strdup(iniparser_getstring(ini, "input:source", "/tmp/mpd.fifo")); p->audio_source = strdup(iniparser_getstring(ini, "input:source", "/tmp/mpd.fifo"));
p->fifoSample = iniparser_getint(ini, "input:sample_rate", 44100); p->fifoSample = iniparser_getint(ini, "input:sample_rate", 44100);
} break;
if (strcmp(inputMethod, "pulse") == 0) { #ifdef PULSE
p->im = 3; case INPUT_PULSE:
p->audio_source = strdup(iniparser_getstring(ini, "input:source", "auto")); p->audio_source = strdup(iniparser_getstring(ini, "input:source", "auto"));
} break;
#endif
#ifdef SNDIO #ifdef SNDIO
if (strcmp(inputMethod, "sndio") == 0) { case INPUT_SNDIO:
p->im = 4;
p->audio_source = strdup(iniparser_getstring(ini, "input:source", SIO_DEVANY)); p->audio_source = strdup(iniparser_getstring(ini, "input:source", SIO_DEVANY));
} break;
#endif #endif
#ifdef SHMEM #ifdef SHMEM
if (strcmp(inputMethod, "shmem") == 0) { case INPUT_SHMEM:
p->im = 5;
p->audio_source = p->audio_source =
strdup(iniparser_getstring(ini, "input:source", "/squeezelite-00:00:00:00:00:00")); strdup(iniparser_getstring(ini, "input:source", "/squeezelite-00:00:00:00:00:00"));
} break;
#endif #endif
#ifdef PORTAUDIO #ifdef PORTAUDIO
if (strcmp(inputMethod, "portaudio") == 0) { case INPUT_PORTAUDIO:
p->im = 6;
p->audio_source = strdup(iniparser_getstring(ini, "input:source", "auto")); p->audio_source = strdup(iniparser_getstring(ini, "input:source", "auto"));
} break;
#endif #endif
case INPUT_MAX: {
char supported_methods[255] = "";
for (int i = 0; i < INPUT_MAX; i++) {
if (has_input_method[i]) {
strcat(supported_methods, "'");
strcat(supported_methods, input_method_names[i]);
strcat(supported_methods, "' ");
}
}
write_errorf(error, "input method '%s' is not supported, supported methods are: %s\n",
input_method_name, supported_methods);
return false;
}
default:
write_errorf(error, "cava was built without '%s' input support\n",
input_method_names[p->im]);
return false;
}
bool result = validate_config(supportedInput, p, error); bool result = validate_config(p, error);
iniparser_freedict(ini); iniparser_freedict(ini);
return result; return result;
} }

@ -2,9 +2,52 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#define MAX_ERROR_LEN 1024 #define MAX_ERROR_LEN 1024
#ifdef PORTAUDIO
#define HAS_PORTAUDIO true
#else
#define HAS_PORTAUDIO false
#endif
#ifdef ALSA
#define HAS_ALSA true
#else
#define HAS_ALSA false
#endif
#ifdef PULSE
#define HAS_PULSE true
#else
#define HAS_PULSE false
#endif
#ifdef SNDIO
#define HAS_SNDIO true
#else
#define HAS_SNDIO false
#endif
#ifdef SHMEM
#define HAS_SHMEM true
#else
#define HAS_SHMEM false
#endif
// These are in order of least-favourable to most-favourable choices, in case
// multiple are supported and configured.
enum input_method {
INPUT_FIFO,
INPUT_PORTAUDIO,
INPUT_ALSA,
INPUT_PULSE,
INPUT_SNDIO,
INPUT_SHMEM,
INPUT_MAX
};
struct config_params { struct config_params {
char *color, *bcolor, *raw_target, *audio_source, char *color, *bcolor, *raw_target, *audio_source,
/**gradient_color_1, *gradient_color_2,*/ **gradient_colors, *data_format, *mono_option; /**gradient_color_1, *gradient_color_2,*/ **gradient_colors, *data_format, *mono_option;
@ -12,7 +55,8 @@ struct config_params {
double monstercat, integral, gravity, ignore, sens; double monstercat, integral, gravity, ignore, sens;
unsigned int lowcf, highcf; unsigned int lowcf, highcf;
double *smooth; double *smooth;
int smcount, customEQ, im, om, col, bgcol, autobars, stereo, is_bin, ascii_range, bit_format, enum input_method im;
int smcount, customEQ, om, col, bgcol, autobars, stereo, is_bin, ascii_range, bit_format,
gradient, gradient_count, fixedbars, framerate, bw, bs, autosens, overshoot, waves, gradient, gradient_count, fixedbars, framerate, bw, bs, autosens, overshoot, waves,
FFTbufferSize, fifoSample; FFTbufferSize, fifoSample;
}; };
@ -22,5 +66,5 @@ struct error_s {
int length; int length;
}; };
bool load_config(char configPath[255], char supportedInput[255], struct config_params *p, bool load_config(char configPath[255], struct config_params *p, bool colorsOnly,
bool colorsOnly, struct error_s *error); struct error_s *error);

Loading…
Cancel
Save