Added GetDeviceCaps to the DC driver interface. Removed devCaps
[wine] / dlls / gdi / driver.c
1 /*
2  * Graphics driver management functions
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
7 #include <string.h>
8 #include "winbase.h"
9 #include "winreg.h"
10 #include "ntddk.h"
11
12 #include "gdi.h"
13 #include "debugtools.h"
14
15 DEFAULT_DEBUG_CHANNEL(driver);
16
17 struct graphics_driver
18 {
19     struct graphics_driver *next;
20     struct graphics_driver *prev;
21     HMODULE                 module;  /* module handle */
22     unsigned int            count;   /* reference count */
23     DC_FUNCTIONS            funcs;
24 };
25
26 static struct graphics_driver *first_driver;
27 static struct graphics_driver *display_driver;
28
29
30 /**********************************************************************
31  *           create_driver
32  *
33  * Allocate and fill the driver structure for a given module.
34  */
35 static struct graphics_driver *create_driver( HMODULE module )
36 {
37     struct graphics_driver *driver;
38
39     if (!(driver = HeapAlloc( GetProcessHeap(), 0, sizeof(*driver)))) return NULL;
40     driver->next   = NULL;
41     driver->prev   = NULL;
42     driver->module = module;
43     driver->count  = 1;
44
45     /* fill the function table */
46
47 #define GET_FUNC(name) driver->funcs.p##name = (void*)GetProcAddress( module, #name )
48
49     GET_FUNC(AbortDoc);
50     GET_FUNC(AbortPath);
51     GET_FUNC(AngleArc);
52     GET_FUNC(Arc);
53     GET_FUNC(ArcTo);
54     GET_FUNC(BeginPath);
55     GET_FUNC(BitBlt);
56     GET_FUNC(BitmapBits);
57     GET_FUNC(ChoosePixelFormat);
58     GET_FUNC(Chord);
59     GET_FUNC(CloseFigure);
60     GET_FUNC(CreateBitmap);
61     GET_FUNC(CreateDC);
62     GET_FUNC(CreateDIBSection);
63     GET_FUNC(DeleteDC);
64     GET_FUNC(DeleteObject);
65     GET_FUNC(DescribePixelFormat);
66     GET_FUNC(DeviceCapabilities);
67     GET_FUNC(Ellipse);
68     GET_FUNC(EndDoc);
69     GET_FUNC(EndPage);
70     GET_FUNC(EndPath);
71     GET_FUNC(EnumDeviceFonts);
72     GET_FUNC(Escape);
73     GET_FUNC(ExcludeClipRect);
74     GET_FUNC(ExtDeviceMode);
75     GET_FUNC(ExtFloodFill);
76     GET_FUNC(ExtTextOut);
77     GET_FUNC(FillPath);
78     GET_FUNC(FillRgn);
79     GET_FUNC(FlattenPath);
80     GET_FUNC(FrameRgn);
81     GET_FUNC(GetCharWidth);
82     GET_FUNC(GetDCOrgEx);
83     GET_FUNC(GetDeviceCaps);
84     GET_FUNC(GetDeviceGammaRamp);
85     GET_FUNC(GetPixel);
86     GET_FUNC(GetPixelFormat);
87     GET_FUNC(GetTextExtentPoint);
88     GET_FUNC(GetTextMetrics);
89     GET_FUNC(IntersectClipRect);
90     GET_FUNC(InvertRgn);
91     GET_FUNC(LineTo);
92     GET_FUNC(MoveTo);
93     GET_FUNC(OffsetClipRgn);
94     GET_FUNC(OffsetViewportOrg);
95     GET_FUNC(OffsetWindowOrg);
96     GET_FUNC(PaintRgn);
97     GET_FUNC(PatBlt);
98     GET_FUNC(Pie);
99     GET_FUNC(PolyBezier);
100     GET_FUNC(PolyBezierTo);
101     GET_FUNC(PolyDraw);
102     GET_FUNC(PolyPolygon);
103     GET_FUNC(PolyPolyline);
104     GET_FUNC(Polygon);
105     GET_FUNC(Polyline);
106     GET_FUNC(PolylineTo);
107     GET_FUNC(RealizePalette);
108     GET_FUNC(Rectangle);
109     GET_FUNC(RestoreDC);
110     GET_FUNC(RoundRect);
111     GET_FUNC(SaveDC);
112     GET_FUNC(ScaleViewportExt);
113     GET_FUNC(ScaleWindowExt);
114     GET_FUNC(SelectClipPath);
115     GET_FUNC(SelectClipRgn);
116     GET_FUNC(SelectObject);
117     GET_FUNC(SelectPalette);
118     GET_FUNC(SetBkColor);
119     GET_FUNC(SetBkMode);
120     GET_FUNC(SetDIBitsToDevice);
121     GET_FUNC(SetDeviceClipping);
122     GET_FUNC(SetDeviceGammaRamp);
123     GET_FUNC(SetMapMode);
124     GET_FUNC(SetMapperFlags);
125     GET_FUNC(SetPixel);
126     GET_FUNC(SetPixelFormat);
127     GET_FUNC(SetPolyFillMode);
128     GET_FUNC(SetROP2);
129     GET_FUNC(SetRelAbs);
130     GET_FUNC(SetStretchBltMode);
131     GET_FUNC(SetTextAlign);
132     GET_FUNC(SetTextCharacterExtra);
133     GET_FUNC(SetTextColor);
134     GET_FUNC(SetTextJustification);
135     GET_FUNC(SetViewportExt);
136     GET_FUNC(SetViewportOrg);
137     GET_FUNC(SetWindowExt);
138     GET_FUNC(SetWindowOrg);
139     GET_FUNC(StartDoc);
140     GET_FUNC(StartPage);
141     GET_FUNC(StretchBlt);
142     GET_FUNC(StretchDIBits);
143     GET_FUNC(StrokeAndFillPath);
144     GET_FUNC(StrokePath);
145     GET_FUNC(SwapBuffers);
146     GET_FUNC(WidenPath);
147 #undef GET_FUNC
148
149     /* add it to the list */
150     driver->prev = NULL;
151     if ((driver->next = first_driver)) driver->next->prev = driver;
152     first_driver = driver;
153     return driver;
154 }
155
156
157 /**********************************************************************
158  *           load_display_driver
159  *
160  * Special case for loading the display driver: get the name from the config file
161  */
162 static struct graphics_driver *load_display_driver(void)
163 {
164     char buffer[MAX_PATH];
165     HMODULE module;
166     HKEY hkey;
167
168     if (display_driver)  /* already loaded */
169     {
170         display_driver->count++;
171         return display_driver;
172     }
173
174     strcpy( buffer, "x11drv" );  /* default value */
175     if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine", &hkey ))
176     {
177         DWORD type, count = sizeof(buffer);
178         RegQueryValueExA( hkey, "GraphicsDriver", 0, &type, buffer, &count );
179         RegCloseKey( hkey );
180     }
181
182     if (!(module = LoadLibraryA( buffer )))
183     {
184         MESSAGE( "Could not load graphics driver '%s'\n", buffer );
185         return NULL;
186     }
187
188     if (!(display_driver = create_driver( module )))
189     {
190         MESSAGE( "Could not create graphics driver '%s'\n", buffer );
191         FreeLibrary( module );
192         return NULL;
193     }
194
195     display_driver->count++;  /* we don't want to free it */
196     return display_driver;
197 }
198
199
200 /**********************************************************************
201  *           DRIVER_load_driver
202  */
203 const DC_FUNCTIONS *DRIVER_load_driver( LPCSTR name )
204 {
205     HMODULE module;
206     struct graphics_driver *driver;
207
208     RtlAcquirePebLock();
209
210     /* display driver is a special case */
211     if (!strcasecmp( name, "display" ))
212     {
213         driver = load_display_driver();
214         RtlReleasePebLock();
215         return &driver->funcs;
216     }
217
218     if ((module = GetModuleHandleA( name )))
219     {
220         for (driver = first_driver; driver; driver = driver->next)
221         {
222             if (driver->module == module)
223             {
224                 driver->count++;
225                 RtlReleasePebLock();
226                 return &driver->funcs;
227             }
228         }
229     }
230
231     if (!(module = LoadLibraryA( name )))
232     {
233         RtlReleasePebLock();
234         return NULL;
235     }
236
237     if (!(driver = create_driver( module )))
238     {
239         FreeLibrary( module );
240         RtlReleasePebLock();
241         return NULL;
242     }
243
244     TRACE( "loaded driver %p for %s\n", driver, name );
245     RtlReleasePebLock();
246     return &driver->funcs;
247 }
248
249
250 /**********************************************************************
251  *           DRIVER_get_driver
252  *
253  * Get a new copy of an existing driver.
254  */
255 const DC_FUNCTIONS *DRIVER_get_driver( const DC_FUNCTIONS *funcs )
256 {
257     struct graphics_driver *driver;
258
259     RtlAcquirePebLock();
260     for (driver = first_driver; driver; driver = driver->next)
261         if (&driver->funcs == funcs) break;
262     if (!driver) ERR( "driver not found, trouble ahead\n" );
263     driver->count++;
264     RtlReleasePebLock();
265     return funcs;
266 }
267
268
269 /**********************************************************************
270  *           DRIVER_release_driver
271  *
272  * Release a driver by decrementing ref count and freeing it if needed.
273  */
274 void DRIVER_release_driver( const DC_FUNCTIONS *funcs )
275 {
276     struct graphics_driver *driver;
277
278     RtlAcquirePebLock();
279
280     for (driver = first_driver; driver; driver = driver->next)
281         if (&driver->funcs == funcs) break;
282
283     if (!driver) goto done;
284     if (--driver->count) goto done;
285
286     /* removed last reference, free it */
287     if (driver->next) driver->next->prev = driver->prev;
288     if (driver->prev) driver->prev->next = driver->next;
289     else first_driver = driver->next;
290     if (driver == display_driver) display_driver = NULL;
291
292     FreeLibrary( driver->module );
293     HeapFree( GetProcessHeap(), 0, driver );
294  done:
295     RtlReleasePebLock();
296 }
297
298
299 /*****************************************************************************
300  *      DRIVER_GetDriverName
301  *
302  */
303 BOOL DRIVER_GetDriverName( LPCSTR device, LPSTR driver, DWORD size )
304 {
305     char *p;
306     size = GetProfileStringA("devices", device, "", driver, size);
307     if(!size) {
308         WARN("Unable to find '%s' in [devices] section of win.ini\n", device);
309         return FALSE;
310     }
311     p = strchr(driver, ',');
312     if(!p)
313     {
314         WARN("'%s' entry in [devices] section of win.ini is malformed.\n", device);
315         return FALSE;
316     }
317     *p = '\0';
318     TRACE("Found '%s' for '%s'\n", driver, device);
319     return TRUE;
320 }
321
322 /*****************************************************************************
323  *      @ [GDI32.100]
324  *
325  * This should thunk to 16-bit and simply call the proc with the given args.
326  */
327 INT WINAPI GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc, HWND hWnd,
328                                  LPSTR lpModelName, LPSTR OldPort, LPSTR NewPort )
329 {
330     FIXME("(%p, %04x, %s, %s, %s)\n", lpfnDevInstallProc, hWnd, lpModelName, OldPort, NewPort );
331     return -1;
332 }
333
334 /*****************************************************************************
335  *      @ [GDI32.101]
336  *
337  * This should load the correct driver for lpszDevice and calls this driver's
338  * ExtDeviceModePropSheet proc. 
339  *
340  * Note: The driver calls a callback routine for each property sheet page; these 
341  * pages are supposed to be filled into the structure pointed to by lpPropSheet.
342  * The layout of this structure is:
343  * 
344  * struct
345  * {
346  *   DWORD  nPages;
347  *   DWORD  unknown;
348  *   HPROPSHEETPAGE  pages[10];
349  * };
350  */
351 INT WINAPI GDI_CallExtDeviceModePropSheet16( HWND hWnd, LPCSTR lpszDevice,
352                                              LPCSTR lpszPort, LPVOID lpPropSheet )
353 {
354     FIXME("(%04x, %s, %s, %p)\n", hWnd, lpszDevice, lpszPort, lpPropSheet );
355     return -1;
356 }
357
358 /*****************************************************************************
359  *      @ [GDI32.102]
360  *
361  * This should load the correct driver for lpszDevice and calls this driver's
362  * ExtDeviceMode proc.
363  */
364 INT WINAPI GDI_CallExtDeviceMode16( HWND hwnd,
365                                     LPDEVMODEA lpdmOutput, LPSTR lpszDevice,
366                                     LPSTR lpszPort, LPDEVMODEA lpdmInput,
367                                     LPSTR lpszProfile, DWORD fwMode )
368 {
369     char buf[300];
370     HDC hdc;
371     DC *dc;
372     INT ret = -1;
373
374     TRACE("(%04x, %p, %s, %s, %p, %s, %ld)\n",
375           hwnd, lpdmOutput, lpszDevice, lpszPort, lpdmInput, lpszProfile, fwMode );
376
377     if(!DRIVER_GetDriverName( lpszDevice, buf, sizeof(buf) )) return -1;
378
379     if (!(hdc = CreateICA( buf, NULL, lpszPort, NULL ))) return -1;
380
381     if ((dc = DC_GetDCPtr( hdc )))
382     {
383         if (dc->funcs->pExtDeviceMode)
384             ret = dc->funcs->pExtDeviceMode(buf, hwnd, lpdmOutput, lpszDevice, lpszPort,
385                                             lpdmInput, lpszProfile, fwMode);
386         GDI_ReleaseObj( hdc );
387     }
388     DeleteDC( hdc );
389     return ret;
390 }
391
392 /****************************************************************************
393  *      @ [GDI32.103]
394  *
395  * This should load the correct driver for lpszDevice and calls this driver's
396  * AdvancedSetupDialog proc.
397  */
398 INT WINAPI GDI_CallAdvancedSetupDialog16( HWND hwnd, LPSTR lpszDevice,
399                                           LPDEVMODEA devin, LPDEVMODEA devout )
400 {
401     TRACE("(%04x, %s, %p, %p)\n", hwnd, lpszDevice, devin, devout );
402     return -1;
403 }
404
405 /*****************************************************************************
406  *      @ [GDI32.104]
407  *
408  * This should load the correct driver for lpszDevice and calls this driver's
409  * DeviceCapabilities proc.
410  */
411 DWORD WINAPI GDI_CallDeviceCapabilities16( LPCSTR lpszDevice, LPCSTR lpszPort,
412                                            WORD fwCapability, LPSTR lpszOutput,
413                                            LPDEVMODEA lpdm )
414 {
415     char buf[300];
416     HDC hdc;
417     DC *dc;
418     INT ret = -1;
419
420     TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice, lpszPort, fwCapability, lpszOutput, lpdm );
421
422     if(!DRIVER_GetDriverName( lpszDevice, buf, sizeof(buf) )) return -1;
423
424     if (!(hdc = CreateICA( buf, NULL, lpszPort, NULL ))) return -1;
425
426     if ((dc = DC_GetDCPtr( hdc )))
427     {
428         if (dc->funcs->pDeviceCapabilities)
429             ret = dc->funcs->pDeviceCapabilities( buf, lpszDevice, lpszPort,
430                                                   fwCapability, lpszOutput, lpdm );
431         GDI_ReleaseObj( hdc );
432     }
433     DeleteDC( hdc );
434     return ret;
435 }