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