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