Change variable name from l to k.
[wine] / programs / winecfg / audio.c
1 /*
2  * Audio management UI code
3  *
4  * Copyright 2004 Chris Morgan
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <windef.h>
32 #include <winbase.h>
33 #include <winreg.h>
34 #include <wine/debug.h>
35 #include <shellapi.h>
36 #include <objbase.h>
37 #include <shlguid.h>
38 #include <shlwapi.h>
39 #include <shlobj.h>
40
41 #include "winecfg.h"
42 #include "resource.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
45
46 /* Select the correct entry in the combobox based on drivername */
47 void selectAudioDriver(HWND hDlg, char *drivername)
48 {
49   int i;
50   const AUDIO_DRIVER *pAudioDrv = NULL;
51
52   if ((pAudioDrv = getAudioDrivers()))
53   {
54     for (i = 0; *pAudioDrv->szName; i++, pAudioDrv++)
55     {
56       if (!strcmp (pAudioDrv->szDriver, drivername))
57       {
58         addTransaction("Winmm", "Drivers", ACTION_SET, pAudioDrv->szDriver);
59         SendDlgItemMessage(hDlg, IDC_AUDIO_DRIVER, CB_SETCURSEL,
60                            (WPARAM) i, 0);
61       }
62     }
63   }
64 }
65
66 void
67 initAudioDlg (HWND hDlg)
68 {
69   char *curAudioDriver = getConfigValue("Winmm", "Drivers", "winealsa.drv");
70   const AUDIO_DRIVER *pAudioDrv = NULL;
71   int i;
72
73     if ((pAudioDrv = getAudioDrivers ()))
74     {
75         for (i = 0; *pAudioDrv->szName; i++, pAudioDrv++)
76         {
77             SendDlgItemMessage (hDlg, IDC_AUDIO_DRIVER, CB_ADDSTRING,
78                                 0, (LPARAM) pAudioDrv->szName);
79             if (!strcmp (pAudioDrv->szDriver, curAudioDriver))
80               selectAudioDriver(hDlg, (char*)pAudioDrv->szDriver);
81         }
82     }
83 }
84
85 char *audioAutoDetect(void)
86 {
87   struct stat buf;
88   const char *argv_new[4];
89   int fd;
90
91   char *driversFound[10];
92   char *name[10];
93   int numFound = 0;
94
95   argv_new[0] = "/bin/sh";
96   argv_new[1] = "-c";
97   argv_new[3] = NULL;
98
99   /* try to detect arts */
100   argv_new[2] = "ps awx|grep artsd|grep -v grep|grep artsd > /dev/null";
101   if(!spawnvp(_P_WAIT, "/bin/sh", argv_new))
102   {
103     driversFound[numFound] = "winearts.drv";
104     name[numFound] = "aRts";
105     numFound++;
106   }
107
108   /* try to detect jack */
109   argv_new[2] = "ps awx|grep jackd|grep -v grep|grep jackd > /dev/null";
110   if(!spawnvp(_P_WAIT, "/bin/sh", argv_new))
111   {
112     driversFound[numFound] = "winejack.drv";
113     name[numFound] = "jack";
114     numFound++;
115   }
116
117   /* try to detect nas */
118   /* TODO */
119
120   /* try to detect audioIO (solaris) */
121   /* TODO */
122
123   /* try to detect alsa */
124   if(!stat("/proc/asound", &buf))
125   {
126     driversFound[numFound] = "winealsa.drv";
127     name[numFound] = "Alsa";
128     numFound++;
129   }
130
131   /* try to detect oss */
132   fd = open("/dev/dsp", O_WRONLY | O_NONBLOCK);
133   if(fd)
134   {
135     close(fd);
136     driversFound[numFound] = "wineoss.drv";
137     name[numFound] = "OSS";
138     numFound++;
139   }
140
141
142   if(numFound == 0)
143   {
144     MessageBox(NULL, "Could not detect any audio devices/servers", "Failed", MB_OK);
145     return "";
146   }
147   else
148   {
149     /* TODO: possibly smarter handling of multiple drivers? */
150     char text[128];
151     snprintf(text, sizeof(text), "Found %s", name[0]);
152     MessageBox(NULL, (LPCTSTR)text, "Successful", MB_OK);
153     return driversFound[0];
154   }
155 }
156
157
158 INT_PTR CALLBACK
159 AudioDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
160 {
161   switch (uMsg) {
162       case WM_COMMAND:
163         switch (LOWORD(wParam)) {
164            case IDC_AUDIO_AUTODETECT:
165               selectAudioDriver(hDlg, audioAutoDetect());
166               break;
167            case IDC_AUDIO_DRIVER:
168              if ((HIWORD(wParam) == CBN_SELCHANGE) ||
169                  (HIWORD(wParam) == CBN_SELCHANGE))
170              {
171                 const AUDIO_DRIVER *pAudioDrv = getAudioDrivers();
172                 int selected_driver = SendDlgItemMessage(hDlg, IDC_AUDIO_DRIVER, CB_GETCURSEL, 0, 0);
173                 selectAudioDriver(hDlg, (char*)pAudioDrv[selected_driver].szDriver);
174              }
175              break;
176         }
177         break;
178         
179       case WM_NOTIFY:
180         switch(((LPNMHDR)lParam)->code) {
181             case PSN_KILLACTIVE:
182               SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
183               break;
184             case PSN_APPLY:
185               SetWindowLong(hDlg, DWL_MSGRESULT, PSNRET_NOERROR);
186               break;
187             case PSN_SETACTIVE:
188               break;
189         }
190         break;
191
192   case WM_INITDIALOG:
193     initAudioDlg(hDlg);
194     break;
195   }
196
197   return FALSE;
198 }