1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * Original license header:
22  *
23  * Cabal - Legacy Game Implementations
24  *
25  * Cabal is the legal property of its developers, whose names
26  * are too numerous to list here. Please refer to the COPYRIGHT
27  * file distributed with this source distribution.
28  *
29  * This program is free software; you can redistribute it and/or
30  * modify it under the terms of the GNU General Public License
31  * as published by the Free Software Foundation; either version 2
32  * of the License, or (at your option) any later version.
33  *
34  * This program is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  * GNU General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License
40  * along with this program; if not, write to the Free Software
41  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
42  *
43  */
44 
45 #ifdef MACOSX
46 
47 #include <sys/stat.h>
48 #include <sys/mount.h>
49 #include <limits.h>
50 
51 #include "common/scummsys.h"
52 
53 #include "audio/audiostream.h"
54 #include "audio/decoders/aiff.h"
55 #include "audio/timestamp.h"
56 #include "common/config-manager.h"
57 #include "common/debug.h"
58 #include "common/fs.h"
59 #include "common/hashmap.h"
60 #include "common/textconsole.h"
61 #include "backends/audiocd/default/default-audiocd.h"
62 #include "backends/audiocd/macosx/macosx-audiocd.h"
63 #include "backends/fs/stdiostream.h"
64 
65 // Partially based on SDL's code
66 
67 /**
68  * The Mac OS X audio cd manager. Implements real audio cd playback.
69  */
70 class MacOSXAudioCDManager : public DefaultAudioCDManager {
71 public:
MacOSXAudioCDManager()72 	MacOSXAudioCDManager() {}
73 	~MacOSXAudioCDManager();
74 
75 	bool open() override;
76 	void close() override;
77 	bool play(int track, int numLoops, int startFrame, int duration, bool onlyEmulate,
78 			Audio::Mixer::SoundType soundType) override;
79 
80 protected:
81 	bool openCD(int drive) override;
82 	bool openCD(const Common::String &drive) override;
83 
84 private:
85 	struct Drive {
DriveMacOSXAudioCDManager::Drive86 		Drive(const Common::String &m, const Common::String &d, const Common::String &f) :
87 			mountPoint(m), deviceName(d), fsType(f) {}
88 
89 		Common::String mountPoint;
90 		Common::String deviceName;
91 		Common::String fsType;
92 	};
93 
94 	typedef Common::Array<Drive> DriveList;
95 	DriveList detectAllDrives();
96 	DriveList detectCDDADrives();
97 
98 	bool findTrackNames(const Common::String &drivePath);
99 
100 	Common::HashMap<uint, Common::String> _trackMap;
101 };
102 
~MacOSXAudioCDManager()103 MacOSXAudioCDManager::~MacOSXAudioCDManager() {
104 	close();
105 }
106 
open()107 bool MacOSXAudioCDManager::open() {
108 	close();
109 
110 	if (openRealCD())
111 		return true;
112 
113 	return DefaultAudioCDManager::open();
114 }
115 
116 /**
117  * Find the base disk number of device name.
118  * Returns -1 if mount point is not /dev/disk*
119  */
findBaseDiskNumber(const Common::String & diskName)120 static int findBaseDiskNumber(const Common::String &diskName) {
121 	if (!diskName.hasPrefix("/dev/disk"))
122 		return -1;
123 
124 	const char *startPtr = diskName.c_str() + 9;
125 	char *endPtr;
126 	int baseDiskNumber = strtol(startPtr, &endPtr, 10);
127 	if (startPtr == endPtr)
128 		return -1;
129 
130 	return baseDiskNumber;
131 }
132 
openCD(int drive)133 bool MacOSXAudioCDManager::openCD(int drive) {
134 	DriveList allDrives = detectAllDrives();
135 	if (allDrives.empty())
136 		return false;
137 
138 	DriveList cddaDrives;
139 
140 	// Try to get the volume related to the game's path
141 	if (ConfMan.hasKey("path")) {
142 		Common::String gamePath = ConfMan.get("path");
143 		struct statfs gamePathStat;
144 		if (statfs(gamePath.c_str(), &gamePathStat) == 0) {
145 			int baseDiskNumber = findBaseDiskNumber(gamePathStat.f_mntfromname);
146 			if (baseDiskNumber >= 0) {
147 				// Look for a CDDA drive with the same base disk number
148 				for (uint32 i = 0; i < allDrives.size(); i++) {
149 					if (allDrives[i].fsType == "cddafs" && findBaseDiskNumber(allDrives[i].deviceName) == baseDiskNumber) {
150 						debug(1, "Preferring drive '%s'", allDrives[i].mountPoint.c_str());
151 						cddaDrives.push_back(allDrives[i]);
152 						allDrives.remove_at(i);
153 						break;
154 					}
155 				}
156 			}
157 		}
158 	}
159 
160 	// Add the remaining CDDA drives to the CDDA list
161 	for (uint32 i = 0; i < allDrives.size(); i++)
162 		if (allDrives[i].fsType == "cddafs")
163 			cddaDrives.push_back(allDrives[i]);
164 
165 	if (drive >= (int)cddaDrives.size())
166 		return false;
167 
168 	debug(1, "Using '%s' as the CD drive", cddaDrives[drive].mountPoint.c_str());
169 
170 	return findTrackNames(cddaDrives[drive].mountPoint);
171 }
172 
openCD(const Common::String & drive)173 bool MacOSXAudioCDManager::openCD(const Common::String &drive) {
174 	DriveList drives = detectAllDrives();
175 
176 	for (uint32 i = 0; i < drives.size(); i++) {
177 		if (drives[i].fsType != "cddafs")
178 			continue;
179 
180 		if (drives[i].mountPoint == drive || drives[i].deviceName == drive) {
181 			debug(1, "Using '%s' as the CD drive", drives[i].mountPoint.c_str());
182 			return findTrackNames(drives[i].mountPoint);
183 		}
184 	}
185 
186 	return false;
187 }
188 
close()189 void MacOSXAudioCDManager::close() {
190 	DefaultAudioCDManager::close();
191 	_trackMap.clear();
192 }
193 
194 enum {
195 	// Some crazy high number that we'll never actually hit
196 	kMaxDriveCount = 256
197 };
198 
detectAllDrives()199 MacOSXAudioCDManager::DriveList MacOSXAudioCDManager::detectAllDrives() {
200 	// Fetch the lists of drives
201 	struct statfs driveStats[kMaxDriveCount];
202 	int foundDrives = getfsstat(driveStats, sizeof(driveStats), MNT_WAIT);
203 	if (foundDrives <= 0)
204 		return DriveList();
205 
206 	DriveList drives;
207 	for (int i = 0; i < foundDrives; i++)
208 		drives.push_back(Drive(driveStats[i].f_mntonname, driveStats[i].f_mntfromname, driveStats[i].f_fstypename));
209 
210 	return drives;
211 }
212 
play(int track,int numLoops,int startFrame,int duration,bool onlyEmulate,Audio::Mixer::SoundType soundType)213 bool MacOSXAudioCDManager::play(int track, int numLoops, int startFrame, int duration, bool onlyEmulate,
214 		Audio::Mixer::SoundType soundType) {
215 	// Prefer emulation
216 	if (DefaultAudioCDManager::play(track, numLoops, startFrame, duration, onlyEmulate, soundType))
217 		return true;
218 
219 	// If we're set to only emulate, or have no CD drive, return here
220 	if (onlyEmulate || !_trackMap.contains(track))
221 		return false;
222 
223 	if (!numLoops && !startFrame)
224 		return false;
225 
226 	// Now load the AIFF track from the name
227 	Common::String fileName = _trackMap[track];
228 	Common::SeekableReadStream *stream = StdioStream::makeFromPath(fileName.c_str(), false);
229 
230 	if (!stream) {
231 		warning("Failed to open track '%s'", fileName.c_str());
232 		return false;
233 	}
234 
235 	Audio::AudioStream *audioStream = Audio::makeAIFFStream(stream, DisposeAfterUse::YES);
236 	if (!audioStream) {
237 		warning("Track '%s' is not an AIFF track", fileName.c_str());
238 		return false;
239 	}
240 
241 	Audio::SeekableAudioStream *seekStream = dynamic_cast<Audio::SeekableAudioStream *>(audioStream);
242 	if (!seekStream) {
243 		warning("Track '%s' is not seekable", fileName.c_str());
244 		return false;
245 	}
246 
247 	Audio::Timestamp start = Audio::Timestamp(0, startFrame, 75);
248 	Audio::Timestamp end = duration ? Audio::Timestamp(0, startFrame + duration, 75) : seekStream->getLength();
249 
250 	// Fake emulation since we're really playing an AIFF file
251 	_emulating = true;
252 
253 	_mixer->playStream(soundType, &_handle,
254 	                   Audio::makeLoopingAudioStream(seekStream, start, end, (numLoops < 1) ? numLoops + 1 : numLoops), -1, _cd.volume, _cd.balance);
255 	return true;
256 }
257 
findTrackNames(const Common::String & drivePath)258 bool MacOSXAudioCDManager::findTrackNames(const Common::String &drivePath) {
259 	Common::FSNode directory(drivePath);
260 
261 	if (!directory.exists()) {
262 		warning("Directory '%s' does not exist", drivePath.c_str());
263 		return false;
264 	}
265 
266 	if (!directory.isDirectory()) {
267 		warning("'%s' is not a directory", drivePath.c_str());
268 		return false;
269 	}
270 
271 	Common::FSList children;
272 	if (!directory.getChildren(children, Common::FSNode::kListFilesOnly)) {
273 		warning("Failed to find children for '%s'", drivePath.c_str());
274 		return false;
275 	}
276 
277 	for (uint32 i = 0; i < children.size(); i++) {
278 		if (!children[i].isDirectory()) {
279 			Common::String fileName = children[i].getName();
280 
281 			if (fileName.hasSuffix(".aiff") || fileName.hasSuffix(".cdda")) {
282 				uint j = 0;
283 
284 				// Search for the track ID in the file name.
285 				for (; j < fileName.size() && !Common::isDigit(fileName[j]); j++)
286 					;
287 
288 				const char *trackIDString = fileName.c_str() + j;
289 				char *endPtr = nullptr;
290 				long trackID = strtol(trackIDString, &endPtr, 10);
291 
292 				if (trackIDString != endPtr && trackID > 0 && trackID < UINT_MAX) {
293 					_trackMap[trackID - 1] = drivePath + '/' + fileName;
294 				} else {
295 					warning("Invalid track file name: '%s'", fileName.c_str());
296 				}
297 			}
298 		}
299 	}
300 
301 	return true;
302 }
303 
createMacOSXAudioCDManager()304 AudioCDManager *createMacOSXAudioCDManager() {
305 	return new MacOSXAudioCDManager();
306 }
307 
308 #endif // MACOSX
309