Added SystemParametersInfo unit test.
[wine] / dlls / msacm / driver.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  *      MSACM32 library
5  *
6  *      Copyright 1998  Patrik Stridvall
7  *                1999  Eric Pouech
8  */
9
10 #include <stdio.h>
11
12 #include "winbase.h"
13 #include "winerror.h"
14 #include "windef.h"
15 #include "wingdi.h"
16 #include "winuser.h"
17 #include "winnls.h"
18 #include "mmsystem.h"
19 #include "msacm.h"
20 #include "msacmdrv.h"
21 #include "wineacm.h"
22 #include "winreg.h"
23 #include "debugtools.h"
24
25 DEFAULT_DEBUG_CHANNEL(msacm);
26         
27 /***********************************************************************
28  *           acmDriverAddA (MSACM32.@)
29  */
30 MMRESULT WINAPI acmDriverAddA(PHACMDRIVERID phadid, HINSTANCE hinstModule,
31                               LPARAM lParam, DWORD dwPriority, DWORD fdwAdd)
32 {
33     if (!phadid)
34         return MMSYSERR_INVALPARAM;
35     
36     /* Check if any unknown flags */
37     if (fdwAdd & 
38         ~(ACM_DRIVERADDF_FUNCTION|ACM_DRIVERADDF_NOTIFYHWND|
39           ACM_DRIVERADDF_GLOBAL))
40         return MMSYSERR_INVALFLAG;
41     
42     /* Check if any incompatible flags */
43     if ((fdwAdd & ACM_DRIVERADDF_FUNCTION) && 
44         (fdwAdd & ACM_DRIVERADDF_NOTIFYHWND))
45         return MMSYSERR_INVALFLAG;
46     
47     /* FIXME: in fact, should GetModuleFileName(hinstModule) and do a 
48      * LoadDriver on it, to be sure we can call SendDriverMessage on the
49      * hDrvr handle.
50      */
51     *phadid = (HACMDRIVERID) MSACM_RegisterDriver(NULL, NULL, hinstModule);
52     
53     /* FIXME: lParam, dwPriority and fdwAdd ignored */
54     
55     return MMSYSERR_NOERROR;
56 }
57
58 /***********************************************************************
59  *           acmDriverAddW (MSACM32.@)
60  * FIXME
61  *   Not implemented
62  */
63 MMRESULT WINAPI acmDriverAddW(PHACMDRIVERID phadid, HINSTANCE hinstModule,
64                               LPARAM lParam, DWORD dwPriority, DWORD fdwAdd)
65 {
66     FIXME("(%p, 0x%08x, %ld, %ld, %ld): stub\n",
67           phadid, hinstModule, lParam, dwPriority, fdwAdd);
68     
69     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
70     return MMSYSERR_ERROR;
71 }
72
73 /***********************************************************************
74  *           acmDriverClose (MSACM32.@)
75  */
76 MMRESULT WINAPI acmDriverClose(HACMDRIVER had, DWORD fdwClose)
77 {
78     PWINE_ACMDRIVER     pad;
79     PWINE_ACMDRIVERID   padid;
80     PWINE_ACMDRIVER*    tpad;
81
82     if (fdwClose)
83         return MMSYSERR_INVALFLAG;
84     
85     pad = MSACM_GetDriver(had);
86     if (!pad)
87         return MMSYSERR_INVALHANDLE;
88
89     padid = pad->obj.pACMDriverID;
90
91     /* remove driver from list */
92     for (tpad = &(padid->pACMDriverList); *tpad; *tpad = (*tpad)->pNextACMDriver) {
93         if (*tpad == pad) {
94             *tpad = (*tpad)->pNextACMDriver;
95             break;
96         }
97     }
98     
99     /* close driver if it has been opened */
100     if (pad->hDrvr && !padid->hInstModule)
101         CloseDriver(pad->hDrvr, 0, 0);
102     
103     HeapFree(MSACM_hHeap, 0, pad);
104     
105     return MMSYSERR_NOERROR;
106 }
107
108 /***********************************************************************
109  *           acmDriverDetailsA (MSACM32.@)
110  */
111 MMRESULT WINAPI acmDriverDetailsA(HACMDRIVERID hadid, PACMDRIVERDETAILSA padd, DWORD fdwDetails)
112 {
113     MMRESULT mmr;
114     ACMDRIVERDETAILSW   addw;
115     
116     addw.cbStruct = sizeof(addw);
117     mmr = acmDriverDetailsW(hadid, &addw, fdwDetails);
118     if (mmr == 0) {
119         padd->fccType = addw.fccType; 
120         padd->fccComp = addw.fccComp; 
121         padd->wMid = addw.wMid; 
122         padd->wPid = addw.wPid; 
123         padd->vdwACM = addw.vdwACM; 
124         padd->vdwDriver = addw.vdwDriver; 
125         padd->fdwSupport = addw.fdwSupport; 
126         padd->cFormatTags = addw.cFormatTags; 
127         padd->cFilterTags = addw.cFilterTags; 
128         padd->hicon = addw.hicon; 
129         WideCharToMultiByte( CP_ACP, 0, addw.szShortName, -1, padd->szShortName,
130                              sizeof(padd->szShortName), NULL, NULL );
131         WideCharToMultiByte( CP_ACP, 0, addw.szLongName, -1, padd->szLongName,
132                              sizeof(padd->szLongName), NULL, NULL );
133         WideCharToMultiByte( CP_ACP, 0, addw.szCopyright, -1, padd->szCopyright,
134                              sizeof(padd->szCopyright), NULL, NULL );
135         WideCharToMultiByte( CP_ACP, 0, addw.szLicensing, -1, padd->szLicensing,
136                              sizeof(padd->szLicensing), NULL, NULL );
137         WideCharToMultiByte( CP_ACP, 0, addw.szFeatures, -1, padd->szFeatures,
138                              sizeof(padd->szFeatures), NULL, NULL );
139     }
140     return mmr;
141 }
142
143 /***********************************************************************
144  *           acmDriverDetailsW (MSACM32.@)
145  */
146 MMRESULT WINAPI acmDriverDetailsW(HACMDRIVERID hadid, PACMDRIVERDETAILSW padd, DWORD fdwDetails)
147 {
148     HACMDRIVER acmDrvr;
149     MMRESULT mmr;
150     
151     if (fdwDetails)
152         return MMSYSERR_INVALFLAG;
153     
154     mmr = acmDriverOpen(&acmDrvr, hadid, 0);
155     if (mmr == MMSYSERR_NOERROR) {
156         mmr = (MMRESULT)MSACM_Message(acmDrvr, ACMDM_DRIVER_DETAILS, (LPARAM)padd,  0);
157     
158         acmDriverClose(acmDrvr, 0);
159     }
160     
161     return mmr;
162 }
163
164 /***********************************************************************
165  *           acmDriverEnum (MSACM32.@)
166  */
167 MMRESULT WINAPI acmDriverEnum(ACMDRIVERENUMCB fnCallback, DWORD dwInstance, DWORD fdwEnum)
168 {
169     PWINE_ACMDRIVERID   padid;
170     DWORD               fdwSupport;
171
172     if (!fnCallback) return MMSYSERR_INVALPARAM;
173     
174     if (fdwEnum & ~(ACM_DRIVERENUMF_NOLOCAL|ACM_DRIVERENUMF_DISABLED))
175         return MMSYSERR_INVALFLAG;
176     
177     for (padid = MSACM_pFirstACMDriverID; padid; padid = padid->pNextACMDriverID) {
178         fdwSupport = padid->fdwSupport;
179         
180         if (padid->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_DISABLED) {
181             if (fdwEnum & ACM_DRIVERENUMF_DISABLED)
182                 fdwSupport |= ACMDRIVERDETAILS_SUPPORTF_DISABLED;
183             else
184                 continue;
185         }
186         if (!(*fnCallback)((HACMDRIVERID)padid, dwInstance, fdwSupport))
187             break;
188     }
189     
190     return MMSYSERR_NOERROR;
191 }
192
193 /***********************************************************************
194  *           acmDriverID (MSACM32.@)
195  */
196 MMRESULT WINAPI acmDriverID(HACMOBJ hao, PHACMDRIVERID phadid, DWORD fdwDriverID)
197 {
198     PWINE_ACMOBJ pao;
199     
200     if (!phadid)
201         return MMSYSERR_INVALPARAM;
202     
203     if (fdwDriverID)
204         return MMSYSERR_INVALFLAG;
205     
206     pao = MSACM_GetObj(hao, WINE_ACMOBJ_DONTCARE);
207     if (!pao)
208         return MMSYSERR_INVALHANDLE;
209     
210     *phadid = (HACMDRIVERID) pao->pACMDriverID;
211     
212     return MMSYSERR_NOERROR;
213 }
214
215 /***********************************************************************
216  *           acmDriverMessage (MSACM32.@)
217  *
218  */
219 LRESULT WINAPI acmDriverMessage(HACMDRIVER had, UINT uMsg, LPARAM lParam1, LPARAM lParam2)
220 {
221     if ((uMsg >= ACMDM_USER && uMsg < ACMDM_RESERVED_LOW) ||
222         uMsg == ACMDM_DRIVER_ABOUT ||
223         uMsg == DRV_QUERYCONFIGURE ||
224         uMsg == DRV_CONFIGURE)
225         return MSACM_Message(had, uMsg, lParam1, lParam2);
226     return MMSYSERR_INVALPARAM;
227 }
228
229 static  MMRESULT        MSACM_DriverOpenHelper(PWINE_ACMDRIVER* ppad, PWINE_ACMDRIVERID padid, DWORD fdwOpen, BOOL useDesc)
230 {
231     MMRESULT            ret = MMSYSERR_ERROR;
232     PWINE_ACMDRIVER     pad;
233
234     *ppad = NULL;
235
236     pad = HeapAlloc(MSACM_hHeap, 0, sizeof(WINE_ACMDRIVER));
237     if (!pad) return MMSYSERR_NOMEM;
238
239     pad->obj.dwType = WINE_ACMOBJ_DRIVER;
240     pad->obj.pACMDriverID = padid;
241
242     if (!(pad->hDrvr = padid->hInstModule)) {
243         /* this is not an externally added driver... need to load it */
244         if (!padid->pszDriverAlias) goto gotError;
245
246         if (useDesc) {
247             ACMDRVOPENDESCW     adod;
248             int                 len;
249
250             adod.cbStruct = sizeof(adod);
251             adod.fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
252             adod.fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
253             adod.dwVersion = acmGetVersion();
254             adod.dwFlags = fdwOpen;
255             adod.dwError = 0;
256             len = strlen("Drivers32") + 1;
257             adod.pszSectionName = HeapAlloc(MSACM_hHeap, 0, len * sizeof(WCHAR));
258             MultiByteToWideChar(CP_ACP, 0, "Drivers32", -1, (LPWSTR)adod.pszSectionName, len);
259             len = strlen(padid->pszDriverAlias) + 1;
260             adod.pszAliasName = HeapAlloc(MSACM_hHeap, 0, len * sizeof(WCHAR));
261             MultiByteToWideChar(CP_ACP, 0, padid->pszDriverAlias, -1, (LPWSTR)adod.pszAliasName, len);
262             adod.dnDevNode = 0;
263
264             pad->hDrvr = OpenDriverA(padid->pszDriverAlias, NULL, (DWORD)&adod);
265
266             HeapFree(MSACM_hHeap, 0, (LPWSTR)adod.pszSectionName);
267             HeapFree(MSACM_hHeap, 0, (LPWSTR)adod.pszAliasName);
268             if (!pad->hDrvr) {
269                 ret = adod.dwError;
270                 goto gotError;
271             }
272         } else {
273             if (!(pad->hDrvr = OpenDriverA(padid->pszDriverAlias, 0L, 0L))) {
274                 ret = MMSYSERR_ERROR;
275                 goto gotError;
276             }
277         }
278     }
279     *ppad = pad;
280     return MMSYSERR_NOERROR;
281  gotError:
282     HeapFree(MSACM_hHeap, 0, pad);
283     return ret;
284 }
285
286 /***********************************************************************
287  *           acmDriverOpen (MSACM32.@)
288  */
289 MMRESULT WINAPI acmDriverOpen(PHACMDRIVER phad, HACMDRIVERID hadid, DWORD fdwOpen)
290 {
291     PWINE_ACMDRIVERID   padid;
292     PWINE_ACMDRIVER     pad = NULL, first_pad = NULL;
293     MMRESULT            ret;
294
295     TRACE("(%p, %x, %08lu)\n", phad, hadid, fdwOpen);
296
297     if (!phad)
298         return MMSYSERR_INVALPARAM;
299     
300     if (fdwOpen)
301         return MMSYSERR_INVALFLAG;
302     
303     padid = MSACM_GetDriverID(hadid); 
304     if (!padid)
305         return MMSYSERR_INVALHANDLE;
306
307     /* first driver to be loaded ? */
308     if (!padid->pACMDriverList && !padid->hInstModule) {
309         ret = MSACM_DriverOpenHelper(&first_pad, padid, fdwOpen, FALSE);
310         if (ret) goto gotError;
311
312         
313         /* insert new pad at beg of list */
314         first_pad->pNextACMDriver = NULL;
315         padid->pACMDriverList = first_pad;
316     }
317
318     ret = MSACM_DriverOpenHelper(&pad, padid, fdwOpen, TRUE);
319     if (ret) {
320         if (first_pad)
321             acmDriverClose((HACMDRIVER)first_pad, 0L);
322         goto gotError;
323     }
324
325     /* insert new pad at beg of list */
326     pad->pNextACMDriver = padid->pACMDriverList;
327     padid->pACMDriverList = pad;
328
329     /* FIXME: Create a WINE_ACMDRIVER32 */
330     *phad = (HACMDRIVER)pad;
331     TRACE("'%s' => %08lx\n", padid->pszDriverAlias, (DWORD)pad);
332
333     return MMSYSERR_NOERROR;
334  gotError:
335     if (pad && !pad->hDrvr)
336         HeapFree(MSACM_hHeap, 0, pad);
337     return ret;
338 }
339
340 /***********************************************************************
341  *           acmDriverPriority (MSACM32.@)
342  */
343 MMRESULT WINAPI acmDriverPriority(HACMDRIVERID hadid, DWORD dwPriority, DWORD fdwPriority)
344 {
345     PWINE_ACMDRIVERID padid;
346     CHAR szSubKey[17];
347     CHAR szBuffer[256];
348     LONG lBufferLength = sizeof(szBuffer);
349     LONG lError;
350     HKEY hPriorityKey;
351     DWORD dwPriorityCounter;
352     
353     padid = MSACM_GetDriverID(hadid);
354     if (!padid)
355         return MMSYSERR_INVALHANDLE;
356     
357     /* Check for unknown flags */
358     if (fdwPriority & 
359         ~(ACM_DRIVERPRIORITYF_ENABLE|ACM_DRIVERPRIORITYF_DISABLE|
360           ACM_DRIVERPRIORITYF_BEGIN|ACM_DRIVERPRIORITYF_END))
361         return MMSYSERR_INVALFLAG;
362     
363     /* Check for incompatible flags */
364     if ((fdwPriority & ACM_DRIVERPRIORITYF_ENABLE) &&
365         (fdwPriority & ACM_DRIVERPRIORITYF_DISABLE))
366         return MMSYSERR_INVALFLAG;
367     
368     /* Check for incompatible flags */
369     if ((fdwPriority & ACM_DRIVERPRIORITYF_BEGIN) &&
370         (fdwPriority & ACM_DRIVERPRIORITYF_END))
371         return MMSYSERR_INVALFLAG;
372     
373     lError = RegOpenKeyA(HKEY_CURRENT_USER, 
374                          "Software\\Microsoft\\Multimedia\\"
375                          "Audio Compression Manager\\Priority v4.00",
376                          &hPriorityKey
377                          );
378     /* FIXME: Create key */
379     if (lError != ERROR_SUCCESS)
380         return MMSYSERR_ERROR;
381     
382     for (dwPriorityCounter = 1; ; dwPriorityCounter++)  {
383         snprintf(szSubKey, 17, "Priorty%ld", dwPriorityCounter);
384         lError = RegQueryValueA(hPriorityKey, szSubKey, szBuffer, &lBufferLength);
385         if (lError != ERROR_SUCCESS)
386             break;
387         
388         FIXME("(0x%08x, %ld, %ld): stub (partial)\n", 
389               hadid, dwPriority, fdwPriority);
390         break;
391     }
392     
393     RegCloseKey(hPriorityKey);
394     
395     return MMSYSERR_ERROR;
396 }
397
398 /***********************************************************************
399  *           acmDriverRemove (MSACM32.@)
400  */
401 MMRESULT WINAPI acmDriverRemove(HACMDRIVERID hadid, DWORD fdwRemove)
402 {
403     PWINE_ACMDRIVERID padid;
404     
405     padid = MSACM_GetDriverID(hadid);
406     if (!padid)
407         return MMSYSERR_INVALHANDLE;
408     
409     if (fdwRemove)
410         return MMSYSERR_INVALFLAG;
411     
412     MSACM_UnregisterDriver(padid);
413     
414     return MMSYSERR_NOERROR;
415 }
416