wined3d: Merge the various resource desc structures.
[wine] / dlls / gdi32 / driver.c
1 /*
2  * Graphics driver management functions
3  *
4  * Copyright 1994 Bob Amstadt
5  * Copyright 1996, 2001 Alexandre Julliard
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "ddrawgdi.h"
32 #include "wine/winbase16.h"
33
34 #include "gdi_private.h"
35 #include "wine/unicode.h"
36 #include "wine/list.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(driver);
40
41 struct graphics_driver
42 {
43     struct list             entry;
44     HMODULE                 module;  /* module handle */
45     DC_FUNCTIONS            funcs;
46 };
47
48 static struct list drivers = LIST_INIT( drivers );
49 static struct graphics_driver *display_driver;
50
51 static CRITICAL_SECTION driver_section;
52 static CRITICAL_SECTION_DEBUG critsect_debug =
53 {
54     0, 0, &driver_section,
55     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
56       0, 0, { (DWORD_PTR)(__FILE__ ": driver_section") }
57 };
58 static CRITICAL_SECTION driver_section = { &critsect_debug, -1, 0, 0, 0, 0 };
59
60 /**********************************************************************
61  *           create_driver
62  *
63  * Allocate and fill the driver structure for a given module.
64  */
65 static struct graphics_driver *create_driver( HMODULE module )
66 {
67     struct graphics_driver *driver;
68
69     if (!(driver = HeapAlloc( GetProcessHeap(), 0, sizeof(*driver)))) return NULL;
70     driver->module = module;
71
72     /* fill the function table */
73     if (module)
74     {
75 #define GET_FUNC(name) driver->funcs.p##name = (void*)GetProcAddress( module, #name )
76         GET_FUNC(AbortDoc);
77         GET_FUNC(AbortPath);
78         GET_FUNC(AlphaBlend);
79         GET_FUNC(AngleArc);
80         GET_FUNC(Arc);
81         GET_FUNC(ArcTo);
82         GET_FUNC(BeginPath);
83         GET_FUNC(BitBlt);
84         GET_FUNC(ChoosePixelFormat);
85         GET_FUNC(Chord);
86         GET_FUNC(CloseFigure);
87         GET_FUNC(CreateBitmap);
88         GET_FUNC(CreateDC);
89         GET_FUNC(CreateDIBSection);
90         GET_FUNC(DeleteBitmap);
91         GET_FUNC(DeleteDC);
92         GET_FUNC(DescribePixelFormat);
93         GET_FUNC(DeviceCapabilities);
94         GET_FUNC(Ellipse);
95         GET_FUNC(EndDoc);
96         GET_FUNC(EndPage);
97         GET_FUNC(EndPath);
98         GET_FUNC(EnumDeviceFonts);
99         GET_FUNC(EnumICMProfiles);
100         GET_FUNC(ExcludeClipRect);
101         GET_FUNC(ExtDeviceMode);
102         GET_FUNC(ExtEscape);
103         GET_FUNC(ExtFloodFill);
104         GET_FUNC(ExtSelectClipRgn);
105         GET_FUNC(ExtTextOut);
106         GET_FUNC(FillPath);
107         GET_FUNC(FillRgn);
108         GET_FUNC(FlattenPath);
109         GET_FUNC(FrameRgn);
110         GET_FUNC(GdiComment);
111         GET_FUNC(GetBitmapBits);
112         GET_FUNC(GetCharWidth);
113         GET_FUNC(GetDIBits);
114         GET_FUNC(GetDeviceCaps);
115         GET_FUNC(GetDeviceGammaRamp);
116         GET_FUNC(GetICMProfile);
117         GET_FUNC(GetNearestColor);
118         GET_FUNC(GetPixel);
119         GET_FUNC(GetPixelFormat);
120         GET_FUNC(GetSystemPaletteEntries);
121         GET_FUNC(GetTextExtentExPoint);
122         GET_FUNC(GetTextMetrics);
123         GET_FUNC(IntersectClipRect);
124         GET_FUNC(InvertRgn);
125         GET_FUNC(LineTo);
126         GET_FUNC(MoveTo);
127         GET_FUNC(ModifyWorldTransform);
128         GET_FUNC(OffsetClipRgn);
129         GET_FUNC(OffsetViewportOrg);
130         GET_FUNC(OffsetWindowOrg);
131         GET_FUNC(PaintRgn);
132         GET_FUNC(PatBlt);
133         GET_FUNC(Pie);
134         GET_FUNC(PolyBezier);
135         GET_FUNC(PolyBezierTo);
136         GET_FUNC(PolyDraw);
137         GET_FUNC(PolyPolygon);
138         GET_FUNC(PolyPolyline);
139         GET_FUNC(Polygon);
140         GET_FUNC(Polyline);
141         GET_FUNC(PolylineTo);
142         GET_FUNC(RealizeDefaultPalette);
143         GET_FUNC(RealizePalette);
144         GET_FUNC(Rectangle);
145         GET_FUNC(ResetDC);
146         GET_FUNC(RestoreDC);
147         GET_FUNC(RoundRect);
148         GET_FUNC(SaveDC);
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);
156         GET_FUNC(SelectPen);
157         GET_FUNC(SetArcDirection);
158         GET_FUNC(SetBitmapBits);
159         GET_FUNC(SetBkColor);
160         GET_FUNC(SetBkMode);
161         GET_FUNC(SetDCBrushColor);
162         GET_FUNC(SetDCPenColor);
163         GET_FUNC(SetDIBColorTable);
164         GET_FUNC(SetDIBits);
165         GET_FUNC(SetDIBitsToDevice);
166         GET_FUNC(SetDeviceClipping);
167         GET_FUNC(SetDeviceGammaRamp);
168         GET_FUNC(SetMapMode);
169         GET_FUNC(SetMapperFlags);
170         GET_FUNC(SetPixel);
171         GET_FUNC(SetPixelFormat);
172         GET_FUNC(SetPolyFillMode);
173         GET_FUNC(SetROP2);
174         GET_FUNC(SetRelAbs);
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);
185         GET_FUNC(StartDoc);
186         GET_FUNC(StartPage);
187         GET_FUNC(StretchBlt);
188         GET_FUNC(StretchDIBits);
189         GET_FUNC(StrokeAndFillPath);
190         GET_FUNC(StrokePath);
191         GET_FUNC(SwapBuffers);
192         GET_FUNC(UnrealizePalette);
193         GET_FUNC(WidenPath);
194
195         /* OpenGL32 */
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);
207 #undef GET_FUNC
208     }
209     else memset( &driver->funcs, 0, sizeof(driver->funcs) );
210
211     return driver;
212 }
213
214
215 /**********************************************************************
216  *           DRIVER_get_display_driver
217  *
218  * Special case for loading the display driver: get the name from the config file
219  */
220 const DC_FUNCTIONS *DRIVER_get_display_driver(void)
221 {
222     struct graphics_driver *driver;
223     char buffer[MAX_PATH], libname[32], *name, *next;
224     HMODULE module = 0;
225     HKEY hkey;
226
227     if (display_driver) return &display_driver->funcs;  /* already loaded */
228
229     strcpy( buffer, "x11" );  /* default value */
230     /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
231     if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Drivers", &hkey ))
232     {
233         DWORD type, count = sizeof(buffer);
234         RegQueryValueExA( hkey, "Graphics", 0, &type, (LPBYTE) buffer, &count );
235         RegCloseKey( hkey );
236     }
237
238     name = buffer;
239     while (name)
240     {
241         next = strchr( name, ',' );
242         if (next) *next++ = 0;
243
244         snprintf( libname, sizeof(libname), "wine%s.drv", name );
245         if ((module = LoadLibraryA( libname )) != 0) break;
246         name = next;
247     }
248
249     if (!(driver = create_driver( module )))
250     {
251         MESSAGE( "Could not create graphics driver '%s'\n", buffer );
252         FreeLibrary( module );
253         ExitProcess(1);
254     }
255     if (InterlockedCompareExchangePointer( (void **)&display_driver, driver, NULL ))
256     {
257         /* somebody beat us to it */
258         FreeLibrary( driver->module );
259         HeapFree( GetProcessHeap(), 0, driver );
260     }
261     return &display_driver->funcs;
262 }
263
264
265 /**********************************************************************
266  *           DRIVER_load_driver
267  */
268 const DC_FUNCTIONS *DRIVER_load_driver( LPCWSTR name )
269 {
270     HMODULE module;
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};
274
275     /* display driver is a special case */
276     if (!strcmpiW( name, displayW ) || !strcmpiW( name, display1W )) return DRIVER_get_display_driver();
277
278     if ((module = GetModuleHandleW( name )))
279     {
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 )
283         {
284             if (driver->module == module) goto done;
285         }
286         LeaveCriticalSection( &driver_section );
287     }
288
289     if (!(module = LoadLibraryW( name ))) return NULL;
290
291     if (!(new_driver = create_driver( module )))
292     {
293         FreeLibrary( module );
294         return NULL;
295     }
296
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 )
300     {
301         if (driver->module != module) continue;
302         FreeLibrary( module );
303         HeapFree( GetProcessHeap(), 0, new_driver );
304         goto done;
305     }
306     driver = new_driver;
307     list_add_head( &drivers, &driver->entry );
308     TRACE( "loaded driver %p for %s\n", driver, debugstr_w(name) );
309 done:
310     LeaveCriticalSection( &driver_section );
311     return &driver->funcs;
312 }
313
314
315 static INT CDECL nulldrv_AbortDoc( PHYSDEV dev )
316 {
317     return 0;
318 }
319
320 static BOOL CDECL nulldrv_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
321                                INT xstart, INT ystart, INT xend, INT yend )
322 {
323     return TRUE;
324 }
325
326 static BOOL CDECL nulldrv_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
327                                  INT xstart, INT ystart, INT xend, INT yend )
328 {
329     return TRUE;
330 }
331
332 static BOOL CDECL nulldrv_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
333 {
334     return TRUE;
335 }
336
337 static INT CDECL nulldrv_EndDoc( PHYSDEV dev )
338 {
339     return 0;
340 }
341
342 static INT CDECL nulldrv_EndPage( PHYSDEV dev )
343 {
344     return 0;
345 }
346
347 static BOOL CDECL nulldrv_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT type )
348 {
349     return TRUE;
350 }
351
352 static COLORREF CDECL nulldrv_GetPixel( PHYSDEV dev, INT x, INT y )
353 {
354     return 0;
355 }
356
357 static BOOL CDECL nulldrv_LineTo( PHYSDEV dev, INT x, INT y )
358 {
359     return TRUE;
360 }
361
362 static BOOL CDECL nulldrv_MoveTo( PHYSDEV dev, INT x, INT y )
363 {
364     return TRUE;
365 }
366
367 static BOOL CDECL nulldrv_PaintRgn( PHYSDEV dev, HRGN rgn )
368 {
369     return TRUE;
370 }
371
372 static BOOL CDECL nulldrv_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
373                                INT xstart, INT ystart, INT xend, INT yend )
374 {
375     return TRUE;
376 }
377
378 static BOOL CDECL nulldrv_PolyPolygon( PHYSDEV dev, const POINT *points, const INT *counts, UINT polygons )
379 {
380     /* FIXME: could be implemented with Polygon */
381     return TRUE;
382 }
383
384 static BOOL CDECL nulldrv_PolyPolyline( PHYSDEV dev, const POINT *points, const DWORD *counts, DWORD lines )
385 {
386     /* FIXME: could be implemented with Polyline */
387     return TRUE;
388 }
389
390 static BOOL CDECL nulldrv_Polygon( PHYSDEV dev, const POINT *points, INT count )
391 {
392     return TRUE;
393 }
394
395 static BOOL CDECL nulldrv_Polyline( PHYSDEV dev, const POINT *points, INT count )
396 {
397     return TRUE;
398 }
399
400 static BOOL CDECL nulldrv_Rectangle( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
401 {
402     return TRUE;
403 }
404
405 static BOOL CDECL nulldrv_RoundRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
406                                      INT ell_width, INT ell_height )
407 {
408     return TRUE;
409 }
410
411 static COLORREF CDECL nulldrv_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
412 {
413     return color;
414 }
415
416 static INT CDECL nulldrv_StartDoc( PHYSDEV dev, const DOCINFOW *info )
417 {
418     return 0;
419 }
420
421 static INT CDECL nulldrv_StartPage( PHYSDEV dev )
422 {
423     return 1;
424 }
425
426 const DC_FUNCTIONS null_driver =
427 {
428     nulldrv_AbortDoc,                   /* pAbortDoc */
429     NULL,                               /* pAbortPath */
430     NULL,                               /* pAlphaBlend */
431     nulldrv_AngleArc,                   /* pAngleArc */
432     nulldrv_Arc,                        /* pArc */
433     nulldrv_ArcTo,                      /* pArcTo */
434     NULL,                               /* pBeginPath */
435     NULL,                               /* pBitBlt */
436     NULL,                               /* pChoosePixelFormat */
437     nulldrv_Chord,                      /* pChord */
438     NULL,                               /* pCloseFigure */
439     NULL,                               /* pCreateBitmap */
440     NULL,                               /* pCreateDC */
441     NULL,                               /* pCreateDIBSection */
442     NULL,                               /* pDeleteBitmap */
443     NULL,                               /* pDeleteDC */
444     NULL,                               /* pDeleteObject */
445     NULL,                               /* pDescribePixelFormat */
446     NULL,                               /* pDeviceCapabilities */
447     nulldrv_Ellipse,                    /* pEllipse */
448     nulldrv_EndDoc,                     /* pEndDoc */
449     nulldrv_EndPage,                    /* pEndPage */
450     NULL,                               /* pEndPath */
451     NULL,                               /* pEnumICMProfiles */
452     NULL,                               /* pEnumDeviceFonts */
453     NULL,                               /* pExcludeClipRect */
454     NULL,                               /* pExtDeviceMode */
455     NULL,                               /* pExtEscape */
456     nulldrv_ExtFloodFill,               /* pExtFloodFill */
457     NULL,                               /* pExtSelectClipRgn */
458     NULL,                               /* pExtTextOut */
459     NULL,                               /* pFillPath */
460     nulldrv_FillRgn,                    /* pFillRgn */
461     NULL,                               /* pFlattenPath */
462     nulldrv_FrameRgn,                   /* pFrameRgn */
463     NULL,                               /* pGdiComment */
464     NULL,                               /* pGetBitmapBits */
465     NULL,                               /* pGetCharWidth */
466     NULL,                               /* pGetDIBits */
467     NULL,                               /* pGetDeviceCaps */
468     NULL,                               /* pGetDeviceGammaRamp */
469     NULL,                               /* pGetICMProfile */
470     NULL,                               /* pGetNearestColor */
471     nulldrv_GetPixel,                   /* pGetPixel */
472     NULL,                               /* pGetPixelFormat */
473     NULL,                               /* pGetSystemPaletteEntries */
474     NULL,                               /* pGetTextExtentExPoint */
475     NULL,                               /* pGetTextMetrics */
476     NULL,                               /* pIntersectClipRect */
477     nulldrv_InvertRgn,                  /* pInvertRgn */
478     nulldrv_LineTo,                     /* pLineTo */
479     NULL,                               /* pModifyWorldTransform */
480     nulldrv_MoveTo,                     /* pMoveTo */
481     NULL,                               /* pOffsetClipRgn */
482     NULL,                               /* pOffsetViewportOrg */
483     NULL,                               /* pOffsetWindowOrg */
484     nulldrv_PaintRgn,                   /* pPaintRgn */
485     NULL,                               /* pPatBlt */
486     nulldrv_Pie,                        /* pPie */
487     nulldrv_PolyBezier,                 /* pPolyBezier */
488     nulldrv_PolyBezierTo,               /* pPolyBezierTo */
489     nulldrv_PolyDraw,                   /* pPolyDraw */
490     nulldrv_PolyPolygon,                /* pPolyPolygon */
491     nulldrv_PolyPolyline,               /* pPolyPolyline */
492     nulldrv_Polygon,                    /* pPolygon */
493     nulldrv_Polyline,                   /* pPolyline */
494     nulldrv_PolylineTo,                 /* pPolylineTo */
495     NULL,                               /* pRealizeDefaultPalette */
496     NULL,                               /* pRealizePalette */
497     nulldrv_Rectangle,                  /* pRectangle */
498     NULL,                               /* pResetDC */
499     NULL,                               /* pRestoreDC */
500     nulldrv_RoundRect,                  /* pRoundRect */
501     NULL,                               /* pSaveDC */
502     NULL,                               /* pScaleViewportExt */
503     NULL,                               /* pScaleWindowExt */
504     NULL,                               /* pSelectBitmap */
505     NULL,                               /* pSelectBrush */
506     NULL,                               /* pSelectClipPath */
507     NULL,                               /* pSelectFont */
508     NULL,                               /* pSelectPalette */
509     NULL,                               /* pSelectPen */
510     NULL,                               /* pSetArcDirection */
511     NULL,                               /* pSetBitmapBits */
512     NULL,                               /* pSetBkColor */
513     NULL,                               /* pSetBkMode */
514     NULL,                               /* pSetDCBrushColor */
515     NULL,                               /* pSetDCPenColor */
516     NULL,                               /* pSetDIBColorTable */
517     NULL,                               /* pSetDIBits */
518     NULL,                               /* pSetDIBitsToDevice */
519     NULL,                               /* pSetDeviceClipping */
520     NULL,                               /* pSetDeviceGammaRamp */
521     NULL,                               /* pSetMapMode */
522     NULL,                               /* pSetMapperFlags */
523     nulldrv_SetPixel,                   /* pSetPixel */
524     NULL,                               /* pSetPixelFormat */
525     NULL,                               /* pSetPolyFillMode */
526     NULL,                               /* pSetROP2 */
527     NULL,                               /* pSetRelAbs */
528     NULL,                               /* pSetStretchBltMode */
529     NULL,                               /* pSetTextAlign */
530     NULL,                               /* pSetTextCharacterExtra */
531     NULL,                               /* pSetTextColor */
532     NULL,                               /* pSetTextJustification */
533     NULL,                               /* pSetViewportExt */
534     NULL,                               /* pSetViewportOrg */
535     NULL,                               /* pSetWindowExt */
536     NULL,                               /* pSetWindowOrg */
537     NULL,                               /* pSetWorldTransform */
538     nulldrv_StartDoc,                   /* pStartDoc */
539     nulldrv_StartPage,                  /* pStartPage */
540     NULL,                               /* pStretchBlt */
541     NULL,                               /* pStretchDIBits */
542     NULL,                               /* pStrokeAndFillPath */
543     NULL,                               /* pStrokePath */
544     NULL,                               /* pSwapBuffers */
545     NULL,                               /* pUnrealizePalette */
546     NULL,                               /* pWidenPath */
547     NULL,                               /* pwglCopyContext */
548     NULL,                               /* pwglCreateContext */
549     NULL,                               /* pwglCreateContextAttribsARB */
550     NULL,                               /* pwglDeleteContext */
551     NULL,                               /* pwglGetProcAddress */
552     NULL,                               /* pwglGetPbufferDCARB */
553     NULL,                               /* pwglMakeCurrent */
554     NULL,                               /* pwglMakeContextCurrentARB */
555     NULL,                               /* pwglSetPixelFormatWINE */
556     NULL,                               /* pwglShareLists */
557     NULL,                               /* pwglUseFontBitmapsA */
558     NULL,                               /* pwglUseFontBitmapsW */
559 };
560
561
562 /*****************************************************************************
563  *      DRIVER_GetDriverName
564  *
565  */
566 BOOL DRIVER_GetDriverName( LPCWSTR device, LPWSTR driver, DWORD size )
567 {
568     static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
569     static const WCHAR devicesW[] = { 'd','e','v','i','c','e','s',0 };
570     static const WCHAR display1W[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y','1',0};
571     static const WCHAR empty_strW[] = { 0 };
572     WCHAR *p;
573
574     /* display is a special case */
575     if (!strcmpiW( device, displayW ) ||
576         !strcmpiW( device, display1W ))
577     {
578         lstrcpynW( driver, displayW, size );
579         return TRUE;
580     }
581
582     size = GetProfileStringW(devicesW, device, empty_strW, driver, size);
583     if(!size) {
584         WARN("Unable to find %s in [devices] section of win.ini\n", debugstr_w(device));
585         return FALSE;
586     }
587     p = strchrW(driver, ',');
588     if(!p)
589     {
590         WARN("%s entry in [devices] section of win.ini is malformed.\n", debugstr_w(device));
591         return FALSE;
592     }
593     *p = 0;
594     TRACE("Found %s for %s\n", debugstr_w(driver), debugstr_w(device));
595     return TRUE;
596 }
597
598
599 /***********************************************************************
600  *           GdiConvertToDevmodeW    (GDI32.@)
601  */
602 DEVMODEW * WINAPI GdiConvertToDevmodeW(const DEVMODEA *dmA)
603 {
604     DEVMODEW *dmW;
605     WORD dmW_size, dmA_size;
606
607     dmA_size = dmA->dmSize;
608
609     /* this is the minimal dmSize that XP accepts */
610     if (dmA_size < FIELD_OFFSET(DEVMODEA, dmFields))
611         return NULL;
612
613     if (dmA_size > sizeof(DEVMODEA))
614         dmA_size = sizeof(DEVMODEA);
615
616     dmW_size = dmA_size + CCHDEVICENAME;
617     if (dmA_size >= FIELD_OFFSET(DEVMODEA, dmFormName) + CCHFORMNAME)
618         dmW_size += CCHFORMNAME;
619
620     dmW = HeapAlloc(GetProcessHeap(), 0, dmW_size + dmA->dmDriverExtra);
621     if (!dmW) return NULL;
622
623     MultiByteToWideChar(CP_ACP, 0, (const char*) dmA->dmDeviceName, -1,
624                                    dmW->dmDeviceName, CCHDEVICENAME);
625     /* copy slightly more, to avoid long computations */
626     memcpy(&dmW->dmSpecVersion, &dmA->dmSpecVersion, dmA_size - CCHDEVICENAME);
627
628     if (dmA_size >= FIELD_OFFSET(DEVMODEA, dmFormName) + CCHFORMNAME)
629     {
630         if (dmA->dmFields & DM_FORMNAME)
631             MultiByteToWideChar(CP_ACP, 0, (const char*) dmA->dmFormName, -1,
632                                        dmW->dmFormName, CCHFORMNAME);
633         else
634             dmW->dmFormName[0] = 0;
635
636         if (dmA_size > FIELD_OFFSET(DEVMODEA, dmLogPixels))
637             memcpy(&dmW->dmLogPixels, &dmA->dmLogPixels, dmA_size - FIELD_OFFSET(DEVMODEA, dmLogPixels));
638     }
639
640     if (dmA->dmDriverExtra)
641         memcpy((char *)dmW + dmW_size, (const char *)dmA + dmA_size, dmA->dmDriverExtra);
642
643     dmW->dmSize = dmW_size;
644
645     return dmW;
646 }
647
648
649 /*****************************************************************************
650  *      @ [GDI32.100]
651  *
652  * This should thunk to 16-bit and simply call the proc with the given args.
653  */
654 INT WINAPI GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc, HWND hWnd,
655                                  LPSTR lpModelName, LPSTR OldPort, LPSTR NewPort )
656 {
657     FIXME("(%p, %p, %s, %s, %s)\n", lpfnDevInstallProc, hWnd, lpModelName, OldPort, NewPort );
658     return -1;
659 }
660
661 /*****************************************************************************
662  *      @ [GDI32.101]
663  *
664  * This should load the correct driver for lpszDevice and calls this driver's
665  * ExtDeviceModePropSheet proc.
666  *
667  * Note: The driver calls a callback routine for each property sheet page; these
668  * pages are supposed to be filled into the structure pointed to by lpPropSheet.
669  * The layout of this structure is:
670  *
671  * struct
672  * {
673  *   DWORD  nPages;
674  *   DWORD  unknown;
675  *   HPROPSHEETPAGE  pages[10];
676  * };
677  */
678 INT WINAPI GDI_CallExtDeviceModePropSheet16( HWND hWnd, LPCSTR lpszDevice,
679                                              LPCSTR lpszPort, LPVOID lpPropSheet )
680 {
681     FIXME("(%p, %s, %s, %p)\n", hWnd, lpszDevice, lpszPort, lpPropSheet );
682     return -1;
683 }
684
685 /*****************************************************************************
686  *      @ [GDI32.102]
687  *
688  * This should load the correct driver for lpszDevice and call this driver's
689  * ExtDeviceMode proc.
690  *
691  * FIXME: convert ExtDeviceMode to unicode in the driver interface
692  */
693 INT WINAPI GDI_CallExtDeviceMode16( HWND hwnd,
694                                     LPDEVMODEA lpdmOutput, LPSTR lpszDevice,
695                                     LPSTR lpszPort, LPDEVMODEA lpdmInput,
696                                     LPSTR lpszProfile, DWORD fwMode )
697 {
698     WCHAR deviceW[300];
699     WCHAR bufW[300];
700     char buf[300];
701     HDC hdc;
702     DC *dc;
703     INT ret = -1;
704
705     TRACE("(%p, %p, %s, %s, %p, %s, %d)\n",
706           hwnd, lpdmOutput, lpszDevice, lpszPort, lpdmInput, lpszProfile, fwMode );
707
708     if (!lpszDevice) return -1;
709     if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
710
711     if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
712
713     if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
714
715     if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
716
717     if ((dc = get_dc_ptr( hdc )))
718     {
719         if (dc->funcs->pExtDeviceMode)
720             ret = dc->funcs->pExtDeviceMode( buf, hwnd, lpdmOutput, lpszDevice, lpszPort,
721                                              lpdmInput, lpszProfile, fwMode );
722         release_dc_ptr( dc );
723     }
724     DeleteDC( hdc );
725     return ret;
726 }
727
728 /****************************************************************************
729  *      @ [GDI32.103]
730  *
731  * This should load the correct driver for lpszDevice and calls this driver's
732  * AdvancedSetupDialog proc.
733  */
734 INT WINAPI GDI_CallAdvancedSetupDialog16( HWND hwnd, LPSTR lpszDevice,
735                                           LPDEVMODEA devin, LPDEVMODEA devout )
736 {
737     TRACE("(%p, %s, %p, %p)\n", hwnd, lpszDevice, devin, devout );
738     return -1;
739 }
740
741 /*****************************************************************************
742  *      @ [GDI32.104]
743  *
744  * This should load the correct driver for lpszDevice and calls this driver's
745  * DeviceCapabilities proc.
746  *
747  * FIXME: convert DeviceCapabilities to unicode in the driver interface
748  */
749 DWORD WINAPI GDI_CallDeviceCapabilities16( LPCSTR lpszDevice, LPCSTR lpszPort,
750                                            WORD fwCapability, LPSTR lpszOutput,
751                                            LPDEVMODEA lpdm )
752 {
753     WCHAR deviceW[300];
754     WCHAR bufW[300];
755     char buf[300];
756     HDC hdc;
757     DC *dc;
758     INT ret = -1;
759
760     TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice, lpszPort, fwCapability, lpszOutput, lpdm );
761
762     if (!lpszDevice) return -1;
763     if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
764
765     if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
766
767     if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
768
769     if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
770
771     if ((dc = get_dc_ptr( hdc )))
772     {
773         if (dc->funcs->pDeviceCapabilities)
774             ret = dc->funcs->pDeviceCapabilities( buf, lpszDevice, lpszPort,
775                                                   fwCapability, lpszOutput, lpdm );
776         release_dc_ptr( dc );
777     }
778     DeleteDC( hdc );
779     return ret;
780 }
781
782
783 /************************************************************************
784  *             Escape  [GDI32.@]
785  */
786 INT WINAPI Escape( HDC hdc, INT escape, INT in_count, LPCSTR in_data, LPVOID out_data )
787 {
788     INT ret;
789     POINT *pt;
790
791     switch (escape)
792     {
793     case ABORTDOC:
794         return AbortDoc( hdc );
795
796     case ENDDOC:
797         return EndDoc( hdc );
798
799     case GETPHYSPAGESIZE:
800         pt = out_data;
801         pt->x = GetDeviceCaps( hdc, PHYSICALWIDTH );
802         pt->y = GetDeviceCaps( hdc, PHYSICALHEIGHT );
803         return 1;
804
805     case GETPRINTINGOFFSET:
806         pt = out_data;
807         pt->x = GetDeviceCaps( hdc, PHYSICALOFFSETX );
808         pt->y = GetDeviceCaps( hdc, PHYSICALOFFSETY );
809         return 1;
810
811     case GETSCALINGFACTOR:
812         pt = out_data;
813         pt->x = GetDeviceCaps( hdc, SCALINGFACTORX );
814         pt->y = GetDeviceCaps( hdc, SCALINGFACTORY );
815         return 1;
816
817     case NEWFRAME:
818         return EndPage( hdc );
819
820     case SETABORTPROC:
821         return SetAbortProc( hdc, (ABORTPROC)in_data );
822
823     case STARTDOC:
824         {
825             DOCINFOA doc;
826             char *name = NULL;
827
828             /* in_data may not be 0 terminated so we must copy it */
829             if (in_data)
830             {
831                 name = HeapAlloc( GetProcessHeap(), 0, in_count+1 );
832                 memcpy( name, in_data, in_count );
833                 name[in_count] = 0;
834             }
835             /* out_data is actually a pointer to the DocInfo structure and used as
836              * a second input parameter */
837             if (out_data) doc = *(DOCINFOA *)out_data;
838             else
839             {
840                 doc.cbSize = sizeof(doc);
841                 doc.lpszOutput = NULL;
842                 doc.lpszDatatype = NULL;
843                 doc.fwType = 0;
844             }
845             doc.lpszDocName = name;
846             ret = StartDocA( hdc, &doc );
847             HeapFree( GetProcessHeap(), 0, name );
848             if (ret > 0) ret = StartPage( hdc );
849             return ret;
850         }
851
852     case QUERYESCSUPPORT:
853         {
854             const INT *ptr = (const INT *)in_data;
855             if (in_count < sizeof(INT)) return 0;
856             switch(*ptr)
857             {
858             case ABORTDOC:
859             case ENDDOC:
860             case GETPHYSPAGESIZE:
861             case GETPRINTINGOFFSET:
862             case GETSCALINGFACTOR:
863             case NEWFRAME:
864             case QUERYESCSUPPORT:
865             case SETABORTPROC:
866             case STARTDOC:
867                 return TRUE;
868             }
869             break;
870         }
871     }
872
873     /* if not handled internally, pass it to the driver */
874     return ExtEscape( hdc, escape, in_count, in_data, 0, out_data );
875 }
876
877
878 /******************************************************************************
879  *              ExtEscape       [GDI32.@]
880  *
881  * Access capabilities of a particular device that are not available through GDI.
882  *
883  * PARAMS
884  *    hdc         [I] Handle to device context
885  *    nEscape     [I] Escape function
886  *    cbInput     [I] Number of bytes in input structure
887  *    lpszInData  [I] Pointer to input structure
888  *    cbOutput    [I] Number of bytes in output structure
889  *    lpszOutData [O] Pointer to output structure
890  *
891  * RETURNS
892  *    Success: >0
893  *    Not implemented: 0
894  *    Failure: <0
895  */
896 INT WINAPI ExtEscape( HDC hdc, INT nEscape, INT cbInput, LPCSTR lpszInData,
897                       INT cbOutput, LPSTR lpszOutData )
898 {
899     INT ret = 0;
900     DC * dc = get_dc_ptr( hdc );
901     if (dc)
902     {
903         if (dc->funcs->pExtEscape)
904             ret = dc->funcs->pExtEscape( dc->physDev, nEscape, cbInput, lpszInData, cbOutput, lpszOutData );
905         release_dc_ptr( dc );
906     }
907     return ret;
908 }
909
910
911 /*******************************************************************
912  *      DrawEscape [GDI32.@]
913  *
914  *
915  */
916 INT WINAPI DrawEscape(HDC hdc, INT nEscape, INT cbInput, LPCSTR lpszInData)
917 {
918     FIXME("DrawEscape, stub\n");
919     return 0;
920 }
921
922 /*******************************************************************
923  *      NamedEscape [GDI32.@]
924  */
925 INT WINAPI NamedEscape( HDC hdc, LPCWSTR pDriver, INT nEscape, INT cbInput, LPCSTR lpszInData,
926                         INT cbOutput, LPSTR lpszOutData )
927 {
928     FIXME("(%p, %s, %d, %d, %p, %d, %p)\n",
929           hdc, wine_dbgstr_w(pDriver), nEscape, cbInput, lpszInData, cbOutput,
930           lpszOutData);
931     return 0;
932 }
933
934 /*******************************************************************
935  *      DdQueryDisplaySettingsUniqueness [GDI32.@]
936  *      GdiEntry13                       [GDI32.@]
937  */
938 ULONG WINAPI DdQueryDisplaySettingsUniqueness(VOID)
939 {
940     static int warn_once;
941
942     if (!warn_once++)
943         FIXME("stub\n");
944     return 0;
945 }