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