Torque3D Documentation / _generateds / x86UNIXRedbook.cpp

x86UNIXRedbook.cpp

Engine/source/platformX86UNIX/x86UNIXRedbook.cpp

More...

Classes:

Public Functions

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   mDeviceName = new char[dStrlen(deviceName) + 1];
106   dStrcpy(mDeviceName, deviceName);
107#endif   // !defined(__FreeBSD__)
108}
109
110//------------------------------------------------------------------------------
111bool UnixRedBookDevice::open()
112{
113   return false; // TODO LINUX
114}
115
116//------------------------------------------------------------------------------
117bool UnixRedBookDevice::close()
118{
119   return false; // TODO LINUX
120}
121
122//------------------------------------------------------------------------------
123bool UnixRedBookDevice::play(U32 track)
124{
125   return false; // TODO LINUX
126}
127
128//------------------------------------------------------------------------------
129bool UnixRedBookDevice::stop()
130{
131   return false; // TODO LINUX
132}
133
134//------------------------------------------------------------------------------
135bool UnixRedBookDevice::getTrackCount(U32 * numTracks)
136{
137   return false; // TODO LINUX
138}
139
140template <class Type>
141static inline Type max(Type v1, Type v2)
142{
143#if !defined(__FreeBSD__)
144   if (v1 <= v2)
145      return v2;
146   else
147      return v1;
148#endif   // !defined(__FreeBSD__)
149}
150//------------------------------------------------------------------------------
151bool UnixRedBookDevice::getVolume(F32 * volume)
152{
153#if !defined(__FreeBSD__)
154   if(!mAcquired)
155   {
156      setLastError("Device has not been acquired");
157      return(false);
158   }
159
160   if(!mVolumeInitialized)
161   {
162      setLastError("Volume failed to initialize");
163      return(false);
164   }
165
166#if defined(__linux__)
167   AssertFatal(0, "SDL CD not implemented");
168   return true;
169#else
170   return(false);
171#endif
172#endif   // !defined(__FreeBSD__)
173}
174
175//------------------------------------------------------------------------------
176bool UnixRedBookDevice::setVolume(F32 volume)
177{
178#if !defined(__FreeBSD__)
179   if(!mAcquired)
180   {
181      setLastError("Device has not been acquired");
182      return(false);
183   }
184
185   if(!mVolumeInitialized)
186   {
187      setLastError("Volume failed to initialize");
188      return(false);
189   }
190
191#if defined(__linux__)
192   AssertFatal(0, "SDL CD not implemented");
193   return true;
194#else
195   return(false);
196#endif
197#endif   // !defined(__FreeBSD__)
198}
199
200//------------------------------------------------------------------------------
201void UnixRedBookDevice::openVolume()
202{
203#if !defined(__FreeBSD__)
204// Its unforunate that we have to do it this way, but SDL does not currently
205// support setting CD audio volume
206#if defined(__linux__)
207   AssertFatal(0, "SDL CD not implemented");
208#else
209   setLastError("Volume failed to initialize");
210#endif
211#endif   // !defined(__FreeBSD__)
212}
213
214void UnixRedBookDevice::closeVolume()
215{
216#if !defined(__FreeBSD__)
217   if(!mVolumeInitialized)
218      return;
219
220#if defined(__linux__)
221   AssertFatal(0, "SDL CD not implemented");
222#endif
223
224   mVolumeInitialized = false;
225#endif   // !defined(__FreeBSD__)
226}
227
228//------------------------------------------------------------------------------
229void UnixRedBookDevice::setLastError(const char * error)
230{
231#if !defined(__FreeBSD__)
232   RedBook::setLastError(error);
233#endif   // !defined(__FreeBSD__)
234}
235
236//------------------------------------------------------------------------------
237void InstallRedBookDevices()
238{
239
240}
241
242//------------------------------------------------------------------------------
243void PollRedbookDevices()
244{
245#if !defined(__FreeBSD__)
246   UnixRedBookDevice *device = dynamic_cast<UnixRedBookDevice*>(RedBook::getCurrentDevice());
247
248   if (device == NULL || !device->isPlaying())
249      return;
250
251   static const U32 PollDelay = 1000;
252
253   static U32 lastPollTime = 0;
254   U32 curTime = Platform::getVirtualMilliseconds();
255
256   if (lastPollTime != 0 &&
257      (curTime - lastPollTime) < PollDelay)
258      return;
259
260   lastPollTime = curTime;
261
262   if (device->isPlaying())
263   {
264      device->updateStatus();
265      if (!device->isPlaying())
266         RedBook::handleCallback(RedBook::PlayFinished);
267   }
268#endif   // !defined(__FreeBSD__)
269}
270