winealsa.drv: Leave the CriticalSection before destroying it. Found by Smatch.
[wine/wine-kai.git] / dlls / winealsa.drv / mixer.c
blobdaa45a1f85e2372a8f6bfeae6897c317adca2068
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 static char str[64];
81 #define MSG_TO_STR(x) case x: return #x;
82 switch (uMsg){
83 MSG_TO_STR(DRVM_INIT);
84 MSG_TO_STR(DRVM_EXIT);
85 MSG_TO_STR(DRVM_ENABLE);
86 MSG_TO_STR(DRVM_DISABLE);
87 MSG_TO_STR(MXDM_GETDEVCAPS);
88 MSG_TO_STR(MXDM_GETLINEINFO);
89 MSG_TO_STR(MXDM_GETNUMDEVS);
90 MSG_TO_STR(MXDM_OPEN);
91 MSG_TO_STR(MXDM_CLOSE);
92 MSG_TO_STR(MXDM_GETLINECONTROLS);
93 MSG_TO_STR(MXDM_GETCONTROLDETAILS);
94 MSG_TO_STR(MXDM_SETCONTROLDETAILS);
95 default: break;
97 #undef MSG_TO_STR
98 sprintf(str, "UNKNOWN(%08x)", uMsg);
99 return str;
102 static const char * getControlType(DWORD dwControlType)
104 #define TYPE_TO_STR(x) case x: return #x;
105 switch (dwControlType) {
106 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_CUSTOM);
107 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BOOLEANMETER);
108 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SIGNEDMETER);
109 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PEAKMETER);
110 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_UNSIGNEDMETER);
111 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BOOLEAN);
112 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_ONOFF);
113 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MUTE);
114 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MONO);
115 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_LOUDNESS);
116 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_STEREOENH);
117 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BASS_BOOST);
118 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BUTTON);
119 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_DECIBELS);
120 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SIGNED);
121 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_UNSIGNED);
122 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PERCENT);
123 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SLIDER);
124 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_PAN);
125 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_QSOUNDPAN);
126 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_FADER);
127 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_VOLUME);
128 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_BASS);
129 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_TREBLE);
130 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_EQUALIZER);
131 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_SINGLESELECT);
132 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MUX);
133 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MULTIPLESELECT);
134 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MIXER);
135 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MICROTIME);
136 TYPE_TO_STR(MIXERCONTROL_CONTROLTYPE_MILLITIME);
138 #undef TYPE_TO_STR
139 return wine_dbg_sprintf("UNKNOWN(%08x)", dwControlType);
142 /* A simple declaration of a line control
143 * These are each of the channels that show up
145 typedef struct line {
146 /* Name we present to outside world */
147 WCHAR name[MAXPNAMELEN];
149 DWORD component;
150 DWORD dst;
151 DWORD capt;
152 DWORD chans;
153 snd_mixer_elem_t *elem;
154 } line;
156 /* A control structure, with toggle enabled switch
157 * Control structures control volume, muted, which capture source
159 typedef struct control {
160 BOOL enabled;
161 MIXERCONTROLW c;
162 } control;
164 /* Mixer device */
165 typedef struct mixer
167 snd_mixer_t *mix;
168 WCHAR mixername[MAXPNAMELEN];
170 int chans, dests;
171 LPDRVCALLBACK callback;
172 DWORD_PTR callbackpriv;
173 HDRVR hmx;
175 line *lines;
176 control *controls;
177 } mixer;
179 #define MAX_MIXERS 32
180 #define CONTROLSPERLINE 3
181 #define OFS_MUTE 2
182 #define OFS_MUX 1
184 static int cards = 0;
185 static mixer mixdev[MAX_MIXERS];
186 static HANDLE thread;
187 static int elem_callback(snd_mixer_elem_t *elem, unsigned int mask);
188 static DWORD WINAPI ALSA_MixerPollThread(LPVOID lParam);
189 static CRITICAL_SECTION elem_crst;
190 static int msg_pipe[2];
191 static LONG refcnt;
193 /* found channel names in alsa lib, alsa api doesn't have another way for this
194 * map name -> componenttype, worst case we get a wrong componenttype which is
195 * mostly harmless
198 static const struct mixerlinetype {
199 const char *name; DWORD cmpt;
200 } converttable[] = {
201 { "Master", MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, },
202 { "Capture", MIXERLINE_COMPONENTTYPE_DST_WAVEIN, },
203 { "PCM", MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT, },
204 { "PC Speaker", MIXERLINE_COMPONENTTYPE_SRC_PCSPEAKER, },
205 { "Synth", MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER, },
206 { "Headphone", MIXERLINE_COMPONENTTYPE_DST_HEADPHONES, },
207 { "Mic", MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, },
208 { "Aux", MIXERLINE_COMPONENTTYPE_SRC_UNDEFINED, },
209 { "CD", MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC, },
210 { "Line", MIXERLINE_COMPONENTTYPE_SRC_LINE, },
211 { "Phone", MIXERLINE_COMPONENTTYPE_SRC_TELEPHONE, },
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 !snd_mixer_selem_has_capture_switch(elem)))
237 blisted = 1;
239 TRACE("%s: %x\n", name, blisted);
240 return blisted;
243 static void fillcontrols(mixer *mmixer)
245 int id;
246 for (id = 0; id < mmixer->chans; ++id)
248 line *mline = &mmixer->lines[id];
249 int ofs = CONTROLSPERLINE * id;
250 int x;
251 long min, max;
253 TRACE("Filling control %d\n", id);
254 if (id == 1 && !mline->elem)
255 continue;
257 if (mline->capt && snd_mixer_selem_has_capture_volume(mline->elem))
258 snd_mixer_selem_get_capture_volume_range(mline->elem, &min, &max);
259 else
260 snd_mixer_selem_get_playback_volume_range(mline->elem, &min, &max);
262 /* (!snd_mixer_selem_has_playback_volume(elem) || snd_mixer_selem_has_capture_volume(elem)) */
263 /* Volume, always enabled by definition of blacklisted channels */
264 mmixer->controls[ofs].enabled = 1;
265 mmixer->controls[ofs].c.cbStruct = sizeof(mmixer->controls[ofs].c);
266 mmixer->controls[ofs].c.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
267 mmixer->controls[ofs].c.dwControlID = ofs;
268 mmixer->controls[ofs].c.Bounds.s1.dwMinimum = 0;
269 mmixer->controls[ofs].c.Bounds.s1.dwMaximum = 65535;
270 mmixer->controls[ofs].c.Metrics.cSteps = 65536/(max-min);
272 if ((id == 1 && snd_mixer_selem_has_capture_switch(mline->elem)) ||
273 (!mline->capt && snd_mixer_selem_has_playback_switch(mline->elem)))
274 { /* MUTE button optional, main capture channel should have one too */
275 mmixer->controls[ofs+OFS_MUTE].enabled = 1;
276 mmixer->controls[ofs+OFS_MUTE].c.cbStruct = sizeof(mmixer->controls[ofs].c);
277 mmixer->controls[ofs+OFS_MUTE].c.dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
278 mmixer->controls[ofs+OFS_MUTE].c.dwControlID = ofs+OFS_MUTE;
279 mmixer->controls[ofs+OFS_MUTE].c.Bounds.s1.dwMaximum = 1;
282 if (mline->capt && snd_mixer_selem_has_capture_switch_exclusive(mline->elem))
283 mmixer->controls[CONTROLSPERLINE+OFS_MUX].c.dwControlType = MIXERCONTROL_CONTROLTYPE_MUX;
285 if (id == 1)
286 { /* Capture select, in case cMultipleItems is 0, it means capture is disabled anyway */
287 mmixer->controls[ofs+OFS_MUX].enabled = 1;
288 mmixer->controls[ofs+OFS_MUX].c.cbStruct = sizeof(mmixer->controls[ofs].c);
289 mmixer->controls[ofs+OFS_MUX].c.dwControlType = MIXERCONTROL_CONTROLTYPE_MIXER;
290 mmixer->controls[ofs+OFS_MUX].c.dwControlID = ofs+OFS_MUX;
291 mmixer->controls[ofs+OFS_MUX].c.fdwControl = MIXERCONTROL_CONTROLF_MULTIPLE;
293 for (x = 0; x<mmixer->chans; ++x)
294 if (x != id && mmixer->lines[x].dst == id)
295 ++(mmixer->controls[ofs+OFS_MUX].c.cMultipleItems);
296 if (!mmixer->controls[ofs+OFS_MUX].c.cMultipleItems)
297 mmixer->controls[ofs+OFS_MUX].enabled = 0;
299 mmixer->controls[ofs+OFS_MUX].c.Bounds.s1.dwMaximum = mmixer->controls[ofs+OFS_MUX].c.cMultipleItems - 1;
300 mmixer->controls[ofs+OFS_MUX].c.Metrics.cSteps = mmixer->controls[ofs+OFS_MUX].c.cMultipleItems;
302 for (x=0; x<CONTROLSPERLINE; ++x)
304 lstrcpynW(mmixer->controls[ofs+x].c.szShortName, mline->name, sizeof(mmixer->controls[ofs+x].c.szShortName)/sizeof(WCHAR));
305 lstrcpynW(mmixer->controls[ofs+x].c.szName, mline->name, sizeof(mmixer->controls[ofs+x].c.szName)/sizeof(WCHAR));
310 /* get amount of channels for elem */
311 /* Officially we should keep capture/playback separated,
312 * but that's not going to work in the alsa api */
313 static int chans(mixer *mmixer, snd_mixer_elem_t * elem, DWORD capt)
315 int ret=0, chn;
317 if (capt && snd_mixer_selem_has_capture_volume(elem)) {
318 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
319 if (snd_mixer_selem_has_capture_channel(elem, chn))
320 ++ret;
321 } else {
322 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
323 if (snd_mixer_selem_has_playback_channel(elem, chn))
324 ++ret;
326 if (!ret)
327 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"));
328 return ret;
331 static void filllines(mixer *mmixer, snd_mixer_elem_t *mastelem, snd_mixer_elem_t *captelem, int capt)
333 snd_mixer_elem_t *elem;
334 line *mline = mmixer->lines;
336 /* Master control */
337 MultiByteToWideChar(CP_UNIXCP, 0, snd_mixer_selem_get_name(mastelem), -1, mline->name, sizeof(mline->name)/sizeof(WCHAR));
338 mline->component = getcomponenttype(snd_mixer_selem_get_name(mastelem));
339 mline->dst = 0;
340 mline->capt = 0;
341 mline->elem = mastelem;
342 mline->chans = chans(mmixer, mastelem, 0);
344 snd_mixer_elem_set_callback(mastelem, &elem_callback);
345 snd_mixer_elem_set_callback_private(mastelem, mmixer);
347 /* Capture control
348 * Note: since mmixer->dests = 1, it means only playback control is visible
349 * This makes sense, because if there are no capture sources capture control
350 * can't do anything and should be invisible */
352 /* Control 1 is reserved for capture even when not enabled */
353 ++mline;
354 if (capt)
356 MultiByteToWideChar(CP_UNIXCP, 0, snd_mixer_selem_get_name(captelem), -1, mline->name, sizeof(mline->name)/sizeof(WCHAR));
357 mline->component = getcomponenttype(snd_mixer_selem_get_name(captelem));
358 mline->dst = 1;
359 mline->capt = 1;
360 mline->elem = captelem;
361 mline->chans = chans(mmixer, captelem, 1);
363 snd_mixer_elem_set_callback(captelem, &elem_callback);
364 snd_mixer_elem_set_callback_private(captelem, mmixer);
367 for (elem = snd_mixer_first_elem(mmixer->mix); elem; elem = snd_mixer_elem_next(elem))
368 if (elem != mastelem && elem != captelem && !blacklisted(elem))
370 const char * name = snd_mixer_selem_get_name(elem);
371 DWORD comp = getcomponenttype(name);
373 if (snd_mixer_selem_has_playback_volume(elem))
375 (++mline)->component = comp;
376 MultiByteToWideChar(CP_UNIXCP, 0, name, -1, mline->name, MAXPNAMELEN);
377 mline->capt = mline->dst = 0;
378 mline->elem = elem;
379 mline->chans = chans(mmixer, elem, 0);
381 else if (!capt)
382 continue;
384 if (capt && snd_mixer_selem_has_capture_switch(elem))
386 (++mline)->component = comp;
387 MultiByteToWideChar(CP_UNIXCP, 0, name, -1, mline->name, MAXPNAMELEN);
388 mline->capt = mline->dst = 1;
389 mline->elem = elem;
390 mline->chans = chans(mmixer, elem, 1);
393 snd_mixer_elem_set_callback(elem, &elem_callback);
394 snd_mixer_elem_set_callback_private(elem, mmixer);
398 /* Windows api wants to have a 'master' device to which all slaves are attached
399 * There are 2 ones in this code:
400 * - 'Master', fall back to 'Headphone' if unavailable, and if that's not available 'PCM'
401 * - 'Capture'
402 * Capture might not always be available, so should be prepared to be without if needed
405 static void ALSA_MixerInit(void)
407 int x, mixnum = 0;
409 for (x = 0; x < MAX_MIXERS; ++x)
411 int card, err, capcontrols = 0;
412 char cardind[6], cardname[10];
414 snd_ctl_t *ctl;
415 snd_mixer_elem_t *elem, *mastelem = NULL, *headelem = NULL, *captelem = NULL, *pcmelem = NULL;
416 snd_ctl_card_info_t *info = NULL;
417 snd_ctl_card_info_alloca(&info);
419 memset(&mixdev[mixnum], 0, sizeof(*mixdev));
420 snprintf(cardind, sizeof(cardind), "%d", x);
421 card = snd_card_get_index(cardind);
422 if (card < 0)
423 continue;
425 snprintf(cardname, sizeof(cardname), "hw:%d", card);
427 err = snd_ctl_open(&ctl, cardname, 0);
428 if (err < 0)
430 WARN("Cannot open card: %s\n", snd_strerror(err));
431 continue;
434 err = snd_ctl_card_info(ctl, info);
435 if (err < 0)
437 WARN("Cannot get card info: %s\n", snd_strerror(err));
438 snd_ctl_close(ctl);
439 continue;
442 MultiByteToWideChar(CP_UNIXCP, 0, snd_ctl_card_info_get_name(info), -1, mixdev[mixnum].mixername, sizeof(mixdev[mixnum].mixername)/sizeof(WCHAR));
443 snd_ctl_close(ctl);
445 err = snd_mixer_open(&mixdev[mixnum].mix, 0);
446 if (err < 0)
448 WARN("Error occurred opening mixer: %s\n", snd_strerror(err));
449 continue;
452 err = snd_mixer_attach(mixdev[mixnum].mix, cardname);
453 if (err < 0)
454 goto eclose;
456 err = snd_mixer_selem_register(mixdev[mixnum].mix, NULL, NULL);
457 if (err < 0)
458 goto eclose;
460 err = snd_mixer_load(mixdev[mixnum].mix);
461 if (err < 0)
462 goto eclose;
464 /* First, lets see what's available..
465 * If there are multiple Master or Captures, all except 1 will be added as slaves
467 for (elem = snd_mixer_first_elem(mixdev[mixnum].mix); elem; elem = snd_mixer_elem_next(elem))
468 if (!strcasecmp(snd_mixer_selem_get_name(elem), "Master") && !mastelem)
469 mastelem = elem;
470 else if (!strcasecmp(snd_mixer_selem_get_name(elem), "Capture") && !captelem)
471 captelem = elem;
472 else if (!blacklisted(elem))
474 if (snd_mixer_selem_has_capture_switch(elem))
475 ++capcontrols;
476 if (snd_mixer_selem_has_playback_volume(elem))
478 if (!strcasecmp(snd_mixer_selem_get_name(elem), "Headphone") && !headelem)
479 headelem = elem;
480 else if (!strcasecmp(snd_mixer_selem_get_name(elem), "PCM") && !pcmelem)
481 pcmelem = elem;
482 else
483 ++(mixdev[mixnum].chans);
487 /* Add master channel, uncounted channels and an extra for capture */
488 mixdev[mixnum].chans += !!mastelem + !!headelem + !!pcmelem + 1;
490 /* If there is only 'Capture' and 'Master', this device is not worth it */
491 if (mixdev[mixnum].chans == 2)
493 WARN("No channels found, skipping device!\n");
494 goto close;
497 /* Master element can't have a capture control in this code, so
498 * if Headphone or PCM is promoted to master, unset its capture control */
499 if (headelem && !mastelem)
501 /* Using 'Headphone' as master device */
502 mastelem = headelem;
503 capcontrols -= !!snd_mixer_selem_has_capture_switch(mastelem);
505 else if (pcmelem && !mastelem)
507 /* Use 'PCM' as master device */
508 mastelem = pcmelem;
509 capcontrols -= !!snd_mixer_selem_has_capture_switch(mastelem);
511 else if (!mastelem)
513 /* If there is nothing sensible that can act as 'Master' control, something is wrong */
514 FIXME("No master control found on %s, disabling mixer\n", snd_ctl_card_info_get_name(info));
515 goto close;
518 if (!captelem || !capcontrols)
520 /* Can't enable capture, so disabling it
521 * Note: capture control will still exist because
522 * dwLineID 0 and 1 are reserved for Master and Capture
524 WARN("No use enabling capture part of mixer, capture control found: %s, amount of capture controls: %d\n",
525 (!captelem ? "no" : "yes"), capcontrols);
526 capcontrols = 0;
527 mixdev[mixnum].dests = 1;
529 else
531 mixdev[mixnum].chans += capcontrols;
532 mixdev[mixnum].dests = 2;
535 mixdev[mixnum].lines = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(line) * mixdev[mixnum].chans);
536 mixdev[mixnum].controls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(control) * CONTROLSPERLINE*mixdev[mixnum].chans);
537 err = -ENOMEM;
538 if (!mixdev[mixnum].lines || !mixdev[mixnum].controls)
539 goto close;
541 filllines(&mixdev[mixnum], mastelem, captelem, capcontrols);
542 fillcontrols(&mixdev[mixnum]);
544 TRACE("%s: Amount of controls: %i/%i, name: %s\n", cardname, mixdev[mixnum].dests, mixdev[mixnum].chans, debugstr_w(mixdev[mixnum].mixername));
545 mixnum++;
546 continue;
548 eclose:
549 WARN("Error occurred initialising mixer: %s\n", snd_strerror(err));
550 close:
551 HeapFree(GetProcessHeap(), 0, mixdev[mixnum].lines);
552 HeapFree(GetProcessHeap(), 0, mixdev[mixnum].controls);
553 snd_mixer_close(mixdev[mixnum].mix);
555 cards = mixnum;
557 /* There is no trouble with already assigning callbacks without initialising critsect:
558 * Callbacks only occur when snd_mixer_handle_events is called (only happens in thread)
560 InitializeCriticalSection(&elem_crst);
561 elem_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_MIXER.elem_crst");
562 TRACE("\n");
565 static void ALSA_MixerExit(void)
567 int x;
569 if (refcnt)
571 WARN("Callback thread still alive, terminating uncleanly, refcnt: %d\n", refcnt);
572 /* Least we can do is making sure we're not in 'foreign' code */
573 EnterCriticalSection(&elem_crst);
574 TerminateThread(thread, 1);
575 refcnt = 0;
576 LeaveCriticalSection(&elem_crst);
579 TRACE("Cleaning up\n");
581 elem_crst.DebugInfo->Spare[0] = 0;
582 DeleteCriticalSection(&elem_crst);
583 for (x = 0; x < cards; ++x)
585 snd_mixer_close(mixdev[x].mix);
586 HeapFree(GetProcessHeap(), 0, mixdev[x].lines);
587 HeapFree(GetProcessHeap(), 0, mixdev[x].controls);
589 cards = 0;
592 static mixer* MIX_GetMix(UINT wDevID)
594 mixer *mmixer;
596 if (wDevID < 0 || wDevID >= cards)
598 WARN("Invalid mixer id: %d\n", wDevID);
599 return NULL;
602 mmixer = &mixdev[wDevID];
603 return mmixer;
606 /* Since alsa doesn't tell what exactly changed, just assume all affected controls changed */
607 static int elem_callback(snd_mixer_elem_t *elem, unsigned int type)
609 mixer *mmixer = snd_mixer_elem_get_callback_private(elem);
610 int x;
611 BOOL captchanged = 0;
613 if (type != SND_CTL_EVENT_MASK_VALUE)
614 return 0;
616 assert(mmixer);
618 EnterCriticalSection(&elem_crst);
620 if (!mmixer->callback)
621 goto out;
623 for (x=0; x<mmixer->chans; ++x)
625 const int ofs = CONTROLSPERLINE*x;
626 if (elem != mmixer->lines[x].elem)
627 continue;
629 if (mmixer->lines[x].capt)
630 ++captchanged;
632 TRACE("Found changed control %s\n", debugstr_w(mmixer->lines[x].name));
633 mmixer->callback(mmixer->hmx, MM_MIXM_LINE_CHANGE, mmixer->callbackpriv, x, 0);
634 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, ofs, 0);
636 if (mmixer->controls[ofs+OFS_MUTE].enabled)
637 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, ofs+OFS_MUTE, 0);
639 if (captchanged)
640 mmixer->callback(mmixer->hmx, MM_MIXM_CONTROL_CHANGE, mmixer->callbackpriv, CONTROLSPERLINE+OFS_MUX, 0);
642 out:
643 LeaveCriticalSection(&elem_crst);
645 return 0;
648 static DWORD WINAPI ALSA_MixerPollThread(LPVOID lParam)
650 struct pollfd *pfds = NULL;
651 int x, y, err, mcnt, count = 1;
653 TRACE("%p\n", lParam);
655 for (x = 0; x < cards; ++x)
656 count += snd_mixer_poll_descriptors_count(mixdev[x].mix);
658 TRACE("Counted %d descriptors\n", count);
659 pfds = HeapAlloc(GetProcessHeap(), 0, count * sizeof(struct pollfd));
661 if (!pfds)
663 WARN("Out of memory\n");
664 goto die;
667 pfds[0].fd = msg_pipe[0];
668 pfds[0].events = POLLIN;
670 y = 1;
671 for (x = 0; x < cards; ++x)
672 y += snd_mixer_poll_descriptors(mixdev[x].mix, &pfds[y], count - y);
674 while ((err = poll(pfds, (unsigned int) count, -1)) >= 0 || errno == EINTR || errno == EAGAIN)
676 if (pfds[0].revents & POLLIN)
677 break;
679 mcnt = 1;
680 for (x = y = 0; x < cards; ++x)
682 int j, max = snd_mixer_poll_descriptors_count(mixdev[x].mix);
683 for (j = 0; j < max; ++j)
684 if (pfds[mcnt+j].revents)
686 y += snd_mixer_handle_events(mixdev[x].mix);
687 break;
689 mcnt += max;
691 if (y)
692 TRACE("Handled %d events\n", y);
695 die:
696 TRACE("Shutting down\n");
697 HeapFree(GetProcessHeap(), 0, pfds);
699 y = read(msg_pipe[0], &x, sizeof(x));
700 close(msg_pipe[1]);
701 close(msg_pipe[0]);
702 return 0;
705 static DWORD MIX_Open(UINT wDevID, LPMIXEROPENDESC desc, DWORD_PTR flags)
707 mixer *mmixer = MIX_GetMix(wDevID);
708 if (!mmixer)
709 return MMSYSERR_BADDEVICEID;
711 flags &= CALLBACK_TYPEMASK;
712 switch (flags)
714 case CALLBACK_NULL:
715 goto done;
717 case CALLBACK_FUNCTION:
718 break;
720 default:
721 FIXME("Unhandled callback type: %08lx\n", flags & CALLBACK_TYPEMASK);
722 return MIXERR_INVALVALUE;
725 mmixer->callback = (LPDRVCALLBACK)desc->dwCallback;
726 mmixer->callbackpriv = desc->dwInstance;
727 mmixer->hmx = (HDRVR)desc->hmx;
729 done:
730 if (InterlockedIncrement(&refcnt) == 1)
732 if (pipe(msg_pipe) >= 0)
734 thread = CreateThread(NULL, 0, ALSA_MixerPollThread, NULL, 0, NULL);
735 if (!thread)
737 close(msg_pipe[0]);
738 close(msg_pipe[1]);
739 msg_pipe[0] = msg_pipe[1] = -1;
742 else
743 msg_pipe[0] = msg_pipe[1] = -1;
746 return MMSYSERR_NOERROR;
749 static DWORD MIX_Close(UINT wDevID)
751 int x;
752 mixer *mmixer = MIX_GetMix(wDevID);
753 if (!mmixer)
754 return MMSYSERR_BADDEVICEID;
756 EnterCriticalSection(&elem_crst);
757 mmixer->callback = 0;
758 LeaveCriticalSection(&elem_crst);
760 if (!InterlockedDecrement(&refcnt))
762 if (write(msg_pipe[1], &x, sizeof(x)) > 0)
764 TRACE("Shutting down thread...\n");
765 WaitForSingleObject(thread, INFINITE);
766 TRACE("Done\n");
770 return MMSYSERR_NOERROR;
773 static DWORD MIX_GetDevCaps(UINT wDevID, LPMIXERCAPS2W caps, DWORD_PTR parm2)
775 mixer *mmixer = MIX_GetMix(wDevID);
776 MIXERCAPS2W capsW;
778 if (!caps)
779 return MMSYSERR_INVALPARAM;
781 if (!mmixer)
782 return MMSYSERR_BADDEVICEID;
784 memset(&capsW, 0, sizeof(MIXERCAPS2W));
786 capsW.wMid = WINE_MIXER_MANUF_ID;
787 capsW.wPid = WINE_MIXER_PRODUCT_ID;
788 capsW.vDriverVersion = WINE_MIXER_VERSION;
790 lstrcpynW(capsW.szPname, mmixer->mixername, sizeof(capsW.szPname)/sizeof(WCHAR));
791 capsW.cDestinations = mmixer->dests;
792 memcpy(caps, &capsW, min(parm2, sizeof(capsW)));
793 return MMSYSERR_NOERROR;
796 /* convert win32 volume to alsa volume, and vice versa */
797 static INT normalized(INT value, INT prevmax, INT nextmax)
799 int ret = MulDiv(value, nextmax, prevmax);
801 /* Have to stay in range */
802 TRACE("%d/%d -> %d/%d\n", value, prevmax, ret, nextmax);
803 if (ret > nextmax)
804 ret = nextmax;
805 else if (ret < 0)
806 ret = 0;
808 return ret;
811 /* get amount of sources for dest */
812 static int getsrccntfromchan(mixer *mmixer, int dad)
814 int i, j=0;
816 for (i=0; i<mmixer->chans; ++i)
817 if (i != dad && mmixer->lines[i].dst == dad)
819 ++j;
821 if (!j)
822 FIXME("No src found for %i (%s)?\n", dad, debugstr_w(mmixer->lines[dad].name));
823 return j;
826 /* find lineid for source 'num' with dest 'dad' */
827 static int getsrclinefromchan(mixer *mmixer, int dad, int num)
829 int i, j=0;
830 for (i=0; i<mmixer->chans; ++i)
831 if (i != dad && mmixer->lines[i].dst == dad)
833 if (num == j)
834 return i;
835 ++j;
837 WARN("No src found for src %i from dest %i\n", num, dad);
838 return 0;
841 /* get the source number belonging to line */
842 static int getsrcfromline(mixer *mmixer, int line)
844 int i, j=0, dad = mmixer->lines[line].dst;
846 for (i=0; i<mmixer->chans; ++i)
847 if (i != dad && mmixer->lines[i].dst == dad)
849 if (line == i)
850 return j;
851 ++j;
853 WARN("No src found for line %i with dad %i\n", line, dad);
854 return 0;
857 /* Get volume/muted/capture channel */
858 static DWORD MIX_GetControlDetails(UINT wDevID, LPMIXERCONTROLDETAILS mctrld, DWORD_PTR flags)
860 mixer *mmixer = MIX_GetMix(wDevID);
861 DWORD ctrl;
862 DWORD line;
863 control *ct;
865 if (!mctrld)
866 return MMSYSERR_INVALPARAM;
868 ctrl = mctrld->dwControlID;
869 line = ctrl/CONTROLSPERLINE;
871 if (mctrld->cbStruct != sizeof(*mctrld))
872 return MMSYSERR_INVALPARAM;
874 if (!mmixer)
875 return MMSYSERR_BADDEVICEID;
877 if (line < 0 || line >= mmixer->chans || !mmixer->controls[ctrl].enabled)
878 return MIXERR_INVALCONTROL;
880 ct = &mmixer->controls[ctrl];
882 flags &= MIXER_GETCONTROLDETAILSF_QUERYMASK;
884 switch (flags) {
885 case MIXER_GETCONTROLDETAILSF_VALUE:
886 TRACE("MIXER_GETCONTROLDETAILSF_VALUE (%d/%d)\n", ctrl, line);
887 switch (ct->c.dwControlType)
889 case MIXERCONTROL_CONTROLTYPE_VOLUME:
891 long min = 0, max = 0, vol = 0;
892 int chn;
893 LPMIXERCONTROLDETAILS_UNSIGNED mcdu;
894 snd_mixer_elem_t * elem = mmixer->lines[line].elem;
896 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_UNSIGNED))
898 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
899 return MMSYSERR_INVALPARAM;
902 TRACE("%s MIXERCONTROLDETAILS_UNSIGNED[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
904 mcdu = (LPMIXERCONTROLDETAILS_UNSIGNED)mctrld->paDetails;
906 if (mctrld->cChannels != 1 && mmixer->lines[line].chans != mctrld->cChannels)
908 WARN("Unsupported cChannels (%d instead of %d)\n", mctrld->cChannels, mmixer->lines[line].chans);
909 return MMSYSERR_INVALPARAM;
912 if (mmixer->lines[line].capt && snd_mixer_selem_has_capture_volume(elem)) {
913 snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
914 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
915 if (snd_mixer_selem_has_capture_channel(elem, chn))
917 snd_mixer_selem_get_capture_volume(elem, chn, &vol);
918 mcdu->dwValue = normalized(vol - min, max, 65535);
919 if (mctrld->cChannels == 1)
920 break;
921 ++mcdu;
923 } else {
924 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
926 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
927 if (snd_mixer_selem_has_playback_channel(elem, chn))
929 snd_mixer_selem_get_playback_volume(elem, chn, &vol);
930 mcdu->dwValue = normalized(vol - min, max, 65535);
931 if (mctrld->cChannels == 1)
932 break;
933 ++mcdu;
937 return MMSYSERR_NOERROR;
940 case MIXERCONTROL_CONTROLTYPE_ONOFF:
941 case MIXERCONTROL_CONTROLTYPE_MUTE:
943 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
944 int chn, ival;
945 snd_mixer_elem_t * elem = mmixer->lines[line].elem;
947 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
949 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
950 return MMSYSERR_INVALPARAM;
953 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
955 mcdb = (LPMIXERCONTROLDETAILS_BOOLEAN)mctrld->paDetails;
957 if (line == 1)
958 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
960 if (!snd_mixer_selem_has_capture_channel(elem, chn))
961 continue;
962 snd_mixer_selem_get_capture_switch(elem, chn, &ival);
963 break;
965 else
966 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
968 if (!snd_mixer_selem_has_playback_channel(elem, chn))
969 continue;
970 snd_mixer_selem_get_playback_switch(elem, chn, &ival);
971 break;
974 mcdb->fValue = !ival;
975 TRACE("=> %s\n", mcdb->fValue ? "on" : "off");
976 return MMSYSERR_NOERROR;
978 case MIXERCONTROL_CONTROLTYPE_MIXER:
979 case MIXERCONTROL_CONTROLTYPE_MUX:
981 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
982 int x, i=0, ival = 0, chn;
984 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
986 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
987 return MMSYSERR_INVALPARAM;
990 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
992 mcdb = (LPMIXERCONTROLDETAILS_BOOLEAN)mctrld->paDetails;
994 for (x = 0; x<mmixer->chans; ++x)
995 if (line != x && mmixer->lines[x].dst == line)
997 ival = 0;
998 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1000 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1001 continue;
1002 snd_mixer_selem_get_capture_switch(mmixer->lines[x].elem, chn, &ival);
1003 if (ival)
1004 break;
1006 if (i >= mctrld->u.cMultipleItems)
1008 TRACE("overflow\n");
1009 return MMSYSERR_INVALPARAM;
1011 TRACE("fVal[%i] = %sselected\n", i, (!ival ? "un" : ""));
1012 mcdb[i++].fValue = ival;
1014 break;
1016 default:
1018 FIXME("Unhandled controltype %s\n", getControlType(ct->c.dwControlType));
1019 return MMSYSERR_INVALPARAM;
1021 return MMSYSERR_NOERROR;
1023 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
1024 TRACE("MIXER_GETCONTROLDETAILSF_LISTTEXT (%d)\n", ctrl);
1026 if (ct->c.dwControlType == MIXERCONTROL_CONTROLTYPE_MUX || ct->c.dwControlType == MIXERCONTROL_CONTROLTYPE_MIXER)
1028 LPMIXERCONTROLDETAILS_LISTTEXTW mcdlt = (LPMIXERCONTROLDETAILS_LISTTEXTW)mctrld->paDetails;
1029 int i, j;
1031 for (i = j = 0; j < mmixer->chans; ++j)
1032 if (j != line && mmixer->lines[j].dst == line)
1034 if (i > mctrld->u.cMultipleItems)
1035 return MMSYSERR_INVALPARAM;
1036 mcdlt->dwParam1 = j;
1037 mcdlt->dwParam2 = mmixer->lines[j].component;
1038 lstrcpynW(mcdlt->szName, mmixer->lines[j].name, sizeof(mcdlt->szName) / sizeof(WCHAR));
1039 TRACE("Adding %i as %s\n", j, debugstr_w(mcdlt->szName));
1040 ++i; ++mcdlt;
1042 if (i < mctrld->u.cMultipleItems)
1043 return MMSYSERR_INVALPARAM;
1044 return MMSYSERR_NOERROR;
1046 FIXME ("Imagine this code being horribly broken and incomplete, introducing: reality\n");
1047 return MMSYSERR_INVALPARAM;
1049 default:
1050 WARN("Unknown flag (%08lx)\n", flags);
1051 return MMSYSERR_INVALPARAM;
1055 /* Set volume/capture channel/muted for control */
1056 static DWORD MIX_SetControlDetails(UINT wDevID, LPMIXERCONTROLDETAILS mctrld, DWORD_PTR flags)
1058 mixer *mmixer = MIX_GetMix(wDevID);
1059 DWORD ctrl, line, i;
1060 control *ct;
1061 snd_mixer_elem_t * elem;
1063 if (!mctrld)
1064 return MMSYSERR_INVALPARAM;
1066 ctrl = mctrld->dwControlID;
1067 line = ctrl/CONTROLSPERLINE;
1069 if (mctrld->cbStruct != sizeof(*mctrld))
1071 WARN("Invalid size of mctrld %d\n", mctrld->cbStruct);
1072 return MMSYSERR_INVALPARAM;
1075 if (!mmixer)
1076 return MMSYSERR_BADDEVICEID;
1078 if (line < 0 || line >= mmixer->chans)
1080 WARN("Invalid line id: %d not in range of 0-%d\n", line, mmixer->chans-1);
1081 return MMSYSERR_INVALPARAM;
1084 if (!mmixer->controls[ctrl].enabled)
1086 WARN("Control %d not enabled\n", ctrl);
1087 return MIXERR_INVALCONTROL;
1090 ct = &mmixer->controls[ctrl];
1091 elem = mmixer->lines[line].elem;
1092 flags &= MIXER_SETCONTROLDETAILSF_QUERYMASK;
1094 switch (flags) {
1095 case MIXER_SETCONTROLDETAILSF_VALUE:
1096 TRACE("MIXER_SETCONTROLDETAILSF_VALUE (%d)\n", ctrl);
1097 break;
1099 default:
1100 WARN("Unknown flag (%08lx)\n", flags);
1101 return MMSYSERR_INVALPARAM;
1104 switch (ct->c.dwControlType)
1106 case MIXERCONTROL_CONTROLTYPE_VOLUME:
1108 long min = 0, max = 0;
1109 int chn;
1110 LPMIXERCONTROLDETAILS_UNSIGNED mcdu;
1111 snd_mixer_elem_t * elem = mmixer->lines[line].elem;
1113 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_UNSIGNED))
1115 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1116 return MMSYSERR_INVALPARAM;
1119 if (mctrld->cChannels != 1 && mmixer->lines[line].chans != mctrld->cChannels)
1121 WARN("Unsupported cChannels (%d instead of %d)\n", mctrld->cChannels, mmixer->lines[line].chans);
1122 return MMSYSERR_INVALPARAM;
1125 TRACE("%s MIXERCONTROLDETAILS_UNSIGNED[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1126 mcdu = (LPMIXERCONTROLDETAILS_UNSIGNED)mctrld->paDetails;
1128 for (chn=0; chn<mctrld->cChannels;++chn)
1130 TRACE("Chan %d value %d\n", chn, mcdu[chn].dwValue);
1133 /* There isn't always a capture volume, so in that case change playback volume */
1134 if (mmixer->lines[line].capt && snd_mixer_selem_has_capture_volume(elem))
1136 snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
1138 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1139 if (snd_mixer_selem_has_capture_channel(elem, chn))
1141 snd_mixer_selem_set_capture_volume(elem, chn, min + normalized(mcdu->dwValue, 65535, max));
1142 if (mctrld->cChannels != 1)
1143 mcdu++;
1146 else
1148 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
1150 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1151 if (snd_mixer_selem_has_playback_channel(elem, chn))
1153 snd_mixer_selem_set_playback_volume(elem, chn, min + normalized(mcdu->dwValue, 65535, max));
1154 if (mctrld->cChannels != 1)
1155 mcdu++;
1159 break;
1161 case MIXERCONTROL_CONTROLTYPE_MUTE:
1162 case MIXERCONTROL_CONTROLTYPE_ONOFF:
1164 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
1166 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
1168 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1169 return MMSYSERR_INVALPARAM;
1172 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1174 mcdb = (LPMIXERCONTROLDETAILS_BOOLEAN)mctrld->paDetails;
1175 if (line == 1) /* Mute/unmute capturing */
1176 for (i = 0; i <= SND_MIXER_SCHN_LAST; ++i)
1178 if (snd_mixer_selem_has_capture_channel(elem, i))
1179 snd_mixer_selem_set_capture_switch(elem, i, !mcdb->fValue);
1181 else
1182 for (i = 0; i <= SND_MIXER_SCHN_LAST; ++i)
1183 if (snd_mixer_selem_has_playback_channel(elem, i))
1184 snd_mixer_selem_set_playback_switch(elem, i, !mcdb->fValue);
1185 break;
1188 case MIXERCONTROL_CONTROLTYPE_MIXER:
1189 case MIXERCONTROL_CONTROLTYPE_MUX:
1191 LPMIXERCONTROLDETAILS_BOOLEAN mcdb;
1192 int x, i=0, chn;
1193 int didone = 0, canone = (ct->c.dwControlType == MIXERCONTROL_CONTROLTYPE_MUX);
1195 if (mctrld->cbDetails != sizeof(MIXERCONTROLDETAILS_BOOLEAN))
1197 WARN("invalid parameter: cbDetails %d\n", mctrld->cbDetails);
1198 return MMSYSERR_INVALPARAM;
1201 TRACE("%s MIXERCONTROLDETAILS_BOOLEAN[%u]\n", getControlType(ct->c.dwControlType), mctrld->cChannels);
1202 mcdb = (LPMIXERCONTROLDETAILS_BOOLEAN)mctrld->paDetails;
1204 for (x=i=0; x < mmixer->chans; ++x)
1205 if (line != x && mmixer->lines[x].dst == line)
1207 TRACE("fVal[%i] (%s) = %i\n", i, debugstr_w(mmixer->lines[x].name), mcdb[i].fValue);
1208 if (i >= mctrld->u.cMultipleItems)
1210 TRACE("Too many items to fit, overflowing\n");
1211 return MIXERR_INVALVALUE;
1213 if (mcdb[i].fValue && canone && didone)
1215 TRACE("Nice try, but it's not going to work\n");
1216 elem_callback(mmixer->lines[1].elem, SND_CTL_EVENT_MASK_VALUE);
1217 return MIXERR_INVALVALUE;
1219 if (mcdb[i].fValue)
1220 didone = 1;
1221 ++i;
1224 if (canone && !didone)
1226 TRACE("Nice try, this is not going to work either\n");
1227 elem_callback(mmixer->lines[1].elem, SND_CTL_EVENT_MASK_VALUE);
1228 return MIXERR_INVALVALUE;
1231 for (x = i = 0; x<mmixer->chans; ++x)
1232 if (line != x && mmixer->lines[x].dst == line)
1234 if (mcdb[i].fValue)
1235 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1237 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1238 continue;
1239 snd_mixer_selem_set_capture_switch(mmixer->lines[x].elem, chn, mcdb[i].fValue);
1241 ++i;
1244 /* If it's a MUX, it means that only 1 channel can be selected
1245 * and the other channels are unselected
1247 * For MIXER multiple sources are allowed, so unselect here
1249 if (canone)
1250 break;
1252 for (x = i = 0; x<mmixer->chans; ++x)
1253 if (line != x && mmixer->lines[x].dst == line)
1255 if (!mcdb[i].fValue)
1256 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; ++chn)
1258 if (!snd_mixer_selem_has_capture_channel(mmixer->lines[x].elem, chn))
1259 continue;
1260 snd_mixer_selem_set_capture_switch(mmixer->lines[x].elem, chn, mcdb[i].fValue);
1262 ++i;
1264 break;
1266 default:
1267 FIXME("Unhandled type %s\n", getControlType(ct->c.dwControlType));
1268 return MMSYSERR_INVALPARAM;
1270 return MMSYSERR_NOERROR;
1273 /* Here we give info over the source/dest line given by dwSource+dwDest or dwDest, respectively
1274 * It is also possible that a line is found by componenttype or target type, latter is not implemented yet
1275 * Most important values returned in struct:
1276 * dwLineID
1277 * sz(Short)Name
1278 * line control count
1279 * amount of channels
1281 static DWORD MIX_GetLineInfo(UINT wDevID, LPMIXERLINEW Ml, DWORD_PTR flags)
1283 DWORD_PTR qf = flags & MIXER_GETLINEINFOF_QUERYMASK;
1284 mixer *mmixer = MIX_GetMix(wDevID);
1285 line *mline;
1286 int idx, i;
1288 if (!Ml)
1290 WARN("No Ml\n");
1291 return MMSYSERR_INVALPARAM;
1294 if (!mmixer)
1296 WARN("Device %u not found\n", wDevID);
1297 return MMSYSERR_BADDEVICEID;
1300 if (Ml->cbStruct != sizeof(*Ml))
1302 WARN("invalid parameter: Ml->cbStruct = %d\n", Ml->cbStruct);
1303 return MMSYSERR_INVALPARAM;
1306 Ml->fdwLine = MIXERLINE_LINEF_ACTIVE;
1307 Ml->dwUser = 0;
1309 switch (qf)
1311 case MIXER_GETLINEINFOF_COMPONENTTYPE:
1313 Ml->dwLineID = 0xFFFF;
1314 for (idx = 0; idx < mmixer->chans; ++idx)
1315 if (mmixer->lines[idx].component == Ml->dwComponentType)
1317 Ml->dwLineID = idx;
1318 break;
1320 if (Ml->dwLineID == 0xFFFF)
1321 return MMSYSERR_KEYNOTFOUND;
1322 /* Now that we have lineid, fallback to lineid*/
1325 case MIXER_GETLINEINFOF_LINEID:
1326 if (Ml->dwLineID < 0 || Ml->dwLineID >= mmixer->chans)
1327 return MIXERR_INVALLINE;
1329 TRACE("MIXER_GETLINEINFOF_LINEID %d\n", Ml->dwLineID);
1330 Ml->dwDestination = mmixer->lines[Ml->dwLineID].dst;
1332 if (Ml->dwDestination != Ml->dwLineID)
1334 Ml->dwSource = getsrcfromline(mmixer, Ml->dwLineID);
1335 Ml->cConnections = 1;
1337 else
1339 Ml->cConnections = getsrccntfromchan(mmixer, Ml->dwLineID);
1340 Ml->dwSource = 0xFFFFFFFF;
1342 TRACE("Connections %d, source %d\n", Ml->cConnections, Ml->dwSource);
1343 break;
1345 case MIXER_GETLINEINFOF_DESTINATION:
1346 if (Ml->dwDestination < 0 || Ml->dwDestination >= mmixer->dests)
1348 WARN("dest %d out of bounds\n", Ml->dwDestination);
1349 return MIXERR_INVALLINE;
1352 Ml->dwLineID = Ml->dwDestination;
1353 Ml->cConnections = getsrccntfromchan(mmixer, Ml->dwLineID);
1354 Ml->dwSource = 0xFFFFFFFF;
1355 break;
1357 case MIXER_GETLINEINFOF_SOURCE:
1358 if (Ml->dwDestination < 0 || Ml->dwDestination >= mmixer->dests)
1360 WARN("dest %d for source out of bounds\n", Ml->dwDestination);
1361 return MIXERR_INVALLINE;
1364 if (Ml->dwSource < 0 || Ml->dwSource >= getsrccntfromchan(mmixer, Ml->dwDestination))
1366 WARN("src %d out of bounds\n", Ml->dwSource);
1367 return MIXERR_INVALLINE;
1370 Ml->dwLineID = getsrclinefromchan(mmixer, Ml->dwDestination, Ml->dwSource);
1371 Ml->cConnections = 1;
1372 break;
1374 case MIXER_GETLINEINFOF_TARGETTYPE:
1375 FIXME("TODO: TARGETTYPE, stub\n");
1376 return MMSYSERR_INVALPARAM;
1378 default:
1379 FIXME("Unknown query flag: %08lx\n", qf);
1380 return MMSYSERR_INVALPARAM;
1383 if (Ml->dwLineID >= mmixer->dests)
1384 Ml->fdwLine |= MIXERLINE_LINEF_SOURCE;
1386 mline = &mmixer->lines[Ml->dwLineID];
1387 Ml->dwComponentType = mline->component;
1388 Ml->cChannels = mmixer->lines[Ml->dwLineID].chans;
1389 Ml->cControls = 0;
1391 for (i=CONTROLSPERLINE*Ml->dwLineID;i<CONTROLSPERLINE*(Ml->dwLineID+1); ++i)
1392 if (mmixer->controls[i].enabled)
1393 ++(Ml->cControls);
1395 lstrcpynW(Ml->szShortName, mmixer->lines[Ml->dwLineID].name, sizeof(Ml->szShortName)/sizeof(WCHAR));
1396 lstrcpynW(Ml->szName, mmixer->lines[Ml->dwLineID].name, sizeof(Ml->szName)/sizeof(WCHAR));
1397 if (mline->capt)
1398 Ml->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
1399 else
1400 Ml->Target.dwType = MIXERLINE_TARGETTYPE_WAVEOUT;
1401 Ml->Target.dwDeviceID = 0xFFFFFFFF;
1402 Ml->Target.wMid = WINE_MIXER_MANUF_ID;
1403 Ml->Target.wPid = WINE_MIXER_PRODUCT_ID;
1404 Ml->Target.vDriverVersion = WINE_MIXER_VERSION;
1405 lstrcpynW(Ml->Target.szPname, mmixer->mixername, sizeof(Ml->Target.szPname)/sizeof(WCHAR));
1406 return MMSYSERR_NOERROR;
1409 /* Get the controls that belong to a certain line, either all or 1 */
1410 static DWORD MIX_GetLineControls(UINT wDevID, LPMIXERLINECONTROLSW mlc, DWORD_PTR flags)
1412 mixer *mmixer = MIX_GetMix(wDevID);
1413 int i,j = 0;
1414 DWORD ct;
1416 if (!mlc || mlc->cbStruct != sizeof(*mlc))
1418 WARN("Invalid mlc %p, cbStruct: %d\n", mlc, (!mlc ? -1 : mlc->cbStruct));
1419 return MMSYSERR_INVALPARAM;
1422 if (mlc->cbmxctrl != sizeof(MIXERCONTROLW))
1424 WARN("cbmxctrl %d\n", mlc->cbmxctrl);
1425 return MMSYSERR_INVALPARAM;
1428 if (!mmixer)
1429 return MMSYSERR_BADDEVICEID;
1431 flags &= MIXER_GETLINECONTROLSF_QUERYMASK;
1433 if (flags == MIXER_GETLINECONTROLSF_ONEBYID)
1434 mlc->dwLineID = mlc->u.dwControlID / CONTROLSPERLINE;
1436 if (mlc->dwLineID < 0 || mlc->dwLineID >= mmixer->chans)
1438 TRACE("Invalid dwLineID %d\n", mlc->dwLineID);
1439 return MIXERR_INVALLINE;
1442 switch (flags)
1444 case MIXER_GETLINECONTROLSF_ALL:
1445 TRACE("line=%08x MIXER_GETLINECONTROLSF_ALL (%d)\n", mlc->dwLineID, mlc->cControls);
1446 for (i = 0; i < CONTROLSPERLINE; ++i)
1447 if (mmixer->controls[i+mlc->dwLineID * CONTROLSPERLINE].enabled)
1449 memcpy(&mlc->pamxctrl[j], &mmixer->controls[i+mlc->dwLineID * CONTROLSPERLINE].c, sizeof(MIXERCONTROLW));
1450 TRACE("Added %s (%s)\n", debugstr_w(mlc->pamxctrl[j].szShortName), debugstr_w(mlc->pamxctrl[j].szName));
1451 ++j;
1452 if (j > mlc->cControls)
1454 WARN("invalid parameter\n");
1455 return MMSYSERR_INVALPARAM;
1459 if (!j || mlc->cControls > j)
1461 WARN("invalid parameter\n");
1462 return MMSYSERR_INVALPARAM;
1464 break;
1465 case MIXER_GETLINECONTROLSF_ONEBYID:
1466 TRACE("line=%08x MIXER_GETLINECONTROLSF_ONEBYID (%x)\n", mlc->dwLineID, mlc->u.dwControlID);
1468 if (!mmixer->controls[mlc->u.dwControlID].enabled)
1469 return MIXERR_INVALCONTROL;
1471 mlc->pamxctrl[0] = mmixer->controls[mlc->u.dwControlID].c;
1472 break;
1473 case MIXER_GETLINECONTROLSF_ONEBYTYPE:
1474 TRACE("line=%08x MIXER_GETLINECONTROLSF_ONEBYTYPE (%s)\n", mlc->dwLineID, getControlType(mlc->u.dwControlType));
1476 ct = mlc->u.dwControlType & MIXERCONTROL_CT_CLASS_MASK;
1477 for (i = 0; i <= CONTROLSPERLINE; ++i)
1479 const int ofs = i+mlc->dwLineID*CONTROLSPERLINE;
1480 if (i == CONTROLSPERLINE)
1482 WARN("invalid parameter: control %s not found\n", getControlType(mlc->u.dwControlType));
1483 return MIXERR_INVALCONTROL;
1485 if (mmixer->controls[ofs].enabled && (mmixer->controls[ofs].c.dwControlType & MIXERCONTROL_CT_CLASS_MASK) == ct)
1487 mlc->pamxctrl[0] = mmixer->controls[ofs].c;
1488 break;
1491 break;
1492 default:
1493 FIXME("Unknown flag %08lx\n", flags & MIXER_GETLINECONTROLSF_QUERYMASK);
1494 return MMSYSERR_INVALPARAM;
1497 return MMSYSERR_NOERROR;
1500 #endif /*HAVE_ALSA*/
1502 /**************************************************************************
1503 * mxdMessage (WINEALSA.3)
1505 DWORD WINAPI ALSA_mxdMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1506 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1508 #ifdef HAVE_ALSA
1509 DWORD ret;
1510 TRACE("(%04X, %s, %08lX, %08lX, %08lX);\n", wDevID, getMessage(wMsg),
1511 dwUser, dwParam1, dwParam2);
1513 switch (wMsg)
1515 case DRVM_INIT: ALSA_MixerInit(); ret = MMSYSERR_NOERROR; break;
1516 case DRVM_EXIT: ALSA_MixerExit(); ret = MMSYSERR_NOERROR; break;
1517 /* All taken care of by driver initialisation */
1518 /* Unimplemented, and not needed */
1519 case DRVM_ENABLE:
1520 case DRVM_DISABLE:
1521 ret = MMSYSERR_NOERROR; break;
1523 case MXDM_OPEN:
1524 ret = MIX_Open(wDevID, (LPMIXEROPENDESC) dwParam1, dwParam2); break;
1526 case MXDM_CLOSE:
1527 ret = MIX_Close(wDevID); break;
1529 case MXDM_GETDEVCAPS:
1530 ret = MIX_GetDevCaps(wDevID, (LPMIXERCAPS2W)dwParam1, dwParam2); break;
1532 case MXDM_GETLINEINFO:
1533 ret = MIX_GetLineInfo(wDevID, (LPMIXERLINEW)dwParam1, dwParam2); break;
1535 case MXDM_GETLINECONTROLS:
1536 ret = MIX_GetLineControls(wDevID, (LPMIXERLINECONTROLSW)dwParam1, dwParam2); break;
1538 case MXDM_GETCONTROLDETAILS:
1539 ret = MIX_GetControlDetails(wDevID, (LPMIXERCONTROLDETAILS)dwParam1, dwParam2); break;
1541 case MXDM_SETCONTROLDETAILS:
1542 ret = MIX_SetControlDetails(wDevID, (LPMIXERCONTROLDETAILS)dwParam1, dwParam2); break;
1544 case MXDM_GETNUMDEVS:
1545 ret = cards; break;
1547 default:
1548 WARN("unknown message %s!\n", getMessage(wMsg));
1549 return MMSYSERR_NOTSUPPORTED;
1552 TRACE("Returning %08X\n", ret);
1553 return ret;
1554 #else /*HAVE_ALSA*/
1555 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1557 return MMSYSERR_NOTENABLED;
1558 #endif /*HAVE_ALSA*/