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 "../sound/sound_driver.hpp"
00016 #include "../video/video_driver.hpp"
00017 #include "extmidi.h"
00018 #include <fcntl.h>
00019 #include <sys/types.h>
00020 #include <sys/wait.h>
00021 #include <unistd.h>
00022 #include <signal.h>
00023 #include <sys/stat.h>
00024 #include <errno.h>
00025
00026 #ifndef EXTERNAL_PLAYER
00027 #define EXTERNAL_PLAYER "timidity"
00028 #endif
00029
00030 static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi;
00031
00032 const char *MusicDriver_ExtMidi::Start(const char * const * parm)
00033 {
00034 if (strcmp(_video_driver->GetName(), "allegro") == 0 ||
00035 strcmp(_sound_driver->GetName(), "allegro") == 0) {
00036 return "the extmidi driver does not work when Allegro is loaded.";
00037 }
00038
00039 const char *command = GetDriverParam(parm, "cmd");
00040 if (StrEmpty(command)) command = EXTERNAL_PLAYER;
00041
00042 this->command = strdup(command);
00043 this->song[0] = '\0';
00044 this->pid = -1;
00045 return NULL;
00046 }
00047
00048 void MusicDriver_ExtMidi::Stop()
00049 {
00050 free(command);
00051 this->song[0] = '\0';
00052 this->DoStop();
00053 }
00054
00055 void MusicDriver_ExtMidi::PlaySong(const char *filename)
00056 {
00057 strecpy(this->song, filename, lastof(this->song));
00058 this->DoStop();
00059 }
00060
00061 void MusicDriver_ExtMidi::StopSong()
00062 {
00063 this->song[0] = '\0';
00064 this->DoStop();
00065 }
00066
00067 bool MusicDriver_ExtMidi::IsSongPlaying()
00068 {
00069 if (this->pid != -1 && waitpid(this->pid, NULL, WNOHANG) == this->pid) {
00070 this->pid = -1;
00071 }
00072 if (this->pid == -1 && this->song[0] != '\0') this->DoPlay();
00073 return this->pid != -1;
00074 }
00075
00076 void MusicDriver_ExtMidi::SetVolume(byte vol)
00077 {
00078 DEBUG(driver, 1, "extmidi: set volume not implemented");
00079 }
00080
00081 void MusicDriver_ExtMidi::DoPlay()
00082 {
00083 this->pid = fork();
00084 switch (this->pid) {
00085 case 0: {
00086 close(0);
00087 int d = open("/dev/null", O_RDONLY);
00088 if (d != -1 && dup2(d, 1) != -1 && dup2(d, 2) != -1) {
00089 #if defined(MIDI_ARG)
00090 execlp(this->command, "extmidi", MIDI_ARG, this->song, (char*)0);
00091 #else
00092 execlp(this->command, "extmidi", this->song, (char*)0);
00093 #endif
00094 }
00095 _exit(1);
00096 }
00097
00098 case -1:
00099 DEBUG(driver, 0, "extmidi: couldn't fork: %s", strerror(errno));
00100
00101
00102 default:
00103 this->song[0] = '\0';
00104 break;
00105 }
00106 }
00107
00108 void MusicDriver_ExtMidi::DoStop()
00109 {
00110 if (this->pid != -1) kill(this->pid, SIGTERM);
00111 }
00112
00113 #endif