Fix spec for InitiateSystemShutdownExA, as pointed out by Stefan
[wine/multimedia.git] / windows / driver.c
blob1cd7abba4abac9222d5795d58140a7fb4a46553d
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * WINE Drivers functions
6 * Copyright 1994 Martin Ayotte
7 * Copyright 1998 Marcus Meissner
8 * Copyright 1999,2000 Eric Pouech
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <string.h>
27 #include "windef.h"
28 #include "wine/winbase16.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "wownt32.h"
32 #include "mmddk.h"
33 #include "wine/mmsystem16.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(driver);
38 typedef struct tagWINE_DRIVER
40 char szAliasName[128];
41 /* as usual LPWINE_DRIVER == hDriver32 */
42 HDRVR16 hDriver16;
43 HMODULE16 hModule16;
44 DRIVERPROC16 lpDrvProc;
45 DWORD dwDriverID;
46 struct tagWINE_DRIVER* lpPrevItem;
47 struct tagWINE_DRIVER* lpNextItem;
48 } WINE_DRIVER, *LPWINE_DRIVER;
50 static LPWINE_DRIVER lpDrvItemList = NULL;
52 /* TODO list :
53 * - LoadModule count and clean up is not handled correctly (it's not a
54 * problem as long as FreeLibrary is not working correctly)
58 /**************************************************************************
59 * LoadStartupDrivers [internal]
61 static void WINE_UNUSED DRIVER_LoadStartupDrivers(void)
63 char str[256];
65 if (GetPrivateProfileStringA("drivers", NULL, "", str, sizeof(str), "SYSTEM.INI") < 2) {
66 ERR("Can't find drivers section in system.ini\n");
67 } else {
68 HDRVR16 hDrv;
69 LPSTR ptr;
71 for (ptr = str; *ptr; ptr += strlen(ptr) + 1) {
72 TRACE("str='%s'\n", ptr);
73 hDrv = OpenDriver16(ptr, "drivers", 0L);
74 TRACE("hDrv=%04x\n", hDrv);
76 TRACE("end of list !\n");
80 /**************************************************************************
81 * DRIVER_GetNumberOfModuleRefs [internal]
83 * Returns the number of open drivers which share the same module.
85 static WORD DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv)
87 LPWINE_DRIVER lpDrv;
88 WORD count = 0;
90 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
91 if (lpDrv->hModule16 == lpNewDrv->hModule16) {
92 count++;
95 return count;
98 /**************************************************************************
99 * DRIVER_FindFromHDrvr16 [internal]
101 * From a hDrvr being 16 bits, returns the WINE internal structure.
103 static LPWINE_DRIVER DRIVER_FindFromHDrvr16(HDRVR16 hDrvr)
105 LPWINE_DRIVER lpDrv;
107 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
108 if (lpDrv->hDriver16 == hDrvr) {
109 break;
112 return lpDrv;
115 /**************************************************************************
116 * DRIVER_SendMessage [internal]
118 static LRESULT inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT16 msg,
119 LPARAM lParam1, LPARAM lParam2)
121 WORD args[8];
122 LRESULT ret;
124 TRACE("Before CallDriverProc proc=%p driverID=%08lx wMsg=%04x p1=%08lx p2=%08lx\n",
125 lpDrv->lpDrvProc, lpDrv->dwDriverID, msg, lParam1, lParam2);
127 args[7] = HIWORD(lpDrv->dwDriverID);
128 args[6] = LOWORD(lpDrv->dwDriverID);
129 args[5] = lpDrv->hDriver16;
130 args[4] = msg;
131 args[3] = HIWORD(lParam1);
132 args[2] = LOWORD(lParam1);
133 args[1] = HIWORD(lParam2);
134 args[0] = LOWORD(lParam2);
135 WOWCallback16Ex( (DWORD)lpDrv->lpDrvProc, WCB16_PASCAL, sizeof(args), args, &ret );
136 return ret;
139 /**************************************************************************
140 * SendDriverMessage (USER.251)
142 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
143 LPARAM lParam2)
145 LPWINE_DRIVER lpDrv;
146 LRESULT retval = 0;
148 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
150 if ((lpDrv = DRIVER_FindFromHDrvr16(hDriver)) != NULL) {
151 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
152 } else {
153 WARN("Bad driver handle %u\n", hDriver);
156 TRACE("retval = %ld\n", retval);
157 return retval;
160 /**************************************************************************
161 * DRIVER_RemoveFromList [internal]
163 * Generates all the logic to handle driver closure / deletion
164 * Removes a driver struct to the list of open drivers.
166 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
168 lpDrv->dwDriverID = 0;
169 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
170 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
171 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
174 if (lpDrv->lpPrevItem)
175 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
176 else
177 lpDrvItemList = lpDrv->lpNextItem;
178 if (lpDrv->lpNextItem)
179 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
181 return TRUE;
184 /**************************************************************************
185 * DRIVER_AddToList [internal]
187 * Adds a driver struct to the list of open drivers.
188 * Generates all the logic to handle driver creation / open.
190 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
192 /* First driver to be loaded for this module, need to load correctly the module */
193 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
194 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
195 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
196 return FALSE;
198 /* returned value is not checked */
199 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
202 lpNewDrv->lpNextItem = NULL;
203 if (lpDrvItemList == NULL) {
204 lpDrvItemList = lpNewDrv;
205 lpNewDrv->lpPrevItem = NULL;
206 } else {
207 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
208 while (lpDrv->lpNextItem != NULL)
209 lpDrv = lpDrv->lpNextItem;
211 lpDrv->lpNextItem = lpNewDrv;
212 lpNewDrv->lpPrevItem = lpDrv;
214 /* Now just open a new instance of a driver on this module */
215 lpNewDrv->dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
217 if (lpNewDrv->dwDriverID == 0) {
218 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
219 DRIVER_RemoveFromList(lpNewDrv);
220 return FALSE;
223 return TRUE;
226 /**************************************************************************
227 * DRIVER_TryOpenDriver16 [internal]
229 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
231 static LPWINE_DRIVER DRIVER_TryOpenDriver16(LPCSTR lpFileName, LPARAM lParam2)
233 static WORD DRIVER_hDrvr16Counter /* = 0 */;
234 LPWINE_DRIVER lpDrv = NULL;
235 HMODULE16 hModule;
236 DRIVERPROC16 lpProc;
237 LPCSTR lpSFN;
238 LPSTR ptr;
240 TRACE("('%s', %08lX);\n", lpFileName, lParam2);
242 if (strlen(lpFileName) < 1) return lpDrv;
244 if ((lpSFN = strrchr(lpFileName, '\\')) == NULL)
245 lpSFN = lpFileName;
246 else
247 lpSFN++;
248 if ((ptr = strchr(lpFileName, ' ')) != NULL) {
249 *ptr++ = '\0';
250 while (*ptr == ' ') ptr++;
251 if (*ptr == '\0') ptr = NULL;
254 if ((hModule = LoadLibrary16(lpFileName)) < 32) goto exit;
255 if ((lpProc = (DRIVERPROC16)GetProcAddress16(hModule, "DRIVERPROC")) == NULL)
256 goto exit;
258 if ((lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER))) == NULL)
259 goto exit;
261 lpDrv->dwDriverID = 0;
262 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
263 lpDrv->hDriver16 = DRIVER_hDrvr16Counter;
264 lstrcpynA(lpDrv->szAliasName, lpSFN, sizeof(lpDrv->szAliasName));
265 lpDrv->hModule16 = hModule;
266 lpDrv->lpDrvProc = lpProc;
268 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) goto exit;
270 return lpDrv;
271 exit:
272 TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName, hModule);
273 if (hModule >= 32) FreeLibrary16(hModule);
274 HeapFree(GetProcessHeap(), 0, lpDrv);
275 return NULL;
278 /**************************************************************************
279 * OpenDriver (USER.252)
281 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
283 LPWINE_DRIVER lpDrv = NULL;
284 char drvName[128];
286 TRACE("(%s, %s, %08lX);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
288 if (!lpDriverName || !*lpDriverName) return 0;
290 if (lpSectionName == NULL) {
291 strcpy(drvName, lpDriverName);
293 if ((lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2)))
294 goto the_end;
295 /* in case hDriver is NULL, search in Drivers section */
296 lpSectionName = "Drivers";
298 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "",
299 drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
300 lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2);
302 if (!lpDrv) {
303 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName));
304 return 0;
306 the_end:
307 TRACE("=> %04x / %08lx\n", lpDrv->hDriver16, (DWORD)lpDrv);
308 return lpDrv->hDriver16;
311 /**************************************************************************
312 * CloseDriver (USER.253)
314 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
316 LPWINE_DRIVER lpDrv;
318 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
320 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
321 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
323 if (DRIVER_RemoveFromList(lpDrv)) {
324 HeapFree(GetProcessHeap(), 0, lpDrv);
325 return TRUE;
328 WARN("Failed to close driver\n");
329 return FALSE;
332 /**************************************************************************
333 * GetDriverModuleHandle (USER.254)
335 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
337 LPWINE_DRIVER lpDrv;
338 HMODULE16 hModule = 0;
340 TRACE("(%04x);\n", hDrvr);
342 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
343 hModule = lpDrv->hModule16;
345 TRACE("=> %04x\n", hModule);
346 return hModule;
349 /**************************************************************************
350 * DefDriverProc (USER.255)
352 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
353 LPARAM lParam1, LPARAM lParam2)
355 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
356 dwDevID, hDriv, wMsg, lParam1, lParam2);
358 switch(wMsg) {
359 case DRV_LOAD:
360 case DRV_FREE:
361 case DRV_ENABLE:
362 case DRV_DISABLE:
363 return (LRESULT)1L;
364 case DRV_OPEN:
365 case DRV_CLOSE:
366 case DRV_QUERYCONFIGURE:
367 return (LRESULT)0L;
368 case DRV_CONFIGURE:
369 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
370 return (LRESULT)0L;
371 case DRV_INSTALL:
372 case DRV_REMOVE:
373 return DRV_SUCCESS;
374 default:
375 return (LRESULT)0L;
379 /**************************************************************************
380 * GetDriverInfo (USER.256)
382 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
384 LPWINE_DRIVER lpDrv;
385 BOOL16 ret = FALSE;
387 TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
389 if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16))
390 return FALSE;
392 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
393 lpDrvInfo->hDriver = lpDrv->hDriver16;
394 lpDrvInfo->hModule = lpDrv->hModule16;
395 lstrcpynA(lpDrvInfo->szAliasName, lpDrv->szAliasName, sizeof(lpDrvInfo->szAliasName));
396 ret = TRUE;
399 return ret;
402 /**************************************************************************
403 * GetNextDriver (USER.257)
405 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
407 HDRVR16 hRetDrv = 0;
408 LPWINE_DRIVER lpDrv;
410 TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
412 if (hDrvr == 0) {
413 if (lpDrvItemList == NULL) {
414 FIXME("drivers list empty !\n");
415 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
416 * I (EPP) don't quite understand this
418 if (lpDrvItemList == NULL)
419 return 0;
421 lpDrv = lpDrvItemList;
422 if (dwFlags & GND_REVERSE) {
423 while (lpDrv->lpNextItem)
424 lpDrv = lpDrv->lpNextItem;
426 } else {
427 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
428 if (dwFlags & GND_REVERSE) {
429 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
430 } else {
431 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
436 hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
437 TRACE("return %04x !\n", hRetDrv);
438 return hRetDrv;