Remove unneeded casts.
[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 #include <mmsystem.h>
41
42 #include "winecfg.h"
43 #include "resource.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
46
47 /* Select the correct entry in the combobox based on drivername */
48 static void selectAudioDriver(HWND hDlg, const char *drivername)
49 {
50   int i;
51   const AUDIO_DRIVER *pAudioDrv = NULL;
52
53   if ((pAudioDrv = getAudioDrivers()))
54   {
55     for (i = 0; *pAudioDrv->szName; i++, pAudioDrv++)
56     {
57       if (!strcmp (pAudioDrv->szDriver, drivername))
58       {
59         set_reg_key(config_key, "Drivers", "Audio", (char *) pAudioDrv->szDriver);
60         SendMessage(GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0); /* enable apply button */
61         SendDlgItemMessage(hDlg, IDC_AUDIO_DRIVER, CB_SETCURSEL,
62                            (WPARAM) i, 0);
63       }
64     }
65   }
66 }
67
68 static void configureAudioDriver(HWND hDlg, const char *drivername)
69 {
70   int i;
71   const AUDIO_DRIVER *pAudioDrv = NULL;
72
73   if ((pAudioDrv = getAudioDrivers()))
74   {
75     for (i = 0; *pAudioDrv->szName; i++, pAudioDrv++)
76     {
77       if (!strcmp (pAudioDrv->szDriver, drivername))
78       {
79         if (strlen(pAudioDrv->szDriver) != 0)
80         {
81           HDRVR hdrvr;
82           char wine_driver[MAX_NAME_LENGTH + 8];
83           sprintf(wine_driver, "wine%s.drv", pAudioDrv->szDriver);
84           hdrvr = OpenDriverA(wine_driver, 0, 0);
85           if (hdrvr != 0)
86           {
87             if (SendDriverMessage(hdrvr, DRV_QUERYCONFIGURE, 0, 0) != 0)
88             {
89               DRVCONFIGINFO dci;
90               LONG lRes;
91               dci.dwDCISize = sizeof (dci);
92               dci.lpszDCISectionName = NULL;
93               dci.lpszDCIAliasName = NULL;
94               lRes = SendDriverMessage(hdrvr, DRV_CONFIGURE, 0, (LONG)&dci);
95             }
96             CloseDriver(hdrvr, 0, 0);
97           }
98           else
99           {
100             char str[1024];
101             sprintf(str, "Couldn't open %s!", wine_driver);
102             MessageBox(NULL, str, "Fixme", MB_OK | MB_ICONERROR);
103           }
104         }
105         break;
106       }
107     }
108   }
109 }
110
111 static void initAudioDlg (HWND hDlg)
112 {
113     char *curAudioDriver = get_reg_key(config_key, "Drivers", "Audio", "alsa");
114     const AUDIO_DRIVER *pAudioDrv = NULL;
115     int i;
116
117     WINE_TRACE("\n");
118
119     pAudioDrv = getAudioDrivers ();
120     for (i = 0; *pAudioDrv->szName; i++, pAudioDrv++) {
121         SendDlgItemMessage (hDlg, IDC_AUDIO_DRIVER, CB_ADDSTRING, 
122                 0, (LPARAM) pAudioDrv->szName);
123         if (!strcmp (pAudioDrv->szDriver, curAudioDriver)) {
124             SendDlgItemMessage(hDlg, IDC_AUDIO_DRIVER, CB_SETCURSEL, i, 0);
125         }
126     }
127 }
128
129 static const char *audioAutoDetect(void)
130 {
131   struct stat buf;
132   const char *argv_new[4];
133   int fd;
134
135   const char *driversFound[10];
136   const char *name[10];
137   int numFound = 0;
138
139   argv_new[0] = "/bin/sh";
140   argv_new[1] = "-c";
141   argv_new[3] = NULL;
142
143   /* try to detect oss */
144   fd = open("/dev/dsp", O_WRONLY | O_NONBLOCK);
145   if(fd)
146   {
147     close(fd);
148     driversFound[numFound] = "oss";
149     name[numFound] = "OSS";
150     numFound++;
151   }
152   
153     /* try to detect alsa */
154   if(!stat("/proc/asound", &buf))
155   {
156     driversFound[numFound] = "alsa";
157     name[numFound] = "ALSA";
158     numFound++;
159   }
160
161   /* try to detect arts */
162   argv_new[2] = "ps awx|grep artsd|grep -v grep|grep artsd > /dev/null";
163   if(!spawnvp(_P_WAIT, "/bin/sh", argv_new))
164   {
165     driversFound[numFound] = "arts";
166     name[numFound] = "aRts";
167     numFound++;
168   }
169
170   /* try to detect jack */
171   argv_new[2] = "ps awx|grep jackd|grep -v grep|grep jackd > /dev/null";
172   if(!spawnvp(_P_WAIT, "/bin/sh", argv_new))
173   {
174     driversFound[numFound] = "jack";
175     name[numFound] = "JACK";
176     numFound++;
177   }
178
179   /* try to detect nas */
180   /* TODO */
181
182   /* try to detect audioIO (solaris) */
183   /* TODO */
184
185   if(numFound == 0)
186   {
187     MessageBox(NULL, "Could not detect any audio devices/servers", "Failed", MB_OK);
188     return "";
189   }
190   else
191   {
192     /* TODO: possibly smarter handling of multiple drivers? */
193     char text[128];
194     snprintf(text, sizeof(text), "Found %s", name[0]);
195     MessageBox(NULL, (LPCTSTR)text, "Successful", MB_OK);
196     return driversFound[0];
197   }
198 }
199
200
201 INT_PTR CALLBACK
202 AudioDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
203 {
204   switch (uMsg) {
205       case WM_COMMAND:
206         switch (LOWORD(wParam)) {
207            case IDC_AUDIO_AUTODETECT:
208               selectAudioDriver(hDlg, audioAutoDetect());
209               break;
210            case IDC_AUDIO_DRIVER:
211              if ((HIWORD(wParam) == CBN_SELCHANGE) ||
212                  (HIWORD(wParam) == CBN_SELCHANGE))
213              {
214                 const AUDIO_DRIVER *pAudioDrv = getAudioDrivers();
215                 int selected_driver = SendDlgItemMessage(hDlg, IDC_AUDIO_DRIVER, CB_GETCURSEL, 0, 0);
216                 selectAudioDriver(hDlg, (char*)pAudioDrv[selected_driver].szDriver);
217              }
218              break;
219           case IDC_AUDIO_CONFIGURE:
220              {
221                 const AUDIO_DRIVER *pAudioDrv = getAudioDrivers();
222                 int selected_driver = SendDlgItemMessage(hDlg, IDC_AUDIO_DRIVER, CB_GETCURSEL, 0, 0);
223                 configureAudioDriver(hDlg, (char*)pAudioDrv[selected_driver].szDriver);
224              }
225              break;
226           case IDC_AUDIO_CONTROL_PANEL:
227              MessageBox(NULL, "Launching audio control panel not implemented yet!", "Fixme", MB_OK | MB_ICONERROR);
228              break;
229         }
230         break;
231
232       case WM_SHOWWINDOW:
233         set_window_title(hDlg);
234         break;
235         
236       case WM_NOTIFY:
237         switch(((LPNMHDR)lParam)->code) {
238             case PSN_KILLACTIVE:
239               SetWindowLongPtr(hDlg, DWLP_MSGRESULT, FALSE);
240               break;
241             case PSN_APPLY:
242               apply();
243               SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
244               break;
245             case PSN_SETACTIVE:
246               break;
247         }
248         break;
249
250   case WM_INITDIALOG:
251     initAudioDlg(hDlg);
252     break;
253   }
254
255   return FALSE;
256 }