2 * Alsa MIXER Wine Driver for Linux
3 * Very loosely based on wineoss mixer driver
5 * Copyright 2007 Maarten Lankhorst
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
35 #ifdef HAVE_SYS_IOCTL_H
36 # include <sys/ioctl.h>
39 #define NONAMELESSUNION
40 #define NONAMELESSSTRUCT
50 #include "wine/unicode.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(mixer);
57 #define WINE_MIXER_MANUF_ID 0xAA
58 #define WINE_MIXER_PRODUCT_ID 0x55
59 #define WINE_MIXER_VERSION 0x0100
62 * In windows it seems to be required for all controls to have a volume switch
63 * In alsa that's optional
65 * I assume for playback controls, that there is always a playback volume switch available
68 * For capture controls, it is needed that there is a capture switch and a volume switch,
69 * It doesn't matter whether it is a playback volume switch or a capture volume switch.
70 * The code will first try to get/adjust capture volume, if that fails it tries playback volume
71 * It is not pretty, but under my 3 test cards it seems that there is no other choice:
72 * Most capture controls don't have a capture volume setting
74 * MUX means that only capture source can be exclusively selected,
75 * MIXER means that multiple sources can be selected simultaneously.
78 static const char * getMessage(UINT uMsg)
80 #define MSG_TO_STR(x) case x: return #x;
82 MSG_TO_STR(DRVM_INIT);
83 MSG_TO_STR(DRVM_EXIT);
84 MSG_TO_STR(DRVM_ENABLE);
85 MSG_TO_STR(DRVM_DISABLE);
86 MSG_TO_STR(MXDM_GETDEVCAPS);
87 MSG_TO_STR(MXDM_GETLINEINFO);
88 MSG_TO_STR(MXDM_GETNUMDEVS);
89 MSG_TO_STR(MXDM_OPEN);
90 MSG_TO_STR(MXDM_CLOSE);
91 MSG_TO_STR(MXDM_GETLINECONTROLS);
92 MSG_TO_STR(MXDM_GETCONTROLDETAILS);
93 MSG_TO_STR(MXDM_SETCONTROLDETAILS);
97 return wine_dbg_sprintf("UNKNOWN(%08x)", uMsg);
100 static const char * getControlType(DWORD dwControlType)
102 #define TYPE_TO_STR(x) case x: return #x;
103 switch (dwControlType) {
104 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_CUSTOM);
105 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BOOLEANMETER);
106 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SIGNEDMETER);
107 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PEAKMETER);
108 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_UNSIGNEDMETER);
109 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BOOLEAN);
110 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_ONOFF);
111 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MUTE);
112 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MONO);
113 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_LOUDNESS);
114 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_STEREOENH);
115 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BASS_BOOST);
116 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BUTTON);
117 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_DECIBELS);
118 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SIGNED);
119 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_UNSIGNED);
120 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PERCENT);
121 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SLIDER);
122 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PAN);
123 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_QSOUNDPAN);
124 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_FADER);
125 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_VOLUME);
126 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BASS);
127 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_TREBLE);
128 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_EQUALIZER);
129 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SINGLESELECT);
130 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MUX);
131 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MULTIPLESELECT);
132 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MIXER);
133 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MICROTIME);
134 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MILLITIME);
137 return wine_dbg_sprintf("UNKNOWN(%08x)", dwControlType);
140 /* A simple declaration of a line control
141 * These are each of the channels that show up
143 typedef struct line {
144 /* Name we present to outside world */
145 WCHAR name[MAXPNAMELEN];
151 snd_mixer_elem_t *elem;
154 /* A control structure, with toggle enabled switch
155 * Control structures control volume, muted, which capture source
157 typedef struct control {
166 WCHAR mixername[MAXPNAMELEN];
169 LPDRVCALLBACK callback;
170 DWORD_PTR callbackpriv;
177 #define MAX_MIXERS 32
178 #define CONTROLSPERLINE 3
182 static int cards = 0;
183 static mixer mixdev[MAX_MIXERS];
184 static HANDLE thread;
185 static int elem_callback(snd_mixer_elem_t *elem, unsigned int mask);
186 static DWORD WINAPI ALSA_MixerPollThread(LPVOID lParam);
187 static CRITICAL_SECTION elem_crst;
188 static int msg_pipe[2];
191 /* found channel names in alsa lib, alsa api doesn't have another way for this
192 * map name -> componenttype, worst case we get a wrong componenttype which is
196 static const struct mixerlinetype {
197 const char *name; DWORD cmpt;
199 { "Master", MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, },
200 { "Capture", MIXERLINE_COMPONENTTYPE_DST_WAVEIN, },
201 { "PCM", MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT, },
202 { "PC Speaker", MIXERLINE_COMPONENTTYPE_SRC_PCSPEAKER, },
203 { "Synth", MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER, },
204 { "Headphone", MIXERLINE_COMPONENTTYPE_DST_HEADPHONES, },
205 { "Mic", MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, },
206 { "Aux", MIXERLINE_COMPONENTTYPE_SRC_UNDEFINED, },
207 { "CD", MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC, },
208 { "Line", MIXERLINE_COMPONENTTYPE_SRC_LINE, },
209 { "Phone", MIXERLINE_COMPONENTTYPE_SRC_TELEPHONE, },
210 { "Digital", MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, },
211 { "Front Mic", MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, },
214 /* Map name to MIXERLINE_COMPONENTTYPE_XXX */
215 static int getcomponenttype(const char *name)
218 for (x=0; x< sizeof(converttable)/sizeof(converttable[0]); ++x)
219 if (!strcasecmp(name, converttable[x].name))
221 TRACE("%d -> %s\n", x, name);
222 return converttable[x].cmpt;
224 WARN("Unknown mixer name %s, probably harmless\n", name);
225 return MIXERLINE_COMPONENTTYPE_SRC_UNDEFINED;
228 /* Is this control suited for showing up? */
229 static int blacklisted(snd_mixer_elem_t *elem)
231 const char *name = snd_mixer_selem_get_name(elem);
234 if (!snd_mixer_selem_has_playback_volume(elem) &&
235 !snd_mixer_selem_has_capture_volume(elem))
238 TRACE("%s: %x\n", name, blisted);
242 static void fillcontrols(mixer *mmixer)
245 for (id = 0; id < mmixer->chans; ++id)
247 line *mline = &mmixer->lines[id];
248 int ofs = CONTROLSPERLINE * id;
252 TRACE("Filling control %d\n", id);
255 if (id == 1 && !mline->elem)
258 if (mline->capt && snd_mixer_selem_has_capture_volume(mline->elem))
259 snd_mixer_selem_get_capture_volume_range(mline->elem, &min, &max);
261 snd_mixer_selem_get_playback_volume_range(mline->elem, &min, &max);
263 /* (!snd_mixer_selem_has_playback_volume(elem) || snd_mixer_selem_has_capture_volume(elem)) */
264 /* Volume, always enabled by definition of blacklisted channels */
265 mmixer->controls[ofs].enabled = 1;
266 mmixer->controls[ofs].c.cbStruct = sizeof(mmixer->controls[ofs].c);
267 mmixer->controls[ofs].c.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
268 mmixer->controls[ofs].c.dwControlID = ofs;
269 mmixer->controls[ofs].c.Bounds.s1.dwMinimum = 0;
270 mmixer->controls[ofs].c.Bounds.s1.dwMaximum = 65535;
271 mmixer->controls[ofs].c.Metrics.cSteps = 65536/(max-min);
273 if ((id == 1 && snd_mixer_selem_has_capture_switch(mline->elem)) ||
274 (!mline->capt && snd_mixer_selem_has_playback_switch(mline->elem)))
275 { /* MUTE button optional, main capture channel should have one too */
276 mmixer->controls[ofs+OFS_MUTE].enabled = 1;
277 mmixer->controls[ofs+OFS_MUTE].c.cbStruct = sizeof(mmixer->controls[ofs].c);
278 mmixer->controls[ofs+OFS_MUTE].c.dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
279 mmixer->controls[ofs+OFS_MUTE].c.dwControlID = ofs+OFS_MUTE;
280 mmixer->controls[ofs+OFS_MUTE].c.Bounds.s1.dwMaximum = 1;
283 if (mline->capt && snd_mixer_selem_has_capture_switch_exclusive(mline->elem))
284 mmixer->controls[CONTROLSPERLINE+OFS_MUX].c.dwControlType = MIXERCONTROL_CONTROLTYPE_MUX;
287 { /* Capture select, in case cMultipleItems is 0, it means capture is disabled anyway */
288 mmixer->controls[ofs+OFS_MUX].enabled = 1;
289 mmixer->controls[ofs+OFS_MUX].c.cbStruct = sizeof(mmixer->controls[ofs].c);
290 mmixer->controls[ofs+OFS_MUX].c.dwControlType = MIXERCONTROL_CONTROLTYPE_MIXER;
291 mmixer->controls[ofs+OFS_MUX].c.dwControlID = ofs+OFS_MUX;
292 mmixer->controls[ofs+OFS_MUX].c.fdwControl = MIXERCONTROL_CONTROLF_MULTIPLE;
294 for (x = 0; x<mmixer->chans; ++x)
295 if (x != id && mmixer->lines[x].dst == id)
296 ++(mmixer->controls[ofs+OFS_MUX].c.cMultipleItems);
297 if (!mmixer->controls[ofs+OFS_MUX].c.cMultipleItems)
298 mmixer->controls[ofs+OFS_MUX].enabled = 0;
300 mmixer->controls[ofs+OFS_MUX].c.Bounds.s1.dwMaximum = mmixer->controls[ofs+OFS_MUX].c.cMultipleItems - 1;
301 mmixer->controls[ofs+OFS_MUX].c.Metrics.cSteps = mmixer->controls[ofs+OFS_MUX].c.cMultipleItems;
303 for (x=0; x<CONTROLSPERLINE; ++x)
305 lstrcpynW(mmixer->controls[ofs+x].c.szShortName, mline->name, sizeof(mmixer->controls[ofs+x].c.szShortName)/sizeof(WCHAR));
306 lstrcpynW(mmixer->controls[ofs+x].c.szName, mline->name, sizeof(mmixer->controls[ofs+x].c.szName)/sizeof(WCHAR));
311 /* get amount of channels for elem */
312 /* Officially we should keep capture/playback separated,
313 * but that's not going to work in the alsa api */
314 static int chans(mixer *mmixer, snd_mixer_elem_t * elem, DWORD capt)
318 if (capt && snd_mixer_selem_has_capture_volume(elem)) {
319 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
320 if (snd_mixer_selem_has_capture_channel(elem, chn))
323 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
324 if (snd_mixer_selem_has_playback_channel(elem, chn))
328 FIXME("Mixer channel %s was found for %s, but no channels were found? Wrong selection!\n", snd_mixer_selem_get_name(elem), (snd_mixer_selem_has_playback_volume(elem) ? "playback" : "capture"));
332 static void filllines(mixer *mmixer, snd_mixer_elem_t *mastelem, snd_mixer_elem_t *captelem, int capt)
334 snd_mixer_elem_t *elem;
335 line *mline = mmixer->lines;
339 MultiByteToWideChar(CP_UNIXCP, 0, snd_mixer_selem_get_name(mastelem), -1, mline->name, sizeof(mline->name)/sizeof(WCHAR));
340 mline->component = getcomponenttype(snd_mixer_selem_get_name(mastelem));
343 mline->elem = mastelem;
344 mline->chans = chans(mmixer, mastelem, 0);
346 snd_mixer_elem_set_callback(mastelem, &elem_callback);
347 snd_mixer_elem_set_callback_private(mastelem, mmixer);
349 MultiByteToWideChar(CP_UNIXCP, 0, "Empty Master Element", -1, mline->name, sizeof(mline->name)/sizeof(WCHAR));
353 * Note: since mmixer->dests = 1, it means only playback control is visible
354 * This makes sense, because if there are no capture sources capture control
355 * can't do anything and should be invisible */
357 /* Control 1 is reserved for capture even when not enabled */
361 MultiByteToWideChar(CP_UNIXCP, 0, snd_mixer_selem_get_name(captelem), -1, mline->name, sizeof(mline->name)/sizeof(WCHAR));
362 mline->component = getcomponenttype(snd_mixer_selem_get_name(captelem));
365 mline->elem = captelem;
366 mline->chans = chans(mmixer, captelem, 1);
368 snd_mixer_elem_set_callback(captelem, &elem_callback);
369 snd_mixer_elem_set_callback_private(captelem, mmixer);
372 for (elem = snd_mixer_first_elem(mmixer->mix); elem; elem = snd_mixer_elem_next(elem))
373 if (elem != mastelem && elem != captelem && !blacklisted(elem))
375 const char * name = snd_mixer_selem_get_name(elem);
376 DWORD comp = getcomponenttype(name);
378 if (snd_mixer_selem_has_playback_volume(elem) &&
379 (snd_mixer_selem_has_capture_volume(elem) || comp != MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE))
381 (++mline)->component = comp;
382 MultiByteToWideChar(CP_UNIXCP, 0, name, -1, mline->name, MAXPNAMELEN);
383 mline->capt = mline->dst = 0;
385 mline->chans = chans(mmixer, elem, 0);
390 if (capt && (snd_mixer_selem_has_capture_volume(elem) || comp == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE))
392 (++mline)->component = comp;
393 MultiByteToWideChar(CP_UNIXCP, 0, name, -1, mline->name, MAXPNAMELEN);
394 mline->capt = mline->dst = 1;
396 mline->chans = chans(mmixer, elem, 1);
399 snd_mixer_elem_set_callback(elem, &elem_callback);
400 snd_mixer_elem_set_callback_private(elem, mmixer);
404 static void filllines_no_master(mixer *mmixer, snd_mixer_elem_t *captelem, int capt)
406 line *mline = mmixer->lines;
408 MultiByteToWideChar(CP_UNIXCP, 0, snd_mixer_selem_get_name(captelem), -1, mline->name, sizeof(mline->name)/sizeof(WCHAR));
409 mline->component = getcomponenttype(snd_mixer_selem_get_name(captelem));
412 mline->elem = captelem;
413 mline->chans = chans(mmixer, captelem, 1);
415 snd_mixer_elem_set_callback(captelem, &elem_callback);
416 snd_mixer_elem_set_callback_private(captelem, mmixer);
419 /* Windows api wants to have a 'master' device to which all slaves are attached
420 * There are 2 ones in this code:
421 * - 'Master', fall back to 'Headphone' if unavailable, and if that's not available 'PCM'
423 * Capture might not always be available, so should be prepared to be without if needed
426 static void ALSA_MixerInit(void)
429 snd_ctl_card_info_t *info;
431 info = HeapAlloc( GetProcessHeap(), 0, snd_ctl_card_info_sizeof());
432 for (x = 0; x < MAX_MIXERS; ++x)
434 int card, err, capcontrols, total_elems = 0;
435 char cardind[6], cardname[10];
438 snd_mixer_elem_t *elem, *mastelem = NULL, *headelem = NULL, *captelem = NULL, *pcmelem = NULL, *micelem = NULL;
440 memset(info, 0, snd_ctl_card_info_sizeof());
441 memset(&mixdev[mixnum], 0, sizeof(*mixdev));
442 snprintf(cardind, sizeof(cardind), "%d", x);
443 card = snd_card_get_index(cardind);
447 snprintf(cardname, sizeof(cardname), "hw:%d", card);
449 err = snd_ctl_open(&ctl, cardname, 0);
452 WARN("Cannot open card: %s\n", snd_strerror(err));
456 err = snd_ctl_card_info(ctl, info);
459 WARN("Cannot get card info: %s\n", snd_strerror(err));
464 MultiByteToWideChar(CP_UNIXCP, 0, snd_ctl_card_info_get_name(info), -1, mixdev[mixnum].mixername, sizeof(mixdev[mixnum].mixername)/sizeof(WCHAR));
467 err = snd_mixer_open(&mixdev[mixnum].mix, 0);
470 WARN("Error occurred opening mixer: %s\n", snd_strerror(err));
474 err = snd_mixer_attach(mixdev[mixnum].mix, cardname);
478 err = snd_mixer_selem_register(mixdev[mixnum].mix, NULL, NULL);
482 err = snd_mixer_load(mixdev[mixnum].mix);
486 /* First, lets see what's available..
487 * If there are multiple Master or Captures, all except 1 will be added as slaves
489 total_elems = snd_mixer_get_count(mixdev[mixnum].mix);
490 TRACE("Total elems: %d\n", total_elems);
492 for (elem = snd_mixer_first_elem(mixdev[mixnum].mix); elem; elem = snd_mixer_elem_next(elem))
493 if (!strcasecmp(snd_mixer_selem_get_name(elem), "Master") && !mastelem)
496 ++(mixdev[mixnum].chans);
498 else if (!strcasecmp(snd_mixer_selem_get_name(elem), "Capture") && !captelem)
500 else if (!strcasecmp(snd_mixer_selem_get_name(elem), "Mic") && !micelem && !mastelem && total_elems == 1)
501 /* this is what snd-usb-audio mics look like; just a Mic control and that's it.*/
503 else if (!blacklisted(elem))
505 DWORD comp = getcomponenttype(snd_mixer_selem_get_name(elem));
508 /* Work around buggy drivers: Make this a capture control if the name is recognised as a microphone */
509 if (snd_mixer_selem_has_capture_volume(elem))
511 else if (comp == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE)
517 if (!skip && snd_mixer_selem_has_playback_volume(elem))
519 if (!strcasecmp(snd_mixer_selem_get_name(elem), "Headphone") && !headelem)
521 else if (!strcasecmp(snd_mixer_selem_get_name(elem), "PCM") && !pcmelem)
523 ++(mixdev[mixnum].chans);
527 /* Add dummy capture channel, wanted by Windows */
528 mixdev[mixnum].chans += 1;
530 /* If there is only 'Capture' and 'Master', this device is not worth it */
531 if (mixdev[mixnum].chans == 2)
533 WARN("No channels found, skipping device!\n");
537 /* Master element can't have a capture control in this code, so
538 * if Headphone or PCM is promoted to master, unset its capture control */
539 if (headelem && !mastelem)
541 /* Using 'Headphone' as master device */
543 capcontrols -= !!snd_mixer_selem_has_capture_switch(mastelem);
545 else if (pcmelem && !mastelem)
547 /* Use 'PCM' as master device */
549 capcontrols -= !!snd_mixer_selem_has_capture_switch(mastelem);
551 else if (!mastelem && !captelem && !micelem)
553 /* If there is nothing sensible that can act as 'Master' control, something is wrong */
554 FIXME("No master control found on %s, disabling mixer\n", snd_ctl_card_info_get_name(info));
558 if (!captelem || !capcontrols)
560 /* Can't enable capture, so disabling it
561 * Note: capture control will still exist because
562 * dwLineID 0 and 1 are reserved for Master and Capture
564 WARN("No use enabling capture part of mixer, capture control found: %s, amount of capture controls: %d\n",
565 (!captelem ? "no" : "yes"), capcontrols);
567 mixdev[mixnum].dests = 1;
571 mixdev[mixnum].chans += capcontrols;
572 mixdev[mixnum].dests = 2;
575 mixdev[mixnum].lines = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(line) * mixdev[mixnum].chans);
576 mixdev[mixnum].controls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(control) * CONTROLSPERLINE*mixdev[mixnum].chans);
578 if (!mixdev[mixnum].lines || !mixdev[mixnum].controls)
582 filllines(&mixdev[mixnum], mastelem, captelem, capcontrols);
584 filllines_no_master(&mixdev[mixnum], micelem, 1);
585 fillcontrols(&mixdev[mixnum]);
587 TRACE("%s: Amount of controls: %i/%i, name: %s\n", cardname, mixdev[mixnum].dests, mixdev[mixnum].chans, debugstr_w(mixdev[mixnum].mixername));
592 WARN("Error occurred initialising mixer: %s\n", snd_strerror(err));
594 HeapFree(GetProcessHeap(), 0, mixdev[mixnum].lines);
595 HeapFree(GetProcessHeap(), 0, mixdev[mixnum].controls);
596 snd_mixer_close(mixdev[mixnum].mix);
599 HeapFree( GetProcessHeap(), 0, info );
601 /* There is no trouble with already assigning callbacks without initialising critsect:
602 * Callbacks only occur when snd_mixer_handle_events is called (only happens in thread)
604 InitializeCriticalSection(&elem_crst);
605 elem_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_MIXER.elem_crst");
609 static void ALSA_MixerExit(void)
615 WARN("Callback thread still alive, terminating uncleanly, refcnt: %d\n", refcnt);
616 /* Least we can do is making sure we're not in 'foreign' code */
617 EnterCriticalSection(&elem_crst);
618 TerminateThread(thread, 1);
620 LeaveCriticalSection(&elem_crst);
623 TRACE("Cleaning up\n");
625 elem_crst.DebugInfo->Spare[0] = 0;
626 DeleteCriticalSection(&elem_crst);
627 for (x = 0; x < cards; ++x)
629 snd_mixer_close(mixdev[x].mix);
630 HeapFree(GetProcessHeap(), 0, mixdev[x].lines);
631 HeapFree(GetProcessHeap(), 0, mixdev[x].controls);
636 static mixer* MIX_GetMix(UINT wDevID)
642 WARN("Invalid mixer id: %d\n", wDevID);
646 mmixer = &mixdev[wDevID];
650 /* Since alsa doesn't tell what exactly changed, just assume all affected controls changed */
651 static int elem_callback(snd_mixer_elem_t *elem, unsigned int type)
653 mixer *mmixer = snd_mixer_elem_get_callback_private(elem);
655 BOOL captchanged = 0;
657 if (type != SND_CTL_EVENT_MASK_VALUE)
662 EnterCriticalSection(&elem_crst);
664 if (!mmixer->callback)
667 for (x=0; x<mmixer->chans; ++x)
669 const int ofs = CONTROLSPERLINE*x;
670 if (elem != mmixer->lines[x].elem)
673 if (mmixer->lines[x].capt)
676 TRACE("Found changed control %s\n", debugstr_w(mmixer->lines[x].name));
677 mmixer->callback(mmixer->hmx, MM_MIXM_LINE_CHANGE, mmixer->callbackpriv, x, 0);
678 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, ofs, 0);
680 if (mmixer->controls[ofs+OFS_MUTE].enabled)
681 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, ofs+OFS_MUTE, 0);
684 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, CONTROLSPERLINE+OFS_MUX, 0);
687 LeaveCriticalSection(&elem_crst);
692 static DWORD WINAPI ALSA_MixerPollThread(LPVOID lParam)
694 struct pollfd *pfds = NULL;
695 int x, y, err, mcnt, count = 1;
697 TRACE("%p\n", lParam);
699 for (x = 0; x < cards; ++x)
700 count += snd_mixer_poll_descriptors_count(mixdev[x].mix);
702 TRACE("Counted %d descriptors\n", count);
703 pfds = HeapAlloc(GetProcessHeap(), 0, count * sizeof(struct pollfd));
707 WARN("Out of memory\n");
711 pfds[0].fd = msg_pipe[0];
712 pfds[0].events = POLLIN;
715 for (x = 0; x < cards; ++x)
716 y += snd_mixer_poll_descriptors(mixdev[x].mix, &pfds[y], count - y);
718 while ((err = poll(pfds, (unsigned int) count, -1)) >= 0 || errno == EINTR || errno == EAGAIN)
720 if (pfds[0].revents & POLLIN)
724 for (x = y = 0; x < cards; ++x)
726 int j, max = snd_mixer_poll_descriptors_count(mixdev[x].mix);
727 for (j = 0; j < max; ++j)
728 if (pfds[mcnt+j].revents)
730 y += snd_mixer_handle_events(mixdev[x].mix);
736 TRACE("Handled %d events\n", y);
740 TRACE("Shutting down\n");
741 HeapFree(GetProcessHeap(), 0, pfds);
743 y = read(msg_pipe[0], &x, sizeof(x));
749 static DWORD MIX_Open(UINT wDevID, LPMIXEROPENDESC desc, DWORD_PTR flags)
751 mixer *mmixer = MIX_GetMix(wDevID);
753 return MMSYSERR_BADDEVICEID;
755 flags &= CALLBACK_TYPEMASK;
761 case CALLBACK_FUNCTION:
765 FIXME("Unhandled callback type: %08lx\n", flags & CALLBACK_TYPEMASK);
766 return MIXERR_INVALVALUE;
769 mmixer->callback = (LPDRVCALLBACK)desc->dwCallback;
770 mmixer->callbackpriv = desc->dwInstance;
771 mmixer->hmx = (HDRVR)desc->hmx;
774 if (InterlockedIncrement(&refcnt) == 1)
776 if (pipe(msg_pipe) >= 0)
778 thread = CreateThread(NULL, 0, ALSA_MixerPollThread, NULL, 0, NULL);
783 msg_pipe[0] = msg_pipe[1] = -1;
787 msg_pipe[0] = msg_pipe[1] = -1;
790 return MMSYSERR_NOERROR;
793 static DWORD MIX_Close(UINT wDevID)
796 mixer *mmixer = MIX_GetMix(wDevID);
798 return MMSYSERR_BADDEVICEID;
800 EnterCriticalSection(&elem_crst);
801 mmixer->callback = 0;
802 LeaveCriticalSection(&elem_crst);
804 if (!InterlockedDecrement(&refcnt))
806 if (write(msg_pipe[1], &x, sizeof(x)) > 0)
808 TRACE("Shutting down thread...\n");
809 WaitForSingleObject(thread, INFINITE);
814 return MMSYSERR_NOERROR;
817 static DWORD MIX_GetDevCaps(UINT wDevID, LPMIXERCAPS2W caps, DWORD_PTR parm2)
819 mixer *mmixer = MIX_GetMix(wDevID);
823 return MMSYSERR_INVALPARAM;
826 return MMSYSERR_BADDEVICEID;
828 memset(&capsW, 0, sizeof(MIXERCAPS2W));
830 capsW.wMid = WINE_MIXER_MANUF_ID;
831 capsW.wPid = WINE_MIXER_PRODUCT_ID;
832 capsW.vDriverVersion = WINE_MIXER_VERSION;
834 lstrcpynW(capsW.szPname, mmixer->mixername, sizeof(capsW.szPname)/sizeof(WCHAR));
835 capsW.cDestinations = mmixer->dests;
836 memcpy(caps, &capsW, min(parm2, sizeof(capsW)));
837 return MMSYSERR_NOERROR;
840 /* convert win32 volume to alsa volume, and vice versa */
841 static INT normalized(INT value, INT prevmax, INT nextmax)
843 int ret = MulDiv(value, nextmax, prevmax);
845 /* Have to stay in range */
846 TRACE("%d/%d -> %d/%d\n", value, prevmax, ret, nextmax);
855 /* get amount of sources for dest */
856 static int getsrccntfromchan(mixer *mmixer, int dad)
860 for (i=0; i<mmixer->chans; ++i)
861 if (i != dad && mmixer->lines[i].dst == dad)
866 FIXME("No src found for %i (%s)?\n", dad, debugstr_w(mmixer->lines[dad].name));
870 /* find lineid for source 'num' with dest 'dad' */
871 static int getsrclinefromchan(mixer *mmixer, int dad, int num)
874 for (i=0; i<mmixer->chans; ++i)
875 if (i != dad && mmixer->lines[i].dst == dad)
881 WARN("No src found for src %i from dest %i\n", num, dad);
885 /* get the source number belonging to line */
886 static int getsrcfromline(mixer *mmixer, int line)
888 int i, j=0, dad = mmixer->lines[line].dst;
890 for (i=0; i<mmixer->chans; ++i)
891 if (i != dad && mmixer->lines[i].dst == dad)
897 WARN("No src found for line %i with dad %i\n", line, dad);
901 /* Get volume/muted/capture channel */
902 static DWORD MIX_GetControlDetails(UINT wDevID, LPMIXERCONTROLDETAILS mctrld, DWORD_PTR flags)
904 mixer *mmixer = MIX_GetMix(wDevID);
910 return MMSYSERR_INVALPARAM;
912 ctrl = mctrld->dwControlID;
913 line = ctrl/CONTROLSPERLINE;
915 if (mctrld->cbStruct != sizeof(*mctrld))
916 return MMSYSERR_INVALPARAM;
919 return MMSYSERR_BADDEVICEID;
921 if (line >= mmixer->chans || !mmixer->controls[ctrl].enabled)
922 return MIXERR_INVALCONTROL;
924 ct = &mmixer->controls[ctrl];
926 flags &= MIXER_GETCONTROLDETAILSF_QUERYMASK;
929 case MIXER_GETCONTROLDETAILSF_VALUE:
930 TRACE("MIXER_GETCONTROLDETAILSF_VALUE (%d/%d)\n", ctrl, line);
931 switch (ct->c.dwControlType)
933 case MIXERCONTROL_CONTROLTYPE_VOLUME:
935 long min = 0, max = 0, vol = 0;
937 LPMIXERCONTROLDETAILS_UNSIGNED mcdu;
938 snd_mixer_elem_t * elem = mmixer->lines[line].elem;
940 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_UNSIGNED))
942 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
943 return MMSYSERR_INVALPARAM;
946 TRACE("%s MIXERCONTROLDETAILS_UNSIGNED[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
948 mcdu = mctrld->paDetails;
950 if (mctrld->cChannels != 1 && mmixer->lines[line].chans != mctrld->cChannels)
952 WARN("Unsupported cChannels (%d instead of %d)\n", mctrld->cChannels, mmixer->lines[line].chans);
953 return MMSYSERR_INVALPARAM;
956 if (mmixer->lines[line].capt && snd_mixer_selem_has_capture_volume(elem)) {
957 snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
958 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
959 if (snd_mixer_selem_has_capture_channel(elem, chn))
961 snd_mixer_selem_get_capture_volume(elem, chn, &vol);
962 mcdu->dwValue = normalized(vol - min, max, 65535);
963 if (mctrld->cChannels == 1)
968 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
970 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
971 if (snd_mixer_selem_has_playback_channel(elem, chn))
973 snd_mixer_selem_get_playback_volume(elem, chn, &vol);
974 mcdu->dwValue = normalized(vol - min, max, 65535);
975 if (mctrld->cChannels == 1)
981 return MMSYSERR_NOERROR;
984 case MIXERCONTROL_CONTROLTYPE_ONOFF:
985 case MIXERCONTROL_CONTROLTYPE_MUTE:
987 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
989 snd_mixer_elem_t * elem = mmixer->lines[line].elem;
991 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
993 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
994 return MMSYSERR_INVALPARAM;
997 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
999 mcdb = mctrld->paDetails;
1002 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1004 if (!snd_mixer_selem_has_capture_channel(elem, chn))
1006 snd_mixer_selem_get_capture_switch(elem, chn, &ival);
1010 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1012 if (!snd_mixer_selem_has_playback_channel(elem, chn))
1014 snd_mixer_selem_get_playback_switch(elem, chn, &ival);
1018 if (chn > SND_MIXER_SCHN_LAST)
1020 TRACE("can't find active channel\n");
1021 return MMSYSERR_INVALPARAM; /* fixme: what's right error? */
1024 mcdb->fValue = !ival;
1025 TRACE("=> %s\n", mcdb->fValue ? "on" : "off");
1026 return MMSYSERR_NOERROR;
1028 case MIXERCONTROL_CONTROLTYPE_MIXER:
1029 case MIXERCONTROL_CONTROLTYPE_MUX:
1031 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
1032 int x, i=0, ival = 0, chn;
1034 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
1036 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1037 return MMSYSERR_INVALPARAM;
1040 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1042 mcdb = mctrld->paDetails;
1044 for (x = 0; x<mmixer->chans; ++x)
1045 if (line != x && mmixer->lines[x].dst == line)
1048 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1050 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1052 snd_mixer_selem_get_capture_switch(mmixer->lines[x].elem, chn, &ival);
1056 if (i >= mctrld->u.cMultipleItems)
1058 TRACE("overflow\n");
1059 return MMSYSERR_INVALPARAM;
1061 TRACE("fVal[%i] = %sselected\n", i, (!ival ? "un" : ""));
1062 mcdb[i++].fValue = ival;
1068 FIXME("Unhandled controltype %s\n", getControlType(ct->c.dwControlType));
1069 return MMSYSERR_INVALPARAM;
1071 return MMSYSERR_NOERROR;
1073 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
1074 TRACE("MIXER_GETCONTROLDETAILSF_LISTTEXT (%d)\n", ctrl);
1076 if (ct->c.dwControlType == MIXERCONTROL_CONTROLTYPE_MUX || ct->c.dwControlType == MIXERCONTROL_CONTROLTYPE_MIXER)
1078 LPMIXERCONTROLDETAILS_LISTTEXTW mcdlt = mctrld->paDetails;
1081 for (i = j = 0; j < mmixer->chans; ++j)
1082 if (j != line && mmixer->lines[j].dst == line)
1084 if (i > mctrld->u.cMultipleItems)
1085 return MMSYSERR_INVALPARAM;
1086 mcdlt->dwParam1 = j;
1087 mcdlt->dwParam2 = mmixer->lines[j].component;
1088 lstrcpynW(mcdlt->szName, mmixer->lines[j].name, sizeof(mcdlt->szName) / sizeof(WCHAR));
1089 TRACE("Adding %i as %s\n", j, debugstr_w(mcdlt->szName));
1092 if (i < mctrld->u.cMultipleItems)
1093 return MMSYSERR_INVALPARAM;
1094 return MMSYSERR_NOERROR;
1096 FIXME ("Imagine this code being horribly broken and incomplete, introducing: reality\n");
1097 return MMSYSERR_INVALPARAM;
1100 WARN("Unknown flag (%08lx)\n", flags);
1101 return MMSYSERR_INVALPARAM;
1105 /* Set volume/capture channel/muted for control */
1106 static DWORD MIX_SetControlDetails(UINT wDevID, LPMIXERCONTROLDETAILS mctrld, DWORD_PTR flags)
1108 mixer *mmixer = MIX_GetMix(wDevID);
1109 DWORD ctrl, line, i;
1111 snd_mixer_elem_t * elem;
1114 return MMSYSERR_INVALPARAM;
1116 ctrl = mctrld->dwControlID;
1117 line = ctrl/CONTROLSPERLINE;
1119 if (mctrld->cbStruct != sizeof(*mctrld))
1121 WARN("Invalid size of mctrld %d\n", mctrld->cbStruct);
1122 return MMSYSERR_INVALPARAM;
1126 return MMSYSERR_BADDEVICEID;
1128 if (line >= mmixer->chans)
1130 WARN("Invalid line id: %d not in range of 0-%d\n", line, mmixer->chans-1);
1131 return MMSYSERR_INVALPARAM;
1134 if (!mmixer->controls[ctrl].enabled)
1136 WARN("Control %d not enabled\n", ctrl);
1137 return MIXERR_INVALCONTROL;
1140 ct = &mmixer->controls[ctrl];
1141 elem = mmixer->lines[line].elem;
1142 flags &= MIXER_SETCONTROLDETAILSF_QUERYMASK;
1145 case MIXER_SETCONTROLDETAILSF_VALUE:
1146 TRACE("MIXER_SETCONTROLDETAILSF_VALUE (%d)\n", ctrl);
1150 WARN("Unknown flag (%08lx)\n", flags);
1151 return MMSYSERR_INVALPARAM;
1154 switch (ct->c.dwControlType)
1156 case MIXERCONTROL_CONTROLTYPE_VOLUME:
1158 long min = 0, max = 0;
1160 LPMIXERCONTROLDETAILS_UNSIGNED mcdu;
1161 snd_mixer_elem_t * elem = mmixer->lines[line].elem;
1163 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_UNSIGNED))
1165 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1166 return MMSYSERR_INVALPARAM;
1169 if (mctrld->cChannels != 1 && mmixer->lines[line].chans != mctrld->cChannels)
1171 WARN("Unsupported cChannels (%d instead of %d)\n", mctrld->cChannels, mmixer->lines[line].chans);
1172 return MMSYSERR_INVALPARAM;
1175 TRACE("%s MIXERCONTROLDETAILS_UNSIGNED[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1176 mcdu = mctrld->paDetails;
1178 for (chn=0; chn<mctrld->cChannels;++chn)
1180 TRACE("Chan %d value %d\n", chn, mcdu[chn].dwValue);
1183 /* There isn't always a capture volume, so in that case change playback volume */
1184 if (mmixer->lines[line].capt && snd_mixer_selem_has_capture_volume(elem))
1186 snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
1188 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1189 if (snd_mixer_selem_has_capture_channel(elem, chn))
1191 snd_mixer_selem_set_capture_volume(elem, chn, min + normalized(mcdu->dwValue, 65535, max));
1192 if (mctrld->cChannels != 1)
1198 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
1200 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1201 if (snd_mixer_selem_has_playback_channel(elem, chn))
1203 snd_mixer_selem_set_playback_volume(elem, chn, min + normalized(mcdu->dwValue, 65535, max));
1204 if (mctrld->cChannels != 1)
1211 case MIXERCONTROL_CONTROLTYPE_MUTE:
1212 case MIXERCONTROL_CONTROLTYPE_ONOFF:
1214 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
1216 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
1218 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1219 return MMSYSERR_INVALPARAM;
1222 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1224 mcdb = mctrld->paDetails;
1225 if (line == 1) /* Mute/unmute capturing */
1226 for (i = 0; i <= SND_MIXER_SCHN_LAST; ++i)
1228 if (snd_mixer_selem_has_capture_channel(elem, i))
1229 snd_mixer_selem_set_capture_switch(elem, i, !mcdb->fValue);
1232 for (i = 0; i <= SND_MIXER_SCHN_LAST; ++i)
1233 if (snd_mixer_selem_has_playback_channel(elem, i))
1234 snd_mixer_selem_set_playback_switch(elem, i, !mcdb->fValue);
1238 case MIXERCONTROL_CONTROLTYPE_MIXER:
1239 case MIXERCONTROL_CONTROLTYPE_MUX:
1241 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
1243 int didone = 0, canone = (ct->c.dwControlType == MIXERCONTROL_CONTROLTYPE_MUX);
1245 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
1247 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1248 return MMSYSERR_INVALPARAM;
1251 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1252 mcdb = mctrld->paDetails;
1254 for (x=i=0; x < mmixer->chans; ++x)
1255 if (line != x && mmixer->lines[x].dst == line)
1257 TRACE("fVal[%i] (%s) = %i\n", i, debugstr_w(mmixer->lines[x].name), mcdb[i].fValue);
1258 if (i >= mctrld->u.cMultipleItems)
1260 TRACE("Too many items to fit, overflowing\n");
1261 return MIXERR_INVALVALUE;
1263 if (mcdb[i].fValue && canone && didone)
1265 TRACE("Nice try, but it's not going to work\n");
1266 elem_callback(mmixer->lines[1].elem, SND_CTL_EVENT_MASK_VALUE);
1267 return MIXERR_INVALVALUE;
1274 if (canone && !didone)
1276 TRACE("Nice try, this is not going to work either\n");
1277 elem_callback(mmixer->lines[1].elem, SND_CTL_EVENT_MASK_VALUE);
1278 return MIXERR_INVALVALUE;
1281 for (x = i = 0; x<mmixer->chans; ++x)
1282 if (line != x && mmixer->lines[x].dst == line)
1285 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1287 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1289 snd_mixer_selem_set_capture_switch(mmixer->lines[x].elem, chn, mcdb[i].fValue);
1294 /* If it's a MUX, it means that only 1 channel can be selected
1295 * and the other channels are unselected
1297 * For MIXER multiple sources are allowed, so unselect here
1302 for (x = i = 0; x<mmixer->chans; ++x)
1303 if (line != x && mmixer->lines[x].dst == line)
1305 if (!mcdb[i].fValue)
1306 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1308 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1310 snd_mixer_selem_set_capture_switch(mmixer->lines[x].elem, chn, mcdb[i].fValue);
1317 FIXME("Unhandled type %s\n", getControlType(ct->c.dwControlType));
1318 return MMSYSERR_INVALPARAM;
1320 return MMSYSERR_NOERROR;
1323 /* Here we give info over the source/dest line given by dwSource+dwDest or dwDest, respectively
1324 * It is also possible that a line is found by componenttype or target type, latter is not implemented yet
1325 * Most important values returned in struct:
1328 * line control count
1329 * amount of channels
1331 static DWORD MIX_GetLineInfo(UINT wDevID, LPMIXERLINEW Ml, DWORD_PTR flags)
1333 DWORD_PTR qf = flags & MIXER_GETLINEINFOF_QUERYMASK;
1334 mixer *mmixer = MIX_GetMix(wDevID);
1341 return MMSYSERR_INVALPARAM;
1346 WARN("Device %u not found\n", wDevID);
1347 return MMSYSERR_BADDEVICEID;
1350 if (Ml->cbStruct != sizeof(*Ml))
1352 WARN("invalid parameter: Ml->cbStruct = %d\n", Ml->cbStruct);
1353 return MMSYSERR_INVALPARAM;
1357 Ml->fdwLine = MIXERLINE_LINEF_DISCONNECTED;
1360 case MIXER_GETLINEINFOF_COMPONENTTYPE:
1362 Ml->dwLineID = 0xFFFF;
1363 TRACE("Looking for componenttype %d/%x\n", Ml->dwComponentType, Ml->dwComponentType);
1364 for (idx = 0; idx < mmixer->chans; ++idx)
1365 if (mmixer->lines[idx].component == Ml->dwComponentType)
1370 if (Ml->dwLineID == 0xFFFF)
1371 return MMSYSERR_KEYNOTFOUND;
1372 /* Now that we have lineid, fallback to lineid*/
1375 case MIXER_GETLINEINFOF_LINEID:
1376 if (Ml->dwLineID >= mmixer->chans)
1377 return MIXERR_INVALLINE;
1379 TRACE("MIXER_GETLINEINFOF_LINEID %d\n", Ml->dwLineID);
1380 Ml->dwDestination = mmixer->lines[Ml->dwLineID].dst;
1382 if (Ml->dwDestination != Ml->dwLineID)
1384 Ml->dwSource = getsrcfromline(mmixer, Ml->dwLineID);
1385 Ml->cConnections = 1;
1389 Ml->cConnections = getsrccntfromchan(mmixer, Ml->dwLineID);
1390 Ml->dwSource = 0xFFFFFFFF;
1392 TRACE("Connections %d, source %d\n", Ml->cConnections, Ml->dwSource);
1395 case MIXER_GETLINEINFOF_DESTINATION:
1396 if (Ml->dwDestination >= mmixer->dests)
1398 WARN("dest %d out of bounds\n", Ml->dwDestination);
1399 return MIXERR_INVALLINE;
1402 Ml->dwLineID = Ml->dwDestination;
1403 Ml->cConnections = getsrccntfromchan(mmixer, Ml->dwLineID);
1404 Ml->dwSource = 0xFFFFFFFF;
1407 case MIXER_GETLINEINFOF_SOURCE:
1408 if (Ml->dwDestination >= mmixer->dests)
1410 WARN("dest %d for source out of bounds\n", Ml->dwDestination);
1411 return MIXERR_INVALLINE;
1414 if (Ml->dwSource >= getsrccntfromchan(mmixer, Ml->dwDestination))
1416 WARN("src %d out of bounds\n", Ml->dwSource);
1417 return MIXERR_INVALLINE;
1420 Ml->dwLineID = getsrclinefromchan(mmixer, Ml->dwDestination, Ml->dwSource);
1421 Ml->cConnections = 1;
1424 case MIXER_GETLINEINFOF_TARGETTYPE:
1425 FIXME("TODO: TARGETTYPE, stub\n");
1426 return MMSYSERR_INVALPARAM;
1429 FIXME("Unknown query flag: %08lx\n", qf);
1430 return MMSYSERR_INVALPARAM;
1433 Ml->fdwLine &= ~MIXERLINE_LINEF_DISCONNECTED;
1434 Ml->fdwLine |= MIXERLINE_LINEF_ACTIVE;
1435 if (Ml->dwLineID >= mmixer->dests)
1436 Ml->fdwLine |= MIXERLINE_LINEF_SOURCE;
1438 mline = &mmixer->lines[Ml->dwLineID];
1439 Ml->dwComponentType = mline->component;
1440 Ml->cChannels = mmixer->lines[Ml->dwLineID].chans;
1443 for (i=CONTROLSPERLINE*Ml->dwLineID;i<CONTROLSPERLINE*(Ml->dwLineID+1); ++i)
1444 if (mmixer->controls[i].enabled)
1447 lstrcpynW(Ml->szShortName, mmixer->lines[Ml->dwLineID].name, sizeof(Ml->szShortName)/sizeof(WCHAR));
1448 lstrcpynW(Ml->szName, mmixer->lines[Ml->dwLineID].name, sizeof(Ml->szName)/sizeof(WCHAR));
1450 Ml->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
1452 Ml->Target.dwType = MIXERLINE_TARGETTYPE_WAVEOUT;
1453 Ml->Target.dwDeviceID = 0xFFFFFFFF;
1454 Ml->Target.wMid = WINE_MIXER_MANUF_ID;
1455 Ml->Target.wPid = WINE_MIXER_PRODUCT_ID;
1456 Ml->Target.vDriverVersion = WINE_MIXER_VERSION;
1457 lstrcpynW(Ml->Target.szPname, mmixer->mixername, sizeof(Ml->Target.szPname)/sizeof(WCHAR));
1458 return MMSYSERR_NOERROR;
1461 /* Get the controls that belong to a certain line, either all or 1 */
1462 static DWORD MIX_GetLineControls(UINT wDevID, LPMIXERLINECONTROLSW mlc, DWORD_PTR flags)
1464 mixer *mmixer = MIX_GetMix(wDevID);
1468 if (!mlc || mlc->cbStruct != sizeof(*mlc))
1470 WARN("Invalid mlc %p, cbStruct: %d\n", mlc, (!mlc ? -1 : mlc->cbStruct));
1471 return MMSYSERR_INVALPARAM;
1474 if (mlc->cbmxctrl != sizeof(MIXERCONTROLW))
1476 WARN("cbmxctrl %d\n", mlc->cbmxctrl);
1477 return MMSYSERR_INVALPARAM;
1481 return MMSYSERR_BADDEVICEID;
1483 flags &= MIXER_GETLINECONTROLSF_QUERYMASK;
1485 if (flags == MIXER_GETLINECONTROLSF_ONEBYID)
1486 mlc->dwLineID = mlc->u.dwControlID / CONTROLSPERLINE;
1488 if (mlc->dwLineID >= mmixer->chans)
1490 TRACE("Invalid dwLineID %d\n", mlc->dwLineID);
1491 return MIXERR_INVALLINE;
1496 case MIXER_GETLINECONTROLSF_ALL:
1497 TRACE("line=%08x MIXER_GETLINECONTROLSF_ALL (%d)\n", mlc->dwLineID, mlc->cControls);
1498 for (i = 0; i < CONTROLSPERLINE; ++i)
1499 if (mmixer->controls[i+mlc->dwLineID * CONTROLSPERLINE].enabled)
1501 memcpy(&mlc->pamxctrl[j], &mmixer->controls[i+mlc->dwLineID * CONTROLSPERLINE].c, sizeof(MIXERCONTROLW));
1502 TRACE("Added %s (%s)\n", debugstr_w(mlc->pamxctrl[j].szShortName), debugstr_w(mlc->pamxctrl[j].szName));
1504 if (j > mlc->cControls)
1506 WARN("invalid parameter\n");
1507 return MMSYSERR_INVALPARAM;
1511 if (!j || mlc->cControls > j)
1513 WARN("invalid parameter\n");
1514 return MMSYSERR_INVALPARAM;
1517 case MIXER_GETLINECONTROLSF_ONEBYID:
1518 TRACE("line=%08x MIXER_GETLINECONTROLSF_ONEBYID (%x)\n", mlc->dwLineID, mlc->u.dwControlID);
1520 if (!mmixer->controls[mlc->u.dwControlID].enabled)
1521 return MIXERR_INVALCONTROL;
1523 mlc->pamxctrl[0] = mmixer->controls[mlc->u.dwControlID].c;
1525 case MIXER_GETLINECONTROLSF_ONEBYTYPE:
1526 TRACE("line=%08x MIXER_GETLINECONTROLSF_ONEBYTYPE (%s)\n", mlc->dwLineID, getControlType(mlc->u.dwControlType));
1528 ct = mlc->u.dwControlType & MIXERCONTROL_CT_CLASS_MASK;
1529 for (i = 0; i <= CONTROLSPERLINE; ++i)
1531 const int ofs = i+mlc->dwLineID*CONTROLSPERLINE;
1532 if (i == CONTROLSPERLINE)
1534 WARN("invalid parameter: control %s not found\n", getControlType(mlc->u.dwControlType));
1535 return MIXERR_INVALCONTROL;
1537 if (mmixer->controls[ofs].enabled && (mmixer->controls[ofs].c.dwControlType & MIXERCONTROL_CT_CLASS_MASK) == ct)
1539 mlc->pamxctrl[0] = mmixer->controls[ofs].c;
1545 FIXME("Unknown flag %08lx\n", flags & MIXER_GETLINECONTROLSF_QUERYMASK);
1546 return MMSYSERR_INVALPARAM;
1549 return MMSYSERR_NOERROR;
1552 #endif /*HAVE_ALSA*/
1554 /**************************************************************************
1555 * mxdMessage (WINEALSA.3)
1557 DWORD WINAPI ALSA_mxdMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1558 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1562 TRACE("(%04X, %s, %08lX, %08lX, %08lX);\n", wDevID, getMessage(wMsg),
1563 dwUser, dwParam1, dwParam2);
1567 case DRVM_INIT: ALSA_MixerInit(); ret = MMSYSERR_NOERROR; break;
1568 case DRVM_EXIT: ALSA_MixerExit(); ret = MMSYSERR_NOERROR; break;
1569 /* All taken care of by driver initialisation */
1570 /* Unimplemented, and not needed */
1573 ret = MMSYSERR_NOERROR; break;
1576 ret = MIX_Open(wDevID, (LPMIXEROPENDESC) dwParam1, dwParam2); break;
1579 ret = MIX_Close(wDevID); break;
1581 case MXDM_GETDEVCAPS:
1582 ret = MIX_GetDevCaps(wDevID, (LPMIXERCAPS2W)dwParam1, dwParam2); break;
1584 case MXDM_GETLINEINFO:
1585 ret = MIX_GetLineInfo(wDevID, (LPMIXERLINEW)dwParam1, dwParam2); break;
1587 case MXDM_GETLINECONTROLS:
1588 ret = MIX_GetLineControls(wDevID, (LPMIXERLINECONTROLSW)dwParam1, dwParam2); break;
1590 case MXDM_GETCONTROLDETAILS:
1591 ret = MIX_GetControlDetails(wDevID, (LPMIXERCONTROLDETAILS)dwParam1, dwParam2); break;
1593 case MXDM_SETCONTROLDETAILS:
1594 ret = MIX_SetControlDetails(wDevID, (LPMIXERCONTROLDETAILS)dwParam1, dwParam2); break;
1596 case MXDM_GETNUMDEVS:
1600 WARN("unknown message %s!\n", getMessage(wMsg));
1601 return MMSYSERR_NOTSUPPORTED;
1604 TRACE("Returning %08X\n", ret);
1607 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1609 return MMSYSERR_NOTENABLED;
1610 #endif /*HAVE_ALSA*/