OpenTTD: src/music/extmidi.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

extmidi.cpp
Go to the documentation of this file.
1 /* $Id: extmidi.cpp 26556 2014-05-03 20:21:01Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #include "../stdafx.h"
13 #include "../debug.h"
14 #include "../string_func.h"
15 #include "../sound/sound_driver.hpp"
16 #include "../video/video_driver.hpp"
17 #include "../gfx_func.h"
18 #include "extmidi.h"
19 #include <fcntl.h>
20 #include <sys/types.h>
21 #include <sys/wait.h>
22 #include <unistd.h>
23 #include <signal.h>
24 #include <sys/stat.h>
25 #include <errno.h>
26
27 #include "../safeguards.h"
28
29 #ifndef EXTERNAL_PLAYER
30
31 #define EXTERNAL_PLAYER "timidity"
32 #endif
33
35 static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi;
36
37 const char *MusicDriver_ExtMidi::Start(const char * const * parm)
38 {
39 if (strcmp(VideoDriver::GetInstance()->GetName(), "allegro") == 0 ||
40 strcmp(SoundDriver::GetInstance()->GetName(), "allegro") == 0) {
41 return "the extmidi driver does not work when Allegro is loaded.";
42 }
43
44 const char *command = GetDriverParam(parm, "cmd");
45 if (StrEmpty(command)) command = EXTERNAL_PLAYER;
46
47 this->command = stredup(command);
48 this->song[0] = '\0';
49 this->pid = -1;
50 return NULL;
51 }
52
53 void MusicDriver_ExtMidi::Stop()
54 {
55 free(command);
56 this->song[0] = '\0';
57 this->DoStop();
58 }
59
60 void MusicDriver_ExtMidi::PlaySong(const char *filename)
61 {
62 strecpy(this->song, filename, lastof(this->song));
63 this->DoStop();
64 }
65
66 void MusicDriver_ExtMidi::StopSong()
67 {
68 this->song[0] = '\0';
69 this->DoStop();
70 }
71
72 bool MusicDriver_ExtMidi::IsSongPlaying()
73 {
74 if (this->pid != -1 && waitpid(this->pid, NULL, WNOHANG) == this->pid) {
75 this->pid = -1;
76 }
77 if (this->pid == -1 && this->song[0] != '\0') this->DoPlay();
78 return this->pid != -1;
79 }
80
81 void MusicDriver_ExtMidi::SetVolume(byte vol)
82 {
83 DEBUG(driver, 1, "extmidi: set volume not implemented");
84 }
85
86 void MusicDriver_ExtMidi::DoPlay()
87 {
88 this->pid = fork();
89 switch (this->pid) {
90 case 0: {
91 close(0);
92 int d = open("/dev/null", O_RDONLY);
93 if (d != -1 && dup2(d, 1) != -1 && dup2(d, 2) != -1) {
94 #if defined(MIDI_ARG)
95 execlp(this->command, "extmidi", MIDI_ARG, this->song, (char*)0);
96 #else
97 execlp(this->command, "extmidi", this->song, (char*)0);
98 #endif
99 }
100 _exit(1);
101 }
102
103 case -1:
104 DEBUG(driver, 0, "extmidi: couldn't fork: %s", strerror(errno));
105 /* FALL THROUGH */
106
107 default:
108 this->song[0] = '\0';
109 break;
110 }
111 }
112
113 void MusicDriver_ExtMidi::DoStop()
114 {
115 if (this->pid <= 0) return;
116
117 /* First try to gracefully stop for about five seconds;
118 * 5 seconds = 5000 milliseconds, 10 ms per cycle => 500 cycles. */
119 for (int i = 0; i < 500; i++) {
120 kill(this->pid, SIGTERM);
121 if (waitpid(this->pid, NULL, WNOHANG) == this->pid) {
122 /* It has shut down, so we are done */
123 this->pid = -1;
124 return;
125 }
126 /* Wait 10 milliseconds. */
127 CSleep(10);
128 }
129
130 DEBUG(driver, 0, "extmidi: gracefully stopping failed, trying the hard way");
131 /* Gracefully stopping failed. Do it the hard way
132 * and wait till the process finally died. */
133 kill(this->pid, SIGKILL);
134 waitpid(this->pid, NULL, 0);
135 this->pid = -1;
136 }