Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / windows / driver.c
blob696d83e7ac942dbb3ae1d3a4bb9d55441775e33b
1 /*
2 * WINE Drivers functions
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1998 Marcus Meissner
6 * Copyright 1999,2000 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * TODO:
23 * - LoadModule count and clean up is not handled correctly (it's not a
24 * problem as long as FreeLibrary is not working correctly)
27 #include <stdarg.h>
28 #include <string.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wine/winbase16.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "wownt32.h"
36 #include "mmddk.h"
37 #include "wine/mmsystem16.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(driver);
42 typedef struct tagWINE_DRIVER
44 char szAliasName[128];
45 /* as usual LPWINE_DRIVER == hDriver32 */
46 HDRVR16 hDriver16;
47 HMODULE16 hModule16;
48 DRIVERPROC16 lpDrvProc;
49 DWORD dwDriverID;
50 struct tagWINE_DRIVER* lpPrevItem;
51 struct tagWINE_DRIVER* lpNextItem;
52 } WINE_DRIVER, *LPWINE_DRIVER;
54 static LPWINE_DRIVER lpDrvItemList = NULL;
57 /**************************************************************************
58 * LoadStartupDrivers [internal]
60 #if 0
61 static void DRIVER_LoadStartupDrivers(void)
63 char str[256];
65 if (GetPrivateProfileStringA("drivers", NULL, "", str, sizeof(str), "SYSTEM.INI") < 2) {
66 ERR("Can't find drivers section in system.ini\n");
67 } else {
68 HDRVR16 hDrv;
69 LPSTR ptr;
71 for (ptr = str; *ptr; ptr += strlen(ptr) + 1) {
72 TRACE("str='%s'\n", ptr);
73 hDrv = OpenDriver16(ptr, "drivers", 0L);
74 TRACE("hDrv=%04x\n", hDrv);
76 TRACE("end of list !\n");
79 #endif
81 /**************************************************************************
82 * DRIVER_GetNumberOfModuleRefs [internal]
84 * Returns the number of open drivers which share the same module.
86 static WORD DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv)
88 LPWINE_DRIVER lpDrv;
89 WORD count = 0;
91 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
92 if (lpDrv->hModule16 == lpNewDrv->hModule16) {
93 count++;
96 return count;
99 /**************************************************************************
100 * DRIVER_FindFromHDrvr16 [internal]
102 * From a hDrvr being 16 bits, returns the WINE internal structure.
104 static LPWINE_DRIVER DRIVER_FindFromHDrvr16(HDRVR16 hDrvr)
106 LPWINE_DRIVER lpDrv;
108 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
109 if (lpDrv->hDriver16 == hDrvr) {
110 break;
113 return lpDrv;
116 /**************************************************************************
117 * DRIVER_SendMessage [internal]
119 static LRESULT inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT16 msg,
120 LPARAM lParam1, LPARAM lParam2)
122 WORD args[8];
123 LRESULT ret;
125 TRACE("Before CallDriverProc proc=%p driverID=%08lx wMsg=%04x p1=%08lx p2=%08lx\n",
126 lpDrv->lpDrvProc, lpDrv->dwDriverID, msg, lParam1, lParam2);
128 args[7] = HIWORD(lpDrv->dwDriverID);
129 args[6] = LOWORD(lpDrv->dwDriverID);
130 args[5] = lpDrv->hDriver16;
131 args[4] = msg;
132 args[3] = HIWORD(lParam1);
133 args[2] = LOWORD(lParam1);
134 args[1] = HIWORD(lParam2);
135 args[0] = LOWORD(lParam2);
136 WOWCallback16Ex( (DWORD)lpDrv->lpDrvProc, WCB16_PASCAL, sizeof(args), args, &ret );
137 return ret;
140 /**************************************************************************
141 * SendDriverMessage (USER.251)
143 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
144 LPARAM lParam2)
146 LPWINE_DRIVER lpDrv;
147 LRESULT retval = 0;
149 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
151 if ((lpDrv = DRIVER_FindFromHDrvr16(hDriver)) != NULL) {
152 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
153 } else {
154 WARN("Bad driver handle %u\n", hDriver);
157 TRACE("retval = %ld\n", retval);
158 return retval;
161 /**************************************************************************
162 * DRIVER_RemoveFromList [internal]
164 * Generates all the logic to handle driver closure / deletion
165 * Removes a driver struct to the list of open drivers.
167 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
169 lpDrv->dwDriverID = 0;
170 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
171 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
172 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
175 if (lpDrv->lpPrevItem)
176 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
177 else
178 lpDrvItemList = lpDrv->lpNextItem;
179 if (lpDrv->lpNextItem)
180 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
182 return TRUE;
185 /**************************************************************************
186 * DRIVER_AddToList [internal]
188 * Adds a driver struct to the list of open drivers.
189 * Generates all the logic to handle driver creation / open.
191 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
193 /* First driver to be loaded for this module, need to load correctly the module */
194 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
195 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
196 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
197 return FALSE;
199 /* returned value is not checked */
200 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
203 lpNewDrv->lpNextItem = NULL;
204 if (lpDrvItemList == NULL) {
205 lpDrvItemList = lpNewDrv;
206 lpNewDrv->lpPrevItem = NULL;
207 } else {
208 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
209 while (lpDrv->lpNextItem != NULL)
210 lpDrv = lpDrv->lpNextItem;
212 lpDrv->lpNextItem = lpNewDrv;
213 lpNewDrv->lpPrevItem = lpDrv;
215 /* Now just open a new instance of a driver on this module */
216 lpNewDrv->dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
218 if (lpNewDrv->dwDriverID == 0) {
219 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
220 DRIVER_RemoveFromList(lpNewDrv);
221 return FALSE;
224 return TRUE;
227 /**************************************************************************
228 * DRIVER_TryOpenDriver16 [internal]
230 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
232 static LPWINE_DRIVER DRIVER_TryOpenDriver16(LPCSTR lpFileName, LPARAM lParam2)
234 static WORD DRIVER_hDrvr16Counter /* = 0 */;
235 LPWINE_DRIVER lpDrv = NULL;
236 HMODULE16 hModule;
237 DRIVERPROC16 lpProc;
238 LPCSTR lpSFN;
239 LPSTR ptr;
241 TRACE("('%s', %08lX);\n", lpFileName, lParam2);
243 if (strlen(lpFileName) < 1) return lpDrv;
245 if ((lpSFN = strrchr(lpFileName, '\\')) == NULL)
246 lpSFN = lpFileName;
247 else
248 lpSFN++;
249 if ((ptr = strchr(lpFileName, ' ')) != NULL) {
250 *ptr++ = '\0';
251 while (*ptr == ' ') ptr++;
252 if (*ptr == '\0') ptr = NULL;
255 if ((hModule = LoadLibrary16(lpFileName)) < 32) goto exit;
256 if ((lpProc = (DRIVERPROC16)GetProcAddress16(hModule, "DRIVERPROC")) == NULL)
257 goto exit;
259 if ((lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER))) == NULL)
260 goto exit;
262 lpDrv->dwDriverID = 0;
263 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
264 lpDrv->hDriver16 = DRIVER_hDrvr16Counter;
265 lstrcpynA(lpDrv->szAliasName, lpSFN, sizeof(lpDrv->szAliasName));
266 lpDrv->hModule16 = hModule;
267 lpDrv->lpDrvProc = lpProc;
269 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) goto exit;
271 return lpDrv;
272 exit:
273 TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName, hModule);
274 if (hModule >= 32) FreeLibrary16(hModule);
275 HeapFree(GetProcessHeap(), 0, lpDrv);
276 return NULL;
279 /**************************************************************************
280 * OpenDriver (USER.252)
282 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
284 LPWINE_DRIVER lpDrv = NULL;
285 char drvName[128];
287 TRACE("(%s, %s, %08lX);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
289 if (!lpDriverName || !*lpDriverName) return 0;
291 if (lpSectionName == NULL) {
292 strcpy(drvName, lpDriverName);
294 if ((lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2)))
295 goto the_end;
296 /* in case hDriver is NULL, search in Drivers section */
297 lpSectionName = "Drivers";
299 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "",
300 drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
301 lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2);
303 if (!lpDrv) {
304 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName));
305 return 0;
307 the_end:
308 TRACE("=> %04x / %08lx\n", lpDrv->hDriver16, (DWORD)lpDrv);
309 return lpDrv->hDriver16;
312 /**************************************************************************
313 * CloseDriver (USER.253)
315 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
317 LPWINE_DRIVER lpDrv;
319 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
321 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
322 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
324 if (DRIVER_RemoveFromList(lpDrv)) {
325 HeapFree(GetProcessHeap(), 0, lpDrv);
326 return TRUE;
329 WARN("Failed to close driver\n");
330 return FALSE;
333 /**************************************************************************
334 * GetDriverModuleHandle (USER.254)
336 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
338 LPWINE_DRIVER lpDrv;
339 HMODULE16 hModule = 0;
341 TRACE("(%04x);\n", hDrvr);
343 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
344 hModule = lpDrv->hModule16;
346 TRACE("=> %04x\n", hModule);
347 return hModule;
350 /**************************************************************************
351 * DefDriverProc (USER.255)
353 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
354 LPARAM lParam1, LPARAM lParam2)
356 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
357 dwDevID, hDriv, wMsg, lParam1, lParam2);
359 switch(wMsg) {
360 case DRV_LOAD:
361 case DRV_FREE:
362 case DRV_ENABLE:
363 case DRV_DISABLE:
364 return (LRESULT)1L;
365 case DRV_OPEN:
366 case DRV_CLOSE:
367 case DRV_QUERYCONFIGURE:
368 return (LRESULT)0L;
369 case DRV_CONFIGURE:
370 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
371 return (LRESULT)0L;
372 case DRV_INSTALL:
373 case DRV_REMOVE:
374 return DRV_SUCCESS;
375 default:
376 return (LRESULT)0L;
380 /**************************************************************************
381 * GetDriverInfo (USER.256)
383 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
385 LPWINE_DRIVER lpDrv;
386 BOOL16 ret = FALSE;
388 TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
390 if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16))
391 return FALSE;
393 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
394 lpDrvInfo->hDriver = lpDrv->hDriver16;
395 lpDrvInfo->hModule = lpDrv->hModule16;
396 lstrcpynA(lpDrvInfo->szAliasName, lpDrv->szAliasName, sizeof(lpDrvInfo->szAliasName));
397 ret = TRUE;
400 return ret;
403 /**************************************************************************
404 * GetNextDriver (USER.257)
406 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
408 HDRVR16 hRetDrv = 0;
409 LPWINE_DRIVER lpDrv;
411 TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
413 if (hDrvr == 0) {
414 if (lpDrvItemList == NULL) {
415 FIXME("drivers list empty !\n");
416 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
417 * I (EPP) don't quite understand this
419 if (lpDrvItemList == NULL)
420 return 0;
422 lpDrv = lpDrvItemList;
423 if (dwFlags & GND_REVERSE) {
424 while (lpDrv->lpNextItem)
425 lpDrv = lpDrv->lpNextItem;
427 } else {
428 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
429 if (dwFlags & GND_REVERSE) {
430 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
431 } else {
432 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
437 hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
438 TRACE("return %04x !\n", hRetDrv);
439 return hRetDrv;