Rewrote Escape to avoid calling down to Escape16.
[wine/wine-kai.git] / dlls / gdi / driver.c
blob7819a4186f88432fcb5e8c7d3d5949d8e6ca11d7
1 /*
2 * Graphics driver management functions
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <string.h>
8 #include "winbase.h"
9 #include "winreg.h"
10 #include "ntddk.h"
12 #include "gdi.h"
13 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(driver);
17 struct graphics_driver
19 struct graphics_driver *next;
20 struct graphics_driver *prev;
21 HMODULE module; /* module handle */
22 unsigned int count; /* reference count */
23 DC_FUNCTIONS funcs;
26 static struct graphics_driver *first_driver;
27 static struct graphics_driver *display_driver;
30 /**********************************************************************
31 * create_driver
33 * Allocate and fill the driver structure for a given module.
35 static struct graphics_driver *create_driver( HMODULE module )
37 struct graphics_driver *driver;
39 if (!(driver = HeapAlloc( GetProcessHeap(), 0, sizeof(*driver)))) return NULL;
40 driver->next = NULL;
41 driver->prev = NULL;
42 driver->module = module;
43 driver->count = 1;
45 /* fill the function table */
47 #define GET_FUNC(name) driver->funcs.p##name = (void*)GetProcAddress( module, #name )
49 GET_FUNC(AbortDoc);
50 GET_FUNC(AbortPath);
51 GET_FUNC(AngleArc);
52 GET_FUNC(Arc);
53 GET_FUNC(ArcTo);
54 GET_FUNC(BeginPath);
55 GET_FUNC(BitBlt);
56 GET_FUNC(BitmapBits);
57 GET_FUNC(ChoosePixelFormat);
58 GET_FUNC(Chord);
59 GET_FUNC(CloseFigure);
60 GET_FUNC(CreateBitmap);
61 GET_FUNC(CreateDC);
62 GET_FUNC(CreateDIBSection);
63 GET_FUNC(DeleteDC);
64 GET_FUNC(DeleteObject);
65 GET_FUNC(DescribePixelFormat);
66 GET_FUNC(DeviceCapabilities);
67 GET_FUNC(Ellipse);
68 GET_FUNC(EndDoc);
69 GET_FUNC(EndPage);
70 GET_FUNC(EndPath);
71 GET_FUNC(EnumDeviceFonts);
72 GET_FUNC(ExcludeClipRect);
73 GET_FUNC(ExtDeviceMode);
74 GET_FUNC(ExtEscape);
75 GET_FUNC(ExtFloodFill);
76 GET_FUNC(ExtTextOut);
77 GET_FUNC(FillPath);
78 GET_FUNC(FillRgn);
79 GET_FUNC(FlattenPath);
80 GET_FUNC(FrameRgn);
81 GET_FUNC(GetCharWidth);
82 GET_FUNC(GetDCOrgEx);
83 GET_FUNC(GetDeviceCaps);
84 GET_FUNC(GetDeviceGammaRamp);
85 GET_FUNC(GetPixel);
86 GET_FUNC(GetPixelFormat);
87 GET_FUNC(GetTextExtentPoint);
88 GET_FUNC(GetTextMetrics);
89 GET_FUNC(IntersectClipRect);
90 GET_FUNC(InvertRgn);
91 GET_FUNC(LineTo);
92 GET_FUNC(MoveTo);
93 GET_FUNC(OffsetClipRgn);
94 GET_FUNC(OffsetViewportOrg);
95 GET_FUNC(OffsetWindowOrg);
96 GET_FUNC(PaintRgn);
97 GET_FUNC(PatBlt);
98 GET_FUNC(Pie);
99 GET_FUNC(PolyBezier);
100 GET_FUNC(PolyBezierTo);
101 GET_FUNC(PolyDraw);
102 GET_FUNC(PolyPolygon);
103 GET_FUNC(PolyPolyline);
104 GET_FUNC(Polygon);
105 GET_FUNC(Polyline);
106 GET_FUNC(PolylineTo);
107 GET_FUNC(RealizePalette);
108 GET_FUNC(Rectangle);
109 GET_FUNC(RestoreDC);
110 GET_FUNC(RoundRect);
111 GET_FUNC(SaveDC);
112 GET_FUNC(ScaleViewportExt);
113 GET_FUNC(ScaleWindowExt);
114 GET_FUNC(SelectClipPath);
115 GET_FUNC(SelectClipRgn);
116 GET_FUNC(SelectObject);
117 GET_FUNC(SelectPalette);
118 GET_FUNC(SetBkColor);
119 GET_FUNC(SetBkMode);
120 GET_FUNC(SetDIBitsToDevice);
121 GET_FUNC(SetDeviceClipping);
122 GET_FUNC(SetDeviceGammaRamp);
123 GET_FUNC(SetMapMode);
124 GET_FUNC(SetMapperFlags);
125 GET_FUNC(SetPixel);
126 GET_FUNC(SetPixelFormat);
127 GET_FUNC(SetPolyFillMode);
128 GET_FUNC(SetROP2);
129 GET_FUNC(SetRelAbs);
130 GET_FUNC(SetStretchBltMode);
131 GET_FUNC(SetTextAlign);
132 GET_FUNC(SetTextCharacterExtra);
133 GET_FUNC(SetTextColor);
134 GET_FUNC(SetTextJustification);
135 GET_FUNC(SetViewportExt);
136 GET_FUNC(SetViewportOrg);
137 GET_FUNC(SetWindowExt);
138 GET_FUNC(SetWindowOrg);
139 GET_FUNC(StartDoc);
140 GET_FUNC(StartPage);
141 GET_FUNC(StretchBlt);
142 GET_FUNC(StretchDIBits);
143 GET_FUNC(StrokeAndFillPath);
144 GET_FUNC(StrokePath);
145 GET_FUNC(SwapBuffers);
146 GET_FUNC(WidenPath);
147 #undef GET_FUNC
149 /* add it to the list */
150 driver->prev = NULL;
151 if ((driver->next = first_driver)) driver->next->prev = driver;
152 first_driver = driver;
153 return driver;
157 /**********************************************************************
158 * load_display_driver
160 * Special case for loading the display driver: get the name from the config file
162 static struct graphics_driver *load_display_driver(void)
164 char buffer[MAX_PATH];
165 HMODULE module;
166 HKEY hkey;
168 if (display_driver) /* already loaded */
170 display_driver->count++;
171 return display_driver;
174 strcpy( buffer, "x11drv" ); /* default value */
175 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine", &hkey ))
177 DWORD type, count = sizeof(buffer);
178 RegQueryValueExA( hkey, "GraphicsDriver", 0, &type, buffer, &count );
179 RegCloseKey( hkey );
182 if (!(module = LoadLibraryA( buffer )))
184 MESSAGE( "Could not load graphics driver '%s'\n", buffer );
185 return NULL;
188 if (!(display_driver = create_driver( module )))
190 MESSAGE( "Could not create graphics driver '%s'\n", buffer );
191 FreeLibrary( module );
192 return NULL;
195 display_driver->count++; /* we don't want to free it */
196 return display_driver;
200 /**********************************************************************
201 * DRIVER_load_driver
203 const DC_FUNCTIONS *DRIVER_load_driver( LPCSTR name )
205 HMODULE module;
206 struct graphics_driver *driver;
208 RtlAcquirePebLock();
210 /* display driver is a special case */
211 if (!strcasecmp( name, "display" ))
213 driver = load_display_driver();
214 RtlReleasePebLock();
215 return &driver->funcs;
218 if ((module = GetModuleHandleA( name )))
220 for (driver = first_driver; driver; driver = driver->next)
222 if (driver->module == module)
224 driver->count++;
225 RtlReleasePebLock();
226 return &driver->funcs;
231 if (!(module = LoadLibraryA( name )))
233 RtlReleasePebLock();
234 return NULL;
237 if (!(driver = create_driver( module )))
239 FreeLibrary( module );
240 RtlReleasePebLock();
241 return NULL;
244 TRACE( "loaded driver %p for %s\n", driver, name );
245 RtlReleasePebLock();
246 return &driver->funcs;
250 /**********************************************************************
251 * DRIVER_get_driver
253 * Get a new copy of an existing driver.
255 const DC_FUNCTIONS *DRIVER_get_driver( const DC_FUNCTIONS *funcs )
257 struct graphics_driver *driver;
259 RtlAcquirePebLock();
260 for (driver = first_driver; driver; driver = driver->next)
261 if (&driver->funcs == funcs) break;
262 if (!driver) ERR( "driver not found, trouble ahead\n" );
263 driver->count++;
264 RtlReleasePebLock();
265 return funcs;
269 /**********************************************************************
270 * DRIVER_release_driver
272 * Release a driver by decrementing ref count and freeing it if needed.
274 void DRIVER_release_driver( const DC_FUNCTIONS *funcs )
276 struct graphics_driver *driver;
278 RtlAcquirePebLock();
280 for (driver = first_driver; driver; driver = driver->next)
281 if (&driver->funcs == funcs) break;
283 if (!driver) goto done;
284 if (--driver->count) goto done;
286 /* removed last reference, free it */
287 if (driver->next) driver->next->prev = driver->prev;
288 if (driver->prev) driver->prev->next = driver->next;
289 else first_driver = driver->next;
290 if (driver == display_driver) display_driver = NULL;
292 FreeLibrary( driver->module );
293 HeapFree( GetProcessHeap(), 0, driver );
294 done:
295 RtlReleasePebLock();
299 /*****************************************************************************
300 * DRIVER_GetDriverName
303 BOOL DRIVER_GetDriverName( LPCSTR device, LPSTR driver, DWORD size )
305 char *p;
306 size = GetProfileStringA("devices", device, "", driver, size);
307 if(!size) {
308 WARN("Unable to find '%s' in [devices] section of win.ini\n", device);
309 return FALSE;
311 p = strchr(driver, ',');
312 if(!p)
314 WARN("'%s' entry in [devices] section of win.ini is malformed.\n", device);
315 return FALSE;
317 *p = '\0';
318 TRACE("Found '%s' for '%s'\n", driver, device);
319 return TRUE;
322 /*****************************************************************************
323 * @ [GDI32.100]
325 * This should thunk to 16-bit and simply call the proc with the given args.
327 INT WINAPI GDI_CallDevInstall16( FARPROC16 lpfnDevInstallProc, HWND hWnd,
328 LPSTR lpModelName, LPSTR OldPort, LPSTR NewPort )
330 FIXME("(%p, %04x, %s, %s, %s)\n", lpfnDevInstallProc, hWnd, lpModelName, OldPort, NewPort );
331 return -1;
334 /*****************************************************************************
335 * @ [GDI32.101]
337 * This should load the correct driver for lpszDevice and calls this driver's
338 * ExtDeviceModePropSheet proc.
340 * Note: The driver calls a callback routine for each property sheet page; these
341 * pages are supposed to be filled into the structure pointed to by lpPropSheet.
342 * The layout of this structure is:
344 * struct
346 * DWORD nPages;
347 * DWORD unknown;
348 * HPROPSHEETPAGE pages[10];
349 * };
351 INT WINAPI GDI_CallExtDeviceModePropSheet16( HWND hWnd, LPCSTR lpszDevice,
352 LPCSTR lpszPort, LPVOID lpPropSheet )
354 FIXME("(%04x, %s, %s, %p)\n", hWnd, lpszDevice, lpszPort, lpPropSheet );
355 return -1;
358 /*****************************************************************************
359 * @ [GDI32.102]
361 * This should load the correct driver for lpszDevice and calls this driver's
362 * ExtDeviceMode proc.
364 INT WINAPI GDI_CallExtDeviceMode16( HWND hwnd,
365 LPDEVMODEA lpdmOutput, LPSTR lpszDevice,
366 LPSTR lpszPort, LPDEVMODEA lpdmInput,
367 LPSTR lpszProfile, DWORD fwMode )
369 char buf[300];
370 HDC hdc;
371 DC *dc;
372 INT ret = -1;
374 TRACE("(%04x, %p, %s, %s, %p, %s, %ld)\n",
375 hwnd, lpdmOutput, lpszDevice, lpszPort, lpdmInput, lpszProfile, fwMode );
377 if(!DRIVER_GetDriverName( lpszDevice, buf, sizeof(buf) )) return -1;
379 if (!(hdc = CreateICA( buf, NULL, lpszPort, NULL ))) return -1;
381 if ((dc = DC_GetDCPtr( hdc )))
383 if (dc->funcs->pExtDeviceMode)
384 ret = dc->funcs->pExtDeviceMode(buf, hwnd, lpdmOutput, lpszDevice, lpszPort,
385 lpdmInput, lpszProfile, fwMode);
386 GDI_ReleaseObj( hdc );
388 DeleteDC( hdc );
389 return ret;
392 /****************************************************************************
393 * @ [GDI32.103]
395 * This should load the correct driver for lpszDevice and calls this driver's
396 * AdvancedSetupDialog proc.
398 INT WINAPI GDI_CallAdvancedSetupDialog16( HWND hwnd, LPSTR lpszDevice,
399 LPDEVMODEA devin, LPDEVMODEA devout )
401 TRACE("(%04x, %s, %p, %p)\n", hwnd, lpszDevice, devin, devout );
402 return -1;
405 /*****************************************************************************
406 * @ [GDI32.104]
408 * This should load the correct driver for lpszDevice and calls this driver's
409 * DeviceCapabilities proc.
411 DWORD WINAPI GDI_CallDeviceCapabilities16( LPCSTR lpszDevice, LPCSTR lpszPort,
412 WORD fwCapability, LPSTR lpszOutput,
413 LPDEVMODEA lpdm )
415 char buf[300];
416 HDC hdc;
417 DC *dc;
418 INT ret = -1;
420 TRACE("(%s, %s, %d, %p, %p)\n", lpszDevice, lpszPort, fwCapability, lpszOutput, lpdm );
422 if(!DRIVER_GetDriverName( lpszDevice, buf, sizeof(buf) )) return -1;
424 if (!(hdc = CreateICA( buf, NULL, lpszPort, NULL ))) return -1;
426 if ((dc = DC_GetDCPtr( hdc )))
428 if (dc->funcs->pDeviceCapabilities)
429 ret = dc->funcs->pDeviceCapabilities( buf, lpszDevice, lpszPort,
430 fwCapability, lpszOutput, lpdm );
431 GDI_ReleaseObj( hdc );
433 DeleteDC( hdc );
434 return ret;