Fixed bug in volume control step size so that it actually works.
[wine] / dlls / winmm / wineoss / mixer.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  * Sample MIXER Wine Driver for Linux
5  *
6  * Copyright    1997 Marcus Meissner
7  *              1999,2001 Eric Pouech
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <assert.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 # include <sys/ioctl.h>
38 #endif
39
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
42 #include "windef.h"
43 #include "winbase.h"
44 #include "mmddk.h"
45 #include "oss.h"
46 #include "wine/debug.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(mixer);
49
50 #ifdef HAVE_OSS
51
52 #define MAX_MIXERDRV     (6)
53
54 #define WINE_MIXER_MANUF_ID             0xAA
55 #define WINE_MIXER_PRODUCT_ID           0x55
56 #define WINE_MIXER_VERSION              0x0100
57 #define WINE_MIXER_NAME                 "WINE OSS Mixer"
58
59 #define WINE_CHN_MASK(_x)               (1L << (_x))
60 #define WINE_CHN_SUPPORTS(_c, _x)       ((_c) & WINE_CHN_MASK(_x))
61 /* Bass and Treble are no longer in the mask as Windows does not handle them */
62 #define WINE_MIXER_MASK_SPEAKER         (WINE_CHN_MASK(SOUND_MIXER_SYNTH)  | \
63                                          WINE_CHN_MASK(SOUND_MIXER_PCM)    | \
64                                          WINE_CHN_MASK(SOUND_MIXER_LINE)   | \
65                                          WINE_CHN_MASK(SOUND_MIXER_MIC)    | \
66                                          WINE_CHN_MASK(SOUND_MIXER_CD)     )
67
68 #define WINE_MIXER_MASK_RECORD          (WINE_CHN_MASK(SOUND_MIXER_SYNTH)  | \
69                                          WINE_CHN_MASK(SOUND_MIXER_LINE)   | \
70                                          WINE_CHN_MASK(SOUND_MIXER_MIC)    | \
71                                          WINE_CHN_MASK(SOUND_MIXER_IMIX)   )
72
73 /* FIXME: the two following string arrays should be moved to a resource file in a string table */
74 /* if it's done, better use a struct to hold labels, name, and muted channel volume cache */
75 static const char*      MIX_Labels[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_LABELS;
76 static const char*      MIX_Names [SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
77
78 struct mixerCtrl
79 {
80     DWORD               dwLineID;
81     MIXERCONTROLA       ctrl;
82 };
83
84 struct mixer
85 {
86     const char*         name;
87     int                 volume[SOUND_MIXER_NRDEVICES];
88     int                 devMask;
89     int                 stereoMask;
90     int                 recMask;
91     BOOL                singleRecChannel;
92     struct mixerCtrl*   ctrl;
93     int                 numCtrl;
94 };
95
96 #define LINEID_DST      0xFFFF
97 #define LINEID_SPEAKER  0x0000
98 #define LINEID_RECORD   0x0001
99
100 static int              MIX_NumMixers;
101 static struct mixer     MIX_Mixers[MAX_MIXERDRV];
102
103 /**************************************************************************
104  */
105
106 static const char * getMessage(UINT uMsg)
107 {
108     static char str[64];
109 #define MSG_TO_STR(x) case x: return #x;
110     switch (uMsg) {
111     MSG_TO_STR(DRVM_INIT);
112     MSG_TO_STR(DRVM_EXIT);
113     MSG_TO_STR(DRVM_ENABLE);
114     MSG_TO_STR(DRVM_DISABLE);
115     MSG_TO_STR(MXDM_GETDEVCAPS);
116     MSG_TO_STR(MXDM_GETLINEINFO);
117     MSG_TO_STR(MXDM_GETNUMDEVS);
118     MSG_TO_STR(MXDM_OPEN);
119     MSG_TO_STR(MXDM_CLOSE);
120     MSG_TO_STR(MXDM_GETLINECONTROLS);
121     MSG_TO_STR(MXDM_GETCONTROLDETAILS);
122     MSG_TO_STR(MXDM_SETCONTROLDETAILS);
123     }
124 #undef MSG_TO_STR
125     sprintf(str, "UNKNOWN(%08x)", uMsg);
126     return str;
127 }
128
129 static const char * getIoctlCommand(int command)
130 {
131     static char str[64];
132 #define IOCTL_TO_STR(x) case x: return #x;
133     switch (command) {
134     IOCTL_TO_STR(SOUND_MIXER_NRDEVICES);
135     IOCTL_TO_STR(SOUND_MIXER_VOLUME);
136     IOCTL_TO_STR(SOUND_MIXER_BASS);
137     IOCTL_TO_STR(SOUND_MIXER_TREBLE);
138     IOCTL_TO_STR(SOUND_MIXER_SYNTH);
139     IOCTL_TO_STR(SOUND_MIXER_PCM);
140     IOCTL_TO_STR(SOUND_MIXER_SPEAKER);
141     IOCTL_TO_STR(SOUND_MIXER_LINE);
142     IOCTL_TO_STR(SOUND_MIXER_MIC);
143     IOCTL_TO_STR(SOUND_MIXER_CD);
144     IOCTL_TO_STR(SOUND_MIXER_IMIX);
145     IOCTL_TO_STR(SOUND_MIXER_ALTPCM);
146     IOCTL_TO_STR(SOUND_MIXER_RECLEV);
147     IOCTL_TO_STR(SOUND_MIXER_IGAIN);
148     IOCTL_TO_STR(SOUND_MIXER_OGAIN);
149     IOCTL_TO_STR(SOUND_MIXER_LINE1);
150     IOCTL_TO_STR(SOUND_MIXER_LINE2);
151     IOCTL_TO_STR(SOUND_MIXER_LINE3);
152     IOCTL_TO_STR(SOUND_MIXER_DIGITAL1);
153     IOCTL_TO_STR(SOUND_MIXER_DIGITAL2);
154     IOCTL_TO_STR(SOUND_MIXER_DIGITAL3);
155     IOCTL_TO_STR(SOUND_MIXER_PHONEIN);
156     IOCTL_TO_STR(SOUND_MIXER_PHONEOUT);
157     IOCTL_TO_STR(SOUND_MIXER_VIDEO);
158     IOCTL_TO_STR(SOUND_MIXER_RADIO);
159     IOCTL_TO_STR(SOUND_MIXER_MONITOR);
160     IOCTL_TO_STR(SOUND_ONOFF_MIN);
161     IOCTL_TO_STR(SOUND_ONOFF_MAX);
162     }
163 #undef IOCTL_TO_STR
164     sprintf(str, "UNKNOWN(%08x)", command);
165     return str;
166 }
167
168 static const char * getControlType(DWORD dwControlType)
169 {
170     static char str[64];
171 #define TYPE_TO_STR(x) case x: return #x;
172     switch (dwControlType) {
173     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_CUSTOM);
174     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BOOLEANMETER);
175     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SIGNEDMETER);
176     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PEAKMETER);
177     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_UNSIGNEDMETER);
178     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BOOLEAN);
179     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_ONOFF);
180     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MUTE);
181     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MONO);
182     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_LOUDNESS);
183     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_STEREOENH);
184     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BASS_BOOST);
185     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BUTTON);
186     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_DECIBELS);
187     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SIGNED);
188     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_UNSIGNED);
189     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PERCENT);
190     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SLIDER);
191     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PAN);
192     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_QSOUNDPAN);
193     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_FADER);
194     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_VOLUME);
195     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BASS);
196     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_TREBLE);
197     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_EQUALIZER);
198     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SINGLESELECT);
199     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MUX);
200     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MULTIPLESELECT);
201     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MIXER);
202     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MICROTIME);
203     TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MILLITIME);
204     }
205 #undef TYPE_TO_STR
206     sprintf(str, "UNKNOWN(%08lx)", dwControlType);
207     return str;
208 }
209
210 /**************************************************************************
211  *                              MIX_FillLineControls            [internal]
212  */
213 static void MIX_FillLineControls(struct mixer* mix, int c, DWORD lineID,
214                                  DWORD dwType)
215 {
216     struct mixerCtrl*   mc = &mix->ctrl[c];
217     int                 j;
218
219     TRACE("(%p, %d, %08lx, %s)\n", mix, c, lineID, getControlType(dwType));
220
221     mc->dwLineID = lineID;
222     mc->ctrl.cbStruct = sizeof(MIXERCONTROLA);
223     mc->ctrl.dwControlID = c + 1;
224     mc->ctrl.dwControlType = dwType;
225
226     switch (dwType)
227     {
228     case MIXERCONTROL_CONTROLTYPE_VOLUME:
229         mc->ctrl.fdwControl = 0;
230         mc->ctrl.cMultipleItems = 0;
231         lstrcpynA(mc->ctrl.szShortName, "Vol", MIXER_SHORT_NAME_CHARS);
232         lstrcpynA(mc->ctrl.szName, "Volume", MIXER_LONG_NAME_CHARS);
233         memset(&mc->ctrl.Bounds, 0, sizeof(mc->ctrl.Bounds));
234         /* CONTROLTYPE_VOLUME uses the MIXER_CONTROLDETAILS_UNSIGNED struct,
235          * [0, 100] is the range supported by OSS
236          * whatever the min and max values are they must match
237          * conversions done in (Get|Set)ControlDetails to stay in [0, 100] range
238          */
239         mc->ctrl.Bounds.s1.dwMinimum = 0;
240         mc->ctrl.Bounds.s1.dwMaximum = 65535;
241         memset(&mc->ctrl.Metrics, 0, sizeof(mc->ctrl.Metrics));
242         mc->ctrl.Metrics.cSteps = 656;
243         break;
244     case MIXERCONTROL_CONTROLTYPE_MUTE:
245     case MIXERCONTROL_CONTROLTYPE_ONOFF:
246         mc->ctrl.fdwControl = 0;
247         mc->ctrl.cMultipleItems = 0;
248         lstrcpynA(mc->ctrl.szShortName, "Mute", MIXER_SHORT_NAME_CHARS);
249         lstrcpynA(mc->ctrl.szName, "Mute", MIXER_LONG_NAME_CHARS);
250         memset(&mc->ctrl.Bounds, 0, sizeof(mc->ctrl.Bounds));
251         mc->ctrl.Bounds.s1.dwMinimum = 0;
252         mc->ctrl.Bounds.s1.dwMaximum = 1;
253         memset(&mc->ctrl.Metrics, 0, sizeof(mc->ctrl.Metrics));
254         break;
255     case MIXERCONTROL_CONTROLTYPE_MUX:
256     case MIXERCONTROL_CONTROLTYPE_MIXER:
257         mc->ctrl.fdwControl = MIXERCONTROL_CONTROLF_MULTIPLE;
258         mc->ctrl.cMultipleItems = 0;
259         for (j = 0; j < SOUND_MIXER_NRDEVICES; j++)
260             if (WINE_CHN_SUPPORTS(mix->recMask, j))
261                 mc->ctrl.cMultipleItems++;
262         lstrcpynA(mc->ctrl.szShortName, "Mixer", MIXER_SHORT_NAME_CHARS);
263         lstrcpynA(mc->ctrl.szName, "Mixer", MIXER_LONG_NAME_CHARS);
264         memset(&mc->ctrl.Bounds, 0, sizeof(mc->ctrl.Bounds));
265         memset(&mc->ctrl.Metrics, 0, sizeof(mc->ctrl.Metrics));
266         break;
267
268     default:
269         FIXME("Internal error: unknown type: %08lx\n", dwType);
270     }
271     TRACE("ctrl[%2d]: typ=%08lx lin=%08lx\n", c + 1, dwType, lineID);
272 }
273
274 /******************************************************************
275  *              MIX_GetMixer
276  *
277  *
278  */
279 static struct mixer*    MIX_Get(WORD wDevID)
280 {
281     TRACE("(%04x)\n", wDevID);
282
283     if (wDevID >= MIX_NumMixers || MIX_Mixers[wDevID].name == NULL)
284         return NULL;
285
286     return &MIX_Mixers[wDevID];
287 }
288
289 /**************************************************************************
290  *                              MIX_Open                        [internal]
291  */
292 static DWORD MIX_Open(WORD wDevID, LPMIXEROPENDESC lpMod, DWORD flags)
293 {
294     int                 mixer, i, j;
295     unsigned            caps;
296     struct mixer*       mix;
297     DWORD               ret = MMSYSERR_NOERROR;
298
299     TRACE("(%04X, %p, %lu);\n", wDevID, lpMod, flags);
300
301     /* as we partly init the mixer with MIX_Open, we can allow null open decs
302      * EPP     if (lpMod == NULL) return MMSYSERR_INVALPARAM;
303      * anyway, it seems that WINMM/MMSYSTEM doesn't always open the mixer
304      * device before sending messages to it... it seems to be linked to all
305      * the equivalent of mixer identification 
306      * (with a reference to a wave, midi.. handle
307      */
308     if (!(mix = MIX_Get(wDevID))) {
309         WARN("bad device ID\n");
310         return MMSYSERR_BADDEVICEID;
311     }
312
313     if ((mixer = open(mix->name, O_RDWR)) < 0)
314     {
315         ERR("open(%s, O_RDWR) failed (%s)\n",
316             mix->name, strerror(errno));
317
318         if (errno == ENODEV || errno == ENXIO)
319         {
320             /* no driver present */
321             WARN("no driver\n");
322             return MMSYSERR_NODRIVER;
323         }
324         return MMSYSERR_ERROR;
325     }
326
327     if (ioctl(mixer, SOUND_MIXER_READ_DEVMASK, &mix->devMask) == -1)
328     {
329         ERR("ioctl(%s, SOUND_MIXER_DEVMASK) failed (%s)\n",
330             mix->name, strerror(errno));
331         ret = MMSYSERR_ERROR;
332         goto error;
333     }
334
335     mix->devMask &= WINE_MIXER_MASK_SPEAKER;
336     if (mix->devMask == 0)
337     {
338         WARN("no driver\n");
339         ret = MMSYSERR_NODRIVER;
340         goto error;
341     }
342
343     if (ioctl(mixer, SOUND_MIXER_READ_STEREODEVS, &mix->stereoMask) == -1)
344     {
345         ERR("ioctl(%s, SOUND_MIXER_STEREODEVS) failed (%s)\n",
346             mix->name, strerror(errno));
347         ret = MMSYSERR_ERROR;
348         goto error;
349     }
350     mix->stereoMask &= WINE_MIXER_MASK_SPEAKER;
351
352     if (ioctl(mixer, SOUND_MIXER_READ_RECMASK, &mix->recMask) == -1)
353     {
354         ERR("ioctl(%s, SOUND_MIXER_RECMASK) failed (%s)\n",
355             mix->name, strerror(errno));
356         ret = MMSYSERR_ERROR;
357         goto error;
358     }
359     mix->recMask &= WINE_MIXER_MASK_RECORD;
360     /* FIXME: we may need to support both rec lev & igain */
361     if (!WINE_CHN_SUPPORTS(mix->recMask, SOUND_MIXER_RECLEV))
362     {
363         WARN("The sound card doesn't support rec level\n");
364         if (WINE_CHN_SUPPORTS(mix->recMask, SOUND_MIXER_IGAIN))
365             WARN("but it does support IGain, please report\n");
366     }
367     if (ioctl(mixer, SOUND_MIXER_READ_CAPS, &caps) == -1)
368     {
369         ERR("ioctl(%s, SOUND_MIXER_READ_CAPS) failed (%s)\n",
370             mix->name, strerror(errno));
371         ret = MMSYSERR_ERROR;
372         goto error;
373     }
374     mix->singleRecChannel = caps & SOUND_CAP_EXCL_INPUT;
375     TRACE("dev=%04x rec=%04x stereo=%04x %s\n",
376           mix->devMask, mix->recMask, mix->stereoMask,
377           mix->singleRecChannel ? "single" : "multiple");
378     for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
379     {
380         mix->volume[i] = -1;
381     }
382     mix->numCtrl = 4; /* dst lines... vol&mute on speakers, vol&onoff on rec */
383     /* FIXME: do we always have RECLEV on all cards ??? */
384     for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
385     {
386         if (WINE_CHN_SUPPORTS(mix->devMask, i))
387             mix->numCtrl += 2; /* volume & mute */
388         if (WINE_CHN_SUPPORTS(mix->recMask, i))
389             mix->numCtrl += 2; /* volume & onoff */
390
391     }
392     if (!(mix->ctrl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
393                                 sizeof(mix->ctrl[0]) * mix->numCtrl)))
394     {
395         ret = MMSYSERR_NOMEM;
396         goto error;
397     }
398
399     j = 0;
400     MIX_FillLineControls(mix, j++, MAKELONG(0, LINEID_DST),
401                          MIXERCONTROL_CONTROLTYPE_VOLUME);
402     MIX_FillLineControls(mix, j++, MAKELONG(0, LINEID_DST),
403                          MIXERCONTROL_CONTROLTYPE_MUTE);
404     MIX_FillLineControls(mix, j++, MAKELONG(1, LINEID_DST),
405                          mix->singleRecChannel ?
406                              MIXERCONTROL_CONTROLTYPE_MUX :
407                              MIXERCONTROL_CONTROLTYPE_MIXER);
408     MIX_FillLineControls(mix, j++, MAKELONG(1, LINEID_DST),
409                          MIXERCONTROL_CONTROLTYPE_MUTE/*EPP*/);
410     for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
411     {
412         if (WINE_CHN_SUPPORTS(mix->devMask, i))
413         {
414             MIX_FillLineControls(mix, j++, MAKELONG(LINEID_SPEAKER, i),
415                                  MIXERCONTROL_CONTROLTYPE_VOLUME);
416             MIX_FillLineControls(mix, j++, MAKELONG(LINEID_SPEAKER, i),
417                                  MIXERCONTROL_CONTROLTYPE_MUTE);
418         }
419     }
420     for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
421     {
422         if (WINE_CHN_SUPPORTS(mix->recMask, i))
423         {
424             MIX_FillLineControls(mix, j++, MAKELONG(LINEID_RECORD, i),
425                                  MIXERCONTROL_CONTROLTYPE_VOLUME);
426             MIX_FillLineControls(mix, j++, MAKELONG(LINEID_RECORD, i),
427                                  MIXERCONTROL_CONTROLTYPE_MUTE/*EPP*/);
428         }
429     }
430     assert(j == mix->numCtrl);
431  error:
432     close(mixer);
433     return ret;
434 }
435
436 /**************************************************************************
437  *                              MIX_GetVal                      [internal]
438  */
439 static  BOOL    MIX_GetVal(struct mixer* mix, int chn, int* val)
440 {
441     int         mixer;
442     BOOL        ret = FALSE;
443
444     TRACE("(%p, %s, %p\n", mix, getIoctlCommand(chn), val);
445
446     if ((mixer = open(mix->name, O_RDWR)) < 0) {
447         /* FIXME: ENXIO => no mixer installed */
448         WARN("mixer device not available !\n");
449     } else {
450         if (ioctl(mixer, MIXER_READ(chn), val) >= 0) {
451             TRACE("Reading %04x for %s\n", *val, getIoctlCommand(chn));
452             ret = TRUE;
453         } else {
454             ERR("ioctl(%s, MIXER_READ(%s)) failed (%s)\n",
455                 mix->name, getIoctlCommand(chn), strerror(errno));
456         }
457         close(mixer);
458     }
459     return ret;
460 }
461
462 /**************************************************************************
463  *                              MIX_SetVal                      [internal]
464  */
465 static  BOOL    MIX_SetVal(struct mixer* mix, int chn, int val)
466 {
467     int         mixer;
468     BOOL        ret = FALSE;
469
470     TRACE("(%p, %s, %x)\n", mix, getIoctlCommand(chn), val);
471
472     if ((mixer = open(mix->name, O_RDWR)) < 0) {
473         /* FIXME: ENXIO => no mixer installed */
474         WARN("mixer device not available !\n");
475     } else {
476         if (ioctl(mixer, MIXER_WRITE(chn), &val) >= 0) {
477             TRACE("Set %s to %04x\n", getIoctlCommand(chn), val);
478             ret = TRUE;
479         } else {
480             ERR("ioctl(%s, MIXER_WRITE(%s)) failed (%s)\n",
481                 mix->name, getIoctlCommand(chn), strerror(errno));
482         }
483         close(mixer);
484     }
485     return ret;
486 }
487
488 /******************************************************************
489  *              MIX_GetRecSrc
490  *
491  *
492  */
493 static BOOL     MIX_GetRecSrc(struct mixer* mix, unsigned* mask)
494 {
495     int         mixer;
496     BOOL        ret = FALSE;
497
498     TRACE("(%p, %p)\n", mix, mask);
499
500     if ((mixer = open(mix->name, O_RDWR)) >= 0) {
501         if (ioctl(mixer, SOUND_MIXER_READ_RECSRC, &mask) >= 0) {
502             ret = TRUE;
503         } else {
504             ERR("ioctl(%s, SOUND_MIXER_READ_RECSRC) failed (%s)\n",
505                 mix->name, strerror(errno));
506         }
507         close(mixer);
508     }
509     return ret;
510 }
511
512 /******************************************************************
513  *              MIX_SetRecSrc
514  *
515  *
516  */
517 static BOOL     MIX_SetRecSrc(struct mixer* mix, unsigned mask)
518 {
519     int         mixer;
520     BOOL        ret = FALSE;
521
522     TRACE("(%p, %08x)\n", mix, mask);
523
524     if ((mixer = open(mix->name, O_RDWR)) >= 0) {
525         if (ioctl(mixer, SOUND_MIXER_WRITE_RECSRC, &mask) >= 0) {
526             ret = TRUE;
527         } else {
528             ERR("ioctl(%s, SOUND_MIXER_WRITE_RECSRC) failed (%s)\n",
529                 mix->name, strerror(errno));
530         }
531         close(mixer);
532     }
533     return ret;
534 }
535
536 /**************************************************************************
537  *                              MIX_GetDevCaps                  [internal]
538  */
539 static DWORD MIX_GetDevCaps(WORD wDevID, LPMIXERCAPSA lpCaps, DWORD dwSize)
540 {
541     struct mixer*       mix;
542     MIXERCAPSA          capsA;
543
544     TRACE("(%04X, %p, %lu);\n", wDevID, lpCaps, dwSize);
545
546     if (lpCaps == NULL) {
547         WARN("invalid parameter: lpCaps == NULL\n");
548         return MMSYSERR_INVALPARAM;
549     }
550
551     if (!(mix = MIX_Get(wDevID))) {
552         WARN("bad device ID\n");
553         return MMSYSERR_BADDEVICEID;
554     }
555
556     capsA.wMid = WINE_MIXER_MANUF_ID;
557     capsA.wPid = WINE_MIXER_PRODUCT_ID;
558     capsA.vDriverVersion = WINE_MIXER_VERSION;
559     strcpy(capsA.szPname, WINE_MIXER_NAME);
560
561     capsA.cDestinations = 2; /* speakers & record */
562     capsA.fdwSupport = 0; /* No bits defined yet */
563
564     memcpy(lpCaps, &capsA, min(dwSize, sizeof(capsA)));
565
566     return MMSYSERR_NOERROR;
567 }
568
569 /**************************************************************************
570  *                              MIX_GetLineInfoDst      [internal]
571  */
572 static  DWORD   MIX_GetLineInfoDst(struct mixer* mix, LPMIXERLINEA lpMl,
573                                    DWORD dst)
574 {
575     unsigned mask;
576     int j;
577
578     TRACE("(%p, %p, %08lx)\n", mix, lpMl, dst);
579
580     lpMl->dwDestination = dst;
581     switch (dst)
582     {
583     case 0:
584         lpMl->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
585         mask = mix->devMask;
586         j = SOUND_MIXER_VOLUME;
587         break;
588     case 1:
589         lpMl->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
590         mask = mix->recMask;
591         j = SOUND_MIXER_RECLEV;
592         break;
593     default:
594         FIXME("shouldn't happen\n");
595         return MMSYSERR_ERROR;
596     }
597     lpMl->dwSource = 0xFFFFFFFF;
598     lstrcpynA(lpMl->szShortName, MIX_Labels[j], MIXER_SHORT_NAME_CHARS);
599     lstrcpynA(lpMl->szName, MIX_Names[j], MIXER_LONG_NAME_CHARS);
600
601     /* we have all connections found in the MIX_DevMask */
602     lpMl->cConnections = 0;
603     for (j = 0; j < SOUND_MIXER_NRDEVICES; j++)
604         if (WINE_CHN_SUPPORTS(mask, j))
605             lpMl->cConnections++;
606     lpMl->cChannels = 1;
607     if (WINE_CHN_SUPPORTS(mix->stereoMask, lpMl->dwLineID))
608         lpMl->cChannels++;
609     lpMl->dwLineID = MAKELONG(dst, LINEID_DST);
610     lpMl->cControls = 0;
611     for (j = 0; j < mix->numCtrl; j++)
612         if (mix->ctrl[j].dwLineID == lpMl->dwLineID)
613             lpMl->cControls++;
614
615     return MMSYSERR_NOERROR;
616 }
617
618 /**************************************************************************
619  *                              MIX_GetLineInfoSrc      [internal]
620  */
621 static  DWORD   MIX_GetLineInfoSrc(struct mixer* mix, LPMIXERLINEA lpMl,
622                                    DWORD idx, DWORD dst)
623 {
624     int         i, j;
625     unsigned    mask = (dst) ? mix->recMask : mix->devMask;
626
627     TRACE("(%p, %p, %ld, %08lx)\n", mix, lpMl, idx, dst);
628
629     strcpy(lpMl->szShortName, MIX_Labels[idx]);
630     strcpy(lpMl->szName, MIX_Names[idx]);
631     lpMl->dwLineID = MAKELONG(dst, idx);
632     lpMl->dwDestination = dst;
633     lpMl->cConnections = 1;
634     lpMl->cControls = 0;
635     for (i = 0; i < mix->numCtrl; i++)
636         if (mix->ctrl[i].dwLineID == lpMl->dwLineID)
637             lpMl->cControls++;
638
639     switch (idx)
640     {
641     case SOUND_MIXER_SYNTH:
642         lpMl->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;
643         lpMl->fdwLine    |= MIXERLINE_LINEF_SOURCE;
644         break;
645     case SOUND_MIXER_CD:
646         lpMl->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC;
647         lpMl->fdwLine    |= MIXERLINE_LINEF_SOURCE;
648         break;
649     case SOUND_MIXER_LINE:
650         lpMl->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_LINE;
651         lpMl->fdwLine    |= MIXERLINE_LINEF_SOURCE;
652         break;
653     case SOUND_MIXER_MIC:
654         lpMl->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
655         lpMl->fdwLine    |= MIXERLINE_LINEF_SOURCE;
656         break;
657     case SOUND_MIXER_PCM:
658         lpMl->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
659         lpMl->fdwLine    |= MIXERLINE_LINEF_SOURCE;
660         break;
661     case SOUND_MIXER_IMIX:
662         lpMl->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_UNDEFINED;
663         lpMl->fdwLine    |= MIXERLINE_LINEF_SOURCE;
664         break;
665     default:
666         WARN("Index %ld not handled.\n", idx);
667         return MIXERR_INVALLINE;
668     }
669     lpMl->cChannels = 1;
670     if (dst == 0 && WINE_CHN_SUPPORTS(mix->stereoMask, idx))
671         lpMl->cChannels++;
672     for (i = j = 0; j < SOUND_MIXER_NRDEVICES; j++)
673     {
674         if (WINE_CHN_SUPPORTS(mask, j))
675         {
676             if (j == idx) break;
677             i++;
678         }
679     }
680     lpMl->dwSource = i;
681     return MMSYSERR_NOERROR;
682 }
683
684 /******************************************************************
685  *              MIX_CheckLine
686  */
687 static BOOL MIX_CheckLine(DWORD lineID)
688 {
689     TRACE("(%08lx)\n",lineID);
690
691     return ((HIWORD(lineID) < SOUND_MIXER_NRDEVICES && LOWORD(lineID) < 2) ||
692             (HIWORD(lineID) == LINEID_DST && 
693              LOWORD(lineID) < SOUND_MIXER_NRDEVICES));
694 }
695
696 /**************************************************************************
697  *                              MIX_GetLineInfo                 [internal]
698  */
699 static DWORD MIX_GetLineInfo(WORD wDevID, LPMIXERLINEA lpMl, DWORD fdwInfo)
700 {
701     int                 i, j;
702     DWORD               ret = MMSYSERR_NOERROR;
703     unsigned            mask;
704     struct mixer*       mix;
705
706     TRACE("(%04X, %p, %lu);\n", wDevID, lpMl, fdwInfo);
707
708     if (lpMl == NULL || lpMl->cbStruct != sizeof(*lpMl)) {
709         WARN("invalid parameter\n");
710         return MMSYSERR_INVALPARAM;
711     }
712
713     if ((mix = MIX_Get(wDevID)) == NULL) {
714         WARN("bad device ID\n");
715         return MMSYSERR_BADDEVICEID;
716     }
717
718     /* FIXME: set all the variables correctly... the lines below
719      * are very wrong...
720      */
721     lpMl->fdwLine       = MIXERLINE_LINEF_ACTIVE;
722     lpMl->dwUser        = 0;
723
724     switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK)
725     {
726     case MIXER_GETLINEINFOF_DESTINATION:
727         TRACE("MIXER_GETLINEINFOF_DESTINATION (%08lx)\n", lpMl->dwDestination);
728         if (lpMl->dwDestination >= 2) {
729             WARN("invalid parameter\n");
730             return MMSYSERR_INVALPARAM;
731         }
732         ret = MIX_GetLineInfoDst(mix, lpMl, lpMl->dwDestination);
733         if (ret != MMSYSERR_NOERROR) {
734             WARN("error\n");
735             return ret;
736         }
737         break;
738     case MIXER_GETLINEINFOF_SOURCE:
739         TRACE("MIXER_GETLINEINFOF_SOURCE (%08lx), dst=%08lx\n", lpMl->dwSource,
740               lpMl->dwDestination);
741         switch (lpMl->dwDestination)
742         {
743         case 0: mask = mix->devMask; break;
744         case 1: mask = mix->recMask; break;
745         default:
746             WARN("invalid parameter\n");
747             return MMSYSERR_INVALPARAM;
748         }
749         i = lpMl->dwSource;
750         for (j = 0; j < SOUND_MIXER_NRDEVICES; j++)
751         {
752             if (WINE_CHN_SUPPORTS(mask, j) && (i-- == 0))
753                 break;
754         }
755         if (j >= SOUND_MIXER_NRDEVICES) {
756             WARN("invalid line\n");
757             return MIXERR_INVALLINE;
758         }
759         ret = MIX_GetLineInfoSrc(mix, lpMl, j, lpMl->dwDestination);
760         if (ret != MMSYSERR_NOERROR) {
761             WARN("error\n");
762             return ret;
763         }
764         break;
765     case MIXER_GETLINEINFOF_LINEID:
766         TRACE("MIXER_GETLINEINFOF_LINEID (%08lx)\n", lpMl->dwLineID);
767
768         if (!MIX_CheckLine(lpMl->dwLineID)) {
769             WARN("invalid line\n");
770             return MIXERR_INVALLINE;
771         }
772         if (HIWORD(lpMl->dwLineID) == LINEID_DST)
773             ret = MIX_GetLineInfoDst(mix, lpMl, LOWORD(lpMl->dwLineID));
774         else
775             ret = MIX_GetLineInfoSrc(mix, lpMl, HIWORD(lpMl->dwLineID),
776                                      LOWORD(lpMl->dwLineID));
777         if (ret != MMSYSERR_NOERROR) {
778             WARN("error\n");
779             return ret;
780         }
781         break;
782     case MIXER_GETLINEINFOF_COMPONENTTYPE:
783         TRACE("MIXER_GETLINEINFOF_COMPONENTTYPE (%08lx)\n",
784               lpMl->dwComponentType);
785         switch (lpMl->dwComponentType)
786         {
787         case MIXERLINE_COMPONENTTYPE_DST_SPEAKERS:
788             ret = MIX_GetLineInfoDst(mix, lpMl, 0);
789             break;
790         case MIXERLINE_COMPONENTTYPE_DST_WAVEIN:
791             ret = MIX_GetLineInfoDst(mix, lpMl, 1);
792             break;
793         case MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER:
794             ret = MIX_GetLineInfoSrc(mix, lpMl, SOUND_MIXER_SYNTH, 0);
795             break;
796         case MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC:
797             ret = MIX_GetLineInfoSrc(mix, lpMl, SOUND_MIXER_CD, 0);
798             break;
799         case MIXERLINE_COMPONENTTYPE_SRC_LINE:
800             ret = MIX_GetLineInfoSrc(mix, lpMl, SOUND_MIXER_LINE, 0);
801             break;
802         case MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE:
803             ret = MIX_GetLineInfoSrc(mix, lpMl, SOUND_MIXER_MIC, 1);
804             break;
805         case MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT:
806             ret = MIX_GetLineInfoSrc(mix, lpMl, SOUND_MIXER_PCM, 0);
807             break;
808         case MIXERLINE_COMPONENTTYPE_SRC_UNDEFINED:
809             ret = MIX_GetLineInfoSrc(mix, lpMl, SOUND_MIXER_IMIX, 1);
810             break;
811         default:
812             FIXME("Unhandled component type (%08lx)\n", lpMl->dwComponentType);
813             return MMSYSERR_INVALPARAM;
814         }
815         break;
816     case MIXER_GETLINEINFOF_TARGETTYPE:
817         FIXME("MIXER_GETLINEINFOF_TARGETTYPE not implemented yet.\n");
818         break;
819     default:
820         WARN("Unknown flag (%08lx)\n", fdwInfo & MIXER_GETLINEINFOF_QUERYMASK);
821         break;
822     }
823
824     lpMl->Target.dwType = MIXERLINE_TARGETTYPE_AUX; /* FIXME */
825     lpMl->Target.dwDeviceID = 0xFFFFFFFF;
826     lpMl->Target.wMid = WINE_MIXER_MANUF_ID;
827     lpMl->Target.wPid = WINE_MIXER_PRODUCT_ID;
828     lpMl->Target.vDriverVersion = WINE_MIXER_VERSION;
829     strcpy(lpMl->Target.szPname, WINE_MIXER_NAME);
830
831     return ret;
832 }
833
834 /******************************************************************
835  *              MIX_CheckControl
836  *
837  */
838 static BOOL     MIX_CheckControl(struct mixer* mix, DWORD ctrlID)
839 {
840     TRACE("(%p, %08lx)\n", mix, ctrlID);
841
842     return (ctrlID >= 1 && ctrlID <= mix->numCtrl);
843 }
844
845 /**************************************************************************
846  *                              MIX_GetLineControls             [internal]
847  */
848 static  DWORD   MIX_GetLineControls(WORD wDevID, LPMIXERLINECONTROLSA lpMlc,
849                                     DWORD flags)
850 {
851     DWORD               dwRet = MMSYSERR_NOERROR;
852     struct mixer*       mix;
853
854     TRACE("(%04X, %p, %lu);\n", wDevID, lpMlc, flags);
855
856     if (lpMlc == NULL) {
857         WARN("invalid parameter: lpMlc == NULL\n");
858         return MMSYSERR_INVALPARAM;
859     }
860
861     if (lpMlc->cbStruct < sizeof(*lpMlc) ||
862         lpMlc->cbmxctrl < sizeof(MIXERCONTROLA)) {
863         WARN("invalid parameter: lpMlc\n");
864         return MMSYSERR_INVALPARAM;
865     }
866
867     if ((mix = MIX_Get(wDevID)) == NULL) {
868         WARN("bad device id\n");
869         return MMSYSERR_BADDEVICEID;
870     }
871
872     switch (flags & MIXER_GETLINECONTROLSF_QUERYMASK)
873     {
874     case MIXER_GETLINECONTROLSF_ALL:
875         {
876             int         i, j;
877
878             TRACE("line=%08lx MIXER_GETLINECONTROLSF_ALL (%ld)\n",
879                   lpMlc->dwLineID, lpMlc->cControls);
880
881             for (i = j = 0; i < mix->numCtrl; i++)
882             {
883                 if (mix->ctrl[i].dwLineID == lpMlc->dwLineID)
884                     j++;
885             }
886
887             if (!j || lpMlc->cControls != j) {
888                 WARN("invalid parameter\n");
889                 dwRet = MMSYSERR_INVALPARAM;
890             } else if (!MIX_CheckLine(lpMlc->dwLineID)) {
891                 WARN("invalid line\n");
892                 dwRet = MIXERR_INVALLINE;
893             } else {
894                 for (i = j = 0; i < mix->numCtrl; i++)
895                 {
896                     if (mix->ctrl[i].dwLineID == lpMlc->dwLineID)
897                     {
898                         TRACE("[%d] => [%2d]: typ=%08lx\n", j, i + 1,
899                               mix->ctrl[i].ctrl.dwControlType);
900                         lpMlc->pamxctrl[j++] = mix->ctrl[i].ctrl;
901                     }
902                 }
903             }
904         }
905         break;
906     case MIXER_GETLINECONTROLSF_ONEBYID:
907         TRACE("line=%08lx MIXER_GETLINECONTROLSF_ONEBYID (%lx)\n",
908               lpMlc->dwLineID, lpMlc->u.dwControlID);
909
910         if (!MIX_CheckControl(mix, lpMlc->u.dwControlID) ||
911             mix->ctrl[lpMlc->u.dwControlID - 1].dwLineID != lpMlc->dwLineID) {
912             WARN("invalid parameter\n");
913             dwRet = MMSYSERR_INVALPARAM;
914         } else
915             lpMlc->pamxctrl[0] = mix->ctrl[lpMlc->u.dwControlID - 1].ctrl;
916         break;
917     case MIXER_GETLINECONTROLSF_ONEBYTYPE:
918         TRACE("line=%08lx MIXER_GETLINECONTROLSF_ONEBYTYPE (%s)\n",
919               lpMlc->dwLineID, getControlType(lpMlc->u.dwControlType));
920         if (!MIX_CheckLine(lpMlc->dwLineID)) {
921             WARN("invalid line\n");
922             dwRet = MIXERR_INVALLINE;
923         } else {
924             int   i;
925             DWORD ct = lpMlc->u.dwControlType & MIXERCONTROL_CT_CLASS_MASK;
926             for (i = 0; i < mix->numCtrl; i++) {
927                 if (mix->ctrl[i].dwLineID == lpMlc->dwLineID &&
928                     ct == (mix->ctrl[i].ctrl.dwControlType &
929                     MIXERCONTROL_CT_CLASS_MASK)) {
930                     lpMlc->pamxctrl[0] = mix->ctrl[i].ctrl;
931                     break;
932                 }
933             }
934
935             if (i == mix->numCtrl) {
936                 WARN("invalid parameter: control not found\n");
937                 dwRet = MMSYSERR_INVALPARAM;
938             }
939         }
940         break;
941     default:
942         ERR("Unknown flag %08lx\n", flags & MIXER_GETLINECONTROLSF_QUERYMASK);
943         dwRet = MMSYSERR_INVALPARAM;
944     }
945
946     return dwRet;
947 }
948
949 /**************************************************************************
950  *                              MIX_GetControlDetails           [internal]
951  */
952 static  DWORD   MIX_GetControlDetails(WORD wDevID, LPMIXERCONTROLDETAILS lpmcd,
953                                       DWORD fdwDetails)
954 {
955     DWORD               ret = MMSYSERR_NOTSUPPORTED;
956     DWORD               c, chnl;
957     struct mixer*       mix;
958
959     TRACE("(%04X, %p, %lu);\n", wDevID, lpmcd, fdwDetails);
960
961     if (lpmcd == NULL) {
962         WARN("invalid parameter: lpmcd == NULL\n");
963         return MMSYSERR_INVALPARAM;
964     }
965
966     if ((mix = MIX_Get(wDevID)) == NULL) {
967         WARN("bad device ID\n");
968         return MMSYSERR_BADDEVICEID;
969     }
970
971     switch (fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK)
972     {
973     case MIXER_GETCONTROLDETAILSF_VALUE:
974         TRACE("MIXER_GETCONTROLDETAILSF_VALUE (%08lx)\n", lpmcd->dwControlID);
975         if (MIX_CheckControl(mix, lpmcd->dwControlID))
976         {
977             c = lpmcd->dwControlID - 1;
978             chnl = HIWORD(mix->ctrl[c].dwLineID);
979             if (chnl == LINEID_DST)
980                 chnl = LOWORD(mix->ctrl[c].dwLineID) ? SOUND_MIXER_RECLEV :
981                     SOUND_MIXER_VOLUME;
982             switch (mix->ctrl[c].ctrl.dwControlType)
983             {
984             case MIXERCONTROL_CONTROLTYPE_VOLUME:
985                 {
986                     LPMIXERCONTROLDETAILS_UNSIGNED      mcdu;
987                     int                                 val;
988
989                     if (lpmcd->cbDetails != 
990                         sizeof(MIXERCONTROLDETAILS_UNSIGNED)) {
991                         WARN("invalid parameter: cbDetails != %d\n",
992                              sizeof(MIXERCONTROLDETAILS_UNSIGNED));
993                         return MMSYSERR_INVALPARAM;
994                     }
995
996                     TRACE("%s MIXERCONTROLDETAILS_UNSIGNED[%lu]\n",
997                           getControlType(mix->ctrl[c].ctrl.dwControlType),
998                           lpmcd->cChannels);
999
1000                     mcdu = (LPMIXERCONTROLDETAILS_UNSIGNED)lpmcd->paDetails;
1001
1002                     /* return value is 00RL (4 bytes)... */
1003                     if ((val = mix->volume[chnl]) == -1 &&
1004                         !MIX_GetVal(mix, chnl, &val)) {
1005                         WARN("invalid parameter\n");
1006                         return MMSYSERR_INVALPARAM;
1007                     }
1008
1009                     switch (lpmcd->cChannels)
1010                     {
1011                     case 1:
1012                         TRACE("mono\n");
1013                         /* mono... so R = L */
1014                         mcdu->dwValue = (LOBYTE(LOWORD(val)) * 65536L) / 100;
1015                         break;
1016                     case 2:
1017                         TRACE("stereo\n");
1018                         /* stereo, left is paDetails[0] */
1019                         mcdu->dwValue = (LOBYTE(LOWORD(val)) * 65536L) / 100;
1020                         mcdu++;
1021                         mcdu->dwValue = (HIBYTE(LOWORD(val)) * 65536L) / 100;
1022                         break;
1023                     default:
1024                         WARN("Unsupported cChannels (%ld)\n", lpmcd->cChannels);
1025                         return MMSYSERR_INVALPARAM;
1026                     }
1027                     TRACE("=> %08lx\n", mcdu->dwValue);
1028                 }
1029                 break;
1030             case MIXERCONTROL_CONTROLTYPE_MUTE:
1031             case MIXERCONTROL_CONTROLTYPE_ONOFF:
1032                 {
1033                     LPMIXERCONTROLDETAILS_BOOLEAN       mcdb;
1034
1035                     if (lpmcd->cbDetails != 
1036                         sizeof(MIXERCONTROLDETAILS_BOOLEAN)) {
1037                         WARN("invalid parameter: cbDetails != %d\n",
1038                              sizeof(MIXERCONTROLDETAILS_BOOLEAN));
1039                         return MMSYSERR_INVALPARAM;
1040                     }
1041
1042                     TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%lu]\n",
1043                           getControlType(mix->ctrl[c].ctrl.dwControlType),
1044                           lpmcd->cChannels);
1045
1046                     /* we mute both channels at the same time */
1047                     mcdb = (LPMIXERCONTROLDETAILS_BOOLEAN)lpmcd->paDetails;
1048                     mcdb->fValue = (mix->volume[chnl] != -1);
1049                     TRACE("=> %s\n", mcdb->fValue ? "on" : "off");
1050                 }
1051                 break;
1052             case MIXERCONTROL_CONTROLTYPE_MIXER:
1053             case MIXERCONTROL_CONTROLTYPE_MUX:
1054                 {
1055                     unsigned                            mask;
1056
1057                     if (lpmcd->cbDetails != 
1058                         sizeof(MIXERCONTROLDETAILS_BOOLEAN)) {
1059                         WARN("invalid parameter: cbDetails != %d\n",
1060                              sizeof(MIXERCONTROLDETAILS_BOOLEAN));
1061                         return MMSYSERR_INVALPARAM;
1062                     }
1063
1064                     TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%lu]\n",
1065                           getControlType(mix->ctrl[c].ctrl.dwControlType),
1066                           lpmcd->cChannels);
1067
1068                     if (!MIX_GetRecSrc(mix, &mask))
1069                     {
1070                         /* FIXME: ENXIO => no mixer installed */
1071                         WARN("mixer device not available !\n");
1072                         ret = MMSYSERR_ERROR;
1073                     }
1074                     else
1075                     {
1076                         LPMIXERCONTROLDETAILS_BOOLEAN   mcdb;
1077                         int                             i, j;
1078
1079                         /* we mute both channels at the same time */
1080                         mcdb = (LPMIXERCONTROLDETAILS_BOOLEAN)lpmcd->paDetails;
1081
1082                         for (i = j = 0; j < SOUND_MIXER_NRDEVICES; j++)
1083                         {
1084                             if (WINE_CHN_SUPPORTS(mix->recMask, j))
1085                             {
1086                                 if (i >= lpmcd->u.cMultipleItems)
1087                                     return MMSYSERR_INVALPARAM;
1088                                 mcdb[i++].fValue = WINE_CHN_SUPPORTS(mask, j);
1089                             }
1090                         }
1091                     }
1092                 }
1093                 break;
1094             default:
1095                 WARN("%s Unsupported\n",
1096                      getControlType(mix->ctrl[c].ctrl.dwControlType));
1097             }
1098             ret = MMSYSERR_NOERROR;
1099         }
1100         else
1101         {
1102             WARN("invalid parameter\n");
1103             ret = MMSYSERR_INVALPARAM;
1104         }
1105         break;
1106     case MIXER_GETCONTROLDETAILSF_LISTTEXT:
1107         TRACE("MIXER_GETCONTROLDETAILSF_LISTTEXT (%08lx)\n",
1108               lpmcd->dwControlID);
1109
1110         ret = MMSYSERR_INVALPARAM;
1111         if (MIX_CheckControl(mix, lpmcd->dwControlID))
1112         {
1113             int c = lpmcd->dwControlID - 1;
1114
1115             if (mix->ctrl[c].ctrl.dwControlType == MIXERCONTROL_CONTROLTYPE_MUX ||
1116                 mix->ctrl[c].ctrl.dwControlType == MIXERCONTROL_CONTROLTYPE_MIXER)
1117             {
1118                 LPMIXERCONTROLDETAILS_LISTTEXTA mcdlt;
1119                 int i, j;
1120
1121                 mcdlt = (LPMIXERCONTROLDETAILS_LISTTEXTA)lpmcd->paDetails;
1122                 for (i = j = 0; j < SOUND_MIXER_NRDEVICES; j++)
1123                 {
1124                     if (WINE_CHN_SUPPORTS(mix->recMask, j))
1125                     {
1126                         mcdlt[i].dwParam1 = MAKELONG(LINEID_RECORD, j);
1127                         mcdlt[i].dwParam2 = 0;
1128                         strcpy(mcdlt[i].szName, MIX_Names[j]);
1129                         i++;
1130                     }
1131                 }
1132                 if (i != lpmcd->u.cMultipleItems) FIXME("bad count\n");
1133                 ret = MMSYSERR_NOERROR;
1134             }
1135         }
1136         break;
1137     default:
1138         WARN("Unknown flag (%08lx)\n",
1139              fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK);
1140     }
1141     return ret;
1142 }
1143
1144 /**************************************************************************
1145  *                              MIX_SetControlDetails           [internal]
1146  */
1147 static  DWORD   MIX_SetControlDetails(WORD wDevID, LPMIXERCONTROLDETAILS lpmcd,
1148                                       DWORD fdwDetails)
1149 {
1150     DWORD               ret = MMSYSERR_NOTSUPPORTED;
1151     DWORD               c, chnl;
1152     int                 val;
1153     struct mixer*       mix;
1154
1155     TRACE("(%04X, %p, %lu);\n", wDevID, lpmcd, fdwDetails);
1156
1157     if (lpmcd == NULL) {
1158         TRACE("invalid parameter: lpmcd == NULL\n");
1159         return MMSYSERR_INVALPARAM;
1160     }
1161
1162     if ((mix = MIX_Get(wDevID)) == NULL) {
1163         WARN("bad device ID\n");
1164         return MMSYSERR_BADDEVICEID;
1165     }
1166
1167     switch (fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK)
1168     {
1169     case MIXER_GETCONTROLDETAILSF_VALUE:
1170         TRACE("MIXER_GETCONTROLDETAILSF_VALUE (%08lx)\n", lpmcd->dwControlID);
1171         if (MIX_CheckControl(mix, lpmcd->dwControlID))
1172         {
1173             c = lpmcd->dwControlID - 1;
1174
1175             TRACE("dwLineID=%08lx\n",mix->ctrl[c].dwLineID);
1176
1177             chnl = HIWORD(mix->ctrl[c].dwLineID);
1178             if (chnl == LINEID_DST)
1179                 chnl = LOWORD(mix->ctrl[c].dwLineID) ?
1180                     SOUND_MIXER_RECLEV : SOUND_MIXER_VOLUME;
1181
1182             switch (mix->ctrl[c].ctrl.dwControlType)
1183             {
1184             case MIXERCONTROL_CONTROLTYPE_VOLUME:
1185                 {
1186                     LPMIXERCONTROLDETAILS_UNSIGNED      mcdu;
1187
1188                     if (lpmcd->cbDetails != 
1189                         sizeof(MIXERCONTROLDETAILS_UNSIGNED)) {
1190                         WARN("invalid parameter: cbDetails != %d\n",
1191                              sizeof(MIXERCONTROLDETAILS_UNSIGNED));
1192                         return MMSYSERR_INVALPARAM;
1193                     }
1194
1195                     TRACE("%s MIXERCONTROLDETAILS_UNSIGNED[%lu]\n",
1196                           getControlType(mix->ctrl[c].ctrl.dwControlType),
1197                           lpmcd->cChannels);
1198
1199                     mcdu = (LPMIXERCONTROLDETAILS_UNSIGNED)lpmcd->paDetails;
1200                     /* val should contain 00RL */
1201                     switch (lpmcd->cChannels)
1202                     {
1203                     case 1:
1204                         /* mono... so R = L */
1205                         TRACE("Setting RL to %ld\n", mcdu->dwValue);
1206                         val = 0x101 * ((mcdu->dwValue * 100) >> 16);
1207                         break;
1208                     case 2:
1209                         /* stereo, left is paDetails[0] */
1210                         TRACE("Setting L to %ld\n", mcdu->dwValue);
1211                         val = ((mcdu->dwValue * 100) >> 16);
1212                         mcdu++;
1213                         TRACE("Setting R to %ld\n", mcdu->dwValue);
1214                         val += ((mcdu->dwValue * 100) >> 16) << 8;
1215                         break;
1216                     default:
1217                         WARN("Unsupported cChannels (%ld)\n", lpmcd->cChannels);
1218                         return MMSYSERR_INVALPARAM;
1219                     }
1220
1221                     if (mix->volume[chnl] == -1)
1222                     {
1223                         if (!MIX_SetVal(mix, chnl, val)) {
1224                             WARN("invalid parameter\n");
1225                             return MMSYSERR_INVALPARAM;
1226                         }
1227                     }
1228                     else
1229                     {
1230                         mix->volume[chnl] = val;
1231                     }
1232                 }
1233                 ret = MMSYSERR_NOERROR;
1234                 break;
1235             case MIXERCONTROL_CONTROLTYPE_MUTE:
1236             case MIXERCONTROL_CONTROLTYPE_ONOFF:
1237                 {
1238                     LPMIXERCONTROLDETAILS_BOOLEAN       mcdb;
1239
1240                     if (lpmcd->cbDetails != 
1241                         sizeof(MIXERCONTROLDETAILS_BOOLEAN)) {
1242                         WARN("invalid parameter: cbDetails != %d\n",
1243                              sizeof(MIXERCONTROLDETAILS_BOOLEAN));
1244                         return MMSYSERR_INVALPARAM;
1245                     }
1246
1247                     TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%lu]\n",
1248                           getControlType(mix->ctrl[c].ctrl.dwControlType),
1249                           lpmcd->cChannels);
1250
1251                     mcdb = (LPMIXERCONTROLDETAILS_BOOLEAN)lpmcd->paDetails;
1252                     if (mcdb->fValue)
1253                     {
1254                         /* save the volume and then set it to 0 */
1255                         if (!MIX_GetVal(mix, chnl, &mix->volume[chnl]) ||
1256                             !MIX_SetVal(mix, chnl, 0)) {
1257                             WARN("invalid parameter\n");
1258                             return MMSYSERR_INVALPARAM;
1259                         }
1260                     }
1261                     else
1262                     {
1263                         if (mix->volume[chnl] == -1)
1264                         {
1265                             ret = MMSYSERR_NOERROR;
1266                             break;
1267                         }
1268                         if (!MIX_SetVal(mix, chnl, mix->volume[chnl])) {
1269                             WARN("invalid parameter\n");
1270                             return MMSYSERR_INVALPARAM;
1271                         }
1272                         mix->volume[chnl] = -1;
1273                     }
1274                 }
1275                 ret = MMSYSERR_NOERROR;
1276                 break;
1277             case MIXERCONTROL_CONTROLTYPE_MIXER:
1278             case MIXERCONTROL_CONTROLTYPE_MUX:
1279                 {
1280                     LPMIXERCONTROLDETAILS_BOOLEAN       mcdb;
1281                     unsigned                            mask;
1282                     int                                 i, j;
1283
1284                     if (lpmcd->cbDetails != 
1285                         sizeof(MIXERCONTROLDETAILS_BOOLEAN)) {
1286                         WARN("invalid parameter: cbDetails != %d\n",
1287                              sizeof(MIXERCONTROLDETAILS_BOOLEAN));
1288                         return MMSYSERR_INVALPARAM;
1289                     }
1290
1291                     TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%lu]\n",
1292                           getControlType(mix->ctrl[c].ctrl.dwControlType),
1293                           lpmcd->cChannels);
1294
1295                     /* we mute both channels at the same time */
1296                     mcdb = (LPMIXERCONTROLDETAILS_BOOLEAN)lpmcd->paDetails;
1297                     mask = 0;
1298                     for (i = j = 0; j < SOUND_MIXER_NRDEVICES; j++)
1299                     {
1300                         if (WINE_CHN_SUPPORTS(mix->recMask, j) &&
1301                             mcdb[i++].fValue)
1302                         {
1303                             /* a mux can only select one line at a time... */
1304                             if (mix->singleRecChannel && mask != 0)
1305                             {
1306                                 FIXME("!!!\n");
1307                                 return MMSYSERR_INVALPARAM;
1308                             }
1309                             mask |= WINE_CHN_MASK(j);
1310                         }
1311                     }
1312                     if (i != lpmcd->u.cMultipleItems)
1313                         FIXME("bad count\n");
1314                     TRACE("writing %04x as rec src\n", mask);
1315                     if (!MIX_SetRecSrc(mix, mask))
1316                         ERR("Can't write new mixer settings\n");
1317                     else
1318                         ret = MMSYSERR_NOERROR;
1319                 }
1320                 break;
1321             }
1322         }
1323         break;
1324     default:
1325         WARN("Unknown SetControlDetails flag (%08lx)\n",
1326              fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK);
1327     }
1328     return ret;
1329 }
1330
1331 /**************************************************************************
1332  *                              MIX_Init                        [internal]
1333  */
1334 static  DWORD   MIX_Init(void)
1335 {
1336     int mixer;
1337
1338     TRACE("()\n");
1339
1340 #define MIXER_DEV "/dev/mixer"
1341     if ((mixer = open(MIXER_DEV, O_RDWR)) < 0)
1342     {
1343         if (errno == ENODEV || errno == ENXIO)
1344         {
1345             /* no driver present */
1346             WARN("no driver\n");
1347             return MMSYSERR_NODRIVER;
1348         }
1349         MIX_NumMixers = 0;
1350         return MMSYSERR_ERROR;
1351     }
1352     close(mixer);
1353     MIX_NumMixers = 1;
1354     MIX_Mixers[0].name = MIXER_DEV;
1355     MIX_Open(0, NULL, 0); /* FIXME */
1356 #undef MIXER_DEV
1357     return MMSYSERR_NOERROR;
1358 }
1359
1360 /**************************************************************************
1361  *                              MIX_GetNumDevs                  [internal]
1362  */
1363 static  DWORD   MIX_GetNumDevs(void)
1364 {
1365     TRACE("()\n");
1366
1367     return MIX_NumMixers;
1368 }
1369
1370 #endif /* HAVE_OSS */
1371
1372 /**************************************************************************
1373  *                              mxdMessage (WINEOSS.3)
1374  */
1375 DWORD WINAPI OSS_mxdMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1376                             DWORD dwParam1, DWORD dwParam2)
1377 {
1378     TRACE("(%04X, %s, %08lX, %08lX, %08lX);\n", wDevID, getMessage(wMsg),
1379           dwUser, dwParam1, dwParam2);
1380
1381 #ifdef HAVE_OSS
1382     switch (wMsg)
1383     {
1384     case DRVM_INIT:
1385         return MIX_Init();
1386     case DRVM_EXIT:
1387     case DRVM_ENABLE:
1388     case DRVM_DISABLE:
1389         /* FIXME: Pretend this is supported */
1390         return 0;
1391     case MXDM_GETDEVCAPS:
1392         return MIX_GetDevCaps(wDevID, (LPMIXERCAPSA)dwParam1, dwParam2);
1393     case MXDM_GETLINEINFO:
1394         return MIX_GetLineInfo(wDevID, (LPMIXERLINEA)dwParam1, dwParam2);
1395     case MXDM_GETNUMDEVS:
1396         return MIX_GetNumDevs();
1397     case MXDM_OPEN:
1398         return MMSYSERR_NOERROR;
1399         /* MIX_Open(wDevID, (LPMIXEROPENDESC)dwParam1, dwParam2); */
1400     case MXDM_CLOSE:
1401         return MMSYSERR_NOERROR;
1402     case MXDM_GETLINECONTROLS:
1403         return MIX_GetLineControls(wDevID, (LPMIXERLINECONTROLSA)dwParam1, dwParam2);
1404     case MXDM_GETCONTROLDETAILS:
1405         return MIX_GetControlDetails(wDevID, (LPMIXERCONTROLDETAILS)dwParam1, dwParam2);
1406     case MXDM_SETCONTROLDETAILS:
1407         return MIX_SetControlDetails(wDevID, (LPMIXERCONTROLDETAILS)dwParam1, dwParam2);
1408     default:
1409         WARN("unknown message %d!\n", wMsg);
1410         return MMSYSERR_NOTSUPPORTED;
1411     }
1412 #else
1413     return MMSYSERR_NOTENABLED;
1414 #endif
1415 }