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