Added config files

This commit is contained in:
2017-12-13 19:11:31 +01:00
parent eac1c224c1
commit edde2c0aec
6 changed files with 94 additions and 7 deletions

53
config.c Normal file
View File

@@ -0,0 +1,53 @@
#include <libconfig.h>
#include "config.h"
#include <string.h>
config_t cfg;
const config_setting_t *setting;
void cfginit(void) {
config_init(&cfg);
if (!config_read_file(&cfg, "csoundbox.cfg")) {
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
//return(EXIT_FAILURE);
}
}
void cfgdestroy(void) {
//free ram
config_destroy(&cfg);
}
void cfgreinit(void) {
cfgdestroy();
cfginit();
}
const char *lookupSounds(char input) {
setting = config_lookup(&cfg, "soundmap.sounds");
const char *temp;
if(setting != NULL) {
int count = config_setting_length(setting);
int i;
for(i = 0; i < count; ++i) {
config_setting_t *sound = config_setting_get_elem(setting, i);
/* Only output the record if all of the expected fields are present. */
const char *key;
if (config_setting_lookup_string(sound, "key", &key))
if (key[0] == input) {
config_setting_lookup_string(sound, "path", &temp);
return temp;
}
}
}
return "false";
}

12
config.h Normal file
View File

@@ -0,0 +1,12 @@
typedef struct {
char *beep;
char *path[50];
char *key[50];
} sounds;
void cfginit(void);
void cfgdestroy(void);
void cfgreinit(void);
const char *lookupSounds(char input);

View File

@@ -3,18 +3,32 @@
#include <stdlib.h>
#include <stdio.h>
#include "csoundbox.h"
#include "config.h"
#include <string.h>
#include <ncurses.h>
#define BITS 8
int main(int argc, char *argv[])
{
cfginit();
playSound(lookupSounds('a'));
char input;
initscr();
nonl(); //no newline
keypad(stdscr, FALSE); //Disable the F1-12 keypad
curs_set(0); //Disable the cursor
while ((input = getch()) != 13) {
playSound(lookupSounds(input));
}
endwin();
cfgdestroy();
return 0;
}
void playSound(char *path) {
mpg123_handle *mh;
void playSound(const char *path) {
mpg123_handle *mh;
unsigned char *buffer;
size_t buffer_size;
size_t done;

8
csoundbox.cfg Normal file
View File

@@ -0,0 +1,8 @@
soundmap = {
sounds = ( {path = "ballsofsteel.mp3";
key = "a"},
{path = "neck.mp3";
key = "b"}
);
}

View File

@@ -1,2 +1,2 @@
void playSound(char *path);
void playSound(const char *path);

View File

@@ -5,9 +5,9 @@ VERSION = 1.0
CC = cc
CFLAGS = -Wall -O3 -DVERSION=\"$(VERSION)\"
#LDFLAGS = -lm -lpthread `gtk-config --cflags` `gtk-config --libs` -lgthread
LDFLAGS = -lmpg123 -lao
LDFLAGS = -lmpg123 -lao -lconfig -lncurses
OBJ = csoundbox.o
OBJ = csoundbox.o config.o
all: $(OBJ)
$(CC) $(CFLAGS) -o csoundbox $(OBJ) $(LDFLAGS)