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 */
48 static struct list drivers
= LIST_INIT( drivers
);
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
;
70 driver
->module
= module
;
72 /* fill the function table */
75 #define GET_FUNC(name) driver->funcs.p##name = (void*)GetProcAddress( module, #name )
84 GET_FUNC(ChoosePixelFormat
);
86 GET_FUNC(CloseFigure
);
87 GET_FUNC(CreateBitmap
);
89 GET_FUNC(CreateDIBSection
);
90 GET_FUNC(DeleteBitmap
);
92 GET_FUNC(DescribePixelFormat
);
93 GET_FUNC(DeviceCapabilities
);
98 GET_FUNC(EnumDeviceFonts
);
99 GET_FUNC(EnumICMProfiles
);
100 GET_FUNC(ExcludeClipRect
);
101 GET_FUNC(ExtDeviceMode
);
103 GET_FUNC(ExtFloodFill
);
104 GET_FUNC(ExtSelectClipRgn
);
105 GET_FUNC(ExtTextOut
);
108 GET_FUNC(FlattenPath
);
110 GET_FUNC(GdiComment
);
111 GET_FUNC(GetBitmapBits
);
112 GET_FUNC(GetCharWidth
);
113 GET_FUNC(GetDIBColorTable
);
115 GET_FUNC(GetDeviceCaps
);
116 GET_FUNC(GetDeviceGammaRamp
);
117 GET_FUNC(GetICMProfile
);
118 GET_FUNC(GetNearestColor
);
120 GET_FUNC(GetPixelFormat
);
121 GET_FUNC(GetSystemPaletteEntries
);
122 GET_FUNC(GetTextExtentExPoint
);
123 GET_FUNC(GetTextMetrics
);
124 GET_FUNC(IntersectClipRect
);
128 GET_FUNC(ModifyWorldTransform
);
129 GET_FUNC(OffsetClipRgn
);
130 GET_FUNC(OffsetViewportOrg
);
131 GET_FUNC(OffsetWindowOrg
);
135 GET_FUNC(PolyBezier
);
136 GET_FUNC(PolyBezierTo
);
138 GET_FUNC(PolyPolygon
);
139 GET_FUNC(PolyPolyline
);
142 GET_FUNC(PolylineTo
);
143 GET_FUNC(RealizeDefaultPalette
);
144 GET_FUNC(RealizePalette
);
150 GET_FUNC(ScaleViewportExt
);
151 GET_FUNC(ScaleWindowExt
);
152 GET_FUNC(SelectBitmap
);
153 GET_FUNC(SelectBrush
);
154 GET_FUNC(SelectClipPath
);
155 GET_FUNC(SelectFont
);
156 GET_FUNC(SelectPalette
);
158 GET_FUNC(SetArcDirection
);
159 GET_FUNC(SetBitmapBits
);
160 GET_FUNC(SetBkColor
);
162 GET_FUNC(SetDCBrushColor
);
163 GET_FUNC(SetDCPenColor
);
164 GET_FUNC(SetDIBColorTable
);
166 GET_FUNC(SetDIBitsToDevice
);
167 GET_FUNC(SetDeviceClipping
);
168 GET_FUNC(SetDeviceGammaRamp
);
169 GET_FUNC(SetMapMode
);
170 GET_FUNC(SetMapperFlags
);
172 GET_FUNC(SetPixelFormat
);
173 GET_FUNC(SetPolyFillMode
);
176 GET_FUNC(SetStretchBltMode
);
177 GET_FUNC(SetTextAlign
);
178 GET_FUNC(SetTextCharacterExtra
);
179 GET_FUNC(SetTextColor
);
180 GET_FUNC(SetTextJustification
);
181 GET_FUNC(SetViewportExt
);
182 GET_FUNC(SetViewportOrg
);
183 GET_FUNC(SetWindowExt
);
184 GET_FUNC(SetWindowOrg
);
185 GET_FUNC(SetWorldTransform
);
188 GET_FUNC(StretchBlt
);
189 GET_FUNC(StretchDIBits
);
190 GET_FUNC(StrokeAndFillPath
);
191 GET_FUNC(StrokePath
);
192 GET_FUNC(SwapBuffers
);
193 GET_FUNC(UnrealizePalette
);
197 GET_FUNC(wglCreateContext
);
198 GET_FUNC(wglCreateContextAttribsARB
);
199 GET_FUNC(wglDeleteContext
);
200 GET_FUNC(wglGetProcAddress
);
201 GET_FUNC(wglGetPbufferDCARB
);
202 GET_FUNC(wglMakeContextCurrentARB
);
203 GET_FUNC(wglMakeCurrent
);
204 GET_FUNC(wglSetPixelFormatWINE
);
205 GET_FUNC(wglShareLists
);
206 GET_FUNC(wglUseFontBitmapsA
);
207 GET_FUNC(wglUseFontBitmapsW
);
210 else memset( &driver
->funcs
, 0, sizeof(driver
->funcs
) );
216 /**********************************************************************
217 * DRIVER_get_display_driver
219 * Special case for loading the display driver: get the name from the config file
221 const DC_FUNCTIONS
*DRIVER_get_display_driver(void)
223 struct graphics_driver
*driver
;
224 char buffer
[MAX_PATH
], libname
[32], *name
, *next
;
228 if (display_driver
) return &display_driver
->funcs
; /* already loaded */
230 strcpy( buffer
, "x11" ); /* default value */
231 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
232 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Drivers", &hkey
))
234 DWORD type
, count
= sizeof(buffer
);
235 RegQueryValueExA( hkey
, "Graphics", 0, &type
, (LPBYTE
) buffer
, &count
);
242 next
= strchr( name
, ',' );
243 if (next
) *next
++ = 0;
245 snprintf( libname
, sizeof(libname
), "wine%s.drv", name
);
246 if ((module
= LoadLibraryA( libname
)) != 0) break;
250 if (!(driver
= create_driver( module
)))
252 MESSAGE( "Could not create graphics driver '%s'\n", buffer
);
253 FreeLibrary( module
);
256 if (InterlockedCompareExchangePointer( (void **)&display_driver
, driver
, NULL
))
258 /* somebody beat us to it */
259 FreeLibrary( driver
->module
);
260 HeapFree( GetProcessHeap(), 0, driver
);
262 return &display_driver
->funcs
;
266 /**********************************************************************
269 const DC_FUNCTIONS
*DRIVER_load_driver( LPCWSTR name
)
272 struct graphics_driver
*driver
, *new_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 /* display driver is a special case */
277 if (!strcmpiW( name
, displayW
) || !strcmpiW( name
, display1W
)) return DRIVER_get_display_driver();
279 if ((module
= GetModuleHandleW( name
)))
281 if (display_driver
&& display_driver
->module
== module
) return &display_driver
->funcs
;
282 EnterCriticalSection( &driver_section
);
283 LIST_FOR_EACH_ENTRY( driver
, &drivers
, struct graphics_driver
, entry
)
285 if (driver
->module
== module
) goto done
;
287 LeaveCriticalSection( &driver_section
);
290 if (!(module
= LoadLibraryW( name
))) return NULL
;
292 if (!(new_driver
= create_driver( module
)))
294 FreeLibrary( module
);
298 /* check if someone else added it in the meantime */
299 EnterCriticalSection( &driver_section
);
300 LIST_FOR_EACH_ENTRY( driver
, &drivers
, struct graphics_driver
, entry
)
302 if (driver
->module
!= module
) continue;
303 FreeLibrary( module
);
304 HeapFree( GetProcessHeap(), 0, new_driver
);
308 list_add_head( &drivers
, &driver
->entry
);
309 TRACE( "loaded driver %p for %s\n", driver
, debugstr_w(name
) );
311 LeaveCriticalSection( &driver_section
);
312 return &driver
->funcs
;
316 /*****************************************************************************
317 * DRIVER_GetDriverName
320 BOOL
DRIVER_GetDriverName( LPCWSTR device
, LPWSTR driver
, DWORD size
)
322 static const WCHAR displayW
[] = { 'd','i','s','p','l','a','y',0 };
323 static const WCHAR devicesW
[] = { 'd','e','v','i','c','e','s',0 };
324 static const WCHAR display1W
[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
325 static const WCHAR empty_strW
[] = { 0 };
328 /* display is a special case */
329 if (!strcmpiW( device
, displayW
) ||
330 !strcmpiW( device
, display1W
))
332 lstrcpynW( driver
, displayW
, size
);
336 size
= GetProfileStringW(devicesW
, device
, empty_strW
, driver
, size
);
338 WARN("Unable to find %s in [devices] section of win.ini\n", debugstr_w(device
));
341 p
= strchrW(driver
, ',');
344 WARN("%s entry in [devices] section of win.ini is malformed.\n", debugstr_w(device
));
348 TRACE("Found %s for %s\n", debugstr_w(driver
), debugstr_w(device
));
353 /***********************************************************************
354 * GdiConvertToDevmodeW (GDI32.@)
356 DEVMODEW
* WINAPI
GdiConvertToDevmodeW(const DEVMODEA
*dmA
)
359 WORD dmW_size
, dmA_size
;
361 dmA_size
= dmA
->dmSize
;
363 /* this is the minimal dmSize that XP accepts */
364 if (dmA_size
< FIELD_OFFSET(DEVMODEA
, dmFields
))
367 if (dmA_size
> sizeof(DEVMODEA
))
368 dmA_size
= sizeof(DEVMODEA
);
370 dmW_size
= dmA_size
+ CCHDEVICENAME
;
371 if (dmA_size
>= FIELD_OFFSET(DEVMODEA
, dmFormName
) + CCHFORMNAME
)
372 dmW_size
+= CCHFORMNAME
;
374 dmW
= HeapAlloc(GetProcessHeap(), 0, dmW_size
+ dmA
->dmDriverExtra
);
375 if (!dmW
) return NULL
;
377 MultiByteToWideChar(CP_ACP
, 0, (const char*) dmA
->dmDeviceName
, -1,
378 dmW
->dmDeviceName
, CCHDEVICENAME
);
379 /* copy slightly more, to avoid long computations */
380 memcpy(&dmW
->dmSpecVersion
, &dmA
->dmSpecVersion
, dmA_size
- CCHDEVICENAME
);
382 if (dmA_size
>= FIELD_OFFSET(DEVMODEA
, dmFormName
) + CCHFORMNAME
)
384 if (dmA
->dmFields
& DM_FORMNAME
)
385 MultiByteToWideChar(CP_ACP
, 0, (const char*) dmA
->dmFormName
, -1,
386 dmW
->dmFormName
, CCHFORMNAME
);
388 dmW
->dmFormName
[0] = 0;
390 if (dmA_size
> FIELD_OFFSET(DEVMODEA
, dmLogPixels
))
391 memcpy(&dmW
->dmLogPixels
, &dmA
->dmLogPixels
, dmA_size
- FIELD_OFFSET(DEVMODEA
, dmLogPixels
));
394 if (dmA
->dmDriverExtra
)
395 memcpy((char *)dmW
+ dmW_size
, (const char *)dmA
+ dmA_size
, dmA
->dmDriverExtra
);
397 dmW
->dmSize
= dmW_size
;
403 /*****************************************************************************
406 * This should thunk to 16-bit and simply call the proc with the given args.
408 INT WINAPI
GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc
, HWND hWnd
,
409 LPSTR lpModelName
, LPSTR OldPort
, LPSTR NewPort
)
411 FIXME("(%p, %p, %s, %s, %s)\n", lpfnDevInstallProc
, hWnd
, lpModelName
, OldPort
, NewPort
);
415 /*****************************************************************************
418 * This should load the correct driver for lpszDevice and calls this driver's
419 * ExtDeviceModePropSheet proc.
421 * Note: The driver calls a callback routine for each property sheet page; these
422 * pages are supposed to be filled into the structure pointed to by lpPropSheet.
423 * The layout of this structure is:
429 * HPROPSHEETPAGE pages[10];
432 INT WINAPI
GDI_CallExtDeviceModePropSheet16( HWND hWnd
, LPCSTR lpszDevice
,
433 LPCSTR lpszPort
, LPVOID lpPropSheet
)
435 FIXME("(%p, %s, %s, %p)\n", hWnd
, lpszDevice
, lpszPort
, lpPropSheet
);
439 /*****************************************************************************
442 * This should load the correct driver for lpszDevice and call this driver's
443 * ExtDeviceMode proc.
445 * FIXME: convert ExtDeviceMode to unicode in the driver interface
447 INT WINAPI
GDI_CallExtDeviceMode16( HWND hwnd
,
448 LPDEVMODEA lpdmOutput
, LPSTR lpszDevice
,
449 LPSTR lpszPort
, LPDEVMODEA lpdmInput
,
450 LPSTR lpszProfile
, DWORD fwMode
)
459 TRACE("(%p, %p, %s, %s, %p, %s, %d)\n",
460 hwnd
, lpdmOutput
, lpszDevice
, lpszPort
, lpdmInput
, lpszProfile
, fwMode
);
462 if (!lpszDevice
) return -1;
463 if (!MultiByteToWideChar(CP_ACP
, 0, lpszDevice
, -1, deviceW
, 300)) return -1;
465 if(!DRIVER_GetDriverName( deviceW
, bufW
, 300 )) return -1;
467 if (!WideCharToMultiByte(CP_ACP
, 0, bufW
, -1, buf
, 300, NULL
, NULL
)) return -1;
469 if (!(hdc
= CreateICA( buf
, lpszDevice
, lpszPort
, NULL
))) return -1;
471 if ((dc
= get_dc_ptr( hdc
)))
473 if (dc
->funcs
->pExtDeviceMode
)
474 ret
= dc
->funcs
->pExtDeviceMode( buf
, hwnd
, lpdmOutput
, lpszDevice
, lpszPort
,
475 lpdmInput
, lpszProfile
, fwMode
);
476 release_dc_ptr( dc
);
482 /****************************************************************************
485 * This should load the correct driver for lpszDevice and calls this driver's
486 * AdvancedSetupDialog proc.
488 INT WINAPI
GDI_CallAdvancedSetupDialog16( HWND hwnd
, LPSTR lpszDevice
,
489 LPDEVMODEA devin
, LPDEVMODEA devout
)
491 TRACE("(%p, %s, %p, %p)\n", hwnd
, lpszDevice
, devin
, devout
);
495 /*****************************************************************************
498 * This should load the correct driver for lpszDevice and calls this driver's
499 * DeviceCapabilities proc.
501 * FIXME: convert DeviceCapabilities to unicode in the driver interface
503 DWORD WINAPI
GDI_CallDeviceCapabilities16( LPCSTR lpszDevice
, LPCSTR lpszPort
,
504 WORD fwCapability
, LPSTR lpszOutput
,
514 TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice
, lpszPort
, fwCapability
, lpszOutput
, lpdm
);
516 if (!lpszDevice
) return -1;
517 if (!MultiByteToWideChar(CP_ACP
, 0, lpszDevice
, -1, deviceW
, 300)) return -1;
519 if(!DRIVER_GetDriverName( deviceW
, bufW
, 300 )) return -1;
521 if (!WideCharToMultiByte(CP_ACP
, 0, bufW
, -1, buf
, 300, NULL
, NULL
)) return -1;
523 if (!(hdc
= CreateICA( buf
, lpszDevice
, lpszPort
, NULL
))) return -1;
525 if ((dc
= get_dc_ptr( hdc
)))
527 if (dc
->funcs
->pDeviceCapabilities
)
528 ret
= dc
->funcs
->pDeviceCapabilities( buf
, lpszDevice
, lpszPort
,
529 fwCapability
, lpszOutput
, lpdm
);
530 release_dc_ptr( dc
);
537 /************************************************************************
540 INT WINAPI
Escape( HDC hdc
, INT escape
, INT in_count
, LPCSTR in_data
, LPVOID out_data
)
548 return AbortDoc( hdc
);
551 return EndDoc( hdc
);
553 case GETPHYSPAGESIZE
:
555 pt
->x
= GetDeviceCaps( hdc
, PHYSICALWIDTH
);
556 pt
->y
= GetDeviceCaps( hdc
, PHYSICALHEIGHT
);
559 case GETPRINTINGOFFSET
:
561 pt
->x
= GetDeviceCaps( hdc
, PHYSICALOFFSETX
);
562 pt
->y
= GetDeviceCaps( hdc
, PHYSICALOFFSETY
);
565 case GETSCALINGFACTOR
:
567 pt
->x
= GetDeviceCaps( hdc
, SCALINGFACTORX
);
568 pt
->y
= GetDeviceCaps( hdc
, SCALINGFACTORY
);
572 return EndPage( hdc
);
575 return SetAbortProc( hdc
, (ABORTPROC
)in_data
);
582 /* in_data may not be 0 terminated so we must copy it */
585 name
= HeapAlloc( GetProcessHeap(), 0, in_count
+1 );
586 memcpy( name
, in_data
, in_count
);
589 /* out_data is actually a pointer to the DocInfo structure and used as
590 * a second input parameter */
591 if (out_data
) doc
= *(DOCINFOA
*)out_data
;
594 doc
.cbSize
= sizeof(doc
);
595 doc
.lpszOutput
= NULL
;
596 doc
.lpszDatatype
= NULL
;
599 doc
.lpszDocName
= name
;
600 ret
= StartDocA( hdc
, &doc
);
601 HeapFree( GetProcessHeap(), 0, name
);
602 if (ret
> 0) ret
= StartPage( hdc
);
606 case QUERYESCSUPPORT
:
608 const INT
*ptr
= (const INT
*)in_data
;
609 if (in_count
< sizeof(INT
)) return 0;
614 case GETPHYSPAGESIZE
:
615 case GETPRINTINGOFFSET
:
616 case GETSCALINGFACTOR
:
618 case QUERYESCSUPPORT
:
627 /* if not handled internally, pass it to the driver */
628 return ExtEscape( hdc
, escape
, in_count
, in_data
, 0, out_data
);
632 /******************************************************************************
633 * ExtEscape [GDI32.@]
635 * Access capabilities of a particular device that are not available through GDI.
638 * hdc [I] Handle to device context
639 * nEscape [I] Escape function
640 * cbInput [I] Number of bytes in input structure
641 * lpszInData [I] Pointer to input structure
642 * cbOutput [I] Number of bytes in output structure
643 * lpszOutData [O] Pointer to output structure
650 INT WINAPI
ExtEscape( HDC hdc
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
,
651 INT cbOutput
, LPSTR lpszOutData
)
654 DC
* dc
= get_dc_ptr( hdc
);
657 if (dc
->funcs
->pExtEscape
)
658 ret
= dc
->funcs
->pExtEscape( dc
->physDev
, nEscape
, cbInput
, lpszInData
, cbOutput
, lpszOutData
);
659 release_dc_ptr( dc
);
665 /*******************************************************************
666 * DrawEscape [GDI32.@]
670 INT WINAPI
DrawEscape(HDC hdc
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
)
672 FIXME("DrawEscape, stub\n");
676 /*******************************************************************
677 * NamedEscape [GDI32.@]
679 INT WINAPI
NamedEscape( HDC hdc
, LPCWSTR pDriver
, INT nEscape
, INT cbInput
, LPCSTR lpszInData
,
680 INT cbOutput
, LPSTR lpszOutData
)
682 FIXME("(%p, %s, %d, %d, %p, %d, %p)\n",
683 hdc
, wine_dbgstr_w(pDriver
), nEscape
, cbInput
, lpszInData
, cbOutput
,
688 /*******************************************************************
689 * DdQueryDisplaySettingsUniqueness [GDI32.@]
690 * GdiEntry13 [GDI32.@]
692 ULONG WINAPI
DdQueryDisplaySettingsUniqueness(VOID
)
694 static int warn_once
;