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(LPCSTR lpFileName
, LPARAM lParam2
)
208 static WORD DRIVER_hDrvr16Counter
/* = 0 */;
209 LPWINE_DRIVER lpDrv
= NULL
;
215 TRACE("('%s', %08lX);\n", lpFileName
, lParam2
);
217 if (strlen(lpFileName
) < 1) return lpDrv
;
219 if ((lpSFN
= strrchr(lpFileName
, '\\')) == NULL
)
223 if ((ptr
= strchr(lpFileName
, ' ')) != NULL
) {
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
)
233 if ((lpDrv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER
))) == NULL
)
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
;
246 TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName
, hModule
);
247 if (hModule
>= 32) FreeLibrary16(hModule
);
248 HeapFree(GetProcessHeap(), 0, lpDrv
);
252 /**************************************************************************
253 * DrvOpen (MMSYSTEM.1100)
255 HDRVR16 WINAPI
DrvOpen16(LPCSTR lpDriverName
, LPCSTR lpSectionName
, LPARAM lParam2
)
257 LPWINE_DRIVER lpDrv
= NULL
;
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
)))
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
);
277 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName
), debugstr_a(lpSectionName
));
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
)
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
);
302 WARN("Failed to close driver\n");
306 /**************************************************************************
307 * DrvGetModuleHandle (MMSYSTEM.1103)
309 HMODULE16 WINAPI
DrvGetModuleHandle16(HDRVR16 hDrvr
)
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
);
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
);
340 case DRV_QUERYCONFIGURE
:
343 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK
);
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
);