Implement LBS_COMBOBOX, and make use of it.
[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         set("Winmm", "Drivers", (char *) pAudioDrv->szDriver);
59         SendMessage(GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0); /* enable apply button */
60         SendDlgItemMessage(hDlg, IDC_AUDIO_DRIVER, CB_SETCURSEL,
61                            (WPARAM) i, 0);
62       }
63     }
64   }
65 }
66
67 void
68 initAudioDlg (HWND hDlg)
69 {
70   char *curAudioDriver = get("Winmm", "Drivers", "winealsa.drv");
71   const AUDIO_DRIVER *pAudioDrv = NULL;
72   int i;
73
74     if ((pAudioDrv = getAudioDrivers ()))
75     {
76         for (i = 0; *pAudioDrv->szName; i++, pAudioDrv++)
77         {
78             SendDlgItemMessage (hDlg, IDC_AUDIO_DRIVER, CB_ADDSTRING,
79                                 0, (LPARAM) pAudioDrv->szName);
80             if (!strcmp (pAudioDrv->szDriver, curAudioDriver))
81               selectAudioDriver(hDlg, (char*)pAudioDrv->szDriver);
82         }
83     }
84 }
85
86 char *audioAutoDetect(void)
87 {
88   struct stat buf;
89   const char *argv_new[4];
90   int fd;
91
92   char *driversFound[10];
93   char *name[10];
94   int numFound = 0;
95
96   argv_new[0] = "/bin/sh";
97   argv_new[1] = "-c";
98   argv_new[3] = NULL;
99
100   /* try to detect arts */
101   argv_new[2] = "ps awx|grep artsd|grep -v grep|grep artsd > /dev/null";
102   if(!spawnvp(_P_WAIT, "/bin/sh", argv_new))
103   {
104     driversFound[numFound] = "winearts.drv";
105     name[numFound] = "aRts";
106     numFound++;
107   }
108
109   /* try to detect jack */
110   argv_new[2] = "ps awx|grep jackd|grep -v grep|grep jackd > /dev/null";
111   if(!spawnvp(_P_WAIT, "/bin/sh", argv_new))
112   {
113     driversFound[numFound] = "winejack.drv";
114     name[numFound] = "jack";
115     numFound++;
116   }
117
118   /* try to detect nas */
119   /* TODO */
120
121   /* try to detect audioIO (solaris) */
122   /* TODO */
123
124   /* try to detect alsa */
125   if(!stat("/proc/asound", &buf))
126   {
127     driversFound[numFound] = "winealsa.drv";
128     name[numFound] = "Alsa";
129     numFound++;
130   }
131
132   /* try to detect oss */
133   fd = open("/dev/dsp", O_WRONLY | O_NONBLOCK);
134   if(fd)
135   {
136     close(fd);
137     driversFound[numFound] = "wineoss.drv";
138     name[numFound] = "OSS";
139     numFound++;
140   }
141
142
143   if(numFound == 0)
144   {
145     MessageBox(NULL, "Could not detect any audio devices/servers", "Failed", MB_OK);
146     return "";
147   }
148   else
149   {
150     /* TODO: possibly smarter handling of multiple drivers? */
151     char text[128];
152     snprintf(text, sizeof(text), "Found %s", name[0]);
153     MessageBox(NULL, (LPCTSTR)text, "Successful", MB_OK);
154     return driversFound[0];
155   }
156 }
157
158
159 INT_PTR CALLBACK
160 AudioDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
161 {
162   switch (uMsg) {
163       case WM_COMMAND:
164         switch (LOWORD(wParam)) {
165            case IDC_AUDIO_AUTODETECT:
166               selectAudioDriver(hDlg, audioAutoDetect());
167               break;
168            case IDC_AUDIO_DRIVER:
169              if ((HIWORD(wParam) == CBN_SELCHANGE) ||
170                  (HIWORD(wParam) == CBN_SELCHANGE))
171              {
172                 const AUDIO_DRIVER *pAudioDrv = getAudioDrivers();
173                 int selected_driver = SendDlgItemMessage(hDlg, IDC_AUDIO_DRIVER, CB_GETCURSEL, 0, 0);
174                 selectAudioDriver(hDlg, (char*)pAudioDrv[selected_driver].szDriver);
175              }
176              break;
177         }
178         break;
179
180       case WM_SHOWWINDOW:
181         set_window_title(hDlg);
182         break;
183         
184       case WM_NOTIFY:
185         switch(((LPNMHDR)lParam)->code) {
186             case PSN_KILLACTIVE:
187               SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
188               break;
189             case PSN_APPLY:
190               apply();
191               SetWindowLong(hDlg, DWL_MSGRESULT, PSNRET_NOERROR);
192               break;
193             case PSN_SETACTIVE:
194               break;
195         }
196         break;
197
198   case WM_INITDIALOG:
199     initAudioDlg(hDlg);
200     break;
201   }
202
203   return FALSE;
204 }