Fixed a number of incompatibilities in OEM icons/cursors handling.
[wine] / windows / driver.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  * WINE Drivers functions
5  *
6  * Copyright 1994 Martin Ayotte
7  * Copyright 1998 Marcus Meissner
8  * Copyright 1999,2000 Eric Pouech
9  */
10
11 #include <string.h>
12 #include "heap.h"
13 #include "windef.h"
14 #include "wine/winbase16.h"
15 #include "wingdi.h"
16 #include "winuser.h"
17 #include "mmddk.h"
18 #include "debugtools.h"
19
20 DEFAULT_DEBUG_CHANNEL(driver);
21
22 typedef struct tagWINE_DRIVER
23 {
24     char                        szAliasName[128];
25     /* as usual LPWINE_DRIVER == hDriver32 */
26     HDRVR16                     hDriver16;
27     HMODULE16                   hModule16;
28     DRIVERPROC16                lpDrvProc;
29     DWORD                       dwDriverID;
30     struct tagWINE_DRIVER*      lpPrevItem;
31     struct tagWINE_DRIVER*      lpNextItem;
32 } WINE_DRIVER, *LPWINE_DRIVER;
33
34 static LPWINE_DRIVER    lpDrvItemList = NULL;
35
36 /* TODO list :
37  *      - LoadModule count and clean up is not handled correctly (it's not a 
38  *        problem as long as FreeLibrary is not working correctly)
39  */
40
41 /* ### start build ### */
42 extern LONG CALLBACK DRIVER_CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
43 /* ### stop build ### */
44
45 /**************************************************************************
46  *                      LoadStartupDrivers                      [internal]
47  */
48 static void WINE_UNUSED DRIVER_LoadStartupDrivers(void)
49 {
50     char        str[256];
51     
52     if (GetPrivateProfileStringA("drivers", NULL, "", str, sizeof(str), "SYSTEM.INI") < 2) {
53         ERR("Can't find drivers section in system.ini\n");
54     } else {
55         HDRVR16 hDrv;
56         LPSTR   ptr;
57
58         for (ptr = str; *ptr; ptr += strlen(ptr) + 1) {
59             TRACE("str='%s'\n", ptr);
60             hDrv = OpenDriver16(ptr, "drivers", 0L);
61             TRACE("hDrv=%04x\n", hDrv);
62         }
63         TRACE("end of list !\n");
64     }
65 }
66
67 /**************************************************************************
68  *                      DRIVER_GetNumberOfModuleRefs            [internal]
69  *
70  * Returns the number of open drivers which share the same module.
71  */
72 static  WORD    DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv)
73 {
74     LPWINE_DRIVER       lpDrv;
75     WORD                count = 0;
76     
77     for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
78         if (lpDrv->hModule16 == lpNewDrv->hModule16) {
79             count++;
80         }
81     }
82     return count;
83 }
84
85 /**************************************************************************
86  *                              DRIVER_FindFromHDrvr16          [internal]
87  *
88  * From a hDrvr being 16 bits, returns the WINE internal structure.
89  */
90 static  LPWINE_DRIVER   DRIVER_FindFromHDrvr16(HDRVR16 hDrvr)
91 {    
92     LPWINE_DRIVER       lpDrv;
93     
94     for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
95         if (lpDrv->hDriver16 == hDrvr) {
96             break;
97         }
98     }
99     return lpDrv;
100 }
101
102 /**************************************************************************
103  *                              DRIVER_SendMessage              [internal]
104  */
105 static LRESULT inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT16 msg, 
106                                          LPARAM lParam1, LPARAM lParam2)
107 {
108     TRACE("Before CallDriverProc proc=%p driverID=%08lx wMsg=%04x p1=%08lx p2=%08lx\n", 
109           lpDrv->lpDrvProc, lpDrv->dwDriverID, msg, lParam1, lParam2);
110     return DRIVER_CallTo16_long_lwwll((FARPROC16)lpDrv->lpDrvProc, lpDrv->dwDriverID,
111                                       lpDrv->hDriver16, msg, lParam1, lParam2);
112 }
113
114 /**************************************************************************
115  *              SendDriverMessage (USER.251)
116  */
117 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
118                                    LPARAM lParam2)
119 {
120     LPWINE_DRIVER       lpDrv;
121     LRESULT             retval = 0;
122     
123     TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
124     
125     if ((lpDrv = DRIVER_FindFromHDrvr16(hDriver)) != NULL) {
126         retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
127     } else {
128         WARN("Bad driver handle %u\n", hDriver);
129     }
130     
131     TRACE("retval = %ld\n", retval);
132     return retval;
133 }
134
135 /**************************************************************************
136  *                              DRIVER_RemoveFromList           [internal]
137  *
138  * Generates all the logic to handle driver closure / deletion
139  * Removes a driver struct to the list of open drivers.
140  */
141 static  BOOL    DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
142 {
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);
147     }
148     
149     if (lpDrv->lpPrevItem)
150         lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
151     else
152         lpDrvItemList = lpDrv->lpNextItem;
153     if (lpDrv->lpNextItem)
154         lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
155
156     return TRUE;
157 }
158
159 /**************************************************************************
160  *                              DRIVER_AddToList                [internal]
161  *
162  * Adds a driver struct to the list of open drivers.
163  * Generates all the logic to handle driver creation / open.
164  */
165 static  BOOL    DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
166 {
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 0x%08lx\n", (DWORD)lpNewDrv);
171             return FALSE;
172         }
173         /* returned value is not checked */
174         DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
175     }
176
177     lpNewDrv->lpNextItem = NULL;
178     if (lpDrvItemList == NULL) {
179         lpDrvItemList = lpNewDrv;
180         lpNewDrv->lpPrevItem = NULL;
181     } else {
182         LPWINE_DRIVER   lpDrv = lpDrvItemList;  /* find end of list */
183         while (lpDrv->lpNextItem != NULL)
184             lpDrv = lpDrv->lpNextItem;
185         
186         lpDrv->lpNextItem = lpNewDrv;
187         lpNewDrv->lpPrevItem = lpDrv;
188     }
189     /* Now just open a new instance of a driver on this module */
190     lpNewDrv->dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
191
192     if (lpNewDrv->dwDriverID == 0) {
193         TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
194         DRIVER_RemoveFromList(lpNewDrv);
195         return FALSE;
196     }
197     
198     return TRUE;
199 }
200
201 /**************************************************************************
202  *                              DRIVER_TryOpenDriver16          [internal]
203  *
204  * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
205  */
206 static  LPWINE_DRIVER   DRIVER_TryOpenDriver16(LPCSTR lpFileName, LPARAM lParam2)
207 {
208     static      WORD    DRIVER_hDrvr16Counter /* = 0 */;
209     LPWINE_DRIVER       lpDrv = NULL;
210     HMODULE16           hModule;
211     DRIVERPROC16        lpProc;
212     LPCSTR              lpSFN;
213     LPSTR               ptr;
214     
215     TRACE("('%s', %08lX);\n", lpFileName, lParam2);
216
217     if (strlen(lpFileName) < 1) return lpDrv;
218
219     if ((lpSFN = strrchr(lpFileName, '\\')) == NULL)
220         lpSFN = lpFileName;
221     else
222         lpSFN++;
223     if ((ptr = strchr(lpFileName, ' ')) != NULL) {
224         *ptr++ = '\0';
225         while (*ptr == ' ') ptr++;
226         if (*ptr == '\0') ptr = NULL;
227     }
228     
229     if ((hModule = LoadLibrary16(lpFileName)) < 32) goto exit;
230     if ((lpProc = (DRIVERPROC16)GetProcAddress16(hModule, "DRIVERPROC")) == NULL)
231         goto exit;
232
233     if ((lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER))) == NULL)
234         goto exit;
235
236     lpDrv->dwDriverID  = 0;
237     while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
238     lpDrv->hDriver16   = DRIVER_hDrvr16Counter;
239     lstrcpynA(lpDrv->szAliasName, lpSFN, sizeof(lpDrv->szAliasName));
240     lpDrv->hModule16   = hModule;
241     lpDrv->lpDrvProc   = lpProc;
242     
243     if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) goto exit;
244
245     return lpDrv;
246  exit:
247     TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName, hModule);
248     if (hModule >= 32)  FreeLibrary16(hModule);
249     HeapFree(GetProcessHeap(), 0, lpDrv);
250     return NULL;
251 }
252
253 /**************************************************************************
254  *              OpenDriver16 (USER.252)
255  */
256 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
257 {
258     LPWINE_DRIVER       lpDrv = NULL;
259     char                drvName[128];
260
261     TRACE("('%s', '%s', %08lX);\n", lpDriverName, lpSectionName, lParam2);
262
263     if (!lpDriverName || !*lpDriverName) return 0;
264
265     if (lpSectionName == NULL) {
266         strcpy(drvName, lpDriverName);
267     
268         if ((lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2)))
269             goto the_end;
270         /* in case hDriver is NULL, search in Drivers section */
271         lpSectionName = "Drivers";
272     }
273     if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "", 
274                                  drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
275         lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2);
276     }
277     if (!lpDrv) {
278         TRACE("Failed to open driver %s from system.ini file, section %s\n", lpDriverName, lpSectionName);
279         return 0;
280     }
281  the_end:
282     TRACE("=> %04x / %08lx\n", lpDrv->hDriver16, (DWORD)lpDrv);
283     return lpDrv->hDriver16;
284 }
285
286 /**************************************************************************
287  *              CloseDriver (USER.253)
288  */
289 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
290 {
291     LPWINE_DRIVER       lpDrv;
292
293     TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
294     
295     if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
296         DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
297     
298         if (DRIVER_RemoveFromList(lpDrv)) {
299             HeapFree(GetProcessHeap(), 0, lpDrv);
300             return TRUE;
301         }
302     }
303     WARN("Failed to close driver\n");
304     return FALSE;
305 }
306
307 /**************************************************************************
308  *              GetDriverModuleHandle (USER.254)
309  */
310 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
311 {
312     LPWINE_DRIVER       lpDrv;
313     HMODULE16           hModule = 0;
314     
315     TRACE("(%04x);\n", hDrvr);
316     
317     if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
318         hModule = lpDrv->hModule16;
319     }
320     TRACE("=> %04x\n", hModule);
321     return hModule;
322 }
323
324 /**************************************************************************
325  *              DefDriverProc (USER.255)
326  */
327 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg, 
328                                LPARAM lParam1, LPARAM lParam2)
329 {
330     TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
331           dwDevID, hDriv, wMsg, lParam1, lParam2);
332
333     switch(wMsg) {
334     case DRV_LOAD:              
335     case DRV_FREE:              
336     case DRV_ENABLE:
337     case DRV_DISABLE:
338         return (LRESULT)1L;
339     case DRV_OPEN:              
340     case DRV_CLOSE:             
341     case DRV_QUERYCONFIGURE:
342         return (LRESULT)0L;
343     case DRV_CONFIGURE:         
344         MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK); 
345         return (LRESULT)0L;
346     case DRV_INSTALL:           
347     case DRV_REMOVE:            
348         return DRV_SUCCESS;
349     default:                    
350         return (LRESULT)0L;
351     }
352 }
353
354 /**************************************************************************
355  *              GetDriverInfo (USER.256)
356  */
357 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
358 {
359     LPWINE_DRIVER       lpDrv;
360     BOOL16              ret = FALSE;
361     
362     TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
363     
364     if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16)) 
365         return FALSE;
366     
367     if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
368         lpDrvInfo->hDriver = lpDrv->hDriver16;
369         lpDrvInfo->hModule = lpDrv->hModule16;
370         lstrcpynA(lpDrvInfo->szAliasName, lpDrv->szAliasName, sizeof(lpDrvInfo->szAliasName));
371         ret = TRUE;
372     }
373     
374     return ret;
375 }
376
377 /**************************************************************************
378  *              GetNextDriver (USER.257)
379  */
380 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
381 {
382     HDRVR16             hRetDrv = 0;
383     LPWINE_DRIVER       lpDrv;
384     
385     TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
386     
387     if (hDrvr == 0) {
388         if (lpDrvItemList == NULL) {
389             FIXME("drivers list empty !\n");
390             /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ? 
391              * I (EPP) don't quite understand this 
392              */
393             if (lpDrvItemList == NULL) 
394                 return 0;
395         }
396         lpDrv = lpDrvItemList;
397         if (dwFlags & GND_REVERSE) {
398             while (lpDrv->lpNextItem)
399                 lpDrv = lpDrv->lpNextItem;
400         }
401     } else {
402         if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
403             if (dwFlags & GND_REVERSE) {
404                 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
405             } else {
406                 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
407             }
408         }
409     }
410     
411     hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
412     TRACE("return %04x !\n", hRetDrv);
413     return hRetDrv;
414 }
415