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/list.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(driver
);
41 struct graphics_driver
44 HMODULE module
; /* module handle */
45 unsigned int count
; /* reference count */
49 static struct list drivers
= LIST_INIT( drivers
);
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
;
71 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
) );
214 list_add_head( &drivers
, &driver
->entry
);
219 /**********************************************************************
220 * load_display_driver
222 * Special case for loading the display driver: get the name from the config file
224 static struct graphics_driver
*load_display_driver(void)
226 char buffer
[MAX_PATH
], libname
[32], *name
, *next
;
230 if (display_driver
) /* already loaded */
232 display_driver
->count
++;
233 return display_driver
;
236 strcpy( buffer
, "x11" ); /* default value */
237 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
238 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Drivers", &hkey
))
240 DWORD type
, count
= sizeof(buffer
);
241 RegQueryValueExA( hkey
, "Graphics", 0, &type
, (LPBYTE
) buffer
, &count
);
248 next
= strchr( name
, ',' );
249 if (next
) *next
++ = 0;
251 snprintf( libname
, sizeof(libname
), "wine%s.drv", name
);
252 if ((module
= LoadLibraryA( libname
)) != 0) break;
256 if (!(display_driver
= create_driver( module
)))
258 MESSAGE( "Could not create graphics driver '%s'\n", buffer
);
259 FreeLibrary( module
);
263 display_driver
->count
++; /* we don't want to free it */
264 return display_driver
;
268 /**********************************************************************
271 const DC_FUNCTIONS
*DRIVER_load_driver( LPCWSTR name
)
274 struct graphics_driver
*driver
;
275 static const WCHAR displayW
[] = { 'd','i','s','p','l','a','y',0 };
276 static const WCHAR display1W
[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
278 EnterCriticalSection( &driver_section
);
280 /* display driver is a special case */
281 if (!strcmpiW( name
, displayW
) ||
282 !strcmpiW( name
, display1W
))
284 driver
= load_display_driver();
285 LeaveCriticalSection( &driver_section
);
286 return &driver
->funcs
;
289 if ((module
= GetModuleHandleW( name
)))
291 LIST_FOR_EACH_ENTRY( driver
, &drivers
, struct graphics_driver
, entry
)
293 if (driver
->module
== module
)
296 LeaveCriticalSection( &driver_section
);
297 return &driver
->funcs
;
302 if (!(module
= LoadLibraryW( name
)))
304 LeaveCriticalSection( &driver_section
);
308 if (!(driver
= create_driver( module
)))
310 FreeLibrary( module
);
311 LeaveCriticalSection( &driver_section
);
315 TRACE( "loaded driver %p for %s\n", driver
, debugstr_w(name
) );
316 LeaveCriticalSection( &driver_section
);
317 return &driver
->funcs
;
321 /**********************************************************************
324 * Get a new copy of an existing driver.
326 const DC_FUNCTIONS
*DRIVER_get_driver( const DC_FUNCTIONS
*funcs
)
328 struct graphics_driver
*driver
;
330 EnterCriticalSection( &driver_section
);
331 LIST_FOR_EACH_ENTRY( driver
, &drivers
, struct graphics_driver
, entry
)
332 if (&driver
->funcs
== funcs
) break;
333 if (&driver
->entry
== &drivers
) ERR( "driver not found, trouble ahead\n" );
335 LeaveCriticalSection( &driver_section
);
340 /**********************************************************************
341 * DRIVER_release_driver
343 * Release a driver by decrementing ref count and freeing it if needed.
345 void DRIVER_release_driver( const DC_FUNCTIONS
*funcs
)
347 struct graphics_driver
*driver
;
349 EnterCriticalSection( &driver_section
);
351 LIST_FOR_EACH_ENTRY( driver
, &drivers
, struct graphics_driver
, entry
)
353 if (&driver
->funcs
!= funcs
) continue;
354 if (--driver
->count
) break;
355 list_remove( &driver
->entry
);
356 if (driver
== display_driver
) display_driver
= NULL
;
357 FreeLibrary( driver
->module
);
358 HeapFree( GetProcessHeap(), 0, driver
);
361 LeaveCriticalSection( &driver_section
);
365 /*****************************************************************************
366 * DRIVER_GetDriverName
369 BOOL
DRIVER_GetDriverName( LPCWSTR device
, LPWSTR driver
, DWORD size
)
371 static const WCHAR displayW
[] = { 'd','i','s','p','l','a','y',0 };
372 static const WCHAR devicesW
[] = { 'd','e','v','i','c','e','s',0 };
373 static const WCHAR display1W
[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
374 static const WCHAR empty_strW
[] = { 0 };
377 /* display is a special case */
378 if (!strcmpiW( device
, displayW
) ||
379 !strcmpiW( device
, display1W
))
381 lstrcpynW( driver
, displayW
, size
);
385 size
= GetProfileStringW(devicesW
, device
, empty_strW
, driver
, size
);
387 WARN("Unable to find %s in [devices] section of win.ini\n", debugstr_w(device
));
390 p
= strchrW(driver
, ',');
393 WARN("%s entry in [devices] section of win.ini is malformed.\n", debugstr_w(device
));
397 TRACE("Found %s for %s\n", debugstr_w(driver
), debugstr_w(device
));
402 /***********************************************************************
403 * GdiConvertToDevmodeW (GDI32.@)
405 DEVMODEW
* WINAPI
GdiConvertToDevmodeW(const DEVMODEA
*dmA
)
408 WORD dmW_size
, dmA_size
;
410 dmA_size
= dmA
->dmSize
;
412 /* this is the minimal dmSize that XP accepts */
413 if (dmA_size
< FIELD_OFFSET(DEVMODEA
, dmFields
))
416 if (dmA_size
> sizeof(DEVMODEA
))
417 dmA_size
= sizeof(DEVMODEA
);
419 dmW_size
= dmA_size
+ CCHDEVICENAME
;
420 if (dmA_size
>= FIELD_OFFSET(DEVMODEA
, dmFormName
) + CCHFORMNAME
)
421 dmW_size
+= CCHFORMNAME
;
423 dmW
= HeapAlloc(GetProcessHeap(), 0, dmW_size
+ dmA
->dmDriverExtra
);
424 if (!dmW
) return NULL
;
426 MultiByteToWideChar(CP_ACP
, 0, (const char*) dmA
->dmDeviceName
, -1,
427 dmW
->dmDeviceName
, CCHDEVICENAME
);
428 /* copy slightly more, to avoid long computations */
429 memcpy(&dmW
->dmSpecVersion
, &dmA
->dmSpecVersion
, dmA_size
- CCHDEVICENAME
);
431 if (dmA_size
>= FIELD_OFFSET(DEVMODEA
, dmFormName
) + CCHFORMNAME
)
433 if (dmA
->dmFields
& DM_FORMNAME
)
434 MultiByteToWideChar(CP_ACP
, 0, (const char*) dmA
->dmFormName
, -1,
435 dmW
->dmFormName
, CCHFORMNAME
);
437 dmW
->dmFormName
[0] = 0;
439 if (dmA_size
> FIELD_OFFSET(DEVMODEA
, dmLogPixels
))
440 memcpy(&dmW
->dmLogPixels
, &dmA
->dmLogPixels
, dmA_size
- FIELD_OFFSET(DEVMODEA
, dmLogPixels
));
443 if (dmA
->dmDriverExtra
)
444 memcpy((char *)dmW
+ dmW_size
, (const char *)dmA
+ dmA_size
, dmA
->dmDriverExtra
);
446 dmW
->dmSize
= dmW_size
;
452 /*****************************************************************************
455 * This should thunk to 16-bit and simply call the proc with the given args.
457 INT WINAPI
GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc
, HWND hWnd
,
458 LPSTR lpModelName
, LPSTR OldPort
, LPSTR NewPort
)
460 FIXME("(%p, %p, %s, %s, %s)\n", lpfnDevInstallProc
, hWnd
, lpModelName
, OldPort
, NewPort
);
464 /*****************************************************************************
467 * This should load the correct driver for lpszDevice and calls this driver's
468 * ExtDeviceModePropSheet proc.
470 * Note: The driver calls a callback routine for each property sheet page; these
471 * pages are supposed to be filled into the structure pointed to by lpPropSheet.
472 * The layout of this structure is:
478 * HPROPSHEETPAGE pages[10];
481 INT WINAPI
GDI_CallExtDeviceModePropSheet16( HWND hWnd
, LPCSTR lpszDevice
,
482 LPCSTR lpszPort
, LPVOID lpPropSheet
)
484 FIXME("(%p, %s, %s, %p)\n", hWnd
, lpszDevice
, lpszPort
, lpPropSheet
);
488 /*****************************************************************************
491 * This should load the correct driver for lpszDevice and call this driver's
492 * ExtDeviceMode proc.
494 * FIXME: convert ExtDeviceMode to unicode in the driver interface
496 INT WINAPI
GDI_CallExtDeviceMode16( HWND hwnd
,
497 LPDEVMODEA lpdmOutput
, LPSTR lpszDevice
,
498 LPSTR lpszPort
, LPDEVMODEA lpdmInput
,
499 LPSTR lpszProfile
, DWORD fwMode
)
508 TRACE("(%p, %p, %s, %s, %p, %s, %d)\n",
509 hwnd
, lpdmOutput
, lpszDevice
, lpszPort
, lpdmInput
, lpszProfile
, fwMode
);
511 if (!lpszDevice
) return -1;
512 if (!MultiByteToWideChar(CP_ACP
, 0, lpszDevice
, -1, deviceW
, 300)) return -1;
514 if(!DRIVER_GetDriverName( deviceW
, bufW
, 300 )) return -1;
516 if (!WideCharToMultiByte(CP_ACP
, 0, bufW
, -1, buf
, 300, NULL
, NULL
)) return -1;
518 if (!(hdc
= CreateICA( buf
, lpszDevice
, lpszPort
, NULL
))) return -1;
520 if ((dc
= get_dc_ptr( hdc
)))
522 if (dc
->funcs
->pExtDeviceMode
)
523 ret
= dc
->funcs
->pExtDeviceMode( buf
, hwnd
, lpdmOutput
, lpszDevice
, lpszPort
,
524 lpdmInput
, lpszProfile
, fwMode
);
525 release_dc_ptr( dc
);
531 /****************************************************************************
534 * This should load the correct driver for lpszDevice and calls this driver's
535 * AdvancedSetupDialog proc.
537 INT WINAPI
GDI_CallAdvancedSetupDialog16( HWND hwnd
, LPSTR lpszDevice
,
538 LPDEVMODEA devin
, LPDEVMODEA devout
)
540 TRACE("(%p, %s, %p, %p)\n", hwnd
, lpszDevice
, devin
, devout
);
544 /*****************************************************************************
547 * This should load the correct driver for lpszDevice and calls this driver's
548 * DeviceCapabilities proc.
550 * FIXME: convert DeviceCapabilities to unicode in the driver interface
552 DWORD WINAPI
GDI_CallDeviceCapabilities16( LPCSTR lpszDevice
, LPCSTR lpszPort
,
553 WORD fwCapability
, LPSTR lpszOutput
,
563 TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice
, lpszPort
, fwCapability
, lpszOutput
, lpdm
);
565 if (!lpszDevice
) return -1;
566 if (!MultiByteToWideChar(CP_ACP
, 0, lpszDevice
, -1, deviceW
, 300)) return -1;
568 if(!DRIVER_GetDriverName( deviceW
, bufW
, 300 )) return -1;
570 if (!WideCharToMultiByte(CP_ACP
, 0, bufW
, -1, buf
, 300, NULL
, NULL
)) return -1;
572 if (!(hdc
= CreateICA( buf
, lpszDevice
, lpszPort
, NULL
))) return -1;
574 if ((dc
= get_dc_ptr( hdc
)))
576 if (dc
->funcs
->pDeviceCapabilities
)
577 ret
= dc
->funcs
->pDeviceCapabilities( buf
, lpszDevice
, lpszPort
,
578 fwCapability
, lpszOutput
, lpdm
);
579 release_dc_ptr( dc
);
586 /************************************************************************
589 INT WINAPI
Escape( HDC hdc
, INT escape
, INT in_count
, LPCSTR in_data
, LPVOID out_data
)
597 return AbortDoc( hdc
);
600 return EndDoc( hdc
);
602 case GETPHYSPAGESIZE
:
604 pt
->x
= GetDeviceCaps( hdc
, PHYSICALWIDTH
);
605 pt
->y
= GetDeviceCaps( hdc
, PHYSICALHEIGHT
);
608 case GETPRINTINGOFFSET
:
610 pt
->x
= GetDeviceCaps( hdc
, PHYSICALOFFSETX
);
611 pt
->y
= GetDeviceCaps( hdc
, PHYSICALOFFSETY
);
614 case GETSCALINGFACTOR
:
616 pt
->x
= GetDeviceCaps( hdc
, SCALINGFACTORX
);
617 pt
->y
= GetDeviceCaps( hdc
, SCALINGFACTORY
);
621 return EndPage( hdc
);
624 return SetAbortProc( hdc
, (ABORTPROC
)in_data
);
631 /* in_data may not be 0 terminated so we must copy it */
634 name
= HeapAlloc( GetProcessHeap(), 0, in_count
+1 );
635 memcpy( name
, in_data
, in_count
);
638 /* out_data is actually a pointer to the DocInfo structure and used as
639 * a second input parameter */
640 if (out_data
) doc
= *(DOCINFOA
*)out_data
;
643 doc
.cbSize
= sizeof(doc
);
644 doc
.lpszOutput
= NULL
;
645 doc
.lpszDatatype
= NULL
;
648 doc
.lpszDocName
= name
;
649 ret
= StartDocA( hdc
, &doc
);
650 HeapFree( GetProcessHeap(), 0, name
);
651 if (ret
> 0) ret
= StartPage( hdc
);
655 case QUERYESCSUPPORT
:
657 const INT
*ptr
= (const INT
*)in_data
;
658 if (in_count
< sizeof(INT
)) return 0;
663 case GETPHYSPAGESIZE
:
664 case GETPRINTINGOFFSET
:
665 case GETSCALINGFACTOR
:
667 case QUERYESCSUPPORT
:
676 /* if not handled internally, pass it to the driver */
677 return ExtEscape( hdc
, escape
, in_count
, in_data
, 0, out_data
);
681 /******************************************************************************
682 * ExtEscape [GDI32.@]
684 * Access capabilities of a particular device that are not available through GDI.
687 * hdc [I] Handle to device context
688 * nEscape [I] Escape function
689 * cbInput [I] Number of bytes in input structure
690 * lpszInData [I] Pointer to input structure
691 * cbOutput [I] Number of bytes in output structure
692 * lpszOutData [O] Pointer to output structure
699 INT WINAPI
ExtEscape( HDC hdc
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
,
700 INT cbOutput
, LPSTR lpszOutData
)
703 DC
* dc
= get_dc_ptr( hdc
);
706 if (dc
->funcs
->pExtEscape
)
707 ret
= dc
->funcs
->pExtEscape( dc
->physDev
, nEscape
, cbInput
, lpszInData
, cbOutput
, lpszOutData
);
708 release_dc_ptr( dc
);
714 /*******************************************************************
715 * DrawEscape [GDI32.@]
719 INT WINAPI
DrawEscape(HDC hdc
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
)
721 FIXME("DrawEscape, stub\n");
725 /*******************************************************************
726 * NamedEscape [GDI32.@]
728 INT WINAPI
NamedEscape( HDC hdc
, LPCWSTR pDriver
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
,
729 INT cbOutput
, LPSTR lpszOutData
)
731 FIXME("(%p, %s, %d, %d, %p, %d, %p)\n",
732 hdc
, wine_dbgstr_w(pDriver
), nEscape
, cbInput
, lpszInData
, cbOutput
,
737 /*******************************************************************
738 * DdQueryDisplaySettingsUniqueness [GDI32.@]
739 * GdiEntry13 [GDI32.@]
741 ULONG WINAPI
DdQueryDisplaySettingsUniqueness(VOID
)
743 static int warn_once
;