Shell_GetCachedImageIndex32[A|W] implemented.
[wine/multimedia.git] / windows / driver.c
blob945f4a73500a56f2fedcc5781101491532dc5f1e
1 /*
2 * WINE Drivers functions
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1998 Marcus Meissner
6 */
8 #include "wine/winuser16.h"
9 #include "heap.h"
10 #include "win.h"
11 #include "callback.h"
12 #include "driver.h"
13 #include "module.h"
14 #include "debug.h"
15 #include <string.h>
17 LPDRIVERITEM lpDrvItemList = NULL;
18 LPDRIVERITEM32A lpDrvItemList32 = NULL;
20 /**************************************************************************
21 * LoadStartupDrivers
23 static void LoadStartupDrivers(void)
25 HDRVR16 hDrv;
26 char str[256];
27 LPSTR ptr;
29 if (GetPrivateProfileString32A( "drivers", NULL, "", str, sizeof(str),
30 "SYSTEM.INI" ) < 2)
32 ERR( driver,"Can't find drivers section in system.ini\n" );
33 return;
36 ptr = str;
37 while (lstrlen32A( ptr ) != 0)
39 TRACE(driver, "str='%s'\n", ptr );
40 hDrv = OpenDriver16( ptr, "drivers", 0L );
41 TRACE(driver, "hDrv=%04x\n", hDrv );
42 ptr += lstrlen32A(ptr) + 1;
44 TRACE(driver, "end of list !\n" );
45 return;
48 /**************************************************************************
49 * SendDriverMessage [USER.251]
51 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
52 LPARAM lParam2)
54 LPDRIVERITEM lpdrv;
55 LRESULT retval;
57 TRACE(driver, "(%04x, %04X, %08lX, %08lX)\n",
58 hDriver, msg, lParam1, lParam2 );
60 lpdrv = (LPDRIVERITEM)GlobalLock16( hDriver );
61 if (lpdrv == NULL || lpdrv->dis.hDriver != hDriver)
63 GlobalUnlock16( hDriver );
64 return 0;
67 retval = Callbacks->CallDriverProc( lpdrv->lpDrvProc, 0L /* FIXME */,
68 hDriver, msg, lParam1, lParam2 );
70 TRACE(driver, "retval = %ld\n", retval );
72 GlobalUnlock16( hDriver );
73 return retval;
76 /**************************************************************************
77 * SendDriverMessage [WINMM.19]
79 LRESULT WINAPI SendDriverMessage32(HDRVR32 hDriver, UINT32 msg, LPARAM lParam1,
80 LPARAM lParam2)
82 LPDRIVERITEM32A lpdrv;
83 LRESULT retval;
85 TRACE(driver, "(%04x, %04X, %08lX, %08lX)\n",
86 hDriver, msg, lParam1, lParam2 );
88 lpdrv = (LPDRIVERITEM32A)hDriver;
89 if (!lpdrv)
90 return 0;
92 retval = lpdrv->driverproc(lpdrv->dis.hDriver,hDriver,msg,lParam1,lParam2);
94 TRACE(driver, "retval = %ld\n", retval );
96 return retval;
99 /**************************************************************************
100 * OpenDriver [USER.252]
102 HDRVR16 WINAPI OpenDriver16(
103 LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam
105 HDRVR16 hDrvr;
106 LPDRIVERITEM lpdrv, lpnewdrv;
107 char DrvName[128];
109 TRACE(driver,"('%s', '%s', %08lX);\n",
110 lpDriverName, lpSectionName, lParam );
112 if (lpSectionName == NULL) {
113 lstrcpyn32A(DrvName,lpDriverName,sizeof(DrvName));
114 FIXME(driver,"sectionname NULL, assuming directfilename (%s)\n",lpDriverName);
115 } else {
116 GetPrivateProfileString32A( lpSectionName, lpDriverName, "", DrvName,
117 sizeof(DrvName), "SYSTEM.INI" );
119 TRACE(driver,"DrvName='%s'\n", DrvName );
120 if (lstrlen32A(DrvName) < 1) return 0;
122 lpdrv = lpDrvItemList;
123 while (lpdrv) /* XXX find it... like this? */
125 if (!strcasecmp( lpDriverName, lpdrv->dis.szAliasName ))
127 lpdrv->count++;
128 SendDriverMessage16( lpdrv->dis.hDriver, DRV_OPEN, 0L, lParam );
129 return lpdrv->dis.hDriver;
131 lpdrv = lpdrv->lpNextItem;
134 lpdrv = lpDrvItemList; /* find end of list */
135 if (lpdrv != NULL)
136 while (lpdrv->lpNextItem != NULL)
137 lpdrv = lpdrv->lpNextItem;
139 hDrvr = GlobalAlloc16( GMEM_MOVEABLE, sizeof(DRIVERITEM) );
140 lpnewdrv = (LPDRIVERITEM)GlobalLock16( hDrvr );
141 if (lpnewdrv == NULL) return 0;
142 lpnewdrv->dis.length = sizeof( DRIVERINFOSTRUCT16 );
143 lpnewdrv->dis.hModule = LoadModule16( DrvName, (LPVOID)-1 );
144 if (!lpnewdrv->dis.hModule)
146 GlobalUnlock16( hDrvr );
147 GlobalFree16( hDrvr );
148 return 0;
150 lpnewdrv->dis.hDriver = hDrvr;
151 lstrcpy32A( lpnewdrv->dis.szAliasName, lpDriverName );
152 lpnewdrv->count = 1;
153 if (!(lpnewdrv->lpDrvProc = (DRIVERPROC16)WIN32_GetProcAddress16(lpnewdrv->dis.hModule, "DRIVERPROC" )))
155 FreeModule16( lpnewdrv->dis.hModule );
156 GlobalUnlock16( hDrvr );
157 GlobalFree16( hDrvr );
158 return 0;
161 lpnewdrv->lpNextItem = NULL;
162 if (lpDrvItemList == NULL)
164 lpDrvItemList = lpnewdrv;
165 lpnewdrv->lpPrevItem = NULL;
167 else
169 lpdrv->lpNextItem = lpnewdrv;
170 lpnewdrv->lpPrevItem = lpdrv;
173 SendDriverMessage16( hDrvr, DRV_LOAD, 0L, lParam );
174 SendDriverMessage16( hDrvr, DRV_ENABLE, 0L, lParam );
175 SendDriverMessage16( hDrvr, DRV_OPEN, 0L, lParam );
177 TRACE(driver, "hDrvr=%04x loaded !\n", hDrvr );
178 return hDrvr;
181 /**************************************************************************
182 * OpenDriver [WINMM.15]
183 * (0,1,DRV_LOAD ,0 ,0)
184 * (0,1,DRV_ENABLE,0 ,0)
185 * (0,1,DRV_OPEN ,buf[256],0)
187 HDRVR32 WINAPI OpenDriver32A(
188 LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam
190 HDRVR32 hDrvr;
191 LPDRIVERITEM32A lpdrv, lpnewdrv;
192 char DrvName[128],*tmpbuf;
194 TRACE(driver,"('%s', '%s', %08lX);\n",
195 lpDriverName,lpSectionName,lParam );
197 if (lpSectionName == NULL) {
198 lstrcpyn32A(DrvName,lpDriverName,sizeof(DrvName));
199 FIXME(driver,"sectionname NULL, assuming directfilename (%s)\n",lpDriverName);
200 } else {
201 GetPrivateProfileString32A( lpSectionName, lpDriverName, "", DrvName,
202 sizeof(DrvName), "system.ini" );
204 TRACE(driver,"DrvName='%s'\n", DrvName );
205 if (lstrlen32A(DrvName) < 1) return 0;
207 lpdrv = lpDrvItemList32;
208 while (lpdrv) {
209 if (!strcasecmp( lpDriverName, lpdrv->dis.szAliasName )) {
210 lpdrv->count++;
211 lpdrv->dis.hDriver = SendDriverMessage32( lpdrv->dis.hDriver, DRV_OPEN, (LPARAM)""/*FIXME*/, 0L );
212 return (HDRVR32)lpdrv;
214 lpdrv = lpdrv->next;
217 hDrvr = (HDRVR32)HeapAlloc(SystemHeap,0, sizeof(DRIVERITEM32A) );
218 if (!hDrvr) {
219 ERR(driver,"out of memory");
220 return 0;
222 lpnewdrv = (DRIVERITEM32A*)hDrvr;
223 lpnewdrv->dis.length = sizeof( DRIVERINFOSTRUCT32A );
224 lpnewdrv->dis.hModule = LoadLibrary32A( DrvName );
225 if (!lpnewdrv->dis.hModule) {
226 FIXME(driver,"could not load library %s\n",DrvName);
227 HeapFree( SystemHeap,0,(LPVOID)hDrvr );
228 return 0;
230 lstrcpy32A( lpnewdrv->dis.szAliasName, lpDriverName );
231 lpnewdrv->count = 1;
232 if (!(lpnewdrv->driverproc = (DRIVERPROC32)GetProcAddress32(lpnewdrv->dis.hModule, "DriverProc" )))
234 FIXME(driver,"no 'DriverProc' found in %s\n",DrvName);
235 FreeModule32( lpnewdrv->dis.hModule );
236 HeapFree( SystemHeap,0, (LPVOID)hDrvr );
237 return 0;
240 lpnewdrv->next = lpDrvItemList32;
241 lpDrvItemList32 = lpnewdrv;
243 SendDriverMessage32( hDrvr, DRV_LOAD, 0L, lParam );
244 SendDriverMessage32( hDrvr, DRV_ENABLE, 0L, lParam );
245 tmpbuf = HeapAlloc(SystemHeap,0,256);
246 lpnewdrv->dis.hDriver=SendDriverMessage32(hDrvr,DRV_OPEN,(LPARAM)tmpbuf,lParam);
247 HeapFree(SystemHeap,0,tmpbuf);
248 TRACE(driver, "hDrvr=%04x loaded !\n", hDrvr );
249 return hDrvr;
252 /**************************************************************************
253 * OpenDriver [WINMM.15]
255 HDRVR32 WINAPI OpenDriver32W(
256 LPCWSTR lpDriverName, LPCWSTR lpSectionName, LPARAM lParam
258 LPSTR dn = HEAP_strdupWtoA(GetProcessHeap(),0,lpDriverName);
259 LPSTR sn = HEAP_strdupWtoA(GetProcessHeap(),0,lpSectionName);
260 HDRVR32 ret = OpenDriver32A(dn,sn,lParam);
262 if (dn) HeapFree(GetProcessHeap(),0,dn);
263 if (sn) HeapFree(GetProcessHeap(),0,sn);
264 return ret;
267 /**************************************************************************
268 * CloseDriver [USER.253]
270 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
272 LPDRIVERITEM lpdrv;
274 TRACE(driver, "(%04x, %08lX, %08lX);\n",
275 hDrvr, lParam1, lParam2 );
277 lpdrv = (LPDRIVERITEM)GlobalLock16( hDrvr );
278 if (lpdrv != NULL && lpdrv->dis.hDriver == hDrvr)
280 SendDriverMessage16( hDrvr, DRV_CLOSE, lParam1, lParam2 );
281 if (--lpdrv->count == 0)
283 SendDriverMessage16( hDrvr, DRV_DISABLE, lParam1, lParam2 );
284 SendDriverMessage16( hDrvr, DRV_FREE, lParam1, lParam2 );
286 if (lpdrv->lpPrevItem)
287 lpdrv->lpPrevItem->lpNextItem = lpdrv->lpNextItem;
288 else
289 lpDrvItemList = lpdrv->lpNextItem;
290 if (lpdrv->lpNextItem)
291 lpdrv->lpNextItem->lpPrevItem = lpdrv->lpPrevItem;
293 FreeModule16( lpdrv->dis.hModule );
294 GlobalUnlock16( hDrvr );
295 GlobalFree16( hDrvr );
298 TRACE(driver, "hDrvr=%04x closed !\n",
299 hDrvr );
300 return TRUE;
302 return FALSE;
305 /**************************************************************************
306 * GetDriverModuleHandle [USER.254]
308 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
310 LPDRIVERITEM lpdrv;
311 HMODULE16 hModule = 0;
313 TRACE(driver, "(%04x);\n", hDrvr);
315 lpdrv = (LPDRIVERITEM)GlobalLock16( hDrvr );
316 if (lpdrv != NULL && lpdrv->dis.hDriver == hDrvr)
318 hModule = lpdrv->dis.hModule;
319 GlobalUnlock16( hDrvr );
321 return hModule;
324 /**************************************************************************
325 * GetDriverModuleHandle [USER.254]
327 HMODULE32 WINAPI GetDriverModuleHandle32(HDRVR32 hDrvr)
329 LPDRIVERITEM32A lpdrv = (LPDRIVERITEM32A)hDrvr;
331 TRACE(driver, "(%04x);\n", hDrvr);
332 if (!lpdrv)
333 return 0;
334 return lpdrv->dis.hModule;
337 /**************************************************************************
338 * DefDriverProc16 [USER.255]
340 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
341 LPARAM lParam1, LPARAM lParam2)
343 switch(wMsg)
345 case DRV_LOAD:
346 return (LRESULT)0L;
347 case DRV_FREE:
348 return (LRESULT)0L;
349 case DRV_OPEN:
350 return (LRESULT)0L;
351 case DRV_CLOSE:
352 return (LRESULT)0L;
353 case DRV_ENABLE:
354 return (LRESULT)0L;
355 case DRV_DISABLE:
356 return (LRESULT)0L;
357 case DRV_QUERYCONFIGURE:
358 return (LRESULT)0L;
360 case DRV_CONFIGURE:
361 MessageBox16( 0, "Driver isn't configurable !", "Wine Driver", MB_OK );
362 return (LRESULT)0L;
364 case DRV_INSTALL:
365 return (LRESULT)DRVCNF_RESTART;
367 case DRV_REMOVE:
368 return (LRESULT)DRVCNF_RESTART;
370 default:
371 return (LRESULT)0L;
375 /**************************************************************************
376 * GetDriverInfo [USER.256]
378 BOOL16 WINAPI GetDriverInfo(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
380 LPDRIVERITEM lpdrv;
382 TRACE(driver, "(%04x, %p);\n", hDrvr, lpDrvInfo );
384 if (lpDrvInfo == NULL) return FALSE;
386 lpdrv = (LPDRIVERITEM)GlobalLock16( hDrvr );
387 if (lpdrv == NULL) return FALSE;
388 memcpy( lpDrvInfo, &lpdrv->dis, sizeof(DRIVERINFOSTRUCT16) );
389 GlobalUnlock16( hDrvr );
391 return TRUE;
394 /**************************************************************************
395 * GetNextDriver [USER.257]
397 HDRVR16 WINAPI GetNextDriver(HDRVR16 hDrvr, DWORD dwFlags)
399 LPDRIVERITEM lpdrv;
400 HDRVR16 hRetDrv = 0;
402 TRACE(driver, "(%04x, %08lX);\n", hDrvr, dwFlags );
404 if (hDrvr == 0)
406 if (lpDrvItemList == NULL)
408 TRACE(driver, "drivers list empty !\n");
409 LoadStartupDrivers();
410 if (lpDrvItemList == NULL) return 0;
412 TRACE(driver,"return first %04x !\n",
413 lpDrvItemList->dis.hDriver );
414 return lpDrvItemList->dis.hDriver;
417 lpdrv = (LPDRIVERITEM)GlobalLock16( hDrvr );
418 if (lpdrv != NULL)
420 if (dwFlags & GND_REVERSE)
422 if (lpdrv->lpPrevItem)
423 hRetDrv = lpdrv->lpPrevItem->dis.hDriver;
425 else
427 if (lpdrv->lpNextItem)
428 hRetDrv = lpdrv->lpNextItem->dis.hDriver;
430 GlobalUnlock16( hDrvr );
433 TRACE(driver, "return %04x !\n", hRetDrv );
434 return hRetDrv;