[SNDVOL32] Initialize and update the volume and balance trackbars. Moving the trackba...
[reactos.git] / base / applications / sndvol32 / sndvol32.c
1 /*
2 * ReactOS Sound Volume Control
3 * Copyright (C) 2004-2005 Thomas Weidenmueller
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS Sound Volume Control
22 * FILE: base/applications/sndvol32/sndvol32.c
23 * PROGRAMMERS: Thomas Weidenmueller <w3seek@reactos.com>
24 */
25
26 #include "sndvol32.h"
27
28 #include <shellapi.h>
29
30 HINSTANCE hAppInstance;
31 ATOM MainWindowClass;
32 HWND hMainWnd;
33 HANDLE hAppHeap;
34 LPTSTR lpAppTitle;
35 PREFERENCES_CONTEXT Preferences;
36
37 #define GetDialogData(hwndDlg, type) \
38 ( P##type )GetWindowLongPtr((hwndDlg), DWLP_USER)
39 #define GetWindowData(hwnd, type) \
40 ( P##type )GetWindowLongPtr((hwnd), GWL_USERDATA)
41
42 /******************************************************************************/
43
44
45
46 typedef struct _PREFERENCES_FILL_DEVICES
47 {
48 PPREFERENCES_CONTEXT PrefContext;
49 HWND hComboBox;
50 UINT Selected;
51 } PREFERENCES_FILL_DEVICES, *PPREFERENCES_FILL_DEVICES;
52
53 static BOOL CALLBACK
54 FillDeviceComboBox(PSND_MIXER Mixer,
55 UINT Id,
56 LPCTSTR ProductName,
57 PVOID Context)
58 {
59 LRESULT lres;
60 PPREFERENCES_FILL_DEVICES FillContext = (PPREFERENCES_FILL_DEVICES)Context;
61
62 UNREFERENCED_PARAMETER(Mixer);
63
64 lres = SendMessage(FillContext->hComboBox,
65 CB_ADDSTRING,
66 0,
67 (LPARAM)ProductName);
68 if (lres != CB_ERR)
69 {
70 /* save the index so we don't screw stuff when the combobox is sorted... */
71 SendMessage(FillContext->hComboBox,
72 CB_SETITEMDATA,
73 (WPARAM)lres,
74 Id);
75
76 if (Id == FillContext->Selected)
77 {
78 SendMessage(FillContext->hComboBox,
79 CB_SETCURSEL,
80 (WPARAM)lres,
81 0);
82 }
83 }
84
85 return TRUE;
86 }
87
88 static BOOL CALLBACK
89 PrefDlgAddLine(PSND_MIXER Mixer,
90 LPMIXERLINE Line,
91 UINT DisplayControls,
92 PVOID Context)
93 {
94 PPREFERENCES_CONTEXT PrefContext = (PPREFERENCES_CONTEXT)Context;
95
96 UNREFERENCED_PARAMETER(Mixer);
97 UNREFERENCED_PARAMETER(DisplayControls);
98
99 switch (Line->dwComponentType)
100 {
101 case MIXERLINE_COMPONENTTYPE_DST_SPEAKERS:
102 if (PrefContext->PlaybackID == (DWORD)-1)
103 {
104 PrefContext->PlaybackID = Line->dwLineID;
105
106 if (PrefContext->SelectedLine == (DWORD)-1)
107 {
108 PrefContext->SelectedLine = Line->dwLineID;
109 }
110 }
111 else
112 goto AddToOthersLines;
113
114 break;
115
116 case MIXERLINE_COMPONENTTYPE_DST_WAVEIN:
117 if (PrefContext->RecordingID == (DWORD)-1)
118 {
119 PrefContext->RecordingID = Line->dwLineID;
120
121 if (PrefContext->SelectedLine == (DWORD)-1)
122 {
123 PrefContext->SelectedLine = Line->dwLineID;
124 }
125 }
126 else
127 goto AddToOthersLines;
128
129 break;
130
131 default:
132 {
133 LRESULT lres;
134 HWND hwndCbOthers;
135
136 if (PrefContext->SelectedLine == (DWORD)-1)
137 {
138 PrefContext->SelectedLine = Line->dwLineID;
139 }
140
141 AddToOthersLines:
142 hwndCbOthers = GetDlgItem(PrefContext->hwndDlg,
143 IDC_LINE);
144
145 lres = SendMessage(hwndCbOthers,
146 CB_ADDSTRING,
147 0,
148 (LPARAM)Line->szName);
149 if (lres != CB_ERR)
150 {
151 SendMessage(hwndCbOthers,
152 CB_SETITEMDATA,
153 (WPARAM)lres,
154 Line->dwLineID);
155
156 PrefContext->OtherLines++;
157 }
158 break;
159 }
160 }
161
162 return TRUE;
163 }
164
165 static BOOL CALLBACK
166 PrefDlgAddConnection(PSND_MIXER Mixer,
167 DWORD LineID,
168 LPMIXERLINE Line,
169 PVOID Context)
170 {
171 PPREFERENCES_CONTEXT PrefContext = (PPREFERENCES_CONTEXT)Context;
172 HWND hwndControls;
173 LVITEM lvi;
174 UINT i;
175
176 UNREFERENCED_PARAMETER(Mixer);
177 UNREFERENCED_PARAMETER(LineID);
178
179 if (Line->cControls != 0)
180 {
181 hwndControls = GetDlgItem(PrefContext->hwndDlg,
182 IDC_CONTROLS);
183
184 lvi.mask = LVIF_TEXT | LVIF_PARAM;
185 lvi.iItem = PrefContext->tmp++;
186 lvi.iSubItem = 0;
187 lvi.pszText = Line->szName;
188 lvi.lParam = (LPARAM)Line->dwSource;
189
190 i = SendMessage(hwndControls,
191 LVM_INSERTITEM,
192 0,
193 (LPARAM)&lvi);
194 if (i != (UINT)-1)
195 {
196 TCHAR LineName[MIXER_LONG_NAME_CHARS];
197 DWORD Flags;
198 BOOL SelLine = FALSE;
199
200 if (SndMixerGetLineName(PrefContext->Mixer,
201 PrefContext->SelectedLine,
202 LineName,
203 MIXER_LONG_NAME_CHARS,
204 TRUE) == -1)
205 {
206 LineName[0] = TEXT('\0');
207 }
208
209 if (ReadLineConfig(PrefContext->DeviceName,
210 LineName,
211 Line->szName,
212 &Flags))
213 {
214 if (Flags != 0x4)
215 {
216 SelLine = TRUE;
217 }
218 }
219
220 ListView_SetCheckState(hwndControls,
221 i,
222 SelLine);
223 }
224 }
225
226 return TRUE;
227 }
228
229 static VOID
230 UpdatePrefDlgControls(PPREFERENCES_CONTEXT Context,
231 DWORD LineID)
232 {
233 INT OldID, MixerID = 0;
234 INT DeviceCbIndex;
235
236 /* select the mixer */
237 DeviceCbIndex = SendDlgItemMessage(Context->hwndDlg,
238 IDC_MIXERDEVICE,
239 CB_GETCURSEL,
240 0,
241 0);
242 if (DeviceCbIndex != CB_ERR)
243 {
244 MixerID = SendDlgItemMessage(Context->hwndDlg,
245 IDC_MIXERDEVICE,
246 CB_GETITEMDATA,
247 DeviceCbIndex,
248 0);
249 if (MixerID == CB_ERR)
250 {
251 MixerID = 0;
252 }
253 }
254
255 OldID = Context->Selected;
256 if (MixerID != OldID &&
257 SndMixerSelect(Context->Mixer,
258 MixerID))
259 {
260 Context->Selected = SndMixerGetSelection(Context->Mixer);
261
262 /* update the controls */
263 Context->PlaybackID = (DWORD)-1;
264 Context->RecordingID = (DWORD)-1;
265 Context->OtherLines = 0;
266 Context->SelectedLine = (DWORD)-1;
267
268 SndMixerGetProductName(Context->Mixer,
269 Context->DeviceName,
270 sizeof(Context->DeviceName) / sizeof(Context->DeviceName[0]));
271
272 if (SndMixerEnumLines(Context->Mixer,
273 PrefDlgAddLine,
274 Context))
275 {
276 UINT SelBox = 0;
277
278 /* enable/disable controls and make default selection */
279 EnableWindow(GetDlgItem(Context->hwndDlg,
280 IDC_PLAYBACK),
281 Context->PlaybackID != (DWORD)-1);
282 CheckDlgButton(Context->hwndDlg,
283 IDC_PLAYBACK,
284 (Context->PlaybackID != (DWORD)-1 && SelBox++ == 0) ?
285 BST_CHECKED : BST_UNCHECKED);
286
287 EnableWindow(GetDlgItem(Context->hwndDlg,
288 IDC_RECORDING),
289 Context->RecordingID != (DWORD)-1);
290 CheckDlgButton(Context->hwndDlg,
291 IDC_RECORDING,
292 (Context->RecordingID != (DWORD)-1 && SelBox++ == 0) ?
293 BST_CHECKED : BST_UNCHECKED);
294
295 if (Context->OtherLines != 0)
296 {
297 /* select the first item in the other lines combo box by default */
298 SendDlgItemMessage(Context->hwndDlg,
299 IDC_LINE,
300 CB_SETCURSEL,
301 0,
302 0);
303 }
304 EnableWindow(GetDlgItem(Context->hwndDlg,
305 IDC_LINE),
306 FALSE);
307 EnableWindow(GetDlgItem(Context->hwndDlg,
308 IDC_OTHER),
309 Context->OtherLines != 0);
310 CheckDlgButton(Context->hwndDlg,
311 IDC_LINE,
312 (Context->OtherLines != 0 && SelBox++ == 0) ?
313 BST_CHECKED : BST_UNCHECKED);
314
315 /* disable the OK button if the device doesn't have any lines */
316 EnableWindow(GetDlgItem(Context->hwndDlg,
317 IDOK),
318 Context->PlaybackID != (DWORD)-1 ||
319 Context->RecordingID != (DWORD)-1 ||
320 Context->OtherLines != 0);
321
322 LineID = Context->SelectedLine;
323 }
324 }
325
326 /* update the line sources list */
327 if ((MixerID != OldID && Context->SelectedLine != (DWORD)-1) ||
328 (Context->SelectedLine != LineID && LineID != (DWORD)-1))
329 {
330 Context->SelectedLine = LineID;
331
332 (void)ListView_DeleteAllItems(GetDlgItem(Context->hwndDlg,
333 IDC_CONTROLS));
334
335 Context->tmp = 0;
336 SndMixerEnumConnections(Context->Mixer,
337 LineID,
338 PrefDlgAddConnection,
339 Context);
340 }
341 }
342
343 static
344 VOID
345 WriteLineSettings(PPREFERENCES_CONTEXT Context, HWND hwndDlg)
346 {
347 HWND hwndControls;
348 INT Count, Index;
349 WCHAR LineName[MIXER_LONG_NAME_CHARS];
350 WCHAR DestinationName[MIXER_LONG_NAME_CHARS];
351 DWORD Flags;
352 PSNDVOL_REG_LINESTATE LineStates;
353
354 /* get list view */
355 hwndControls = GetDlgItem(hwndDlg, IDC_CONTROLS);
356
357 /* get list item count */
358 Count = ListView_GetItemCount(hwndControls);
359
360 /* sanity check */
361 assert(Count);
362
363 if (SndMixerGetLineName(Context->Mixer, Context->SelectedLine, DestinationName, MIXER_LONG_NAME_CHARS, TRUE) == -1)
364 {
365 /* failed to get destination line name */
366 return;
367 }
368
369 /* allocate line states array */
370 LineStates = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SNDVOL_REG_LINESTATE) * Count);
371 if (LineStates == NULL)
372 {
373 /* failed to allocate line states array */
374 return;
375 }
376
377
378 for(Index = 0; Index < Count; Index++)
379 {
380 /* set to empty */
381 LineName[0] = L'\0';
382
383 /* get item text */
384 ListView_GetItemText(hwndControls, Index, 0, LineName, MIXER_LONG_NAME_CHARS);
385
386 /* make sure it is null terminated */
387 LineName[MIXER_LONG_NAME_CHARS-1] = L'\0';
388
389 /* get check state */
390 Flags = (ListView_GetCheckState(hwndControls, Index) == 0 ? 0x4 : 0);
391
392 /* copy line name */
393 wcscpy(LineStates[Index].LineName, LineName);
394
395 /* store flags */
396 LineStates[Index].Flags = Flags;
397 }
398
399 /* now write the line config */
400 WriteLineConfig(Context->DeviceName, DestinationName, LineStates, sizeof(SNDVOL_REG_LINESTATE) * Count);
401
402 /* free line states */
403 HeapFree(GetProcessHeap(), 0, LineStates);
404 }
405
406 static INT_PTR CALLBACK
407 DlgPreferencesProc(HWND hwndDlg,
408 UINT uMsg,
409 WPARAM wParam,
410 LPARAM lParam)
411 {
412 PPREFERENCES_CONTEXT Context;
413
414 switch (uMsg)
415 {
416 case WM_COMMAND:
417 {
418 Context = GetDialogData(hwndDlg,
419 PREFERENCES_CONTEXT);
420 switch (LOWORD(wParam))
421 {
422 case IDC_MIXERDEVICE:
423 {
424 if (HIWORD(wParam) == CBN_SELCHANGE)
425 {
426 UpdatePrefDlgControls(Context,
427 (DWORD)-1);
428 }
429 break;
430 }
431
432 case IDC_LINE:
433 {
434 if (HIWORD(wParam) == CBN_SELCHANGE)
435 {
436 INT LineID;
437 INT Index;
438
439 Index = SendDlgItemMessage(hwndDlg,
440 IDC_LINE,
441 CB_GETCURSEL,
442 0,
443 0);
444 if (Index != CB_ERR)
445 {
446 LineID = SendDlgItemMessage(hwndDlg,
447 IDC_LINE,
448 CB_GETITEMDATA,
449 Index,
450 0);
451 if (LineID != CB_ERR)
452 {
453 UpdatePrefDlgControls(Context,
454 LineID);
455 }
456 }
457 }
458 break;
459 }
460
461 case IDC_PLAYBACK:
462 {
463 UpdatePrefDlgControls(Context,
464 Context->PlaybackID);
465 EnableWindow(GetDlgItem(hwndDlg,
466 IDC_LINE),
467 FALSE);
468 break;
469 }
470
471 case IDC_RECORDING:
472 {
473 UpdatePrefDlgControls(Context,
474 Context->RecordingID);
475 EnableWindow(GetDlgItem(hwndDlg,
476 IDC_LINE),
477 FALSE);
478 break;
479 }
480
481 case IDC_OTHER:
482 {
483 INT LineCbIndex;
484 INT LineID;
485
486 EnableWindow(GetDlgItem(hwndDlg,
487 IDC_LINE),
488 TRUE);
489
490 LineCbIndex = SendDlgItemMessage(hwndDlg,
491 IDC_LINE,
492 CB_GETCURSEL,
493 0,
494 0);
495 if (LineCbIndex != CB_ERR)
496 {
497 LineID = SendDlgItemMessage(hwndDlg,
498 IDC_LINE,
499 CB_GETITEMDATA,
500 LineCbIndex,
501 0);
502 if (LineID != CB_ERR)
503 {
504 UpdatePrefDlgControls(Context,
505 LineID);
506 }
507 }
508 break;
509 }
510
511 case IDOK:
512 {
513 /* write line settings */
514 WriteLineSettings(Context, hwndDlg);
515
516 /* fall through */
517 }
518 case IDCANCEL:
519 {
520 EndDialog(hwndDlg,
521 LOWORD(wParam));
522 break;
523 }
524 }
525 break;
526 }
527
528 case WM_INITDIALOG:
529 {
530 PREFERENCES_FILL_DEVICES FillDevContext;
531 LVCOLUMN lvc;
532 RECT rcClient;
533 HWND hwndControls;
534
535 SetWindowLongPtr(hwndDlg,
536 DWLP_USER,
537 (LONG_PTR)lParam);
538 Context = (PPREFERENCES_CONTEXT)((LONG_PTR)lParam);
539 Context->hwndDlg = hwndDlg;
540 Context->Mixer = SndMixerCreate(hwndDlg, Context->MixerWindow->MixerId);
541 Context->Selected = (UINT)-1;
542
543 FillDevContext.PrefContext = Context;
544 FillDevContext.hComboBox = GetDlgItem(hwndDlg,
545 IDC_MIXERDEVICE);
546 FillDevContext.Selected = SndMixerGetSelection(Context->Mixer);
547 SndMixerEnumProducts(Context->Mixer,
548 FillDeviceComboBox,
549 &FillDevContext);
550
551 /* initialize the list view control */
552 hwndControls = GetDlgItem(hwndDlg,
553 IDC_CONTROLS);
554 (void)ListView_SetExtendedListViewStyle(hwndControls,
555 LVS_EX_CHECKBOXES);
556
557 GetClientRect(hwndControls,
558 &rcClient);
559 lvc.mask = LVCF_TEXT | LVCF_WIDTH;
560 lvc.pszText = TEXT("");
561 lvc.cx = rcClient.right;
562 SendMessage(hwndControls,
563 LVM_INSERTCOLUMN,
564 0,
565 (LPARAM)&lvc);
566
567 /* update all controls */
568 UpdatePrefDlgControls(Context,
569 (DWORD)Context->SelectedLine);
570 return TRUE;
571 }
572
573 case WM_CLOSE:
574 {
575 EndDialog(hwndDlg,
576 IDCANCEL);
577 break;
578 }
579
580 case WM_SYSCOLORCHANGE:
581 {
582 HWND hwndControls;
583
584 /* Forward WM_SYSCOLORCHANGE */
585 hwndControls = GetDlgItem(hwndDlg, IDC_CONTROLS);
586 SendMessage(hwndControls, WM_SYSCOLORCHANGE, 0, 0);
587 break;
588 }
589 }
590
591 return 0;
592 }
593
594
595 /******************************************************************************/
596
597 static VOID
598 DeleteMixerWindowControls(PMIXER_WINDOW MixerWindow)
599 {
600 DWORD Index;
601
602 for(Index = 0; Index < MixerWindow->WindowCount; Index++)
603 {
604 /* destroys the window */
605 DestroyWindow(MixerWindow->Window[Index]);
606 }
607
608 /* free memory */
609 HeapFree(GetProcessHeap(), 0, MixerWindow->Window);
610
611 /* set to null */
612 MixerWindow->Window = NULL;
613 MixerWindow->WindowCount = 0;
614 }
615
616 static BOOL
617 RebuildMixerWindowControls(PPREFERENCES_CONTEXT PrefContext)
618 {
619 /* delete existing mixer controls */
620 DeleteMixerWindowControls(PrefContext->MixerWindow);
621
622 /* load new mixer controls */
623 LoadDialogCtrls(PrefContext);
624
625 return TRUE;
626 }
627
628 static
629 BOOL
630 CALLBACK
631 SetVolumeCallback(PSND_MIXER Mixer, DWORD LineID, LPMIXERLINE Line, PVOID Ctx)
632 {
633 UINT ControlCount = 0, Index;
634 LPMIXERCONTROL Control = NULL;
635 MIXERCONTROLDETAILS_UNSIGNED uDetails;
636 MIXERCONTROLDETAILS_BOOLEAN bDetails;
637 PSET_VOLUME_CONTEXT Context = (PSET_VOLUME_CONTEXT)Ctx;
638
639 /* check if the line name is equal */
640 if (wcsicmp(Line->szName, Context->LineName))
641 {
642 /* it is not */
643 return TRUE;
644 }
645
646 /* query controls */
647 if (SndMixerQueryControls(Mixer, &ControlCount, Line, &Control) == FALSE)
648 {
649 /* failed to query for controls */
650 return FALSE;
651 }
652
653 /* now go through all controls and compare control ids */
654 for (Index = 0; Index < ControlCount; Index++)
655 {
656 if (Context->bVertical)
657 {
658 if ((Control[Index].dwControlType & MIXERCONTROL_CT_CLASS_MASK) == MIXERCONTROL_CT_CLASS_FADER)
659 {
660 DWORD Step = (Control[Index].Bounds.dwMaximum - Control[Index].Bounds.dwMinimum) / (VOLUME_MAX - VOLUME_MIN);
661
662 /* set up details */
663 uDetails.dwValue = ((VOLUME_MAX - Context->SliderPos) * Step) + Control[Index].Bounds.dwMinimum;
664
665 /* set volume */
666 SndMixerSetVolumeControlDetails(Preferences.MixerWindow->Mixer, Control[Index].dwControlID, 1, sizeof(MIXERCONTROLDETAILS_UNSIGNED), (LPVOID)&uDetails);
667
668 /* done */
669 break;
670 }
671 }
672 else if (Context->bSwitch)
673 {
674 if ((Control[Index].dwControlType & MIXERCONTROL_CT_CLASS_MASK) == MIXERCONTROL_CT_CLASS_SWITCH)
675 {
676 /* set up details */
677 bDetails.fValue = Context->SliderPos;
678
679 /* set volume */
680 SndMixerSetVolumeControlDetails(Preferences.MixerWindow->Mixer, Control[Index].dwControlID, 1, sizeof(MIXERCONTROLDETAILS_BOOLEAN), (LPVOID)&bDetails);
681
682 /* done */
683 break;
684 }
685 }
686 else
687 {
688 /* FIXME: implement left - right channel switch support */
689 assert(0);
690 }
691 }
692
693 /* free controls */
694 HeapFree(GetProcessHeap(), 0, Control);
695
696
697 /* done */
698 return TRUE;
699 }
700
701 static
702 BOOL
703 CALLBACK
704 MixerControlChangeCallback(PSND_MIXER Mixer, DWORD LineID, LPMIXERLINE Line, PVOID Context)
705 {
706 PMIXERCONTROLDETAILS_UNSIGNED pVolumeDetails = NULL;
707 UINT ControlCount = 0, Index;
708 LPMIXERCONTROL Control = NULL;
709
710 /* check if the line has controls */
711 if (Line->cControls == 0)
712 {
713 /* no controls */
714 return TRUE;
715 }
716
717 /* query controls */
718 if (SndMixerQueryControls(Mixer, &ControlCount, Line, &Control) == FALSE)
719 {
720 /* failed to query for controls */
721 return FALSE;
722 }
723
724 pVolumeDetails = HeapAlloc(GetProcessHeap(),
725 0,
726 Line->cChannels * sizeof(MIXERCONTROLDETAILS_UNSIGNED));
727 if (pVolumeDetails == NULL)
728 goto done;
729
730 /* now go through all controls and compare control ids */
731 for (Index = 0; Index < ControlCount; Index++)
732 {
733 if (Control[Index].dwControlID == PtrToUlong(Context))
734 {
735 if ((Control[Index].dwControlType & MIXERCONTROL_CT_CLASS_MASK) == MIXERCONTROL_CT_CLASS_SWITCH)
736 {
737 MIXERCONTROLDETAILS_BOOLEAN Details;
738
739 /* get volume control details */
740 if (SndMixerGetVolumeControlDetails(Preferences.MixerWindow->Mixer, Control[Index].dwControlID, 1, sizeof(MIXERCONTROLDETAILS_BOOLEAN), (LPVOID)&Details) != -1)
741 {
742 /* update dialog control */
743 UpdateDialogLineSwitchControl(&Preferences, Line, Details.fValue);
744 }
745 }
746 else if ((Control[Index].dwControlType & MIXERCONTROL_CT_CLASS_MASK) == MIXERCONTROL_CT_CLASS_FADER)
747 {
748 /* get volume control details */
749 if (SndMixerGetVolumeControlDetails(Preferences.MixerWindow->Mixer, Control[Index].dwControlID, Line->cChannels, sizeof(MIXERCONTROLDETAILS_UNSIGNED), (LPVOID)pVolumeDetails) != -1)
750 {
751 /* update dialog control */
752 DWORD volumePosition, volumeStep, maxVolume, i;
753 DWORD balancePosition, balanceStep;
754
755 volumeStep = (Control[Index].Bounds.dwMaximum - Control[Index].Bounds.dwMinimum) / (VOLUME_MAX - VOLUME_MIN);
756
757 maxVolume = 0;
758 for (i = 0; i < Line->cChannels; i++)
759 {
760 if (pVolumeDetails[i].dwValue > maxVolume)
761 maxVolume = pVolumeDetails[i].dwValue;
762 }
763
764 volumePosition = (maxVolume - Control[Index].Bounds.dwMinimum) / volumeStep;
765
766 if (Line->cChannels == 1)
767 {
768 balancePosition = BALANCE_CENTER;
769 }
770 else if (Line->cChannels == 2)
771 {
772 if (pVolumeDetails[0].dwValue == pVolumeDetails[1].dwValue)
773 {
774 balancePosition = BALANCE_CENTER;
775 }
776 else if (pVolumeDetails[0].dwValue == Control[Index].Bounds.dwMinimum)
777 {
778 balancePosition = BALANCE_RIGHT;
779 }
780 else if (pVolumeDetails[1].dwValue == Control[Index].Bounds.dwMinimum)
781 {
782 balancePosition = BALANCE_LEFT;
783 }
784 else
785 {
786 balanceStep = (maxVolume - Control[Index].Bounds.dwMinimum) / (BALANCE_STEPS / 2);
787
788 if (pVolumeDetails[0].dwValue < pVolumeDetails[1].dwValue)
789 {
790 balancePosition = (pVolumeDetails[0].dwValue - Control[Index].Bounds.dwMinimum) / balanceStep;
791 balancePosition = BALANCE_RIGHT - balancePosition;
792 }
793 else if (pVolumeDetails[1].dwValue < pVolumeDetails[0].dwValue)
794 {
795 balancePosition = (pVolumeDetails[1].dwValue - Control[Index].Bounds.dwMinimum) / balanceStep;
796 balancePosition = BALANCE_LEFT + balancePosition;
797 }
798 }
799 }
800
801 /* Update the volume control slider */
802 UpdateDialogLineSliderControl(&Preferences, Line, IDC_LINE_SLIDER_VERT, VOLUME_MAX - volumePosition);
803
804 /* Update the balance control slider */
805 UpdateDialogLineSliderControl(&Preferences, Line, IDC_LINE_SLIDER_HORZ, balancePosition);
806 }
807 }
808 break;
809 }
810 }
811
812 done:
813 /* Free the volume details */
814 if (pVolumeDetails)
815 HeapFree(GetProcessHeap(), 0, pVolumeDetails);
816
817 /* free controls */
818 HeapFree(GetProcessHeap(), 0, Control);
819
820 /* done */
821 return TRUE;
822 }
823
824
825 static LRESULT CALLBACK
826 MainWindowProc(HWND hwnd,
827 UINT uMsg,
828 WPARAM wParam,
829 LPARAM lParam)
830 {
831 PMIXER_WINDOW MixerWindow;
832 DWORD CtrlID, LineOffset;
833 LRESULT Result = 0;
834 SET_VOLUME_CONTEXT Context;
835
836 switch (uMsg)
837 {
838 case WM_COMMAND:
839 {
840 MixerWindow = GetWindowData(hwnd,
841 MIXER_WINDOW);
842
843 switch (LOWORD(wParam))
844 {
845 case IDC_PROPERTIES:
846 {
847 PREFERENCES_CONTEXT Pref;
848
849 Pref.MixerWindow = MixerWindow;
850 Pref.Mixer = NULL;
851 Pref.SelectedLine = Preferences.SelectedLine;
852
853 if (DialogBoxParam(hAppInstance,
854 MAKEINTRESOURCE(IDD_PREFERENCES),
855 hwnd,
856 DlgPreferencesProc,
857 (LPARAM)&Pref) == IDOK)
858 {
859 /* update window */
860 TCHAR szProduct[MAXPNAMELEN];
861
862 /* get mixer product name */
863 if (SndMixerGetProductName(Pref.Mixer,
864 szProduct,
865 sizeof(szProduct) / sizeof(szProduct[0])) == -1)
866 {
867 /* failed to get name */
868 szProduct[0] = L'\0';
869 }
870 else
871 {
872 /* copy product */
873 wcscpy(Preferences.DeviceName, szProduct);
874 }
875
876 /* destroy old status bar */
877 if (MixerWindow->Mode == NORMAL_MODE)
878 DestroyWindow(MixerWindow->hStatusBar);
879
880 /* update details */
881 Preferences.SelectedLine = Pref.SelectedLine;
882
883 /* destroy old mixer */
884 SndMixerDestroy(Preferences.MixerWindow->Mixer);
885
886 /* use new selected mixer */
887 Preferences.MixerWindow->Mixer = Pref.Mixer;
888
889 /* rebuild dialog controls */
890 RebuildMixerWindowControls(&Preferences);
891
892 /* create status window */
893 if (MixerWindow->Mode == NORMAL_MODE)
894 {
895 MixerWindow->hStatusBar = CreateStatusWindow(WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS,
896 NULL,
897 hwnd,
898 0);
899 if (MixerWindow->hStatusBar)
900 {
901 /* Set status bar */
902 SendMessage(MixerWindow->hStatusBar,
903 WM_SETTEXT,
904 0,
905 (LPARAM)szProduct);
906 }
907 }
908 }
909 break;
910 }
911
912 case IDC_EXIT:
913 {
914 PostQuitMessage(0);
915 break;
916 }
917
918 case IDC_ABOUT:
919 {
920 HICON hAppIcon = (HICON)GetClassLongPtrW(hwnd,
921 GCLP_HICON);
922 ShellAbout(hwnd,
923 lpAppTitle,
924 NULL,
925 hAppIcon);
926 break;
927 }
928
929 default:
930 {
931 /* get button id */
932 CtrlID = LOWORD(wParam);
933
934 /* check if the message is from the line switch */
935 if (HIWORD(wParam) == BN_CLICKED && (CtrlID % IDC_LINE_SWITCH == 0))
936 {
937 /* compute line offset */
938 LineOffset = CtrlID / IDC_LINE_SWITCH;
939
940 /* compute window id of line name static control */
941 CtrlID = LineOffset * IDC_LINE_NAME;
942
943 /* get line name */
944 if (GetDlgItemTextW(hwnd, CtrlID, Context.LineName, MIXER_LONG_NAME_CHARS) != 0)
945 {
946 /* setup context */
947 Context.SliderPos = SendMessage((HWND)lParam, BM_GETCHECK, 0, 0);
948 Context.bVertical = FALSE;
949 Context.bSwitch = TRUE;
950
951 /* set volume */
952 SndMixerEnumConnections(Preferences.MixerWindow->Mixer, Preferences.SelectedLine, SetVolumeCallback, (LPVOID)&Context);
953 }
954 }
955 }
956
957 }
958 break;
959 }
960
961 case MM_MIXM_LINE_CHANGE:
962 {
963 DPRINT("MM_MIXM_LINE_CHANGE\n");
964 break;
965 }
966
967 case MM_MIXM_CONTROL_CHANGE:
968 {
969 DPRINT("MM_MIXM_CONTROL_CHANGE\n");
970
971 /* get mixer window */
972 MixerWindow = GetWindowData(hwnd,
973 MIXER_WINDOW);
974
975 /* sanity checks */
976 assert(MixerWindow);
977 assert(MixerWindow->Mixer->hmx == (HMIXER)wParam);
978
979 SndMixerEnumConnections(MixerWindow->Mixer, Preferences.SelectedLine, MixerControlChangeCallback, (PVOID)lParam);
980 break;
981 }
982
983 case WM_VSCROLL:
984 switch (LOWORD(wParam))
985 {
986 case TB_THUMBTRACK:
987 /* get dialog item ctrl */
988 CtrlID = GetDlgCtrlID((HWND)lParam);
989
990 /* get line index */
991 LineOffset = CtrlID / IDC_LINE_SLIDER_VERT;
992
993 /* compute window id of line name static control */
994 CtrlID = LineOffset * IDC_LINE_NAME;
995
996 /* get line name */
997 if (GetDlgItemTextW(hwnd, CtrlID, Context.LineName, MIXER_LONG_NAME_CHARS) != 0)
998 {
999 /* setup context */
1000 Context.SliderPos = HIWORD(wParam);
1001 Context.bVertical = TRUE;
1002 Context.bSwitch = FALSE;
1003
1004 /* set volume */
1005 SndMixerEnumConnections(Preferences.MixerWindow->Mixer, Preferences.SelectedLine, SetVolumeCallback, (LPVOID)&Context);
1006 }
1007 break;
1008
1009 case TB_ENDTRACK:
1010 MixerWindow = GetWindowData(hwnd,
1011 MIXER_WINDOW);
1012
1013 /* get dialog item ctrl */
1014 CtrlID = GetDlgCtrlID((HWND)lParam);
1015
1016 /* get line index */
1017 LineOffset = CtrlID / IDC_LINE_SLIDER_VERT;
1018
1019 if (LineOffset == 1 && MixerWindow->Mixer->MixerId == 0)
1020 PlaySound((LPCTSTR)SND_ALIAS_SYSTEMDEFAULT, NULL, SND_ASYNC | SND_ALIAS_ID);
1021 break;
1022
1023 default:
1024 break;
1025 }
1026 break;
1027
1028 case WM_HSCROLL:
1029 switch (LOWORD(wParam))
1030 {
1031 case TB_THUMBTRACK:
1032 /* FIXME */
1033 break;
1034
1035 case TB_ENDTRACK:
1036 MixerWindow = GetWindowData(hwnd,
1037 MIXER_WINDOW);
1038
1039 /* get dialog item ctrl */
1040 CtrlID = GetDlgCtrlID((HWND)lParam);
1041
1042 /* get line index */
1043 LineOffset = CtrlID / IDC_LINE_SLIDER_HORZ;
1044
1045 if (LineOffset == 1 && MixerWindow->Mixer->MixerId == 0)
1046 PlaySound((LPCTSTR)SND_ALIAS_SYSTEMDEFAULT, NULL, SND_ASYNC | SND_ALIAS_ID);
1047 break;
1048
1049 default:
1050 break;
1051 }
1052 break;
1053
1054
1055 case WM_CREATE:
1056 {
1057 MixerWindow = ((LPCREATESTRUCT)lParam)->lpCreateParams;
1058 SetWindowLongPtr(hwnd,
1059 GWL_USERDATA,
1060 (LONG_PTR)MixerWindow);
1061 MixerWindow->hWnd = hwnd;
1062 MixerWindow->Mixer = SndMixerCreate(MixerWindow->hWnd, MixerWindow->MixerId);
1063 if (MixerWindow->Mixer != NULL)
1064 {
1065 TCHAR szProduct[MAXPNAMELEN];
1066
1067 /* get mixer product name */
1068 if (SndMixerGetProductName(MixerWindow->Mixer,
1069 szProduct,
1070 sizeof(szProduct) / sizeof(szProduct[0])) == -1)
1071 {
1072 /* failed to get name */
1073 szProduct[0] = L'\0';
1074 }
1075
1076
1077 /* initialize preferences */
1078 ZeroMemory(&Preferences, sizeof(Preferences));
1079
1080 /* store mixer */
1081 Preferences.Mixer = MixerWindow->Mixer;
1082
1083 /* store mixer window */
1084 Preferences.MixerWindow = MixerWindow;
1085
1086 /* first destination line id */
1087 Preferences.SelectedLine = 0xFFFF0000;
1088
1089 /* copy product */
1090 wcscpy(Preferences.DeviceName, szProduct);
1091
1092 if (!RebuildMixerWindowControls(&Preferences))
1093 {
1094 DPRINT("Rebuilding mixer window controls failed!\n");
1095 SndMixerDestroy(MixerWindow->Mixer);
1096 MixerWindow->Mixer = NULL;
1097 Result = -1;
1098 }
1099
1100 /* create status window */
1101 if (MixerWindow->Mode == NORMAL_MODE)
1102 {
1103 MixerWindow->hStatusBar = CreateStatusWindow(WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS,
1104 NULL,
1105 hwnd,
1106 0);
1107 if (MixerWindow->hStatusBar)
1108 {
1109 SendMessage(MixerWindow->hStatusBar,
1110 WM_SETTEXT,
1111 0,
1112 (LPARAM)szProduct);
1113 }
1114 }
1115
1116 /* Disable the 'Advanced Controls' menu item */
1117 EnableMenuItem(GetMenu(hwnd), IDC_ADVANCED_CONTROLS, MF_BYCOMMAND | MF_GRAYED);
1118 }
1119 break;
1120 }
1121
1122 case WM_DESTROY:
1123 {
1124 MixerWindow = GetWindowData(hwnd,
1125 MIXER_WINDOW);
1126 if (MixerWindow != NULL)
1127 {
1128 if (MixerWindow->Mixer != NULL)
1129 {
1130 SndMixerDestroy(MixerWindow->Mixer);
1131 }
1132 if (MixerWindow->hFont)
1133 DeleteObject(MixerWindow->hFont);
1134 HeapFree(hAppHeap, 0, MixerWindow);
1135 }
1136 break;
1137 }
1138
1139 case WM_CLOSE:
1140 {
1141 PostQuitMessage(0);
1142 break;
1143 }
1144
1145 default:
1146 {
1147 Result = DefWindowProc(hwnd,
1148 uMsg,
1149 wParam,
1150 lParam);
1151 break;
1152 }
1153 }
1154
1155 return Result;
1156 }
1157
1158 static BOOL
1159 RegisterApplicationClasses(VOID)
1160 {
1161 WNDCLASSEX wc;
1162
1163 wc.cbSize = sizeof(WNDCLASSEX);
1164 wc.style = CS_HREDRAW | CS_VREDRAW;
1165 wc.lpfnWndProc = MainWindowProc;
1166 wc.cbClsExtra = 0;
1167 wc.cbWndExtra = sizeof(PMIXER_WINDOW);
1168 wc.hInstance = hAppInstance;
1169 wc.hIcon = LoadIcon(hAppInstance,
1170 MAKEINTRESOURCE(IDI_MAINAPP));
1171 wc.hCursor = LoadCursor(NULL,
1172 IDC_ARROW);
1173 wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1174 wc.lpszMenuName = NULL;
1175 wc.lpszClassName = SZ_APP_CLASS;
1176 wc.hIconSm = NULL;
1177 MainWindowClass = RegisterClassEx(&wc);
1178
1179 return MainWindowClass != 0;
1180 }
1181
1182 static VOID
1183 UnregisterApplicationClasses(VOID)
1184 {
1185 UnregisterClass(SZ_APP_CLASS,
1186 hAppInstance);
1187 }
1188
1189 static HWND
1190 CreateApplicationWindow(
1191 WINDOW_MODE WindowMode,
1192 UINT MixerId)
1193 {
1194 HWND hWnd;
1195
1196 PMIXER_WINDOW MixerWindow = HeapAlloc(hAppHeap,
1197 HEAP_ZERO_MEMORY,
1198 sizeof(MIXER_WINDOW));
1199 if (MixerWindow == NULL)
1200 {
1201 return NULL;
1202 }
1203
1204 MixerWindow->Mode = WindowMode;
1205 MixerWindow->MixerId = MixerId;
1206
1207 if (mixerGetNumDevs() > 0)
1208 {
1209 hWnd = CreateWindowEx(WS_EX_WINDOWEDGE | WS_EX_CONTROLPARENT,
1210 SZ_APP_CLASS,
1211 lpAppTitle,
1212 WS_DLGFRAME | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE,
1213 0, 0, 300, 315,
1214 NULL,
1215 LoadMenu(hAppInstance,
1216 MAKEINTRESOURCE(IDM_MAINMENU)),
1217 hAppInstance,
1218 MixerWindow);
1219 }
1220 else
1221 {
1222 LPTSTR lpErrMessage;
1223
1224 /*
1225 * no mixer devices are available!
1226 */
1227
1228 hWnd = NULL;
1229 if (AllocAndLoadString(&lpErrMessage,
1230 hAppInstance,
1231 IDS_NOMIXERDEVICES))
1232 {
1233 MessageBox(NULL,
1234 lpErrMessage,
1235 lpAppTitle,
1236 MB_ICONINFORMATION);
1237 LocalFree(lpErrMessage);
1238 }
1239 }
1240
1241 if (hWnd == NULL)
1242 {
1243 HeapFree(hAppHeap,
1244 0,
1245 MixerWindow);
1246 }
1247
1248 return hWnd;
1249 }
1250
1251 static
1252 BOOL
1253 HandleCommandLine(LPTSTR cmdline,
1254 PWINDOW_MODE pMode,
1255 PUINT pMixerId)
1256 {
1257 TCHAR option;
1258
1259 *pMixerId = 0;
1260 *pMode = SMALL_MODE;
1261
1262 while (*cmdline == _T(' ') || *cmdline == _T('-') || *cmdline == _T('/'))
1263 {
1264 if (*cmdline++ == _T(' '))
1265 continue;
1266
1267 option = *cmdline;
1268 if (option)
1269 cmdline++;
1270 while (*cmdline == _T(' '))
1271 cmdline++;
1272
1273 switch (option)
1274 {
1275 case 'd': /* Device */
1276 case 'D':
1277 break;
1278
1279 case 'n': /* Normal size */
1280 case 'N':
1281 *pMode = NORMAL_MODE;
1282 break;
1283
1284 case 's': /* Small size */
1285 case 'S':
1286 *pMode = SMALL_MODE;
1287 break;
1288
1289 case 't': /* Tray size */
1290 case 'T':
1291 *pMode = TRAY_MODE;
1292 break;
1293
1294 case 'p': /* Play mode */
1295 case 'P':
1296 *pMixerId = 0;
1297 break;
1298
1299 case 'r': /* Record mode */
1300 case 'R':
1301 *pMixerId = 1;
1302 break;
1303
1304 default:
1305 return FALSE;
1306 }
1307 }
1308
1309 return TRUE;
1310 }
1311
1312 int WINAPI
1313 _tWinMain(HINSTANCE hInstance,
1314 HINSTANCE hPrevInstance,
1315 LPTSTR lpszCmdLine,
1316 int nCmdShow)
1317 {
1318 MSG Msg;
1319 int Ret = 1;
1320 INITCOMMONCONTROLSEX Controls;
1321 WINDOW_MODE WindowMode = SMALL_MODE;
1322 UINT MixerId = 0;
1323
1324 UNREFERENCED_PARAMETER(hPrevInstance);
1325 UNREFERENCED_PARAMETER(nCmdShow);
1326
1327 hAppInstance = hInstance;
1328 hAppHeap = GetProcessHeap();
1329
1330 HandleCommandLine(lpszCmdLine, &WindowMode, &MixerId);
1331
1332 if (InitAppConfig())
1333 {
1334 /* load the application title */
1335 if (!AllocAndLoadString(&lpAppTitle,
1336 hAppInstance,
1337 IDS_SNDVOL32))
1338 {
1339 lpAppTitle = NULL;
1340 }
1341
1342 Controls.dwSize = sizeof(INITCOMMONCONTROLSEX);
1343 Controls.dwICC = ICC_BAR_CLASSES | ICC_STANDARD_CLASSES;
1344
1345 InitCommonControlsEx(&Controls);
1346
1347 if (WindowMode == TRAY_MODE)
1348 {
1349 DialogBoxParam(hAppInstance,
1350 MAKEINTRESOURCE(IDD_TRAY_MASTER),
1351 NULL,
1352 TrayDlgProc,
1353 0);
1354 }
1355 else
1356 {
1357 if (RegisterApplicationClasses())
1358 {
1359 hMainWnd = CreateApplicationWindow(WindowMode, MixerId);
1360 if (hMainWnd != NULL)
1361 {
1362 BOOL bRet;
1363 while ((bRet =GetMessage(&Msg,
1364 NULL,
1365 0,
1366 0)) != 0)
1367 {
1368 if (bRet != -1)
1369 {
1370 TranslateMessage(&Msg);
1371 DispatchMessage(&Msg);
1372 }
1373 }
1374
1375 DestroyWindow(hMainWnd);
1376 Ret = 0;
1377 }
1378 else
1379 {
1380 DPRINT("Failed to create application window (LastError: %d)!\n", GetLastError());
1381 }
1382
1383 UnregisterApplicationClasses();
1384 }
1385 else
1386 {
1387 DPRINT("Failed to register application classes (LastError: %d)!\n", GetLastError());
1388 }
1389 }
1390
1391 if (lpAppTitle != NULL)
1392 {
1393 LocalFree(lpAppTitle);
1394 }
1395
1396 CloseAppConfig();
1397 }
1398 else
1399 {
1400 DPRINT("Unable to open the Volume Control registry key!\n");
1401 }
1402
1403 return Ret;
1404 }