Avoid excessive heap memory reallocation when generating EMF
[wine] / dlls / gdi / driver.c
1 /*
2  * Graphics driver management functions
3  *
4  * Copyright 1996 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29
30 #include "gdi.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(driver);
35
36 struct graphics_driver
37 {
38     struct graphics_driver *next;
39     struct graphics_driver *prev;
40     HMODULE                 module;  /* module handle */
41     unsigned int            count;   /* reference count */
42     DC_FUNCTIONS            funcs;
43 };
44
45 static struct graphics_driver *first_driver;
46 static struct graphics_driver *display_driver;
47
48 static CRITICAL_SECTION driver_section;
49 static CRITICAL_SECTION_DEBUG critsect_debug =
50 {
51     0, 0, &driver_section,
52     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
53       0, 0, { 0, (DWORD)(__FILE__ ": driver_section") }
54 };
55 static CRITICAL_SECTION driver_section = { &critsect_debug, -1, 0, 0, 0, 0 };
56
57 /**********************************************************************
58  *           create_driver
59  *
60  * Allocate and fill the driver structure for a given module.
61  */
62 static struct graphics_driver *create_driver( HMODULE module )
63 {
64     struct graphics_driver *driver;
65
66     if (!(driver = HeapAlloc( GetProcessHeap(), 0, sizeof(*driver)))) return NULL;
67     driver->next   = NULL;
68     driver->prev   = NULL;
69     driver->module = module;
70     driver->count  = 1;
71
72     /* fill the function table */
73
74 #define GET_FUNC(name) driver->funcs.p##name = (void*)GetProcAddress( module, #name )
75
76     GET_FUNC(AbortDoc);
77     GET_FUNC(AbortPath);
78     GET_FUNC(AngleArc);
79     GET_FUNC(Arc);
80     GET_FUNC(ArcTo);
81     GET_FUNC(BeginPath);
82     GET_FUNC(BitBlt);
83     GET_FUNC(ChoosePixelFormat);
84     GET_FUNC(Chord);
85     GET_FUNC(CloseFigure);
86     GET_FUNC(CreateBitmap);
87     GET_FUNC(CreateDC);
88     GET_FUNC(CreateDIBSection);
89     GET_FUNC(DeleteBitmap);
90     GET_FUNC(DeleteDC);
91     GET_FUNC(DescribePixelFormat);
92     GET_FUNC(DeviceCapabilities);
93     GET_FUNC(Ellipse);
94     GET_FUNC(EndDoc);
95     GET_FUNC(EndPage);
96     GET_FUNC(EndPath);
97     GET_FUNC(EnumDeviceFonts);
98     GET_FUNC(ExcludeClipRect);
99     GET_FUNC(ExtDeviceMode);
100     GET_FUNC(ExtEscape);
101     GET_FUNC(ExtFloodFill);
102     GET_FUNC(ExtSelectClipRgn);
103     GET_FUNC(ExtTextOut);
104     GET_FUNC(FillPath);
105     GET_FUNC(FillRgn);
106     GET_FUNC(FlattenPath);
107     GET_FUNC(FrameRgn);
108     GET_FUNC(GdiComment);
109     GET_FUNC(GetBitmapBits);
110     GET_FUNC(GetCharWidth);
111     GET_FUNC(GetDCOrgEx);
112     GET_FUNC(GetDIBColorTable);
113     GET_FUNC(GetDIBits);
114     GET_FUNC(GetDeviceCaps);
115     GET_FUNC(GetDeviceGammaRamp);
116     GET_FUNC(GetNearestColor);
117     GET_FUNC(GetPixel);
118     GET_FUNC(GetPixelFormat);
119     GET_FUNC(GetSystemPaletteEntries);
120     GET_FUNC(GetTextExtentPoint);
121     GET_FUNC(GetTextMetrics);
122     GET_FUNC(IntersectClipRect);
123     GET_FUNC(InvertRgn);
124     GET_FUNC(LineTo);
125     GET_FUNC(MoveTo);
126     GET_FUNC(ModifyWorldTransform);
127     GET_FUNC(OffsetClipRgn);
128     GET_FUNC(OffsetViewportOrg);
129     GET_FUNC(OffsetWindowOrg);
130     GET_FUNC(PaintRgn);
131     GET_FUNC(PatBlt);
132     GET_FUNC(Pie);
133     GET_FUNC(PolyBezier);
134     GET_FUNC(PolyBezierTo);
135     GET_FUNC(PolyDraw);
136     GET_FUNC(PolyPolygon);
137     GET_FUNC(PolyPolyline);
138     GET_FUNC(Polygon);
139     GET_FUNC(Polyline);
140     GET_FUNC(PolylineTo);
141     GET_FUNC(RealizeDefaultPalette);
142     GET_FUNC(RealizePalette);
143     GET_FUNC(Rectangle);
144     GET_FUNC(ResetDC);
145     GET_FUNC(RestoreDC);
146     GET_FUNC(RoundRect);
147     GET_FUNC(SaveDC);
148     GET_FUNC(ScaleViewportExt);
149     GET_FUNC(ScaleWindowExt);
150     GET_FUNC(SelectBitmap);
151     GET_FUNC(SelectBrush);
152     GET_FUNC(SelectClipPath);
153     GET_FUNC(SelectFont);
154     GET_FUNC(SelectPalette);
155     GET_FUNC(SelectPen);
156     GET_FUNC(SetArcDirection);
157     GET_FUNC(SetBitmapBits);
158     GET_FUNC(SetBkColor);
159     GET_FUNC(SetBkMode);
160     GET_FUNC(SetDCOrg);
161     GET_FUNC(SetDIBColorTable);
162     GET_FUNC(SetDIBits);
163     GET_FUNC(SetDIBitsToDevice);
164     GET_FUNC(SetDeviceClipping);
165     GET_FUNC(SetDeviceGammaRamp);
166     GET_FUNC(SetMapMode);
167     GET_FUNC(SetMapperFlags);
168     GET_FUNC(SetPixel);
169     GET_FUNC(SetPixelFormat);
170     GET_FUNC(SetPolyFillMode);
171     GET_FUNC(SetROP2);
172     GET_FUNC(SetRelAbs);
173     GET_FUNC(SetStretchBltMode);
174     GET_FUNC(SetTextAlign);
175     GET_FUNC(SetTextCharacterExtra);
176     GET_FUNC(SetTextColor);
177     GET_FUNC(SetTextJustification);
178     GET_FUNC(SetViewportExt);
179     GET_FUNC(SetViewportOrg);
180     GET_FUNC(SetWindowExt);
181     GET_FUNC(SetWindowOrg);
182     GET_FUNC(SetWorldTransform);
183     GET_FUNC(StartDoc);
184     GET_FUNC(StartPage);
185     GET_FUNC(StretchBlt);
186     GET_FUNC(StretchDIBits);
187     GET_FUNC(StrokeAndFillPath);
188     GET_FUNC(StrokePath);
189     GET_FUNC(SwapBuffers);
190     GET_FUNC(WidenPath);
191 #undef GET_FUNC
192
193     /* add it to the list */
194     driver->prev = NULL;
195     if ((driver->next = first_driver)) driver->next->prev = driver;
196     first_driver = driver;
197     return driver;
198 }
199
200
201 /**********************************************************************
202  *           load_display_driver
203  *
204  * Special case for loading the display driver: get the name from the config file
205  */
206 static struct graphics_driver *load_display_driver(void)
207 {
208     char buffer[MAX_PATH];
209     HMODULE module;
210     HKEY hkey;
211
212     if (display_driver)  /* already loaded */
213     {
214         display_driver->count++;
215         return display_driver;
216     }
217
218     strcpy( buffer, "x11drv" );  /* default value */
219     if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine", &hkey ))
220     {
221         DWORD type, count = sizeof(buffer);
222         RegQueryValueExA( hkey, "GraphicsDriver", 0, &type, buffer, &count );
223         RegCloseKey( hkey );
224     }
225
226     if (!(module = LoadLibraryA( buffer )))
227     {
228         MESSAGE( "Could not load graphics driver '%s'\n", buffer );
229         return NULL;
230     }
231
232     if (!(display_driver = create_driver( module )))
233     {
234         MESSAGE( "Could not create graphics driver '%s'\n", buffer );
235         FreeLibrary( module );
236         return NULL;
237     }
238
239     display_driver->count++;  /* we don't want to free it */
240     return display_driver;
241 }
242
243
244 /**********************************************************************
245  *           DRIVER_load_driver
246  */
247 const DC_FUNCTIONS *DRIVER_load_driver( LPCWSTR name )
248 {
249     HMODULE module;
250     struct graphics_driver *driver;
251     static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
252
253     EnterCriticalSection( &driver_section );
254
255     /* display driver is a special case */
256     if (!strcmpiW( name, displayW ))
257     {
258         driver = load_display_driver();
259         LeaveCriticalSection( &driver_section );
260         return &driver->funcs;
261     }
262
263     if ((module = GetModuleHandleW( name )))
264     {
265         for (driver = first_driver; driver; driver = driver->next)
266         {
267             if (driver->module == module)
268             {
269                 driver->count++;
270                 LeaveCriticalSection( &driver_section );
271                 return &driver->funcs;
272             }
273         }
274     }
275
276     if (!(module = LoadLibraryW( name )))
277     {
278         LeaveCriticalSection( &driver_section );
279         return NULL;
280     }
281
282     if (!(driver = create_driver( module )))
283     {
284         FreeLibrary( module );
285         LeaveCriticalSection( &driver_section );
286         return NULL;
287     }
288
289     TRACE( "loaded driver %p for %s\n", driver, debugstr_w(name) );
290     LeaveCriticalSection( &driver_section );
291     return &driver->funcs;
292 }
293
294
295 /**********************************************************************
296  *           DRIVER_get_driver
297  *
298  * Get a new copy of an existing driver.
299  */
300 const DC_FUNCTIONS *DRIVER_get_driver( const DC_FUNCTIONS *funcs )
301 {
302     struct graphics_driver *driver;
303
304     EnterCriticalSection( &driver_section );
305     for (driver = first_driver; driver; driver = driver->next)
306         if (&driver->funcs == funcs) break;
307     if (!driver) ERR( "driver not found, trouble ahead\n" );
308     driver->count++;
309     LeaveCriticalSection( &driver_section );
310     return funcs;
311 }
312
313
314 /**********************************************************************
315  *           DRIVER_release_driver
316  *
317  * Release a driver by decrementing ref count and freeing it if needed.
318  */
319 void DRIVER_release_driver( const DC_FUNCTIONS *funcs )
320 {
321     struct graphics_driver *driver;
322
323     EnterCriticalSection( &driver_section );
324
325     for (driver = first_driver; driver; driver = driver->next)
326         if (&driver->funcs == funcs) break;
327
328     if (!driver) goto done;
329     if (--driver->count) goto done;
330
331     /* removed last reference, free it */
332     if (driver->next) driver->next->prev = driver->prev;
333     if (driver->prev) driver->prev->next = driver->next;
334     else first_driver = driver->next;
335     if (driver == display_driver) display_driver = NULL;
336
337     FreeLibrary( driver->module );
338     HeapFree( GetProcessHeap(), 0, driver );
339  done:
340     LeaveCriticalSection( &driver_section );
341 }
342
343
344 /*****************************************************************************
345  *      DRIVER_GetDriverName
346  *
347  */
348 BOOL DRIVER_GetDriverName( LPCWSTR device, LPWSTR driver, DWORD size )
349 {
350     static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
351     static const WCHAR devicesW[] = { 'd','e','v','i','c','e','s',0 };
352     static const WCHAR empty_strW[] = { 0 };
353     WCHAR *p;
354
355     /* display is a special case */
356     if (!strcmpiW( device, displayW ))
357     {
358         lstrcpynW( driver, displayW, size );
359         return TRUE;
360     }
361
362     size = GetProfileStringW(devicesW, device, empty_strW, driver, size);
363     if(!size) {
364         WARN("Unable to find %s in [devices] section of win.ini\n", debugstr_w(device));
365         return FALSE;
366     }
367     p = strchrW(driver, ',');
368     if(!p)
369     {
370         WARN("%s entry in [devices] section of win.ini is malformed.\n", debugstr_w(device));
371         return FALSE;
372     }
373     *p = 0;
374     TRACE("Found %s for %s\n", debugstr_w(driver), debugstr_w(device));
375     return TRUE;
376 }
377
378
379 /***********************************************************************
380  *           GdiConvertToDevmodeW    (GDI32.@)
381  */
382 DEVMODEW * WINAPI GdiConvertToDevmodeW(const DEVMODEA *dmA)
383 {
384     DEVMODEW *dmW;
385     WORD dmW_size;
386
387     dmW_size = dmA->dmSize + CCHDEVICENAME;
388     if (dmA->dmSize >= (char *)dmA->dmFormName - (char *)dmA + CCHFORMNAME)
389         dmW_size += CCHFORMNAME;
390
391     dmW = HeapAlloc(GetProcessHeap(), 0, dmW_size + dmA->dmDriverExtra);
392     if (!dmW) return NULL;
393
394     MultiByteToWideChar(CP_ACP, 0, dmA->dmDeviceName, CCHDEVICENAME,
395                                    dmW->dmDeviceName, CCHDEVICENAME);
396     /* copy slightly more, to avoid long computations */
397     memcpy(&dmW->dmSpecVersion, &dmA->dmSpecVersion, dmA->dmSize - CCHDEVICENAME);
398
399     if (dmA->dmSize >= (char *)dmA->dmFormName - (char *)dmA + CCHFORMNAME)
400     {
401         MultiByteToWideChar(CP_ACP, 0, dmA->dmFormName, CCHFORMNAME,
402                                        dmW->dmFormName, CCHFORMNAME);
403         if (dmA->dmSize > (char *)&dmA->dmLogPixels - (char *)dmA)
404             memcpy(&dmW->dmLogPixels, &dmA->dmLogPixels, dmA->dmSize - ((char *)&dmA->dmLogPixels - (char *)dmA));
405     }
406
407     if (dmA->dmDriverExtra)
408         memcpy((char *)dmW + dmW_size, (char *)dmA + dmA->dmSize, dmA->dmDriverExtra);
409
410     dmW->dmSize = dmW_size;
411
412     return dmW;
413 }
414
415
416 /*****************************************************************************
417  *      @ [GDI32.100]
418  *
419  * This should thunk to 16-bit and simply call the proc with the given args.
420  */
421 INT WINAPI GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc, HWND hWnd,
422                                  LPSTR lpModelName, LPSTR OldPort, LPSTR NewPort )
423 {
424     FIXME("(%p, %p, %s, %s, %s)\n", lpfnDevInstallProc, hWnd, lpModelName, OldPort, NewPort );
425     return -1;
426 }
427
428 /*****************************************************************************
429  *      @ [GDI32.101]
430  *
431  * This should load the correct driver for lpszDevice and calls this driver's
432  * ExtDeviceModePropSheet proc.
433  *
434  * Note: The driver calls a callback routine for each property sheet page; these
435  * pages are supposed to be filled into the structure pointed to by lpPropSheet.
436  * The layout of this structure is:
437  *
438  * struct
439  * {
440  *   DWORD  nPages;
441  *   DWORD  unknown;
442  *   HPROPSHEETPAGE  pages[10];
443  * };
444  */
445 INT WINAPI GDI_CallExtDeviceModePropSheet16( HWND hWnd, LPCSTR lpszDevice,
446                                              LPCSTR lpszPort, LPVOID lpPropSheet )
447 {
448     FIXME("(%p, %s, %s, %p)\n", hWnd, lpszDevice, lpszPort, lpPropSheet );
449     return -1;
450 }
451
452 /*****************************************************************************
453  *      @ [GDI32.102]
454  *
455  * This should load the correct driver for lpszDevice and calls this driver's
456  * ExtDeviceMode proc.
457  *
458  * FIXME: convert ExtDeviceMode to unicode in the driver interface
459  */
460 INT WINAPI GDI_CallExtDeviceMode16( HWND hwnd,
461                                     LPDEVMODEA lpdmOutput, LPSTR lpszDevice,
462                                     LPSTR lpszPort, LPDEVMODEA lpdmInput,
463                                     LPSTR lpszProfile, DWORD fwMode )
464 {
465     WCHAR deviceW[300];
466     WCHAR bufW[300];
467     char buf[300];
468     HDC hdc;
469     DC *dc;
470     INT ret = -1;
471     INT (*pExtDeviceMode)(LPSTR,HWND,LPDEVMODEA,LPSTR,LPSTR,LPDEVMODEA,LPSTR,DWORD);
472
473     TRACE("(%p, %p, %s, %s, %p, %s, %ld)\n",
474           hwnd, lpdmOutput, lpszDevice, lpszPort, lpdmInput, lpszProfile, fwMode );
475
476     if (!lpszDevice) return -1;
477     if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
478
479     if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
480
481     if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
482
483     if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
484
485     if ((dc = DC_GetDCPtr( hdc )))
486     {
487         pExtDeviceMode = dc->funcs->pExtDeviceMode;
488         GDI_ReleaseObj( hdc );
489         if (pExtDeviceMode)
490             ret = pExtDeviceMode(buf, hwnd, lpdmOutput, lpszDevice, lpszPort,
491                                             lpdmInput, lpszProfile, fwMode);
492     }
493     DeleteDC( hdc );
494     return ret;
495 }
496
497 /****************************************************************************
498  *      @ [GDI32.103]
499  *
500  * This should load the correct driver for lpszDevice and calls this driver's
501  * AdvancedSetupDialog proc.
502  */
503 INT WINAPI GDI_CallAdvancedSetupDialog16( HWND hwnd, LPSTR lpszDevice,
504                                           LPDEVMODEA devin, LPDEVMODEA devout )
505 {
506     TRACE("(%p, %s, %p, %p)\n", hwnd, lpszDevice, devin, devout );
507     return -1;
508 }
509
510 /*****************************************************************************
511  *      @ [GDI32.104]
512  *
513  * This should load the correct driver for lpszDevice and calls this driver's
514  * DeviceCapabilities proc.
515  *
516  * FIXME: convert DeviceCapabilities to unicode in the driver interface
517  */
518 DWORD WINAPI GDI_CallDeviceCapabilities16( LPCSTR lpszDevice, LPCSTR lpszPort,
519                                            WORD fwCapability, LPSTR lpszOutput,
520                                            LPDEVMODEA lpdm )
521 {
522     WCHAR deviceW[300];
523     WCHAR bufW[300];
524     char buf[300];
525     HDC hdc;
526     DC *dc;
527     INT ret = -1;
528
529     TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice, lpszPort, fwCapability, lpszOutput, lpdm );
530
531     if (!lpszDevice) return -1;
532     if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
533
534     if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
535
536     if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
537
538     if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
539
540     if ((dc = DC_GetDCPtr( hdc )))
541     {
542         if (dc->funcs->pDeviceCapabilities)
543             ret = dc->funcs->pDeviceCapabilities( buf, lpszDevice, lpszPort,
544                                                   fwCapability, lpszOutput, lpdm );
545         GDI_ReleaseObj( hdc );
546     }
547     DeleteDC( hdc );
548     return ret;
549 }