music_cmd.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 20 Dec 2001 00:01:30 +0000
changeset 155 53f259dae79d
parent 140 efa15d2a5403
child 241 503416fca921
permissions -rw-r--r--
*** empty log message ***
slouken@0
     1
/*
slouken@138
     2
    SDL_mixer:  An audio mixer library based on the SDL library
slouken@138
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@138
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@140
    23
/* $Id$ */
slouken@138
    24
slouken@0
    25
/* This file supports an external command for playing music */
slouken@0
    26
slouken@0
    27
#ifdef unix		/* This is a UNIX-specific hack */
slouken@0
    28
slouken@0
    29
#include <sys/types.h>
slouken@0
    30
#include <sys/wait.h>
slouken@0
    31
#include <stdio.h>
slouken@0
    32
#include <stdlib.h>
slouken@0
    33
#include <unistd.h>
slouken@0
    34
#include <string.h>
slouken@0
    35
#include <signal.h>
slouken@155
    36
#include <ctype.h>
slouken@0
    37
slouken@34
    38
#include "SDL_mixer.h"
slouken@0
    39
#include "music_cmd.h"
slouken@0
    40
slouken@0
    41
/* Unimplemented */
slouken@0
    42
void MusicCMD_SetVolume(int volume)
slouken@0
    43
{
slouken@0
    44
	Mix_SetError("No way to modify external player volume");
slouken@0
    45
}
slouken@0
    46
slouken@0
    47
/* Load a music stream from the given file */
slouken@0
    48
MusicCMD *MusicCMD_LoadSong(const char *cmd, const char *file)
slouken@0
    49
{
slouken@0
    50
	MusicCMD *music;
slouken@0
    51
slouken@0
    52
	/* Allocate and fill the music structure */
slouken@0
    53
	music = (MusicCMD *)malloc(sizeof *music);
slouken@0
    54
	if ( music == NULL ) {
slouken@0
    55
		Mix_SetError("Out of memory");
slouken@0
    56
		return(NULL);
slouken@0
    57
	}
slouken@0
    58
	strncpy(music->file, file, (sizeof music->file)-1);
slouken@0
    59
	music->file[(sizeof music->file)-1] = '\0';
slouken@0
    60
	strncpy(music->cmd, cmd, (sizeof music->cmd)-1);
slouken@0
    61
	music->cmd[(sizeof music->cmd)-1] = '\0';
slouken@0
    62
	music->pid = 0;
slouken@0
    63
slouken@0
    64
	/* We're done */
slouken@0
    65
	return(music);
slouken@0
    66
}
slouken@0
    67
slouken@0
    68
/* Parse a command line buffer into arguments */
slouken@0
    69
static int ParseCommandLine(char *cmdline, char **argv)
slouken@0
    70
{
slouken@0
    71
	char *bufp;
slouken@0
    72
	int argc;
slouken@0
    73
slouken@0
    74
	argc = 0;
slouken@0
    75
	for ( bufp = cmdline; *bufp; ) {
slouken@0
    76
		/* Skip leading whitespace */
slouken@0
    77
		while ( isspace(*bufp) ) {
slouken@0
    78
			++bufp;
slouken@0
    79
		}
slouken@0
    80
		/* Skip over argument */
slouken@0
    81
		if ( *bufp == '"' ) {
slouken@0
    82
			++bufp;
slouken@0
    83
			if ( *bufp ) {
slouken@0
    84
				if ( argv ) {
slouken@0
    85
					argv[argc] = bufp;
slouken@0
    86
				}
slouken@0
    87
				++argc;
slouken@0
    88
			}
slouken@0
    89
			/* Skip over word */
slouken@0
    90
			while ( *bufp && (*bufp != '"') ) {
slouken@0
    91
				++bufp;
slouken@0
    92
			}
slouken@0
    93
		} else {
slouken@0
    94
			if ( *bufp ) {
slouken@0
    95
				if ( argv ) {
slouken@0
    96
					argv[argc] = bufp;
slouken@0
    97
				}
slouken@0
    98
				++argc;
slouken@0
    99
			}
slouken@0
   100
			/* Skip over word */
slouken@0
   101
			while ( *bufp && ! isspace(*bufp) ) {
slouken@0
   102
				++bufp;
slouken@0
   103
			}
slouken@0
   104
		}
slouken@0
   105
		if ( *bufp ) {
slouken@0
   106
			if ( argv ) {
slouken@0
   107
				*bufp = '\0';
slouken@0
   108
			}
slouken@0
   109
			++bufp;
slouken@0
   110
		}
slouken@0
   111
	}
slouken@0
   112
	if ( argv ) {
slouken@0
   113
		argv[argc] = NULL;
slouken@0
   114
	}
slouken@0
   115
	return(argc);
slouken@0
   116
}
slouken@0
   117
slouken@0
   118
static char **parse_args(char *command, char *last_arg)
slouken@0
   119
{
slouken@0
   120
	int argc;
slouken@0
   121
	char **argv;
slouken@0
   122
slouken@0
   123
	/* Parse the command line */
slouken@0
   124
	argc = ParseCommandLine(command, NULL);
slouken@0
   125
	if ( last_arg ) {
slouken@0
   126
		++argc;
slouken@0
   127
	}
slouken@0
   128
	argv = (char **)malloc((argc+1)*(sizeof *argv));
slouken@0
   129
	if ( argv == NULL ) {
slouken@0
   130
		return(NULL);
slouken@0
   131
	}
slouken@0
   132
	argc = ParseCommandLine(command, argv);
slouken@0
   133
slouken@0
   134
	/* Add last command line argument */
slouken@0
   135
	if ( last_arg ) {
slouken@0
   136
		argv[argc++] = last_arg;
slouken@0
   137
	}
slouken@0
   138
	argv[argc] = NULL;
slouken@0
   139
slouken@0
   140
	/* We're ready! */
slouken@0
   141
	return(argv);
slouken@0
   142
}
slouken@0
   143
slouken@0
   144
/* Start playback of a given music stream */
slouken@0
   145
void MusicCMD_Start(MusicCMD *music)
slouken@0
   146
{
slouken@0
   147
	music->pid = fork();
slouken@0
   148
	switch(music->pid) {
slouken@0
   149
	    /* Failed fork() system call */
slouken@0
   150
	    case -1:
slouken@0
   151
		Mix_SetError("fork() failed");
slouken@0
   152
		return;
slouken@0
   153
slouken@0
   154
	    /* Child process - executes here */
slouken@0
   155
	    case 0: {
slouken@0
   156
		    char command[PATH_MAX];
slouken@0
   157
		    char **argv;
slouken@0
   158
slouken@0
   159
		    /* Execute the command */
slouken@0
   160
		    strcpy(command, music->cmd);
slouken@0
   161
		    argv = parse_args(command, music->file);
slouken@0
   162
		    if ( argv != NULL ) {
slouken@0
   163
			execvp(argv[0], argv);
slouken@0
   164
		    }
slouken@0
   165
slouken@0
   166
		    /* exec() failed */
slouken@0
   167
		    perror(argv[0]);
slouken@0
   168
		    _exit(-1);
slouken@0
   169
		}
slouken@0
   170
		break;
slouken@0
   171
slouken@0
   172
	    /* Parent process - executes here */
slouken@0
   173
	    default:
slouken@0
   174
		break;
slouken@0
   175
	}
slouken@0
   176
	return;
slouken@0
   177
}
slouken@0
   178
slouken@0
   179
/* Stop playback of a stream previously started with MusicCMD_Start() */
slouken@0
   180
void MusicCMD_Stop(MusicCMD *music)
slouken@0
   181
{
slouken@0
   182
	int status;
slouken@0
   183
slouken@0
   184
	if ( music->pid > 0 ) {
slouken@0
   185
		while ( kill(music->pid, 0) == 0 ) {
slouken@0
   186
			kill(music->pid, SIGTERM);
slouken@0
   187
			sleep(1);
slouken@0
   188
			waitpid(music->pid, &status, WNOHANG);
slouken@0
   189
		}
slouken@0
   190
		music->pid = 0;
slouken@0
   191
	}
slouken@0
   192
}
slouken@0
   193
slouken@0
   194
/* Pause playback of a given music stream */
slouken@0
   195
void MusicCMD_Pause(MusicCMD *music)
slouken@0
   196
{
slouken@0
   197
	if ( music->pid > 0 ) {
slouken@0
   198
		kill(music->pid, SIGSTOP);
slouken@0
   199
	}
slouken@0
   200
}
slouken@0
   201
slouken@0
   202
/* Resume playback of a given music stream */
slouken@0
   203
void MusicCMD_Resume(MusicCMD *music)
slouken@0
   204
{
slouken@0
   205
	if ( music->pid > 0 ) {
slouken@0
   206
		kill(music->pid, SIGCONT);
slouken@0
   207
	}
slouken@0
   208
}
slouken@0
   209
slouken@0
   210
/* Close the given music stream */
slouken@0
   211
void MusicCMD_FreeSong(MusicCMD *music)
slouken@0
   212
{
slouken@0
   213
	free(music);
slouken@0
   214
}
slouken@0
   215
slouken@0
   216
/* Return non-zero if a stream is currently playing */
slouken@0
   217
int MusicCMD_Active(MusicCMD *music)
slouken@0
   218
{
slouken@0
   219
	int status;
slouken@0
   220
	int active;
slouken@0
   221
slouken@0
   222
	active = 0;
slouken@0
   223
	if ( music->pid > 0 ) {
slouken@0
   224
		waitpid(music->pid, &status, WNOHANG);
slouken@0
   225
		if ( kill(music->pid, 0) == 0 ) {
slouken@0
   226
			active = 1;
slouken@0
   227
		}
slouken@0
   228
	}
slouken@0
   229
	return(active);
slouken@0
   230
}
slouken@0
   231
slouken@0
   232
#endif /* unix */