3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS Multimedia
5 * FILE: lib/mmdrv/utils.c
6 * PURPOSE: Multimedia User Mode Driver (utility functions)
7 * PROGRAMMER: Andrew Greenwood
9 * Jan 30, 2004: Imported into ReactOS tree
17 typedef struct _DEVICE_LIST
19 struct _DEVICE_LIST
*Next
;
22 PVOID DeviceInstanceData
;
23 ULONG DeviceInstanceDataSize
;
25 } DEVICE_LIST
, *PDEVICE_LIST
;
27 PDEVICE_LIST DeviceList
;
30 DWORD
TranslateStatus(void)
32 switch(GetLastError())
35 case ERROR_IO_PENDING
:
36 return MMSYSERR_NOERROR
;
39 return MMSYSERR_ALLOCATED
;
41 case ERROR_NOT_SUPPORTED
:
42 case ERROR_INVALID_FUNCTION
:
43 return MMSYSERR_NOTSUPPORTED
;
45 case ERROR_NOT_ENOUGH_MEMORY
:
46 return MMSYSERR_NOMEM
;
48 case ERROR_ACCESS_DENIED
:
49 return MMSYSERR_BADDEVICEID
;
51 case ERROR_INSUFFICIENT_BUFFER
:
52 return MMSYSERR_INVALPARAM
;
55 return MMSYSERR_ERROR
;
61 MMRESULT
OpenDevice(UINT DeviceType
, DWORD ID
, PHANDLE pDeviceHandle
,
64 DPRINT("OpenDevice()\n");
65 WCHAR DeviceName
[SOUND_MAX_DEVICE_NAME
];
66 *pDeviceHandle
= INVALID_HANDLE_VALUE
;
68 if (ID
> SOUND_MAX_DEVICES
)
69 return MMSYSERR_BADDEVICEID
;
74 wsprintf(DeviceName
, L
"\\\\.%ls%d", WAVE_OUT_DEVICE_NAME_U
+ strlen("\\Device"), ID
);
77 wsprintf(DeviceName
, L
"\\\\.%ls%d", WAVE_IN_DEVICE_NAME_U
+ strlen("\\Device"), ID
);
80 wsprintf(DeviceName
, L
"\\\\.%ls%d", MIDI_OUT_DEVICE_NAME_U
+ strlen("\\Device"), ID
);
83 wsprintf(DeviceName
, L
"\\\\.%ls%d", MIDI_IN_DEVICE_NAME_U
+ strlen("\\Device"), ID
);
86 wsprintf(DeviceName
, L
"\\\\.%ls%d", AUX_DEVICE_NAME_U
+ strlen("\\Device"), ID
);
89 DPRINT("No Auido Device Found");
90 return MMSYSERR_BADDEVICEID
; /* Maybe we should change error code */
93 DPRINT("Attempting to open %S\n", DeviceName
);
95 *pDeviceHandle
= CreateFile(DeviceName
, Access
, FILE_SHARE_WRITE
, NULL
,
96 OPEN_EXISTING
, Access
!= GENERIC_READ
? FILE_FLAG_OVERLAPPED
: 0,
99 DPRINT("DeviceHandle == 0x%x\n", (int)*pDeviceHandle
);
101 if (pDeviceHandle
== INVALID_HANDLE_VALUE
)
102 return TranslateStatus();
104 return MMSYSERR_NOERROR
;
108 // DEVICE LIST MANAGEMENT
111 BOOL
AddDeviceToList(PDEVICE_LIST
* pList
, DWORD DeviceType
, DWORD CardIndex
,
114 PDEVICE_LIST pNewDevice
;
116 DPRINT("AddDeviceToList()\n");
118 pNewDevice
= (PDEVICE_LIST
) HeapAlloc(Heap
, 0,
119 sizeof(DEVICE_LIST
) + lstrlen(Name
) * sizeof(WCHAR
));
123 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
127 pNewDevice
->DeviceType
= DeviceType
;
128 pNewDevice
->CardIndex
= CardIndex
;
129 lstrcpy(pNewDevice
->Name
, Name
);
130 pNewDevice
->DeviceInstanceData
= NULL
;
131 pNewDevice
->Next
= *pList
;
134 DPRINT("Success!\n");
140 VOID
FreeDeviceList()
142 PDEVICE_LIST pDevice
;
144 DPRINT("FreeDeviceList()\n");
148 pDevice
= DeviceList
;
149 DeviceList
= pDevice
->Next
;
151 if (pDevice
->DeviceInstanceData
)
152 HeapFree(Heap
, 0, (LPVOID
)pDevice
->DeviceInstanceData
);
154 HeapFree(Heap
, 0, (LPSTR
)pDevice
);
159 MMRESULT
FindDevices()
164 DPRINT("Finding devices\n");
166 // DriverKey = OpenParametersKey();
167 // see drvutil.c of MS DDK for how this SHOULD be done...
172 WCHAR DeviceName
[SOUND_MAX_DEVICE_NAME
];
174 for (i
=0; OpenDevice(WaveOutDevice
, i
, &h
, GENERIC_READ
) == MMSYSERR_NOERROR
; i
++)
176 wsprintf(DeviceName
, L
"WaveOut%d\0", i
);
178 AddDeviceToList(&DeviceList
, WaveOutDevice
, 0, DeviceName
);
181 for (i
=0; OpenDevice(WaveInDevice
, i
, &h
, GENERIC_READ
) == MMSYSERR_NOERROR
; i
++)
183 wsprintf(DeviceName
, L
"WaveIn%d\0", i
);
185 AddDeviceToList(&DeviceList
, WaveInDevice
, 0, DeviceName
);
188 for (i
=0; OpenDevice(MidiOutDevice
, i
, &h
, GENERIC_READ
) == MMSYSERR_NOERROR
; i
++)
190 wsprintf(DeviceName
, L
"MidiOut%d\0", i
);
192 AddDeviceToList(&DeviceList
, MidiOutDevice
, 0, DeviceName
);
195 for (i
=0; OpenDevice(MidiInDevice
, i
, &h
, GENERIC_READ
) == MMSYSERR_NOERROR
; i
++)
197 wsprintf(DeviceName
, L
"MidiIn%d\0", i
);
199 AddDeviceToList(&DeviceList
, MidiInDevice
, 0, DeviceName
);
202 for (i
=0; OpenDevice(AuxDevice
, i
, &h
, GENERIC_READ
) == MMSYSERR_NOERROR
; i
++)
204 wsprintf(DeviceName
, L
"Aux%d\0", i
);
206 AddDeviceToList(&DeviceList
, AuxDevice
, 0, DeviceName
);
210 // MIDI Out 0: MPU-401 UART
211 // AddDeviceToList(&DeviceList, MidiOutDevice, 0, L"MidiOut0");
212 // Wave Out 0: Sound Blaster 16 (ok?)
213 // AddDeviceToList(&DeviceList, WaveOutDevice, 0, L"WaveOut0");
215 return MMSYSERR_NOERROR
; // ok?
220 DWORD
GetDeviceCount(UINT DeviceType
)
225 for (List
= DeviceList
, i
= 0; List
!= NULL
; List
= List
->Next
)
226 if (List
->DeviceType
== DeviceType
)