I'm writing a media server for my raspberry pi. I was able to create a program which uses popen to control omxplayer via a remote control.
I would now like to control mpg123 for music. I took the same code that worked in the omxplayer program with popen and applied it to mpg123, but it isn't working. It starts up, but won't acknowledge any input sent to it. I don't know why one would work and the other wouldn't.
Here is my code:
void play_music (char *list, int random)
{
FILE *pp;
char c;
char command[501];
struct stat buf;
if(access(list, R_OK) == -1)
{
fprintf(stderr, "%s: play_music: access failed (%s) (%s)\n", program_name, strerror(errno), list);
exit(EXIT_FAILURE);
}
if(stat(list, &buf) == -1)
{
fprintf(stderr, "%s: play_music: stat failed (%s) (%s)\n", program_name, strerror(errno), list);
exit(EXIT_FAILURE);
}
strcpy(command, "/usr/bin/mpg123 -C ");
if(random == 1)
strcat(command, "-z ");
if(S_ISREG(buf.st_mode) == 1)
{
strcat(command, "-@ ");
strcat(command, list);
}
else if(S_ISDIR(buf.st_mode) == 1)
{
strcat(command, list);
if(list[strlen(list) - 1] != '*')
{
if(list[strlen(list) - 1] != '/')
strcat(command, "/");
strcat(command, "*");
}
}
else
{
fprintf(stderr, "%s: play_music: stat reported unknown (%s)\n", program_name, list);
exit(EXIT_FAILURE);
}
strcat(command, " > /dev/null 2>&1");
if((pp = popen(command, "w")) == NULL)
{
fprintf(stderr, "%s: play_music popen failed (%s)\n", program_name, strerror(errno));
exit(EXIT_FAILURE);
}
while((c = get_code()))
{
if(system("pidof mpg123 > /dev/null") != 0)
return;
switch(c)
{
case 31:
fputc('f', pp);
break;
case 32:
fputc('d', pp);
break;
case 33:
fputc('s', pp);
break;
case 34:
fputc('q', pp);
}
if(fflush(pp) == EOF)
{
fprintf(stderr, "%s: play_music fflush failed (%s)\n", program_name, strerror(errno));
exit(EXIT_FAILURE);
}
}
}
I've been trying to figure this out for far too long, can someone please help!
Notes:
get_code() is a working function that returns an int based on which remote control button is pressed.
The variable 'list' is either a directory path or a playlist filename.
The variable 'random' is an int flag (1 for random play).