- ISF_Desktop_fnGetUIObjectOf(): allow context menu on the desktop
[wine.git] / windows / driver.c
blobbefbad1e60261fc843f983af9d27c15624f54d72
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 <stdarg.h>
26 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wine/winbase16.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "wownt32.h"
34 #include "mmddk.h"
35 #include "wine/mmsystem16.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(driver);
40 typedef struct tagWINE_DRIVER
42 char szAliasName[128];
43 /* as usual LPWINE_DRIVER == hDriver32 */
44 HDRVR16 hDriver16;
45 HMODULE16 hModule16;
46 DRIVERPROC16 lpDrvProc;
47 DWORD dwDriverID;
48 struct tagWINE_DRIVER* lpPrevItem;
49 struct tagWINE_DRIVER* lpNextItem;
50 } WINE_DRIVER, *LPWINE_DRIVER;
52 static LPWINE_DRIVER lpDrvItemList = NULL;
54 /* TODO list :
55 * - LoadModule count and clean up is not handled correctly (it's not a
56 * problem as long as FreeLibrary is not working correctly)
60 /**************************************************************************
61 * LoadStartupDrivers [internal]
63 static void WINE_UNUSED DRIVER_LoadStartupDrivers(void)
65 char str[256];
67 if (GetPrivateProfileStringA("drivers", NULL, "", str, sizeof(str), "SYSTEM.INI") < 2) {
68 ERR("Can't find drivers section in system.ini\n");
69 } else {
70 HDRVR16 hDrv;
71 LPSTR ptr;
73 for (ptr = str; *ptr; ptr += strlen(ptr) + 1) {
74 TRACE("str='%s'\n", ptr);
75 hDrv = OpenDriver16(ptr, "drivers", 0L);
76 TRACE("hDrv=%04x\n", hDrv);
78 TRACE("end of list !\n");
82 /**************************************************************************
83 * DRIVER_GetNumberOfModuleRefs [internal]
85 * Returns the number of open drivers which share the same module.
87 static WORD DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv)
89 LPWINE_DRIVER lpDrv;
90 WORD count = 0;
92 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
93 if (lpDrv->hModule16 == lpNewDrv->hModule16) {
94 count++;
97 return count;
100 /**************************************************************************
101 * DRIVER_FindFromHDrvr16 [internal]
103 * From a hDrvr being 16 bits, returns the WINE internal structure.
105 static LPWINE_DRIVER DRIVER_FindFromHDrvr16(HDRVR16 hDrvr)
107 LPWINE_DRIVER lpDrv;
109 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
110 if (lpDrv->hDriver16 == hDrvr) {
111 break;
114 return lpDrv;
117 /**************************************************************************
118 * DRIVER_SendMessage [internal]
120 static LRESULT inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT16 msg,
121 LPARAM lParam1, LPARAM lParam2)
123 WORD args[8];
124 LRESULT ret;
126 TRACE("Before CallDriverProc proc=%p driverID=%08lx wMsg=%04x p1=%08lx p2=%08lx\n",
127 lpDrv->lpDrvProc, lpDrv->dwDriverID, msg, lParam1, lParam2);
129 args[7] = HIWORD(lpDrv->dwDriverID);
130 args[6] = LOWORD(lpDrv->dwDriverID);
131 args[5] = lpDrv->hDriver16;
132 args[4] = msg;
133 args[3] = HIWORD(lParam1);
134 args[2] = LOWORD(lParam1);
135 args[1] = HIWORD(lParam2);
136 args[0] = LOWORD(lParam2);
137 WOWCallback16Ex( (DWORD)lpDrv->lpDrvProc, WCB16_PASCAL, sizeof(args), args, &ret );
138 return ret;
141 /**************************************************************************
142 * SendDriverMessage (USER.251)
144 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
145 LPARAM lParam2)
147 LPWINE_DRIVER lpDrv;
148 LRESULT retval = 0;
150 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
152 if ((lpDrv = DRIVER_FindFromHDrvr16(hDriver)) != NULL) {
153 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
154 } else {
155 WARN("Bad driver handle %u\n", hDriver);
158 TRACE("retval = %ld\n", retval);
159 return retval;
162 /**************************************************************************
163 * DRIVER_RemoveFromList [internal]
165 * Generates all the logic to handle driver closure / deletion
166 * Removes a driver struct to the list of open drivers.
168 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
170 lpDrv->dwDriverID = 0;
171 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
172 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
173 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
176 if (lpDrv->lpPrevItem)
177 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
178 else
179 lpDrvItemList = lpDrv->lpNextItem;
180 if (lpDrv->lpNextItem)
181 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
183 return TRUE;
186 /**************************************************************************
187 * DRIVER_AddToList [internal]
189 * Adds a driver struct to the list of open drivers.
190 * Generates all the logic to handle driver creation / open.
192 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
194 /* First driver to be loaded for this module, need to load correctly the module */
195 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
196 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
197 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
198 return FALSE;
200 /* returned value is not checked */
201 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
204 lpNewDrv->lpNextItem = NULL;
205 if (lpDrvItemList == NULL) {
206 lpDrvItemList = lpNewDrv;
207 lpNewDrv->lpPrevItem = NULL;
208 } else {
209 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
210 while (lpDrv->lpNextItem != NULL)
211 lpDrv = lpDrv->lpNextItem;
213 lpDrv->lpNextItem = lpNewDrv;
214 lpNewDrv->lpPrevItem = lpDrv;
216 /* Now just open a new instance of a driver on this module */
217 lpNewDrv->dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
219 if (lpNewDrv->dwDriverID == 0) {
220 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
221 DRIVER_RemoveFromList(lpNewDrv);
222 return FALSE;
225 return TRUE;
228 /**************************************************************************
229 * DRIVER_TryOpenDriver16 [internal]
231 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
233 static LPWINE_DRIVER DRIVER_TryOpenDriver16(LPCSTR lpFileName, LPARAM lParam2)
235 static WORD DRIVER_hDrvr16Counter /* = 0 */;
236 LPWINE_DRIVER lpDrv = NULL;
237 HMODULE16 hModule;
238 DRIVERPROC16 lpProc;
239 LPCSTR lpSFN;
240 LPSTR ptr;
242 TRACE("('%s', %08lX);\n", lpFileName, lParam2);
244 if (strlen(lpFileName) < 1) return lpDrv;
246 if ((lpSFN = strrchr(lpFileName, '\\')) == NULL)
247 lpSFN = lpFileName;
248 else
249 lpSFN++;
250 if ((ptr = strchr(lpFileName, ' ')) != NULL) {
251 *ptr++ = '\0';
252 while (*ptr == ' ') ptr++;
253 if (*ptr == '\0') ptr = NULL;
256 if ((hModule = LoadLibrary16(lpFileName)) < 32) goto exit;
257 if ((lpProc = (DRIVERPROC16)GetProcAddress16(hModule, "DRIVERPROC")) == NULL)
258 goto exit;
260 if ((lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER))) == NULL)
261 goto exit;
263 lpDrv->dwDriverID = 0;
264 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
265 lpDrv->hDriver16 = DRIVER_hDrvr16Counter;
266 lstrcpynA(lpDrv->szAliasName, lpSFN, sizeof(lpDrv->szAliasName));
267 lpDrv->hModule16 = hModule;
268 lpDrv->lpDrvProc = lpProc;
270 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) goto exit;
272 return lpDrv;
273 exit:
274 TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName, hModule);
275 if (hModule >= 32) FreeLibrary16(hModule);
276 HeapFree(GetProcessHeap(), 0, lpDrv);
277 return NULL;
280 /**************************************************************************
281 * OpenDriver (USER.252)
283 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
285 LPWINE_DRIVER lpDrv = NULL;
286 char drvName[128];
288 TRACE("(%s, %s, %08lX);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
290 if (!lpDriverName || !*lpDriverName) return 0;
292 if (lpSectionName == NULL) {
293 strcpy(drvName, lpDriverName);
295 if ((lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2)))
296 goto the_end;
297 /* in case hDriver is NULL, search in Drivers section */
298 lpSectionName = "Drivers";
300 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "",
301 drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
302 lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2);
304 if (!lpDrv) {
305 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName));
306 return 0;
308 the_end:
309 TRACE("=> %04x / %08lx\n", lpDrv->hDriver16, (DWORD)lpDrv);
310 return lpDrv->hDriver16;
313 /**************************************************************************
314 * CloseDriver (USER.253)
316 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
318 LPWINE_DRIVER lpDrv;
320 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
322 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
323 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
325 if (DRIVER_RemoveFromList(lpDrv)) {
326 HeapFree(GetProcessHeap(), 0, lpDrv);
327 return TRUE;
330 WARN("Failed to close driver\n");
331 return FALSE;
334 /**************************************************************************
335 * GetDriverModuleHandle (USER.254)
337 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
339 LPWINE_DRIVER lpDrv;
340 HMODULE16 hModule = 0;
342 TRACE("(%04x);\n", hDrvr);
344 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
345 hModule = lpDrv->hModule16;
347 TRACE("=> %04x\n", hModule);
348 return hModule;
351 /**************************************************************************
352 * DefDriverProc (USER.255)
354 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
355 LPARAM lParam1, LPARAM lParam2)
357 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
358 dwDevID, hDriv, wMsg, lParam1, lParam2);
360 switch(wMsg) {
361 case DRV_LOAD:
362 case DRV_FREE:
363 case DRV_ENABLE:
364 case DRV_DISABLE:
365 return (LRESULT)1L;
366 case DRV_OPEN:
367 case DRV_CLOSE:
368 case DRV_QUERYCONFIGURE:
369 return (LRESULT)0L;
370 case DRV_CONFIGURE:
371 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
372 return (LRESULT)0L;
373 case DRV_INSTALL:
374 case DRV_REMOVE:
375 return DRV_SUCCESS;
376 default:
377 return (LRESULT)0L;
381 /**************************************************************************
382 * GetDriverInfo (USER.256)
384 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
386 LPWINE_DRIVER lpDrv;
387 BOOL16 ret = FALSE;
389 TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
391 if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16))
392 return FALSE;
394 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
395 lpDrvInfo->hDriver = lpDrv->hDriver16;
396 lpDrvInfo->hModule = lpDrv->hModule16;
397 lstrcpynA(lpDrvInfo->szAliasName, lpDrv->szAliasName, sizeof(lpDrvInfo->szAliasName));
398 ret = TRUE;
401 return ret;
404 /**************************************************************************
405 * GetNextDriver (USER.257)
407 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
409 HDRVR16 hRetDrv = 0;
410 LPWINE_DRIVER lpDrv;
412 TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
414 if (hDrvr == 0) {
415 if (lpDrvItemList == NULL) {
416 FIXME("drivers list empty !\n");
417 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
418 * I (EPP) don't quite understand this
420 if (lpDrvItemList == NULL)
421 return 0;
423 lpDrv = lpDrvItemList;
424 if (dwFlags & GND_REVERSE) {
425 while (lpDrv->lpNextItem)
426 lpDrv = lpDrv->lpNextItem;
428 } else {
429 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
430 if (dwFlags & GND_REVERSE) {
431 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
432 } else {
433 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
438 hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
439 TRACE("return %04x !\n", hRetDrv);
440 return hRetDrv;