# extmidi.cpp

Go to the documentation of this file.

    00001 /* $Id: extmidi.cpp 19175 2010-02-20 21:10:58Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    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 #define EXTERNAL_PLAYER "timidity"
    00030 #endif
    00031
    00032 static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi;
    00033
    00034 const char *MusicDriver_ExtMidi::Start(const char * const * parm)
    00035 {
    00036   if (strcmp(_video_driver->GetName(), "allegro") == 0 ||
    00037       strcmp(_sound_driver->GetName(), "allegro") == 0) {
    00038     return "the extmidi driver does not work when Allegro is loaded.";
    00039   }
    00040
    00041   const char *command = GetDriverParam(parm, "cmd");
    00042   if (StrEmpty(command)) command = EXTERNAL_PLAYER;
    00043
    00044   this->command = strdup(command);
    00045   this->song[0] = '\0';
    00046   this->pid = -1;
    00047   return NULL;
    00048 }
    00049
    00050 void MusicDriver_ExtMidi::Stop()
    00051 {
    00052   free(command);
    00053   this->song[0] = '\0';
    00054   this->DoStop();
    00055 }
    00056
    00057 void MusicDriver_ExtMidi::PlaySong(const char *filename)
    00058 {
    00059   strecpy(this->song, filename, lastof(this->song));
    00060   this->DoStop();
    00061 }
    00062
    00063 void MusicDriver_ExtMidi::StopSong()
    00064 {
    00065   this->song[0] = '\0';
    00066   this->DoStop();
    00067 }
    00068
    00069 bool MusicDriver_ExtMidi::IsSongPlaying()
    00070 {
    00071   if (this->pid != -1 && waitpid(this->pid, NULL, WNOHANG) == this->pid) {
    00072     this->pid = -1;
    00073   }
    00074   if (this->pid == -1 && this->song[0] != '\0') this->DoPlay();
    00075   return this->pid != -1;
    00076 }
    00077
    00078 void MusicDriver_ExtMidi::SetVolume(byte vol)
    00079 {
    00080   DEBUG(driver, 1, "extmidi: set volume not implemented");
    00081 }
    00082
    00083 void MusicDriver_ExtMidi::DoPlay()
    00084 {
    00085   this->pid = fork();
    00086   switch (this->pid) {
    00087     case 0: {
    00088       close(0);
    00089       int d = open("/dev/null", O_RDONLY);
    00090       if (d != -1 && dup2(d, 1) != -1 && dup2(d, 2) != -1) {
    00091         #if defined(MIDI_ARG)
    00092           execlp(this->command, "extmidi", MIDI_ARG, this->song, (char*)0);
    00093         #else
    00094           execlp(this->command, "extmidi", this->song, (char*)0);
    00095         #endif
    00096       }
    00097       _exit(1);
    00098     }
    00099
    00100     case -1:
    00101       DEBUG(driver, 0, "extmidi: couldn't fork: %s", strerror(errno));
    00102       /* FALLTHROUGH */
    00103
    00104     default:
    00105       this->song[0] = '\0';
    00106       break;
    00107   }
    00108 }
    00109
    00110 void MusicDriver_ExtMidi::DoStop()
    00111 {
    00112   if (this->pid <= 0) return;
    00113
    00114   /* First try to gracefully stop for about five seconds;
    00115    * 5 seconds = 5000 milliseconds, 10 ms per cycle => 500 cycles. */
    00116   for (int i = 0; i < 500; i++) {
    00117     kill(this->pid, SIGTERM);
    00118     if (waitpid(this->pid, NULL, WNOHANG) == this->pid) {
    00119       /* It has shut down, so we are done */
    00120       this->pid = -1;
    00121       return;
    00122     }
    00123     /* Wait 10 milliseconds. */
    00124     CSleep(10);
    00125   }
    00126
    00127   DEBUG(driver, 0, "extmidi: gracefully stopping failed, trying the hard way");
    00128   /* Gracefully stopping failed. Do it the hard way
    00129    * and wait till the process finally died. */
    00130   kill(this->pid, SIGKILL);
    00131   waitpid(this->pid, NULL, 0);
    00132   this->pid = -1;
    00133 }
    00134
    00135 #endif /* __MORPHOS__ */