- Added f8 (history retrieval from partial command) support
[wine/multimedia.git] / windows / driver.c
blob02d250cfb7c1ddf73f1b9b657b39615f9e3bb0ca
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 "mmddk.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(driver);
36 typedef struct tagWINE_DRIVER
38 char szAliasName[128];
39 /* as usual LPWINE_DRIVER == hDriver32 */
40 HDRVR16 hDriver16;
41 HMODULE16 hModule16;
42 DRIVERPROC16 lpDrvProc;
43 DWORD dwDriverID;
44 struct tagWINE_DRIVER* lpPrevItem;
45 struct tagWINE_DRIVER* lpNextItem;
46 } WINE_DRIVER, *LPWINE_DRIVER;
48 static LPWINE_DRIVER lpDrvItemList = NULL;
50 /* TODO list :
51 * - LoadModule count and clean up is not handled correctly (it's not a
52 * problem as long as FreeLibrary is not working correctly)
55 /* ### start build ### */
56 extern LONG CALLBACK DRIVER_CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
57 /* ### stop build ### */
59 /**************************************************************************
60 * LoadStartupDrivers [internal]
62 static void WINE_UNUSED DRIVER_LoadStartupDrivers(void)
64 char str[256];
66 if (GetPrivateProfileStringA("drivers", NULL, "", str, sizeof(str), "SYSTEM.INI") < 2) {
67 ERR("Can't find drivers section in system.ini\n");
68 } else {
69 HDRVR16 hDrv;
70 LPSTR ptr;
72 for (ptr = str; *ptr; ptr += strlen(ptr) + 1) {
73 TRACE("str='%s'\n", ptr);
74 hDrv = OpenDriver16(ptr, "drivers", 0L);
75 TRACE("hDrv=%04x\n", hDrv);
77 TRACE("end of list !\n");
81 /**************************************************************************
82 * DRIVER_GetNumberOfModuleRefs [internal]
84 * Returns the number of open drivers which share the same module.
86 static WORD DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv)
88 LPWINE_DRIVER lpDrv;
89 WORD count = 0;
91 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
92 if (lpDrv->hModule16 == lpNewDrv->hModule16) {
93 count++;
96 return count;
99 /**************************************************************************
100 * DRIVER_FindFromHDrvr16 [internal]
102 * From a hDrvr being 16 bits, returns the WINE internal structure.
104 static LPWINE_DRIVER DRIVER_FindFromHDrvr16(HDRVR16 hDrvr)
106 LPWINE_DRIVER lpDrv;
108 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
109 if (lpDrv->hDriver16 == hDrvr) {
110 break;
113 return lpDrv;
116 /**************************************************************************
117 * DRIVER_SendMessage [internal]
119 static LRESULT inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT16 msg,
120 LPARAM lParam1, LPARAM lParam2)
122 TRACE("Before CallDriverProc proc=%p driverID=%08lx wMsg=%04x p1=%08lx p2=%08lx\n",
123 lpDrv->lpDrvProc, lpDrv->dwDriverID, msg, lParam1, lParam2);
124 return DRIVER_CallTo16_long_lwwll((FARPROC16)lpDrv->lpDrvProc, lpDrv->dwDriverID,
125 lpDrv->hDriver16, msg, lParam1, lParam2);
128 /**************************************************************************
129 * SendDriverMessage (USER.251)
130 * SendDriverMessage16 (USER32.@)
132 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
133 LPARAM lParam2)
135 LPWINE_DRIVER lpDrv;
136 LRESULT retval = 0;
138 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
140 if ((lpDrv = DRIVER_FindFromHDrvr16(hDriver)) != NULL) {
141 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
142 } else {
143 WARN("Bad driver handle %u\n", hDriver);
146 TRACE("retval = %ld\n", retval);
147 return retval;
150 /**************************************************************************
151 * DRIVER_RemoveFromList [internal]
153 * Generates all the logic to handle driver closure / deletion
154 * Removes a driver struct to the list of open drivers.
156 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
158 lpDrv->dwDriverID = 0;
159 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
160 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
161 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
164 if (lpDrv->lpPrevItem)
165 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
166 else
167 lpDrvItemList = lpDrv->lpNextItem;
168 if (lpDrv->lpNextItem)
169 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
171 return TRUE;
174 /**************************************************************************
175 * DRIVER_AddToList [internal]
177 * Adds a driver struct to the list of open drivers.
178 * Generates all the logic to handle driver creation / open.
180 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
182 /* First driver to be loaded for this module, need to load correctly the module */
183 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
184 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
185 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
186 return FALSE;
188 /* returned value is not checked */
189 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
192 lpNewDrv->lpNextItem = NULL;
193 if (lpDrvItemList == NULL) {
194 lpDrvItemList = lpNewDrv;
195 lpNewDrv->lpPrevItem = NULL;
196 } else {
197 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
198 while (lpDrv->lpNextItem != NULL)
199 lpDrv = lpDrv->lpNextItem;
201 lpDrv->lpNextItem = lpNewDrv;
202 lpNewDrv->lpPrevItem = lpDrv;
204 /* Now just open a new instance of a driver on this module */
205 lpNewDrv->dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
207 if (lpNewDrv->dwDriverID == 0) {
208 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
209 DRIVER_RemoveFromList(lpNewDrv);
210 return FALSE;
213 return TRUE;
216 /**************************************************************************
217 * DRIVER_TryOpenDriver16 [internal]
219 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
221 static LPWINE_DRIVER DRIVER_TryOpenDriver16(LPCSTR lpFileName, LPARAM lParam2)
223 static WORD DRIVER_hDrvr16Counter /* = 0 */;
224 LPWINE_DRIVER lpDrv = NULL;
225 HMODULE16 hModule;
226 DRIVERPROC16 lpProc;
227 LPCSTR lpSFN;
228 LPSTR ptr;
230 TRACE("('%s', %08lX);\n", lpFileName, lParam2);
232 if (strlen(lpFileName) < 1) return lpDrv;
234 if ((lpSFN = strrchr(lpFileName, '\\')) == NULL)
235 lpSFN = lpFileName;
236 else
237 lpSFN++;
238 if ((ptr = strchr(lpFileName, ' ')) != NULL) {
239 *ptr++ = '\0';
240 while (*ptr == ' ') ptr++;
241 if (*ptr == '\0') ptr = NULL;
244 if ((hModule = LoadLibrary16(lpFileName)) < 32) goto exit;
245 if ((lpProc = (DRIVERPROC16)GetProcAddress16(hModule, "DRIVERPROC")) == NULL)
246 goto exit;
248 if ((lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER))) == NULL)
249 goto exit;
251 lpDrv->dwDriverID = 0;
252 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
253 lpDrv->hDriver16 = DRIVER_hDrvr16Counter;
254 lstrcpynA(lpDrv->szAliasName, lpSFN, sizeof(lpDrv->szAliasName));
255 lpDrv->hModule16 = hModule;
256 lpDrv->lpDrvProc = lpProc;
258 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) goto exit;
260 return lpDrv;
261 exit:
262 TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName, hModule);
263 if (hModule >= 32) FreeLibrary16(hModule);
264 HeapFree(GetProcessHeap(), 0, lpDrv);
265 return NULL;
268 /**************************************************************************
269 * OpenDriver (USER.252)
270 * OpenDriver16 (USER32.@)
272 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
274 LPWINE_DRIVER lpDrv = NULL;
275 char drvName[128];
277 TRACE("(%s, %s, %08lX);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
279 if (!lpDriverName || !*lpDriverName) return 0;
281 if (lpSectionName == NULL) {
282 strcpy(drvName, lpDriverName);
284 if ((lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2)))
285 goto the_end;
286 /* in case hDriver is NULL, search in Drivers section */
287 lpSectionName = "Drivers";
289 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "",
290 drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
291 lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2);
293 if (!lpDrv) {
294 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName));
295 return 0;
297 the_end:
298 TRACE("=> %04x / %08lx\n", lpDrv->hDriver16, (DWORD)lpDrv);
299 return lpDrv->hDriver16;
302 /**************************************************************************
303 * CloseDriver (USER.253)
304 * CloseDriver16 (USER32.@)
306 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
308 LPWINE_DRIVER lpDrv;
310 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
312 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
313 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
315 if (DRIVER_RemoveFromList(lpDrv)) {
316 HeapFree(GetProcessHeap(), 0, lpDrv);
317 return TRUE;
320 WARN("Failed to close driver\n");
321 return FALSE;
324 /**************************************************************************
325 * GetDriverModuleHandle (USER.254)
326 * GetDriverModuleHandle16 (USER32.@)
328 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
330 LPWINE_DRIVER lpDrv;
331 HMODULE16 hModule = 0;
333 TRACE("(%04x);\n", hDrvr);
335 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
336 hModule = lpDrv->hModule16;
338 TRACE("=> %04x\n", hModule);
339 return hModule;
342 /**************************************************************************
343 * DefDriverProc (USER.255)
344 * DefDriverProc16 (USER32.@)
346 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
347 LPARAM lParam1, LPARAM lParam2)
349 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
350 dwDevID, hDriv, wMsg, lParam1, lParam2);
352 switch(wMsg) {
353 case DRV_LOAD:
354 case DRV_FREE:
355 case DRV_ENABLE:
356 case DRV_DISABLE:
357 return (LRESULT)1L;
358 case DRV_OPEN:
359 case DRV_CLOSE:
360 case DRV_QUERYCONFIGURE:
361 return (LRESULT)0L;
362 case DRV_CONFIGURE:
363 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
364 return (LRESULT)0L;
365 case DRV_INSTALL:
366 case DRV_REMOVE:
367 return DRV_SUCCESS;
368 default:
369 return (LRESULT)0L;
373 /**************************************************************************
374 * GetDriverInfo (USER.256)
376 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
378 LPWINE_DRIVER lpDrv;
379 BOOL16 ret = FALSE;
381 TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
383 if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16))
384 return FALSE;
386 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
387 lpDrvInfo->hDriver = lpDrv->hDriver16;
388 lpDrvInfo->hModule = lpDrv->hModule16;
389 lstrcpynA(lpDrvInfo->szAliasName, lpDrv->szAliasName, sizeof(lpDrvInfo->szAliasName));
390 ret = TRUE;
393 return ret;
396 /**************************************************************************
397 * GetNextDriver (USER.257)
399 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
401 HDRVR16 hRetDrv = 0;
402 LPWINE_DRIVER lpDrv;
404 TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
406 if (hDrvr == 0) {
407 if (lpDrvItemList == NULL) {
408 FIXME("drivers list empty !\n");
409 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
410 * I (EPP) don't quite understand this
412 if (lpDrvItemList == NULL)
413 return 0;
415 lpDrv = lpDrvItemList;
416 if (dwFlags & GND_REVERSE) {
417 while (lpDrv->lpNextItem)
418 lpDrv = lpDrv->lpNextItem;
420 } else {
421 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
422 if (dwFlags & GND_REVERSE) {
423 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
424 } else {
425 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
430 hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
431 TRACE("return %04x !\n", hRetDrv);
432 return hRetDrv;