Torque3D Documentation / _generateds / x86UNIXRedbook.cpp

x86UNIXRedbook.cpp

Engine/source/platformX86UNIX/x86UNIXRedbook.cpp

More...

Classes:

Detailed Description

Public Functions

InstallRedBookDevices()

max(Type v1, Type v2)

PollRedbookDevices()

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "console/console.h"
 25#include "platformX86UNIX/platformX86UNIX.h"
 26#include "platform/platformRedBook.h"
 27#include "core/strings/stringFunctions.h"
 28
 29#if defined(__linux__)
 30#include <linux/cdrom.h>
 31#include <sys/ioctl.h>
 32#include <errno.h>
 33#include <string.h>
 34#endif
 35
 36#include <SDL.h>
 37
 38class SDL_CD; // TODO SDL remove
 39
 40class UnixRedBookDevice : public RedBookDevice
 41{
 42#if !defined(__FreeBSD__)
 43   private:
 44      S32 mDeviceId;
 45      SDL_CD *mCD;
 46      cdrom_volctrl mOriginalVolume;
 47      bool mVolumeInitialized;
 48#endif
 49      bool mPlaying;
 50
 51      void openVolume();
 52      void closeVolume();
 53      void setLastError(const char *);
 54
 55   public:
 56      UnixRedBookDevice();
 57      ~UnixRedBookDevice();
 58
 59      bool open();
 60      bool close();
 61      bool play(U32);
 62      bool stop();
 63      bool getTrackCount(U32 *);
 64      bool getVolume(F32 *);
 65      bool setVolume(F32);
 66
 67      bool isPlaying() { return mPlaying; }
 68      bool updateStatus();
 69      void setDeviceInfo(S32 deviceId, const char *deviceName);
 70};
 71
 72//-------------------------------------------------------------------------------
 73// Class: UnixRedBookDevice
 74//-------------------------------------------------------------------------------
 75UnixRedBookDevice::UnixRedBookDevice()
 76{
 77#if !defined(__FreeBSD__)
 78   mVolumeInitialized = false;
 79   mDeviceId = -1;
 80   mDeviceName = NULL;
 81   mCD = NULL;
 82   mPlaying = false;
 83#endif   // !defined(__FreeBSD__)
 84}
 85
 86//------------------------------------------------------------------------------
 87UnixRedBookDevice::~UnixRedBookDevice()
 88{
 89#if !defined(__FreeBSD__)
 90   close();
 91#endif   // !defined(__FreeBSD__)
 92}
 93
 94//------------------------------------------------------------------------------
 95bool UnixRedBookDevice::updateStatus()
 96{
 97   return false; // TODO LINUX
 98}
 99
100//------------------------------------------------------------------------------
101void UnixRedBookDevice::setDeviceInfo(S32 deviceId, const char *deviceName)
102{
103#if !defined(__FreeBSD__)
104   mDeviceId = deviceId;
105   dsize_t deviceNameLen = dStrlen(deviceName) + 1;
106   mDeviceName = new char[deviceNameLen];
107   dStrcpy(mDeviceName, deviceName, deviceNameLen);
108#endif   // !defined(__FreeBSD__)
109}
110
111//------------------------------------------------------------------------------
112bool UnixRedBookDevice::open()
113{
114   return false; // TODO LINUX
115}
116
117//------------------------------------------------------------------------------
118bool UnixRedBookDevice::close()
119{
120   return false; // TODO LINUX
121}
122
123//------------------------------------------------------------------------------
124bool UnixRedBookDevice::play(U32 track)
125{
126   return false; // TODO LINUX
127}
128
129//------------------------------------------------------------------------------
130bool UnixRedBookDevice::stop()
131{
132   return false; // TODO LINUX
133}
134
135//------------------------------------------------------------------------------
136bool UnixRedBookDevice::getTrackCount(U32 * numTracks)
137{
138   return false; // TODO LINUX
139}
140
141template <class Type>
142static inline Type max(Type v1, Type v2)
143{
144#if !defined(__FreeBSD__)
145   if (v1 <= v2)
146      return v2;
147   else
148      return v1;
149#endif   // !defined(__FreeBSD__)
150}
151//------------------------------------------------------------------------------
152bool UnixRedBookDevice::getVolume(F32 * volume)
153{
154#if !defined(__FreeBSD__)
155   if(!mAcquired)
156   {
157      setLastError("Device has not been acquired");
158      return(false);
159   }
160
161   if(!mVolumeInitialized)
162   {
163      setLastError("Volume failed to initialize");
164      return(false);
165   }
166
167#if defined(__linux__)
168   AssertFatal(0, "SDL CD not implemented");
169   return true;
170#else
171   return(false);
172#endif
173#endif   // !defined(__FreeBSD__)
174}
175
176//------------------------------------------------------------------------------
177bool UnixRedBookDevice::setVolume(F32 volume)
178{
179#if !defined(__FreeBSD__)
180   if(!mAcquired)
181   {
182      setLastError("Device has not been acquired");
183      return(false);
184   }
185
186   if(!mVolumeInitialized)
187   {
188      setLastError("Volume failed to initialize");
189      return(false);
190   }
191
192#if defined(__linux__)
193   AssertFatal(0, "SDL CD not implemented");
194   return true;
195#else
196   return(false);
197#endif
198#endif   // !defined(__FreeBSD__)
199}
200
201//------------------------------------------------------------------------------
202void UnixRedBookDevice::openVolume()
203{
204#if !defined(__FreeBSD__)
205// Its unforunate that we have to do it this way, but SDL does not currently
206// support setting CD audio volume
207#if defined(__linux__)
208   AssertFatal(0, "SDL CD not implemented");
209#else
210   setLastError("Volume failed to initialize");
211#endif
212#endif   // !defined(__FreeBSD__)
213}
214
215void UnixRedBookDevice::closeVolume()
216{
217#if !defined(__FreeBSD__)
218   if(!mVolumeInitialized)
219      return;
220
221#if defined(__linux__)
222   AssertFatal(0, "SDL CD not implemented");
223#endif
224
225   mVolumeInitialized = false;
226#endif   // !defined(__FreeBSD__)
227}
228
229//------------------------------------------------------------------------------
230void UnixRedBookDevice::setLastError(const char * error)
231{
232#if !defined(__FreeBSD__)
233   RedBook::setLastError(error);
234#endif   // !defined(__FreeBSD__)
235}
236
237//------------------------------------------------------------------------------
238void InstallRedBookDevices()
239{
240
241}
242
243//------------------------------------------------------------------------------
244void PollRedbookDevices()
245{
246#if !defined(__FreeBSD__)
247   UnixRedBookDevice *device = dynamic_cast<UnixRedBookDevice*>(RedBook::getCurrentDevice());
248
249   if (device == NULL || !device->isPlaying())
250      return;
251
252   static const U32 PollDelay = 1000;
253
254   static U32 lastPollTime = 0;
255   U32 curTime = Platform::getVirtualMilliseconds();
256
257   if (lastPollTime != 0 &&
258      (curTime - lastPollTime) < PollDelay)
259      return;
260
261   lastPollTime = curTime;
262
263   if (device->isPlaying())
264   {
265      device->updateStatus();
266      if (!device->isPlaying())
267         RedBook::handleCallback(RedBook::PlayFinished);
268   }
269#endif   // !defined(__FreeBSD__)
270}
271