2 * Graphics driver management functions
4 * Copyright 1994 Bob Amstadt
5 * Copyright 1996, 2001 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
33 #include "gdi_private.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(driver
);
39 struct graphics_driver
41 struct graphics_driver
*next
;
42 struct graphics_driver
*prev
;
43 HMODULE module
; /* module handle */
44 unsigned int count
; /* reference count */
48 static struct graphics_driver
*first_driver
;
49 static struct graphics_driver
*display_driver
;
51 static CRITICAL_SECTION driver_section
;
52 static CRITICAL_SECTION_DEBUG critsect_debug
=
54 0, 0, &driver_section
,
55 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
56 0, 0, { (DWORD_PTR
)(__FILE__
": driver_section") }
58 static CRITICAL_SECTION driver_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
60 /**********************************************************************
63 * Allocate and fill the driver structure for a given module.
65 static struct graphics_driver
*create_driver( HMODULE module
)
67 struct graphics_driver
*driver
;
69 if (!(driver
= HeapAlloc( GetProcessHeap(), 0, sizeof(*driver
)))) return NULL
;
72 driver
->module
= module
;
75 /* fill the function table */
78 #define GET_FUNC(name) driver->funcs.p##name = (void*)GetProcAddress( module, #name )
87 GET_FUNC(ChoosePixelFormat
);
89 GET_FUNC(CloseFigure
);
90 GET_FUNC(CreateBitmap
);
92 GET_FUNC(CreateDIBSection
);
93 GET_FUNC(DeleteBitmap
);
95 GET_FUNC(DescribePixelFormat
);
96 GET_FUNC(DeviceCapabilities
);
101 GET_FUNC(EnumDeviceFonts
);
102 GET_FUNC(ExcludeClipRect
);
103 GET_FUNC(ExtDeviceMode
);
105 GET_FUNC(ExtFloodFill
);
106 GET_FUNC(ExtSelectClipRgn
);
107 GET_FUNC(ExtTextOut
);
110 GET_FUNC(FlattenPath
);
112 GET_FUNC(GdiComment
);
113 GET_FUNC(GetBitmapBits
);
114 GET_FUNC(GetCharWidth
);
115 GET_FUNC(GetDCOrgEx
);
116 GET_FUNC(GetDIBColorTable
);
118 GET_FUNC(GetDeviceCaps
);
119 GET_FUNC(GetDeviceGammaRamp
);
120 GET_FUNC(GetNearestColor
);
122 GET_FUNC(GetPixelFormat
);
123 GET_FUNC(GetSystemPaletteEntries
);
124 GET_FUNC(GetTextExtentExPoint
);
125 GET_FUNC(GetTextMetrics
);
126 GET_FUNC(IntersectClipRect
);
130 GET_FUNC(ModifyWorldTransform
);
131 GET_FUNC(OffsetClipRgn
);
132 GET_FUNC(OffsetViewportOrg
);
133 GET_FUNC(OffsetWindowOrg
);
137 GET_FUNC(PolyBezier
);
138 GET_FUNC(PolyBezierTo
);
140 GET_FUNC(PolyPolygon
);
141 GET_FUNC(PolyPolyline
);
144 GET_FUNC(PolylineTo
);
145 GET_FUNC(RealizeDefaultPalette
);
146 GET_FUNC(RealizePalette
);
152 GET_FUNC(ScaleViewportExt
);
153 GET_FUNC(ScaleWindowExt
);
154 GET_FUNC(SelectBitmap
);
155 GET_FUNC(SelectBrush
);
156 GET_FUNC(SelectClipPath
);
157 GET_FUNC(SelectFont
);
158 GET_FUNC(SelectPalette
);
160 GET_FUNC(SetArcDirection
);
161 GET_FUNC(SetBitmapBits
);
162 GET_FUNC(SetBkColor
);
164 GET_FUNC(SetDCBrushColor
);
166 GET_FUNC(SetDCPenColor
);
167 GET_FUNC(SetDIBColorTable
);
169 GET_FUNC(SetDIBitsToDevice
);
170 GET_FUNC(SetDeviceClipping
);
171 GET_FUNC(SetDeviceGammaRamp
);
172 GET_FUNC(SetMapMode
);
173 GET_FUNC(SetMapperFlags
);
175 GET_FUNC(SetPixelFormat
);
176 GET_FUNC(SetPolyFillMode
);
179 GET_FUNC(SetStretchBltMode
);
180 GET_FUNC(SetTextAlign
);
181 GET_FUNC(SetTextCharacterExtra
);
182 GET_FUNC(SetTextColor
);
183 GET_FUNC(SetTextJustification
);
184 GET_FUNC(SetViewportExt
);
185 GET_FUNC(SetViewportOrg
);
186 GET_FUNC(SetWindowExt
);
187 GET_FUNC(SetWindowOrg
);
188 GET_FUNC(SetWorldTransform
);
191 GET_FUNC(StretchBlt
);
192 GET_FUNC(StretchDIBits
);
193 GET_FUNC(StrokeAndFillPath
);
194 GET_FUNC(StrokePath
);
195 GET_FUNC(SwapBuffers
);
199 GET_FUNC(wglCreateContext
);
200 GET_FUNC(wglDeleteContext
);
201 GET_FUNC(wglMakeCurrent
);
202 GET_FUNC(wglShareLists
);
203 GET_FUNC(wglUseFontBitmapsA
);
204 GET_FUNC(wglUseFontBitmapsW
);
207 else memset( &driver
->funcs
, 0, sizeof(driver
->funcs
) );
209 /* add it to the list */
211 if ((driver
->next
= first_driver
)) driver
->next
->prev
= driver
;
212 first_driver
= driver
;
217 /**********************************************************************
218 * load_display_driver
220 * Special case for loading the display driver: get the name from the config file
222 static struct graphics_driver
*load_display_driver(void)
224 char buffer
[MAX_PATH
], libname
[32], *name
, *next
;
228 if (display_driver
) /* already loaded */
230 display_driver
->count
++;
231 return display_driver
;
234 strcpy( buffer
, "x11" ); /* default value */
235 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
236 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Drivers", &hkey
))
238 DWORD type
, count
= sizeof(buffer
);
239 RegQueryValueExA( hkey
, "Graphics", 0, &type
, (LPBYTE
) buffer
, &count
);
246 next
= strchr( name
, ',' );
247 if (next
) *next
++ = 0;
249 snprintf( libname
, sizeof(libname
), "wine%s.drv", name
);
250 if ((module
= LoadLibraryA( libname
)) != 0) break;
254 if (!(display_driver
= create_driver( module
)))
256 MESSAGE( "Could not create graphics driver '%s'\n", buffer
);
257 FreeLibrary( module
);
261 display_driver
->count
++; /* we don't want to free it */
262 return display_driver
;
266 /**********************************************************************
269 const DC_FUNCTIONS
*DRIVER_load_driver( LPCWSTR name
)
272 struct graphics_driver
*driver
;
273 static const WCHAR displayW
[] = { 'd','i','s','p','l','a','y',0 };
274 static const WCHAR display1W
[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
276 EnterCriticalSection( &driver_section
);
278 /* display driver is a special case */
279 if (!strcmpiW( name
, displayW
) ||
280 !strcmpiW( name
, display1W
))
282 driver
= load_display_driver();
283 LeaveCriticalSection( &driver_section
);
284 return &driver
->funcs
;
287 if ((module
= GetModuleHandleW( name
)))
289 for (driver
= first_driver
; driver
; driver
= driver
->next
)
291 if (driver
->module
== module
)
294 LeaveCriticalSection( &driver_section
);
295 return &driver
->funcs
;
300 if (!(module
= LoadLibraryW( name
)))
302 LeaveCriticalSection( &driver_section
);
306 if (!(driver
= create_driver( module
)))
308 FreeLibrary( module
);
309 LeaveCriticalSection( &driver_section
);
313 TRACE( "loaded driver %p for %s\n", driver
, debugstr_w(name
) );
314 LeaveCriticalSection( &driver_section
);
315 return &driver
->funcs
;
319 /**********************************************************************
322 * Get a new copy of an existing driver.
324 const DC_FUNCTIONS
*DRIVER_get_driver( const DC_FUNCTIONS
*funcs
)
326 struct graphics_driver
*driver
;
328 EnterCriticalSection( &driver_section
);
329 for (driver
= first_driver
; driver
; driver
= driver
->next
)
330 if (&driver
->funcs
== funcs
) break;
331 if (!driver
) ERR( "driver not found, trouble ahead\n" );
333 LeaveCriticalSection( &driver_section
);
338 /**********************************************************************
339 * DRIVER_release_driver
341 * Release a driver by decrementing ref count and freeing it if needed.
343 void DRIVER_release_driver( const DC_FUNCTIONS
*funcs
)
345 struct graphics_driver
*driver
;
347 EnterCriticalSection( &driver_section
);
349 for (driver
= first_driver
; driver
; driver
= driver
->next
)
350 if (&driver
->funcs
== funcs
) break;
352 if (!driver
) goto done
;
353 if (--driver
->count
) goto done
;
355 /* removed last reference, free it */
356 if (driver
->next
) driver
->next
->prev
= driver
->prev
;
357 if (driver
->prev
) driver
->prev
->next
= driver
->next
;
358 else first_driver
= driver
->next
;
359 if (driver
== display_driver
) display_driver
= NULL
;
361 FreeLibrary( driver
->module
);
362 HeapFree( GetProcessHeap(), 0, driver
);
364 LeaveCriticalSection( &driver_section
);
368 /*****************************************************************************
369 * DRIVER_GetDriverName
372 BOOL
DRIVER_GetDriverName( LPCWSTR device
, LPWSTR driver
, DWORD size
)
374 static const WCHAR displayW
[] = { 'd','i','s','p','l','a','y',0 };
375 static const WCHAR devicesW
[] = { 'd','e','v','i','c','e','s',0 };
376 static const WCHAR display1W
[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
377 static const WCHAR empty_strW
[] = { 0 };
380 /* display is a special case */
381 if (!strcmpiW( device
, displayW
) ||
382 !strcmpiW( device
, display1W
))
384 lstrcpynW( driver
, displayW
, size
);
388 size
= GetProfileStringW(devicesW
, device
, empty_strW
, driver
, size
);
390 WARN("Unable to find %s in [devices] section of win.ini\n", debugstr_w(device
));
393 p
= strchrW(driver
, ',');
396 WARN("%s entry in [devices] section of win.ini is malformed.\n", debugstr_w(device
));
400 TRACE("Found %s for %s\n", debugstr_w(driver
), debugstr_w(device
));
405 /***********************************************************************
406 * GdiConvertToDevmodeW (GDI32.@)
408 DEVMODEW
* WINAPI
GdiConvertToDevmodeW(const DEVMODEA
*dmA
)
413 dmW_size
= dmA
->dmSize
+ CCHDEVICENAME
;
414 if (dmA
->dmSize
>= (const char *)dmA
->dmFormName
- (const char *)dmA
+ CCHFORMNAME
)
415 dmW_size
+= CCHFORMNAME
;
417 dmW
= HeapAlloc(GetProcessHeap(), 0, dmW_size
+ dmA
->dmDriverExtra
);
418 if (!dmW
) return NULL
;
420 MultiByteToWideChar(CP_ACP
, 0, (const char*) dmA
->dmDeviceName
, CCHDEVICENAME
,
421 dmW
->dmDeviceName
, CCHDEVICENAME
);
422 /* copy slightly more, to avoid long computations */
423 memcpy(&dmW
->dmSpecVersion
, &dmA
->dmSpecVersion
, dmA
->dmSize
- CCHDEVICENAME
);
425 if (dmA
->dmSize
>= (const char *)dmA
->dmFormName
- (const char *)dmA
+ CCHFORMNAME
)
427 MultiByteToWideChar(CP_ACP
, 0, (const char*) dmA
->dmFormName
, CCHFORMNAME
,
428 dmW
->dmFormName
, CCHFORMNAME
);
429 if (dmA
->dmSize
> (const char *)&dmA
->dmLogPixels
- (const char *)dmA
)
430 memcpy(&dmW
->dmLogPixels
, &dmA
->dmLogPixels
, dmA
->dmSize
- ((const char *)&dmA
->dmLogPixels
- (const char *)dmA
));
433 if (dmA
->dmDriverExtra
)
434 memcpy((char *)dmW
+ dmW_size
, (const char *)dmA
+ dmA
->dmSize
, dmA
->dmDriverExtra
);
436 dmW
->dmSize
= dmW_size
;
442 /*****************************************************************************
445 * This should thunk to 16-bit and simply call the proc with the given args.
447 INT WINAPI
GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc
, HWND hWnd
,
448 LPSTR lpModelName
, LPSTR OldPort
, LPSTR NewPort
)
450 FIXME("(%p, %p, %s, %s, %s)\n", lpfnDevInstallProc
, hWnd
, lpModelName
, OldPort
, NewPort
);
454 /*****************************************************************************
457 * This should load the correct driver for lpszDevice and calls this driver's
458 * ExtDeviceModePropSheet proc.
460 * Note: The driver calls a callback routine for each property sheet page; these
461 * pages are supposed to be filled into the structure pointed to by lpPropSheet.
462 * The layout of this structure is:
468 * HPROPSHEETPAGE pages[10];
471 INT WINAPI
GDI_CallExtDeviceModePropSheet16( HWND hWnd
, LPCSTR lpszDevice
,
472 LPCSTR lpszPort
, LPVOID lpPropSheet
)
474 FIXME("(%p, %s, %s, %p)\n", hWnd
, lpszDevice
, lpszPort
, lpPropSheet
);
478 /*****************************************************************************
481 * This should load the correct driver for lpszDevice and calls this driver's
482 * ExtDeviceMode proc.
484 * FIXME: convert ExtDeviceMode to unicode in the driver interface
486 INT WINAPI
GDI_CallExtDeviceMode16( HWND hwnd
,
487 LPDEVMODEA lpdmOutput
, LPSTR lpszDevice
,
488 LPSTR lpszPort
, LPDEVMODEA lpdmInput
,
489 LPSTR lpszProfile
, DWORD fwMode
)
497 INT (*pExtDeviceMode
)(LPSTR
,HWND
,LPDEVMODEA
,LPSTR
,LPSTR
,LPDEVMODEA
,LPSTR
,DWORD
);
499 TRACE("(%p, %p, %s, %s, %p, %s, %d)\n",
500 hwnd
, lpdmOutput
, lpszDevice
, lpszPort
, lpdmInput
, lpszProfile
, fwMode
);
502 if (!lpszDevice
) return -1;
503 if (!MultiByteToWideChar(CP_ACP
, 0, lpszDevice
, -1, deviceW
, 300)) return -1;
505 if(!DRIVER_GetDriverName( deviceW
, bufW
, 300 )) return -1;
507 if (!WideCharToMultiByte(CP_ACP
, 0, bufW
, -1, buf
, 300, NULL
, NULL
)) return -1;
509 if (!(hdc
= CreateICA( buf
, lpszDevice
, lpszPort
, NULL
))) return -1;
511 if ((dc
= DC_GetDCPtr( hdc
)))
513 pExtDeviceMode
= dc
->funcs
->pExtDeviceMode
;
514 GDI_ReleaseObj( hdc
);
516 ret
= pExtDeviceMode(buf
, hwnd
, lpdmOutput
, lpszDevice
, lpszPort
,
517 lpdmInput
, lpszProfile
, fwMode
);
523 /****************************************************************************
526 * This should load the correct driver for lpszDevice and calls this driver's
527 * AdvancedSetupDialog proc.
529 INT WINAPI
GDI_CallAdvancedSetupDialog16( HWND hwnd
, LPSTR lpszDevice
,
530 LPDEVMODEA devin
, LPDEVMODEA devout
)
532 TRACE("(%p, %s, %p, %p)\n", hwnd
, lpszDevice
, devin
, devout
);
536 /*****************************************************************************
539 * This should load the correct driver for lpszDevice and calls this driver's
540 * DeviceCapabilities proc.
542 * FIXME: convert DeviceCapabilities to unicode in the driver interface
544 DWORD WINAPI
GDI_CallDeviceCapabilities16( LPCSTR lpszDevice
, LPCSTR lpszPort
,
545 WORD fwCapability
, LPSTR lpszOutput
,
555 TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice
, lpszPort
, fwCapability
, lpszOutput
, lpdm
);
557 if (!lpszDevice
) return -1;
558 if (!MultiByteToWideChar(CP_ACP
, 0, lpszDevice
, -1, deviceW
, 300)) return -1;
560 if(!DRIVER_GetDriverName( deviceW
, bufW
, 300 )) return -1;
562 if (!WideCharToMultiByte(CP_ACP
, 0, bufW
, -1, buf
, 300, NULL
, NULL
)) return -1;
564 if (!(hdc
= CreateICA( buf
, lpszDevice
, lpszPort
, NULL
))) return -1;
566 if ((dc
= DC_GetDCPtr( hdc
)))
568 if (dc
->funcs
->pDeviceCapabilities
)
569 ret
= dc
->funcs
->pDeviceCapabilities( buf
, lpszDevice
, lpszPort
,
570 fwCapability
, lpszOutput
, lpdm
);
571 GDI_ReleaseObj( hdc
);
578 /************************************************************************
581 INT WINAPI
Escape( HDC hdc
, INT escape
, INT in_count
, LPCSTR in_data
, LPVOID out_data
)
589 return AbortDoc( hdc
);
592 return EndDoc( hdc
);
594 case GETPHYSPAGESIZE
:
596 pt
->x
= GetDeviceCaps( hdc
, PHYSICALWIDTH
);
597 pt
->y
= GetDeviceCaps( hdc
, PHYSICALHEIGHT
);
600 case GETPRINTINGOFFSET
:
602 pt
->x
= GetDeviceCaps( hdc
, PHYSICALOFFSETX
);
603 pt
->y
= GetDeviceCaps( hdc
, PHYSICALOFFSETY
);
606 case GETSCALINGFACTOR
:
608 pt
->x
= GetDeviceCaps( hdc
, SCALINGFACTORX
);
609 pt
->y
= GetDeviceCaps( hdc
, SCALINGFACTORY
);
613 return EndPage( hdc
);
616 return SetAbortProc( hdc
, (ABORTPROC
)in_data
);
623 /* in_data may not be 0 terminated so we must copy it */
626 name
= HeapAlloc( GetProcessHeap(), 0, in_count
+1 );
627 memcpy( name
, in_data
, in_count
);
630 /* out_data is actually a pointer to the DocInfo structure and used as
631 * a second input parameter */
632 if (out_data
) doc
= *(DOCINFOA
*)out_data
;
635 doc
.cbSize
= sizeof(doc
);
636 doc
.lpszOutput
= NULL
;
637 doc
.lpszDatatype
= NULL
;
640 doc
.lpszDocName
= name
;
641 ret
= StartDocA( hdc
, &doc
);
642 HeapFree( GetProcessHeap(), 0, name
);
643 if (ret
> 0) ret
= StartPage( hdc
);
647 case QUERYESCSUPPORT
:
649 const INT
*ptr
= (const INT
*)in_data
;
650 if (in_count
< sizeof(INT
)) return 0;
655 case GETPHYSPAGESIZE
:
656 case GETPRINTINGOFFSET
:
657 case GETSCALINGFACTOR
:
659 case QUERYESCSUPPORT
:
668 /* if not handled internally, pass it to the driver */
669 return ExtEscape( hdc
, escape
, in_count
, in_data
, 0, out_data
);
673 /******************************************************************************
674 * ExtEscape [GDI32.@]
676 * Access capabilities of a particular device that are not available through GDI.
679 * hdc [I] Handle to device context
680 * nEscape [I] Escape function
681 * cbInput [I] Number of bytes in input structure
682 * lpszInData [I] Pointer to input structure
683 * cbOutput [I] Number of bytes in output structure
684 * lpszOutData [O] Pointer to output structure
691 INT WINAPI
ExtEscape( HDC hdc
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
,
692 INT cbOutput
, LPSTR lpszOutData
)
695 DC
* dc
= DC_GetDCPtr( hdc
);
698 if (dc
->funcs
->pExtEscape
)
699 ret
= dc
->funcs
->pExtEscape( dc
->physDev
, nEscape
, cbInput
, lpszInData
, cbOutput
, lpszOutData
);
700 GDI_ReleaseObj( hdc
);
706 /*******************************************************************
707 * DrawEscape [GDI32.@]
711 INT WINAPI
DrawEscape(HDC hdc
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
)
713 FIXME("DrawEscape, stub\n");