2 * Graphics driver management functions
4 * Copyright 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(driver);
36 struct graphics_driver
38 struct graphics_driver *next;
39 struct graphics_driver *prev;
40 HMODULE module; /* module handle */
41 unsigned int count; /* reference count */
45 static struct graphics_driver *first_driver;
46 static struct graphics_driver *display_driver;
48 static CRITICAL_SECTION driver_section;
49 static CRITICAL_SECTION_DEBUG critsect_debug =
51 0, 0, &driver_section,
52 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
53 0, 0, { 0, (DWORD)(__FILE__ ": driver_section") }
55 static CRITICAL_SECTION driver_section = { &critsect_debug, -1, 0, 0, 0, 0 };
57 /**********************************************************************
60 * Allocate and fill the driver structure for a given module.
62 static struct graphics_driver *create_driver( HMODULE module )
64 struct graphics_driver *driver;
66 if (!(driver = HeapAlloc( GetProcessHeap(), 0, sizeof(*driver)))) return NULL;
69 driver->module = module;
72 /* fill the function table */
74 #define GET_FUNC(name) driver->funcs.p##name = (void*)GetProcAddress( module, #name )
83 GET_FUNC(ChoosePixelFormat);
85 GET_FUNC(CloseFigure);
86 GET_FUNC(CreateBitmap);
88 GET_FUNC(CreateDIBSection);
89 GET_FUNC(DeleteBitmap);
91 GET_FUNC(DescribePixelFormat);
92 GET_FUNC(DeviceCapabilities);
97 GET_FUNC(EnumDeviceFonts);
98 GET_FUNC(ExcludeClipRect);
99 GET_FUNC(ExtDeviceMode);
101 GET_FUNC(ExtFloodFill);
102 GET_FUNC(ExtSelectClipRgn);
103 GET_FUNC(ExtTextOut);
106 GET_FUNC(FlattenPath);
108 GET_FUNC(GdiComment);
109 GET_FUNC(GetBitmapBits);
110 GET_FUNC(GetCharWidth);
111 GET_FUNC(GetDCOrgEx);
112 GET_FUNC(GetDIBColorTable);
114 GET_FUNC(GetDeviceCaps);
115 GET_FUNC(GetDeviceGammaRamp);
116 GET_FUNC(GetNearestColor);
118 GET_FUNC(GetPixelFormat);
119 GET_FUNC(GetSystemPaletteEntries);
120 GET_FUNC(GetTextExtentPoint);
121 GET_FUNC(GetTextMetrics);
122 GET_FUNC(IntersectClipRect);
126 GET_FUNC(ModifyWorldTransform);
127 GET_FUNC(OffsetClipRgn);
128 GET_FUNC(OffsetViewportOrg);
129 GET_FUNC(OffsetWindowOrg);
133 GET_FUNC(PolyBezier);
134 GET_FUNC(PolyBezierTo);
136 GET_FUNC(PolyPolygon);
137 GET_FUNC(PolyPolyline);
140 GET_FUNC(PolylineTo);
141 GET_FUNC(RealizeDefaultPalette);
142 GET_FUNC(RealizePalette);
148 GET_FUNC(ScaleViewportExt);
149 GET_FUNC(ScaleWindowExt);
150 GET_FUNC(SelectBitmap);
151 GET_FUNC(SelectBrush);
152 GET_FUNC(SelectClipPath);
153 GET_FUNC(SelectFont);
154 GET_FUNC(SelectPalette);
156 GET_FUNC(SetBitmapBits);
157 GET_FUNC(SetBkColor);
160 GET_FUNC(SetDIBColorTable);
162 GET_FUNC(SetDIBitsToDevice);
163 GET_FUNC(SetDeviceClipping);
164 GET_FUNC(SetDeviceGammaRamp);
165 GET_FUNC(SetMapMode);
166 GET_FUNC(SetMapperFlags);
168 GET_FUNC(SetPixelFormat);
169 GET_FUNC(SetPolyFillMode);
172 GET_FUNC(SetStretchBltMode);
173 GET_FUNC(SetTextAlign);
174 GET_FUNC(SetTextCharacterExtra);
175 GET_FUNC(SetTextColor);
176 GET_FUNC(SetTextJustification);
177 GET_FUNC(SetViewportExt);
178 GET_FUNC(SetViewportOrg);
179 GET_FUNC(SetWindowExt);
180 GET_FUNC(SetWindowOrg);
181 GET_FUNC(SetWorldTransform);
184 GET_FUNC(StretchBlt);
185 GET_FUNC(StretchDIBits);
186 GET_FUNC(StrokeAndFillPath);
187 GET_FUNC(StrokePath);
188 GET_FUNC(SwapBuffers);
192 /* add it to the list */
194 if ((driver->next = first_driver)) driver->next->prev = driver;
195 first_driver = driver;
200 /**********************************************************************
201 * load_display_driver
203 * Special case for loading the display driver: get the name from the config file
205 static struct graphics_driver *load_display_driver(void)
207 char buffer[MAX_PATH];
211 if (display_driver) /* already loaded */
213 display_driver->count++;
214 return display_driver;
217 strcpy( buffer, "x11drv" ); /* default value */
218 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine", &hkey ))
220 DWORD type, count = sizeof(buffer);
221 RegQueryValueExA( hkey, "GraphicsDriver", 0, &type, buffer, &count );
225 if (!(module = LoadLibraryA( buffer )))
227 MESSAGE( "Could not load graphics driver '%s'\n", buffer );
231 if (!(display_driver = create_driver( module )))
233 MESSAGE( "Could not create graphics driver '%s'\n", buffer );
234 FreeLibrary( module );
238 display_driver->count++; /* we don't want to free it */
239 return display_driver;
243 /**********************************************************************
246 const DC_FUNCTIONS *DRIVER_load_driver( LPCWSTR name )
249 struct graphics_driver *driver;
250 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
252 EnterCriticalSection( &driver_section );
254 /* display driver is a special case */
255 if (!strcmpiW( name, displayW ))
257 driver = load_display_driver();
258 LeaveCriticalSection( &driver_section );
259 return &driver->funcs;
262 if ((module = GetModuleHandleW( name )))
264 for (driver = first_driver; driver; driver = driver->next)
266 if (driver->module == module)
269 LeaveCriticalSection( &driver_section );
270 return &driver->funcs;
275 if (!(module = LoadLibraryW( name )))
277 LeaveCriticalSection( &driver_section );
281 if (!(driver = create_driver( module )))
283 FreeLibrary( module );
284 LeaveCriticalSection( &driver_section );
288 TRACE( "loaded driver %p for %s\n", driver, debugstr_w(name) );
289 LeaveCriticalSection( &driver_section );
290 return &driver->funcs;
294 /**********************************************************************
297 * Get a new copy of an existing driver.
299 const DC_FUNCTIONS *DRIVER_get_driver( const DC_FUNCTIONS *funcs )
301 struct graphics_driver *driver;
303 EnterCriticalSection( &driver_section );
304 for (driver = first_driver; driver; driver = driver->next)
305 if (&driver->funcs == funcs) break;
306 if (!driver) ERR( "driver not found, trouble ahead\n" );
308 LeaveCriticalSection( &driver_section );
313 /**********************************************************************
314 * DRIVER_release_driver
316 * Release a driver by decrementing ref count and freeing it if needed.
318 void DRIVER_release_driver( const DC_FUNCTIONS *funcs )
320 struct graphics_driver *driver;
322 EnterCriticalSection( &driver_section );
324 for (driver = first_driver; driver; driver = driver->next)
325 if (&driver->funcs == funcs) break;
327 if (!driver) goto done;
328 if (--driver->count) goto done;
330 /* removed last reference, free it */
331 if (driver->next) driver->next->prev = driver->prev;
332 if (driver->prev) driver->prev->next = driver->next;
333 else first_driver = driver->next;
334 if (driver == display_driver) display_driver = NULL;
336 FreeLibrary( driver->module );
337 HeapFree( GetProcessHeap(), 0, driver );
339 LeaveCriticalSection( &driver_section );
343 /*****************************************************************************
344 * DRIVER_GetDriverName
347 BOOL DRIVER_GetDriverName( LPCWSTR device, LPWSTR driver, DWORD size )
349 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
350 static const WCHAR devicesW[] = { 'd','e','v','i','c','e','s',0 };
351 static const WCHAR empty_strW[] = { 0 };
354 /* display is a special case */
355 if (!strcmpiW( device, displayW ))
357 lstrcpynW( driver, displayW, size );
361 size = GetProfileStringW(devicesW, device, empty_strW, driver, size);
363 WARN("Unable to find %s in [devices] section of win.ini\n", debugstr_w(device));
366 p = strchrW(driver, ',');
369 WARN("%s entry in [devices] section of win.ini is malformed.\n", debugstr_w(device));
373 TRACE("Found %s for %s\n", debugstr_w(driver), debugstr_w(device));
378 /***********************************************************************
379 * GdiConvertToDevmodeW (GDI32.@)
381 DEVMODEW * WINAPI GdiConvertToDevmodeW(const DEVMODEA *dmA)
386 dmW_size = dmA->dmSize + CCHDEVICENAME;
387 if (dmA->dmSize >= (char *)dmA->dmFormName - (char *)dmA + CCHFORMNAME)
388 dmW_size += CCHFORMNAME;
390 dmW = HeapAlloc(GetProcessHeap(), 0, dmW_size + dmA->dmDriverExtra);
391 if (!dmW) return NULL;
393 MultiByteToWideChar(CP_ACP, 0, dmA->dmDeviceName, CCHDEVICENAME,
394 dmW->dmDeviceName, CCHDEVICENAME);
395 /* copy slightly more, to avoid long computations */
396 memcpy(&dmW->dmSpecVersion, &dmA->dmSpecVersion, dmA->dmSize - CCHDEVICENAME);
398 if (dmA->dmSize >= (char *)dmA->dmFormName - (char *)dmA + CCHFORMNAME)
400 MultiByteToWideChar(CP_ACP, 0, dmA->dmFormName, CCHFORMNAME,
401 dmW->dmFormName, CCHFORMNAME);
402 if (dmA->dmSize > (char *)&dmA->dmLogPixels - (char *)dmA)
403 memcpy(&dmW->dmLogPixels, &dmA->dmLogPixels, dmA->dmSize - ((char *)&dmA->dmLogPixels - (char *)dmA));
406 if (dmA->dmDriverExtra)
407 memcpy((char *)dmW + dmW_size, (char *)dmA + dmA->dmSize, dmA->dmDriverExtra);
409 dmW->dmSize = dmW_size;
415 /*****************************************************************************
418 * This should thunk to 16-bit and simply call the proc with the given args.
420 INT WINAPI GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc, HWND hWnd,
421 LPSTR lpModelName, LPSTR OldPort, LPSTR NewPort )
423 FIXME("(%p, %p, %s, %s, %s)\n", lpfnDevInstallProc, hWnd, lpModelName, OldPort, NewPort );
427 /*****************************************************************************
430 * This should load the correct driver for lpszDevice and calls this driver's
431 * ExtDeviceModePropSheet proc.
433 * Note: The driver calls a callback routine for each property sheet page; these
434 * pages are supposed to be filled into the structure pointed to by lpPropSheet.
435 * The layout of this structure is:
441 * HPROPSHEETPAGE pages[10];
444 INT WINAPI GDI_CallExtDeviceModePropSheet16( HWND hWnd, LPCSTR lpszDevice,
445 LPCSTR lpszPort, LPVOID lpPropSheet )
447 FIXME("(%p, %s, %s, %p)\n", hWnd, lpszDevice, lpszPort, lpPropSheet );
451 /*****************************************************************************
454 * This should load the correct driver for lpszDevice and calls this driver's
455 * ExtDeviceMode proc.
457 * FIXME: convert ExtDeviceMode to unicode in the driver interface
459 INT WINAPI GDI_CallExtDeviceMode16( HWND hwnd,
460 LPDEVMODEA lpdmOutput, LPSTR lpszDevice,
461 LPSTR lpszPort, LPDEVMODEA lpdmInput,
462 LPSTR lpszProfile, DWORD fwMode )
470 INT (*pExtDeviceMode)(LPSTR,HWND,LPDEVMODEA,LPSTR,LPSTR,LPDEVMODEA,LPSTR,DWORD);
472 TRACE("(%p, %p, %s, %s, %p, %s, %ld)\n",
473 hwnd, lpdmOutput, lpszDevice, lpszPort, lpdmInput, lpszProfile, fwMode );
475 if (!lpszDevice) return -1;
476 if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
478 if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
480 if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
482 if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
484 if ((dc = DC_GetDCPtr( hdc )))
486 pExtDeviceMode = dc->funcs->pExtDeviceMode;
487 GDI_ReleaseObj( hdc );
489 ret = pExtDeviceMode(buf, hwnd, lpdmOutput, lpszDevice, lpszPort,
490 lpdmInput, lpszProfile, fwMode);
496 /****************************************************************************
499 * This should load the correct driver for lpszDevice and calls this driver's
500 * AdvancedSetupDialog proc.
502 INT WINAPI GDI_CallAdvancedSetupDialog16( HWND hwnd, LPSTR lpszDevice,
503 LPDEVMODEA devin, LPDEVMODEA devout )
505 TRACE("(%p, %s, %p, %p)\n", hwnd, lpszDevice, devin, devout );
509 /*****************************************************************************
512 * This should load the correct driver for lpszDevice and calls this driver's
513 * DeviceCapabilities proc.
515 * FIXME: convert DeviceCapabilities to unicode in the driver interface
517 DWORD WINAPI GDI_CallDeviceCapabilities16( LPCSTR lpszDevice, LPCSTR lpszPort,
518 WORD fwCapability, LPSTR lpszOutput,
528 TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice, lpszPort, fwCapability, lpszOutput, lpdm );
530 if (!lpszDevice) return -1;
531 if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
533 if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
535 if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
537 if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
539 if ((dc = DC_GetDCPtr( hdc )))
541 if (dc->funcs->pDeviceCapabilities)
542 ret = dc->funcs->pDeviceCapabilities( buf, lpszDevice, lpszPort,
543 fwCapability, lpszOutput, lpdm );
544 GDI_ReleaseObj( hdc );