# extmidi.cpp

Go to the documentation of this file.

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