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(wglShareLists);
207 GET_FUNC(wglUseFontBitmapsA);
208 GET_FUNC(wglUseFontBitmapsW);
211 else memset( &driver->funcs, 0, sizeof(driver->funcs) );
213 /* add it to the list */
215 if ((driver->next = first_driver)) driver->next->prev = driver;
216 first_driver = driver;
221 /**********************************************************************
222 * load_display_driver
224 * Special case for loading the display driver: get the name from the config file
226 static struct graphics_driver *load_display_driver(void)
228 char buffer[MAX_PATH], libname[32], *name, *next;
232 if (display_driver) /* already loaded */
234 display_driver->count++;
235 return display_driver;
238 strcpy( buffer, "x11" ); /* default value */
239 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
240 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Drivers", &hkey ))
242 DWORD type, count = sizeof(buffer);
243 RegQueryValueExA( hkey, "Graphics", 0, &type, (LPBYTE) buffer, &count );
250 next = strchr( name, ',' );
251 if (next) *next++ = 0;
253 snprintf( libname, sizeof(libname), "wine%s.drv", name );
254 if ((module = LoadLibraryA( libname )) != 0) break;
258 if (!(display_driver = create_driver( module )))
260 MESSAGE( "Could not create graphics driver '%s'\n", buffer );
261 FreeLibrary( module );
265 display_driver->count++; /* we don't want to free it */
266 return display_driver;
270 /**********************************************************************
273 const DC_FUNCTIONS *DRIVER_load_driver( LPCWSTR name )
276 struct graphics_driver *driver;
277 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
278 static const WCHAR display1W[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
280 EnterCriticalSection( &driver_section );
282 /* display driver is a special case */
283 if (!strcmpiW( name, displayW ) ||
284 !strcmpiW( name, display1W ))
286 driver = load_display_driver();
287 LeaveCriticalSection( &driver_section );
288 return &driver->funcs;
291 if ((module = GetModuleHandleW( name )))
293 for (driver = first_driver; driver; driver = driver->next)
295 if (driver->module == module)
298 LeaveCriticalSection( &driver_section );
299 return &driver->funcs;
304 if (!(module = LoadLibraryW( name )))
306 LeaveCriticalSection( &driver_section );
310 if (!(driver = create_driver( module )))
312 FreeLibrary( module );
313 LeaveCriticalSection( &driver_section );
317 TRACE( "loaded driver %p for %s\n", driver, debugstr_w(name) );
318 LeaveCriticalSection( &driver_section );
319 return &driver->funcs;
323 /**********************************************************************
326 * Get a new copy of an existing driver.
328 const DC_FUNCTIONS *DRIVER_get_driver( const DC_FUNCTIONS *funcs )
330 struct graphics_driver *driver;
332 EnterCriticalSection( &driver_section );
333 for (driver = first_driver; driver; driver = driver->next)
334 if (&driver->funcs == funcs) break;
335 if (!driver) ERR( "driver not found, trouble ahead\n" );
337 LeaveCriticalSection( &driver_section );
342 /**********************************************************************
343 * DRIVER_release_driver
345 * Release a driver by decrementing ref count and freeing it if needed.
347 void DRIVER_release_driver( const DC_FUNCTIONS *funcs )
349 struct graphics_driver *driver;
351 EnterCriticalSection( &driver_section );
353 for (driver = first_driver; driver; driver = driver->next)
354 if (&driver->funcs == funcs) break;
356 if (!driver) goto done;
357 if (--driver->count) goto done;
359 /* removed last reference, free it */
360 if (driver->next) driver->next->prev = driver->prev;
361 if (driver->prev) driver->prev->next = driver->next;
362 else first_driver = driver->next;
363 if (driver == display_driver) display_driver = NULL;
365 FreeLibrary( driver->module );
366 HeapFree( GetProcessHeap(), 0, driver );
368 LeaveCriticalSection( &driver_section );
372 /*****************************************************************************
373 * DRIVER_GetDriverName
376 BOOL DRIVER_GetDriverName( LPCWSTR device, LPWSTR driver, DWORD size )
378 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
379 static const WCHAR devicesW[] = { 'd','e','v','i','c','e','s',0 };
380 static const WCHAR display1W[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
381 static const WCHAR empty_strW[] = { 0 };
384 /* display is a special case */
385 if (!strcmpiW( device, displayW ) ||
386 !strcmpiW( device, display1W ))
388 lstrcpynW( driver, displayW, size );
392 size = GetProfileStringW(devicesW, device, empty_strW, driver, size);
394 WARN("Unable to find %s in [devices] section of win.ini\n", debugstr_w(device));
397 p = strchrW(driver, ',');
400 WARN("%s entry in [devices] section of win.ini is malformed.\n", debugstr_w(device));
404 TRACE("Found %s for %s\n", debugstr_w(driver), debugstr_w(device));
409 /***********************************************************************
410 * GdiConvertToDevmodeW (GDI32.@)
412 DEVMODEW * WINAPI GdiConvertToDevmodeW(const DEVMODEA *dmA)
415 WORD dmW_size, dmA_size;
417 dmA_size = dmA->dmSize;
419 /* this is the minimal dmSize that XP accepts */
420 if (dmA_size < FIELD_OFFSET(DEVMODEA, dmFields))
423 if (dmA_size > sizeof(DEVMODEA))
424 dmA_size = sizeof(DEVMODEA);
426 dmW_size = dmA_size + CCHDEVICENAME;
427 if (dmA_size >= FIELD_OFFSET(DEVMODEA, dmFormName) + CCHFORMNAME)
428 dmW_size += CCHFORMNAME;
430 dmW = HeapAlloc(GetProcessHeap(), 0, dmW_size + dmA->dmDriverExtra);
431 if (!dmW) return NULL;
433 MultiByteToWideChar(CP_ACP, 0, (const char*) dmA->dmDeviceName, CCHDEVICENAME,
434 dmW->dmDeviceName, CCHDEVICENAME);
435 /* copy slightly more, to avoid long computations */
436 memcpy(&dmW->dmSpecVersion, &dmA->dmSpecVersion, dmA_size - CCHDEVICENAME);
438 if (dmA_size >= FIELD_OFFSET(DEVMODEA, dmFormName) + CCHFORMNAME)
440 MultiByteToWideChar(CP_ACP, 0, (const char*) dmA->dmFormName, CCHFORMNAME,
441 dmW->dmFormName, CCHFORMNAME);
442 if (dmA_size > FIELD_OFFSET(DEVMODEA, dmLogPixels))
443 memcpy(&dmW->dmLogPixels, &dmA->dmLogPixels, dmA_size - FIELD_OFFSET(DEVMODEA, dmLogPixels));
446 if (dmA->dmDriverExtra)
447 memcpy((char *)dmW + dmW_size, (const char *)dmA + dmA_size, dmA->dmDriverExtra);
449 dmW->dmSize = dmW_size;
455 /*****************************************************************************
458 * This should thunk to 16-bit and simply call the proc with the given args.
460 INT WINAPI GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc, HWND hWnd,
461 LPSTR lpModelName, LPSTR OldPort, LPSTR NewPort )
463 FIXME("(%p, %p, %s, %s, %s)\n", lpfnDevInstallProc, hWnd, lpModelName, OldPort, NewPort );
467 /*****************************************************************************
470 * This should load the correct driver for lpszDevice and calls this driver's
471 * ExtDeviceModePropSheet proc.
473 * Note: The driver calls a callback routine for each property sheet page; these
474 * pages are supposed to be filled into the structure pointed to by lpPropSheet.
475 * The layout of this structure is:
481 * HPROPSHEETPAGE pages[10];
484 INT WINAPI GDI_CallExtDeviceModePropSheet16( HWND hWnd, LPCSTR lpszDevice,
485 LPCSTR lpszPort, LPVOID lpPropSheet )
487 FIXME("(%p, %s, %s, %p)\n", hWnd, lpszDevice, lpszPort, lpPropSheet );
491 /*****************************************************************************
494 * This should load the correct driver for lpszDevice and calls this driver's
495 * ExtDeviceMode proc.
497 * FIXME: convert ExtDeviceMode to unicode in the driver interface
499 INT WINAPI GDI_CallExtDeviceMode16( HWND hwnd,
500 LPDEVMODEA lpdmOutput, LPSTR lpszDevice,
501 LPSTR lpszPort, LPDEVMODEA lpdmInput,
502 LPSTR lpszProfile, DWORD fwMode )
511 TRACE("(%p, %p, %s, %s, %p, %s, %d)\n",
512 hwnd, lpdmOutput, lpszDevice, lpszPort, lpdmInput, lpszProfile, fwMode );
514 if (!lpszDevice) return -1;
515 if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
517 if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
519 if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
521 if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
523 if ((dc = get_dc_ptr( hdc )))
525 if (dc->funcs->pExtDeviceMode)
526 ret = dc->funcs->pExtDeviceMode( buf, hwnd, lpdmOutput, lpszDevice, lpszPort,
527 lpdmInput, lpszProfile, fwMode );
528 release_dc_ptr( dc );
534 /****************************************************************************
537 * This should load the correct driver for lpszDevice and calls this driver's
538 * AdvancedSetupDialog proc.
540 INT WINAPI GDI_CallAdvancedSetupDialog16( HWND hwnd, LPSTR lpszDevice,
541 LPDEVMODEA devin, LPDEVMODEA devout )
543 TRACE("(%p, %s, %p, %p)\n", hwnd, lpszDevice, devin, devout );
547 /*****************************************************************************
550 * This should load the correct driver for lpszDevice and calls this driver's
551 * DeviceCapabilities proc.
553 * FIXME: convert DeviceCapabilities to unicode in the driver interface
555 DWORD WINAPI GDI_CallDeviceCapabilities16( LPCSTR lpszDevice, LPCSTR lpszPort,
556 WORD fwCapability, LPSTR lpszOutput,
566 TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice, lpszPort, fwCapability, lpszOutput, lpdm );
568 if (!lpszDevice) return -1;
569 if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
571 if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
573 if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
575 if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
577 if ((dc = get_dc_ptr( hdc )))
579 if (dc->funcs->pDeviceCapabilities)
580 ret = dc->funcs->pDeviceCapabilities( buf, lpszDevice, lpszPort,
581 fwCapability, lpszOutput, lpdm );
582 release_dc_ptr( dc );
589 /************************************************************************
592 INT WINAPI Escape( HDC hdc, INT escape, INT in_count, LPCSTR in_data, LPVOID out_data )
600 return AbortDoc( hdc );
603 return EndDoc( hdc );
605 case GETPHYSPAGESIZE:
607 pt->x = GetDeviceCaps( hdc, PHYSICALWIDTH );
608 pt->y = GetDeviceCaps( hdc, PHYSICALHEIGHT );
611 case GETPRINTINGOFFSET:
613 pt->x = GetDeviceCaps( hdc, PHYSICALOFFSETX );
614 pt->y = GetDeviceCaps( hdc, PHYSICALOFFSETY );
617 case GETSCALINGFACTOR:
619 pt->x = GetDeviceCaps( hdc, SCALINGFACTORX );
620 pt->y = GetDeviceCaps( hdc, SCALINGFACTORY );
624 return EndPage( hdc );
627 return SetAbortProc( hdc, (ABORTPROC)in_data );
634 /* in_data may not be 0 terminated so we must copy it */
637 name = HeapAlloc( GetProcessHeap(), 0, in_count+1 );
638 memcpy( name, in_data, in_count );
641 /* out_data is actually a pointer to the DocInfo structure and used as
642 * a second input parameter */
643 if (out_data) doc = *(DOCINFOA *)out_data;
646 doc.cbSize = sizeof(doc);
647 doc.lpszOutput = NULL;
648 doc.lpszDatatype = NULL;
651 doc.lpszDocName = name;
652 ret = StartDocA( hdc, &doc );
653 HeapFree( GetProcessHeap(), 0, name );
654 if (ret > 0) ret = StartPage( hdc );
658 case QUERYESCSUPPORT:
660 const INT *ptr = (const INT *)in_data;
661 if (in_count < sizeof(INT)) return 0;
666 case GETPHYSPAGESIZE:
667 case GETPRINTINGOFFSET:
668 case GETSCALINGFACTOR:
670 case QUERYESCSUPPORT:
679 /* if not handled internally, pass it to the driver */
680 return ExtEscape( hdc, escape, in_count, in_data, 0, out_data );
684 /******************************************************************************
685 * ExtEscape [GDI32.@]
687 * Access capabilities of a particular device that are not available through GDI.
690 * hdc [I] Handle to device context
691 * nEscape [I] Escape function
692 * cbInput [I] Number of bytes in input structure
693 * lpszInData [I] Pointer to input structure
694 * cbOutput [I] Number of bytes in output structure
695 * lpszOutData [O] Pointer to output structure
702 INT WINAPI ExtEscape( HDC hdc, INT nEscape, INT cbInput, LPCSTR lpszInData,
703 INT cbOutput, LPSTR lpszOutData )
706 DC * dc = get_dc_ptr( hdc );
709 if (dc->funcs->pExtEscape)
710 ret = dc->funcs->pExtEscape( dc->physDev, nEscape, cbInput, lpszInData, cbOutput, lpszOutData );
711 release_dc_ptr( dc );
717 /*******************************************************************
718 * DrawEscape [GDI32.@]
722 INT WINAPI DrawEscape(HDC hdc, INT nEscape, INT cbInput, LPCSTR lpszInData)
724 FIXME("DrawEscape, stub\n");