Files
@ r25895:573b0b525189
Branch filter:
Location: cpp/openttd-patchpack/source/src/music/extmidi.cpp - annotation
r25895:573b0b525189
4.3 KiB
text/x-c
Fix: [Actions] changelog assumed major.minor.patch versioning (#9482)
We now use major.minor versioning, so it failed to pick up the
correct information.
We now use major.minor versioning, so it failed to pick up the
correct information.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r9111:983de9c5a848 r9111:983de9c5a848 r5584:545d748cc681 r5584:545d748cc681 r14248:a9050881acd7 r22623:5b6187e46d80 r13972:27207432d43d r13972:27207432d43d r14582:fbb94a5fa480 r5584:545d748cc681 r22883:e713e5b73adc r23589:b3521e885d81 r22888:a5ba89e2c64e r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r21383:942c32fb8b0e r21383:942c32fb8b0e r10617:374c2a51fc3a r17629:21e9dfd343cd r10617:374c2a51fc3a r10617:374c2a51fc3a r10617:374c2a51fc3a r17629:21e9dfd343cd r7170:38b143754b40 r7170:38b143754b40 r24218:c32caa9f014d r5584:545d748cc681 r21436:38079fc3bcd8 r21436:38079fc3bcd8 r13972:27207432d43d r13972:27207432d43d r13972:27207432d43d r10617:374c2a51fc3a r22623:5b6187e46d80 r10617:374c2a51fc3a r22623:5b6187e46d80 r22623:5b6187e46d80 r22623:5b6187e46d80 r22623:5b6187e46d80 r23607:36c15679007d r22623:5b6187e46d80 r22623:5b6187e46d80 r22623:5b6187e46d80 r22623:5b6187e46d80 r22623:5b6187e46d80 r10617:374c2a51fc3a r22623:5b6187e46d80 r22623:5b6187e46d80 r22623:5b6187e46d80 r22623:5b6187e46d80 r23607:36c15679007d r22623:5b6187e46d80 r22623:5b6187e46d80 r22623:5b6187e46d80 r22623:5b6187e46d80 r22623:5b6187e46d80 r22623:5b6187e46d80 r22623:5b6187e46d80 r22623:5b6187e46d80 r22623:5b6187e46d80 r7172:5498839741c0 r7172:5498839741c0 r23607:36c15679007d r5584:545d748cc681 r5584:545d748cc681 r7170:38b143754b40 r5584:545d748cc681 r22623:5b6187e46d80 r22623:5b6187e46d80 r7172:5498839741c0 r7172:5498839741c0 r5584:545d748cc681 r5584:545d748cc681 r22883:e713e5b73adc r5584:545d748cc681 r22888:a5ba89e2c64e r22888:a5ba89e2c64e r22888:a5ba89e2c64e r22888:a5ba89e2c64e r22888:a5ba89e2c64e r5584:545d748cc681 r5584:545d748cc681 r7170:38b143754b40 r5584:545d748cc681 r7172:5498839741c0 r7172:5498839741c0 r5584:545d748cc681 r5584:545d748cc681 r7170:38b143754b40 r5584:545d748cc681 r23607:36c15679007d r7172:5498839741c0 r11627:b2d9c9dc2432 r7172:5498839741c0 r7172:5498839741c0 r5584:545d748cc681 r5584:545d748cc681 r7170:38b143754b40 r5584:545d748cc681 r25655:1030dcb7eb52 r5584:545d748cc681 r5584:545d748cc681 r7172:5498839741c0 r5584:545d748cc681 r7172:5498839741c0 r7172:5498839741c0 r5584:545d748cc681 r5584:545d748cc681 r11627:b2d9c9dc2432 r5584:545d748cc681 r22623:5b6187e46d80 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r25655:1030dcb7eb52 r22672:3473f7daf422 r5584:545d748cc681 r5584:545d748cc681 r7172:5498839741c0 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r7172:5498839741c0 r5584:545d748cc681 r14582:fbb94a5fa480 r14582:fbb94a5fa480 r14582:fbb94a5fa480 r14582:fbb94a5fa480 r14582:fbb94a5fa480 r14582:fbb94a5fa480 r23607:36c15679007d r14582:fbb94a5fa480 r14582:fbb94a5fa480 r14582:fbb94a5fa480 r14582:fbb94a5fa480 r14582:fbb94a5fa480 r14582:fbb94a5fa480 r14582:fbb94a5fa480 r14582:fbb94a5fa480 r25655:1030dcb7eb52 r14582:fbb94a5fa480 r14582:fbb94a5fa480 r14582:fbb94a5fa480 r23607:36c15679007d r14582:fbb94a5fa480 r5584:545d748cc681 | /*
* 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/>.
*/
/** @file extmidi.cpp Playing music via an external player. */
#include "../stdafx.h"
#include "../debug.h"
#include "../string_func.h"
#include "../core/alloc_func.hpp"
#include "../sound/sound_driver.hpp"
#include "../video/video_driver.hpp"
#include "../gfx_func.h"
#include "extmidi.h"
#include "../base_media_base.h"
#include "../thread.h"
#include "midifile.hpp"
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <sys/stat.h>
#include <errno.h>
#include "../safeguards.h"
#ifndef EXTERNAL_PLAYER
/** The default external midi player. */
#define EXTERNAL_PLAYER "timidity"
#endif
/** Factory for the midi player that uses external players. */
static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi;
const char *MusicDriver_ExtMidi::Start(const StringList &parm)
{
if (strcmp(VideoDriver::GetInstance()->GetName(), "allegro") == 0 ||
strcmp(SoundDriver::GetInstance()->GetName(), "allegro") == 0) {
return "the extmidi driver does not work when Allegro is loaded.";
}
const char *command = GetDriverParam(parm, "cmd");
#ifndef MIDI_ARG
if (StrEmpty(command)) command = EXTERNAL_PLAYER;
#else
if (StrEmpty(command)) command = EXTERNAL_PLAYER " " MIDI_ARG;
#endif
/* Count number of arguments, but include 3 extra slots: 1st for command, 2nd for song title, and 3rd for terminating nullptr. */
uint num_args = 3;
for (const char *t = command; *t != '\0'; t++) if (*t == ' ') num_args++;
this->params = CallocT<char *>(num_args);
this->params[0] = stredup(command);
/* Replace space with \0 and add next arg to params */
uint p = 1;
while (true) {
this->params[p] = strchr(this->params[p - 1], ' ');
if (this->params[p] == nullptr) break;
this->params[p][0] = '\0';
this->params[p]++;
p++;
}
/* Last parameter is the song file. */
this->params[p] = this->song;
this->song[0] = '\0';
this->pid = -1;
return nullptr;
}
void MusicDriver_ExtMidi::Stop()
{
free(params[0]);
free(params);
this->song[0] = '\0';
this->DoStop();
}
void MusicDriver_ExtMidi::PlaySong(const MusicSongInfo &song)
{
std::string filename = MidiFile::GetSMFFile(song);
if (!filename.empty()) {
strecpy(this->song, filename.c_str(), lastof(this->song));
this->DoStop();
}
}
void MusicDriver_ExtMidi::StopSong()
{
this->song[0] = '\0';
this->DoStop();
}
bool MusicDriver_ExtMidi::IsSongPlaying()
{
if (this->pid != -1 && waitpid(this->pid, nullptr, WNOHANG) == this->pid) {
this->pid = -1;
}
if (this->pid == -1 && this->song[0] != '\0') this->DoPlay();
return this->pid != -1;
}
void MusicDriver_ExtMidi::SetVolume(byte vol)
{
Debug(driver, 1, "extmidi: set volume not implemented");
}
void MusicDriver_ExtMidi::DoPlay()
{
this->pid = fork();
switch (this->pid) {
case 0: {
close(0);
int d = open("/dev/null", O_RDONLY);
if (d != -1 && dup2(d, 1) != -1 && dup2(d, 2) != -1) {
execvp(this->params[0], this->params);
}
_exit(1);
}
case -1:
Debug(driver, 0, "extmidi: couldn't fork: {}", strerror(errno));
FALLTHROUGH;
default:
this->song[0] = '\0';
break;
}
}
void MusicDriver_ExtMidi::DoStop()
{
if (this->pid <= 0) return;
/* First try to gracefully stop for about five seconds;
* 5 seconds = 5000 milliseconds, 10 ms per cycle => 500 cycles. */
for (int i = 0; i < 500; i++) {
kill(this->pid, SIGTERM);
if (waitpid(this->pid, nullptr, WNOHANG) == this->pid) {
/* It has shut down, so we are done */
this->pid = -1;
return;
}
/* Wait 10 milliseconds. */
CSleep(10);
}
Debug(driver, 0, "extmidi: gracefully stopping failed, trying the hard way");
/* Gracefully stopping failed. Do it the hard way
* and wait till the process finally died. */
kill(this->pid, SIGKILL);
waitpid(this->pid, nullptr, 0);
this->pid = -1;
}
|