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
23 * - LoadModule count and clean up is not handled correctly (it's not a
24 * problem as long as FreeLibrary is not working correctly)
32 #include "wine/winbase16.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 */
47 DRIVERPROC16 lpDrvProc;
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)
66 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
67 if (lpDrv->hModule16 == lpNewDrv->hModule16) {
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)
83 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
84 if (lpDrv->hDriver16 == hDrvr) {
91 /**************************************************************************
92 * DRIVER_SendMessage [internal]
94 static inline LRESULT DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT16 msg,
95 LPARAM lParam1, LPARAM lParam2)
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;
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 );
115 /**************************************************************************
116 * DrvSendMessage (MMSYSTEM.1102)
118 LRESULT WINAPI DrvSendMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1, LPARAM lParam2)
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);
128 WARN("Bad driver handle %u\n", hDriver);
131 TRACE("retval = %ld\n", 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;
152 lpDrvItemList = lpDrv->lpNextItem;
153 if (lpDrv->lpNextItem)
154 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
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);
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;
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);
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(LPSTR lpFileName, LPARAM lParam2)
208 static WORD DRIVER_hDrvr16Counter /* = 0 */;
209 LPWINE_DRIVER lpDrv = NULL;
214 TRACE("('%s', %08lX);\n", lpFileName, lParam2);
216 if (strlen(lpFileName) < 1) return lpDrv;
218 if ((ptr = strchr(lpFileName, ' ')) != NULL) {
220 while (*ptr == ' ') ptr++;
221 if (*ptr == '\0') ptr = NULL;
224 if ((hModule = LoadLibrary16(lpFileName)) < 32) goto exit;
225 if ((lpProc = (DRIVERPROC16)GetProcAddress16(hModule, "DRIVERPROC")) == NULL)
228 if ((lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER))) == NULL)
231 lpDrv->dwDriverID = 0;
232 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
233 lpDrv->hDriver16 = DRIVER_hDrvr16Counter;
234 lpDrv->hModule16 = hModule;
235 lpDrv->lpDrvProc = lpProc;
237 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) goto exit;
241 TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName, hModule);
242 if (hModule >= 32) FreeLibrary16(hModule);
243 HeapFree(GetProcessHeap(), 0, lpDrv);
247 /**************************************************************************
248 * DrvOpen (MMSYSTEM.1100)
250 HDRVR16 WINAPI DrvOpen16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
252 LPWINE_DRIVER lpDrv = NULL;
255 TRACE("(%s, %s, %08lX);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
257 if (!lpDriverName || !*lpDriverName) return 0;
259 if (lpSectionName == NULL) {
260 strcpy(drvName, lpDriverName);
262 if ((lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2)))
264 /* in case hDriver is NULL, search in Drivers section */
265 lpSectionName = "Drivers";
267 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "",
268 drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
269 lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2);
272 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName));
276 TRACE("=> %04x / %p\n", lpDrv->hDriver16, lpDrv);
277 return lpDrv->hDriver16;
280 /**************************************************************************
281 * DrvClose16 (MMSYSTEM.1101)
283 LRESULT WINAPI DrvClose16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
287 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
289 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
290 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
292 if (DRIVER_RemoveFromList(lpDrv)) {
293 HeapFree(GetProcessHeap(), 0, lpDrv);
297 WARN("Failed to close driver\n");
301 /**************************************************************************
302 * DrvGetModuleHandle (MMSYSTEM.1103)
304 HMODULE16 WINAPI DrvGetModuleHandle16(HDRVR16 hDrvr)
307 HMODULE16 hModule = 0;
309 TRACE("(%04x);\n", hDrvr);
311 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
312 hModule = lpDrv->hModule16;
314 TRACE("=> %04x\n", hModule);
318 /**************************************************************************
319 * DrvDefDriverProc (MMSYSTEM.1104)
321 LRESULT WINAPI DrvDefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
322 LPARAM lParam1, LPARAM lParam2)
324 TRACE("devID=0x%08x hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
325 dwDevID, hDriv, wMsg, lParam1, lParam2);
335 case DRV_QUERYCONFIGURE:
338 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
348 /**************************************************************************
349 * DriverProc (MMSYSTEM.6)
351 LRESULT WINAPI DriverProc16(DWORD dwDevID, HDRVR16 hDrv, WORD wMsg,
352 DWORD dwParam1, DWORD dwParam2)
354 return DrvDefDriverProc16(dwDevID, hDrv, wMsg, dwParam1, dwParam2);