x86UNIXRedbook.cpp 7.0 KB

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