More WAVEOUTCAPSW tests.
[wine.git] / windows / driver.c
blobeca8a24f56a5e95d36947bb8c927910d4bf91aa4
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 char szAliasName[128];
45 /* as usual LPWINE_DRIVER == hDriver32 */
46 HDRVR16 hDriver16;
47 HMODULE16 hModule16;
48 DRIVERPROC16 lpDrvProc;
49 DWORD dwDriverID;
50 struct tagWINE_DRIVER* lpPrevItem;
51 struct tagWINE_DRIVER* lpNextItem;
52 } WINE_DRIVER, *LPWINE_DRIVER;
54 static LPWINE_DRIVER lpDrvItemList = NULL;
57 /**************************************************************************
58 * LoadStartupDrivers [internal]
60 static void WINE_UNUSED DRIVER_LoadStartupDrivers(void)
62 char str[256];
64 if (GetPrivateProfileStringA("drivers", NULL, "", str, sizeof(str), "SYSTEM.INI") < 2) {
65 ERR("Can't find drivers section in system.ini\n");
66 } else {
67 HDRVR16 hDrv;
68 LPSTR ptr;
70 for (ptr = str; *ptr; ptr += strlen(ptr) + 1) {
71 TRACE("str='%s'\n", ptr);
72 hDrv = OpenDriver16(ptr, "drivers", 0L);
73 TRACE("hDrv=%04x\n", hDrv);
75 TRACE("end of list !\n");
79 /**************************************************************************
80 * DRIVER_GetNumberOfModuleRefs [internal]
82 * Returns the number of open drivers which share the same module.
84 static WORD DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv)
86 LPWINE_DRIVER lpDrv;
87 WORD count = 0;
89 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
90 if (lpDrv->hModule16 == lpNewDrv->hModule16) {
91 count++;
94 return count;
97 /**************************************************************************
98 * DRIVER_FindFromHDrvr16 [internal]
100 * From a hDrvr being 16 bits, returns the WINE internal structure.
102 static LPWINE_DRIVER DRIVER_FindFromHDrvr16(HDRVR16 hDrvr)
104 LPWINE_DRIVER lpDrv;
106 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
107 if (lpDrv->hDriver16 == hDrvr) {
108 break;
111 return lpDrv;
114 /**************************************************************************
115 * DRIVER_SendMessage [internal]
117 static LRESULT inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT16 msg,
118 LPARAM lParam1, LPARAM lParam2)
120 WORD args[8];
121 LRESULT ret;
123 TRACE("Before CallDriverProc proc=%p driverID=%08lx wMsg=%04x p1=%08lx p2=%08lx\n",
124 lpDrv->lpDrvProc, lpDrv->dwDriverID, msg, lParam1, lParam2);
126 args[7] = HIWORD(lpDrv->dwDriverID);
127 args[6] = LOWORD(lpDrv->dwDriverID);
128 args[5] = lpDrv->hDriver16;
129 args[4] = msg;
130 args[3] = HIWORD(lParam1);
131 args[2] = LOWORD(lParam1);
132 args[1] = HIWORD(lParam2);
133 args[0] = LOWORD(lParam2);
134 WOWCallback16Ex( (DWORD)lpDrv->lpDrvProc, WCB16_PASCAL, sizeof(args), args, &ret );
135 return ret;
138 /**************************************************************************
139 * SendDriverMessage (USER.251)
141 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
142 LPARAM lParam2)
144 LPWINE_DRIVER lpDrv;
145 LRESULT retval = 0;
147 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
149 if ((lpDrv = DRIVER_FindFromHDrvr16(hDriver)) != NULL) {
150 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
151 } else {
152 WARN("Bad driver handle %u\n", hDriver);
155 TRACE("retval = %ld\n", retval);
156 return retval;
159 /**************************************************************************
160 * DRIVER_RemoveFromList [internal]
162 * Generates all the logic to handle driver closure / deletion
163 * Removes a driver struct to the list of open drivers.
165 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
167 lpDrv->dwDriverID = 0;
168 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
169 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
170 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
173 if (lpDrv->lpPrevItem)
174 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
175 else
176 lpDrvItemList = lpDrv->lpNextItem;
177 if (lpDrv->lpNextItem)
178 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
180 return TRUE;
183 /**************************************************************************
184 * DRIVER_AddToList [internal]
186 * Adds a driver struct to the list of open drivers.
187 * Generates all the logic to handle driver creation / open.
189 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
191 /* First driver to be loaded for this module, need to load correctly the module */
192 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
193 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
194 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
195 return FALSE;
197 /* returned value is not checked */
198 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
201 lpNewDrv->lpNextItem = NULL;
202 if (lpDrvItemList == NULL) {
203 lpDrvItemList = lpNewDrv;
204 lpNewDrv->lpPrevItem = NULL;
205 } else {
206 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
207 while (lpDrv->lpNextItem != NULL)
208 lpDrv = lpDrv->lpNextItem;
210 lpDrv->lpNextItem = lpNewDrv;
211 lpNewDrv->lpPrevItem = lpDrv;
213 /* Now just open a new instance of a driver on this module */
214 lpNewDrv->dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
216 if (lpNewDrv->dwDriverID == 0) {
217 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
218 DRIVER_RemoveFromList(lpNewDrv);
219 return FALSE;
222 return TRUE;
225 /**************************************************************************
226 * DRIVER_TryOpenDriver16 [internal]
228 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
230 static LPWINE_DRIVER DRIVER_TryOpenDriver16(LPCSTR lpFileName, LPARAM lParam2)
232 static WORD DRIVER_hDrvr16Counter /* = 0 */;
233 LPWINE_DRIVER lpDrv = NULL;
234 HMODULE16 hModule;
235 DRIVERPROC16 lpProc;
236 LPCSTR lpSFN;
237 LPSTR ptr;
239 TRACE("('%s', %08lX);\n", lpFileName, lParam2);
241 if (strlen(lpFileName) < 1) return lpDrv;
243 if ((lpSFN = strrchr(lpFileName, '\\')) == NULL)
244 lpSFN = lpFileName;
245 else
246 lpSFN++;
247 if ((ptr = strchr(lpFileName, ' ')) != NULL) {
248 *ptr++ = '\0';
249 while (*ptr == ' ') ptr++;
250 if (*ptr == '\0') ptr = NULL;
253 if ((hModule = LoadLibrary16(lpFileName)) < 32) goto exit;
254 if ((lpProc = (DRIVERPROC16)GetProcAddress16(hModule, "DRIVERPROC")) == NULL)
255 goto exit;
257 if ((lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER))) == NULL)
258 goto exit;
260 lpDrv->dwDriverID = 0;
261 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
262 lpDrv->hDriver16 = DRIVER_hDrvr16Counter;
263 lstrcpynA(lpDrv->szAliasName, lpSFN, sizeof(lpDrv->szAliasName));
264 lpDrv->hModule16 = hModule;
265 lpDrv->lpDrvProc = lpProc;
267 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) goto exit;
269 return lpDrv;
270 exit:
271 TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName, hModule);
272 if (hModule >= 32) FreeLibrary16(hModule);
273 HeapFree(GetProcessHeap(), 0, lpDrv);
274 return NULL;
277 /**************************************************************************
278 * OpenDriver (USER.252)
280 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
282 LPWINE_DRIVER lpDrv = NULL;
283 char drvName[128];
285 TRACE("(%s, %s, %08lX);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
287 if (!lpDriverName || !*lpDriverName) return 0;
289 if (lpSectionName == NULL) {
290 strcpy(drvName, lpDriverName);
292 if ((lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2)))
293 goto the_end;
294 /* in case hDriver is NULL, search in Drivers section */
295 lpSectionName = "Drivers";
297 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "",
298 drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
299 lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2);
301 if (!lpDrv) {
302 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName));
303 return 0;
305 the_end:
306 TRACE("=> %04x / %08lx\n", lpDrv->hDriver16, (DWORD)lpDrv);
307 return lpDrv->hDriver16;
310 /**************************************************************************
311 * CloseDriver (USER.253)
313 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
315 LPWINE_DRIVER lpDrv;
317 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
319 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
320 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
322 if (DRIVER_RemoveFromList(lpDrv)) {
323 HeapFree(GetProcessHeap(), 0, lpDrv);
324 return TRUE;
327 WARN("Failed to close driver\n");
328 return FALSE;
331 /**************************************************************************
332 * GetDriverModuleHandle (USER.254)
334 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
336 LPWINE_DRIVER lpDrv;
337 HMODULE16 hModule = 0;
339 TRACE("(%04x);\n", hDrvr);
341 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
342 hModule = lpDrv->hModule16;
344 TRACE("=> %04x\n", hModule);
345 return hModule;
348 /**************************************************************************
349 * DefDriverProc (USER.255)
351 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
352 LPARAM lParam1, LPARAM lParam2)
354 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
355 dwDevID, hDriv, wMsg, lParam1, lParam2);
357 switch(wMsg) {
358 case DRV_LOAD:
359 case DRV_FREE:
360 case DRV_ENABLE:
361 case DRV_DISABLE:
362 return (LRESULT)1L;
363 case DRV_OPEN:
364 case DRV_CLOSE:
365 case DRV_QUERYCONFIGURE:
366 return (LRESULT)0L;
367 case DRV_CONFIGURE:
368 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
369 return (LRESULT)0L;
370 case DRV_INSTALL:
371 case DRV_REMOVE:
372 return DRV_SUCCESS;
373 default:
374 return (LRESULT)0L;
378 /**************************************************************************
379 * GetDriverInfo (USER.256)
381 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
383 LPWINE_DRIVER lpDrv;
384 BOOL16 ret = FALSE;
386 TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
388 if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16))
389 return FALSE;
391 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
392 lpDrvInfo->hDriver = lpDrv->hDriver16;
393 lpDrvInfo->hModule = lpDrv->hModule16;
394 lstrcpynA(lpDrvInfo->szAliasName, lpDrv->szAliasName, sizeof(lpDrvInfo->szAliasName));
395 ret = TRUE;
398 return ret;
401 /**************************************************************************
402 * GetNextDriver (USER.257)
404 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
406 HDRVR16 hRetDrv = 0;
407 LPWINE_DRIVER lpDrv;
409 TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
411 if (hDrvr == 0) {
412 if (lpDrvItemList == NULL) {
413 FIXME("drivers list empty !\n");
414 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
415 * I (EPP) don't quite understand this
417 if (lpDrvItemList == NULL)
418 return 0;
420 lpDrv = lpDrvItemList;
421 if (dwFlags & GND_REVERSE) {
422 while (lpDrv->lpNextItem)
423 lpDrv = lpDrv->lpNextItem;
425 } else {
426 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
427 if (dwFlags & GND_REVERSE) {
428 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
429 } else {
430 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
435 hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
436 TRACE("return %04x !\n", hRetDrv);
437 return hRetDrv;