123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- //MGT: not needed on dedicated and help removal of SDL
- #ifndef TORQUE_DEDICATED
- #include "console/console.h"
- #include "platformX86UNIX/platformX86UNIX.h"
- #include "platform/platformRedBook.h"
- #include "core/strings/stringFunctions.h"
- #if defined(__linux__)
- #include <linux/cdrom.h>
- #include <sys/ioctl.h>
- #include <errno.h>
- #include <string.h>
- #endif
- #include <SDL/SDL.h>
- class UnixRedBookDevice : public RedBookDevice
- {
- #if !defined(__FreeBSD__)
- private:
- S32 mDeviceId;
- SDL_CD *mCD;
- cdrom_volctrl mOriginalVolume;
- bool mVolumeInitialized;
- #endif
- bool mPlaying;
- void openVolume();
- void closeVolume();
- void setLastError(const char *);
- public:
- UnixRedBookDevice();
- ~UnixRedBookDevice();
- bool open();
- bool close();
- bool play(U32);
- bool stop();
- bool getTrackCount(U32 *);
- bool getVolume(F32 *);
- bool setVolume(F32);
- bool isPlaying() { return mPlaying; }
- bool updateStatus();
- void setDeviceInfo(S32 deviceId, const char *deviceName);
- };
- //-------------------------------------------------------------------------------
- // Class: UnixRedBookDevice
- //-------------------------------------------------------------------------------
- UnixRedBookDevice::UnixRedBookDevice()
- {
- #if !defined(__FreeBSD__)
- mVolumeInitialized = false;
- mDeviceId = -1;
- mDeviceName = NULL;
- mCD = NULL;
- mPlaying = false;
- #endif // !defined(__FreeBSD__)
- }
- //------------------------------------------------------------------------------
- UnixRedBookDevice::~UnixRedBookDevice()
- {
- #if !defined(__FreeBSD__)
- close();
- #endif // !defined(__FreeBSD__)
- }
- //------------------------------------------------------------------------------
- bool UnixRedBookDevice::updateStatus()
- {
- #if !defined(__FreeBSD__)
- AssertFatal(mCD, "mCD is NULL");
- CDstatus status = SDL_CDStatus(mCD);
- if (status == CD_ERROR)
- {
- setLastError("Error accessing device");
- return(false);
- }
- else if (status == CD_TRAYEMPTY)
- {
- setLastError("CD tray empty");
- return false;
- }
- mPlaying = (status == CD_PLAYING);
- return true;
- #endif // !defined(__FreeBSD__)
- }
- //------------------------------------------------------------------------------
- void UnixRedBookDevice::setDeviceInfo(S32 deviceId, const char *deviceName)
- {
- #if !defined(__FreeBSD__)
- mDeviceId = deviceId;
- mDeviceName = new char[dStrlen(deviceName) + 1];
- dStrcpy(mDeviceName, deviceName);
- #endif // !defined(__FreeBSD__)
- }
- //------------------------------------------------------------------------------
- bool UnixRedBookDevice::open()
- {
- #if !defined(__FreeBSD__)
- if(mAcquired)
- {
- setLastError("Device is already open.");
- return(false);
- }
- // open the device
- mCD = SDL_CDOpen(mDeviceId);
- if (mCD == NULL)
- {
- setLastError(SDL_GetError());
- return false;
- }
- mAcquired = true;
- openVolume();
- setLastError("");
- return(true);
- #endif // !defined(__FreeBSD__)
- }
- //------------------------------------------------------------------------------
- bool UnixRedBookDevice::close()
- {
- #if !defined(__FreeBSD__)
- if(!mAcquired)
- {
- setLastError("Device has not been acquired");
- return(false);
- }
- stop();
- closeVolume();
- if (mCD != NULL)
- {
- SDL_CDClose(mCD);
- mCD = NULL;
- }
- mAcquired = false;
- setLastError("");
- return(true);
- #endif // !defined(__FreeBSD__)
- }
- //------------------------------------------------------------------------------
- bool UnixRedBookDevice::play(U32 track)
- {
- #if !defined(__FreeBSD__)
- if(!mAcquired)
- {
- setLastError("Device has not been acquired");
- return(false);
- }
- U32 numTracks;
- if(!getTrackCount(&numTracks))
- return(false);
- if(track >= numTracks)
- {
- setLastError("Track index is out of range");
- return(false);
- }
- if (!updateStatus())
- return false;
- AssertFatal(mCD, "mCD is NULL");
- if (SDL_CDPlayTracks(mCD, track, 0, 1, 0) == -1)
- {
- setLastError(SDL_GetError());
- return false;
- }
- mPlaying = true;
- setLastError("");
- return(true);
- #endif // !defined(__FreeBSD__)
- }
- //------------------------------------------------------------------------------
- bool UnixRedBookDevice::stop()
- {
- #if !defined(__FreeBSD__)
- if(!mAcquired)
- {
- setLastError("Device has not been acquired");
- return(false);
- }
- AssertFatal(mCD, "mCD is NULL");
- if (SDL_CDStop(mCD) == -1)
- {
- setLastError(SDL_GetError());
- return(false);
- }
- mPlaying = false;
- setLastError("");
- return(true);
- #endif // !defined(__FreeBSD__)
- }
- //------------------------------------------------------------------------------
- bool UnixRedBookDevice::getTrackCount(U32 * numTracks)
- {
- #if !defined(__FreeBSD__)
- if(!mAcquired)
- {
- setLastError("Device has not been acquired");
- return(false);
- }
- if (!updateStatus())
- return false;
- AssertFatal(mCD, "mCD is NULL");
- *numTracks = mCD->numtracks;
- return(true);
- #endif // !defined(__FreeBSD__)
- }
- template <class Type>
- static inline Type max(Type v1, Type v2)
- {
- #if !defined(__FreeBSD__)
- if (v1 <= v2)
- return v2;
- else
- return v1;
- #endif // !defined(__FreeBSD__)
- }
- //------------------------------------------------------------------------------
- bool UnixRedBookDevice::getVolume(F32 * volume)
- {
- #if !defined(__FreeBSD__)
- if(!mAcquired)
- {
- setLastError("Device has not been acquired");
- return(false);
- }
- if(!mVolumeInitialized)
- {
- setLastError("Volume failed to initialize");
- return(false);
- }
- #if defined(__linux__)
- AssertFatal(mCD, "mCD is NULL");
- setLastError("");
- cdrom_volctrl sysvol;
- if (ioctl(mCD->id, CDROMVOLREAD, &sysvol) == -1)
- {
- setLastError(strerror(errno));
- return(false);
- }
- U8 maxVol = max(sysvol.channel0, sysvol.channel1);
- // JMQTODO: support different left/right channel volumes?
- *volume = static_cast<F32>(maxVol) / 255.f;
- return true;
- #else
- return(false);
- #endif
- #endif // !defined(__FreeBSD__)
- }
- //------------------------------------------------------------------------------
- bool UnixRedBookDevice::setVolume(F32 volume)
- {
- #if !defined(__FreeBSD__)
- if(!mAcquired)
- {
- setLastError("Device has not been acquired");
- return(false);
- }
- if(!mVolumeInitialized)
- {
- setLastError("Volume failed to initialize");
- return(false);
- }
- #if defined(__linux__)
- AssertFatal(mCD, "mCD is NULL");
- setLastError("");
- cdrom_volctrl sysvol;
- volume = volume * 255.f;
- if (volume > 255)
- volume = 255;
- if (volume < 0)
- volume = 0;
- sysvol.channel0 = sysvol.channel1 = static_cast<__u8>(volume);
- if (ioctl(mCD->id, CDROMVOLCTRL, &sysvol) == -1)
- {
- setLastError(strerror(errno));
- return(false);
- }
- return true;
- #else
- return(false);
- #endif
- #endif // !defined(__FreeBSD__)
- }
- //------------------------------------------------------------------------------
- void UnixRedBookDevice::openVolume()
- {
- #if !defined(__FreeBSD__)
- // Its unforunate that we have to do it this way, but SDL does not currently
- // support setting CD audio volume
- #if defined(__linux__)
- AssertFatal(mCD, "mCD is NULL");
- setLastError("");
- if (ioctl(mCD->id, CDROMVOLREAD, &mOriginalVolume) == -1)
- {
- setLastError(strerror(errno));
- return;
- }
- mVolumeInitialized = true;
- #else
- setLastError("Volume failed to initialize");
- #endif
- #endif // !defined(__FreeBSD__)
- }
- void UnixRedBookDevice::closeVolume()
- {
- #if !defined(__FreeBSD__)
- if(!mVolumeInitialized)
- return;
- #if defined(__linux__)
- AssertFatal(mCD, "mCD is NULL");
- setLastError("");
- if (ioctl(mCD->id, CDROMVOLCTRL, &mOriginalVolume) == -1)
- {
- setLastError(strerror(errno));
- return;
- }
- #endif
- mVolumeInitialized = false;
- #endif // !defined(__FreeBSD__)
- }
- //------------------------------------------------------------------------------
- void UnixRedBookDevice::setLastError(const char * error)
- {
- #if !defined(__FreeBSD__)
- RedBook::setLastError(error);
- #endif // !defined(__FreeBSD__)
- }
- //------------------------------------------------------------------------------
- void InstallRedBookDevices()
- {
- #if !defined(__FreeBSD__)
- Con::printf("CD Audio Init:");
- if (SDL_InitSubSystem(SDL_INIT_CDROM) == -1)
- {
- Con::printf(" Unable to initialize CD Audio: %s", SDL_GetError());
- return;
- }
- S32 numDrives = SDL_CDNumDrives();
- if (numDrives == 0)
- {
- Con::printf(" No drives found.");
- return;
- }
- for (int i = 0; i < numDrives; ++i)
- {
- const char * deviceName = SDL_CDName(i);
- Con::printf(" Installing CD Audio device: %s", deviceName);
- UnixRedBookDevice * device = new UnixRedBookDevice;
- device->setDeviceInfo(i, deviceName);
- RedBook::installDevice(device);
- }
- Con::printf(" ");
- #endif // !defined(__FreeBSD__)
- }
- //------------------------------------------------------------------------------
- void PollRedbookDevices()
- {
- #if !defined(__FreeBSD__)
- UnixRedBookDevice *device = dynamic_cast<UnixRedBookDevice*>(RedBook::getCurrentDevice());
- if (device == NULL || !device->isPlaying())
- return;
- static const U32 PollDelay = 1000;
- static U32 lastPollTime = 0;
- U32 curTime = Platform::getVirtualMilliseconds();
- if (lastPollTime != 0 &&
- (curTime - lastPollTime) < PollDelay)
- return;
- lastPollTime = curTime;
- if (device->isPlaying())
- {
- device->updateStatus();
- if (!device->isPlaying())
- RedBook::handleCallback(RedBook::PlayFinished);
- }
- #endif // !defined(__FreeBSD__)
- }
- #endif
- //MGT: end
|