music_cmd.c
changeset 0 4ce2db4db959
child 34 25e00090b15c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/music_cmd.c	Thu Oct 21 18:02:08 1999 +0000
     1.3 @@ -0,0 +1,231 @@
     1.4 +/*
     1.5 +    MIXERLIB:  An audio mixer library based on the SDL library
     1.6 +    Copyright (C) 1997-1999  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    5635-34 Springhouse Dr.
    1.24 +    Pleasanton, CA 94588 (USA)
    1.25 +    slouken@devolution.com
    1.26 +*/
    1.27 +
    1.28 +/* This file supports an external command for playing music */
    1.29 +
    1.30 +#ifdef unix		/* This is a UNIX-specific hack */
    1.31 +
    1.32 +#include <sys/types.h>
    1.33 +#include <sys/wait.h>
    1.34 +#include <stdio.h>
    1.35 +#include <stdlib.h>
    1.36 +#include <unistd.h>
    1.37 +#include <string.h>
    1.38 +#include <signal.h>
    1.39 +
    1.40 +#include "mixer.h"
    1.41 +#include "music_cmd.h"
    1.42 +
    1.43 +/* Unimplemented */
    1.44 +void MusicCMD_SetVolume(int volume)
    1.45 +{
    1.46 +	Mix_SetError("No way to modify external player volume");
    1.47 +}
    1.48 +
    1.49 +/* Load a music stream from the given file */
    1.50 +MusicCMD *MusicCMD_LoadSong(const char *cmd, const char *file)
    1.51 +{
    1.52 +	MusicCMD *music;
    1.53 +
    1.54 +	/* Allocate and fill the music structure */
    1.55 +	music = (MusicCMD *)malloc(sizeof *music);
    1.56 +	if ( music == NULL ) {
    1.57 +		Mix_SetError("Out of memory");
    1.58 +		return(NULL);
    1.59 +	}
    1.60 +	strncpy(music->file, file, (sizeof music->file)-1);
    1.61 +	music->file[(sizeof music->file)-1] = '\0';
    1.62 +	strncpy(music->cmd, cmd, (sizeof music->cmd)-1);
    1.63 +	music->cmd[(sizeof music->cmd)-1] = '\0';
    1.64 +	music->pid = 0;
    1.65 +
    1.66 +	/* We're done */
    1.67 +	return(music);
    1.68 +}
    1.69 +
    1.70 +/* Parse a command line buffer into arguments */
    1.71 +static int ParseCommandLine(char *cmdline, char **argv)
    1.72 +{
    1.73 +	char *bufp;
    1.74 +	int argc;
    1.75 +
    1.76 +	argc = 0;
    1.77 +	for ( bufp = cmdline; *bufp; ) {
    1.78 +		/* Skip leading whitespace */
    1.79 +		while ( isspace(*bufp) ) {
    1.80 +			++bufp;
    1.81 +		}
    1.82 +		/* Skip over argument */
    1.83 +		if ( *bufp == '"' ) {
    1.84 +			++bufp;
    1.85 +			if ( *bufp ) {
    1.86 +				if ( argv ) {
    1.87 +					argv[argc] = bufp;
    1.88 +				}
    1.89 +				++argc;
    1.90 +			}
    1.91 +			/* Skip over word */
    1.92 +			while ( *bufp && (*bufp != '"') ) {
    1.93 +				++bufp;
    1.94 +			}
    1.95 +		} else {
    1.96 +			if ( *bufp ) {
    1.97 +				if ( argv ) {
    1.98 +					argv[argc] = bufp;
    1.99 +				}
   1.100 +				++argc;
   1.101 +			}
   1.102 +			/* Skip over word */
   1.103 +			while ( *bufp && ! isspace(*bufp) ) {
   1.104 +				++bufp;
   1.105 +			}
   1.106 +		}
   1.107 +		if ( *bufp ) {
   1.108 +			if ( argv ) {
   1.109 +				*bufp = '\0';
   1.110 +			}
   1.111 +			++bufp;
   1.112 +		}
   1.113 +	}
   1.114 +	if ( argv ) {
   1.115 +		argv[argc] = NULL;
   1.116 +	}
   1.117 +	return(argc);
   1.118 +}
   1.119 +
   1.120 +static char **parse_args(char *command, char *last_arg)
   1.121 +{
   1.122 +	int argc;
   1.123 +	char **argv;
   1.124 +
   1.125 +	/* Parse the command line */
   1.126 +	argc = ParseCommandLine(command, NULL);
   1.127 +	if ( last_arg ) {
   1.128 +		++argc;
   1.129 +	}
   1.130 +	argv = (char **)malloc((argc+1)*(sizeof *argv));
   1.131 +	if ( argv == NULL ) {
   1.132 +		return(NULL);
   1.133 +	}
   1.134 +	argc = ParseCommandLine(command, argv);
   1.135 +
   1.136 +	/* Add last command line argument */
   1.137 +	if ( last_arg ) {
   1.138 +		argv[argc++] = last_arg;
   1.139 +	}
   1.140 +	argv[argc] = NULL;
   1.141 +
   1.142 +	/* We're ready! */
   1.143 +	return(argv);
   1.144 +}
   1.145 +
   1.146 +/* Start playback of a given music stream */
   1.147 +void MusicCMD_Start(MusicCMD *music)
   1.148 +{
   1.149 +	music->pid = fork();
   1.150 +	switch(music->pid) {
   1.151 +	    /* Failed fork() system call */
   1.152 +	    case -1:
   1.153 +		Mix_SetError("fork() failed");
   1.154 +		return;
   1.155 +
   1.156 +	    /* Child process - executes here */
   1.157 +	    case 0: {
   1.158 +		    char command[PATH_MAX];
   1.159 +		    char **argv;
   1.160 +
   1.161 +		    /* Execute the command */
   1.162 +		    strcpy(command, music->cmd);
   1.163 +		    argv = parse_args(command, music->file);
   1.164 +		    if ( argv != NULL ) {
   1.165 +			execvp(argv[0], argv);
   1.166 +		    }
   1.167 +
   1.168 +		    /* exec() failed */
   1.169 +		    perror(argv[0]);
   1.170 +		    _exit(-1);
   1.171 +		}
   1.172 +		break;
   1.173 +
   1.174 +	    /* Parent process - executes here */
   1.175 +	    default:
   1.176 +		break;
   1.177 +	}
   1.178 +	return;
   1.179 +}
   1.180 +
   1.181 +/* Stop playback of a stream previously started with MusicCMD_Start() */
   1.182 +void MusicCMD_Stop(MusicCMD *music)
   1.183 +{
   1.184 +	int status;
   1.185 +
   1.186 +	if ( music->pid > 0 ) {
   1.187 +		while ( kill(music->pid, 0) == 0 ) {
   1.188 +			kill(music->pid, SIGTERM);
   1.189 +			sleep(1);
   1.190 +			waitpid(music->pid, &status, WNOHANG);
   1.191 +		}
   1.192 +		music->pid = 0;
   1.193 +	}
   1.194 +}
   1.195 +
   1.196 +/* Pause playback of a given music stream */
   1.197 +void MusicCMD_Pause(MusicCMD *music)
   1.198 +{
   1.199 +	if ( music->pid > 0 ) {
   1.200 +		kill(music->pid, SIGSTOP);
   1.201 +	}
   1.202 +}
   1.203 +
   1.204 +/* Resume playback of a given music stream */
   1.205 +void MusicCMD_Resume(MusicCMD *music)
   1.206 +{
   1.207 +	if ( music->pid > 0 ) {
   1.208 +		kill(music->pid, SIGCONT);
   1.209 +	}
   1.210 +}
   1.211 +
   1.212 +/* Close the given music stream */
   1.213 +void MusicCMD_FreeSong(MusicCMD *music)
   1.214 +{
   1.215 +	free(music);
   1.216 +}
   1.217 +
   1.218 +/* Return non-zero if a stream is currently playing */
   1.219 +int MusicCMD_Active(MusicCMD *music)
   1.220 +{
   1.221 +	int status;
   1.222 +	int active;
   1.223 +
   1.224 +	active = 0;
   1.225 +	if ( music->pid > 0 ) {
   1.226 +		waitpid(music->pid, &status, WNOHANG);
   1.227 +		if ( kill(music->pid, 0) == 0 ) {
   1.228 +			active = 1;
   1.229 +		}
   1.230 +	}
   1.231 +	return(active);
   1.232 +}
   1.233 +
   1.234 +#endif /* unix */