2 * Graphics driver management functions
4 * Copyright 1994 Bob Amstadt
5 * Copyright 1996, 2001 Alexandre Julliard
6 * Copyright 2009 Massimo Del Fedele
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
34 #include "wine/winbase16.h"
36 #include "gdi_private.h"
37 #include "wine/unicode.h"
38 #include "wine/list.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(driver
);
43 struct graphics_driver
46 HMODULE module
; /* module handle */
50 static struct list drivers
= LIST_INIT( drivers
);
51 static struct graphics_driver
*display_driver
;
53 static CRITICAL_SECTION driver_section
;
54 static CRITICAL_SECTION_DEBUG critsect_debug
=
56 0, 0, &driver_section
,
57 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
58 0, 0, { (DWORD_PTR
)(__FILE__
": driver_section") }
60 static CRITICAL_SECTION driver_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
62 /**********************************************************************
65 * Allocate and fill the driver structure for a given module.
67 static struct graphics_driver
*create_driver( HMODULE module
)
69 struct graphics_driver
*driver
;
71 if (!(driver
= HeapAlloc( GetProcessHeap(), 0, sizeof(*driver
)))) return NULL
;
72 driver
->module
= module
;
74 /* fill the function table */
77 #define GET_FUNC(name) driver->funcs.p##name = (void*)GetProcAddress( module, #name )
86 GET_FUNC(ChoosePixelFormat
);
88 GET_FUNC(CloseFigure
);
89 GET_FUNC(CreateBitmap
);
91 GET_FUNC(CreateDIBSection
);
92 GET_FUNC(DeleteBitmap
);
94 GET_FUNC(DescribePixelFormat
);
95 GET_FUNC(DeviceCapabilities
);
100 GET_FUNC(EnumDeviceFonts
);
101 GET_FUNC(ExcludeClipRect
);
102 GET_FUNC(ExtDeviceMode
);
104 GET_FUNC(ExtFloodFill
);
105 GET_FUNC(ExtSelectClipRgn
);
106 GET_FUNC(ExtTextOut
);
109 GET_FUNC(FlattenPath
);
111 GET_FUNC(GdiComment
);
112 GET_FUNC(GetBitmapBits
);
113 GET_FUNC(GetCharWidth
);
114 GET_FUNC(GetDCOrgEx
);
115 GET_FUNC(GetDIBColorTable
);
117 GET_FUNC(GetDeviceCaps
);
118 GET_FUNC(GetDeviceGammaRamp
);
119 GET_FUNC(GetICMProfile
);
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
);
165 GET_FUNC(SetDCPenColor
);
166 GET_FUNC(SetDIBColorTable
);
168 GET_FUNC(SetDIBitsToDevice
);
169 GET_FUNC(SetDeviceClipping
);
170 GET_FUNC(SetDeviceGammaRamp
);
171 GET_FUNC(SetMapMode
);
172 GET_FUNC(SetMapperFlags
);
174 GET_FUNC(SetPixelFormat
);
175 GET_FUNC(SetPolyFillMode
);
178 GET_FUNC(SetStretchBltMode
);
179 GET_FUNC(SetTextAlign
);
180 GET_FUNC(SetTextCharacterExtra
);
181 GET_FUNC(SetTextColor
);
182 GET_FUNC(SetTextJustification
);
183 GET_FUNC(SetViewportExt
);
184 GET_FUNC(SetViewportOrg
);
185 GET_FUNC(SetWindowExt
);
186 GET_FUNC(SetWindowOrg
);
187 GET_FUNC(SetWorldTransform
);
190 GET_FUNC(StretchBlt
);
191 GET_FUNC(StretchDIBits
);
192 GET_FUNC(StrokeAndFillPath
);
193 GET_FUNC(StrokePath
);
194 GET_FUNC(SwapBuffers
);
195 GET_FUNC(UnrealizePalette
);
199 GET_FUNC(wglCreateContext
);
200 GET_FUNC(wglCreateContextAttribsARB
);
201 GET_FUNC(wglDeleteContext
);
202 GET_FUNC(wglGetProcAddress
);
203 GET_FUNC(wglGetPbufferDCARB
);
204 GET_FUNC(wglMakeContextCurrentARB
);
205 GET_FUNC(wglMakeCurrent
);
206 GET_FUNC(wglSetPixelFormatWINE
);
207 GET_FUNC(wglShareLists
);
208 GET_FUNC(wglUseFontBitmapsA
);
209 GET_FUNC(wglUseFontBitmapsW
);
212 else memset( &driver
->funcs
, 0, sizeof(driver
->funcs
) );
218 /**********************************************************************
221 * Check if we want the DIB engine and try to load it
223 static HMODULE
Load_Dib_Driver(void)
227 static const char *winedib_drv
= "winedib.drv";
229 /* we do want use DIB Engine ? */
230 BOOL driverRequired
= TRUE
;
232 /* already checked env/registry for DIB driver ? */
233 BOOL envChecked
= FALSE
;
238 /* environment variable WINEDIB takes precedence */
239 if( (winedib
= getenv("WINEDIB")) != NULL
)
241 if(!strcasecmp(winedib
, "ON") ||
242 !strcasecmp(winedib
, "TRUE") ||
243 !strcasecmp(winedib
, "ENABLE") ||
244 !strcasecmp(winedib
, "ENABLED")
247 TRACE("DIB Engine enabled by environment\n");
249 driverRequired
= TRUE
;
251 else if(!strcasecmp(winedib
, "OFF") ||
252 !strcasecmp(winedib
, "FALSE") ||
253 !strcasecmp(winedib
, "DISABLE") ||
254 !strcasecmp(winedib
, "DISABLED")
257 TRACE("DIB Engine disabled by environment\n");
259 driverRequired
= FALSE
;
262 ERR("Bad WINEDIB environment variable\n");
265 /* no WINEDIB environment var found or wrong value, we check registry */
270 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\DIB Engine", &hkey
))
272 DWORD type
, count
= sizeof(buffer
);
273 RegQueryValueExA( hkey
, "Enable", 0, &type
, (LPBYTE
) buffer
, &count
);
278 /* registry value found, must be Y or y to enable driver, N or n to disable */
279 if(!strncasecmp(buffer
, "Y", 1))
281 TRACE("DIB Engine enabled by registry\n");
283 driverRequired
= TRUE
;
285 else if(!strncasecmp(buffer
, "N", 1))
287 TRACE("DIB Engine disabled by registry\n");
289 driverRequired
= FALSE
;
294 /* none of above, we assume we don't want to use engine */
297 TRACE("DIB Engine disabled by default\n");
299 driverRequired
= FALSE
;
302 /* if DIB Engine is required, try to load it
303 * otherwise just return NULL module */
306 if( (module
= LoadLibraryA( winedib_drv
)) != 0)
307 TRACE("Succesfully loaded DIB Engine\n");
309 ERR("Couldn't load DIB Engine\n");
316 /**********************************************************************
317 * DRIVER_get_display_driver
319 * Special case for loading the display driver: get the name from the config file
321 const DC_FUNCTIONS
*DRIVER_get_display_driver(void)
323 struct graphics_driver
*driver
;
324 char buffer
[MAX_PATH
], libname
[32], *name
, *next
;
328 if (display_driver
) return &display_driver
->funcs
; /* already loaded */
330 /* check at first if DIB engine is present and if we want
332 if( (module
= Load_Dib_Driver()) == 0)
334 /* no DIB Engine loaded, just load normal display driver */
336 strcpy( buffer
, "x11" ); /* default value */
337 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
338 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Drivers", &hkey
))
340 DWORD type
, count
= sizeof(buffer
);
341 RegQueryValueExA( hkey
, "Graphics", 0, &type
, (LPBYTE
) buffer
, &count
);
348 next
= strchr( name
, ',' );
349 if (next
) *next
++ = 0;
351 snprintf( libname
, sizeof(libname
), "wine%s.drv", name
);
352 if ((module
= LoadLibraryA( libname
)) != 0) break;
357 if (!(driver
= create_driver( module
)))
359 MESSAGE( "Could not create graphics driver '%s'\n", buffer
);
360 FreeLibrary( module
);
363 if (InterlockedCompareExchangePointer( (void **)&display_driver
, driver
, NULL
))
365 /* somebody beat us to it */
366 FreeLibrary( driver
->module
);
367 HeapFree( GetProcessHeap(), 0, driver
);
369 return &display_driver
->funcs
;
373 /**********************************************************************
376 const DC_FUNCTIONS
*DRIVER_load_driver( LPCWSTR name
)
379 struct graphics_driver
*driver
, *new_driver
;
380 static const WCHAR displayW
[] = { 'd','i','s','p','l','a','y',0 };
381 static const WCHAR display1W
[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
383 /* display driver is a special case */
384 if (!strcmpiW( name
, displayW
) || !strcmpiW( name
, display1W
)) return DRIVER_get_display_driver();
386 if ((module
= GetModuleHandleW( name
)))
388 if (display_driver
&& display_driver
->module
== module
) return &display_driver
->funcs
;
389 EnterCriticalSection( &driver_section
);
390 LIST_FOR_EACH_ENTRY( driver
, &drivers
, struct graphics_driver
, entry
)
392 if (driver
->module
== module
) goto done
;
394 LeaveCriticalSection( &driver_section
);
397 if (!(module
= LoadLibraryW( name
))) return NULL
;
399 if (!(new_driver
= create_driver( module
)))
401 FreeLibrary( module
);
405 /* check if someone else added it in the meantime */
406 EnterCriticalSection( &driver_section
);
407 LIST_FOR_EACH_ENTRY( driver
, &drivers
, struct graphics_driver
, entry
)
409 if (driver
->module
!= module
) continue;
410 FreeLibrary( module
);
411 HeapFree( GetProcessHeap(), 0, new_driver
);
415 list_add_head( &drivers
, &driver
->entry
);
416 TRACE( "loaded driver %p for %s\n", driver
, debugstr_w(name
) );
418 LeaveCriticalSection( &driver_section
);
419 return &driver
->funcs
;
423 /*****************************************************************************
424 * DRIVER_GetDriverName
427 BOOL
DRIVER_GetDriverName( LPCWSTR device
, LPWSTR driver
, DWORD size
)
429 static const WCHAR displayW
[] = { 'd','i','s','p','l','a','y',0 };
430 static const WCHAR devicesW
[] = { 'd','e','v','i','c','e','s',0 };
431 static const WCHAR display1W
[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
432 static const WCHAR empty_strW
[] = { 0 };
435 /* display is a special case */
436 if (!strcmpiW( device
, displayW
) ||
437 !strcmpiW( device
, display1W
))
439 lstrcpynW( driver
, displayW
, size
);
443 size
= GetProfileStringW(devicesW
, device
, empty_strW
, driver
, size
);
445 WARN("Unable to find %s in [devices] section of win.ini\n", debugstr_w(device
));
448 p
= strchrW(driver
, ',');
451 WARN("%s entry in [devices] section of win.ini is malformed.\n", debugstr_w(device
));
455 TRACE("Found %s for %s\n", debugstr_w(driver
), debugstr_w(device
));
460 /***********************************************************************
461 * GdiConvertToDevmodeW (GDI32.@)
463 DEVMODEW
* WINAPI
GdiConvertToDevmodeW(const DEVMODEA
*dmA
)
466 WORD dmW_size
, dmA_size
;
468 dmA_size
= dmA
->dmSize
;
470 /* this is the minimal dmSize that XP accepts */
471 if (dmA_size
< FIELD_OFFSET(DEVMODEA
, dmFields
))
474 if (dmA_size
> sizeof(DEVMODEA
))
475 dmA_size
= sizeof(DEVMODEA
);
477 dmW_size
= dmA_size
+ CCHDEVICENAME
;
478 if (dmA_size
>= FIELD_OFFSET(DEVMODEA
, dmFormName
) + CCHFORMNAME
)
479 dmW_size
+= CCHFORMNAME
;
481 dmW
= HeapAlloc(GetProcessHeap(), 0, dmW_size
+ dmA
->dmDriverExtra
);
482 if (!dmW
) return NULL
;
484 MultiByteToWideChar(CP_ACP
, 0, (const char*) dmA
->dmDeviceName
, -1,
485 dmW
->dmDeviceName
, CCHDEVICENAME
);
486 /* copy slightly more, to avoid long computations */
487 memcpy(&dmW
->dmSpecVersion
, &dmA
->dmSpecVersion
, dmA_size
- CCHDEVICENAME
);
489 if (dmA_size
>= FIELD_OFFSET(DEVMODEA
, dmFormName
) + CCHFORMNAME
)
491 if (dmA
->dmFields
& DM_FORMNAME
)
492 MultiByteToWideChar(CP_ACP
, 0, (const char*) dmA
->dmFormName
, -1,
493 dmW
->dmFormName
, CCHFORMNAME
);
495 dmW
->dmFormName
[0] = 0;
497 if (dmA_size
> FIELD_OFFSET(DEVMODEA
, dmLogPixels
))
498 memcpy(&dmW
->dmLogPixels
, &dmA
->dmLogPixels
, dmA_size
- FIELD_OFFSET(DEVMODEA
, dmLogPixels
));
501 if (dmA
->dmDriverExtra
)
502 memcpy((char *)dmW
+ dmW_size
, (const char *)dmA
+ dmA_size
, dmA
->dmDriverExtra
);
504 dmW
->dmSize
= dmW_size
;
510 /*****************************************************************************
513 * This should thunk to 16-bit and simply call the proc with the given args.
515 INT WINAPI
GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc
, HWND hWnd
,
516 LPSTR lpModelName
, LPSTR OldPort
, LPSTR NewPort
)
518 FIXME("(%p, %p, %s, %s, %s)\n", lpfnDevInstallProc
, hWnd
, lpModelName
, OldPort
, NewPort
);
522 /*****************************************************************************
525 * This should load the correct driver for lpszDevice and calls this driver's
526 * ExtDeviceModePropSheet proc.
528 * Note: The driver calls a callback routine for each property sheet page; these
529 * pages are supposed to be filled into the structure pointed to by lpPropSheet.
530 * The layout of this structure is:
536 * HPROPSHEETPAGE pages[10];
539 INT WINAPI
GDI_CallExtDeviceModePropSheet16( HWND hWnd
, LPCSTR lpszDevice
,
540 LPCSTR lpszPort
, LPVOID lpPropSheet
)
542 FIXME("(%p, %s, %s, %p)\n", hWnd
, lpszDevice
, lpszPort
, lpPropSheet
);
546 /*****************************************************************************
549 * This should load the correct driver for lpszDevice and call this driver's
550 * ExtDeviceMode proc.
552 * FIXME: convert ExtDeviceMode to unicode in the driver interface
554 INT WINAPI
GDI_CallExtDeviceMode16( HWND hwnd
,
555 LPDEVMODEA lpdmOutput
, LPSTR lpszDevice
,
556 LPSTR lpszPort
, LPDEVMODEA lpdmInput
,
557 LPSTR lpszProfile
, DWORD fwMode
)
566 TRACE("(%p, %p, %s, %s, %p, %s, %d)\n",
567 hwnd
, lpdmOutput
, lpszDevice
, lpszPort
, lpdmInput
, lpszProfile
, fwMode
);
569 if (!lpszDevice
) return -1;
570 if (!MultiByteToWideChar(CP_ACP
, 0, lpszDevice
, -1, deviceW
, 300)) return -1;
572 if(!DRIVER_GetDriverName( deviceW
, bufW
, 300 )) return -1;
574 if (!WideCharToMultiByte(CP_ACP
, 0, bufW
, -1, buf
, 300, NULL
, NULL
)) return -1;
576 if (!(hdc
= CreateICA( buf
, lpszDevice
, lpszPort
, NULL
))) return -1;
578 if ((dc
= get_dc_ptr( hdc
)))
580 if (dc
->funcs
->pExtDeviceMode
)
581 ret
= dc
->funcs
->pExtDeviceMode( buf
, hwnd
, lpdmOutput
, lpszDevice
, lpszPort
,
582 lpdmInput
, lpszProfile
, fwMode
);
583 release_dc_ptr( dc
);
589 /****************************************************************************
592 * This should load the correct driver for lpszDevice and calls this driver's
593 * AdvancedSetupDialog proc.
595 INT WINAPI
GDI_CallAdvancedSetupDialog16( HWND hwnd
, LPSTR lpszDevice
,
596 LPDEVMODEA devin
, LPDEVMODEA devout
)
598 TRACE("(%p, %s, %p, %p)\n", hwnd
, lpszDevice
, devin
, devout
);
602 /*****************************************************************************
605 * This should load the correct driver for lpszDevice and calls this driver's
606 * DeviceCapabilities proc.
608 * FIXME: convert DeviceCapabilities to unicode in the driver interface
610 DWORD WINAPI
GDI_CallDeviceCapabilities16( LPCSTR lpszDevice
, LPCSTR lpszPort
,
611 WORD fwCapability
, LPSTR lpszOutput
,
621 TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice
, lpszPort
, fwCapability
, lpszOutput
, lpdm
);
623 if (!lpszDevice
) return -1;
624 if (!MultiByteToWideChar(CP_ACP
, 0, lpszDevice
, -1, deviceW
, 300)) return -1;
626 if(!DRIVER_GetDriverName( deviceW
, bufW
, 300 )) return -1;
628 if (!WideCharToMultiByte(CP_ACP
, 0, bufW
, -1, buf
, 300, NULL
, NULL
)) return -1;
630 if (!(hdc
= CreateICA( buf
, lpszDevice
, lpszPort
, NULL
))) return -1;
632 if ((dc
= get_dc_ptr( hdc
)))
634 if (dc
->funcs
->pDeviceCapabilities
)
635 ret
= dc
->funcs
->pDeviceCapabilities( buf
, lpszDevice
, lpszPort
,
636 fwCapability
, lpszOutput
, lpdm
);
637 release_dc_ptr( dc
);
644 /************************************************************************
647 INT WINAPI
Escape( HDC hdc
, INT escape
, INT in_count
, LPCSTR in_data
, LPVOID out_data
)
655 return AbortDoc( hdc
);
658 return EndDoc( hdc
);
660 case GETPHYSPAGESIZE
:
662 pt
->x
= GetDeviceCaps( hdc
, PHYSICALWIDTH
);
663 pt
->y
= GetDeviceCaps( hdc
, PHYSICALHEIGHT
);
666 case GETPRINTINGOFFSET
:
668 pt
->x
= GetDeviceCaps( hdc
, PHYSICALOFFSETX
);
669 pt
->y
= GetDeviceCaps( hdc
, PHYSICALOFFSETY
);
672 case GETSCALINGFACTOR
:
674 pt
->x
= GetDeviceCaps( hdc
, SCALINGFACTORX
);
675 pt
->y
= GetDeviceCaps( hdc
, SCALINGFACTORY
);
679 return EndPage( hdc
);
682 return SetAbortProc( hdc
, (ABORTPROC
)in_data
);
689 /* in_data may not be 0 terminated so we must copy it */
692 name
= HeapAlloc( GetProcessHeap(), 0, in_count
+1 );
693 memcpy( name
, in_data
, in_count
);
696 /* out_data is actually a pointer to the DocInfo structure and used as
697 * a second input parameter */
698 if (out_data
) doc
= *(DOCINFOA
*)out_data
;
701 doc
.cbSize
= sizeof(doc
);
702 doc
.lpszOutput
= NULL
;
703 doc
.lpszDatatype
= NULL
;
706 doc
.lpszDocName
= name
;
707 ret
= StartDocA( hdc
, &doc
);
708 HeapFree( GetProcessHeap(), 0, name
);
709 if (ret
> 0) ret
= StartPage( hdc
);
713 case QUERYESCSUPPORT
:
715 const INT
*ptr
= (const INT
*)in_data
;
716 if (in_count
< sizeof(INT
)) return 0;
721 case GETPHYSPAGESIZE
:
722 case GETPRINTINGOFFSET
:
723 case GETSCALINGFACTOR
:
725 case QUERYESCSUPPORT
:
734 /* if not handled internally, pass it to the driver */
735 return ExtEscape( hdc
, escape
, in_count
, in_data
, 0, out_data
);
739 /******************************************************************************
740 * ExtEscape [GDI32.@]
742 * Access capabilities of a particular device that are not available through GDI.
745 * hdc [I] Handle to device context
746 * nEscape [I] Escape function
747 * cbInput [I] Number of bytes in input structure
748 * lpszInData [I] Pointer to input structure
749 * cbOutput [I] Number of bytes in output structure
750 * lpszOutData [O] Pointer to output structure
757 INT WINAPI
ExtEscape( HDC hdc
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
,
758 INT cbOutput
, LPSTR lpszOutData
)
761 DC
* dc
= get_dc_ptr( hdc
);
764 if (dc
->funcs
->pExtEscape
)
765 ret
= dc
->funcs
->pExtEscape( dc
->physDev
, nEscape
, cbInput
, lpszInData
, cbOutput
, lpszOutData
);
766 release_dc_ptr( dc
);
772 /*******************************************************************
773 * DrawEscape [GDI32.@]
777 INT WINAPI
DrawEscape(HDC hdc
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
)
779 FIXME("DrawEscape, stub\n");
783 /*******************************************************************
784 * NamedEscape [GDI32.@]
786 INT WINAPI
NamedEscape( HDC hdc
, LPCWSTR pDriver
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
,
787 INT cbOutput
, LPSTR lpszOutData
)
789 FIXME("(%p, %s, %d, %d, %p, %d, %p)\n",
790 hdc
, wine_dbgstr_w(pDriver
), nEscape
, cbInput
, lpszInData
, cbOutput
,
795 /*******************************************************************
796 * DdQueryDisplaySettingsUniqueness [GDI32.@]
797 * GdiEntry13 [GDI32.@]
799 ULONG WINAPI
DdQueryDisplaySettingsUniqueness(VOID
)
801 static int warn_once
;