[WDMAUD.DRV]
[reactos.git] / reactos / dll / win32 / wdmaud.drv / wdmaud.c
1 /*
2 * PROJECT: ReactOS Sound System
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/wdmaud.drv/wdmaud.c
5 *
6 * PURPOSE: WDM Audio Driver (User-mode part)
7 * PROGRAMMERS: Andrew Greenwood (silverblade@reactos.org)
8 *
9 * NOTES: Looking for wodMessage & co? You won't find them here. Try
10 * the MME Buddy library, which is where these routines are
11 * actually implemented.
12 *
13 */
14
15 #include "wdmaud.h"
16
17 HANDLE KernelHandle = INVALID_HANDLE_VALUE;
18
19 MMRESULT
20 QueryWdmWaveDeviceFormatSupport(
21 IN PSOUND_DEVICE Device,
22 IN PWAVEFORMATEX WaveFormat,
23 IN DWORD WaveFormatSize)
24 {
25 /* Whatever... */
26 return MMSYSERR_NOERROR;
27 }
28
29
30 MMRESULT
31 PopulateWdmDeviceList(
32 MMDEVICE_TYPE DeviceType)
33 {
34 MMRESULT Result;
35 DWORD DeviceCount = 0;
36 PSOUND_DEVICE SoundDevice = NULL;
37 MMFUNCTION_TABLE FuncTable;
38 DWORD i;
39
40 VALIDATE_MMSYS_PARAMETER( IS_VALID_SOUND_DEVICE_TYPE(DeviceType) );
41
42 #ifdef USE_MMIXER_LIB
43 Result = WdmAudGetNumDevsByMMixer(DeviceType, &DeviceCount);
44 #else
45 Result = WdmAudGetNumWdmDevsByLegacy(DeviceType, &DeviceCount);
46 #endif
47
48 if ( ! MMSUCCESS(Result) )
49 {
50 SND_ERR(L"Error %d while obtaining number of devices\n", Result);
51 return TranslateInternalMmResult(Result);
52 }
53
54 SND_TRACE(L"%d devices of type %d found\n", DeviceCount, DeviceType);
55
56
57 for ( i = 0; i < DeviceCount; ++ i )
58 {
59 Result = ListSoundDevice(DeviceType, UlongToPtr(i), &SoundDevice);
60
61 if ( ! MMSUCCESS(Result) )
62 {
63 SND_ERR(L"Failed to list sound device - error %d\n", Result);
64 return TranslateInternalMmResult(Result);
65 }
66
67 /* Set up our function table */
68 ZeroMemory(&FuncTable, sizeof(MMFUNCTION_TABLE));
69 #ifdef USE_MMIXER_LIB
70 FuncTable.GetCapabilities = WdmAudGetCapabilitiesByMMixer;
71 FuncTable.Open = WdmAudOpenSoundDeviceByMMixer;
72 FuncTable.Close = WdmAudCloseSoundDeviceByMMixer;
73 FuncTable.GetDeviceInterfaceString = WdmAudGetDeviceInterfaceStringByMMixer;
74 #else
75 FuncTable.GetCapabilities = WdmAudGetCapabilitiesByLegacy;
76 FuncTable.Open = WdmAudOpenSoundDeviceByLegacy;
77 FuncTable.Close = WdmAudCloseSoundDeviceByLegacy;
78 FuncTable.GetDeviceInterfaceString = WdmAudGetDeviceInterfaceStringByLegacy;
79 #endif
80
81 FuncTable.QueryWaveFormatSupport = QueryWdmWaveDeviceFormatSupport;
82 if (DeviceType == MIXER_DEVICE_TYPE)
83 {
84 #ifdef USE_MMIXER_LIB
85 FuncTable.SetWaveFormat = WdmAudSetMixerDeviceFormatByMMixer;
86 FuncTable.QueryMixerInfo = WdmAudQueryMixerInfoByMMixer;
87 #else
88 FuncTable.SetWaveFormat = WdmAudSetMixerDeviceFormatByLegacy;
89 FuncTable.QueryMixerInfo = WdmAudQueryMixerInfoByLegacy;
90 #endif
91 }
92
93 if (DeviceType == WAVE_IN_DEVICE_TYPE || DeviceType == WAVE_OUT_DEVICE_TYPE)
94 {
95 #ifdef USE_MMIXER_LIB
96 FuncTable.SetWaveFormat = WdmAudSetWdmWaveDeviceFormatByMMixer;
97 FuncTable.SetState = WdmAudSetWdmWaveStateByMMixer;
98 FuncTable.ResetStream = WdmAudResetStreamByMMixer;
99 FuncTable.GetPos = WdmAudGetWdmPositionByMMixer;
100 #else
101 FuncTable.SetWaveFormat = WdmAudSetWaveDeviceFormatByLegacy;
102 FuncTable.SetState = WdmAudSetWaveStateByLegacy;
103 FuncTable.ResetStream = WdmAudResetStreamByLegacy;
104 FuncTable.GetPos = WdmAudGetWavePositionByLegacy;
105 #endif
106
107 #ifdef USE_MMIXER_LIB
108 FuncTable.CommitWaveBuffer = WdmAudCommitWaveBufferByMMixer;
109 #elif defined (USERMODE_MIXER)
110 FuncTable.CommitWaveBuffer = WriteFileEx_Remixer;
111 #else
112 FuncTable.CommitWaveBuffer = WriteFileEx_Committer2;
113 #endif
114 }
115
116 SetSoundDeviceFunctionTable(SoundDevice, &FuncTable);
117 }
118
119 return MMSYSERR_NOERROR;
120 }
121
122 LONG
123 APIENTRY
124 DriverProc(
125 DWORD DriverId,
126 HANDLE DriverHandle,
127 UINT Message,
128 LONG Parameter1,
129 LONG Parameter2)
130 {
131 MMRESULT Result;
132
133 switch ( Message )
134 {
135 case DRV_LOAD :
136 {
137 #ifndef USE_MMIXER_LIB
138 HANDLE Handle;
139 #endif
140
141 SND_TRACE(L"DRV_LOAD\n");
142
143 Result = InitEntrypointMutexes();
144
145 if ( ! MMSUCCESS(Result) )
146 return 0L;
147
148 #ifdef USE_MMIXER_LIB
149 if (!WdmAudInitUserModeMixer())
150 {
151 SND_ERR(L"Failed to initialize mmixer lib\n");
152 return 0;
153 }
154 #else
155 if (WdmAudOpenSoundDeviceByLegacy() != MMSYSERR_NOERROR)
156 {
157 SND_ERR(L"Failed to open %s\n", KERNEL_DEVICE_NAME);
158 CleanupEntrypointMutexes();
159
160 //UnlistAllSoundDevices();
161
162 return 0L;
163 }
164 #endif
165
166 /* Populate the device lists */
167 SND_TRACE(L"Populating device lists\n");
168 PopulateWdmDeviceList(WAVE_OUT_DEVICE_TYPE);
169 PopulateWdmDeviceList(WAVE_IN_DEVICE_TYPE);
170 PopulateWdmDeviceList(MIDI_OUT_DEVICE_TYPE);
171 PopulateWdmDeviceList(MIDI_IN_DEVICE_TYPE);
172 PopulateWdmDeviceList(AUX_DEVICE_TYPE);
173 PopulateWdmDeviceList(MIXER_DEVICE_TYPE);
174
175 SND_TRACE(L"Initialisation complete\n");
176
177 return 1L;
178 }
179
180 case DRV_FREE :
181 {
182 SND_TRACE(L"DRV_FREE\n");
183
184 #ifdef USE_MMIXER_LIB
185 WdmAudCleanupMMixer();
186 #else
187 WdmAudCleanupLegacy();
188 #endif
189
190 /* TODO: Clean up the path names! */
191 UnlistAllSoundDevices();
192
193 CleanupEntrypointMutexes();
194
195 SND_TRACE(L"Unfreed memory blocks: %d\n",
196 GetMemoryAllocationCount());
197
198 return 1L;
199 }
200
201 case DRV_ENABLE :
202 case DRV_DISABLE :
203 {
204 SND_TRACE(L"DRV_ENABLE / DRV_DISABLE\n");
205 return 1L;
206 }
207
208 case DRV_OPEN :
209 case DRV_CLOSE :
210 {
211 SND_TRACE(L"DRV_OPEN / DRV_CLOSE\n");
212 return 1L;
213 }
214
215 case DRV_QUERYCONFIGURE :
216 {
217 SND_TRACE(L"DRV_QUERYCONFIGURE\n");
218 return 0L;
219 }
220 case DRV_CONFIGURE :
221 return DRVCNF_OK;
222
223 default :
224 SND_TRACE(L"Unhandled message %d\n", Message);
225 return DefDriverProc(DriverId,
226 DriverHandle,
227 Message,
228 Parameter1,
229 Parameter2);
230 }
231 }
232
233
234 BOOL WINAPI DllMain(
235 HINSTANCE hinstDLL,
236 DWORD fdwReason,
237 LPVOID lpvReserved)
238 {
239 switch ( fdwReason )
240 {
241 case DLL_PROCESS_ATTACH :
242 SND_TRACE(L"WDMAUD.DRV - Process attached\n");
243 break;
244 case DLL_PROCESS_DETACH :
245 SND_TRACE(L"WDMAUD.DRV - Process detached\n");
246 break;
247 case DLL_THREAD_ATTACH :
248 SND_TRACE(L"WDMAUD.DRV - Thread attached\n");
249 break;
250 case DLL_THREAD_DETACH :
251 SND_TRACE(L"WDMAUD.DRV - Thread detached\n");
252 break;
253 }
254
255 return TRUE;
256 }