Release 970415
[wine/multimedia.git] / windows / driver.c
blob4eb08806515715e271eaa3784ae5af767c389749
1 /*
2 * Wine Drivers functions
4 * Copyright 1994 Martin Ayotte
5 */
7 #include <stdio.h>
8 #include "windows.h"
9 #include "win.h"
10 #include "callback.h"
11 #include "driver.h"
12 #include "module.h"
13 #include "stddebug.h"
14 #include "debug.h"
16 LPDRIVERITEM lpDrvItemList = NULL;
18 void LoadStartupDrivers(void)
20 HDRVR16 hDrv;
21 char str[256];
22 LPSTR ptr;
24 if (GetPrivateProfileString32A( "drivers", NULL, "", str, sizeof(str),
25 "SYSTEM.INI" ) < 2)
27 fprintf( stderr,
28 "LoadStartupDrivers // can't find drivers section in system.ini\n" );
29 return;
32 ptr = str;
33 while (lstrlen32A( ptr ) != 0)
35 dprintf_driver( stddeb, "LoadStartupDrivers // str='%s'\n", ptr );
36 hDrv = OpenDriver( ptr, "drivers", 0L );
37 dprintf_driver( stddeb, "LoadStartupDrivers // hDrv=%04x\n", hDrv );
38 ptr += lstrlen32A(ptr) + 1;
40 dprintf_driver( stddeb, "LoadStartupDrivers // end of list !\n" );
42 return;
45 /**************************************************************************
46 * SendDriverMessage [USER.251]
48 LRESULT SendDriverMessage(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
49 LPARAM lParam2)
51 LPDRIVERITEM lpdrv;
52 LRESULT retval;
54 dprintf_driver( stddeb, "SendDriverMessage(%04x, %04X, %08lX, %08lX)\n",
55 hDriver, msg, lParam1, lParam2 );
57 lpdrv = (LPDRIVERITEM)GlobalLock16( hDriver );
58 if (lpdrv == NULL || lpdrv->dis.hDriver != hDriver)
60 GlobalUnlock16( hDriver );
61 return 0;
64 retval = CallDriverProc( lpdrv->lpDrvProc, 0L /* FIXME */, hDriver, msg,
65 lParam1, lParam2 );
67 dprintf_driver( stddeb, "SendDriverMessage // retval = %ld\n", retval );
69 GlobalUnlock16( hDriver );
70 return retval;
73 /**************************************************************************
74 * OpenDriver [USER.252]
76 HDRVR16 OpenDriver(LPSTR lpDriverName, LPSTR lpSectionName, LPARAM lParam)
78 HDRVR16 hDrvr;
79 LPDRIVERITEM lpdrv, lpnewdrv;
80 char DrvName[128];
81 WORD ordinal;
83 dprintf_driver( stddeb,"OpenDriver('%s', '%s', %08lX);\n",
84 lpDriverName, lpSectionName, lParam );
86 if (lpSectionName == NULL) lpSectionName = "drivers";
87 GetPrivateProfileString32A( lpSectionName, lpDriverName, "", DrvName,
88 sizeof(DrvName), "SYSTEM.INI" );
89 dprintf_driver( stddeb,"OpenDriver // DrvName='%s'\n", DrvName );
90 if (lstrlen32A(DrvName) < 1) return 0;
92 lpdrv = lpDrvItemList;
93 while (lpdrv) /* XXX find it... like this? */
95 if (!lstrcmpi32A( lpDriverName, lpdrv->dis.szAliasName ))
97 lpdrv->count++;
98 SendDriverMessage( lpdrv->dis.hDriver, DRV_OPEN, 0L, lParam );
99 return lpdrv->dis.hDriver;
101 lpdrv = lpdrv->lpNextItem;
104 lpdrv = lpDrvItemList; /* find end of list */
105 if (lpdrv != NULL)
106 while (lpdrv->lpNextItem != NULL)
107 lpdrv = lpdrv->lpNextItem;
109 hDrvr = GlobalAlloc16( GMEM_MOVEABLE, sizeof(DRIVERITEM) );
110 lpnewdrv = (LPDRIVERITEM)GlobalLock16( hDrvr );
111 if (lpnewdrv == NULL) return 0;
112 lpnewdrv->dis.length = sizeof( DRIVERINFOSTRUCT16 );
113 lpnewdrv->dis.hModule = LoadModule16( DrvName, (LPVOID)-1 );
114 if (!lpnewdrv->dis.hModule)
116 GlobalUnlock16( hDrvr );
117 GlobalFree16( hDrvr );
118 return 0;
120 lpnewdrv->dis.hDriver = hDrvr;
121 lstrcpy32A( lpnewdrv->dis.szAliasName, lpDriverName );
122 lpnewdrv->count = 1;
123 ordinal = MODULE_GetOrdinal( lpnewdrv->dis.hModule, "DRIVERPROC" );
124 if (!ordinal ||
125 !(lpnewdrv->lpDrvProc = (DRIVERPROC16)MODULE_GetEntryPoint(
126 lpnewdrv->dis.hModule, ordinal )))
128 FreeModule16( lpnewdrv->dis.hModule );
129 GlobalUnlock16( hDrvr );
130 GlobalFree16( hDrvr );
131 return 0;
134 lpnewdrv->lpNextItem = NULL;
135 if (lpDrvItemList == NULL)
137 lpDrvItemList = lpnewdrv;
138 lpnewdrv->lpPrevItem = NULL;
140 else
142 lpdrv->lpNextItem = lpnewdrv;
143 lpnewdrv->lpPrevItem = lpdrv;
146 SendDriverMessage( hDrvr, DRV_LOAD, 0L, lParam );
147 SendDriverMessage( hDrvr, DRV_ENABLE, 0L, lParam );
148 SendDriverMessage( hDrvr, DRV_OPEN, 0L, lParam );
150 dprintf_driver( stddeb, "OpenDriver // hDrvr=%04x loaded !\n", hDrvr );
151 return hDrvr;
154 /**************************************************************************
155 * CloseDriver [USER.253]
157 LRESULT CloseDriver(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
159 LPDRIVERITEM lpdrv;
161 dprintf_driver( stddeb, "CloseDriver(%04x, %08lX, %08lX);\n",
162 hDrvr, lParam1, lParam2 );
164 lpdrv = (LPDRIVERITEM)GlobalLock16( hDrvr );
165 if (lpdrv != NULL && lpdrv->dis.hDriver == hDrvr)
167 SendDriverMessage( hDrvr, DRV_CLOSE, lParam1, lParam2 );
168 if (--lpdrv->count == 0)
170 SendDriverMessage( hDrvr, DRV_DISABLE, lParam1, lParam2 );
171 SendDriverMessage( hDrvr, DRV_FREE, lParam1, lParam2 );
173 if (lpdrv->lpPrevItem)
174 lpdrv->lpPrevItem->lpNextItem = lpdrv->lpNextItem;
175 else
176 lpDrvItemList = lpdrv->lpNextItem;
177 if (lpdrv->lpNextItem)
178 lpdrv->lpNextItem->lpPrevItem = lpdrv->lpPrevItem;
180 FreeModule16( lpdrv->dis.hModule );
181 GlobalUnlock16( hDrvr );
182 GlobalFree16( hDrvr );
185 dprintf_driver( stddeb, "CloseDriver // hDrvr=%04x closed !\n",
186 hDrvr );
187 return TRUE;
189 return FALSE;
192 /**************************************************************************
193 * GetDriverModuleHandle [USER.254]
195 HMODULE16 GetDriverModuleHandle(HDRVR16 hDrvr)
197 LPDRIVERITEM lpdrv;
198 HMODULE16 hModule = 0;
200 dprintf_driver( stddeb, "GetDriverModuleHandle(%04x);\n", hDrvr);
202 lpdrv = (LPDRIVERITEM)GlobalLock16( hDrvr );
203 if (lpdrv != NULL && lpdrv->dis.hDriver == hDrvr)
205 hModule = lpdrv->dis.hModule;
206 GlobalUnlock16( hDrvr );
208 return hModule;
211 /**************************************************************************
212 * DefDriverProc [USER.255]
214 LRESULT DefDriverProc(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
215 LPARAM lParam1, LPARAM lParam2)
217 switch(wMsg)
219 case DRV_LOAD:
220 return (LRESULT)0L;
221 case DRV_FREE:
222 return (LRESULT)0L;
223 case DRV_OPEN:
224 return (LRESULT)0L;
225 case DRV_CLOSE:
226 return (LRESULT)0L;
227 case DRV_ENABLE:
228 return (LRESULT)0L;
229 case DRV_DISABLE:
230 return (LRESULT)0L;
231 case DRV_QUERYCONFIGURE:
232 return (LRESULT)0L;
234 case DRV_CONFIGURE:
235 MessageBox16( 0, "Driver isn't configurable !", "Wine Driver", MB_OK );
236 return (LRESULT)0L;
238 case DRV_INSTALL:
239 return (LRESULT)DRVCNF_RESTART;
241 case DRV_REMOVE:
242 return (LRESULT)DRVCNF_RESTART;
244 default:
245 return (LRESULT)0L;
249 /**************************************************************************
250 * GetDriverInfo [USER.256]
252 BOOL16 GetDriverInfo(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
254 LPDRIVERITEM lpdrv;
256 dprintf_driver( stddeb, "GetDriverInfo(%04x, %p);\n", hDrvr, lpDrvInfo );
258 if (lpDrvInfo == NULL) return FALSE;
260 lpdrv = (LPDRIVERITEM)GlobalLock16( hDrvr );
261 if (lpdrv == NULL) return FALSE;
262 memcpy( lpDrvInfo, &lpdrv->dis, sizeof(DRIVERINFOSTRUCT16) );
263 GlobalUnlock16( hDrvr );
265 return TRUE;
268 /**************************************************************************
269 * GetNextDriver [USER.257]
271 HDRVR16 GetNextDriver(HDRVR16 hDrvr, DWORD dwFlags)
273 LPDRIVERITEM lpdrv;
274 HDRVR16 hRetDrv = 0;
276 dprintf_driver( stddeb, "GetNextDriver(%04x, %08lX);\n", hDrvr, dwFlags );
278 if (hDrvr == 0)
280 if (lpDrvItemList == NULL)
282 dprintf_driver(stddeb, "GetNextDriver // drivers list empty !\n");
283 LoadStartupDrivers();
284 if (lpDrvItemList == NULL) return 0;
286 dprintf_driver( stddeb,"GetNextDriver // return first %04x !\n",
287 lpDrvItemList->dis.hDriver );
288 return lpDrvItemList->dis.hDriver;
291 lpdrv = (LPDRIVERITEM)GlobalLock16( hDrvr );
292 if (lpdrv != NULL)
294 if (dwFlags & GND_REVERSE)
296 if (lpdrv->lpPrevItem)
297 hRetDrv = lpdrv->lpPrevItem->dis.hDriver;
299 else
301 if (lpdrv->lpNextItem)
302 hRetDrv = lpdrv->lpNextItem->dis.hDriver;
304 GlobalUnlock16( hDrvr );
307 dprintf_driver( stddeb, "GetNextDriver // return %04x !\n", hRetDrv );
308 return hRetDrv;