Exception for allowing the parent to set the cursor for a child wnd is
[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  *              SendDriverMessage16 (USER32.@)
117  */
118 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
119                                    LPARAM lParam2)
120 {
121     LPWINE_DRIVER       lpDrv;
122     LRESULT             retval = 0;
123     
124     TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
125     
126     if ((lpDrv = DRIVER_FindFromHDrvr16(hDriver)) != NULL) {
127         retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
128     } else {
129         WARN("Bad driver handle %u\n", hDriver);
130     }
131     
132     TRACE("retval = %ld\n", retval);
133     return retval;
134 }
135
136 /**************************************************************************
137  *                              DRIVER_RemoveFromList           [internal]
138  *
139  * Generates all the logic to handle driver closure / deletion
140  * Removes a driver struct to the list of open drivers.
141  */
142 static  BOOL    DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
143 {
144     lpDrv->dwDriverID = 0;
145     if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
146         DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
147         DRIVER_SendMessage(lpDrv, DRV_FREE,    0L, 0L);
148     }
149     
150     if (lpDrv->lpPrevItem)
151         lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
152     else
153         lpDrvItemList = lpDrv->lpNextItem;
154     if (lpDrv->lpNextItem)
155         lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
156
157     return TRUE;
158 }
159
160 /**************************************************************************
161  *                              DRIVER_AddToList                [internal]
162  *
163  * Adds a driver struct to the list of open drivers.
164  * Generates all the logic to handle driver creation / open.
165  */
166 static  BOOL    DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
167 {
168     /* First driver to be loaded for this module, need to load correctly the module */
169     if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
170         if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
171             TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
172             return FALSE;
173         }
174         /* returned value is not checked */
175         DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
176     }
177
178     lpNewDrv->lpNextItem = NULL;
179     if (lpDrvItemList == NULL) {
180         lpDrvItemList = lpNewDrv;
181         lpNewDrv->lpPrevItem = NULL;
182     } else {
183         LPWINE_DRIVER   lpDrv = lpDrvItemList;  /* find end of list */
184         while (lpDrv->lpNextItem != NULL)
185             lpDrv = lpDrv->lpNextItem;
186         
187         lpDrv->lpNextItem = lpNewDrv;
188         lpNewDrv->lpPrevItem = lpDrv;
189     }
190     /* Now just open a new instance of a driver on this module */
191     lpNewDrv->dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
192
193     if (lpNewDrv->dwDriverID == 0) {
194         TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
195         DRIVER_RemoveFromList(lpNewDrv);
196         return FALSE;
197     }
198     
199     return TRUE;
200 }
201
202 /**************************************************************************
203  *                              DRIVER_TryOpenDriver16          [internal]
204  *
205  * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
206  */
207 static  LPWINE_DRIVER   DRIVER_TryOpenDriver16(LPCSTR lpFileName, LPARAM lParam2)
208 {
209     static      WORD    DRIVER_hDrvr16Counter /* = 0 */;
210     LPWINE_DRIVER       lpDrv = NULL;
211     HMODULE16           hModule;
212     DRIVERPROC16        lpProc;
213     LPCSTR              lpSFN;
214     LPSTR               ptr;
215     
216     TRACE("('%s', %08lX);\n", lpFileName, lParam2);
217
218     if (strlen(lpFileName) < 1) return lpDrv;
219
220     if ((lpSFN = strrchr(lpFileName, '\\')) == NULL)
221         lpSFN = lpFileName;
222     else
223         lpSFN++;
224     if ((ptr = strchr(lpFileName, ' ')) != NULL) {
225         *ptr++ = '\0';
226         while (*ptr == ' ') ptr++;
227         if (*ptr == '\0') ptr = NULL;
228     }
229     
230     if ((hModule = LoadLibrary16(lpFileName)) < 32) goto exit;
231     if ((lpProc = (DRIVERPROC16)GetProcAddress16(hModule, "DRIVERPROC")) == NULL)
232         goto exit;
233
234     if ((lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER))) == NULL)
235         goto exit;
236
237     lpDrv->dwDriverID  = 0;
238     while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
239     lpDrv->hDriver16   = DRIVER_hDrvr16Counter;
240     lstrcpynA(lpDrv->szAliasName, lpSFN, sizeof(lpDrv->szAliasName));
241     lpDrv->hModule16   = hModule;
242     lpDrv->lpDrvProc   = lpProc;
243     
244     if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) goto exit;
245
246     return lpDrv;
247  exit:
248     TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName, hModule);
249     if (hModule >= 32)  FreeLibrary16(hModule);
250     HeapFree(GetProcessHeap(), 0, lpDrv);
251     return NULL;
252 }
253
254 /**************************************************************************
255  *              OpenDriver (USER.252)
256  *              OpenDriver16 (USER32.@)
257  */
258 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
259 {
260     LPWINE_DRIVER       lpDrv = NULL;
261     char                drvName[128];
262
263     TRACE("('%s', '%s', %08lX);\n", lpDriverName, lpSectionName, lParam2);
264
265     if (!lpDriverName || !*lpDriverName) return 0;
266
267     if (lpSectionName == NULL) {
268         strcpy(drvName, lpDriverName);
269     
270         if ((lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2)))
271             goto the_end;
272         /* in case hDriver is NULL, search in Drivers section */
273         lpSectionName = "Drivers";
274     }
275     if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "", 
276                                  drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
277         lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2);
278     }
279     if (!lpDrv) {
280         TRACE("Failed to open driver %s from system.ini file, section %s\n", lpDriverName, lpSectionName);
281         return 0;
282     }
283  the_end:
284     TRACE("=> %04x / %08lx\n", lpDrv->hDriver16, (DWORD)lpDrv);
285     return lpDrv->hDriver16;
286 }
287
288 /**************************************************************************
289  *              CloseDriver (USER.253)
290  *              CloseDriver16 (USER32.@)
291  */
292 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
293 {
294     LPWINE_DRIVER       lpDrv;
295
296     TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
297     
298     if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
299         DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
300     
301         if (DRIVER_RemoveFromList(lpDrv)) {
302             HeapFree(GetProcessHeap(), 0, lpDrv);
303             return TRUE;
304         }
305     }
306     WARN("Failed to close driver\n");
307     return FALSE;
308 }
309
310 /**************************************************************************
311  *              GetDriverModuleHandle (USER.254)
312  *              GetDriverModuleHandle16 (USER32.@)
313  */
314 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
315 {
316     LPWINE_DRIVER       lpDrv;
317     HMODULE16           hModule = 0;
318     
319     TRACE("(%04x);\n", hDrvr);
320     
321     if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
322         hModule = lpDrv->hModule16;
323     }
324     TRACE("=> %04x\n", hModule);
325     return hModule;
326 }
327
328 /**************************************************************************
329  *              DefDriverProc (USER.255)
330  *              DefDriverProc16 (USER32.@)
331  */
332 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg, 
333                                LPARAM lParam1, LPARAM lParam2)
334 {
335     TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
336           dwDevID, hDriv, wMsg, lParam1, lParam2);
337
338     switch(wMsg) {
339     case DRV_LOAD:              
340     case DRV_FREE:              
341     case DRV_ENABLE:
342     case DRV_DISABLE:
343         return (LRESULT)1L;
344     case DRV_OPEN:              
345     case DRV_CLOSE:             
346     case DRV_QUERYCONFIGURE:
347         return (LRESULT)0L;
348     case DRV_CONFIGURE:         
349         MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK); 
350         return (LRESULT)0L;
351     case DRV_INSTALL:           
352     case DRV_REMOVE:            
353         return DRV_SUCCESS;
354     default:                    
355         return (LRESULT)0L;
356     }
357 }
358
359 /**************************************************************************
360  *              GetDriverInfo (USER.256)
361  */
362 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
363 {
364     LPWINE_DRIVER       lpDrv;
365     BOOL16              ret = FALSE;
366     
367     TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
368     
369     if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16)) 
370         return FALSE;
371     
372     if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
373         lpDrvInfo->hDriver = lpDrv->hDriver16;
374         lpDrvInfo->hModule = lpDrv->hModule16;
375         lstrcpynA(lpDrvInfo->szAliasName, lpDrv->szAliasName, sizeof(lpDrvInfo->szAliasName));
376         ret = TRUE;
377     }
378     
379     return ret;
380 }
381
382 /**************************************************************************
383  *              GetNextDriver (USER.257)
384  */
385 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
386 {
387     HDRVR16             hRetDrv = 0;
388     LPWINE_DRIVER       lpDrv;
389     
390     TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
391     
392     if (hDrvr == 0) {
393         if (lpDrvItemList == NULL) {
394             FIXME("drivers list empty !\n");
395             /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ? 
396              * I (EPP) don't quite understand this 
397              */
398             if (lpDrvItemList == NULL) 
399                 return 0;
400         }
401         lpDrv = lpDrvItemList;
402         if (dwFlags & GND_REVERSE) {
403             while (lpDrv->lpNextItem)
404                 lpDrv = lpDrv->lpNextItem;
405         }
406     } else {
407         if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
408             if (dwFlags & GND_REVERSE) {
409                 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
410             } else {
411                 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
412             }
413         }
414     }
415     
416     hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
417     TRACE("return %04x !\n", hRetDrv);
418     return hRetDrv;
419 }
420