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