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
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(driver);
32 struct graphics_driver
34 struct graphics_driver *next;
35 struct graphics_driver *prev;
36 HMODULE module; /* module handle */
37 unsigned int count; /* reference count */
41 static struct graphics_driver *first_driver;
42 static struct graphics_driver *display_driver;
43 static const DC_FUNCTIONS *win16_driver;
44 static CRITICAL_SECTION driver_section = CRITICAL_SECTION_INIT( "driver_section" );
46 /**********************************************************************
49 * Allocate and fill the driver structure for a given module.
51 static struct graphics_driver *create_driver( HMODULE module )
53 struct graphics_driver *driver;
55 if (!(driver = HeapAlloc( GetProcessHeap(), 0, sizeof(*driver)))) return NULL;
58 driver->module = module;
61 /* fill the function table */
63 #define GET_FUNC(name) driver->funcs.p##name = (void*)GetProcAddress( module, #name )
73 GET_FUNC(ChoosePixelFormat);
75 GET_FUNC(CloseFigure);
76 GET_FUNC(CreateBitmap);
78 GET_FUNC(CreateDIBSection);
80 GET_FUNC(DeleteObject);
81 GET_FUNC(DescribePixelFormat);
82 GET_FUNC(DeviceCapabilities);
87 GET_FUNC(EnumDeviceFonts);
88 GET_FUNC(ExcludeClipRect);
89 GET_FUNC(ExtDeviceMode);
91 GET_FUNC(ExtFloodFill);
95 GET_FUNC(FlattenPath);
97 GET_FUNC(GetCharWidth);
99 GET_FUNC(GetDeviceCaps);
100 GET_FUNC(GetDeviceGammaRamp);
102 GET_FUNC(GetPixelFormat);
103 GET_FUNC(GetTextExtentPoint);
104 GET_FUNC(GetTextMetrics);
105 GET_FUNC(IntersectClipRect);
109 GET_FUNC(OffsetClipRgn);
110 GET_FUNC(OffsetViewportOrg);
111 GET_FUNC(OffsetWindowOrg);
115 GET_FUNC(PolyBezier);
116 GET_FUNC(PolyBezierTo);
118 GET_FUNC(PolyPolygon);
119 GET_FUNC(PolyPolyline);
122 GET_FUNC(PolylineTo);
123 GET_FUNC(RealizePalette);
128 GET_FUNC(ScaleViewportExt);
129 GET_FUNC(ScaleWindowExt);
130 GET_FUNC(SelectClipPath);
131 GET_FUNC(SelectClipRgn);
132 GET_FUNC(SelectObject);
133 GET_FUNC(SelectPalette);
134 GET_FUNC(SetBkColor);
136 GET_FUNC(SetDIBitsToDevice);
137 GET_FUNC(SetDeviceClipping);
138 GET_FUNC(SetDeviceGammaRamp);
139 GET_FUNC(SetMapMode);
140 GET_FUNC(SetMapperFlags);
142 GET_FUNC(SetPixelFormat);
143 GET_FUNC(SetPolyFillMode);
146 GET_FUNC(SetStretchBltMode);
147 GET_FUNC(SetTextAlign);
148 GET_FUNC(SetTextCharacterExtra);
149 GET_FUNC(SetTextColor);
150 GET_FUNC(SetTextJustification);
151 GET_FUNC(SetViewportExt);
152 GET_FUNC(SetViewportOrg);
153 GET_FUNC(SetWindowExt);
154 GET_FUNC(SetWindowOrg);
157 GET_FUNC(StretchBlt);
158 GET_FUNC(StretchDIBits);
159 GET_FUNC(StrokeAndFillPath);
160 GET_FUNC(StrokePath);
161 GET_FUNC(SwapBuffers);
165 /* add it to the list */
167 if ((driver->next = first_driver)) driver->next->prev = driver;
168 first_driver = driver;
173 /**********************************************************************
174 * load_display_driver
176 * Special case for loading the display driver: get the name from the config file
178 static struct graphics_driver *load_display_driver(void)
180 char buffer[MAX_PATH];
184 if (display_driver) /* already loaded */
186 display_driver->count++;
187 return display_driver;
190 strcpy( buffer, "x11drv" ); /* default value */
191 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine", &hkey ))
193 DWORD type, count = sizeof(buffer);
194 RegQueryValueExA( hkey, "GraphicsDriver", 0, &type, buffer, &count );
198 if (!(module = LoadLibraryA( buffer )))
200 MESSAGE( "Could not load graphics driver '%s'\n", buffer );
204 if (!(display_driver = create_driver( module )))
206 MESSAGE( "Could not create graphics driver '%s'\n", buffer );
207 FreeLibrary( module );
211 display_driver->count++; /* we don't want to free it */
212 return display_driver;
216 /**********************************************************************
219 const DC_FUNCTIONS *DRIVER_load_driver( LPCSTR name )
222 struct graphics_driver *driver;
224 RtlEnterCriticalSection( &driver_section );
226 /* display driver is a special case */
227 if (!strcasecmp( name, "display" ))
229 driver = load_display_driver();
230 RtlLeaveCriticalSection( &driver_section );
231 return &driver->funcs;
234 if ((module = GetModuleHandleA( name )))
236 for (driver = first_driver; driver; driver = driver->next)
238 if (driver->module == module)
241 RtlLeaveCriticalSection( &driver_section );
242 return &driver->funcs;
247 if (!(module = LoadLibraryA( name )))
249 if (!win16_driver) win16_driver = WIN16DRV_Init();
250 RtlLeaveCriticalSection( &driver_section );
254 if (!(driver = create_driver( module )))
256 FreeLibrary( module );
257 RtlLeaveCriticalSection( &driver_section );
261 TRACE( "loaded driver %p for %s\n", driver, name );
262 RtlLeaveCriticalSection( &driver_section );
263 return &driver->funcs;
267 /**********************************************************************
270 * Get a new copy of an existing driver.
272 const DC_FUNCTIONS *DRIVER_get_driver( const DC_FUNCTIONS *funcs )
274 struct graphics_driver *driver;
276 RtlEnterCriticalSection( &driver_section );
277 if (funcs != win16_driver)
279 for (driver = first_driver; driver; driver = driver->next)
280 if (&driver->funcs == funcs) break;
281 if (!driver) ERR( "driver not found, trouble ahead\n" );
284 RtlLeaveCriticalSection( &driver_section );
289 /**********************************************************************
290 * DRIVER_release_driver
292 * Release a driver by decrementing ref count and freeing it if needed.
294 void DRIVER_release_driver( const DC_FUNCTIONS *funcs )
296 struct graphics_driver *driver;
298 RtlEnterCriticalSection( &driver_section );
300 if (funcs == win16_driver) goto done;
302 for (driver = first_driver; driver; driver = driver->next)
303 if (&driver->funcs == funcs) break;
305 if (!driver) goto done;
306 if (--driver->count) goto done;
308 /* removed last reference, free it */
309 if (driver->next) driver->next->prev = driver->prev;
310 if (driver->prev) driver->prev->next = driver->next;
311 else first_driver = driver->next;
312 if (driver == display_driver) display_driver = NULL;
314 FreeLibrary( driver->module );
315 HeapFree( GetProcessHeap(), 0, driver );
317 RtlLeaveCriticalSection( &driver_section );
321 /*****************************************************************************
322 * DRIVER_GetDriverName
325 BOOL DRIVER_GetDriverName( LPCSTR device, LPSTR driver, DWORD size )
328 size = GetProfileStringA("devices", device, "", driver, size);
330 WARN("Unable to find '%s' in [devices] section of win.ini\n", device);
333 p = strchr(driver, ',');
336 WARN("'%s' entry in [devices] section of win.ini is malformed.\n", device);
340 TRACE("Found '%s' for '%s'\n", driver, device);
344 /*****************************************************************************
347 * This should thunk to 16-bit and simply call the proc with the given args.
349 INT WINAPI GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc, HWND hWnd,
350 LPSTR lpModelName, LPSTR OldPort, LPSTR NewPort )
352 FIXME("(%p, %04x, %s, %s, %s)\n", lpfnDevInstallProc, hWnd, lpModelName, OldPort, NewPort );
356 /*****************************************************************************
359 * This should load the correct driver for lpszDevice and calls this driver's
360 * ExtDeviceModePropSheet proc.
362 * Note: The driver calls a callback routine for each property sheet page; these
363 * pages are supposed to be filled into the structure pointed to by lpPropSheet.
364 * The layout of this structure is:
370 * HPROPSHEETPAGE pages[10];
373 INT WINAPI GDI_CallExtDeviceModePropSheet16( HWND hWnd, LPCSTR lpszDevice,
374 LPCSTR lpszPort, LPVOID lpPropSheet )
376 FIXME("(%04x, %s, %s, %p)\n", hWnd, lpszDevice, lpszPort, lpPropSheet );
380 /*****************************************************************************
383 * This should load the correct driver for lpszDevice and calls this driver's
384 * ExtDeviceMode proc.
386 INT WINAPI GDI_CallExtDeviceMode16( HWND hwnd,
387 LPDEVMODEA lpdmOutput, LPSTR lpszDevice,
388 LPSTR lpszPort, LPDEVMODEA lpdmInput,
389 LPSTR lpszProfile, DWORD fwMode )
395 INT (*pExtDeviceMode)(LPSTR,HWND,LPDEVMODEA,LPSTR,LPSTR,LPDEVMODEA,LPSTR,DWORD);
397 TRACE("(%04x, %p, %s, %s, %p, %s, %ld)\n",
398 hwnd, lpdmOutput, lpszDevice, lpszPort, lpdmInput, lpszProfile, fwMode );
400 if(!DRIVER_GetDriverName( lpszDevice, buf, sizeof(buf) )) return -1;
402 if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
404 if ((dc = DC_GetDCPtr( hdc )))
406 pExtDeviceMode = dc->funcs->pExtDeviceMode;
407 GDI_ReleaseObj( hdc );
409 ret = pExtDeviceMode(buf, hwnd, lpdmOutput, lpszDevice, lpszPort,
410 lpdmInput, lpszProfile, fwMode);
416 /****************************************************************************
419 * This should load the correct driver for lpszDevice and calls this driver's
420 * AdvancedSetupDialog proc.
422 INT WINAPI GDI_CallAdvancedSetupDialog16( HWND hwnd, LPSTR lpszDevice,
423 LPDEVMODEA devin, LPDEVMODEA devout )
425 TRACE("(%04x, %s, %p, %p)\n", hwnd, lpszDevice, devin, devout );
429 /*****************************************************************************
432 * This should load the correct driver for lpszDevice and calls this driver's
433 * DeviceCapabilities proc.
435 DWORD WINAPI GDI_CallDeviceCapabilities16( LPCSTR lpszDevice, LPCSTR lpszPort,
436 WORD fwCapability, LPSTR lpszOutput,
444 TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice, lpszPort, fwCapability, lpszOutput, lpdm );
446 if(!DRIVER_GetDriverName( lpszDevice, buf, sizeof(buf) )) return -1;
448 if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
450 if ((dc = DC_GetDCPtr( hdc )))
452 if (dc->funcs->pDeviceCapabilities)
453 ret = dc->funcs->pDeviceCapabilities( buf, lpszDevice, lpszPort,
454 fwCapability, lpszOutput, lpdm );
455 GDI_ReleaseObj( hdc );