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 "gdi_private.h"
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(driver);
37 struct graphics_driver
39 struct graphics_driver *next;
40 struct graphics_driver *prev;
41 HMODULE module; /* module handle */
42 unsigned int count; /* reference count */
46 static struct graphics_driver *first_driver;
47 static struct graphics_driver *display_driver;
49 static CRITICAL_SECTION driver_section;
50 static CRITICAL_SECTION_DEBUG critsect_debug =
52 0, 0, &driver_section,
53 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
54 0, 0, { 0, (DWORD)(__FILE__ ": driver_section") }
56 static CRITICAL_SECTION driver_section = { &critsect_debug, -1, 0, 0, 0, 0 };
58 /**********************************************************************
61 * Allocate and fill the driver structure for a given module.
63 static struct graphics_driver *create_driver( HMODULE module )
65 struct graphics_driver *driver;
67 if (!(driver = HeapAlloc( GetProcessHeap(), 0, sizeof(*driver)))) return NULL;
70 driver->module = module;
73 /* fill the function table */
75 #define GET_FUNC(name) driver->funcs.p##name = (void*)GetProcAddress( module, #name )
84 GET_FUNC(ChoosePixelFormat);
86 GET_FUNC(CloseFigure);
87 GET_FUNC(CreateBitmap);
89 GET_FUNC(CreateDIBSection);
90 GET_FUNC(DeleteBitmap);
92 GET_FUNC(DescribePixelFormat);
93 GET_FUNC(DeviceCapabilities);
98 GET_FUNC(EnumDeviceFonts);
99 GET_FUNC(ExcludeClipRect);
100 GET_FUNC(ExtDeviceMode);
102 GET_FUNC(ExtFloodFill);
103 GET_FUNC(ExtSelectClipRgn);
104 GET_FUNC(ExtTextOut);
107 GET_FUNC(FlattenPath);
109 GET_FUNC(GdiComment);
110 GET_FUNC(GetBitmapBits);
111 GET_FUNC(GetCharWidth);
112 GET_FUNC(GetDCOrgEx);
113 GET_FUNC(GetDIBColorTable);
115 GET_FUNC(GetDeviceCaps);
116 GET_FUNC(GetDeviceGammaRamp);
117 GET_FUNC(GetNearestColor);
119 GET_FUNC(GetPixelFormat);
120 GET_FUNC(GetSystemPaletteEntries);
121 GET_FUNC(GetTextExtentPoint);
122 GET_FUNC(GetTextMetrics);
123 GET_FUNC(IntersectClipRect);
127 GET_FUNC(ModifyWorldTransform);
128 GET_FUNC(OffsetClipRgn);
129 GET_FUNC(OffsetViewportOrg);
130 GET_FUNC(OffsetWindowOrg);
134 GET_FUNC(PolyBezier);
135 GET_FUNC(PolyBezierTo);
137 GET_FUNC(PolyPolygon);
138 GET_FUNC(PolyPolyline);
141 GET_FUNC(PolylineTo);
142 GET_FUNC(RealizeDefaultPalette);
143 GET_FUNC(RealizePalette);
149 GET_FUNC(ScaleViewportExt);
150 GET_FUNC(ScaleWindowExt);
151 GET_FUNC(SelectBitmap);
152 GET_FUNC(SelectBrush);
153 GET_FUNC(SelectClipPath);
154 GET_FUNC(SelectFont);
155 GET_FUNC(SelectPalette);
157 GET_FUNC(SetArcDirection);
158 GET_FUNC(SetBitmapBits);
159 GET_FUNC(SetBkColor);
161 GET_FUNC(SetDCBrushColor);
163 GET_FUNC(SetDCPenColor);
164 GET_FUNC(SetDIBColorTable);
166 GET_FUNC(SetDIBitsToDevice);
167 GET_FUNC(SetDeviceClipping);
168 GET_FUNC(SetDeviceGammaRamp);
169 GET_FUNC(SetMapMode);
170 GET_FUNC(SetMapperFlags);
172 GET_FUNC(SetPixelFormat);
173 GET_FUNC(SetPolyFillMode);
176 GET_FUNC(SetStretchBltMode);
177 GET_FUNC(SetTextAlign);
178 GET_FUNC(SetTextCharacterExtra);
179 GET_FUNC(SetTextColor);
180 GET_FUNC(SetTextJustification);
181 GET_FUNC(SetViewportExt);
182 GET_FUNC(SetViewportOrg);
183 GET_FUNC(SetWindowExt);
184 GET_FUNC(SetWindowOrg);
185 GET_FUNC(SetWorldTransform);
188 GET_FUNC(StretchBlt);
189 GET_FUNC(StretchDIBits);
190 GET_FUNC(StrokeAndFillPath);
191 GET_FUNC(StrokePath);
192 GET_FUNC(SwapBuffers);
196 /* add it to the list */
198 if ((driver->next = first_driver)) driver->next->prev = driver;
199 first_driver = driver;
204 /**********************************************************************
205 * load_display_driver
207 * Special case for loading the display driver: get the name from the config file
209 static struct graphics_driver *load_display_driver(void)
211 char buffer[MAX_PATH];
215 if (display_driver) /* already loaded */
217 display_driver->count++;
218 return display_driver;
221 strcpy( buffer, "x11drv" ); /* default value */
222 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine", &hkey ))
224 DWORD type, count = sizeof(buffer);
225 RegQueryValueExA( hkey, "GraphicsDriver", 0, &type, buffer, &count );
229 if (!(module = LoadLibraryA( buffer )))
231 MESSAGE( "Could not load graphics driver '%s'\n", buffer );
235 if (!(display_driver = create_driver( module )))
237 MESSAGE( "Could not create graphics driver '%s'\n", buffer );
238 FreeLibrary( module );
242 display_driver->count++; /* we don't want to free it */
243 return display_driver;
247 /**********************************************************************
250 const DC_FUNCTIONS *DRIVER_load_driver( LPCWSTR name )
253 struct graphics_driver *driver;
254 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
256 EnterCriticalSection( &driver_section );
258 /* display driver is a special case */
259 if (!strcmpiW( name, displayW ))
261 driver = load_display_driver();
262 LeaveCriticalSection( &driver_section );
263 return &driver->funcs;
266 if ((module = GetModuleHandleW( name )))
268 for (driver = first_driver; driver; driver = driver->next)
270 if (driver->module == module)
273 LeaveCriticalSection( &driver_section );
274 return &driver->funcs;
279 if (!(module = LoadLibraryW( name )))
281 LeaveCriticalSection( &driver_section );
285 if (!(driver = create_driver( module )))
287 FreeLibrary( module );
288 LeaveCriticalSection( &driver_section );
292 TRACE( "loaded driver %p for %s\n", driver, debugstr_w(name) );
293 LeaveCriticalSection( &driver_section );
294 return &driver->funcs;
298 /**********************************************************************
301 * Get a new copy of an existing driver.
303 const DC_FUNCTIONS *DRIVER_get_driver( const DC_FUNCTIONS *funcs )
305 struct graphics_driver *driver;
307 EnterCriticalSection( &driver_section );
308 for (driver = first_driver; driver; driver = driver->next)
309 if (&driver->funcs == funcs) break;
310 if (!driver) ERR( "driver not found, trouble ahead\n" );
312 LeaveCriticalSection( &driver_section );
317 /**********************************************************************
318 * DRIVER_release_driver
320 * Release a driver by decrementing ref count and freeing it if needed.
322 void DRIVER_release_driver( const DC_FUNCTIONS *funcs )
324 struct graphics_driver *driver;
326 EnterCriticalSection( &driver_section );
328 for (driver = first_driver; driver; driver = driver->next)
329 if (&driver->funcs == funcs) break;
331 if (!driver) goto done;
332 if (--driver->count) goto done;
334 /* removed last reference, free it */
335 if (driver->next) driver->next->prev = driver->prev;
336 if (driver->prev) driver->prev->next = driver->next;
337 else first_driver = driver->next;
338 if (driver == display_driver) display_driver = NULL;
340 FreeLibrary( driver->module );
341 HeapFree( GetProcessHeap(), 0, driver );
343 LeaveCriticalSection( &driver_section );
347 /*****************************************************************************
348 * DRIVER_GetDriverName
351 BOOL DRIVER_GetDriverName( LPCWSTR device, LPWSTR driver, DWORD size )
353 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
354 static const WCHAR devicesW[] = { 'd','e','v','i','c','e','s',0 };
355 static const WCHAR empty_strW[] = { 0 };
358 /* display is a special case */
359 if (!strcmpiW( device, displayW ))
361 lstrcpynW( driver, displayW, size );
365 size = GetProfileStringW(devicesW, device, empty_strW, driver, size);
367 WARN("Unable to find %s in [devices] section of win.ini\n", debugstr_w(device));
370 p = strchrW(driver, ',');
373 WARN("%s entry in [devices] section of win.ini is malformed.\n", debugstr_w(device));
377 TRACE("Found %s for %s\n", debugstr_w(driver), debugstr_w(device));
382 /***********************************************************************
383 * GdiConvertToDevmodeW (GDI32.@)
385 DEVMODEW * WINAPI GdiConvertToDevmodeW(const DEVMODEA *dmA)
390 dmW_size = dmA->dmSize + CCHDEVICENAME;
391 if (dmA->dmSize >= (char *)dmA->dmFormName - (char *)dmA + CCHFORMNAME)
392 dmW_size += CCHFORMNAME;
394 dmW = HeapAlloc(GetProcessHeap(), 0, dmW_size + dmA->dmDriverExtra);
395 if (!dmW) return NULL;
397 MultiByteToWideChar(CP_ACP, 0, dmA->dmDeviceName, CCHDEVICENAME,
398 dmW->dmDeviceName, CCHDEVICENAME);
399 /* copy slightly more, to avoid long computations */
400 memcpy(&dmW->dmSpecVersion, &dmA->dmSpecVersion, dmA->dmSize - CCHDEVICENAME);
402 if (dmA->dmSize >= (char *)dmA->dmFormName - (char *)dmA + CCHFORMNAME)
404 MultiByteToWideChar(CP_ACP, 0, dmA->dmFormName, CCHFORMNAME,
405 dmW->dmFormName, CCHFORMNAME);
406 if (dmA->dmSize > (char *)&dmA->dmLogPixels - (char *)dmA)
407 memcpy(&dmW->dmLogPixels, &dmA->dmLogPixels, dmA->dmSize - ((char *)&dmA->dmLogPixels - (char *)dmA));
410 if (dmA->dmDriverExtra)
411 memcpy((char *)dmW + dmW_size, (char *)dmA + dmA->dmSize, dmA->dmDriverExtra);
413 dmW->dmSize = dmW_size;
419 /*****************************************************************************
422 * This should thunk to 16-bit and simply call the proc with the given args.
424 INT WINAPI GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc, HWND hWnd,
425 LPSTR lpModelName, LPSTR OldPort, LPSTR NewPort )
427 FIXME("(%p, %p, %s, %s, %s)\n", lpfnDevInstallProc, hWnd, lpModelName, OldPort, NewPort );
431 /*****************************************************************************
434 * This should load the correct driver for lpszDevice and calls this driver's
435 * ExtDeviceModePropSheet proc.
437 * Note: The driver calls a callback routine for each property sheet page; these
438 * pages are supposed to be filled into the structure pointed to by lpPropSheet.
439 * The layout of this structure is:
445 * HPROPSHEETPAGE pages[10];
448 INT WINAPI GDI_CallExtDeviceModePropSheet16( HWND hWnd, LPCSTR lpszDevice,
449 LPCSTR lpszPort, LPVOID lpPropSheet )
451 FIXME("(%p, %s, %s, %p)\n", hWnd, lpszDevice, lpszPort, lpPropSheet );
455 /*****************************************************************************
458 * This should load the correct driver for lpszDevice and calls this driver's
459 * ExtDeviceMode proc.
461 * FIXME: convert ExtDeviceMode to unicode in the driver interface
463 INT WINAPI GDI_CallExtDeviceMode16( HWND hwnd,
464 LPDEVMODEA lpdmOutput, LPSTR lpszDevice,
465 LPSTR lpszPort, LPDEVMODEA lpdmInput,
466 LPSTR lpszProfile, DWORD fwMode )
474 INT (*pExtDeviceMode)(LPSTR,HWND,LPDEVMODEA,LPSTR,LPSTR,LPDEVMODEA,LPSTR,DWORD);
476 TRACE("(%p, %p, %s, %s, %p, %s, %ld)\n",
477 hwnd, lpdmOutput, lpszDevice, lpszPort, lpdmInput, lpszProfile, fwMode );
479 if (!lpszDevice) return -1;
480 if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
482 if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
484 if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
486 if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
488 if ((dc = DC_GetDCPtr( hdc )))
490 pExtDeviceMode = dc->funcs->pExtDeviceMode;
491 GDI_ReleaseObj( hdc );
493 ret = pExtDeviceMode(buf, hwnd, lpdmOutput, lpszDevice, lpszPort,
494 lpdmInput, lpszProfile, fwMode);
500 /****************************************************************************
503 * This should load the correct driver for lpszDevice and calls this driver's
504 * AdvancedSetupDialog proc.
506 INT WINAPI GDI_CallAdvancedSetupDialog16( HWND hwnd, LPSTR lpszDevice,
507 LPDEVMODEA devin, LPDEVMODEA devout )
509 TRACE("(%p, %s, %p, %p)\n", hwnd, lpszDevice, devin, devout );
513 /*****************************************************************************
516 * This should load the correct driver for lpszDevice and calls this driver's
517 * DeviceCapabilities proc.
519 * FIXME: convert DeviceCapabilities to unicode in the driver interface
521 DWORD WINAPI GDI_CallDeviceCapabilities16( LPCSTR lpszDevice, LPCSTR lpszPort,
522 WORD fwCapability, LPSTR lpszOutput,
532 TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice, lpszPort, fwCapability, lpszOutput, lpdm );
534 if (!lpszDevice) return -1;
535 if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
537 if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
539 if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
541 if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
543 if ((dc = DC_GetDCPtr( hdc )))
545 if (dc->funcs->pDeviceCapabilities)
546 ret = dc->funcs->pDeviceCapabilities( buf, lpszDevice, lpszPort,
547 fwCapability, lpszOutput, lpdm );
548 GDI_ReleaseObj( hdc );