12#include "../string_func.h"
13#include "../sound/sound_driver.hpp"
14#include "../video/video_driver.hpp"
15#include "../gfx_func.h"
17#include "../base_media_base.h"
19#include "midifile.hpp"
27#include "../safeguards.h"
29#ifndef EXTERNAL_PLAYER
31#define EXTERNAL_PLAYER "timidity"
41 return "the extmidi driver does not work when Allegro is loaded.";
51 this->command_tokens.clear();
53 std::string_view view = command;
55 auto pos = view.find(
' ');
56 this->command_tokens.emplace_back(view.substr(0, pos));
58 if (pos == std::string_view::npos)
break;
59 view.remove_prefix(pos + 1);
76 if (!filename.empty()) {
77 this->song = std::move(filename);
90 if (this->pid != -1 && waitpid(this->pid,
nullptr, WNOHANG) == this->pid) {
93 if (this->pid == -1 && !this->song.empty()) this->DoPlay();
94 return this->pid != -1;
99 Debug(driver, 1,
"extmidi: set volume not implemented");
102void MusicDriver_ExtMidi::DoPlay()
108 int d = open(
"/dev/null", O_RDONLY);
109 if (d != -1 && dup2(d, 1) != -1 && dup2(d, 2) != -1) {
112 std::vector<char *> parameters;
113 for (
auto &token : this->command_tokens) parameters.emplace_back(token.data());
114 parameters.emplace_back(this->song.data());
115 parameters.emplace_back(
nullptr);
117 execvp(parameters[0], parameters.data());
123 Debug(driver, 0,
"extmidi: couldn't fork: {}", strerror(errno));
142 for (
int i = 0; i < 20; i++) {
144 if (waitpid(pid,
nullptr, WNOHANG) == pid) {
156void MusicDriver_ExtMidi::DoStop()
158 if (this->pid <= 0)
return;
160 if (
KillWait(this->pid, SIGINT))
return;
162 if (
KillWait(this->pid, SIGTERM))
return;
164 Debug(driver, 0,
"extmidi: gracefully stopping failed, trying the hard way");
167 kill(this->pid, SIGKILL);
168 waitpid(this->pid,
nullptr, 0);
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
void Stop() override
Stop this driver.
std::optional< std::string_view > Start(const StringList ¶m) override
Start this driver.
std::string_view GetName() const override
Get the name of this driver.
void StopSong() override
Stop playing the current song.
bool IsSongPlaying() override
Are we currently playing a song?
void SetVolume(uint8_t vol) override
Set the volume, if possible.
static SoundDriver * GetInstance()
Get the currently active instance of the sound driver.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
const char * GetDriverParam(const StringList &parm, const char *name)
Get a string parameter the list of parameters.
static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi
Factory for the midi player that uses external players.
#define EXTERNAL_PLAYER
The default external midi player.
static bool KillWait(pid_t &pid, int signal)
Try to end child process with kill/waitpid for up to 1 second.
Base support for playing music via an external application.
bool StrEmpty(const char *s)
Check if a string buffer is empty.
std::vector< std::string > StringList
Type for a list of strings.
static std::string GetSMFFile(const MusicSongInfo &song)
Get the name of a Standard MIDI File for a given song.
Metadata about a music track.
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.