Preliminary implementation of ICreateTypeInfo2_fnAddFuncDesc() and
[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 "gdi_private.h"
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(driver);
36
37 struct graphics_driver
38 {
39     struct graphics_driver *next;
40     struct graphics_driver *prev;
41     HMODULE                 module;  /* module handle */
42     unsigned int            count;   /* reference count */
43     DC_FUNCTIONS            funcs;
44 };
45
46 static struct graphics_driver *first_driver;
47 static struct graphics_driver *display_driver;
48
49 static CRITICAL_SECTION driver_section;
50 static CRITICAL_SECTION_DEBUG critsect_debug =
51 {
52     0, 0, &driver_section,
53     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
54       0, 0, { 0, (DWORD)(__FILE__ ": driver_section") }
55 };
56 static CRITICAL_SECTION driver_section = { &critsect_debug, -1, 0, 0, 0, 0 };
57
58 /**********************************************************************
59  *           create_driver
60  *
61  * Allocate and fill the driver structure for a given module.
62  */
63 static struct graphics_driver *create_driver( HMODULE module )
64 {
65     struct graphics_driver *driver;
66
67     if (!(driver = HeapAlloc( GetProcessHeap(), 0, sizeof(*driver)))) return NULL;
68     driver->next   = NULL;
69     driver->prev   = NULL;
70     driver->module = module;
71     driver->count  = 1;
72
73     /* fill the function table */
74
75 #define GET_FUNC(name) driver->funcs.p##name = (void*)GetProcAddress( module, #name )
76
77     GET_FUNC(AbortDoc);
78     GET_FUNC(AbortPath);
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(ExcludeClipRect);
100     GET_FUNC(ExtDeviceMode);
101     GET_FUNC(ExtEscape);
102     GET_FUNC(ExtFloodFill);
103     GET_FUNC(ExtSelectClipRgn);
104     GET_FUNC(ExtTextOut);
105     GET_FUNC(FillPath);
106     GET_FUNC(FillRgn);
107     GET_FUNC(FlattenPath);
108     GET_FUNC(FrameRgn);
109     GET_FUNC(GdiComment);
110     GET_FUNC(GetBitmapBits);
111     GET_FUNC(GetCharWidth);
112     GET_FUNC(GetDCOrgEx);
113     GET_FUNC(GetDIBColorTable);
114     GET_FUNC(GetDIBits);
115     GET_FUNC(GetDeviceCaps);
116     GET_FUNC(GetDeviceGammaRamp);
117     GET_FUNC(GetNearestColor);
118     GET_FUNC(GetPixel);
119     GET_FUNC(GetPixelFormat);
120     GET_FUNC(GetSystemPaletteEntries);
121     GET_FUNC(GetTextExtentPoint);
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(SetDCOrg);
163     GET_FUNC(SetDCPenColor);
164     GET_FUNC(SetDIBColorTable);
165     GET_FUNC(SetDIBits);
166     GET_FUNC(SetDIBitsToDevice);
167     GET_FUNC(SetDeviceClipping);
168     GET_FUNC(SetDeviceGammaRamp);
169     GET_FUNC(SetMapMode);
170     GET_FUNC(SetMapperFlags);
171     GET_FUNC(SetPixel);
172     GET_FUNC(SetPixelFormat);
173     GET_FUNC(SetPolyFillMode);
174     GET_FUNC(SetROP2);
175     GET_FUNC(SetRelAbs);
176     GET_FUNC(SetStretchBltMode);
177     GET_FUNC(SetTextAlign);
178     GET_FUNC(SetTextCharacterExtra);
179     GET_FUNC(SetTextColor);
180     GET_FUNC(SetTextJustification);
181     GET_FUNC(SetViewportExt);
182     GET_FUNC(SetViewportOrg);
183     GET_FUNC(SetWindowExt);
184     GET_FUNC(SetWindowOrg);
185     GET_FUNC(SetWorldTransform);
186     GET_FUNC(StartDoc);
187     GET_FUNC(StartPage);
188     GET_FUNC(StretchBlt);
189     GET_FUNC(StretchDIBits);
190     GET_FUNC(StrokeAndFillPath);
191     GET_FUNC(StrokePath);
192     GET_FUNC(SwapBuffers);
193     GET_FUNC(WidenPath);
194 #undef GET_FUNC
195
196     /* add it to the list */
197     driver->prev = NULL;
198     if ((driver->next = first_driver)) driver->next->prev = driver;
199     first_driver = driver;
200     return driver;
201 }
202
203
204 /**********************************************************************
205  *           load_display_driver
206  *
207  * Special case for loading the display driver: get the name from the config file
208  */
209 static struct graphics_driver *load_display_driver(void)
210 {
211     char buffer[MAX_PATH];
212     HMODULE module;
213     HKEY hkey;
214
215     if (display_driver)  /* already loaded */
216     {
217         display_driver->count++;
218         return display_driver;
219     }
220
221     strcpy( buffer, "x11drv" );  /* default value */
222     if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine", &hkey ))
223     {
224         DWORD type, count = sizeof(buffer);
225         RegQueryValueExA( hkey, "GraphicsDriver", 0, &type, buffer, &count );
226         RegCloseKey( hkey );
227     }
228
229     if (!(module = LoadLibraryA( buffer )))
230     {
231         MESSAGE( "Could not load graphics driver '%s'\n", buffer );
232         return NULL;
233     }
234
235     if (!(display_driver = create_driver( module )))
236     {
237         MESSAGE( "Could not create graphics driver '%s'\n", buffer );
238         FreeLibrary( module );
239         return NULL;
240     }
241
242     display_driver->count++;  /* we don't want to free it */
243     return display_driver;
244 }
245
246
247 /**********************************************************************
248  *           DRIVER_load_driver
249  */
250 const DC_FUNCTIONS *DRIVER_load_driver( LPCWSTR name )
251 {
252     HMODULE module;
253     struct graphics_driver *driver;
254     static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
255
256     EnterCriticalSection( &driver_section );
257
258     /* display driver is a special case */
259     if (!strcmpiW( name, displayW ))
260     {
261         driver = load_display_driver();
262         LeaveCriticalSection( &driver_section );
263         return &driver->funcs;
264     }
265
266     if ((module = GetModuleHandleW( name )))
267     {
268         for (driver = first_driver; driver; driver = driver->next)
269         {
270             if (driver->module == module)
271             {
272                 driver->count++;
273                 LeaveCriticalSection( &driver_section );
274                 return &driver->funcs;
275             }
276         }
277     }
278
279     if (!(module = LoadLibraryW( name )))
280     {
281         LeaveCriticalSection( &driver_section );
282         return NULL;
283     }
284
285     if (!(driver = create_driver( module )))
286     {
287         FreeLibrary( module );
288         LeaveCriticalSection( &driver_section );
289         return NULL;
290     }
291
292     TRACE( "loaded driver %p for %s\n", driver, debugstr_w(name) );
293     LeaveCriticalSection( &driver_section );
294     return &driver->funcs;
295 }
296
297
298 /**********************************************************************
299  *           DRIVER_get_driver
300  *
301  * Get a new copy of an existing driver.
302  */
303 const DC_FUNCTIONS *DRIVER_get_driver( const DC_FUNCTIONS *funcs )
304 {
305     struct graphics_driver *driver;
306
307     EnterCriticalSection( &driver_section );
308     for (driver = first_driver; driver; driver = driver->next)
309         if (&driver->funcs == funcs) break;
310     if (!driver) ERR( "driver not found, trouble ahead\n" );
311     driver->count++;
312     LeaveCriticalSection( &driver_section );
313     return funcs;
314 }
315
316
317 /**********************************************************************
318  *           DRIVER_release_driver
319  *
320  * Release a driver by decrementing ref count and freeing it if needed.
321  */
322 void DRIVER_release_driver( const DC_FUNCTIONS *funcs )
323 {
324     struct graphics_driver *driver;
325
326     EnterCriticalSection( &driver_section );
327
328     for (driver = first_driver; driver; driver = driver->next)
329         if (&driver->funcs == funcs) break;
330
331     if (!driver) goto done;
332     if (--driver->count) goto done;
333
334     /* removed last reference, free it */
335     if (driver->next) driver->next->prev = driver->prev;
336     if (driver->prev) driver->prev->next = driver->next;
337     else first_driver = driver->next;
338     if (driver == display_driver) display_driver = NULL;
339
340     FreeLibrary( driver->module );
341     HeapFree( GetProcessHeap(), 0, driver );
342  done:
343     LeaveCriticalSection( &driver_section );
344 }
345
346
347 /*****************************************************************************
348  *      DRIVER_GetDriverName
349  *
350  */
351 BOOL DRIVER_GetDriverName( LPCWSTR device, LPWSTR driver, DWORD size )
352 {
353     static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
354     static const WCHAR devicesW[] = { 'd','e','v','i','c','e','s',0 };
355     static const WCHAR empty_strW[] = { 0 };
356     WCHAR *p;
357
358     /* display is a special case */
359     if (!strcmpiW( device, displayW ))
360     {
361         lstrcpynW( driver, displayW, size );
362         return TRUE;
363     }
364
365     size = GetProfileStringW(devicesW, device, empty_strW, driver, size);
366     if(!size) {
367         WARN("Unable to find %s in [devices] section of win.ini\n", debugstr_w(device));
368         return FALSE;
369     }
370     p = strchrW(driver, ',');
371     if(!p)
372     {
373         WARN("%s entry in [devices] section of win.ini is malformed.\n", debugstr_w(device));
374         return FALSE;
375     }
376     *p = 0;
377     TRACE("Found %s for %s\n", debugstr_w(driver), debugstr_w(device));
378     return TRUE;
379 }
380
381
382 /***********************************************************************
383  *           GdiConvertToDevmodeW    (GDI32.@)
384  */
385 DEVMODEW * WINAPI GdiConvertToDevmodeW(const DEVMODEA *dmA)
386 {
387     DEVMODEW *dmW;
388     WORD dmW_size;
389
390     dmW_size = dmA->dmSize + CCHDEVICENAME;
391     if (dmA->dmSize >= (char *)dmA->dmFormName - (char *)dmA + CCHFORMNAME)
392         dmW_size += CCHFORMNAME;
393
394     dmW = HeapAlloc(GetProcessHeap(), 0, dmW_size + dmA->dmDriverExtra);
395     if (!dmW) return NULL;
396
397     MultiByteToWideChar(CP_ACP, 0, dmA->dmDeviceName, CCHDEVICENAME,
398                                    dmW->dmDeviceName, CCHDEVICENAME);
399     /* copy slightly more, to avoid long computations */
400     memcpy(&dmW->dmSpecVersion, &dmA->dmSpecVersion, dmA->dmSize - CCHDEVICENAME);
401
402     if (dmA->dmSize >= (char *)dmA->dmFormName - (char *)dmA + CCHFORMNAME)
403     {
404         MultiByteToWideChar(CP_ACP, 0, dmA->dmFormName, CCHFORMNAME,
405                                        dmW->dmFormName, CCHFORMNAME);
406         if (dmA->dmSize > (char *)&dmA->dmLogPixels - (char *)dmA)
407             memcpy(&dmW->dmLogPixels, &dmA->dmLogPixels, dmA->dmSize - ((char *)&dmA->dmLogPixels - (char *)dmA));
408     }
409
410     if (dmA->dmDriverExtra)
411         memcpy((char *)dmW + dmW_size, (char *)dmA + dmA->dmSize, dmA->dmDriverExtra);
412
413     dmW->dmSize = dmW_size;
414
415     return dmW;
416 }
417
418
419 /*****************************************************************************
420  *      @ [GDI32.100]
421  *
422  * This should thunk to 16-bit and simply call the proc with the given args.
423  */
424 INT WINAPI GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc, HWND hWnd,
425                                  LPSTR lpModelName, LPSTR OldPort, LPSTR NewPort )
426 {
427     FIXME("(%p, %p, %s, %s, %s)\n", lpfnDevInstallProc, hWnd, lpModelName, OldPort, NewPort );
428     return -1;
429 }
430
431 /*****************************************************************************
432  *      @ [GDI32.101]
433  *
434  * This should load the correct driver for lpszDevice and calls this driver's
435  * ExtDeviceModePropSheet proc.
436  *
437  * Note: The driver calls a callback routine for each property sheet page; these
438  * pages are supposed to be filled into the structure pointed to by lpPropSheet.
439  * The layout of this structure is:
440  *
441  * struct
442  * {
443  *   DWORD  nPages;
444  *   DWORD  unknown;
445  *   HPROPSHEETPAGE  pages[10];
446  * };
447  */
448 INT WINAPI GDI_CallExtDeviceModePropSheet16( HWND hWnd, LPCSTR lpszDevice,
449                                              LPCSTR lpszPort, LPVOID lpPropSheet )
450 {
451     FIXME("(%p, %s, %s, %p)\n", hWnd, lpszDevice, lpszPort, lpPropSheet );
452     return -1;
453 }
454
455 /*****************************************************************************
456  *      @ [GDI32.102]
457  *
458  * This should load the correct driver for lpszDevice and calls this driver's
459  * ExtDeviceMode proc.
460  *
461  * FIXME: convert ExtDeviceMode to unicode in the driver interface
462  */
463 INT WINAPI GDI_CallExtDeviceMode16( HWND hwnd,
464                                     LPDEVMODEA lpdmOutput, LPSTR lpszDevice,
465                                     LPSTR lpszPort, LPDEVMODEA lpdmInput,
466                                     LPSTR lpszProfile, DWORD fwMode )
467 {
468     WCHAR deviceW[300];
469     WCHAR bufW[300];
470     char buf[300];
471     HDC hdc;
472     DC *dc;
473     INT ret = -1;
474     INT (*pExtDeviceMode)(LPSTR,HWND,LPDEVMODEA,LPSTR,LPSTR,LPDEVMODEA,LPSTR,DWORD);
475
476     TRACE("(%p, %p, %s, %s, %p, %s, %ld)\n",
477           hwnd, lpdmOutput, lpszDevice, lpszPort, lpdmInput, lpszProfile, fwMode );
478
479     if (!lpszDevice) return -1;
480     if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
481
482     if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
483
484     if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
485
486     if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
487
488     if ((dc = DC_GetDCPtr( hdc )))
489     {
490         pExtDeviceMode = dc->funcs->pExtDeviceMode;
491         GDI_ReleaseObj( hdc );
492         if (pExtDeviceMode)
493             ret = pExtDeviceMode(buf, hwnd, lpdmOutput, lpszDevice, lpszPort,
494                                             lpdmInput, lpszProfile, fwMode);
495     }
496     DeleteDC( hdc );
497     return ret;
498 }
499
500 /****************************************************************************
501  *      @ [GDI32.103]
502  *
503  * This should load the correct driver for lpszDevice and calls this driver's
504  * AdvancedSetupDialog proc.
505  */
506 INT WINAPI GDI_CallAdvancedSetupDialog16( HWND hwnd, LPSTR lpszDevice,
507                                           LPDEVMODEA devin, LPDEVMODEA devout )
508 {
509     TRACE("(%p, %s, %p, %p)\n", hwnd, lpszDevice, devin, devout );
510     return -1;
511 }
512
513 /*****************************************************************************
514  *      @ [GDI32.104]
515  *
516  * This should load the correct driver for lpszDevice and calls this driver's
517  * DeviceCapabilities proc.
518  *
519  * FIXME: convert DeviceCapabilities to unicode in the driver interface
520  */
521 DWORD WINAPI GDI_CallDeviceCapabilities16( LPCSTR lpszDevice, LPCSTR lpszPort,
522                                            WORD fwCapability, LPSTR lpszOutput,
523                                            LPDEVMODEA lpdm )
524 {
525     WCHAR deviceW[300];
526     WCHAR bufW[300];
527     char buf[300];
528     HDC hdc;
529     DC *dc;
530     INT ret = -1;
531
532     TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice, lpszPort, fwCapability, lpszOutput, lpdm );
533
534     if (!lpszDevice) return -1;
535     if (!MultiByteToWideChar(CP_ACP, 0, lpszDevice, -1, deviceW, 300)) return -1;
536
537     if(!DRIVER_GetDriverName( deviceW, bufW, 300 )) return -1;
538
539     if (!WideCharToMultiByte(CP_ACP, 0, bufW, -1, buf, 300, NULL, NULL)) return -1;
540
541     if (!(hdc = CreateICA( buf, lpszDevice, lpszPort, NULL ))) return -1;
542
543     if ((dc = DC_GetDCPtr( hdc )))
544     {
545         if (dc->funcs->pDeviceCapabilities)
546             ret = dc->funcs->pDeviceCapabilities( buf, lpszDevice, lpszPort,
547                                                   fwCapability, lpszOutput, lpdm );
548         GDI_ReleaseObj( hdc );
549     }
550     DeleteDC( hdc );
551     return ret;
552 }