extmidi.cpp

00001 /* $Id: extmidi.cpp 11777 2008-01-07 14:23:25Z rubidium $ */
00002 
00003 #ifndef __MORPHOS__
00004 #include "../stdafx.h"
00005 #include "../openttd.h"
00006 #include "../sound_func.h"
00007 #include "../variables.h"
00008 #include "../debug.h"
00009 #include "extmidi.h"
00010 #include <fcntl.h>
00011 #include <sys/types.h>
00012 #include <sys/wait.h>
00013 #include <unistd.h>
00014 #include <signal.h>
00015 #include <sys/stat.h>
00016 #include <errno.h>
00017 
00018 static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi;
00019 
00020 const char* MusicDriver_ExtMidi::Start(const char* const * parm)
00021 {
00022   this->song[0] = '\0';
00023   this->pid = -1;
00024   return NULL;
00025 }
00026 
00027 void MusicDriver_ExtMidi::Stop()
00028 {
00029   this->song[0] = '\0';
00030   this->DoStop();
00031 }
00032 
00033 void MusicDriver_ExtMidi::PlaySong(const char* filename)
00034 {
00035   ttd_strlcpy(this->song, filename, lengthof(this->song));
00036   this->DoStop();
00037 }
00038 
00039 void MusicDriver_ExtMidi::StopSong()
00040 {
00041   this->song[0] = '\0';
00042   this->DoStop();
00043 }
00044 
00045 bool MusicDriver_ExtMidi::IsSongPlaying()
00046 {
00047   if (this->pid != -1 && waitpid(this->pid, NULL, WNOHANG) == this->pid)
00048     this->pid = -1;
00049   if (this->pid == -1 && this->song[0] != '\0') this->DoPlay();
00050   return this->pid != -1;
00051 }
00052 
00053 void MusicDriver_ExtMidi::SetVolume(byte vol)
00054 {
00055   DEBUG(driver, 1, "extmidi: set volume not implemented");
00056 }
00057 
00058 void MusicDriver_ExtMidi::DoPlay()
00059 {
00060   this->pid = fork();
00061   switch (this->pid) {
00062     case 0: {
00063       int d;
00064 
00065       close(0);
00066       d = open("/dev/null", O_RDONLY);
00067       if (d != -1 && dup2(d, 1) != -1 && dup2(d, 2) != -1) {
00068         #if defined(MIDI_ARG)
00069           execlp(msf.extmidi, "extmidi", MIDI_ARG, this->song, (char*)0);
00070         #else
00071           execlp(msf.extmidi, "extmidi", this->song, (char*)0);
00072         #endif
00073       }
00074       _exit(1);
00075     }
00076 
00077     case -1:
00078       DEBUG(driver, 0, "extmidi: couldn't fork: %s", strerror(errno));
00079       /* FALLTHROUGH */
00080 
00081     default:
00082       this->song[0] = '\0';
00083       break;
00084   }
00085 }
00086 
00087 void MusicDriver_ExtMidi::DoStop()
00088 {
00089   if (this->pid != -1) kill(this->pid, SIGTERM);
00090 }
00091 
00092 #endif /* __MORPHOS__ */

Generated on Mon Sep 22 20:34:16 2008 for openttd by  doxygen 1.5.6