2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file extmidi.cpp Playing music via an external player. */
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#ifndef __MORPHOS__
|
2005-07-25 07:16:10 +00:00
|
|
|
#include "../stdafx.h"
|
2006-05-02 19:09:49 +00:00
|
|
|
#include "../debug.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "../string_func.h"
|
2009-12-16 23:49:21 +00:00
|
|
|
#include "../sound/sound_driver.hpp"
|
|
|
|
#include "../video/video_driver.hpp"
|
2005-07-25 07:16:10 +00:00
|
|
|
#include "extmidi.h"
|
2005-03-30 19:52:26 +00:00
|
|
|
#include <fcntl.h>
|
2004-08-09 17:04:08 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2009-01-08 12:05:14 +00:00
|
|
|
#ifndef EXTERNAL_PLAYER
|
|
|
|
#define EXTERNAL_PLAYER "timidity"
|
|
|
|
#endif
|
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi;
|
|
|
|
|
2009-01-10 00:31:47 +00:00
|
|
|
const char *MusicDriver_ExtMidi::Start(const char * const * parm)
|
2004-11-06 23:24:59 +00:00
|
|
|
{
|
2009-12-16 23:49:21 +00:00
|
|
|
if (strcmp(_video_driver->GetName(), "allegro") == 0 ||
|
|
|
|
strcmp(_sound_driver->GetName(), "allegro") == 0) {
|
|
|
|
return "the extmidi driver does not work when Allegro is loaded.";
|
|
|
|
}
|
|
|
|
|
2009-01-08 12:05:14 +00:00
|
|
|
const char *command = GetDriverParam(parm, "cmd");
|
|
|
|
if (StrEmpty(command)) command = EXTERNAL_PLAYER;
|
|
|
|
|
|
|
|
this->command = strdup(command);
|
2007-07-05 13:19:35 +00:00
|
|
|
this->song[0] = '\0';
|
|
|
|
this->pid = -1;
|
2004-11-06 23:24:59 +00:00
|
|
|
return NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
void MusicDriver_ExtMidi::Stop()
|
2004-11-06 23:24:59 +00:00
|
|
|
{
|
2009-01-08 12:05:14 +00:00
|
|
|
free(command);
|
2007-07-05 13:19:35 +00:00
|
|
|
this->song[0] = '\0';
|
|
|
|
this->DoStop();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-01-10 00:31:47 +00:00
|
|
|
void MusicDriver_ExtMidi::PlaySong(const char *filename)
|
2004-11-06 23:24:59 +00:00
|
|
|
{
|
2008-11-02 11:20:15 +00:00
|
|
|
strecpy(this->song, filename, lastof(this->song));
|
2007-07-05 13:19:35 +00:00
|
|
|
this->DoStop();
|
2005-03-30 19:52:26 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
void MusicDriver_ExtMidi::StopSong()
|
2005-03-30 19:52:26 +00:00
|
|
|
{
|
2007-07-05 13:19:35 +00:00
|
|
|
this->song[0] = '\0';
|
|
|
|
this->DoStop();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
bool MusicDriver_ExtMidi::IsSongPlaying()
|
2004-11-06 23:24:59 +00:00
|
|
|
{
|
2009-04-10 11:03:48 +00:00
|
|
|
if (this->pid != -1 && waitpid(this->pid, NULL, WNOHANG) == this->pid) {
|
2007-07-05 13:19:35 +00:00
|
|
|
this->pid = -1;
|
2009-04-10 11:03:48 +00:00
|
|
|
}
|
2007-07-05 13:19:35 +00:00
|
|
|
if (this->pid == -1 && this->song[0] != '\0') this->DoPlay();
|
|
|
|
return this->pid != -1;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
void MusicDriver_ExtMidi::SetVolume(byte vol)
|
2004-11-06 23:24:59 +00:00
|
|
|
{
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(driver, 1, "extmidi: set volume not implemented");
|
2005-03-30 19:52:26 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-07-05 13:19:35 +00:00
|
|
|
void MusicDriver_ExtMidi::DoPlay()
|
2005-03-30 19:52:26 +00:00
|
|
|
{
|
2007-07-05 13:19:35 +00:00
|
|
|
this->pid = fork();
|
|
|
|
switch (this->pid) {
|
2005-03-30 19:52:26 +00:00
|
|
|
case 0: {
|
|
|
|
close(0);
|
2009-04-10 11:03:48 +00:00
|
|
|
int d = open("/dev/null", O_RDONLY);
|
2005-09-12 09:53:56 +00:00
|
|
|
if (d != -1 && dup2(d, 1) != -1 && dup2(d, 2) != -1) {
|
|
|
|
#if defined(MIDI_ARG)
|
2009-01-08 12:05:14 +00:00
|
|
|
execlp(this->command, "extmidi", MIDI_ARG, this->song, (char*)0);
|
2005-09-12 09:53:56 +00:00
|
|
|
#else
|
2009-01-08 12:05:14 +00:00
|
|
|
execlp(this->command, "extmidi", this->song, (char*)0);
|
2005-09-12 09:53:56 +00:00
|
|
|
#endif
|
2005-03-30 19:52:26 +00:00
|
|
|
}
|
2005-09-11 09:24:17 +00:00
|
|
|
_exit(1);
|
2005-03-30 19:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case -1:
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(driver, 0, "extmidi: couldn't fork: %s", strerror(errno));
|
2005-03-30 19:52:26 +00:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
|
|
|
default:
|
2007-07-05 13:19:35 +00:00
|
|
|
this->song[0] = '\0';
|
2005-03-30 19:52:26 +00:00
|
|
|
break;
|
2004-11-06 23:24:59 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 13:19:35 +00:00
|
|
|
void MusicDriver_ExtMidi::DoStop()
|
2004-11-06 23:24:59 +00:00
|
|
|
{
|
2007-07-05 13:19:35 +00:00
|
|
|
if (this->pid != -1) kill(this->pid, SIGTERM);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __MORPHOS__ */
|