extmidi.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifndef __MORPHOS__
00013 #include "../stdafx.h"
00014 #include "../debug.h"
00015 #include "../string_func.h"
00016 #include "../sound/sound_driver.hpp"
00017 #include "../video/video_driver.hpp"
00018 #include "../gfx_func.h"
00019 #include "extmidi.h"
00020 #include <fcntl.h>
00021 #include <sys/types.h>
00022 #include <sys/wait.h>
00023 #include <unistd.h>
00024 #include <signal.h>
00025 #include <sys/stat.h>
00026 #include <errno.h>
00027
00028 #ifndef EXTERNAL_PLAYER
00029
00030 #define EXTERNAL_PLAYER "timidity"
00031 #endif
00032
00034 static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi;
00035
00036 const char *MusicDriver_ExtMidi::Start(const char * const * parm)
00037 {
00038 if (strcmp(_video_driver->GetName(), "allegro") == 0 ||
00039 strcmp(_sound_driver->GetName(), "allegro") == 0) {
00040 return "the extmidi driver does not work when Allegro is loaded.";
00041 }
00042
00043 const char *command = GetDriverParam(parm, "cmd");
00044 if (StrEmpty(command)) command = EXTERNAL_PLAYER;
00045
00046 this->command = strdup(command);
00047 this->song[0] = '\0';
00048 this->pid = -1;
00049 return NULL;
00050 }
00051
00052 void MusicDriver_ExtMidi::Stop()
00053 {
00054 free(command);
00055 this->song[0] = '\0';
00056 this->DoStop();
00057 }
00058
00059 void MusicDriver_ExtMidi::PlaySong(const char *filename)
00060 {
00061 strecpy(this->song, filename, lastof(this->song));
00062 this->DoStop();
00063 }
00064
00065 void MusicDriver_ExtMidi::StopSong()
00066 {
00067 this->song[0] = '\0';
00068 this->DoStop();
00069 }
00070
00071 bool MusicDriver_ExtMidi::IsSongPlaying()
00072 {
00073 if (this->pid != -1 && waitpid(this->pid, NULL, WNOHANG) == this->pid) {
00074 this->pid = -1;
00075 }
00076 if (this->pid == -1 && this->song[0] != '\0') this->DoPlay();
00077 return this->pid != -1;
00078 }
00079
00080 void MusicDriver_ExtMidi::SetVolume(byte vol)
00081 {
00082 DEBUG(driver, 1, "extmidi: set volume not implemented");
00083 }
00084
00085 void MusicDriver_ExtMidi::DoPlay()
00086 {
00087 this->pid = fork();
00088 switch (this->pid) {
00089 case 0: {
00090 close(0);
00091 int d = open("/dev/null", O_RDONLY);
00092 if (d != -1 && dup2(d, 1) != -1 && dup2(d, 2) != -1) {
00093 #if defined(MIDI_ARG)
00094 execlp(this->command, "extmidi", MIDI_ARG, this->song, (char*)0);
00095 #else
00096 execlp(this->command, "extmidi", this->song, (char*)0);
00097 #endif
00098 }
00099 _exit(1);
00100 }
00101
00102 case -1:
00103 DEBUG(driver, 0, "extmidi: couldn't fork: %s", strerror(errno));
00104
00105
00106 default:
00107 this->song[0] = '\0';
00108 break;
00109 }
00110 }
00111
00112 void MusicDriver_ExtMidi::DoStop()
00113 {
00114 if (this->pid <= 0) return;
00115
00116
00117
00118 for (int i = 0; i < 500; i++) {
00119 kill(this->pid, SIGTERM);
00120 if (waitpid(this->pid, NULL, WNOHANG) == this->pid) {
00121
00122 this->pid = -1;
00123 return;
00124 }
00125
00126 CSleep(10);
00127 }
00128
00129 DEBUG(driver, 0, "extmidi: gracefully stopping failed, trying the hard way");
00130
00131
00132 kill(this->pid, SIGKILL);
00133 waitpid(this->pid, NULL, 0);
00134 this->pid = -1;
00135 }
00136
00137 #endif