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