music_cmd.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. SDL_mixer: An audio mixer library based on the SDL library
  3. Copyright (C) 1997-2013 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_config.h"
  19. /* This file supports an external command for playing music */
  20. #ifdef CMD_MUSIC
  21. #include <sys/types.h>
  22. #include <sys/wait.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <signal.h>
  28. #include <ctype.h>
  29. #include "SDL_mixer.h"
  30. #include "music_cmd.h"
  31. /* Unimplemented */
  32. void MusicCMD_SetVolume(int volume)
  33. {
  34. Mix_SetError("No way to modify external player volume");
  35. }
  36. /* Load a music stream from the given file */
  37. MusicCMD *MusicCMD_LoadSong(const char *cmd, const char *file)
  38. {
  39. MusicCMD *music;
  40. /* Allocate and fill the music structure */
  41. music = (MusicCMD *)SDL_malloc(sizeof *music);
  42. if ( music == NULL ) {
  43. Mix_SetError("Out of memory");
  44. return(NULL);
  45. }
  46. music->file = SDL_strdup(file);
  47. music->cmd = SDL_strdup(cmd);
  48. music->pid = 0;
  49. /* We're done */
  50. return(music);
  51. }
  52. /* Parse a command line buffer into arguments */
  53. static int ParseCommandLine(char *cmdline, char **argv)
  54. {
  55. char *bufp;
  56. int argc;
  57. argc = 0;
  58. for ( bufp = cmdline; *bufp; ) {
  59. /* Skip leading whitespace */
  60. while ( isspace(*bufp) ) {
  61. ++bufp;
  62. }
  63. /* Skip over argument */
  64. if ( *bufp == '"' ) {
  65. ++bufp;
  66. if ( *bufp ) {
  67. if ( argv ) {
  68. argv[argc] = bufp;
  69. }
  70. ++argc;
  71. }
  72. /* Skip over word */
  73. while ( *bufp && (*bufp != '"') ) {
  74. ++bufp;
  75. }
  76. } else {
  77. if ( *bufp ) {
  78. if ( argv ) {
  79. argv[argc] = bufp;
  80. }
  81. ++argc;
  82. }
  83. /* Skip over word */
  84. while ( *bufp && ! isspace(*bufp) ) {
  85. ++bufp;
  86. }
  87. }
  88. if ( *bufp ) {
  89. if ( argv ) {
  90. *bufp = '\0';
  91. }
  92. ++bufp;
  93. }
  94. }
  95. if ( argv ) {
  96. argv[argc] = NULL;
  97. }
  98. return(argc);
  99. }
  100. static char **parse_args(char *command, char *last_arg)
  101. {
  102. int argc;
  103. char **argv;
  104. /* Parse the command line */
  105. argc = ParseCommandLine(command, NULL);
  106. if ( last_arg ) {
  107. ++argc;
  108. }
  109. argv = (char **)SDL_malloc((argc+1)*(sizeof *argv));
  110. if ( argv == NULL ) {
  111. return(NULL);
  112. }
  113. argc = ParseCommandLine(command, argv);
  114. /* Add last command line argument */
  115. if ( last_arg ) {
  116. argv[argc++] = last_arg;
  117. }
  118. argv[argc] = NULL;
  119. /* We're ready! */
  120. return(argv);
  121. }
  122. /* Start playback of a given music stream */
  123. void MusicCMD_Start(MusicCMD *music)
  124. {
  125. #ifdef HAVE_FORK
  126. music->pid = fork();
  127. #else
  128. music->pid = vfork();
  129. #endif
  130. switch(music->pid) {
  131. /* Failed fork() system call */
  132. case -1:
  133. Mix_SetError("fork() failed");
  134. return;
  135. /* Child process - executes here */
  136. case 0: {
  137. char *command;
  138. char **argv;
  139. /* Unblock signals in case we're called from a thread */
  140. {
  141. sigset_t mask;
  142. sigemptyset(&mask);
  143. sigprocmask(SIG_SETMASK, &mask, NULL);
  144. }
  145. /* Execute the command */
  146. command = SDL_strdup(music->cmd);
  147. argv = parse_args(command, music->file);
  148. if ( argv != NULL ) {
  149. execvp(argv[0], argv);
  150. }
  151. SDL_free(command);
  152. /* exec() failed */
  153. perror(argv[0]);
  154. _exit(-1);
  155. }
  156. break;
  157. /* Parent process - executes here */
  158. default:
  159. break;
  160. }
  161. return;
  162. }
  163. /* Stop playback of a stream previously started with MusicCMD_Start() */
  164. void MusicCMD_Stop(MusicCMD *music)
  165. {
  166. int status;
  167. if ( music->pid > 0 ) {
  168. while ( kill(music->pid, 0) == 0 ) {
  169. kill(music->pid, SIGTERM);
  170. sleep(1);
  171. waitpid(music->pid, &status, WNOHANG);
  172. }
  173. music->pid = 0;
  174. }
  175. }
  176. /* Pause playback of a given music stream */
  177. void MusicCMD_Pause(MusicCMD *music)
  178. {
  179. if ( music->pid > 0 ) {
  180. kill(music->pid, SIGSTOP);
  181. }
  182. }
  183. /* Resume playback of a given music stream */
  184. void MusicCMD_Resume(MusicCMD *music)
  185. {
  186. if ( music->pid > 0 ) {
  187. kill(music->pid, SIGCONT);
  188. }
  189. }
  190. /* Close the given music stream */
  191. void MusicCMD_FreeSong(MusicCMD *music)
  192. {
  193. SDL_free(music->file);
  194. SDL_free(music->cmd);
  195. SDL_free(music);
  196. }
  197. /* Return non-zero if a stream is currently playing */
  198. int MusicCMD_Active(MusicCMD *music)
  199. {
  200. int status;
  201. int active;
  202. active = 0;
  203. if ( music->pid > 0 ) {
  204. waitpid(music->pid, &status, WNOHANG);
  205. if ( kill(music->pid, 0) == 0 ) {
  206. active = 1;
  207. }
  208. }
  209. return(active);
  210. }
  211. #endif /* CMD_MUSIC */