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"
33 #include "gdi_private.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(driver);
39 struct graphics_driver
41 struct graphics_driver *next;
42 struct graphics_driver *prev;
43 HMODULE module; /* module handle */
44 unsigned int count; /* reference count */
48 static struct graphics_driver *first_driver;
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;
72 driver->module = module;
75 /* fill the function table */
78 #define GET_FUNC(name) driver->funcs.p##name = (void*)GetProcAddress( module, #name )
87 GET_FUNC(ChoosePixelFormat);
89 GET_FUNC(CloseFigure);
90 GET_FUNC(CreateBitmap);
92 GET_FUNC(CreateDIBSection);
93 GET_FUNC(DeleteBitmap);
95 GET_FUNC(DescribePixelFormat);
96 GET_FUNC(DeviceCapabilities);
101 GET_FUNC(EnumDeviceFonts);
102 GET_FUNC(ExcludeClipRect);
103 GET_FUNC(ExtDeviceMode);
105 GET_FUNC(ExtFloodFill);
106 GET_FUNC(ExtSelectClipRgn);
107 GET_FUNC(ExtTextOut);
110 GET_FUNC(FlattenPath);
112 GET_FUNC(GdiComment);
113 GET_FUNC(GetBitmapBits);
114 GET_FUNC(GetCharWidth);
115 GET_FUNC(GetDCOrgEx);
116 GET_FUNC(GetDIBColorTable);
118 GET_FUNC(GetDeviceCaps);
119 GET_FUNC(GetDeviceGammaRamp);
120 GET_FUNC(GetICMProfile);
121 GET_FUNC(GetNearestColor);
123 GET_FUNC(GetPixelFormat);
124 GET_FUNC(GetSystemPaletteEntries);
125 GET_FUNC(GetTextExtentExPoint);
126 GET_FUNC(GetTextMetrics);
127 GET_FUNC(IntersectClipRect);
131 GET_FUNC(ModifyWorldTransform);
132 GET_FUNC(OffsetClipRgn);
133 GET_FUNC(OffsetViewportOrg);
134 GET_FUNC(OffsetWindowOrg);
138 GET_FUNC(PolyBezier);
139 GET_FUNC(PolyBezierTo);
141 GET_FUNC(PolyPolygon);
142 GET_FUNC(PolyPolyline);
145 GET_FUNC(PolylineTo);
146 GET_FUNC(RealizeDefaultPalette);
147 GET_FUNC(RealizePalette);
153 GET_FUNC(ScaleViewportExt);
154 GET_FUNC(ScaleWindowExt);
155 GET_FUNC(SelectBitmap);
156 GET_FUNC(SelectBrush);
157 GET_FUNC(SelectClipPath);
158 GET_FUNC(SelectFont);
159 GET_FUNC(SelectPalette);
161 GET_FUNC(SetArcDirection);
162 GET_FUNC(SetBitmapBits);
163 GET_FUNC(SetBkColor);
165 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(wglDeleteContext);
203 GET_FUNC(wglGetProcAddress);
204 GET_FUNC(wglGetPbufferDCARB);
205 GET_FUNC(wglMakeContextCurrentARB);
206 GET_FUNC(wglMakeCurrent);
207 GET_FUNC(wglSetPixelFormatWINE);
208 GET_FUNC(wglShareLists);
209 GET_FUNC(wglUseFontBitmapsA);
210 GET_FUNC(wglUseFontBitmapsW);
213 else memset( &driver->funcs, 0, sizeof(driver->funcs) );
215 /* add it to the list */
217 if ((driver->next = first_driver)) driver->next->prev = driver;
218 first_driver = driver;
223 /**********************************************************************
224 * load_display_driver
226 * Special case for loading the display driver: get the name from the config file
228 static struct graphics_driver *load_display_driver(void)
230 char buffer[MAX_PATH], libname[32], *name, *next;
234 if (display_driver) /* already loaded */
236 display_driver->count++;
237 return display_driver;
240 strcpy( buffer, "x11" ); /* default value */
241 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
242 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Drivers", &hkey ))
244 DWORD type, count = sizeof(buffer);
245 RegQueryValueExA( hkey, "Graphics", 0, &type, (LPBYTE) buffer, &count );
252 next = strchr( name, ',' );
253 if (next) *next++ = 0;
255 snprintf( libname, sizeof(libname), "wine%s.drv", name );
256 if ((module = LoadLibraryA( libname )) != 0) break;
260 if (!(display_driver = create_driver( module )))
262 MESSAGE( "Could not create graphics driver '%s'\n", buffer );
263 FreeLibrary( module );
267 display_driver->count++; /* we don't want to free it */
268 return display_driver;
272 /**********************************************************************
275 const DC_FUNCTIONS *DRIVER_load_driver( LPCWSTR name )
278 struct graphics_driver *driver;
279 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
280 static const WCHAR display1W[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
282 EnterCriticalSection( &driver_section );
284 /* display driver is a special case */
285 if (!strcmpiW( name, displayW ) ||
286 !strcmpiW( name, display1W ))
288 driver = load_display_driver();
289 LeaveCriticalSection( &driver_section );
290 return &driver->funcs;
293 if ((module = GetModuleHandleW( name )))
295 for (driver = first_driver; driver; driver = driver->next)
297 if (driver->module == module)
300 LeaveCriticalSection( &driver_section );
301 return &driver->funcs;
306 if (!(module = LoadLibraryW( name )))
308 LeaveCriticalSection( &driver_section );
312 if (!(driver = create_driver( module )))
314 FreeLibrary( module );
315 LeaveCriticalSection( &driver_section );
319 TRACE( "loaded driver %p for %s\n", driver, debugstr_w(name) );
320 LeaveCriticalSection( &driver_section );
321 return &driver->funcs;
325 /**********************************************************************
328 * Get a new copy of an existing driver.
330 const DC_FUNCTIONS *DRIVER_get_driver( const DC_FUNCTIONS *funcs )
332 struct graphics_driver *driver;
334 EnterCriticalSection( &driver_section );
335 for (driver = first_driver; driver; driver = driver->next)
336 if (&driver->funcs == funcs) break;
337 if (!driver) ERR( "driver not found, trouble ahead\n" );
339 LeaveCriticalSection( &driver_section );
344 /**********************************************************************
345 * DRIVER_release_driver
347 * Release a driver by decrementing ref count and freeing it if needed.
349 void DRIVER_release_driver( const DC_FUNCTIONS *funcs )
351 struct graphics_driver *driver;
353 EnterCriticalSection( &driver_section );
355 for (driver = first_driver; driver; driver = driver->next)
356 if (&driver->funcs == funcs) break;
358 if (!driver) goto done;
359 if (--driver->count) goto done;
361 /* removed last reference, free it */
362 if (driver->next) driver->next->prev = driver->prev;
363 if (driver->prev) driver->prev->next = driver->next;
364 else first_driver = driver->next;
365 if (driver == display_driver) display_driver = NULL;
367 FreeLibrary( driver->module );
368 HeapFree( GetProcessHeap(), 0, driver );
370 LeaveCriticalSection( &driver_section );
374 /*****************************************************************************
375 * DRIVER_GetDriverName
378 BOOL DRIVER_GetDriverName( LPCWSTR device, LPWSTR driver, DWORD size )
380 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
381 static const WCHAR devicesW[] = { 'd','e','v','i','c','e','s',0 };
382 static const WCHAR display1W[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
383 static const WCHAR empty_strW[] = { 0 };
386 /* display is a special case */
387 if (!strcmpiW( device, displayW ) ||
388 !strcmpiW( device, display1W ))
390 lstrcpynW( driver, displayW, size );
394 size = GetProfileStringW(devicesW, device, empty_strW, driver, size);
396 WARN("Unable to find %s in [devices] section of win.ini\n", debugstr_w(device));
399 p = strchrW(driver, ',');
402 WARN("%s entry in [devices] section of win.ini is malformed.\n", debugstr_w(device));
406 TRACE("Found %s for %s\n", debugstr_w(driver), debugstr_w(device));
411 /***********************************************************************
412 * GdiConvertToDevmodeW (GDI32.@)
414 DEVMODEW * WINAPI GdiConvertToDevmodeW(const DEVMODEA *dmA)
417 WORD dmW_size, dmA_size;
419 dmA_size = dmA->dmSize;
421 /* this is the minimal dmSize that XP accepts */
422 if (dmA_size < FIELD_OFFSET(DEVMODEA, dmFields))
425 if (dmA_size > sizeof(DEVMODEA))
426 dmA_size = sizeof(DEVMODEA);
428 dmW_size = dmA_size + CCHDEVICENAME;
429 if (dmA_size >= FIELD_OFFSET(DEVMODEA, dmFormName) + CCHFORMNAME)
430 dmW_size += CCHFORMNAME;
432 dmW = HeapAlloc(GetProcessHeap(), 0, dmW_size + dmA->dmDriverExtra);
433 if (!dmW) return NULL;
435 MultiByteToWideChar(CP_ACP, 0, (const char*) dmA->dmDeviceName, -1,
436 dmW->dmDeviceName, CCHDEVICENAME);
437 /* copy slightly more, to avoid long computations */
438 memcpy(&dmW->dmSpecVersion, &dmA->dmSpecVersion, dmA_size - CCHDEVICENAME);
440 if (dmA_size >= FIELD_OFFSET(DEVMODEA, dmFormName) + CCHFORMNAME)
442 if (dmA->dmFields & DM_FORMNAME)
443 MultiByteToWideChar(CP_ACP, 0, (const char*) dmA->dmFormName, -1,
444 dmW->dmFormName, CCHFORMNAME);
446 dmW->dmFormName[0] = 0;
448 if (dmA_size > FIELD_OFFSET(DEVMODEA, dmLogPixels))
449 memcpy(&dmW->dmLogPixels, &dmA->dmLogPixels, dmA_size - FIELD_OFFSET(DEVMODEA, dmLogPixels));
452 if (dmA->dmDriverExtra)
453 memcpy((char *)dmW + dmW_size, (const char *)dmA + dmA_size, dmA->dmDriverExtra);
455 dmW->dmSize = dmW_size;
461 /*****************************************************************************
464 * This should thunk to 16-bit and simply call the proc with the given args.
466 INT WINAPI GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc, HWND hWnd,
467 LPSTR lpModelName, LPSTR OldPort, LPSTR NewPort )
469 FIXME("(%p, %p, %s, %s, %s)\n", lpfnDevInstallProc, hWnd, lpModelName, OldPort, NewPort );
473 /*****************************************************************************
476 * This should load the correct driver for lpszDevice and calls this driver's
477 * ExtDeviceModePropSheet proc.
479 * Note: The driver calls a callback routine for each property sheet page; these
480 * pages are supposed to be filled into the structure pointed to by lpPropSheet.
481 * The layout of this structure is:
487 * HPROPSHEETPAGE pages[10];
490 INT WINAPI GDI_CallExtDeviceModePropSheet16( HWND hWnd, LPCSTR lpszDevice,
491 LPCSTR lpszPort, LPVOID lpPropSheet )
493 FIXME("(%p, %s, %s, %p)\n", hWnd, lpszDevice, lpszPort, lpPropSheet );
497 /*****************************************************************************
500 * This should load the correct driver for lpszDevice and call this driver's
501 * ExtDeviceMode proc.
503 * FIXME: convert ExtDeviceMode to unicode in the driver interface
505 INT WINAPI GDI_CallExtDeviceMode16( HWND hwnd,
506 LPDEVMODEA lpdmOutput, LPSTR lpszDevice,
507 LPSTR lpszPort, LPDEVMODEA lpdmInput,
508 LPSTR lpszProfile, DWORD fwMode )
517 TRACE("(%p, %p, %s, %s, %p, %s, %d)\n",
518 hwnd, lpdmOutput, lpszDevice, lpszPort, lpdmInput, lpszProfile, fwMode );
520 if (!lpszDevice) return -1;
521 if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
523 if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
525 if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
527 if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
529 if ((dc = get_dc_ptr( hdc )))
531 if (dc->funcs->pExtDeviceMode)
532 ret = dc->funcs->pExtDeviceMode( buf, hwnd, lpdmOutput, lpszDevice, lpszPort,
533 lpdmInput, lpszProfile, fwMode );
534 release_dc_ptr( dc );
540 /****************************************************************************
543 * This should load the correct driver for lpszDevice and calls this driver's
544 * AdvancedSetupDialog proc.
546 INT WINAPI GDI_CallAdvancedSetupDialog16( HWND hwnd, LPSTR lpszDevice,
547 LPDEVMODEA devin, LPDEVMODEA devout )
549 TRACE("(%p, %s, %p, %p)\n", hwnd, lpszDevice, devin, devout );
553 /*****************************************************************************
556 * This should load the correct driver for lpszDevice and calls this driver's
557 * DeviceCapabilities proc.
559 * FIXME: convert DeviceCapabilities to unicode in the driver interface
561 DWORD WINAPI GDI_CallDeviceCapabilities16( LPCSTR lpszDevice, LPCSTR lpszPort,
562 WORD fwCapability, LPSTR lpszOutput,
572 TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice, lpszPort, fwCapability, lpszOutput, lpdm );
574 if (!lpszDevice) return -1;
575 if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
577 if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
579 if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
581 if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
583 if ((dc = get_dc_ptr( hdc )))
585 if (dc->funcs->pDeviceCapabilities)
586 ret = dc->funcs->pDeviceCapabilities( buf, lpszDevice, lpszPort,
587 fwCapability, lpszOutput, lpdm );
588 release_dc_ptr( dc );
595 /************************************************************************
598 INT WINAPI Escape( HDC hdc, INT escape, INT in_count, LPCSTR in_data, LPVOID out_data )
606 return AbortDoc( hdc );
609 return EndDoc( hdc );
611 case GETPHYSPAGESIZE:
613 pt->x = GetDeviceCaps( hdc, PHYSICALWIDTH );
614 pt->y = GetDeviceCaps( hdc, PHYSICALHEIGHT );
617 case GETPRINTINGOFFSET:
619 pt->x = GetDeviceCaps( hdc, PHYSICALOFFSETX );
620 pt->y = GetDeviceCaps( hdc, PHYSICALOFFSETY );
623 case GETSCALINGFACTOR:
625 pt->x = GetDeviceCaps( hdc, SCALINGFACTORX );
626 pt->y = GetDeviceCaps( hdc, SCALINGFACTORY );
630 return EndPage( hdc );
633 return SetAbortProc( hdc, (ABORTPROC)in_data );
640 /* in_data may not be 0 terminated so we must copy it */
643 name = HeapAlloc( GetProcessHeap(), 0, in_count+1 );
644 memcpy( name, in_data, in_count );
647 /* out_data is actually a pointer to the DocInfo structure and used as
648 * a second input parameter */
649 if (out_data) doc = *(DOCINFOA *)out_data;
652 doc.cbSize = sizeof(doc);
653 doc.lpszOutput = NULL;
654 doc.lpszDatatype = NULL;
657 doc.lpszDocName = name;
658 ret = StartDocA( hdc, &doc );
659 HeapFree( GetProcessHeap(), 0, name );
660 if (ret > 0) ret = StartPage( hdc );
664 case QUERYESCSUPPORT:
666 const INT *ptr = (const INT *)in_data;
667 if (in_count < sizeof(INT)) return 0;
672 case GETPHYSPAGESIZE:
673 case GETPRINTINGOFFSET:
674 case GETSCALINGFACTOR:
676 case QUERYESCSUPPORT:
685 /* if not handled internally, pass it to the driver */
686 return ExtEscape( hdc, escape, in_count, in_data, 0, out_data );
690 /******************************************************************************
691 * ExtEscape [GDI32.@]
693 * Access capabilities of a particular device that are not available through GDI.
696 * hdc [I] Handle to device context
697 * nEscape [I] Escape function
698 * cbInput [I] Number of bytes in input structure
699 * lpszInData [I] Pointer to input structure
700 * cbOutput [I] Number of bytes in output structure
701 * lpszOutData [O] Pointer to output structure
708 INT WINAPI ExtEscape( HDC hdc, INT nEscape, INT cbInput, LPCSTR lpszInData,
709 INT cbOutput, LPSTR lpszOutData )
712 DC * dc = get_dc_ptr( hdc );
715 if (dc->funcs->pExtEscape)
716 ret = dc->funcs->pExtEscape( dc->physDev, nEscape, cbInput, lpszInData, cbOutput, lpszOutData );
717 release_dc_ptr( dc );
723 /*******************************************************************
724 * DrawEscape [GDI32.@]
728 INT WINAPI DrawEscape(HDC hdc, INT nEscape, INT cbInput, LPCSTR lpszInData)
730 FIXME("DrawEscape, stub\n");
734 /*******************************************************************
735 * NamedEscape [GDI32.@]
737 INT WINAPI NamedEscape( HDC hdc, LPCWSTR pDriver, INT nEscape, INT cbInput, LPCSTR lpszInData,
738 INT cbOutput, LPSTR lpszOutData )
740 FIXME("(%p, %s, %d, %d, %p, %d, %p)\n",
741 hdc, wine_dbgstr_w(pDriver), nEscape, cbInput, lpszInData, cbOutput,
746 /*******************************************************************
747 * DdQueryDisplaySettingsUniqueness [GDI32.@]
748 * GdiEntry13 [GDI32.@]
750 ULONG WINAPI DdQueryDisplaySettingsUniqueness(VOID)