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