Merge pull request #320 from Xyene/input-method-enum

Refactor input method magic numbers into enum
master
karl 6 years ago committed by GitHub
commit 4f488e73ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  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';
int bars = 25;
char supportedInput[255] = "'fifo'";
int sourceIsAuto = 1;
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;
}
#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
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
struct error_s error;
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);
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;
debug("starting audio thread\n");
switch (p.im) {
#ifdef ALSA
// input_alsa: wait for the input to be ready
if (p.im == 1) {
case INPUT_ALSA:
// input_alsa: wait for the input to be ready
if (is_loop_device_for_sure(audio.source)) {
if (directory_exists("/sys/")) {
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);
}
break;
#endif
if (p.im == 2) {
case INPUT_FIFO:
// starting fifomusic listener
thr_id = pthread_create(&p_thread, NULL, input_fifo, (void *)&audio);
audio.rate = p.fifoSample;
}
break;
#ifdef PULSE
if (p.im == 3) {
case INPUT_PULSE:
if (strcmp(audio.source, "auto") == 0) {
getPulseDefaultSink((void *)&audio);
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
thr_id = pthread_create(&p_thread, NULL, input_pulse, (void *)&audio);
audio.rate = 44100;
}
break;
#endif
#ifdef SNDIO
if (p.im == 4) {
case INPUT_SNDIO:
thr_id = pthread_create(&p_thread, NULL, input_sndio, (void *)&audio);
audio.rate = 44100;
}
break;
#endif
#ifdef SHMEM
if (p.im == 5) {
case INPUT_SHMEM:
thr_id = pthread_create(&p_thread, NULL, input_shmem, (void *)&audio);
// audio.rate = 44100;
}
break;
#endif
#ifdef PORTAUDIO
if (p.im == 6) {
case INPUT_PORTAUDIO:
thr_id = pthread_create(&p_thread, NULL, input_portaudio, (void *)&audio);
audio.rate = 44100;
}
break;
#endif
default:
exit(EXIT_FAILURE); // Can't happen.
}
if (p.highcf > audio.rate / 2) {
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) {
struct error_s error;
error.length = 0;
if (!load_config(configPath, supportedInput, (void *)&p, 1, &error)) {
if (!load_config(configPath, (void *)&p, 1, &error)) {
cleanup();
fprintf(stderr, "Error loading config. %s", error.message);
exit(EXIT_FAILURE);

@ -9,7 +9,26 @@
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, ...) {
struct error_s *error = (struct error_s *)err;
@ -127,55 +146,7 @@ bool validate_colors(void *params, void *err) {
return true;
}
bool validate_config(char supportedInput[255], 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;
}
bool validate_config(struct config_params *p, struct error_s *error) {
// validate: output method
p->om = 0;
if (strcmp(outputMethod, "ncurses") == 0) {
@ -370,8 +341,8 @@ bool load_colors(struct config_params *p, dictionary *ini, void *err) {
return true;
}
bool load_config(char configPath[255], char supportedInput[255], struct config_params *p,
bool colorsOnly, struct error_s *error) {
bool load_config(char configPath[255], struct config_params *p, bool colorsOnly,
struct error_s *error) {
FILE *fp;
// 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);
}
// 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
outputMethod = (char *)iniparser_getstring(ini, "output:method", "ncurses");
#endif
@ -512,42 +465,66 @@ bool load_config(char configPath[255], char supportedInput[255], struct config_p
free(p->audio_source);
// config: input
p->im = 0;
if (strcmp(inputMethod, "alsa") == 0) {
p->im = 1;
p->audio_source = strdup(iniparser_getstring(ini, "input:source", "hw:Loopback,1"));
char *input_method_name;
for (int i = INPUT_MAX - 1; i >= 0; i--) {
if (has_input_method[i]) {
input_method_name =
(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->fifoSample = iniparser_getint(ini, "input:sample_rate", 44100);
}
if (strcmp(inputMethod, "pulse") == 0) {
p->im = 3;
break;
#ifdef PULSE
case INPUT_PULSE:
p->audio_source = strdup(iniparser_getstring(ini, "input:source", "auto"));
}
break;
#endif
#ifdef SNDIO
if (strcmp(inputMethod, "sndio") == 0) {
p->im = 4;
case INPUT_SNDIO:
p->audio_source = strdup(iniparser_getstring(ini, "input:source", SIO_DEVANY));
}
break;
#endif
#ifdef SHMEM
if (strcmp(inputMethod, "shmem") == 0) {
p->im = 5;
case INPUT_SHMEM:
p->audio_source =
strdup(iniparser_getstring(ini, "input:source", "/squeezelite-00:00:00:00:00:00"));
}
break;
#endif
#ifdef PORTAUDIO
if (strcmp(inputMethod, "portaudio") == 0) {
p->im = 6;
case INPUT_PORTAUDIO:
p->audio_source = strdup(iniparser_getstring(ini, "input:source", "auto"));
}
break;
#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);
return result;
}

@ -2,9 +2,52 @@
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#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 {
char *color, *bcolor, *raw_target, *audio_source,
/**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;
unsigned int lowcf, highcf;
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,
FFTbufferSize, fifoSample;
};
@ -22,5 +66,5 @@ struct error_s {
int length;
};
bool load_config(char configPath[255], char supportedInput[255], struct config_params *p,
bool colorsOnly, struct error_s *error);
bool load_config(char configPath[255], struct config_params *p, bool colorsOnly,
struct error_s *error);

Loading…
Cancel
Save