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 "gdi_private.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(driver
);
38 struct graphics_driver
40 struct graphics_driver
*next
;
41 struct graphics_driver
*prev
;
42 HMODULE module
; /* module handle */
43 unsigned int count
; /* reference count */
47 static struct graphics_driver
*first_driver
;
48 static struct graphics_driver
*display_driver
;
50 static CRITICAL_SECTION driver_section
;
51 static CRITICAL_SECTION_DEBUG critsect_debug
=
53 0, 0, &driver_section
,
54 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
55 0, 0, { (DWORD_PTR
)(__FILE__
": driver_section") }
57 static CRITICAL_SECTION driver_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
59 /**********************************************************************
62 * Allocate and fill the driver structure for a given module.
64 static struct graphics_driver
*create_driver( HMODULE module
)
66 struct graphics_driver
*driver
;
68 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(GetNearestColor
);
121 GET_FUNC(GetPixelFormat
);
122 GET_FUNC(GetSystemPaletteEntries
);
123 GET_FUNC(GetTextExtentExPoint
);
124 GET_FUNC(GetTextMetrics
);
125 GET_FUNC(IntersectClipRect
);
129 GET_FUNC(ModifyWorldTransform
);
130 GET_FUNC(OffsetClipRgn
);
131 GET_FUNC(OffsetViewportOrg
);
132 GET_FUNC(OffsetWindowOrg
);
136 GET_FUNC(PolyBezier
);
137 GET_FUNC(PolyBezierTo
);
139 GET_FUNC(PolyPolygon
);
140 GET_FUNC(PolyPolyline
);
143 GET_FUNC(PolylineTo
);
144 GET_FUNC(RealizeDefaultPalette
);
145 GET_FUNC(RealizePalette
);
151 GET_FUNC(ScaleViewportExt
);
152 GET_FUNC(ScaleWindowExt
);
153 GET_FUNC(SelectBitmap
);
154 GET_FUNC(SelectBrush
);
155 GET_FUNC(SelectClipPath
);
156 GET_FUNC(SelectFont
);
157 GET_FUNC(SelectPalette
);
159 GET_FUNC(SetArcDirection
);
160 GET_FUNC(SetBitmapBits
);
161 GET_FUNC(SetBkColor
);
163 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(wglDeleteContext
);
201 GET_FUNC(wglGetProcAddress
);
202 GET_FUNC(wglGetPbufferDCARB
);
203 GET_FUNC(wglMakeContextCurrentARB
);
204 GET_FUNC(wglMakeCurrent
);
205 GET_FUNC(wglShareLists
);
206 GET_FUNC(wglUseFontBitmapsA
);
207 GET_FUNC(wglUseFontBitmapsW
);
210 else memset( &driver
->funcs
, 0, sizeof(driver
->funcs
) );
212 /* add it to the list */
214 if ((driver
->next
= first_driver
)) driver
->next
->prev
= driver
;
215 first_driver
= driver
;
220 /**********************************************************************
221 * load_display_driver
223 * Special case for loading the display driver: get the name from the config file
225 static struct graphics_driver
*load_display_driver(void)
227 char buffer
[MAX_PATH
], libname
[32], *name
, *next
;
231 if (display_driver
) /* already loaded */
233 display_driver
->count
++;
234 return display_driver
;
237 strcpy( buffer
, "x11" ); /* default value */
238 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
239 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Drivers", &hkey
))
241 DWORD type
, count
= sizeof(buffer
);
242 RegQueryValueExA( hkey
, "Graphics", 0, &type
, (LPBYTE
) buffer
, &count
);
249 next
= strchr( name
, ',' );
250 if (next
) *next
++ = 0;
252 snprintf( libname
, sizeof(libname
), "wine%s.drv", name
);
253 if ((module
= LoadLibraryA( libname
)) != 0) break;
257 if (!(display_driver
= create_driver( module
)))
259 MESSAGE( "Could not create graphics driver '%s'\n", buffer
);
260 FreeLibrary( module
);
264 display_driver
->count
++; /* we don't want to free it */
265 return display_driver
;
269 /**********************************************************************
272 const DC_FUNCTIONS
*DRIVER_load_driver( LPCWSTR name
)
275 struct graphics_driver
*driver
;
276 static const WCHAR displayW
[] = { 'd','i','s','p','l','a','y',0 };
277 static const WCHAR display1W
[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
279 EnterCriticalSection( &driver_section
);
281 /* display driver is a special case */
282 if (!strcmpiW( name
, displayW
) ||
283 !strcmpiW( name
, display1W
))
285 driver
= load_display_driver();
286 LeaveCriticalSection( &driver_section
);
287 return &driver
->funcs
;
290 if ((module
= GetModuleHandleW( name
)))
292 for (driver
= first_driver
; driver
; driver
= driver
->next
)
294 if (driver
->module
== module
)
297 LeaveCriticalSection( &driver_section
);
298 return &driver
->funcs
;
303 if (!(module
= LoadLibraryW( name
)))
305 LeaveCriticalSection( &driver_section
);
309 if (!(driver
= create_driver( module
)))
311 FreeLibrary( module
);
312 LeaveCriticalSection( &driver_section
);
316 TRACE( "loaded driver %p for %s\n", driver
, debugstr_w(name
) );
317 LeaveCriticalSection( &driver_section
);
318 return &driver
->funcs
;
322 /**********************************************************************
325 * Get a new copy of an existing driver.
327 const DC_FUNCTIONS
*DRIVER_get_driver( const DC_FUNCTIONS
*funcs
)
329 struct graphics_driver
*driver
;
331 EnterCriticalSection( &driver_section
);
332 for (driver
= first_driver
; driver
; driver
= driver
->next
)
333 if (&driver
->funcs
== funcs
) break;
334 if (!driver
) ERR( "driver not found, trouble ahead\n" );
336 LeaveCriticalSection( &driver_section
);
341 /**********************************************************************
342 * DRIVER_release_driver
344 * Release a driver by decrementing ref count and freeing it if needed.
346 void DRIVER_release_driver( const DC_FUNCTIONS
*funcs
)
348 struct graphics_driver
*driver
;
350 EnterCriticalSection( &driver_section
);
352 for (driver
= first_driver
; driver
; driver
= driver
->next
)
353 if (&driver
->funcs
== funcs
) break;
355 if (!driver
) goto done
;
356 if (--driver
->count
) goto done
;
358 /* removed last reference, free it */
359 if (driver
->next
) driver
->next
->prev
= driver
->prev
;
360 if (driver
->prev
) driver
->prev
->next
= driver
->next
;
361 else first_driver
= driver
->next
;
362 if (driver
== display_driver
) display_driver
= NULL
;
364 FreeLibrary( driver
->module
);
365 HeapFree( GetProcessHeap(), 0, driver
);
367 LeaveCriticalSection( &driver_section
);
371 /*****************************************************************************
372 * DRIVER_GetDriverName
375 BOOL
DRIVER_GetDriverName( LPCWSTR device
, LPWSTR driver
, DWORD size
)
377 static const WCHAR displayW
[] = { 'd','i','s','p','l','a','y',0 };
378 static const WCHAR devicesW
[] = { 'd','e','v','i','c','e','s',0 };
379 static const WCHAR display1W
[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
380 static const WCHAR empty_strW
[] = { 0 };
383 /* display is a special case */
384 if (!strcmpiW( device
, displayW
) ||
385 !strcmpiW( device
, display1W
))
387 lstrcpynW( driver
, displayW
, size
);
391 size
= GetProfileStringW(devicesW
, device
, empty_strW
, driver
, size
);
393 WARN("Unable to find %s in [devices] section of win.ini\n", debugstr_w(device
));
396 p
= strchrW(driver
, ',');
399 WARN("%s entry in [devices] section of win.ini is malformed.\n", debugstr_w(device
));
403 TRACE("Found %s for %s\n", debugstr_w(driver
), debugstr_w(device
));
408 /***********************************************************************
409 * GdiConvertToDevmodeW (GDI32.@)
411 DEVMODEW
* WINAPI
GdiConvertToDevmodeW(const DEVMODEA
*dmA
)
416 dmW_size
= dmA
->dmSize
+ CCHDEVICENAME
;
417 if (dmA
->dmSize
>= (const char *)dmA
->dmFormName
- (const char *)dmA
+ CCHFORMNAME
)
418 dmW_size
+= CCHFORMNAME
;
420 dmW
= HeapAlloc(GetProcessHeap(), 0, dmW_size
+ dmA
->dmDriverExtra
);
421 if (!dmW
) return NULL
;
423 MultiByteToWideChar(CP_ACP
, 0, (const char*) dmA
->dmDeviceName
, CCHDEVICENAME
,
424 dmW
->dmDeviceName
, CCHDEVICENAME
);
425 /* copy slightly more, to avoid long computations */
426 memcpy(&dmW
->dmSpecVersion
, &dmA
->dmSpecVersion
, dmA
->dmSize
- CCHDEVICENAME
);
428 if (dmA
->dmSize
>= (const char *)dmA
->dmFormName
- (const char *)dmA
+ CCHFORMNAME
)
430 MultiByteToWideChar(CP_ACP
, 0, (const char*) dmA
->dmFormName
, CCHFORMNAME
,
431 dmW
->dmFormName
, CCHFORMNAME
);
432 if (dmA
->dmSize
> (const char *)&dmA
->dmLogPixels
- (const char *)dmA
)
433 memcpy(&dmW
->dmLogPixels
, &dmA
->dmLogPixels
, dmA
->dmSize
- ((const char *)&dmA
->dmLogPixels
- (const char *)dmA
));
436 if (dmA
->dmDriverExtra
)
437 memcpy((char *)dmW
+ dmW_size
, (const char *)dmA
+ dmA
->dmSize
, dmA
->dmDriverExtra
);
439 dmW
->dmSize
= dmW_size
;
445 /*****************************************************************************
448 * This should thunk to 16-bit and simply call the proc with the given args.
450 INT WINAPI
GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc
, HWND hWnd
,
451 LPSTR lpModelName
, LPSTR OldPort
, LPSTR NewPort
)
453 FIXME("(%p, %p, %s, %s, %s)\n", lpfnDevInstallProc
, hWnd
, lpModelName
, OldPort
, NewPort
);
457 /*****************************************************************************
460 * This should load the correct driver for lpszDevice and calls this driver's
461 * ExtDeviceModePropSheet proc.
463 * Note: The driver calls a callback routine for each property sheet page; these
464 * pages are supposed to be filled into the structure pointed to by lpPropSheet.
465 * The layout of this structure is:
471 * HPROPSHEETPAGE pages[10];
474 INT WINAPI
GDI_CallExtDeviceModePropSheet16( HWND hWnd
, LPCSTR lpszDevice
,
475 LPCSTR lpszPort
, LPVOID lpPropSheet
)
477 FIXME("(%p, %s, %s, %p)\n", hWnd
, lpszDevice
, lpszPort
, lpPropSheet
);
481 /*****************************************************************************
484 * This should load the correct driver for lpszDevice and calls this driver's
485 * ExtDeviceMode proc.
487 * FIXME: convert ExtDeviceMode to unicode in the driver interface
489 INT WINAPI
GDI_CallExtDeviceMode16( HWND hwnd
,
490 LPDEVMODEA lpdmOutput
, LPSTR lpszDevice
,
491 LPSTR lpszPort
, LPDEVMODEA lpdmInput
,
492 LPSTR lpszProfile
, DWORD fwMode
)
500 INT (*pExtDeviceMode
)(LPSTR
,HWND
,LPDEVMODEA
,LPSTR
,LPSTR
,LPDEVMODEA
,LPSTR
,DWORD
);
502 TRACE("(%p, %p, %s, %s, %p, %s, %d)\n",
503 hwnd
, lpdmOutput
, lpszDevice
, lpszPort
, lpdmInput
, lpszProfile
, fwMode
);
505 if (!lpszDevice
) return -1;
506 if (!MultiByteToWideChar(CP_ACP
, 0, lpszDevice
, -1, deviceW
, 300)) return -1;
508 if(!DRIVER_GetDriverName( deviceW
, bufW
, 300 )) return -1;
510 if (!WideCharToMultiByte(CP_ACP
, 0, bufW
, -1, buf
, 300, NULL
, NULL
)) return -1;
512 if (!(hdc
= CreateICA( buf
, lpszDevice
, lpszPort
, NULL
))) return -1;
514 if ((dc
= DC_GetDCPtr( hdc
)))
516 pExtDeviceMode
= dc
->funcs
->pExtDeviceMode
;
517 GDI_ReleaseObj( hdc
);
519 ret
= pExtDeviceMode(buf
, hwnd
, lpdmOutput
, lpszDevice
, lpszPort
,
520 lpdmInput
, lpszProfile
, fwMode
);
526 /****************************************************************************
529 * This should load the correct driver for lpszDevice and calls this driver's
530 * AdvancedSetupDialog proc.
532 INT WINAPI
GDI_CallAdvancedSetupDialog16( HWND hwnd
, LPSTR lpszDevice
,
533 LPDEVMODEA devin
, LPDEVMODEA devout
)
535 TRACE("(%p, %s, %p, %p)\n", hwnd
, lpszDevice
, devin
, devout
);
539 /*****************************************************************************
542 * This should load the correct driver for lpszDevice and calls this driver's
543 * DeviceCapabilities proc.
545 * FIXME: convert DeviceCapabilities to unicode in the driver interface
547 DWORD WINAPI
GDI_CallDeviceCapabilities16( LPCSTR lpszDevice
, LPCSTR lpszPort
,
548 WORD fwCapability
, LPSTR lpszOutput
,
558 TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice
, lpszPort
, fwCapability
, lpszOutput
, lpdm
);
560 if (!lpszDevice
) return -1;
561 if (!MultiByteToWideChar(CP_ACP
, 0, lpszDevice
, -1, deviceW
, 300)) return -1;
563 if(!DRIVER_GetDriverName( deviceW
, bufW
, 300 )) return -1;
565 if (!WideCharToMultiByte(CP_ACP
, 0, bufW
, -1, buf
, 300, NULL
, NULL
)) return -1;
567 if (!(hdc
= CreateICA( buf
, lpszDevice
, lpszPort
, NULL
))) return -1;
569 if ((dc
= DC_GetDCPtr( hdc
)))
571 if (dc
->funcs
->pDeviceCapabilities
)
572 ret
= dc
->funcs
->pDeviceCapabilities( buf
, lpszDevice
, lpszPort
,
573 fwCapability
, lpszOutput
, lpdm
);
574 GDI_ReleaseObj( hdc
);
581 /************************************************************************
584 INT WINAPI
Escape( HDC hdc
, INT escape
, INT in_count
, LPCSTR in_data
, LPVOID out_data
)
592 return AbortDoc( hdc
);
595 return EndDoc( hdc
);
597 case GETPHYSPAGESIZE
:
599 pt
->x
= GetDeviceCaps( hdc
, PHYSICALWIDTH
);
600 pt
->y
= GetDeviceCaps( hdc
, PHYSICALHEIGHT
);
603 case GETPRINTINGOFFSET
:
605 pt
->x
= GetDeviceCaps( hdc
, PHYSICALOFFSETX
);
606 pt
->y
= GetDeviceCaps( hdc
, PHYSICALOFFSETY
);
609 case GETSCALINGFACTOR
:
611 pt
->x
= GetDeviceCaps( hdc
, SCALINGFACTORX
);
612 pt
->y
= GetDeviceCaps( hdc
, SCALINGFACTORY
);
616 return EndPage( hdc
);
619 return SetAbortProc( hdc
, (ABORTPROC
)in_data
);
626 /* in_data may not be 0 terminated so we must copy it */
629 name
= HeapAlloc( GetProcessHeap(), 0, in_count
+1 );
630 memcpy( name
, in_data
, in_count
);
633 /* out_data is actually a pointer to the DocInfo structure and used as
634 * a second input parameter */
635 if (out_data
) doc
= *(DOCINFOA
*)out_data
;
638 doc
.cbSize
= sizeof(doc
);
639 doc
.lpszOutput
= NULL
;
640 doc
.lpszDatatype
= NULL
;
643 doc
.lpszDocName
= name
;
644 ret
= StartDocA( hdc
, &doc
);
645 HeapFree( GetProcessHeap(), 0, name
);
646 if (ret
> 0) ret
= StartPage( hdc
);
650 case QUERYESCSUPPORT
:
652 const INT
*ptr
= (const INT
*)in_data
;
653 if (in_count
< sizeof(INT
)) return 0;
658 case GETPHYSPAGESIZE
:
659 case GETPRINTINGOFFSET
:
660 case GETSCALINGFACTOR
:
662 case QUERYESCSUPPORT
:
671 /* if not handled internally, pass it to the driver */
672 return ExtEscape( hdc
, escape
, in_count
, in_data
, 0, out_data
);
676 /******************************************************************************
677 * ExtEscape [GDI32.@]
679 * Access capabilities of a particular device that are not available through GDI.
682 * hdc [I] Handle to device context
683 * nEscape [I] Escape function
684 * cbInput [I] Number of bytes in input structure
685 * lpszInData [I] Pointer to input structure
686 * cbOutput [I] Number of bytes in output structure
687 * lpszOutData [O] Pointer to output structure
694 INT WINAPI
ExtEscape( HDC hdc
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
,
695 INT cbOutput
, LPSTR lpszOutData
)
698 DC
* dc
= DC_GetDCPtr( hdc
);
701 if (dc
->funcs
->pExtEscape
)
702 ret
= dc
->funcs
->pExtEscape( dc
->physDev
, nEscape
, cbInput
, lpszInData
, cbOutput
, lpszOutData
);
703 GDI_ReleaseObj( hdc
);
709 /*******************************************************************
710 * DrawEscape [GDI32.@]
714 INT WINAPI
DrawEscape(HDC hdc
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
)
716 FIXME("DrawEscape, stub\n");