x86UNIXRedbook.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. dsize_t deviceNameLen = dStrlen(deviceName) + 1;
  92. mDeviceName = new char[deviceNameLen];
  93. dStrcpy(mDeviceName, deviceName, deviceNameLen);
  94. #endif // !defined(__FreeBSD__)
  95. }
  96. //------------------------------------------------------------------------------
  97. bool UnixRedBookDevice::open()
  98. {
  99. return false; // TODO LINUX
  100. }
  101. //------------------------------------------------------------------------------
  102. bool UnixRedBookDevice::close()
  103. {
  104. return false; // TODO LINUX
  105. }
  106. //------------------------------------------------------------------------------
  107. bool UnixRedBookDevice::play(U32 track)
  108. {
  109. return false; // TODO LINUX
  110. }
  111. //------------------------------------------------------------------------------
  112. bool UnixRedBookDevice::stop()
  113. {
  114. return false; // TODO LINUX
  115. }
  116. //------------------------------------------------------------------------------
  117. bool UnixRedBookDevice::getTrackCount(U32 * numTracks)
  118. {
  119. return false; // TODO LINUX
  120. }
  121. template <class Type>
  122. static inline Type max(Type v1, Type v2)
  123. {
  124. #if !defined(__FreeBSD__)
  125. if (v1 <= v2)
  126. return v2;
  127. else
  128. return v1;
  129. #endif // !defined(__FreeBSD__)
  130. }
  131. //------------------------------------------------------------------------------
  132. bool UnixRedBookDevice::getVolume(F32 * volume)
  133. {
  134. #if !defined(__FreeBSD__)
  135. if(!mAcquired)
  136. {
  137. setLastError("Device has not been acquired");
  138. return(false);
  139. }
  140. if(!mVolumeInitialized)
  141. {
  142. setLastError("Volume failed to initialize");
  143. return(false);
  144. }
  145. #if defined(__linux__)
  146. AssertFatal(0, "SDL CD not implemented");
  147. return true;
  148. #else
  149. return(false);
  150. #endif
  151. #endif // !defined(__FreeBSD__)
  152. }
  153. //------------------------------------------------------------------------------
  154. bool UnixRedBookDevice::setVolume(F32 volume)
  155. {
  156. #if !defined(__FreeBSD__)
  157. if(!mAcquired)
  158. {
  159. setLastError("Device has not been acquired");
  160. return(false);
  161. }
  162. if(!mVolumeInitialized)
  163. {
  164. setLastError("Volume failed to initialize");
  165. return(false);
  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. void UnixRedBookDevice::openVolume()
  177. {
  178. #if !defined(__FreeBSD__)
  179. // Its unforunate that we have to do it this way, but SDL does not currently
  180. // support setting CD audio volume
  181. #if defined(__linux__)
  182. AssertFatal(0, "SDL CD not implemented");
  183. #else
  184. setLastError("Volume failed to initialize");
  185. #endif
  186. #endif // !defined(__FreeBSD__)
  187. }
  188. void UnixRedBookDevice::closeVolume()
  189. {
  190. #if !defined(__FreeBSD__)
  191. if(!mVolumeInitialized)
  192. return;
  193. #if defined(__linux__)
  194. AssertFatal(0, "SDL CD not implemented");
  195. #endif
  196. mVolumeInitialized = false;
  197. #endif // !defined(__FreeBSD__)
  198. }
  199. //------------------------------------------------------------------------------
  200. void UnixRedBookDevice::setLastError(const char * error)
  201. {
  202. #if !defined(__FreeBSD__)
  203. RedBook::setLastError(error);
  204. #endif // !defined(__FreeBSD__)
  205. }
  206. //------------------------------------------------------------------------------
  207. void InstallRedBookDevices()
  208. {
  209. }
  210. //------------------------------------------------------------------------------
  211. void PollRedbookDevices()
  212. {
  213. #if !defined(__FreeBSD__)
  214. UnixRedBookDevice *device = dynamic_cast<UnixRedBookDevice*>(RedBook::getCurrentDevice());
  215. if (device == NULL || !device->isPlaying())
  216. return;
  217. static const U32 PollDelay = 1000;
  218. static U32 lastPollTime = 0;
  219. U32 curTime = Platform::getVirtualMilliseconds();
  220. if (lastPollTime != 0 &&
  221. (curTime - lastPollTime) < PollDelay)
  222. return;
  223. lastPollTime = curTime;
  224. if (device->isPlaying())
  225. {
  226. device->updateStatus();
  227. if (!device->isPlaying())
  228. RedBook::handleCallback(RedBook::PlayFinished);
  229. }
  230. #endif // !defined(__FreeBSD__)
  231. }