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"
32 #include "wine/winbase16.h"
34 #include "gdi_private.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(driver
);
40 struct graphics_driver
42 struct graphics_driver
*next
;
43 struct graphics_driver
*prev
;
44 HMODULE module
; /* module handle */
45 unsigned int count
; /* reference count */
49 static struct graphics_driver
*first_driver
;
50 static struct graphics_driver
*display_driver
;
52 static CRITICAL_SECTION driver_section
;
53 static CRITICAL_SECTION_DEBUG critsect_debug
=
55 0, 0, &driver_section
,
56 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
57 0, 0, { (DWORD_PTR
)(__FILE__
": driver_section") }
59 static CRITICAL_SECTION driver_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
61 /**********************************************************************
64 * Allocate and fill the driver structure for a given module.
66 static struct graphics_driver
*create_driver( HMODULE module
)
68 struct graphics_driver
*driver
;
70 if (!(driver
= HeapAlloc( GetProcessHeap(), 0, sizeof(*driver
)))) return NULL
;
73 driver
->module
= module
;
76 /* fill the function table */
79 #define GET_FUNC(name) driver->funcs.p##name = (void*)GetProcAddress( module, #name )
88 GET_FUNC(ChoosePixelFormat
);
90 GET_FUNC(CloseFigure
);
91 GET_FUNC(CreateBitmap
);
93 GET_FUNC(CreateDIBSection
);
94 GET_FUNC(DeleteBitmap
);
96 GET_FUNC(DescribePixelFormat
);
97 GET_FUNC(DeviceCapabilities
);
102 GET_FUNC(EnumDeviceFonts
);
103 GET_FUNC(ExcludeClipRect
);
104 GET_FUNC(ExtDeviceMode
);
106 GET_FUNC(ExtFloodFill
);
107 GET_FUNC(ExtSelectClipRgn
);
108 GET_FUNC(ExtTextOut
);
111 GET_FUNC(FlattenPath
);
113 GET_FUNC(GdiComment
);
114 GET_FUNC(GetBitmapBits
);
115 GET_FUNC(GetCharWidth
);
116 GET_FUNC(GetDCOrgEx
);
117 GET_FUNC(GetDIBColorTable
);
119 GET_FUNC(GetDeviceCaps
);
120 GET_FUNC(GetDeviceGammaRamp
);
121 GET_FUNC(GetICMProfile
);
122 GET_FUNC(GetNearestColor
);
124 GET_FUNC(GetPixelFormat
);
125 GET_FUNC(GetSystemPaletteEntries
);
126 GET_FUNC(GetTextExtentExPoint
);
127 GET_FUNC(GetTextMetrics
);
128 GET_FUNC(IntersectClipRect
);
132 GET_FUNC(ModifyWorldTransform
);
133 GET_FUNC(OffsetClipRgn
);
134 GET_FUNC(OffsetViewportOrg
);
135 GET_FUNC(OffsetWindowOrg
);
139 GET_FUNC(PolyBezier
);
140 GET_FUNC(PolyBezierTo
);
142 GET_FUNC(PolyPolygon
);
143 GET_FUNC(PolyPolyline
);
146 GET_FUNC(PolylineTo
);
147 GET_FUNC(RealizeDefaultPalette
);
148 GET_FUNC(RealizePalette
);
154 GET_FUNC(ScaleViewportExt
);
155 GET_FUNC(ScaleWindowExt
);
156 GET_FUNC(SelectBitmap
);
157 GET_FUNC(SelectBrush
);
158 GET_FUNC(SelectClipPath
);
159 GET_FUNC(SelectFont
);
160 GET_FUNC(SelectPalette
);
162 GET_FUNC(SetArcDirection
);
163 GET_FUNC(SetBitmapBits
);
164 GET_FUNC(SetBkColor
);
166 GET_FUNC(SetDCBrushColor
);
167 GET_FUNC(SetDCPenColor
);
168 GET_FUNC(SetDIBColorTable
);
170 GET_FUNC(SetDIBitsToDevice
);
171 GET_FUNC(SetDeviceClipping
);
172 GET_FUNC(SetDeviceGammaRamp
);
173 GET_FUNC(SetMapMode
);
174 GET_FUNC(SetMapperFlags
);
176 GET_FUNC(SetPixelFormat
);
177 GET_FUNC(SetPolyFillMode
);
180 GET_FUNC(SetStretchBltMode
);
181 GET_FUNC(SetTextAlign
);
182 GET_FUNC(SetTextCharacterExtra
);
183 GET_FUNC(SetTextColor
);
184 GET_FUNC(SetTextJustification
);
185 GET_FUNC(SetViewportExt
);
186 GET_FUNC(SetViewportOrg
);
187 GET_FUNC(SetWindowExt
);
188 GET_FUNC(SetWindowOrg
);
189 GET_FUNC(SetWorldTransform
);
192 GET_FUNC(StretchBlt
);
193 GET_FUNC(StretchDIBits
);
194 GET_FUNC(StrokeAndFillPath
);
195 GET_FUNC(StrokePath
);
196 GET_FUNC(SwapBuffers
);
197 GET_FUNC(UnrealizePalette
);
201 GET_FUNC(wglCreateContext
);
202 GET_FUNC(wglCreateContextAttribsARB
);
203 GET_FUNC(wglDeleteContext
);
204 GET_FUNC(wglGetProcAddress
);
205 GET_FUNC(wglGetPbufferDCARB
);
206 GET_FUNC(wglMakeContextCurrentARB
);
207 GET_FUNC(wglMakeCurrent
);
208 GET_FUNC(wglSetPixelFormatWINE
);
209 GET_FUNC(wglShareLists
);
210 GET_FUNC(wglUseFontBitmapsA
);
211 GET_FUNC(wglUseFontBitmapsW
);
214 else memset( &driver
->funcs
, 0, sizeof(driver
->funcs
) );
216 /* add it to the list */
218 if ((driver
->next
= first_driver
)) driver
->next
->prev
= driver
;
219 first_driver
= driver
;
224 /**********************************************************************
225 * load_display_driver
227 * Special case for loading the display driver: get the name from the config file
229 static struct graphics_driver
*load_display_driver(void)
231 char buffer
[MAX_PATH
], libname
[32], *name
, *next
;
235 if (display_driver
) /* already loaded */
237 display_driver
->count
++;
238 return display_driver
;
241 strcpy( buffer
, "x11" ); /* default value */
242 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
243 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Drivers", &hkey
))
245 DWORD type
, count
= sizeof(buffer
);
246 RegQueryValueExA( hkey
, "Graphics", 0, &type
, (LPBYTE
) buffer
, &count
);
253 next
= strchr( name
, ',' );
254 if (next
) *next
++ = 0;
256 snprintf( libname
, sizeof(libname
), "wine%s.drv", name
);
257 if ((module
= LoadLibraryA( libname
)) != 0) break;
261 if (!(display_driver
= create_driver( module
)))
263 MESSAGE( "Could not create graphics driver '%s'\n", buffer
);
264 FreeLibrary( module
);
268 display_driver
->count
++; /* we don't want to free it */
269 return display_driver
;
273 /**********************************************************************
276 const DC_FUNCTIONS
*DRIVER_load_driver( LPCWSTR name
)
279 struct graphics_driver
*driver
;
280 static const WCHAR displayW
[] = { 'd','i','s','p','l','a','y',0 };
281 static const WCHAR display1W
[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
283 EnterCriticalSection( &driver_section
);
285 /* display driver is a special case */
286 if (!strcmpiW( name
, displayW
) ||
287 !strcmpiW( name
, display1W
))
289 driver
= load_display_driver();
290 LeaveCriticalSection( &driver_section
);
291 return &driver
->funcs
;
294 if ((module
= GetModuleHandleW( name
)))
296 for (driver
= first_driver
; driver
; driver
= driver
->next
)
298 if (driver
->module
== module
)
301 LeaveCriticalSection( &driver_section
);
302 return &driver
->funcs
;
307 if (!(module
= LoadLibraryW( name
)))
309 LeaveCriticalSection( &driver_section
);
313 if (!(driver
= create_driver( module
)))
315 FreeLibrary( module
);
316 LeaveCriticalSection( &driver_section
);
320 TRACE( "loaded driver %p for %s\n", driver
, debugstr_w(name
) );
321 LeaveCriticalSection( &driver_section
);
322 return &driver
->funcs
;
326 /**********************************************************************
329 * Get a new copy of an existing driver.
331 const DC_FUNCTIONS
*DRIVER_get_driver( const DC_FUNCTIONS
*funcs
)
333 struct graphics_driver
*driver
;
335 EnterCriticalSection( &driver_section
);
336 for (driver
= first_driver
; driver
; driver
= driver
->next
)
337 if (&driver
->funcs
== funcs
) break;
338 if (!driver
) ERR( "driver not found, trouble ahead\n" );
340 LeaveCriticalSection( &driver_section
);
345 /**********************************************************************
346 * DRIVER_release_driver
348 * Release a driver by decrementing ref count and freeing it if needed.
350 void DRIVER_release_driver( const DC_FUNCTIONS
*funcs
)
352 struct graphics_driver
*driver
;
354 EnterCriticalSection( &driver_section
);
356 for (driver
= first_driver
; driver
; driver
= driver
->next
)
357 if (&driver
->funcs
== funcs
) break;
359 if (!driver
) goto done
;
360 if (--driver
->count
) goto done
;
362 /* removed last reference, free it */
363 if (driver
->next
) driver
->next
->prev
= driver
->prev
;
364 if (driver
->prev
) driver
->prev
->next
= driver
->next
;
365 else first_driver
= driver
->next
;
366 if (driver
== display_driver
) display_driver
= NULL
;
368 FreeLibrary( driver
->module
);
369 HeapFree( GetProcessHeap(), 0, driver
);
371 LeaveCriticalSection( &driver_section
);
375 /*****************************************************************************
376 * DRIVER_GetDriverName
379 BOOL
DRIVER_GetDriverName( LPCWSTR device
, LPWSTR driver
, DWORD size
)
381 static const WCHAR displayW
[] = { 'd','i','s','p','l','a','y',0 };
382 static const WCHAR devicesW
[] = { 'd','e','v','i','c','e','s',0 };
383 static const WCHAR display1W
[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
384 static const WCHAR empty_strW
[] = { 0 };
387 /* display is a special case */
388 if (!strcmpiW( device
, displayW
) ||
389 !strcmpiW( device
, display1W
))
391 lstrcpynW( driver
, displayW
, size
);
395 size
= GetProfileStringW(devicesW
, device
, empty_strW
, driver
, size
);
397 WARN("Unable to find %s in [devices] section of win.ini\n", debugstr_w(device
));
400 p
= strchrW(driver
, ',');
403 WARN("%s entry in [devices] section of win.ini is malformed.\n", debugstr_w(device
));
407 TRACE("Found %s for %s\n", debugstr_w(driver
), debugstr_w(device
));
412 /***********************************************************************
413 * GdiConvertToDevmodeW (GDI32.@)
415 DEVMODEW
* WINAPI
GdiConvertToDevmodeW(const DEVMODEA
*dmA
)
418 WORD dmW_size
, dmA_size
;
420 dmA_size
= dmA
->dmSize
;
422 /* this is the minimal dmSize that XP accepts */
423 if (dmA_size
< FIELD_OFFSET(DEVMODEA
, dmFields
))
426 if (dmA_size
> sizeof(DEVMODEA
))
427 dmA_size
= sizeof(DEVMODEA
);
429 dmW_size
= dmA_size
+ CCHDEVICENAME
;
430 if (dmA_size
>= FIELD_OFFSET(DEVMODEA
, dmFormName
) + CCHFORMNAME
)
431 dmW_size
+= CCHFORMNAME
;
433 dmW
= HeapAlloc(GetProcessHeap(), 0, dmW_size
+ dmA
->dmDriverExtra
);
434 if (!dmW
) return NULL
;
436 MultiByteToWideChar(CP_ACP
, 0, (const char*) dmA
->dmDeviceName
, -1,
437 dmW
->dmDeviceName
, CCHDEVICENAME
);
438 /* copy slightly more, to avoid long computations */
439 memcpy(&dmW
->dmSpecVersion
, &dmA
->dmSpecVersion
, dmA_size
- CCHDEVICENAME
);
441 if (dmA_size
>= FIELD_OFFSET(DEVMODEA
, dmFormName
) + CCHFORMNAME
)
443 if (dmA
->dmFields
& DM_FORMNAME
)
444 MultiByteToWideChar(CP_ACP
, 0, (const char*) dmA
->dmFormName
, -1,
445 dmW
->dmFormName
, CCHFORMNAME
);
447 dmW
->dmFormName
[0] = 0;
449 if (dmA_size
> FIELD_OFFSET(DEVMODEA
, dmLogPixels
))
450 memcpy(&dmW
->dmLogPixels
, &dmA
->dmLogPixels
, dmA_size
- FIELD_OFFSET(DEVMODEA
, dmLogPixels
));
453 if (dmA
->dmDriverExtra
)
454 memcpy((char *)dmW
+ dmW_size
, (const char *)dmA
+ dmA_size
, dmA
->dmDriverExtra
);
456 dmW
->dmSize
= dmW_size
;
462 /*****************************************************************************
465 * This should thunk to 16-bit and simply call the proc with the given args.
467 INT WINAPI
GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc
, HWND hWnd
,
468 LPSTR lpModelName
, LPSTR OldPort
, LPSTR NewPort
)
470 FIXME("(%p, %p, %s, %s, %s)\n", lpfnDevInstallProc
, hWnd
, lpModelName
, OldPort
, NewPort
);
474 /*****************************************************************************
477 * This should load the correct driver for lpszDevice and calls this driver's
478 * ExtDeviceModePropSheet proc.
480 * Note: The driver calls a callback routine for each property sheet page; these
481 * pages are supposed to be filled into the structure pointed to by lpPropSheet.
482 * The layout of this structure is:
488 * HPROPSHEETPAGE pages[10];
491 INT WINAPI
GDI_CallExtDeviceModePropSheet16( HWND hWnd
, LPCSTR lpszDevice
,
492 LPCSTR lpszPort
, LPVOID lpPropSheet
)
494 FIXME("(%p, %s, %s, %p)\n", hWnd
, lpszDevice
, lpszPort
, lpPropSheet
);
498 /*****************************************************************************
501 * This should load the correct driver for lpszDevice and call this driver's
502 * ExtDeviceMode proc.
504 * FIXME: convert ExtDeviceMode to unicode in the driver interface
506 INT WINAPI
GDI_CallExtDeviceMode16( HWND hwnd
,
507 LPDEVMODEA lpdmOutput
, LPSTR lpszDevice
,
508 LPSTR lpszPort
, LPDEVMODEA lpdmInput
,
509 LPSTR lpszProfile
, DWORD fwMode
)
518 TRACE("(%p, %p, %s, %s, %p, %s, %d)\n",
519 hwnd
, lpdmOutput
, lpszDevice
, lpszPort
, lpdmInput
, lpszProfile
, fwMode
);
521 if (!lpszDevice
) return -1;
522 if (!MultiByteToWideChar(CP_ACP
, 0, lpszDevice
, -1, deviceW
, 300)) return -1;
524 if(!DRIVER_GetDriverName( deviceW
, bufW
, 300 )) return -1;
526 if (!WideCharToMultiByte(CP_ACP
, 0, bufW
, -1, buf
, 300, NULL
, NULL
)) return -1;
528 if (!(hdc
= CreateICA( buf
, lpszDevice
, lpszPort
, NULL
))) return -1;
530 if ((dc
= get_dc_ptr( hdc
)))
532 if (dc
->funcs
->pExtDeviceMode
)
533 ret
= dc
->funcs
->pExtDeviceMode( buf
, hwnd
, lpdmOutput
, lpszDevice
, lpszPort
,
534 lpdmInput
, lpszProfile
, fwMode
);
535 release_dc_ptr( dc
);
541 /****************************************************************************
544 * This should load the correct driver for lpszDevice and calls this driver's
545 * AdvancedSetupDialog proc.
547 INT WINAPI
GDI_CallAdvancedSetupDialog16( HWND hwnd
, LPSTR lpszDevice
,
548 LPDEVMODEA devin
, LPDEVMODEA devout
)
550 TRACE("(%p, %s, %p, %p)\n", hwnd
, lpszDevice
, devin
, devout
);
554 /*****************************************************************************
557 * This should load the correct driver for lpszDevice and calls this driver's
558 * DeviceCapabilities proc.
560 * FIXME: convert DeviceCapabilities to unicode in the driver interface
562 DWORD WINAPI
GDI_CallDeviceCapabilities16( LPCSTR lpszDevice
, LPCSTR lpszPort
,
563 WORD fwCapability
, LPSTR lpszOutput
,
573 TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice
, lpszPort
, fwCapability
, lpszOutput
, lpdm
);
575 if (!lpszDevice
) return -1;
576 if (!MultiByteToWideChar(CP_ACP
, 0, lpszDevice
, -1, deviceW
, 300)) return -1;
578 if(!DRIVER_GetDriverName( deviceW
, bufW
, 300 )) return -1;
580 if (!WideCharToMultiByte(CP_ACP
, 0, bufW
, -1, buf
, 300, NULL
, NULL
)) return -1;
582 if (!(hdc
= CreateICA( buf
, lpszDevice
, lpszPort
, NULL
))) return -1;
584 if ((dc
= get_dc_ptr( hdc
)))
586 if (dc
->funcs
->pDeviceCapabilities
)
587 ret
= dc
->funcs
->pDeviceCapabilities( buf
, lpszDevice
, lpszPort
,
588 fwCapability
, lpszOutput
, lpdm
);
589 release_dc_ptr( dc
);
596 /************************************************************************
599 INT WINAPI
Escape( HDC hdc
, INT escape
, INT in_count
, LPCSTR in_data
, LPVOID out_data
)
607 return AbortDoc( hdc
);
610 return EndDoc( hdc
);
612 case GETPHYSPAGESIZE
:
614 pt
->x
= GetDeviceCaps( hdc
, PHYSICALWIDTH
);
615 pt
->y
= GetDeviceCaps( hdc
, PHYSICALHEIGHT
);
618 case GETPRINTINGOFFSET
:
620 pt
->x
= GetDeviceCaps( hdc
, PHYSICALOFFSETX
);
621 pt
->y
= GetDeviceCaps( hdc
, PHYSICALOFFSETY
);
624 case GETSCALINGFACTOR
:
626 pt
->x
= GetDeviceCaps( hdc
, SCALINGFACTORX
);
627 pt
->y
= GetDeviceCaps( hdc
, SCALINGFACTORY
);
631 return EndPage( hdc
);
634 return SetAbortProc( hdc
, (ABORTPROC
)in_data
);
641 /* in_data may not be 0 terminated so we must copy it */
644 name
= HeapAlloc( GetProcessHeap(), 0, in_count
+1 );
645 memcpy( name
, in_data
, in_count
);
648 /* out_data is actually a pointer to the DocInfo structure and used as
649 * a second input parameter */
650 if (out_data
) doc
= *(DOCINFOA
*)out_data
;
653 doc
.cbSize
= sizeof(doc
);
654 doc
.lpszOutput
= NULL
;
655 doc
.lpszDatatype
= NULL
;
658 doc
.lpszDocName
= name
;
659 ret
= StartDocA( hdc
, &doc
);
660 HeapFree( GetProcessHeap(), 0, name
);
661 if (ret
> 0) ret
= StartPage( hdc
);
665 case QUERYESCSUPPORT
:
667 const INT
*ptr
= (const INT
*)in_data
;
668 if (in_count
< sizeof(INT
)) return 0;
673 case GETPHYSPAGESIZE
:
674 case GETPRINTINGOFFSET
:
675 case GETSCALINGFACTOR
:
677 case QUERYESCSUPPORT
:
686 /* if not handled internally, pass it to the driver */
687 return ExtEscape( hdc
, escape
, in_count
, in_data
, 0, out_data
);
691 /******************************************************************************
692 * ExtEscape [GDI32.@]
694 * Access capabilities of a particular device that are not available through GDI.
697 * hdc [I] Handle to device context
698 * nEscape [I] Escape function
699 * cbInput [I] Number of bytes in input structure
700 * lpszInData [I] Pointer to input structure
701 * cbOutput [I] Number of bytes in output structure
702 * lpszOutData [O] Pointer to output structure
709 INT WINAPI
ExtEscape( HDC hdc
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
,
710 INT cbOutput
, LPSTR lpszOutData
)
713 DC
* dc
= get_dc_ptr( hdc
);
716 if (dc
->funcs
->pExtEscape
)
717 ret
= dc
->funcs
->pExtEscape( dc
->physDev
, nEscape
, cbInput
, lpszInData
, cbOutput
, lpszOutData
);
718 release_dc_ptr( dc
);
724 /*******************************************************************
725 * DrawEscape [GDI32.@]
729 INT WINAPI
DrawEscape(HDC hdc
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
)
731 FIXME("DrawEscape, stub\n");
735 /*******************************************************************
736 * NamedEscape [GDI32.@]
738 INT WINAPI
NamedEscape( HDC hdc
, LPCWSTR pDriver
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
,
739 INT cbOutput
, LPSTR lpszOutData
)
741 FIXME("(%p, %s, %d, %d, %p, %d, %p)\n",
742 hdc
, wine_dbgstr_w(pDriver
), nEscape
, cbInput
, lpszInData
, cbOutput
,
747 /*******************************************************************
748 * DdQueryDisplaySettingsUniqueness [GDI32.@]
749 * GdiEntry13 [GDI32.@]
751 ULONG WINAPI
DdQueryDisplaySettingsUniqueness(VOID
)