PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/cdex1/tags/cdex_130_b1/cdexos/MCI_CD.cpp

#
C++ | 364 lines | 234 code | 61 blank | 69 comment | 24 complexity | 1dc269cbfc33ef0e36dd111371fc058b MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-2.0, BSD-3-Clause, AGPL-1.0, CC-BY-SA-3.0, GPL-3.0
  1. /*
  2. ** Copyright (C) 1999 Albert L. Faber
  3. **
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include "StdAfx.h"
  19. #include "MCI_CD.h"
  20. #include "Config.h"
  21. #include <mmsystem.h>
  22. static BOOL gs_bDebug=::GetPrivateProfileInt("Debug","DebugMCITOC",FALSE,"CDex.ini");
  23. // if (quickbuf[5+j*8] & 4) vDataTrack[i] = 1; else vDataTrack[i] = 0;
  24. /* if (nHighest > 2) {
  25. if (vDataTrack[nHighest] && !vDataTrack[nHighest-1]) {
  26. bool done = false;
  27. int thisdrive = Form8->ComboBox2->ItemIndex;
  28. j = vStopSector[1];
  29. if ((unsigned char)cdromunits[thisdrive] != 0xFF) done = ANALOGGetLastAudioTrackLenght(cdromunits[thisdrive],nHighest - 1, j);
  30. i = 0;
  31. while (!done && i < 27) {
  32. j = vStopSector[1];
  33. if (((unsigned char)cdromunits[i] != 0xFF) && i != thisdrive) done = ANALOGGetLastAudioTrackLenght(cdromunits[i],nHighest - 1, j);
  34. i++;
  35. }
  36. if (done) {
  37. vStopSector[nHighest-1] = vStartSector[nHighest-1]+j;
  38. multimediadisc = vStopSector[nHighest-1];
  39. }
  40. }
  41. } */
  42. static BOOL GetMCILastudioTrackLenght(char chDriveLetter,int tracknumber, int &tracklength)
  43. {
  44. MCI_INFO_PARMS sMCIInfo;
  45. char szMCIReturnString[80];
  46. MCI_OPEN_PARMS sMCIOpen;
  47. MCI_STATUS_PARMS mciStatusParms;
  48. char zDevice[4];
  49. DWORD nErr;
  50. int firsttracklength;
  51. // Create device string
  52. sprintf(zDevice, "%c:",chDriveLetter);
  53. // Set MCI open parameters
  54. sMCIOpen.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_CD_AUDIO;
  55. sMCIOpen.lpstrElementName = zDevice;
  56. // Try to open the device
  57. nErr = mciSendCommand(NULL, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_SHAREABLE | MCI_OPEN_TYPE_ID | MCI_OPEN_ELEMENT,(DWORD)&sMCIOpen);
  58. if (nErr)
  59. return false;
  60. sMCIInfo.lpstrReturn = szMCIReturnString;
  61. sMCIInfo.dwRetSize = 79;
  62. mciStatusParms.dwItem = MCI_STATUS_POSITION;
  63. // mciStatusParms.dwTrack = 2;
  64. mciStatusParms.dwTrack = tracknumber;
  65. if (mciSendCommand(sMCIOpen.wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK, (DWORD)&mciStatusParms))
  66. goto errorjumpout;
  67. firsttracklength = (DWORD)MCI_MSF_MINUTE(mciStatusParms.dwReturn) * 4500 +
  68. (DWORD)MCI_MSF_SECOND(mciStatusParms.dwReturn) * 75 +
  69. (DWORD)MCI_MSF_FRAME(mciStatusParms.dwReturn)-151;
  70. if (firsttracklength != tracklength)
  71. goto errorjumpout;
  72. mciStatusParms.dwItem = MCI_STATUS_LENGTH;
  73. if (mciSendCommand(sMCIOpen.wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK,(DWORD)&mciStatusParms))
  74. goto errorjumpout;
  75. tracklength = (DWORD)MCI_MSF_MINUTE(mciStatusParms.dwReturn) * 4500 +
  76. (DWORD)MCI_MSF_SECOND(mciStatusParms.dwReturn) * 75 +
  77. (DWORD)MCI_MSF_FRAME(mciStatusParms.dwReturn);
  78. mciSendCommand(sMCIOpen.wDeviceID, MCI_CLOSE, MCI_WAIT, NULL);
  79. return true;
  80. errorjumpout:
  81. mciSendCommand(sMCIOpen.wDeviceID, MCI_CLOSE, 0, NULL);
  82. return false;
  83. }
  84. //---------------------------------------------------------------------------
  85. short MCIGetDiskInfo( char chDriveLetter,
  86. int& nHighest,
  87. LPINT vStartSector,
  88. LPINT vStopSector,
  89. DWORD& dwTotalTime,
  90. DWORD& dwDiscID
  91. )
  92. {
  93. int i;
  94. short vDataTrack[100];
  95. MCI_INFO_PARMS sMCIInfo;
  96. char szMCIReturnString[80];
  97. MCI_OPEN_PARMS sMCIOpen;
  98. MCI_STATUS_PARMS mciStatusParms;
  99. char zDevice[4];
  100. DWORD nErr;
  101. sprintf(zDevice, "%c:",chDriveLetter);
  102. sMCIOpen.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
  103. sMCIOpen.lpstrElementName = zDevice;
  104. memset(vStartSector,0x00,101*sizeof(INT));
  105. memset(vStopSector,0x00,101*sizeof(INT));
  106. // Try to open the device
  107. nErr = mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_TYPE|MCI_OPEN_SHAREABLE|MCI_OPEN_TYPE_ID|MCI_OPEN_ELEMENT,(DWORD)&sMCIOpen);
  108. if (nErr)
  109. {
  110. nErr = mciSendCommand(NULL, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | MCI_OPEN_ELEMENT,(DWORD)&sMCIOpen);
  111. if (nErr)
  112. {
  113. char zError[256];
  114. mciGetErrorString(nErr, zError, 255);
  115. if (gs_bDebug) DebugPrintf(zError);
  116. }
  117. }
  118. sMCIInfo.lpstrReturn = szMCIReturnString;
  119. sMCIInfo.dwRetSize = 79;
  120. mciStatusParms.dwItem = MCI_STATUS_NUMBER_OF_TRACKS;
  121. if (mciSendCommand(sMCIOpen.wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD)(LPVOID) &mciStatusParms))
  122. {
  123. mciSendCommand(sMCIOpen.wDeviceID, MCI_CLOSE, 0, NULL);
  124. return 0;
  125. }
  126. nHighest = (BYTE)mciStatusParms.dwReturn;
  127. // DWORD dwTime=GetTickCount();
  128. for(i = 0; i < nHighest; i++)
  129. {
  130. // DebugPrintf("A %d",GetTickCount()-dwTime);
  131. mciStatusParms.dwItem = MCI_STATUS_POSITION;
  132. mciStatusParms.dwTrack = i+1;
  133. // Open the device
  134. if (mciSendCommand(sMCIOpen.wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK, (DWORD)(LPVOID) &mciStatusParms))
  135. {
  136. mciSendCommand(sMCIOpen.wDeviceID, MCI_CLOSE, 0, NULL);
  137. return 0;
  138. }
  139. // DebugPrintf("B %d",GetTickCount()-dwTime);
  140. vStartSector[i] = (DWORD)MCI_MSF_MINUTE(mciStatusParms.dwReturn) * 4500 +
  141. (DWORD)MCI_MSF_SECOND(mciStatusParms.dwReturn) * 75 +
  142. (DWORD)MCI_MSF_FRAME(mciStatusParms.dwReturn)-150;
  143. int nMin=MCI_MSF_MINUTE(mciStatusParms.dwReturn);
  144. int nSec=MCI_MSF_SECOND(mciStatusParms.dwReturn);
  145. int nFrm=MCI_MSF_FRAME(mciStatusParms.dwReturn);
  146. mciStatusParms.dwItem = MCI_STATUS_LENGTH;
  147. mciStatusParms.dwTrack = i+1;
  148. if (mciSendCommand(sMCIOpen.wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK,(DWORD)&mciStatusParms))
  149. {
  150. mciSendCommand(sMCIOpen.wDeviceID, MCI_CLOSE, 0, NULL);
  151. return 0;
  152. }
  153. // DebugPrintf("C %d",GetTickCount()-dwTime);
  154. vStopSector[i] = (DWORD)MCI_MSF_MINUTE(mciStatusParms.dwReturn) * 4500 +
  155. (DWORD)MCI_MSF_SECOND(mciStatusParms.dwReturn) * 75 +
  156. (DWORD)MCI_MSF_FRAME(mciStatusParms.dwReturn)-1;
  157. vStopSector[i]+=vStartSector[i];
  158. // if (i > 0)
  159. // vStopSector[i-1] = vStartSector[i] - 1;
  160. mciStatusParms.dwItem = MCI_CDA_STATUS_TYPE_TRACK;
  161. mciStatusParms.dwTrack = i;
  162. mciSendCommand (sMCIOpen.wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK | MCI_WAIT, (DWORD)(LPVOID)&mciStatusParms);
  163. // DebugPrintf("D %d",GetTickCount()-dwTime);
  164. if (mciStatusParms.dwReturn != MCI_CDA_TRACK_AUDIO)
  165. {
  166. vDataTrack[i] = 1;
  167. if (gs_bDebug) DebugPrintf("Track %d is not an Audio Track",i+1);
  168. }
  169. else
  170. {
  171. vDataTrack[i] = 0;
  172. }
  173. }
  174. // mciStatusParms.dwItem = MCI_STATUS_LENGTH;
  175. // mciStatusParms.dwTrack = nHighest;
  176. // Open the MCI device
  177. // if (mciSendCommand(sMCIOpen.wDeviceID, MCI_STATUS, MCI_STATUS_ITEM|MCI_TRACK, (DWORD)&mciStatusParms))
  178. // {
  179. // mciSendCommand(sMCIOpen.wDeviceID, MCI_CLOSE, 0, NULL);
  180. // return 0;
  181. // }
  182. // vStopSector[nHighest] = vStartSector[nHighest] +(DWORD)MCI_MSF_MINUTE(mciStatusParms.dwReturn) * 4500 +
  183. // (DWORD)MCI_MSF_SECOND(mciStatusParms.dwReturn) * 75 +
  184. // (DWORD)MCI_MSF_FRAME(mciStatusParms.dwReturn) - vStopSector[0];
  185. if (nHighest>0)
  186. {
  187. vStartSector[nHighest]=vStopSector[nHighest-1]+2;
  188. vStopSector[nHighest]=150;
  189. }
  190. // Set total time
  191. dwTotalTime = vStartSector[nHighest];
  192. // Get volume information
  193. GetVolumeInformation (zDevice,NULL,0,&dwDiscID,NULL,NULL,NULL,0);
  194. sMCIInfo.lpstrReturn = szMCIReturnString;
  195. sMCIInfo.dwRetSize = 79;
  196. // Get the CDPlayer string from the MCI device
  197. nErr = mciSendCommand (sMCIOpen.wDeviceID, MCI_INFO, MCI_INFO_MEDIA_IDENTITY | MCI_WAIT,(DWORD)&sMCIInfo);
  198. if (!nErr)
  199. {
  200. dwDiscID=atol(sMCIInfo.lpstrReturn);
  201. if (gs_bDebug) DebugPrintf("Decimal disc ID %d",dwDiscID);
  202. }
  203. // Close MCI device
  204. mciSendCommand(sMCIOpen.wDeviceID, MCI_CLOSE, MCI_WAIT, NULL);
  205. return 0x0100;
  206. }
  207. BOOL GetMCIToc(int vStartSector[101],int vStopSector[101],int& nTocEntries,DWORD& dwVolumeID)
  208. {
  209. BOOL bFound=FALSE;
  210. int vASPIStartSector[101];
  211. int i;
  212. if (gs_bDebug) DebugPrintf("Entering GetMCIToc");
  213. if (nTocEntries==0)
  214. return FALSE;
  215. // Initialze nTocEntries;
  216. nTocEntries=0;
  217. // Make a copy of the original start sectors
  218. memcpy(vASPIStartSector,vStartSector,sizeof(vASPIStartSector));
  219. // Loop through all CDROM devices
  220. for (char chDrive='C';(chDrive<='Z') && (bFound==FALSE);chDrive++)
  221. {
  222. // Construct root directory drive letter
  223. CString strRoot = CString(chDrive) + ":\\";
  224. // Get logical drive number
  225. UINT lDrive = GetDriveType(strRoot);
  226. // Check if this is a CDROM-DRIVE
  227. if (lDrive == DRIVE_CDROM)
  228. {
  229. DWORD dwTotalTime=0;
  230. DWORD dwVolID=0;
  231. int nHighest=0;
  232. if (gs_bDebug) DebugPrintf("CD-DRIVE %c is a CDROM device",chDrive);
  233. if (MCIGetDiskInfo(chDrive,nHighest,vStartSector,vStopSector,dwTotalTime,dwVolID))
  234. {
  235. int nLastTrackLength=0;
  236. // BOOL bRet=GetMCILastudioTrackLenght(chDrive,nHighest,nLastTrackLength);
  237. // Check if this is the one we searched for
  238. if (
  239. ( (vASPIStartSector[1]==vStartSector[1]) ||
  240. (vASPIStartSector[1]==vStartSector[1]+11250) ))
  241. // if (vASPIStartSector[1]==vStartSector[1])
  242. {
  243. if (gs_bDebug) DebugPrintf("Found matching TOC for drive %c Tracks from %d to %d",chDrive,0,nHighest);
  244. bFound=TRUE;
  245. dwVolumeID=dwVolID;
  246. if (gs_bDebug) DebugPrintf("Found matching TOC for drive %c ",chDrive);
  247. for (i=0;i<=nHighest;i++)
  248. {
  249. if (gs_bDebug) DebugPrintf("Track %d: Start %d Stop %d",i+1,vStartSector[i],vStopSector[i]);
  250. }
  251. if (gs_bDebug) DebugPrintf("Total Lenght: %d",dwTotalTime);
  252. if (gs_bDebug) DebugPrintf("Disc ID: %08X",dwVolumeID);
  253. nTocEntries=nHighest;
  254. if (gs_bDebug) DebugPrintf("NumTOC Entries is : %d",nTocEntries);
  255. }
  256. }
  257. }
  258. }
  259. if (gs_bDebug) DebugPrintf("Leaving GetMCIToc");
  260. return bFound;
  261. }
  262. /*
  263. int GetAnalogDiskInfo(char chDriveLetter)
  264. {
  265. int i;
  266. int nLowest=0;
  267. int nHighest=0;
  268. int vStartSector[101],vStopSector[101];
  269. DWORD dwTotalTime=0;
  270. DWORD dwDiscID=0;
  271. if (MCIGetDiskInfo( chDriveLetter,
  272. nLowest,nHighest,vStartSector,vStopSector,dwTotalTime,dwDiscID))
  273. {
  274. for (i=nLowest;i<=nHighest;i++)
  275. {
  276. if (gs_bDebug) DebugPrintf("Track %d: Start %d",i,vStartSector[i]);
  277. }
  278. if (gs_bDebug) DebugPrintf("Total Lenght: %d",dwTotalTime);
  279. if (gs_bDebug) DebugPrintf("Disc ID: %08X",dwDiscID);
  280. } else {
  281. if (!nLowest) if (gs_bDebug) DebugPrintf("Usage: TOC <CD-ROM driveletter>");
  282. else if (gs_bDebug) DebugPrintf("No disc inserted or disc in use by other program");
  283. }
  284. return 0;
  285. }
  286. //---------------------------------------------------------------------------
  287. */