xref: /reactos/dll/win32/wdmaud.drv/wdmaud.c (revision d6ea8659)
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 #define NDEBUG
18 #include <debug.h>
19 #include <mmebuddy_debug.h>
20 
21 #ifndef USE_MMIXER_LIB
22 #define FUNC_NAME(x) x##ByLegacy
23 #else
24 #define FUNC_NAME(x) x##ByMMixer
25 #endif
26 
27 MMRESULT
28 QueryWdmWaveDeviceFormatSupport(
29     IN  PSOUND_DEVICE Device,
30     IN  PWAVEFORMATEX WaveFormat,
31     IN  DWORD WaveFormatSize)
32 {
33     /* Whatever... */
34     return MMSYSERR_NOERROR;
35 }
36 
37 MMRESULT
38 PopulateWdmDeviceList(
39     MMDEVICE_TYPE DeviceType)
40 {
41     MMRESULT Result;
42     DWORD DeviceCount = 0;
43     PSOUND_DEVICE SoundDevice = NULL;
44     MMFUNCTION_TABLE FuncTable;
45     DWORD i;
46 
47     VALIDATE_MMSYS_PARAMETER( IS_VALID_SOUND_DEVICE_TYPE(DeviceType) );
48 
49     Result = FUNC_NAME(WdmAudGetNumWdmDevs)(DeviceType, &DeviceCount);
50 
51     if ( ! MMSUCCESS(Result) )
52     {
53         SND_ERR(L"Error %d while obtaining number of devices\n", Result);
54         return TranslateInternalMmResult(Result);
55     }
56 
57     SND_TRACE(L"%d devices of type %d found\n", DeviceCount, DeviceType);
58 
59 
60     for ( i = 0; i < DeviceCount; ++ i )
61     {
62         Result = ListSoundDevice(DeviceType, UlongToPtr(i), &SoundDevice);
63 
64         if ( ! MMSUCCESS(Result) )
65         {
66             SND_ERR(L"Failed to list sound device - error %d\n", Result);
67             return TranslateInternalMmResult(Result);
68         }
69 
70         /* Set up our function table */
71         ZeroMemory(&FuncTable, sizeof(MMFUNCTION_TABLE));
72         FuncTable.GetCapabilities = FUNC_NAME(WdmAudGetCapabilities);
73         FuncTable.QueryWaveFormatSupport = QueryWdmWaveDeviceFormatSupport; //FIXME
74         FuncTable.Open = FUNC_NAME(WdmAudOpenSoundDevice);
75         FuncTable.Close = FUNC_NAME(WdmAudCloseSoundDevice);
76         FuncTable.GetDeviceInterfaceString = FUNC_NAME(WdmAudGetDeviceInterfaceString);
77 
78         if (DeviceType == MIXER_DEVICE_TYPE)
79         {
80             FuncTable.SetWaveFormat = FUNC_NAME(WdmAudSetMixerDeviceFormat);
81             FuncTable.QueryMixerInfo = FUNC_NAME(WdmAudQueryMixerInfo);
82         }
83         else if (DeviceType == WAVE_IN_DEVICE_TYPE || DeviceType == WAVE_OUT_DEVICE_TYPE)
84         {
85             FuncTable.SetWaveFormat = FUNC_NAME(WdmAudSetWaveDeviceFormat);
86             FuncTable.SetState = FUNC_NAME(WdmAudSetWaveState);
87             FuncTable.ResetStream = FUNC_NAME(WdmAudResetStream);
88             FuncTable.GetPos = FUNC_NAME(WdmAudGetWavePosition);
89 
90 #ifndef USERMODE_MIXER
91             FuncTable.CommitWaveBuffer = FUNC_NAME(WdmAudCommitWaveBuffer);
92 #else
93             FuncTable.CommitWaveBuffer = WriteFileEx_Remixer;
94 #endif
95         }
96         else if (DeviceType == MIDI_IN_DEVICE_TYPE || DeviceType == MIDI_OUT_DEVICE_TYPE)
97         {
98             FuncTable.SetWaveFormat = FUNC_NAME(WdmAudSetMixerDeviceFormat);
99             FuncTable.SetState = FUNC_NAME(WdmAudSetWaveState);
100             FuncTable.GetPos = FUNC_NAME(WdmAudGetWavePosition);
101         }
102 
103         SetSoundDeviceFunctionTable(SoundDevice, &FuncTable);
104     }
105 
106     return MMSYSERR_NOERROR;
107 }
108 
109 
110 
111 LONG
112 APIENTRY
113 DriverProc(
114     DWORD DriverId,
115     HANDLE DriverHandle,
116     UINT Message,
117     LONG Parameter1,
118     LONG Parameter2)
119 {
120     switch ( Message )
121     {
122         case DRV_LOAD :
123         {
124             HANDLE Handle;
125             MMRESULT Result;
126             SND_TRACE(L"DRV_LOAD\n");
127 
128             Result = InitEntrypointMutexes();
129 
130             if ( ! MMSUCCESS(Result) )
131                 return 0L;
132 
133             Result = FUNC_NAME(WdmAudOpenSoundDevice)(NULL, &Handle);
134 
135             if ( Result != MMSYSERR_NOERROR )
136             {
137                 SND_ERR(L"Failed to open \\\\.\\wdmaud\n");
138                 //UnlistAllSoundDevices();
139 
140                 return 0L;
141             }
142 
143             /* Populate the device lists */
144             SND_TRACE(L"Populating device lists\n");
145             PopulateWdmDeviceList(WAVE_OUT_DEVICE_TYPE);
146             PopulateWdmDeviceList(WAVE_IN_DEVICE_TYPE);
147             PopulateWdmDeviceList(MIDI_OUT_DEVICE_TYPE);
148             PopulateWdmDeviceList(MIDI_IN_DEVICE_TYPE);
149             PopulateWdmDeviceList(AUX_DEVICE_TYPE);
150             PopulateWdmDeviceList(MIXER_DEVICE_TYPE);
151 
152             SND_TRACE(L"Initialisation complete\n");
153 
154             return 1L;
155         }
156 
157         case DRV_FREE :
158         {
159             SND_TRACE(L"DRV_FREE\n");
160 
161             FUNC_NAME(WdmAudCleanup)();
162 
163             /* TODO: Clean up the path names! */
164             UnlistAllSoundDevices();
165 
166             CleanupEntrypointMutexes();
167 
168             SND_TRACE(L"Unfreed memory blocks: %d\n",
169                       GetMemoryAllocationCount());
170 
171             return 1L;
172         }
173 
174         case DRV_ENABLE :
175         case DRV_DISABLE :
176         {
177             SND_TRACE(L"DRV_ENABLE / DRV_DISABLE\n");
178             return 1L;
179         }
180 
181         case DRV_OPEN :
182         case DRV_CLOSE :
183         {
184             SND_TRACE(L"DRV_OPEN / DRV_CLOSE\n");
185             return 1L;
186         }
187 
188         case DRV_QUERYCONFIGURE :
189         {
190             SND_TRACE(L"DRV_QUERYCONFIGURE\n");
191             return 0L;
192         }
193         case DRV_CONFIGURE :
194             return DRVCNF_OK;
195 
196         default :
197             SND_TRACE(L"Unhandled message %d\n", Message);
198             return DefDriverProc(DriverId,
199                                  DriverHandle,
200                                  Message,
201                                  Parameter1,
202                                  Parameter2);
203     }
204 }
205 
206 
207 BOOL WINAPI DllMain(
208     HINSTANCE hinstDLL,
209     DWORD fdwReason,
210     LPVOID lpvReserved)
211 {
212     switch ( fdwReason )
213     {
214         case DLL_PROCESS_ATTACH :
215             SND_TRACE(L"WDMAUD.DRV - Process attached\n");
216             break;
217         case DLL_PROCESS_DETACH :
218             SND_TRACE(L"WDMAUD.DRV - Process detached\n");
219             break;
220         case DLL_THREAD_ATTACH :
221             SND_TRACE(L"WDMAUD.DRV - Thread attached\n");
222             break;
223         case DLL_THREAD_DETACH :
224             SND_TRACE(L"WDMAUD.DRV - Thread detached\n");
225             break;
226     }
227 
228     return TRUE;
229 }
230