Code cleanup: formatting, added conditional error

This commit is contained in:
JC 2018-05-18 11:16:48 -07:00
parent c3e8255546
commit ae23d3c951

@ -1,5 +1,12 @@
/* teres1-audiocontrol.c - audio input/output manager
Should be run as root. On start, it reads an A64 specific
memory address and sets the mixer accordingly. Then the program
exits, unless a device is given as an argument
(normally, /dev/input/by-path/platform-sound.6-event).
In this case, the program runs forever, watching
the jack plug in/out events.
Copyright (C) 2017 Chris Boudacoff
This program is free software; you can redistribute it and/or modify
@ -27,25 +34,20 @@
unsigned long SUNXI_PIO_BASE = 0;
void select_mode(char mmsel,char mval) {
switch (mmsel) {
case 2: // headphones
// printf("Headphones: %d\n", mval);
if (mval == 0)
{
if (mval == 0) {
system("amixer -c 0 set \"External Speaker\" on > /dev/null");
system("amixer -c 0 set \"Headphone\" off > /dev/null");
}
else
{
else {
system("amixer -c 0 set \"External Speaker\" off > /dev/null");
system("amixer -c 0 set \"Headphone\" on > /dev/null");
}
break;
case 4: // mice
case 4: // mic
// printf("Microphone: %d\n", mval);
if (mval == 0)
{
if (mval == 0) {
system("amixer -c 0 sset \"RADC input Mixer MIC1 boost\" unmute > /dev/null" );
system("amixer -c 0 sset \"LADC input Mixer MIC1 boost\" unmute > /dev/null");
system("amixer -c 0 sset \"RADC input Mixer MIC2 boost\" mute > /dev/null");
@ -55,8 +57,7 @@ void select_mode(char mmsel,char mval){
system("amixer -c 0 sset \"AIF1 AD0L Mixer ADCL\" ADC > /dev/null");
system("amixer -c 0 sset \"AIF1 AD0R Mixer ADCR\" ADC > /dev/null");
}
else
{
else {
system("amixer -c 0 sset \"RADC input Mixer MIC2 boost\" unmute > /dev/null");
system("amixer -c 0 sset \"LADC input Mixer MIC2 boost\" unmute > /dev/null");
system("amixer -c 0 sset \"RADC input Mixer MIC1 boost\" mute > /dev/null");
@ -70,6 +71,7 @@ void select_mode(char mmsel,char mval){
}
}
const char *readLine(FILE *file) {
if (file == NULL) {
@ -110,8 +112,9 @@ const char *readLine(FILE *file) {
const char *constLine = line;
return constLine;
}
int main (int argc, char **argv)
{
int main (int argc, char **argv) {
const char *device = NULL;
const char *debug;
int fd;
@ -123,10 +126,11 @@ FILE *uenv;
*/
uenv=fopen("/boot/uEnv.txt","r");
if (uenv) {
while(1)
{
while(1) {
debug = readLine(uenv);
if (strlen(debug)==0) break;
if (strlen(debug)==0) {
break;
}
if (strcmp(debug,"debug=on")==0) {
// device is in debug mode!!!
select_mode(2,0);
@ -136,8 +140,6 @@ FILE *uenv;
}
}
off_t offset = SUNXI_HMIC_STS;
size_t len = 0x01;
size_t pagesize = sysconf(_SC_PAGE_SIZE);
@ -145,51 +147,41 @@ FILE *uenv;
off_t page_offset = offset - page_base;
fd = open("/dev/mem", O_SYNC);
if (fd == -1) {
perror("Cannot open /dev/mem");
return -1;
}
unsigned char *mem = mmap(NULL, page_offset + len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, page_base);
if (mem == MAP_FAILED) {
perror("Can't map memory");
return -1;
}
if ((int)mem[page_offset] && 0x08) //headset?
if ((int)mem[page_offset] && 0x08) { //headset?
select_mode(4,1);
else
}
else {
select_mode(4,0);
if ((int)mem[page_offset] && 0x04) //headphone?
}
if ((int)mem[page_offset] && 0x04) { //headphone?
select_mode(2,1);
else
}
else {
select_mode(2,0);
}
close(fd);
if (!argv[1])
exit(0);
device = argv[1];
for (;;)
{
if((fd = open(device, O_RDONLY)) == -1)
{
for (;;) {
if((fd = open(device, O_RDONLY)) == -1) {
perror("Could not open input device");
exit(255);
}
while(read(fd, &ie, sizeof(struct input_event))>0)
{
while(read(fd, &ie, sizeof(struct input_event)) > 0) {
// printf("type %d\tcode %d\tvalue %d\n", ie.type, ie.code, ie.value);
switch (ie.type)
{
case 5: // pluged
if (ie.type == 5) {
select_mode(ie.code,ie.value);
break;
}
}