Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / winealsa.drv / mixer.c
blob93c8927ba58fc760c706f19bcbdae3d025170083
1 /*
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <assert.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 # include <sys/ioctl.h>
37 #endif
39 #define NONAMELESSUNION
40 #define NONAMELESSSTRUCT
42 #include "windef.h"
43 #include "winbase.h"
44 #include "wingdi.h"
45 #include "winuser.h"
46 #include "winnls.h"
47 #include "mmddk.h"
48 #include "mmsystem.h"
49 #include "alsa.h"
50 #include "wine/unicode.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(mixer);
55 #ifdef HAVE_ALSA
57 #define WINE_MIXER_MANUF_ID 0xAA
58 #define WINE_MIXER_PRODUCT_ID 0x55
59 #define WINE_MIXER_VERSION 0x0100
61 /* Generic notes:
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
66 * Mute is optional
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;
81 switch (uMsg){
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);
94 default: break;
96 #undef MSG_TO_STR
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);
136 #undef TYPE_TO_STR
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];
147 DWORD component;
148 DWORD dst;
149 DWORD capt;
150 DWORD chans;
151 snd_mixer_elem_t *elem;
152 } line;
154 /* A control structure, with toggle enabled switch
155 * Control structures control volume, muted, which capture source
157 typedef struct control {
158 BOOL enabled;
159 MIXERCONTROLW c;
160 } control;
162 /* Mixer device */
163 typedef struct mixer
165 snd_mixer_t *mix;
166 WCHAR mixername[MAXPNAMELEN];
168 int chans, dests;
169 LPDRVCALLBACK callback;
170 DWORD_PTR callbackpriv;
171 HDRVR hmx;
173 line *lines;
174 control *controls;
175 } mixer;
177 #define MAX_MIXERS 32
178 #define CONTROLSPERLINE 3
179 #define OFS_MUTE 2
180 #define OFS_MUX 1
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];
189 static LONG refcnt;
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
193 * mostly harmless
196 static const struct mixerlinetype {
197 const char *name; DWORD cmpt;
198 } converttable[] = {
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)
217 int x;
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);
232 BOOL blisted = 0;
234 if (!snd_mixer_selem_has_playback_volume(elem) &&
235 !snd_mixer_selem_has_capture_volume(elem))
236 blisted = 1;
238 TRACE("%s: %x\n", name, blisted);
239 return blisted;
242 static void fillcontrols(mixer *mmixer)
244 int id;
245 for (id = 0; id < mmixer->chans; ++id)
247 line *mline = &mmixer->lines[id];
248 int ofs = CONTROLSPERLINE * id;
249 int x;
250 long min, max;
252 TRACE("Filling control %d\n", id);
253 if (!mline->elem)
254 break;
255 if (id == 1 && !mline->elem)
256 continue;
258 if (mline->capt && snd_mixer_selem_has_capture_volume(mline->elem))
259 snd_mixer_selem_get_capture_volume_range(mline->elem, &min, &max);
260 else
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;
286 if (id == 1)
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)
316 int ret=0, chn;
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))
321 ++ret;
322 } else {
323 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
324 if (snd_mixer_selem_has_playback_channel(elem, chn))
325 ++ret;
327 if (!ret)
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"));
329 return ret;
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;
337 if (mastelem) {
338 /* Master control */
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));
341 mline->dst = 0;
342 mline->capt = 0;
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);
348 } else {
349 MultiByteToWideChar(CP_UNIXCP, 0, "Empty Master Element", -1, mline->name, sizeof(mline->name)/sizeof(WCHAR));
352 /* Capture control
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 */
358 ++mline;
359 if (capt)
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));
363 mline->dst = 1;
364 mline->capt = 1;
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;
384 mline->elem = elem;
385 mline->chans = chans(mmixer, elem, 0);
387 else if (!capt)
388 continue;
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;
395 mline->elem = elem;
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));
410 mline->dst = 0;
411 mline->capt = 1;
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'
422 * - 'Capture'
423 * Capture might not always be available, so should be prepared to be without if needed
426 static void ALSA_MixerInit(void)
428 int x, mixnum = 0;
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];
437 snd_ctl_t *ctl;
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);
444 if (card < 0)
445 continue;
447 snprintf(cardname, sizeof(cardname), "hw:%d", card);
449 err = snd_ctl_open(&ctl, cardname, 0);
450 if (err < 0)
452 WARN("Cannot open card: %s\n", snd_strerror(err));
453 continue;
456 err = snd_ctl_card_info(ctl, info);
457 if (err < 0)
459 WARN("Cannot get card info: %s\n", snd_strerror(err));
460 snd_ctl_close(ctl);
461 continue;
464 MultiByteToWideChar(CP_UNIXCP, 0, snd_ctl_card_info_get_name(info), -1, mixdev[mixnum].mixername, sizeof(mixdev[mixnum].mixername)/sizeof(WCHAR));
465 snd_ctl_close(ctl);
467 err = snd_mixer_open(&mixdev[mixnum].mix, 0);
468 if (err < 0)
470 WARN("Error occurred opening mixer: %s\n", snd_strerror(err));
471 continue;
474 err = snd_mixer_attach(mixdev[mixnum].mix, cardname);
475 if (err < 0)
476 goto eclose;
478 err = snd_mixer_selem_register(mixdev[mixnum].mix, NULL, NULL);
479 if (err < 0)
480 goto eclose;
482 err = snd_mixer_load(mixdev[mixnum].mix);
483 if (err < 0)
484 goto eclose;
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)
495 mastelem = elem;
496 ++(mixdev[mixnum].chans);
498 else if (!strcasecmp(snd_mixer_selem_get_name(elem), "Capture") && !captelem)
499 captelem = elem;
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.*/
502 micelem = elem;
503 else if (!blacklisted(elem))
505 DWORD comp = getcomponenttype(snd_mixer_selem_get_name(elem));
506 DWORD skip = 0;
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))
510 ++capcontrols;
511 else if (comp == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE)
513 ++capcontrols;
514 skip = 1;
517 if (!skip && snd_mixer_selem_has_playback_volume(elem))
519 if (!strcasecmp(snd_mixer_selem_get_name(elem), "Headphone") && !headelem)
520 headelem = elem;
521 else if (!strcasecmp(snd_mixer_selem_get_name(elem), "PCM") && !pcmelem)
522 pcmelem = elem;
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");
534 goto close;
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 */
542 mastelem = headelem;
543 capcontrols -= !!snd_mixer_selem_has_capture_switch(mastelem);
545 else if (pcmelem && !mastelem)
547 /* Use 'PCM' as master device */
548 mastelem = pcmelem;
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));
555 goto close;
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);
566 capcontrols = 0;
567 mixdev[mixnum].dests = 1;
569 else
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);
577 err = -ENOMEM;
578 if (!mixdev[mixnum].lines || !mixdev[mixnum].controls)
579 goto close;
581 if (mastelem)
582 filllines(&mixdev[mixnum], mastelem, captelem, capcontrols);
583 else if (micelem)
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));
588 mixnum++;
589 continue;
591 eclose:
592 WARN("Error occurred initialising mixer: %s\n", snd_strerror(err));
593 close:
594 HeapFree(GetProcessHeap(), 0, mixdev[mixnum].lines);
595 HeapFree(GetProcessHeap(), 0, mixdev[mixnum].controls);
596 snd_mixer_close(mixdev[mixnum].mix);
598 cards = mixnum;
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");
606 TRACE("\n");
609 static void ALSA_MixerExit(void)
611 int x;
613 if (refcnt)
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);
619 refcnt = 0;
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);
633 cards = 0;
636 static mixer* MIX_GetMix(UINT wDevID)
638 mixer *mmixer;
640 if (wDevID >= cards)
642 WARN("Invalid mixer id: %d\n", wDevID);
643 return NULL;
646 mmixer = &mixdev[wDevID];
647 return mmixer;
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);
654 int x;
655 BOOL captchanged = 0;
657 if (type != SND_CTL_EVENT_MASK_VALUE)
658 return 0;
660 assert(mmixer);
662 EnterCriticalSection(&elem_crst);
664 if (!mmixer->callback)
665 goto out;
667 for (x=0; x<mmixer->chans; ++x)
669 const int ofs = CONTROLSPERLINE*x;
670 if (elem != mmixer->lines[x].elem)
671 continue;
673 if (mmixer->lines[x].capt)
674 ++captchanged;
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);
683 if (captchanged)
684 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, CONTROLSPERLINE+OFS_MUX, 0);
686 out:
687 LeaveCriticalSection(&elem_crst);
689 return 0;
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));
705 if (!pfds)
707 WARN("Out of memory\n");
708 goto die;
711 pfds[0].fd = msg_pipe[0];
712 pfds[0].events = POLLIN;
714 y = 1;
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)
721 break;
723 mcnt = 1;
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);
731 break;
733 mcnt += max;
735 if (y)
736 TRACE("Handled %d events\n", y);
739 die:
740 TRACE("Shutting down\n");
741 HeapFree(GetProcessHeap(), 0, pfds);
743 y = read(msg_pipe[0], &x, sizeof(x));
744 close(msg_pipe[1]);
745 close(msg_pipe[0]);
746 return 0;
749 static DWORD MIX_Open(UINT wDevID, LPMIXEROPENDESC desc, DWORD_PTR flags)
751 mixer *mmixer = MIX_GetMix(wDevID);
752 if (!mmixer)
753 return MMSYSERR_BADDEVICEID;
755 flags &= CALLBACK_TYPEMASK;
756 switch (flags)
758 case CALLBACK_NULL:
759 goto done;
761 case CALLBACK_FUNCTION:
762 break;
764 default:
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;
773 done:
774 if (InterlockedIncrement(&refcnt) == 1)
776 if (pipe(msg_pipe) >= 0)
778 thread = CreateThread(NULL, 0, ALSA_MixerPollThread, NULL, 0, NULL);
779 if (!thread)
781 close(msg_pipe[0]);
782 close(msg_pipe[1]);
783 msg_pipe[0] = msg_pipe[1] = -1;
786 else
787 msg_pipe[0] = msg_pipe[1] = -1;
790 return MMSYSERR_NOERROR;
793 static DWORD MIX_Close(UINT wDevID)
795 int x = 0;
796 mixer *mmixer = MIX_GetMix(wDevID);
797 if (!mmixer)
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);
810 TRACE("Done\n");
814 return MMSYSERR_NOERROR;
817 static DWORD MIX_GetDevCaps(UINT wDevID, LPMIXERCAPS2W caps, DWORD_PTR parm2)
819 mixer *mmixer = MIX_GetMix(wDevID);
820 MIXERCAPS2W capsW;
822 if (!caps)
823 return MMSYSERR_INVALPARAM;
825 if (!mmixer)
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);
847 if (ret > nextmax)
848 ret = nextmax;
849 else if (ret < 0)
850 ret = 0;
852 return ret;
855 /* get amount of sources for dest */
856 static int getsrccntfromchan(mixer *mmixer, int dad)
858 int i, j=0;
860 for (i=0; i<mmixer->chans; ++i)
861 if (i != dad && mmixer->lines[i].dst == dad)
863 ++j;
865 if (!j)
866 FIXME("No src found for %i (%s)?\n", dad, debugstr_w(mmixer->lines[dad].name));
867 return j;
870 /* find lineid for source 'num' with dest 'dad' */
871 static int getsrclinefromchan(mixer *mmixer, int dad, int num)
873 int i, j=0;
874 for (i=0; i<mmixer->chans; ++i)
875 if (i != dad && mmixer->lines[i].dst == dad)
877 if (num == j)
878 return i;
879 ++j;
881 WARN("No src found for src %i from dest %i\n", num, dad);
882 return 0;
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)
893 if (line == i)
894 return j;
895 ++j;
897 WARN("No src found for line %i with dad %i\n", line, dad);
898 return 0;
901 /* Get volume/muted/capture channel */
902 static DWORD MIX_GetControlDetails(UINT wDevID, LPMIXERCONTROLDETAILS mctrld, DWORD_PTR flags)
904 mixer *mmixer = MIX_GetMix(wDevID);
905 DWORD ctrl;
906 DWORD line;
907 control *ct;
909 if (!mctrld)
910 return MMSYSERR_INVALPARAM;
912 ctrl = mctrld->dwControlID;
913 line = ctrl/CONTROLSPERLINE;
915 if (mctrld->cbStruct != sizeof(*mctrld))
916 return MMSYSERR_INVALPARAM;
918 if (!mmixer)
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;
928 switch (flags) {
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;
936 int chn;
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)
964 break;
965 ++mcdu;
967 } else {
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)
976 break;
977 ++mcdu;
981 return MMSYSERR_NOERROR;
984 case MIXERCONTROL_CONTROLTYPE_ONOFF:
985 case MIXERCONTROL_CONTROLTYPE_MUTE:
987 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
988 int chn, ival;
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;
1001 if (line == 1)
1002 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1004 if (!snd_mixer_selem_has_capture_channel(elem, chn))
1005 continue;
1006 snd_mixer_selem_get_capture_switch(elem, chn, &ival);
1007 break;
1009 else
1010 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1012 if (!snd_mixer_selem_has_playback_channel(elem, chn))
1013 continue;
1014 snd_mixer_selem_get_playback_switch(elem, chn, &ival);
1015 break;
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)
1047 ival = 0;
1048 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1050 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1051 continue;
1052 snd_mixer_selem_get_capture_switch(mmixer->lines[x].elem, chn, &ival);
1053 if (ival)
1054 break;
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;
1064 break;
1066 default:
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;
1079 int i, j;
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));
1090 ++i; ++mcdlt;
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;
1099 default:
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;
1110 control *ct;
1111 snd_mixer_elem_t * elem;
1113 if (!mctrld)
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;
1125 if (!mmixer)
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;
1144 switch (flags) {
1145 case MIXER_SETCONTROLDETAILSF_VALUE:
1146 TRACE("MIXER_SETCONTROLDETAILSF_VALUE (%d)\n", ctrl);
1147 break;
1149 default:
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;
1159 int chn;
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)
1193 mcdu++;
1196 else
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)
1205 mcdu++;
1209 break;
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);
1231 else
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);
1235 break;
1238 case MIXERCONTROL_CONTROLTYPE_MIXER:
1239 case MIXERCONTROL_CONTROLTYPE_MUX:
1241 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
1242 int x, i=0, chn;
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;
1269 if (mcdb[i].fValue)
1270 didone = 1;
1271 ++i;
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)
1284 if (mcdb[i].fValue)
1285 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1287 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1288 continue;
1289 snd_mixer_selem_set_capture_switch(mmixer->lines[x].elem, chn, mcdb[i].fValue);
1291 ++i;
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
1299 if (canone)
1300 break;
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))
1309 continue;
1310 snd_mixer_selem_set_capture_switch(mmixer->lines[x].elem, chn, mcdb[i].fValue);
1312 ++i;
1314 break;
1316 default:
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:
1326 * dwLineID
1327 * sz(Short)Name
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);
1335 line *mline;
1336 int idx, i;
1338 if (!Ml)
1340 WARN("No Ml\n");
1341 return MMSYSERR_INVALPARAM;
1344 if (!mmixer)
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;
1356 Ml->dwUser = 0;
1357 Ml->fdwLine = MIXERLINE_LINEF_DISCONNECTED;
1358 switch (qf)
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)
1367 Ml->dwLineID = idx;
1368 break;
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;
1387 else
1389 Ml->cConnections = getsrccntfromchan(mmixer, Ml->dwLineID);
1390 Ml->dwSource = 0xFFFFFFFF;
1392 TRACE("Connections %d, source %d\n", Ml->cConnections, Ml->dwSource);
1393 break;
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;
1405 break;
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;
1422 break;
1424 case MIXER_GETLINEINFOF_TARGETTYPE:
1425 FIXME("TODO: TARGETTYPE, stub\n");
1426 return MMSYSERR_INVALPARAM;
1428 default:
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;
1441 Ml->cControls = 0;
1443 for (i=CONTROLSPERLINE*Ml->dwLineID;i<CONTROLSPERLINE*(Ml->dwLineID+1); ++i)
1444 if (mmixer->controls[i].enabled)
1445 ++(Ml->cControls);
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));
1449 if (mline->capt)
1450 Ml->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
1451 else
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);
1465 int i,j = 0;
1466 DWORD ct;
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;
1480 if (!mmixer)
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;
1494 switch (flags)
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));
1503 ++j;
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;
1516 break;
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;
1524 break;
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;
1540 break;
1543 break;
1544 default:
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)
1560 #ifdef HAVE_ALSA
1561 DWORD ret;
1562 TRACE("(%04X, %s, %08lX, %08lX, %08lX);\n", wDevID, getMessage(wMsg),
1563 dwUser, dwParam1, dwParam2);
1565 switch (wMsg)
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 */
1571 case DRVM_ENABLE:
1572 case DRVM_DISABLE:
1573 ret = MMSYSERR_NOERROR; break;
1575 case MXDM_OPEN:
1576 ret = MIX_Open(wDevID, (LPMIXEROPENDESC) dwParam1, dwParam2); break;
1578 case MXDM_CLOSE:
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:
1597 ret = cards; break;
1599 default:
1600 WARN("unknown message %s!\n", getMessage(wMsg));
1601 return MMSYSERR_NOTSUPPORTED;
1604 TRACE("Returning %08X\n", ret);
1605 return ret;
1606 #else /*HAVE_ALSA*/
1607 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1609 return MMSYSERR_NOTENABLED;
1610 #endif /*HAVE_ALSA*/