Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

572 lines
15 KiB

  1. /*******************************Module*Header*********************************\
  2. * Module Name: cddrvr.c
  3. *
  4. * Installalble driver stuff for the
  5. *
  6. * Media Control Architecture Redbook Audio Device Driver
  7. *
  8. * Created: 10/7/90
  9. * Author: DLL (DavidLe)
  10. *
  11. * History:
  12. *
  13. * Copyright (c) 1990-1999 Microsoft Corporation
  14. *
  15. \******************************************************************************/
  16. #include <stdlib.h>
  17. #include <windows.h>
  18. #include <mmsystem.h>
  19. #include <mmddk.h>
  20. #include <winreg.h>
  21. #include "mcicda.h"
  22. #include "cda.h"
  23. #include "cdio.h"
  24. HANDLE InitCritSection = NULL;
  25. #define MYREGSTR_PATH_MEDIA TEXT("SYSTEM\\CurrentControlSet\\Control\\MediaResources")
  26. static TCHAR gszRegstrCDAPath[] = MYREGSTR_PATH_MEDIA TEXT("\\mci\\cdaudio");
  27. static TCHAR gszUnitEnum[] = TEXT("%s\\unit %d");
  28. static TCHAR gszSettingsKey[] = TEXT("Volume Settings");
  29. static TCHAR gszDefaultCDA[] = TEXT("Default Drive");
  30. int PASCAL FAR CDAConfig (HWND hwndParent);
  31. DWORD NEAR PASCAL drvOpen (LPMCI_OPEN_DRIVER_PARMS lpDrvOpen);
  32. UINT CDAudio_GetDefDrive();
  33. typedef BOOL (WINAPI *SHOWMMCPLPROPSHEETW)(HWND hwndParent,
  34. LPCWSTR szPropSheetID,
  35. LPWSTR szTabName,
  36. LPWSTR szCaption);
  37. #define _MAX_PATH 260
  38. /*****************************************************************************
  39. @doc INTERNAL MCICDA
  40. @api DWORD NEAR PASCAL | drvOpen |
  41. @parm LPMCI_OPEN_DRIVER_PARMS | lpDrvOpen |
  42. @rdesc
  43. @comm
  44. *****************************************************************************/
  45. #define CONFIG_ID 10000L // Use the hiword of dwDriverID to identify
  46. // config. opens
  47. DWORD NEAR PASCAL drvOpen (LPMCI_OPEN_DRIVER_PARMS lpDrvOpen)
  48. {
  49. DWORD dwRes;
  50. if (lpDrvOpen == NULL)
  51. {
  52. dwRes = CONFIG_ID;
  53. }
  54. else
  55. {
  56. long lSupportInfo;
  57. int numdrives;
  58. LPCTSTR lpstrBuf;
  59. DID didDrive;
  60. PINSTDATA pInst;
  61. /*
  62. Sent to the driver when it is opened.
  63. dwDriverID is 0L.
  64. lParam1 is a far pointer to a zero-terminated string
  65. containing the name used to open the driver.
  66. lParam2 is passed through from the drvOpen call.
  67. Return 0L to FAIL the open.
  68. */
  69. lpDrvOpen->wType = MCI_DEVTYPE_CD_AUDIO;
  70. lpDrvOpen->wCustomCommandTable = MCI_TABLE_NOT_PRESENT;
  71. EnterCrit (InitCritSection);
  72. numdrives = CDA_init_audio ();
  73. LeaveCrit (InitCritSection);
  74. dprintf2(("Number of CD drives found = %d", numdrives));
  75. if (numdrives <= 0)
  76. return 0;
  77. if (numdrives > 1)
  78. {
  79. lpstrBuf = lpDrvOpen->lpstrParams;
  80. while (*lpstrBuf == ' ')
  81. ++lpstrBuf;
  82. if (*lpstrBuf == '\0')
  83. {
  84. didDrive = CDAudio_GetDefDrive();
  85. }
  86. else
  87. didDrive = *lpstrBuf - '0';
  88. if (didDrive >= MCIRBOOK_MAX_DRIVES)
  89. return 0;
  90. } else
  91. didDrive = 0;
  92. // get the next drive with audio support
  93. for (;didDrive < numdrives;didDrive++)
  94. {
  95. EnterCrit (CdInfo[didDrive].DeviceCritSec);
  96. if (!CDA_open (didDrive)) {
  97. LeaveCrit (CdInfo[didDrive].DeviceCritSec);
  98. continue;
  99. }
  100. lSupportInfo = CDA_get_support_info(didDrive);
  101. if (lSupportInfo & SUPPORTS_REDBOOKAUDIO)
  102. break;
  103. CDA_close (didDrive);
  104. LeaveCrit (CdInfo[didDrive].DeviceCritSec);
  105. }
  106. // no drives with audio
  107. if (! (lSupportInfo & SUPPORTS_REDBOOKAUDIO))
  108. {
  109. return 0;
  110. }
  111. // Future domain driver will not fail previous checks if no data cable
  112. if ((lSupportInfo & DISC_IN_DRIVE) &&
  113. CDA_time_info (didDrive, NULL, NULL) != COMMAND_SUCCESSFUL)
  114. {
  115. CDA_close (didDrive);
  116. LeaveCrit (CdInfo[didDrive].DeviceCritSec);
  117. return 0;
  118. }
  119. CDA_close (didDrive);
  120. LeaveCrit (CdInfo[didDrive].DeviceCritSec);
  121. if ((pInst = (PINSTDATA)LocalAlloc(LPTR, sizeof(INSTDATA))) == NULL)
  122. {
  123. return 0;
  124. }
  125. pInst->uMCIDeviceID = lpDrvOpen->wDeviceID;
  126. pInst->uDevice = didDrive;
  127. mciSetDriverData (lpDrvOpen->wDeviceID, (DWORD_PTR)pInst);
  128. dwRes = lpDrvOpen->wDeviceID;
  129. }
  130. return dwRes;
  131. }
  132. UINT CDAudio_GetDefDrive()
  133. {
  134. HKEY hkTmp;
  135. DWORD uDrive = 0;
  136. if (RegOpenKey(HKEY_LOCAL_MACHINE
  137. , gszRegstrCDAPath
  138. , &hkTmp ) == ERROR_SUCCESS)
  139. {
  140. DWORD cb = sizeof(DWORD);
  141. RegQueryValueEx(hkTmp
  142. , gszDefaultCDA
  143. , NULL
  144. , NULL
  145. , (LPBYTE)&uDrive
  146. , &cb);
  147. RegCloseKey(hkTmp);
  148. }
  149. return (UINT)uDrive;
  150. }
  151. typedef struct {
  152. DWORD unit;
  153. DWORD dwVol;
  154. } CDAREG, *PCDAREG;
  155. DWORD CDAudio_GetUnitVolume(
  156. UINT uDrive)
  157. {
  158. HKEY hkTmp;
  159. CDAREG cda;
  160. TCHAR szRegstrCDAudio[_MAX_PATH];
  161. wsprintf (szRegstrCDAudio, gszUnitEnum, gszRegstrCDAPath, uDrive);
  162. cda.dwVol = 0xff;
  163. if (RegOpenKey(HKEY_LOCAL_MACHINE,szRegstrCDAudio,&hkTmp) == ERROR_SUCCESS)
  164. {
  165. DWORD cbCDA = sizeof(CDAREG);
  166. RegQueryValueEx(hkTmp
  167. , gszSettingsKey
  168. , NULL
  169. , NULL
  170. , (LPBYTE)&cda
  171. , &cbCDA);
  172. RegCloseKey(hkTmp);
  173. }
  174. return cda.dwVol;
  175. }
  176. /***************************************************************************
  177. *
  178. * @doc INTERNAL
  179. *
  180. * @func DWORD | DriverProc | The entry point for an installable driver.
  181. *
  182. * @parm DWORD | dwDriverId | For most messages, dwDriverId is the DWORD
  183. * value that the driver returns in response to a DRV_OPEN message.
  184. * Each time that the driver is opened, through the DrvOpen API,
  185. * the driver receives a DRV_OPEN message and can return an
  186. * arbitrary, non-zero, value. The installable driver interface
  187. * saves this value and returns a unique driver handle to the
  188. * application. Whenever the application sends a message to the
  189. * driver using the driver handle, the interface routes the message
  190. * to this entry point and passes the corresponding dwDriverId.
  191. *
  192. * This mechanism allows the driver to use the same or different
  193. * identifiers for multiple opens but ensures that driver handles
  194. * are unique at the application interface layer.
  195. *
  196. * The following messages are not related to a particular open
  197. * instance of the driver.
  198. *
  199. * DRV_LOAD, DRV_FREE, DRV_ENABLE, DRV_DISABLE, DRV_OPEN
  200. *
  201. * @parm HANDLE | hDriver | This is the handle returned to the
  202. * application by the driver interface.
  203. *
  204. * @parm UINT | message | The requested action to be performed. Message
  205. * values below DRV_RESERVED are used for globally defined messages.
  206. * Message values from DRV_RESERVED to DRV_USER are used for
  207. * defined driver portocols. Messages above DRV_USER are used
  208. * for driver specific messages.
  209. *
  210. * @parm DWORD | dwParam1 | Data for this message. Defined separately for
  211. * each message
  212. *
  213. * @parm DWORD | dwParam2 | Data for this message. Defined separately for
  214. * each message
  215. *
  216. * @rdesc Defined separately for each message.
  217. *
  218. ***************************************************************************/
  219. LRESULT DriverProc (DWORD_PTR dwDriverID, HANDLE hDriver, UINT message,
  220. LPARAM lParam1, LPARAM lParam2)
  221. {
  222. LRESULT dwRes;
  223. PINSTDATA pInst;
  224. TCHAR szMutex[32];
  225. switch (message)
  226. {
  227. // Standard, globally used messages.
  228. case DRV_LOAD:
  229. {
  230. int i;
  231. InitCritSection = CreateMutex (NULL, FALSE, TEXT ("MCICDA_InitCritSection"));
  232. for ( i = 0; i < MCIRBOOK_MAX_DRIVES; i++ ) {
  233. CdInfo[i].DeviceCritSec = NULL;
  234. wsprintf (szMutex, TEXT ("%s%ld"), TEXT ("MCICDA_DeviceCritSec_"), i);
  235. CdInfo[i].DeviceCritSec = CreateMutex (NULL, FALSE, szMutex);
  236. }
  237. #if DBG
  238. DebugLevel = GetProfileIntW(L"mmdebug", L"mcicda", 0);
  239. #endif
  240. dprintf2(("DRV_LOAD"));
  241. /*
  242. Sent to the driver when it is loaded. Always the first
  243. message received by a driver.
  244. dwDriverID is 0L.
  245. lParam1 is 0L.
  246. lParam2 is 0L.
  247. Return 0L to FAIL the load.
  248. */
  249. hInstance = GetModuleHandleW( L"mcicda");
  250. dwRes = 1L;
  251. }
  252. break;
  253. case DRV_FREE:
  254. {
  255. int i;
  256. dprintf2(("DRV_FREE"));
  257. /*
  258. Sent to the driver when it is about to be discarded. This
  259. will always be the last message received by a driver before
  260. it is freed.
  261. dwDriverID is 0L.
  262. lParam1 is 0L.
  263. lParam2 is 0L.
  264. Return value is IGNORED.
  265. */
  266. dwRes = 1L;
  267. if (InitCritSection) {
  268. CloseHandle (InitCritSection);
  269. InitCritSection = NULL;
  270. }
  271. for ( i = 0; i < MCIRBOOK_MAX_DRIVES; i++ ) {
  272. if (CdInfo[i].DeviceCritSec) {
  273. CloseHandle (CdInfo[i].DeviceCritSec);
  274. CdInfo[i].DeviceCritSec = NULL;
  275. }
  276. }
  277. }
  278. break;
  279. case DRV_OPEN:
  280. dprintf2(("DRV_OPEN"));
  281. dwRes = drvOpen((LPMCI_OPEN_DRIVER_PARMS)lParam2);
  282. break;
  283. case DRV_CLOSE:
  284. dprintf2(("DRV_CLOSE"));
  285. /*
  286. Sent to the driver when it is closed. Drivers are unloaded
  287. when the close count reaches zero.
  288. dwDriverID is the driver identifier returned from the
  289. corresponding DRV_OPEN.
  290. lParam1 is passed through from the drvOpen call.
  291. lParam2 is passed through from the drvOpen call.
  292. Return 0L to FAIL the close.
  293. */
  294. dwRes = 1L;
  295. break;
  296. case DRV_ENABLE:
  297. dprintf2(("DRV_ENABLE"));
  298. /*
  299. Sent to the driver when the driver is loaded or reloaded
  300. and whenever windows is enabled. Drivers should only
  301. hook interrupts or expect ANY part of the driver to be in
  302. memory between enable and disable messages
  303. dwDriverID is 0L.
  304. lParam1 is 0L.
  305. lParam2 is 0L.
  306. Return value is ignored.
  307. */
  308. dwRes = 1L;
  309. break;
  310. case DRV_DISABLE:
  311. dprintf2(("DRV_DISABLE"));
  312. /*
  313. Sent to the driver before the driver is freed.
  314. and whenever windows is disabled
  315. dwDriverID is 0L.
  316. lParam1 is 0L.
  317. lParam2 is 0L.
  318. Return value is ignored.
  319. */
  320. dwRes = 1L;
  321. break;
  322. case DRV_QUERYCONFIGURE:
  323. dprintf2(("DRV_QUERYCONFIGURE"));
  324. /*
  325. Sent to the driver so that applications can
  326. determine whether the driver supports custom
  327. configuration. The driver should return a
  328. non-zero value to indicate that configuration
  329. is supported.
  330. dwDriverID is the value returned from the DRV_OPEN
  331. call that must have succeeded before this message
  332. was sent.
  333. lParam1 is passed from the app and is undefined.
  334. lParam2 is passed from the app and is undefined.
  335. return 0L to indicate configuration NOT supported.
  336. */
  337. dwRes = 1L;
  338. break;
  339. case DRV_CONFIGURE:
  340. dprintf2(("DRV_CONFIGURE"));
  341. /*
  342. Sent to the driver so that it can display a custom
  343. configuration dialog box.
  344. lParam1 is passed from the app. and should contain
  345. the parent window handle in the loword.
  346. lParam2 is passed from the app and is undefined.
  347. return value is undefined.
  348. Drivers should create their own section in
  349. system.ini. The section name should be the driver
  350. name.
  351. */
  352. if (lParam1)
  353. {
  354. dwRes = CDAConfig((HWND)LOWORD (lParam1));
  355. } else {
  356. dwRes = DRVCNF_CANCEL;
  357. }
  358. break;
  359. default:
  360. if (dwDriverID != CONFIG_ID &&
  361. message >= DRV_MCI_FIRST && message <= DRV_MCI_LAST) {
  362. dwRes = CD_MCI_Handler ((MCIDEVICEID)dwDriverID, message,
  363. lParam1, lParam2);
  364. } else {
  365. dwRes = DefDriverProc(dwDriverID, hDriver, message,
  366. lParam1, lParam2);
  367. }
  368. break;
  369. }
  370. return (LRESULT)dwRes;
  371. }
  372. /*****************************Private*Routine******************************\
  373. * EnterCrit
  374. *
  375. *
  376. *
  377. * History:
  378. * dd-mm-94 - StephenE - Created
  379. *
  380. \**************************************************************************/
  381. void
  382. EnterCrit(
  383. HANDLE hMutex
  384. )
  385. {
  386. dprintf4(( "Entering Crit Sect 0x%X", hMutex ));
  387. if (hMutex)
  388. {
  389. WaitForSingleObject (hMutex, INFINITE);
  390. }
  391. }
  392. /*****************************Private*Routine******************************\
  393. * LeaveCrit
  394. *
  395. *
  396. *
  397. * History:
  398. * dd-mm-94 - StephenE - Created
  399. *
  400. \**************************************************************************/
  401. void
  402. LeaveCrit(
  403. HANDLE hMutex
  404. )
  405. {
  406. dprintf4(( "Leaving Crit Sect 0x%X", hMutex ));
  407. if (hMutex)
  408. {
  409. ReleaseMutex (hMutex);
  410. }
  411. }
  412. /*****************************************************************************
  413. @doc INTERNAL MCICDA
  414. @api int | CDAConfig |
  415. @parm HWND | hwndParent |
  416. @rdesc
  417. @comm
  418. *****************************************************************************/
  419. int CDAConfig (HWND hwndParent)
  420. {
  421. static HWND hwndPrevParent = NULL;
  422. WCHAR szCaptionW[ 128 ];
  423. // We need only a unicode version of the caption (for FindWindow()
  424. // and ShowMMCPLPropertySheetW(), which are unicode-enabled).
  425. //
  426. LoadStringW(hInstance,IDS_CDMUSICCAPTION,szCaptionW,cchLENGTH(szCaptionW));
  427. if (hwndPrevParent)
  428. {
  429. BringWindowToTop(FindWindowW(NULL, szCaptionW));
  430. }
  431. else
  432. {
  433. HINSTANCE h;
  434. SHOWMMCPLPROPSHEETW fn;
  435. static TCHAR aszMMSystemW[] = TEXT("MMSYS.CPL");
  436. static char aszShowPropSheetA[] = "ShowMMCPLPropertySheetW";
  437. static WCHAR aszCDAudioW[] = L"CDAUDIO";
  438. WCHAR szCDMusicW[64];
  439. LoadStringW(hInstance, IDS_CDMUSIC, szCDMusicW, cchLENGTH(szCDMusicW));
  440. h = LoadLibrary (aszMMSystemW);
  441. if (h)
  442. {
  443. fn = (SHOWMMCPLPROPSHEETW)GetProcAddress(h, aszShowPropSheetA);
  444. if (fn)
  445. {
  446. BOOL f;
  447. hwndPrevParent = hwndParent;
  448. CDA_init_audio ();
  449. f = fn(hwndParent, aszCDAudioW, szCDMusicW, szCaptionW);
  450. CDA_terminate_audio ();
  451. hwndPrevParent = NULL;
  452. }
  453. FreeLibrary(h);
  454. }
  455. }
  456. return DRVCNF_OK;
  457. }