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(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);
166 GET_FUNC(SetDCPenColor);
167 GET_FUNC(SetDIBColorTable);
169 GET_FUNC(SetDIBitsToDevice);
170 GET_FUNC(SetDeviceClipping);
171 GET_FUNC(SetDeviceGammaRamp);
172 GET_FUNC(SetMapMode);
173 GET_FUNC(SetMapperFlags);
175 GET_FUNC(SetPixelFormat);
176 GET_FUNC(SetPolyFillMode);
179 GET_FUNC(SetStretchBltMode);
180 GET_FUNC(SetTextAlign);
181 GET_FUNC(SetTextCharacterExtra);
182 GET_FUNC(SetTextColor);
183 GET_FUNC(SetTextJustification);
184 GET_FUNC(SetViewportExt);
185 GET_FUNC(SetViewportOrg);
186 GET_FUNC(SetWindowExt);
187 GET_FUNC(SetWindowOrg);
188 GET_FUNC(SetWorldTransform);
191 GET_FUNC(StretchBlt);
192 GET_FUNC(StretchDIBits);
193 GET_FUNC(StrokeAndFillPath);
194 GET_FUNC(StrokePath);
195 GET_FUNC(SwapBuffers);
196 GET_FUNC(UnrealizePalette);
200 GET_FUNC(wglCreateContext);
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 /* add it to the list */
216 if ((driver->next = first_driver)) driver->next->prev = driver;
217 first_driver = driver;
222 /**********************************************************************
223 * load_display_driver
225 * Special case for loading the display driver: get the name from the config file
227 static struct graphics_driver *load_display_driver(void)
229 char buffer[MAX_PATH], libname[32], *name, *next;
233 if (display_driver) /* already loaded */
235 display_driver->count++;
236 return display_driver;
239 strcpy( buffer, "x11" ); /* default value */
240 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
241 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Drivers", &hkey ))
243 DWORD type, count = sizeof(buffer);
244 RegQueryValueExA( hkey, "Graphics", 0, &type, (LPBYTE) buffer, &count );
251 next = strchr( name, ',' );
252 if (next) *next++ = 0;
254 snprintf( libname, sizeof(libname), "wine%s.drv", name );
255 if ((module = LoadLibraryA( libname )) != 0) break;
259 if (!(display_driver = create_driver( module )))
261 MESSAGE( "Could not create graphics driver '%s'\n", buffer );
262 FreeLibrary( module );
266 display_driver->count++; /* we don't want to free it */
267 return display_driver;
271 /**********************************************************************
274 const DC_FUNCTIONS *DRIVER_load_driver( LPCWSTR name )
277 struct graphics_driver *driver;
278 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
279 static const WCHAR display1W[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
281 EnterCriticalSection( &driver_section );
283 /* display driver is a special case */
284 if (!strcmpiW( name, displayW ) ||
285 !strcmpiW( name, display1W ))
287 driver = load_display_driver();
288 LeaveCriticalSection( &driver_section );
289 return &driver->funcs;
292 if ((module = GetModuleHandleW( name )))
294 for (driver = first_driver; driver; driver = driver->next)
296 if (driver->module == module)
299 LeaveCriticalSection( &driver_section );
300 return &driver->funcs;
305 if (!(module = LoadLibraryW( name )))
307 LeaveCriticalSection( &driver_section );
311 if (!(driver = create_driver( module )))
313 FreeLibrary( module );
314 LeaveCriticalSection( &driver_section );
318 TRACE( "loaded driver %p for %s\n", driver, debugstr_w(name) );
319 LeaveCriticalSection( &driver_section );
320 return &driver->funcs;
324 /**********************************************************************
327 * Get a new copy of an existing driver.
329 const DC_FUNCTIONS *DRIVER_get_driver( const DC_FUNCTIONS *funcs )
331 struct graphics_driver *driver;
333 EnterCriticalSection( &driver_section );
334 for (driver = first_driver; driver; driver = driver->next)
335 if (&driver->funcs == funcs) break;
336 if (!driver) ERR( "driver not found, trouble ahead\n" );
338 LeaveCriticalSection( &driver_section );
343 /**********************************************************************
344 * DRIVER_release_driver
346 * Release a driver by decrementing ref count and freeing it if needed.
348 void DRIVER_release_driver( const DC_FUNCTIONS *funcs )
350 struct graphics_driver *driver;
352 EnterCriticalSection( &driver_section );
354 for (driver = first_driver; driver; driver = driver->next)
355 if (&driver->funcs == funcs) break;
357 if (!driver) goto done;
358 if (--driver->count) goto done;
360 /* removed last reference, free it */
361 if (driver->next) driver->next->prev = driver->prev;
362 if (driver->prev) driver->prev->next = driver->next;
363 else first_driver = driver->next;
364 if (driver == display_driver) display_driver = NULL;
366 FreeLibrary( driver->module );
367 HeapFree( GetProcessHeap(), 0, driver );
369 LeaveCriticalSection( &driver_section );
373 /*****************************************************************************
374 * DRIVER_GetDriverName
377 BOOL DRIVER_GetDriverName( LPCWSTR device, LPWSTR driver, DWORD size )
379 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
380 static const WCHAR devicesW[] = { 'd','e','v','i','c','e','s',0 };
381 static const WCHAR display1W[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
382 static const WCHAR empty_strW[] = { 0 };
385 /* display is a special case */
386 if (!strcmpiW( device, displayW ) ||
387 !strcmpiW( device, display1W ))
389 lstrcpynW( driver, displayW, size );
393 size = GetProfileStringW(devicesW, device, empty_strW, driver, size);
395 WARN("Unable to find %s in [devices] section of win.ini\n", debugstr_w(device));
398 p = strchrW(driver, ',');
401 WARN("%s entry in [devices] section of win.ini is malformed.\n", debugstr_w(device));
405 TRACE("Found %s for %s\n", debugstr_w(driver), debugstr_w(device));
410 /***********************************************************************
411 * GdiConvertToDevmodeW (GDI32.@)
413 DEVMODEW * WINAPI GdiConvertToDevmodeW(const DEVMODEA *dmA)
416 WORD dmW_size, dmA_size;
418 dmA_size = dmA->dmSize;
420 /* this is the minimal dmSize that XP accepts */
421 if (dmA_size < FIELD_OFFSET(DEVMODEA, dmFields))
424 if (dmA_size > sizeof(DEVMODEA))
425 dmA_size = sizeof(DEVMODEA);
427 dmW_size = dmA_size + CCHDEVICENAME;
428 if (dmA_size >= FIELD_OFFSET(DEVMODEA, dmFormName) + CCHFORMNAME)
429 dmW_size += CCHFORMNAME;
431 dmW = HeapAlloc(GetProcessHeap(), 0, dmW_size + dmA->dmDriverExtra);
432 if (!dmW) return NULL;
434 MultiByteToWideChar(CP_ACP, 0, (const char*) dmA->dmDeviceName, -1,
435 dmW->dmDeviceName, CCHDEVICENAME);
436 /* copy slightly more, to avoid long computations */
437 memcpy(&dmW->dmSpecVersion, &dmA->dmSpecVersion, dmA_size - CCHDEVICENAME);
439 if (dmA_size >= FIELD_OFFSET(DEVMODEA, dmFormName) + CCHFORMNAME)
441 if (dmA->dmFields & DM_FORMNAME)
442 MultiByteToWideChar(CP_ACP, 0, (const char*) dmA->dmFormName, -1,
443 dmW->dmFormName, CCHFORMNAME);
445 dmW->dmFormName[0] = 0;
447 if (dmA_size > FIELD_OFFSET(DEVMODEA, dmLogPixels))
448 memcpy(&dmW->dmLogPixels, &dmA->dmLogPixels, dmA_size - FIELD_OFFSET(DEVMODEA, dmLogPixels));
451 if (dmA->dmDriverExtra)
452 memcpy((char *)dmW + dmW_size, (const char *)dmA + dmA_size, dmA->dmDriverExtra);
454 dmW->dmSize = dmW_size;
460 /*****************************************************************************
463 * This should thunk to 16-bit and simply call the proc with the given args.
465 INT WINAPI GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc, HWND hWnd,
466 LPSTR lpModelName, LPSTR OldPort, LPSTR NewPort )
468 FIXME("(%p, %p, %s, %s, %s)\n", lpfnDevInstallProc, hWnd, lpModelName, OldPort, NewPort );
472 /*****************************************************************************
475 * This should load the correct driver for lpszDevice and calls this driver's
476 * ExtDeviceModePropSheet proc.
478 * Note: The driver calls a callback routine for each property sheet page; these
479 * pages are supposed to be filled into the structure pointed to by lpPropSheet.
480 * The layout of this structure is:
486 * HPROPSHEETPAGE pages[10];
489 INT WINAPI GDI_CallExtDeviceModePropSheet16( HWND hWnd, LPCSTR lpszDevice,
490 LPCSTR lpszPort, LPVOID lpPropSheet )
492 FIXME("(%p, %s, %s, %p)\n", hWnd, lpszDevice, lpszPort, lpPropSheet );
496 /*****************************************************************************
499 * This should load the correct driver for lpszDevice and calls this driver's
500 * ExtDeviceMode proc.
502 * FIXME: convert ExtDeviceMode to unicode in the driver interface
504 INT WINAPI GDI_CallExtDeviceMode16( HWND hwnd,
505 LPDEVMODEA lpdmOutput, LPSTR lpszDevice,
506 LPSTR lpszPort, LPDEVMODEA lpdmInput,
507 LPSTR lpszProfile, DWORD fwMode )
516 TRACE("(%p, %p, %s, %s, %p, %s, %d)\n",
517 hwnd, lpdmOutput, lpszDevice, lpszPort, lpdmInput, lpszProfile, fwMode );
519 if (!lpszDevice) return -1;
520 if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
522 if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
524 if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
526 if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
528 if ((dc = get_dc_ptr( hdc )))
530 if (dc->funcs->pExtDeviceMode)
531 ret = dc->funcs->pExtDeviceMode( buf, hwnd, lpdmOutput, lpszDevice, lpszPort,
532 lpdmInput, lpszProfile, fwMode );
533 release_dc_ptr( dc );
539 /****************************************************************************
542 * This should load the correct driver for lpszDevice and calls this driver's
543 * AdvancedSetupDialog proc.
545 INT WINAPI GDI_CallAdvancedSetupDialog16( HWND hwnd, LPSTR lpszDevice,
546 LPDEVMODEA devin, LPDEVMODEA devout )
548 TRACE("(%p, %s, %p, %p)\n", hwnd, lpszDevice, devin, devout );
552 /*****************************************************************************
555 * This should load the correct driver for lpszDevice and calls this driver's
556 * DeviceCapabilities proc.
558 * FIXME: convert DeviceCapabilities to unicode in the driver interface
560 DWORD WINAPI GDI_CallDeviceCapabilities16( LPCSTR lpszDevice, LPCSTR lpszPort,
561 WORD fwCapability, LPSTR lpszOutput,
571 TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice, lpszPort, fwCapability, lpszOutput, lpdm );
573 if (!lpszDevice) return -1;
574 if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
576 if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
578 if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
580 if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
582 if ((dc = get_dc_ptr( hdc )))
584 if (dc->funcs->pDeviceCapabilities)
585 ret = dc->funcs->pDeviceCapabilities( buf, lpszDevice, lpszPort,
586 fwCapability, lpszOutput, lpdm );
587 release_dc_ptr( dc );
594 /************************************************************************
597 INT WINAPI Escape( HDC hdc, INT escape, INT in_count, LPCSTR in_data, LPVOID out_data )
605 return AbortDoc( hdc );
608 return EndDoc( hdc );
610 case GETPHYSPAGESIZE:
612 pt->x = GetDeviceCaps( hdc, PHYSICALWIDTH );
613 pt->y = GetDeviceCaps( hdc, PHYSICALHEIGHT );
616 case GETPRINTINGOFFSET:
618 pt->x = GetDeviceCaps( hdc, PHYSICALOFFSETX );
619 pt->y = GetDeviceCaps( hdc, PHYSICALOFFSETY );
622 case GETSCALINGFACTOR:
624 pt->x = GetDeviceCaps( hdc, SCALINGFACTORX );
625 pt->y = GetDeviceCaps( hdc, SCALINGFACTORY );
629 return EndPage( hdc );
632 return SetAbortProc( hdc, (ABORTPROC)in_data );
639 /* in_data may not be 0 terminated so we must copy it */
642 name = HeapAlloc( GetProcessHeap(), 0, in_count+1 );
643 memcpy( name, in_data, in_count );
646 /* out_data is actually a pointer to the DocInfo structure and used as
647 * a second input parameter */
648 if (out_data) doc = *(DOCINFOA *)out_data;
651 doc.cbSize = sizeof(doc);
652 doc.lpszOutput = NULL;
653 doc.lpszDatatype = NULL;
656 doc.lpszDocName = name;
657 ret = StartDocA( hdc, &doc );
658 HeapFree( GetProcessHeap(), 0, name );
659 if (ret > 0) ret = StartPage( hdc );
663 case QUERYESCSUPPORT:
665 const INT *ptr = (const INT *)in_data;
666 if (in_count < sizeof(INT)) return 0;
671 case GETPHYSPAGESIZE:
672 case GETPRINTINGOFFSET:
673 case GETSCALINGFACTOR:
675 case QUERYESCSUPPORT:
684 /* if not handled internally, pass it to the driver */
685 return ExtEscape( hdc, escape, in_count, in_data, 0, out_data );
689 /******************************************************************************
690 * ExtEscape [GDI32.@]
692 * Access capabilities of a particular device that are not available through GDI.
695 * hdc [I] Handle to device context
696 * nEscape [I] Escape function
697 * cbInput [I] Number of bytes in input structure
698 * lpszInData [I] Pointer to input structure
699 * cbOutput [I] Number of bytes in output structure
700 * lpszOutData [O] Pointer to output structure
707 INT WINAPI ExtEscape( HDC hdc, INT nEscape, INT cbInput, LPCSTR lpszInData,
708 INT cbOutput, LPSTR lpszOutData )
711 DC * dc = get_dc_ptr( hdc );
714 if (dc->funcs->pExtEscape)
715 ret = dc->funcs->pExtEscape( dc->physDev, nEscape, cbInput, lpszInData, cbOutput, lpszOutData );
716 release_dc_ptr( dc );
722 /*******************************************************************
723 * DrawEscape [GDI32.@]
727 INT WINAPI DrawEscape(HDC hdc, INT nEscape, INT cbInput, LPCSTR lpszInData)
729 FIXME("DrawEscape, stub\n");