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(ExcludeClipRect);
100 GET_FUNC(ExtDeviceMode);
102 GET_FUNC(ExtFloodFill);
103 GET_FUNC(ExtSelectClipRgn);
104 GET_FUNC(ExtTextOut);
107 GET_FUNC(FlattenPath);
109 GET_FUNC(GdiComment);
110 GET_FUNC(GetBitmapBits);
111 GET_FUNC(GetCharWidth);
112 GET_FUNC(GetDIBColorTable);
114 GET_FUNC(GetDeviceCaps);
115 GET_FUNC(GetDeviceGammaRamp);
116 GET_FUNC(GetICMProfile);
117 GET_FUNC(GetNearestColor);
119 GET_FUNC(GetPixelFormat);
120 GET_FUNC(GetSystemPaletteEntries);
121 GET_FUNC(GetTextExtentExPoint);
122 GET_FUNC(GetTextMetrics);
123 GET_FUNC(IntersectClipRect);
127 GET_FUNC(ModifyWorldTransform);
128 GET_FUNC(OffsetClipRgn);
129 GET_FUNC(OffsetViewportOrg);
130 GET_FUNC(OffsetWindowOrg);
134 GET_FUNC(PolyBezier);
135 GET_FUNC(PolyBezierTo);
137 GET_FUNC(PolyPolygon);
138 GET_FUNC(PolyPolyline);
141 GET_FUNC(PolylineTo);
142 GET_FUNC(RealizeDefaultPalette);
143 GET_FUNC(RealizePalette);
149 GET_FUNC(ScaleViewportExt);
150 GET_FUNC(ScaleWindowExt);
151 GET_FUNC(SelectBitmap);
152 GET_FUNC(SelectBrush);
153 GET_FUNC(SelectClipPath);
154 GET_FUNC(SelectFont);
155 GET_FUNC(SelectPalette);
157 GET_FUNC(SetArcDirection);
158 GET_FUNC(SetBitmapBits);
159 GET_FUNC(SetBkColor);
161 GET_FUNC(SetDCBrushColor);
162 GET_FUNC(SetDCPenColor);
163 GET_FUNC(SetDIBColorTable);
165 GET_FUNC(SetDIBitsToDevice);
166 GET_FUNC(SetDeviceClipping);
167 GET_FUNC(SetDeviceGammaRamp);
168 GET_FUNC(SetMapMode);
169 GET_FUNC(SetMapperFlags);
171 GET_FUNC(SetPixelFormat);
172 GET_FUNC(SetPolyFillMode);
175 GET_FUNC(SetStretchBltMode);
176 GET_FUNC(SetTextAlign);
177 GET_FUNC(SetTextCharacterExtra);
178 GET_FUNC(SetTextColor);
179 GET_FUNC(SetTextJustification);
180 GET_FUNC(SetViewportExt);
181 GET_FUNC(SetViewportOrg);
182 GET_FUNC(SetWindowExt);
183 GET_FUNC(SetWindowOrg);
184 GET_FUNC(SetWorldTransform);
187 GET_FUNC(StretchBlt);
188 GET_FUNC(StretchDIBits);
189 GET_FUNC(StrokeAndFillPath);
190 GET_FUNC(StrokePath);
191 GET_FUNC(SwapBuffers);
192 GET_FUNC(UnrealizePalette);
196 GET_FUNC(wglCreateContext);
197 GET_FUNC(wglCreateContextAttribsARB);
198 GET_FUNC(wglDeleteContext);
199 GET_FUNC(wglGetProcAddress);
200 GET_FUNC(wglGetPbufferDCARB);
201 GET_FUNC(wglMakeContextCurrentARB);
202 GET_FUNC(wglMakeCurrent);
203 GET_FUNC(wglSetPixelFormatWINE);
204 GET_FUNC(wglShareLists);
205 GET_FUNC(wglUseFontBitmapsA);
206 GET_FUNC(wglUseFontBitmapsW);
209 else memset( &driver->funcs, 0, sizeof(driver->funcs) );
215 /**********************************************************************
216 * DRIVER_get_display_driver
218 * Special case for loading the display driver: get the name from the config file
220 const DC_FUNCTIONS *DRIVER_get_display_driver(void)
222 struct graphics_driver *driver;
223 char buffer[MAX_PATH], libname[32], *name, *next;
227 if (display_driver) return &display_driver->funcs; /* already loaded */
229 strcpy( buffer, "x11" ); /* default value */
230 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
231 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Drivers", &hkey ))
233 DWORD type, count = sizeof(buffer);
234 RegQueryValueExA( hkey, "Graphics", 0, &type, (LPBYTE) buffer, &count );
241 next = strchr( name, ',' );
242 if (next) *next++ = 0;
244 snprintf( libname, sizeof(libname), "wine%s.drv", name );
245 if ((module = LoadLibraryA( libname )) != 0) break;
249 if (!(driver = create_driver( module )))
251 MESSAGE( "Could not create graphics driver '%s'\n", buffer );
252 FreeLibrary( module );
255 if (InterlockedCompareExchangePointer( (void **)&display_driver, driver, NULL ))
257 /* somebody beat us to it */
258 FreeLibrary( driver->module );
259 HeapFree( GetProcessHeap(), 0, driver );
261 return &display_driver->funcs;
265 /**********************************************************************
268 const DC_FUNCTIONS *DRIVER_load_driver( LPCWSTR name )
271 struct graphics_driver *driver, *new_driver;
272 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
273 static const WCHAR display1W[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
275 /* display driver is a special case */
276 if (!strcmpiW( name, displayW ) || !strcmpiW( name, display1W )) return DRIVER_get_display_driver();
278 if ((module = GetModuleHandleW( name )))
280 if (display_driver && display_driver->module == module) return &display_driver->funcs;
281 EnterCriticalSection( &driver_section );
282 LIST_FOR_EACH_ENTRY( driver, &drivers, struct graphics_driver, entry )
284 if (driver->module == module) goto done;
286 LeaveCriticalSection( &driver_section );
289 if (!(module = LoadLibraryW( name ))) return NULL;
291 if (!(new_driver = create_driver( module )))
293 FreeLibrary( module );
297 /* check if someone else added it in the meantime */
298 EnterCriticalSection( &driver_section );
299 LIST_FOR_EACH_ENTRY( driver, &drivers, struct graphics_driver, entry )
301 if (driver->module != module) continue;
302 FreeLibrary( module );
303 HeapFree( GetProcessHeap(), 0, new_driver );
307 list_add_head( &drivers, &driver->entry );
308 TRACE( "loaded driver %p for %s\n", driver, debugstr_w(name) );
310 LeaveCriticalSection( &driver_section );
311 return &driver->funcs;
315 /*****************************************************************************
316 * DRIVER_GetDriverName
319 BOOL DRIVER_GetDriverName( LPCWSTR device, LPWSTR driver, DWORD size )
321 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
322 static const WCHAR devicesW[] = { 'd','e','v','i','c','e','s',0 };
323 static const WCHAR display1W[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
324 static const WCHAR empty_strW[] = { 0 };
327 /* display is a special case */
328 if (!strcmpiW( device, displayW ) ||
329 !strcmpiW( device, display1W ))
331 lstrcpynW( driver, displayW, size );
335 size = GetProfileStringW(devicesW, device, empty_strW, driver, size);
337 WARN("Unable to find %s in [devices] section of win.ini\n", debugstr_w(device));
340 p = strchrW(driver, ',');
343 WARN("%s entry in [devices] section of win.ini is malformed.\n", debugstr_w(device));
347 TRACE("Found %s for %s\n", debugstr_w(driver), debugstr_w(device));
352 /***********************************************************************
353 * GdiConvertToDevmodeW (GDI32.@)
355 DEVMODEW * WINAPI GdiConvertToDevmodeW(const DEVMODEA *dmA)
358 WORD dmW_size, dmA_size;
360 dmA_size = dmA->dmSize;
362 /* this is the minimal dmSize that XP accepts */
363 if (dmA_size < FIELD_OFFSET(DEVMODEA, dmFields))
366 if (dmA_size > sizeof(DEVMODEA))
367 dmA_size = sizeof(DEVMODEA);
369 dmW_size = dmA_size + CCHDEVICENAME;
370 if (dmA_size >= FIELD_OFFSET(DEVMODEA, dmFormName) + CCHFORMNAME)
371 dmW_size += CCHFORMNAME;
373 dmW = HeapAlloc(GetProcessHeap(), 0, dmW_size + dmA->dmDriverExtra);
374 if (!dmW) return NULL;
376 MultiByteToWideChar(CP_ACP, 0, (const char*) dmA->dmDeviceName, -1,
377 dmW->dmDeviceName, CCHDEVICENAME);
378 /* copy slightly more, to avoid long computations */
379 memcpy(&dmW->dmSpecVersion, &dmA->dmSpecVersion, dmA_size - CCHDEVICENAME);
381 if (dmA_size >= FIELD_OFFSET(DEVMODEA, dmFormName) + CCHFORMNAME)
383 if (dmA->dmFields & DM_FORMNAME)
384 MultiByteToWideChar(CP_ACP, 0, (const char*) dmA->dmFormName, -1,
385 dmW->dmFormName, CCHFORMNAME);
387 dmW->dmFormName[0] = 0;
389 if (dmA_size > FIELD_OFFSET(DEVMODEA, dmLogPixels))
390 memcpy(&dmW->dmLogPixels, &dmA->dmLogPixels, dmA_size - FIELD_OFFSET(DEVMODEA, dmLogPixels));
393 if (dmA->dmDriverExtra)
394 memcpy((char *)dmW + dmW_size, (const char *)dmA + dmA_size, dmA->dmDriverExtra);
396 dmW->dmSize = dmW_size;
402 /*****************************************************************************
405 * This should thunk to 16-bit and simply call the proc with the given args.
407 INT WINAPI GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc, HWND hWnd,
408 LPSTR lpModelName, LPSTR OldPort, LPSTR NewPort )
410 FIXME("(%p, %p, %s, %s, %s)\n", lpfnDevInstallProc, hWnd, lpModelName, OldPort, NewPort );
414 /*****************************************************************************
417 * This should load the correct driver for lpszDevice and calls this driver's
418 * ExtDeviceModePropSheet proc.
420 * Note: The driver calls a callback routine for each property sheet page; these
421 * pages are supposed to be filled into the structure pointed to by lpPropSheet.
422 * The layout of this structure is:
428 * HPROPSHEETPAGE pages[10];
431 INT WINAPI GDI_CallExtDeviceModePropSheet16( HWND hWnd, LPCSTR lpszDevice,
432 LPCSTR lpszPort, LPVOID lpPropSheet )
434 FIXME("(%p, %s, %s, %p)\n", hWnd, lpszDevice, lpszPort, lpPropSheet );
438 /*****************************************************************************
441 * This should load the correct driver for lpszDevice and call this driver's
442 * ExtDeviceMode proc.
444 * FIXME: convert ExtDeviceMode to unicode in the driver interface
446 INT WINAPI GDI_CallExtDeviceMode16( HWND hwnd,
447 LPDEVMODEA lpdmOutput, LPSTR lpszDevice,
448 LPSTR lpszPort, LPDEVMODEA lpdmInput,
449 LPSTR lpszProfile, DWORD fwMode )
458 TRACE("(%p, %p, %s, %s, %p, %s, %d)\n",
459 hwnd, lpdmOutput, lpszDevice, lpszPort, lpdmInput, lpszProfile, fwMode );
461 if (!lpszDevice) return -1;
462 if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
464 if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
466 if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
468 if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
470 if ((dc = get_dc_ptr( hdc )))
472 if (dc->funcs->pExtDeviceMode)
473 ret = dc->funcs->pExtDeviceMode( buf, hwnd, lpdmOutput, lpszDevice, lpszPort,
474 lpdmInput, lpszProfile, fwMode );
475 release_dc_ptr( dc );
481 /****************************************************************************
484 * This should load the correct driver for lpszDevice and calls this driver's
485 * AdvancedSetupDialog proc.
487 INT WINAPI GDI_CallAdvancedSetupDialog16( HWND hwnd, LPSTR lpszDevice,
488 LPDEVMODEA devin, LPDEVMODEA devout )
490 TRACE("(%p, %s, %p, %p)\n", hwnd, lpszDevice, devin, devout );
494 /*****************************************************************************
497 * This should load the correct driver for lpszDevice and calls this driver's
498 * DeviceCapabilities proc.
500 * FIXME: convert DeviceCapabilities to unicode in the driver interface
502 DWORD WINAPI GDI_CallDeviceCapabilities16( LPCSTR lpszDevice, LPCSTR lpszPort,
503 WORD fwCapability, LPSTR lpszOutput,
513 TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice, lpszPort, fwCapability, lpszOutput, lpdm );
515 if (!lpszDevice) return -1;
516 if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
518 if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
520 if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
522 if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
524 if ((dc = get_dc_ptr( hdc )))
526 if (dc->funcs->pDeviceCapabilities)
527 ret = dc->funcs->pDeviceCapabilities( buf, lpszDevice, lpszPort,
528 fwCapability, lpszOutput, lpdm );
529 release_dc_ptr( dc );
536 /************************************************************************
539 INT WINAPI Escape( HDC hdc, INT escape, INT in_count, LPCSTR in_data, LPVOID out_data )
547 return AbortDoc( hdc );
550 return EndDoc( hdc );
552 case GETPHYSPAGESIZE:
554 pt->x = GetDeviceCaps( hdc, PHYSICALWIDTH );
555 pt->y = GetDeviceCaps( hdc, PHYSICALHEIGHT );
558 case GETPRINTINGOFFSET:
560 pt->x = GetDeviceCaps( hdc, PHYSICALOFFSETX );
561 pt->y = GetDeviceCaps( hdc, PHYSICALOFFSETY );
564 case GETSCALINGFACTOR:
566 pt->x = GetDeviceCaps( hdc, SCALINGFACTORX );
567 pt->y = GetDeviceCaps( hdc, SCALINGFACTORY );
571 return EndPage( hdc );
574 return SetAbortProc( hdc, (ABORTPROC)in_data );
581 /* in_data may not be 0 terminated so we must copy it */
584 name = HeapAlloc( GetProcessHeap(), 0, in_count+1 );
585 memcpy( name, in_data, in_count );
588 /* out_data is actually a pointer to the DocInfo structure and used as
589 * a second input parameter */
590 if (out_data) doc = *(DOCINFOA *)out_data;
593 doc.cbSize = sizeof(doc);
594 doc.lpszOutput = NULL;
595 doc.lpszDatatype = NULL;
598 doc.lpszDocName = name;
599 ret = StartDocA( hdc, &doc );
600 HeapFree( GetProcessHeap(), 0, name );
601 if (ret > 0) ret = StartPage( hdc );
605 case QUERYESCSUPPORT:
607 const INT *ptr = (const INT *)in_data;
608 if (in_count < sizeof(INT)) return 0;
613 case GETPHYSPAGESIZE:
614 case GETPRINTINGOFFSET:
615 case GETSCALINGFACTOR:
617 case QUERYESCSUPPORT:
626 /* if not handled internally, pass it to the driver */
627 return ExtEscape( hdc, escape, in_count, in_data, 0, out_data );
631 /******************************************************************************
632 * ExtEscape [GDI32.@]
634 * Access capabilities of a particular device that are not available through GDI.
637 * hdc [I] Handle to device context
638 * nEscape [I] Escape function
639 * cbInput [I] Number of bytes in input structure
640 * lpszInData [I] Pointer to input structure
641 * cbOutput [I] Number of bytes in output structure
642 * lpszOutData [O] Pointer to output structure
649 INT WINAPI ExtEscape( HDC hdc, INT nEscape, INT cbInput, LPCSTR lpszInData,
650 INT cbOutput, LPSTR lpszOutData )
653 DC * dc = get_dc_ptr( hdc );
656 if (dc->funcs->pExtEscape)
657 ret = dc->funcs->pExtEscape( dc->physDev, nEscape, cbInput, lpszInData, cbOutput, lpszOutData );
658 release_dc_ptr( dc );
664 /*******************************************************************
665 * DrawEscape [GDI32.@]
669 INT WINAPI DrawEscape(HDC hdc, INT nEscape, INT cbInput, LPCSTR lpszInData)
671 FIXME("DrawEscape, stub\n");
675 /*******************************************************************
676 * NamedEscape [GDI32.@]
678 INT WINAPI NamedEscape( HDC hdc, LPCWSTR pDriver, INT nEscape, INT cbInput, LPCSTR lpszInData,
679 INT cbOutput, LPSTR lpszOutData )
681 FIXME("(%p, %s, %d, %d, %p, %d, %p)\n",
682 hdc, wine_dbgstr_w(pDriver), nEscape, cbInput, lpszInData, cbOutput,
687 /*******************************************************************
688 * DdQueryDisplaySettingsUniqueness [GDI32.@]
689 * GdiEntry13 [GDI32.@]
691 ULONG WINAPI DdQueryDisplaySettingsUniqueness(VOID)
693 static int warn_once;