Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / mmsystem.dll16 / driver.c
blobd3d98827f1cf385c92473c09f85c3bacc2a99e59
1 /*
2 * WINE Drivers functions
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1998 Marcus Meissner
6 * Copyright 1999,2000 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * TODO:
23 * - LoadModule count and clean up is not handled correctly (it's not a
24 * problem as long as FreeLibrary is not working correctly)
27 #include <stdarg.h>
28 #include <string.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wine/winbase16.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "wownt32.h"
36 #include "mmddk.h"
37 #include "wine/mmsystem16.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(driver);
42 typedef struct tagWINE_DRIVER
44 /* as usual LPWINE_DRIVER == hDriver32 */
45 HDRVR16 hDriver16;
46 HMODULE16 hModule16;
47 DRIVERPROC16 lpDrvProc;
48 DWORD dwDriverID;
49 struct tagWINE_DRIVER* lpPrevItem;
50 struct tagWINE_DRIVER* lpNextItem;
51 } WINE_DRIVER, *LPWINE_DRIVER;
53 static LPWINE_DRIVER lpDrvItemList = NULL;
56 /**************************************************************************
57 * DRIVER_GetNumberOfModuleRefs [internal]
59 * Returns the number of open drivers which share the same module.
61 static WORD DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv)
63 LPWINE_DRIVER lpDrv;
64 WORD count = 0;
66 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
67 if (lpDrv->hModule16 == lpNewDrv->hModule16) {
68 count++;
71 return count;
74 /**************************************************************************
75 * DRIVER_FindFromHDrvr16 [internal]
77 * From a hDrvr being 16 bits, returns the WINE internal structure.
79 static LPWINE_DRIVER DRIVER_FindFromHDrvr16(HDRVR16 hDrvr)
81 LPWINE_DRIVER lpDrv;
83 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
84 if (lpDrv->hDriver16 == hDrvr) {
85 break;
88 return lpDrv;
91 /**************************************************************************
92 * DRIVER_SendMessage [internal]
94 static inline LRESULT DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT16 msg,
95 LPARAM lParam1, LPARAM lParam2)
97 WORD args[8];
98 DWORD ret;
100 TRACE("Before CallDriverProc proc=%p driverID=%08x wMsg=%04x p1=%08lx p2=%08lx\n",
101 lpDrv->lpDrvProc, lpDrv->dwDriverID, msg, lParam1, lParam2);
103 args[7] = HIWORD(lpDrv->dwDriverID);
104 args[6] = LOWORD(lpDrv->dwDriverID);
105 args[5] = lpDrv->hDriver16;
106 args[4] = msg;
107 args[3] = HIWORD(lParam1);
108 args[2] = LOWORD(lParam1);
109 args[1] = HIWORD(lParam2);
110 args[0] = LOWORD(lParam2);
111 WOWCallback16Ex( (DWORD)lpDrv->lpDrvProc, WCB16_PASCAL, sizeof(args), args, &ret );
112 return ret;
115 /**************************************************************************
116 * DrvSendMessage (MMSYSTEM.1102)
118 LRESULT WINAPI DrvSendMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1, LPARAM lParam2)
120 LPWINE_DRIVER lpDrv;
121 LRESULT retval = 0;
123 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
125 if ((lpDrv = DRIVER_FindFromHDrvr16(hDriver)) != NULL) {
126 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
127 } else {
128 WARN("Bad driver handle %u\n", hDriver);
131 TRACE("retval = %ld\n", retval);
132 return retval;
135 /**************************************************************************
136 * DRIVER_RemoveFromList [internal]
138 * Generates all the logic to handle driver closure / deletion
139 * Removes a driver struct to the list of open drivers.
141 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
143 lpDrv->dwDriverID = 0;
144 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
145 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
146 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
149 if (lpDrv->lpPrevItem)
150 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
151 else
152 lpDrvItemList = lpDrv->lpNextItem;
153 if (lpDrv->lpNextItem)
154 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
156 return TRUE;
159 /**************************************************************************
160 * DRIVER_AddToList [internal]
162 * Adds a driver struct to the list of open drivers.
163 * Generates all the logic to handle driver creation / open.
165 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
167 /* First driver to be loaded for this module, need to load correctly the module */
168 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
169 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
170 TRACE("DRV_LOAD failed on driver %p\n", lpNewDrv);
171 return FALSE;
173 /* returned value is not checked */
174 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
177 lpNewDrv->lpNextItem = NULL;
178 if (lpDrvItemList == NULL) {
179 lpDrvItemList = lpNewDrv;
180 lpNewDrv->lpPrevItem = NULL;
181 } else {
182 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
183 while (lpDrv->lpNextItem != NULL)
184 lpDrv = lpDrv->lpNextItem;
186 lpDrv->lpNextItem = lpNewDrv;
187 lpNewDrv->lpPrevItem = lpDrv;
189 /* Now just open a new instance of a driver on this module */
190 lpNewDrv->dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
192 if (lpNewDrv->dwDriverID == 0) {
193 TRACE("DRV_OPEN failed on driver %p\n", lpNewDrv);
194 DRIVER_RemoveFromList(lpNewDrv);
195 return FALSE;
198 return TRUE;
201 /**************************************************************************
202 * DRIVER_TryOpenDriver16 [internal]
204 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
206 static LPWINE_DRIVER DRIVER_TryOpenDriver16(LPCSTR lpFileName, LPARAM lParam2)
208 static WORD DRIVER_hDrvr16Counter /* = 0 */;
209 LPWINE_DRIVER lpDrv = NULL;
210 HMODULE16 hModule;
211 DRIVERPROC16 lpProc;
212 LPCSTR lpSFN;
213 LPSTR ptr;
215 TRACE("('%s', %08lX);\n", lpFileName, lParam2);
217 if (strlen(lpFileName) < 1) return lpDrv;
219 if ((lpSFN = strrchr(lpFileName, '\\')) == NULL)
220 lpSFN = lpFileName;
221 else
222 lpSFN++;
223 if ((ptr = strchr(lpFileName, ' ')) != NULL) {
224 *ptr++ = '\0';
225 while (*ptr == ' ') ptr++;
226 if (*ptr == '\0') ptr = NULL;
229 if ((hModule = LoadLibrary16(lpFileName)) < 32) goto exit;
230 if ((lpProc = (DRIVERPROC16)GetProcAddress16(hModule, "DRIVERPROC")) == NULL)
231 goto exit;
233 if ((lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER))) == NULL)
234 goto exit;
236 lpDrv->dwDriverID = 0;
237 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
238 lpDrv->hDriver16 = DRIVER_hDrvr16Counter;
239 lpDrv->hModule16 = hModule;
240 lpDrv->lpDrvProc = lpProc;
242 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) goto exit;
244 return lpDrv;
245 exit:
246 TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName, hModule);
247 if (hModule >= 32) FreeLibrary16(hModule);
248 HeapFree(GetProcessHeap(), 0, lpDrv);
249 return NULL;
252 /**************************************************************************
253 * DrvOpen (MMSYSTEM.1100)
255 HDRVR16 WINAPI DrvOpen16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
257 LPWINE_DRIVER lpDrv = NULL;
258 char drvName[128];
260 TRACE("(%s, %s, %08lX);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
262 if (!lpDriverName || !*lpDriverName) return 0;
264 if (lpSectionName == NULL) {
265 strcpy(drvName, lpDriverName);
267 if ((lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2)))
268 goto the_end;
269 /* in case hDriver is NULL, search in Drivers section */
270 lpSectionName = "Drivers";
272 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "",
273 drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
274 lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2);
276 if (!lpDrv) {
277 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName));
278 return 0;
280 the_end:
281 TRACE("=> %04x / %p\n", lpDrv->hDriver16, lpDrv);
282 return lpDrv->hDriver16;
285 /**************************************************************************
286 * DrvClose16 (MMSYSTEM.1101)
288 LRESULT WINAPI DrvClose16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
290 LPWINE_DRIVER lpDrv;
292 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
294 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
295 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
297 if (DRIVER_RemoveFromList(lpDrv)) {
298 HeapFree(GetProcessHeap(), 0, lpDrv);
299 return TRUE;
302 WARN("Failed to close driver\n");
303 return FALSE;
306 /**************************************************************************
307 * DrvGetModuleHandle (MMSYSTEM.1103)
309 HMODULE16 WINAPI DrvGetModuleHandle16(HDRVR16 hDrvr)
311 LPWINE_DRIVER lpDrv;
312 HMODULE16 hModule = 0;
314 TRACE("(%04x);\n", hDrvr);
316 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
317 hModule = lpDrv->hModule16;
319 TRACE("=> %04x\n", hModule);
320 return hModule;
323 /**************************************************************************
324 * DrvDefDriverProc (MMSYSTEM.1104)
326 LRESULT WINAPI DrvDefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
327 LPARAM lParam1, LPARAM lParam2)
329 TRACE("devID=0x%08x hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
330 dwDevID, hDriv, wMsg, lParam1, lParam2);
332 switch(wMsg) {
333 case DRV_LOAD:
334 case DRV_FREE:
335 case DRV_ENABLE:
336 case DRV_DISABLE:
337 return (LRESULT)1L;
338 case DRV_OPEN:
339 case DRV_CLOSE:
340 case DRV_QUERYCONFIGURE:
341 return (LRESULT)0L;
342 case DRV_CONFIGURE:
343 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
344 return (LRESULT)0L;
345 case DRV_INSTALL:
346 case DRV_REMOVE:
347 return DRV_SUCCESS;
348 default:
349 return (LRESULT)0L;
353 /**************************************************************************
354 * DriverProc (MMSYSTEM.6)
356 LRESULT WINAPI DriverProc16(DWORD dwDevID, HDRVR16 hDrv, WORD wMsg,
357 DWORD dwParam1, DWORD dwParam2)
359 return DrvDefDriverProc16(dwDevID, hDrv, wMsg, dwParam1, dwParam2);