wineps: Pass the devmode to OpenPrinter.
[wine/multimedia.git] / dlls / wineps.drv / init.c
blob2e8087bea93473cb896482a648630de487060bc6
1 /*
2 * PostScript driver initialization functions
4 * Copyright 1998 Huw D M Davies
5 * Copyright 2001 Marcus Meissner
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_CUPS_CUPS_H
31 # include <cups/cups.h>
32 #endif
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "winreg.h"
41 #include "winnls.h"
42 #include "psdrv.h"
43 #include "winspool.h"
44 #include "wine/library.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
49 #ifdef SONAME_LIBCUPS
50 static void *cupshandle = NULL;
51 #endif
53 static const PSDRV_DEVMODE DefaultDevmode =
55 { /* dmPublic */
56 /* dmDeviceName */ {'W','i','n','e',' ','P','o','s','t','S','c','r','i','p','t',' ','D','r','i','v','e','r',0},
57 /* dmSpecVersion */ 0x30a,
58 /* dmDriverVersion */ 0x001,
59 /* dmSize */ sizeof(DEVMODEW),
60 /* dmDriverExtra */ sizeof(PSDRV_DEVMODE)-sizeof(DEVMODEW),
61 /* dmFields */ DM_ORIENTATION | DM_PAPERSIZE | DM_SCALE |
62 DM_COPIES | DM_DEFAULTSOURCE | DM_COLOR |
63 DM_YRESOLUTION | DM_TTOPTION,
64 { /* u1 */
65 { /* s1 */
66 /* dmOrientation */ DMORIENT_PORTRAIT,
67 /* dmPaperSize */ DMPAPER_LETTER,
68 /* dmPaperLength */ 2794,
69 /* dmPaperWidth */ 2159,
70 /* dmScale */ 100, /* ?? */
71 /* dmCopies */ 1,
72 /* dmDefaultSource */ DMBIN_AUTO,
73 /* dmPrintQuality */ 0
76 /* dmColor */ DMCOLOR_COLOR,
77 /* dmDuplex */ DMDUP_SIMPLEX,
78 /* dmYResolution */ 0,
79 /* dmTTOption */ DMTT_SUBDEV,
80 /* dmCollate */ 0,
81 /* dmFormName */ {},
82 /* dmUnusedPadding */ 0,
83 /* dmBitsPerPel */ 0,
84 /* dmPelsWidth */ 0,
85 /* dmPelsHeight */ 0,
86 { /* u2 */
87 /* dmDisplayFlags */ 0
89 /* dmDisplayFrequency */ 0,
90 /* dmICMMethod */ 0,
91 /* dmICMIntent */ 0,
92 /* dmMediaType */ 0,
93 /* dmDitherType */ 0,
94 /* dmReserved1 */ 0,
95 /* dmReserved2 */ 0,
96 /* dmPanningWidth */ 0,
97 /* dmPanningHeight */ 0
99 { /* dmDocPrivate */
100 /* dummy */ 0
102 { /* dmDrvPrivate */
103 /* numInstalledOptions */ 0
107 HINSTANCE PSDRV_hInstance = 0;
108 HANDLE PSDRV_Heap = 0;
110 static HFONT PSDRV_DefaultFont = 0;
111 static const LOGFONTA DefaultLogFont = {
112 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
113 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
116 static const struct gdi_dc_funcs psdrv_funcs;
118 /*********************************************************************
119 * DllMain
121 * Initializes font metrics and registers driver. wineps dll entry point.
124 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
126 TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
128 switch(reason) {
130 case DLL_PROCESS_ATTACH:
131 PSDRV_hInstance = hinst;
132 DisableThreadLibraryCalls(hinst);
134 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
135 if (PSDRV_Heap == NULL)
136 return FALSE;
138 if (PSDRV_GetFontMetrics() == FALSE) {
139 HeapDestroy(PSDRV_Heap);
140 return FALSE;
143 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
144 if (PSDRV_DefaultFont == NULL) {
145 HeapDestroy(PSDRV_Heap);
146 return FALSE;
148 #ifdef SONAME_LIBCUPS
149 /* dynamically load CUPS if not yet loaded */
150 if (!cupshandle) {
151 cupshandle = wine_dlopen(SONAME_LIBCUPS, RTLD_NOW, NULL, 0);
152 if (!cupshandle) cupshandle = (void*)-1;
154 #endif
155 break;
157 case DLL_PROCESS_DETACH:
159 DeleteObject( PSDRV_DefaultFont );
160 HeapDestroy( PSDRV_Heap );
161 #ifdef SONAME_LIBCUPS
162 if (cupshandle && (cupshandle != (void*)-1)) {
163 wine_dlclose(cupshandle, NULL, 0);
164 cupshandle = NULL;
166 #endif
167 break;
170 return TRUE;
173 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
175 PAGESIZE *page;
176 INT width = 0, height = 0;
178 if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
179 LIST_FOR_EACH_ENTRY(page, &physDev->pi->ppd->PageSizes, PAGESIZE, entry) {
180 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
181 break;
184 if(&page->entry == &physDev->pi->ppd->PageSizes) {
185 FIXME("Can't find page\n");
186 physDev->ImageableArea.left = 0;
187 physDev->ImageableArea.right = 0;
188 physDev->ImageableArea.bottom = 0;
189 physDev->ImageableArea.top = 0;
190 physDev->PageSize.cx = 0;
191 physDev->PageSize.cy = 0;
192 } else if(page->ImageableArea) {
193 /* physDev sizes in device units; ppd sizes in 1/72" */
194 physDev->ImageableArea.left = page->ImageableArea->llx *
195 physDev->logPixelsX / 72;
196 physDev->ImageableArea.right = page->ImageableArea->urx *
197 physDev->logPixelsX / 72;
198 physDev->ImageableArea.bottom = page->ImageableArea->lly *
199 physDev->logPixelsY / 72;
200 physDev->ImageableArea.top = page->ImageableArea->ury *
201 physDev->logPixelsY / 72;
202 physDev->PageSize.cx = page->PaperDimension->x *
203 physDev->logPixelsX / 72;
204 physDev->PageSize.cy = page->PaperDimension->y *
205 physDev->logPixelsY / 72;
206 } else {
207 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
208 physDev->ImageableArea.right = physDev->PageSize.cx =
209 page->PaperDimension->x * physDev->logPixelsX / 72;
210 physDev->ImageableArea.top = physDev->PageSize.cy =
211 page->PaperDimension->y * physDev->logPixelsY / 72;
213 } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
214 (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
215 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
216 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
217 physDev->ImageableArea.right = physDev->PageSize.cx =
218 physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
219 physDev->logPixelsX / 254;
220 physDev->ImageableArea.top = physDev->PageSize.cy =
221 physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
222 physDev->logPixelsY / 254;
223 } else {
224 FIXME("Odd dmFields %x\n", physDev->Devmode->dmPublic.dmFields);
225 physDev->ImageableArea.left = 0;
226 physDev->ImageableArea.right = 0;
227 physDev->ImageableArea.bottom = 0;
228 physDev->ImageableArea.top = 0;
229 physDev->PageSize.cx = 0;
230 physDev->PageSize.cy = 0;
233 TRACE("ImageableArea = %d,%d - %d,%d: PageSize = %dx%d\n",
234 physDev->ImageableArea.left, physDev->ImageableArea.bottom,
235 physDev->ImageableArea.right, physDev->ImageableArea.top,
236 physDev->PageSize.cx, physDev->PageSize.cy);
238 /* these are in device units */
239 width = physDev->ImageableArea.right - physDev->ImageableArea.left;
240 height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
242 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
243 physDev->horzRes = width;
244 physDev->vertRes = height;
245 } else {
246 physDev->horzRes = height;
247 physDev->vertRes = width;
250 /* these are in mm */
251 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
252 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
254 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
255 "horzRes = %d, vertRes = %d\n",
256 physDev->horzSize, physDev->vertSize,
257 physDev->horzRes, physDev->vertRes);
260 static PSDRV_PDEVICE *create_psdrv_physdev( PRINTERINFO *pi )
262 PSDRV_PDEVICE *physDev;
264 physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) );
265 if (!physDev) return NULL;
267 physDev->Devmode = HeapAlloc( GetProcessHeap(), 0, sizeof(PSDRV_DEVMODE) );
268 if (!physDev->Devmode)
270 HeapFree( GetProcessHeap(), 0, physDev );
271 return NULL;
274 *physDev->Devmode = *pi->Devmode;
275 physDev->pi = pi;
276 physDev->logPixelsX = pi->ppd->DefaultResolution;
277 physDev->logPixelsY = pi->ppd->DefaultResolution;
278 return physDev;
281 /**********************************************************************
282 * PSDRV_CreateDC
284 static BOOL PSDRV_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
285 LPCWSTR output, const DEVMODEW* initData )
287 PSDRV_PDEVICE *physDev;
288 PRINTERINFO *pi;
290 TRACE("(%s %s %s %p)\n", debugstr_w(driver), debugstr_w(device),
291 debugstr_w(output), initData);
293 if (!device) return FALSE;
294 pi = PSDRV_FindPrinterInfo( device );
295 if(!pi) return FALSE;
297 if(!pi->Fonts) {
298 RASTERIZER_STATUS status;
299 if(!GetRasterizerCaps(&status, sizeof(status)) ||
300 !(status.wFlags & TT_AVAILABLE) ||
301 !(status.wFlags & TT_ENABLED)) {
302 MESSAGE("Disabling printer %s since it has no builtin fonts and there are no TrueType fonts available.\n",
303 debugstr_w(device));
304 return FALSE;
308 if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
310 if (output && *output) physDev->job.output = strdupW( output );
312 if(initData)
313 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODE *)initData, pi);
315 PSDRV_UpdateDevCaps(physDev);
316 SelectObject( (*pdev)->hdc, PSDRV_DefaultFont );
317 push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
318 return TRUE;
322 /**********************************************************************
323 * PSDRV_CreateCompatibleDC
325 static BOOL PSDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
327 HDC hdc = (*pdev)->hdc;
328 PSDRV_PDEVICE *physDev, *orig_dev = get_psdrv_dev( orig );
329 PRINTERINFO *pi = PSDRV_FindPrinterInfo( orig_dev->pi->friendly_name );
331 if (!pi) return FALSE;
332 if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
333 PSDRV_MergeDevmodes( physDev->Devmode, orig_dev->Devmode, pi );
334 PSDRV_UpdateDevCaps(physDev);
335 SelectObject( hdc, PSDRV_DefaultFont );
336 push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
337 return TRUE;
342 /**********************************************************************
343 * PSDRV_DeleteDC
345 static BOOL PSDRV_DeleteDC( PHYSDEV dev )
347 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
349 TRACE("\n");
351 HeapFree( GetProcessHeap(), 0, physDev->Devmode );
352 HeapFree( GetProcessHeap(), 0, physDev->job.output );
353 HeapFree( GetProcessHeap(), 0, physDev );
355 return TRUE;
359 /**********************************************************************
360 * ResetDC (WINEPS.@)
362 static HDC PSDRV_ResetDC( PHYSDEV dev, const DEVMODEW *lpInitData )
364 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
366 if (lpInitData)
368 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODE *)lpInitData, physDev->pi);
369 PSDRV_UpdateDevCaps(physDev);
371 return dev->hdc;
374 /***********************************************************************
375 * GetDeviceCaps (WINEPS.@)
377 static INT PSDRV_GetDeviceCaps( PHYSDEV dev, INT cap )
379 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
381 switch(cap)
383 case DRIVERVERSION:
384 return 0;
385 case TECHNOLOGY:
386 return DT_RASPRINTER;
387 case HORZSIZE:
388 return MulDiv(physDev->horzSize, 100,
389 physDev->Devmode->dmPublic.u1.s1.dmScale);
390 case VERTSIZE:
391 return MulDiv(physDev->vertSize, 100,
392 physDev->Devmode->dmPublic.u1.s1.dmScale);
393 case HORZRES:
394 case DESKTOPHORZRES:
395 return physDev->horzRes;
396 case VERTRES:
397 case DESKTOPVERTRES:
398 return physDev->vertRes;
399 case BITSPIXEL:
400 return (physDev->pi->ppd->ColorDevice != CD_False) ? 32 : 1;
401 case PLANES:
402 return 1;
403 case NUMBRUSHES:
404 return -1;
405 case NUMPENS:
406 return 10;
407 case NUMMARKERS:
408 return 0;
409 case NUMFONTS:
410 return 39;
411 case NUMCOLORS:
412 return -1;
413 case PDEVICESIZE:
414 return sizeof(PSDRV_PDEVICE);
415 case CURVECAPS:
416 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
417 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
418 case LINECAPS:
419 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
420 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
421 case POLYGONALCAPS:
422 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
423 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
424 case TEXTCAPS:
425 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
426 case CLIPCAPS:
427 return CP_RECTANGLE;
428 case RASTERCAPS:
429 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
430 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
431 /* Are aspect[XY] and logPixels[XY] correct? */
432 /* Need to handle different res in x and y => fix ppd */
433 case ASPECTX:
434 case ASPECTY:
435 return physDev->pi->ppd->DefaultResolution;
436 case ASPECTXY:
437 return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
438 (double)physDev->pi->ppd->DefaultResolution );
439 case LOGPIXELSX:
440 return MulDiv(physDev->logPixelsX,
441 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
442 case LOGPIXELSY:
443 return MulDiv(physDev->logPixelsY,
444 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
445 case SIZEPALETTE:
446 return 0;
447 case NUMRESERVED:
448 return 0;
449 case COLORRES:
450 return 0;
451 case PHYSICALWIDTH:
452 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
453 physDev->PageSize.cy : physDev->PageSize.cx;
454 case PHYSICALHEIGHT:
455 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
456 physDev->PageSize.cx : physDev->PageSize.cy;
457 case PHYSICALOFFSETX:
458 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
459 if(physDev->pi->ppd->LandscapeOrientation == -90)
460 return physDev->PageSize.cy - physDev->ImageableArea.top;
461 else
462 return physDev->ImageableArea.bottom;
464 return physDev->ImageableArea.left;
466 case PHYSICALOFFSETY:
467 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
468 if(physDev->pi->ppd->LandscapeOrientation == -90)
469 return physDev->PageSize.cx - physDev->ImageableArea.right;
470 else
471 return physDev->ImageableArea.left;
473 return physDev->PageSize.cy - physDev->ImageableArea.top;
475 case SCALINGFACTORX:
476 case SCALINGFACTORY:
477 case VREFRESH:
478 case BLTALIGNMENT:
479 return 0;
480 case SHADEBLENDCAPS:
481 return SB_NONE;
482 default:
483 FIXME("(%p): unsupported capability %d, will return 0\n", dev->hdc, cap );
484 return 0;
488 static PRINTER_ENUM_VALUESA *load_font_sub_table( HANDLE printer, DWORD *num_entries )
490 DWORD res, needed, num;
491 PRINTER_ENUM_VALUESA *table = NULL;
492 static const char fontsubkey[] = "PrinterDriverData\\FontSubTable";
494 *num_entries = 0;
496 res = EnumPrinterDataExA( printer, fontsubkey, NULL, 0, &needed, &num );
497 if (res != ERROR_MORE_DATA) return NULL;
499 table = HeapAlloc( PSDRV_Heap, 0, needed );
500 if (!table) return NULL;
502 res = EnumPrinterDataExA( printer, fontsubkey, (LPBYTE)table, needed, &needed, &num );
503 if (res != ERROR_SUCCESS)
505 HeapFree( PSDRV_Heap, 0, table );
506 return NULL;
509 *num_entries = num;
510 return table;
513 static PSDRV_DEVMODE *get_printer_devmode( HANDLE printer )
515 DWORD needed, dm_size;
516 BOOL res;
517 PRINTER_INFO_9W *info;
518 PSDRV_DEVMODE *dm;
520 GetPrinterW( printer, 9, NULL, 0, &needed );
521 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return NULL;
523 info = HeapAlloc( PSDRV_Heap, 0, needed );
524 res = GetPrinterW( printer, 9, (BYTE *)info, needed, &needed );
525 if (!res || !info->pDevMode)
527 HeapFree( PSDRV_Heap, 0, info );
528 return NULL;
531 /* sanity check the sizes */
532 dm_size = info->pDevMode->dmSize + info->pDevMode->dmDriverExtra;
533 if ((char *)info->pDevMode - (char *)info + dm_size > needed)
535 HeapFree( PSDRV_Heap, 0, info );
536 return NULL;
539 dm = (PSDRV_DEVMODE*)info;
540 memmove( dm, info->pDevMode, dm_size );
541 return dm;
544 static PSDRV_DEVMODE *get_devmode( HANDLE printer, const WCHAR *name, BOOL *is_default )
546 PSDRV_DEVMODE *dm = get_printer_devmode( printer );
548 *is_default = FALSE;
550 if (dm && dm->dmPublic.dmSize + dm->dmPublic.dmDriverExtra >= sizeof(DefaultDevmode))
552 TRACE( "Retrieved devmode from winspool\n" );
553 return dm;
555 HeapFree( PSDRV_Heap, 0, dm );
557 TRACE( "Using default devmode\n" );
558 dm = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
559 if (dm)
561 *dm = DefaultDevmode;
562 lstrcpynW( (WCHAR *)dm->dmPublic.dmDeviceName, name, CCHDEVICENAME );
563 *is_default = TRUE;
565 return dm;
568 static BOOL set_devmode( HANDLE printer, PSDRV_DEVMODE *dm )
570 PRINTER_INFO_9W info;
571 info.pDevMode = &dm->dmPublic;
573 return SetPrinterW( printer, 9, (BYTE *)&info, 0 );
576 static struct list printer_list = LIST_INIT( printer_list );
578 /**********************************************************************
579 * PSDRV_FindPrinterInfo
581 PRINTERINFO *PSDRV_FindPrinterInfo(LPCWSTR name)
583 DWORD needed, res, dwPaperSize;
584 PRINTERINFO *pi;
585 FONTNAME *font;
586 const AFM *afm;
587 HANDLE hPrinter = 0;
588 const char *ppd = NULL;
589 DWORD ppdType;
590 char *ppdFileName = NULL, *nameA = NULL;
591 HKEY hkey;
592 BOOL using_default_devmode = FALSE;
593 int len;
595 TRACE("'%s'\n", debugstr_w(name));
597 LIST_FOR_EACH_ENTRY( pi, &printer_list, PRINTERINFO, entry )
599 if (!strcmpW( pi->friendly_name, name ))
600 return pi;
603 pi = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
604 if (pi == NULL) return NULL;
606 if (!(pi->friendly_name = HeapAlloc( PSDRV_Heap, 0, (strlenW(name)+1)*sizeof(WCHAR) ))) goto fail;
607 strcpyW( pi->friendly_name, name );
609 if (OpenPrinterW( pi->friendly_name, &hPrinter, NULL ) == 0) {
610 ERR ("OpenPrinter failed with code %i\n", GetLastError ());
611 goto cleanup;
614 len = WideCharToMultiByte( CP_ACP, 0, name, -1, NULL, 0, NULL, NULL );
615 nameA = HeapAlloc( GetProcessHeap(), 0, len );
616 WideCharToMultiByte( CP_ACP, 0, name, -1, nameA, len, NULL, NULL );
618 pi->Devmode = get_devmode( hPrinter, name, &using_default_devmode );
619 if (!pi->Devmode) goto closeprinter;
621 #ifdef SONAME_LIBCUPS
622 if (cupshandle != (void*)-1) {
623 typeof(cupsGetPPD) * pcupsGetPPD = NULL;
625 pcupsGetPPD = wine_dlsym(cupshandle, "cupsGetPPD", NULL, 0);
626 if (pcupsGetPPD) {
627 ppd = pcupsGetPPD( nameA );
629 if (ppd) {
630 needed=strlen(ppd)+1;
631 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
632 memcpy(ppdFileName, ppd, needed);
633 ppdType=REG_SZ;
634 res = ERROR_SUCCESS;
635 /* we should unlink() that file later */
636 } else {
637 res = ERROR_FILE_NOT_FOUND;
638 WARN("Did not find ppd for %s\n", debugstr_w(name));
642 #endif
643 if (!ppdFileName) {
644 res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "PPD File", NULL, NULL, 0, &needed);
645 if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
646 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
647 res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "PPD File", &ppdType,
648 (LPBYTE)ppdFileName, needed, &needed);
651 /* Look for a ppd file for this printer in the config file.
652 * First look under that printer's name, and then under 'generic'
654 /* @@ Wine registry key: HKCU\Software\Wine\Printing\PPD Files */
655 if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\PPD Files", &hkey))
657 const char* value_name;
659 if (RegQueryValueExA(hkey, nameA, 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
660 value_name=nameA;
661 } else if (RegQueryValueExA(hkey, "generic", 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
662 value_name="generic";
663 } else {
664 value_name=NULL;
666 if (value_name) {
667 HeapFree(PSDRV_Heap, 0, ppdFileName);
668 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
669 RegQueryValueExA(hkey, value_name, 0, &ppdType, (LPBYTE)ppdFileName, &needed);
671 RegCloseKey(hkey);
674 if (!ppdFileName)
676 const char *data_dir, *filename;
678 if ((data_dir = wine_get_data_dir())) filename = "/generic.ppd";
679 else if ((data_dir = wine_get_build_dir())) filename = "/dlls/wineps.drv/generic.ppd";
680 else
682 res = ERROR_FILE_NOT_FOUND;
683 ERR ("Error %i getting PPD file name for printer '%s'\n", res, debugstr_w(name));
684 goto closeprinter;
686 ppdFileName = HeapAlloc( PSDRV_Heap, 0, strlen(data_dir) + strlen(filename) + 1 );
687 strcpy( ppdFileName, data_dir );
688 strcat( ppdFileName, filename );
689 } else {
690 res = ERROR_SUCCESS;
691 if (ppdType==REG_EXPAND_SZ) {
692 char* tmp;
694 /* Expand environment variable references */
695 needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
696 tmp=HeapAlloc(PSDRV_Heap, 0, needed);
697 ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
698 HeapFree(PSDRV_Heap, 0, ppdFileName);
699 ppdFileName=tmp;
703 pi->ppd = PSDRV_ParsePPD(ppdFileName);
704 if(!pi->ppd) {
705 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
706 ppdFileName);
707 goto closeprinter;
710 /* Some gimp-print ppd files don't contain a DefaultResolution line
711 set it to 300 if it's not specified */
712 if(pi->ppd->DefaultResolution == 0)
713 pi->ppd->DefaultResolution = 300;
715 if(using_default_devmode) {
716 DWORD papersize;
718 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
719 (LPWSTR)&papersize, sizeof(papersize)/sizeof(WCHAR))) {
720 PSDRV_DEVMODE dm;
721 memset(&dm, 0, sizeof(dm));
722 dm.dmPublic.dmFields = DM_PAPERSIZE;
723 dm.dmPublic.u1.s1.dmPaperSize = papersize;
724 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
727 set_devmode( hPrinter, pi->Devmode );
730 if(pi->ppd->DefaultPageSize) { /* We'll let the ppd override the devmode */
731 PSDRV_DEVMODE dm;
732 memset(&dm, 0, sizeof(dm));
733 dm.dmPublic.dmFields = DM_PAPERSIZE;
734 dm.dmPublic.u1.s1.dmPaperSize = pi->ppd->DefaultPageSize->WinPage;
735 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
739 * This is a hack. The default paper size should be read in as part of
740 * the Devmode structure, but Wine doesn't currently provide a convenient
741 * way to configure printers.
743 res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "Paper Size", NULL,
744 (LPBYTE)&dwPaperSize, sizeof(DWORD), &needed);
745 if (res == ERROR_SUCCESS)
746 pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
747 else if (res == ERROR_FILE_NOT_FOUND)
748 TRACE ("No 'Paper Size' for printer '%s'\n", debugstr_w(name));
749 else {
750 ERR ("GetPrinterDataA returned %i\n", res);
751 goto closeprinter;
754 /* Duplex is indicated by the setting of the DM_DUPLEX bit in dmFields.
755 WinDuplex == 0 is a special case which means that the ppd has a
756 *DefaultDuplex: NotCapable entry. In this case we'll try not to confuse
757 apps and set dmDuplex to DMDUP_SIMPLEX but leave the DM_DUPLEX clear.
758 PSDRV_WriteHeader understands this and copes. */
759 pi->Devmode->dmPublic.dmFields &= ~DM_DUPLEX;
760 if(pi->ppd->DefaultDuplex) {
761 pi->Devmode->dmPublic.dmDuplex = pi->ppd->DefaultDuplex->WinDuplex;
762 if(pi->Devmode->dmPublic.dmDuplex != 0)
763 pi->Devmode->dmPublic.dmFields |= DM_DUPLEX;
764 else
765 pi->Devmode->dmPublic.dmDuplex = DMDUP_SIMPLEX;
768 pi->FontSubTable = load_font_sub_table( hPrinter, &pi->FontSubTableSize );
770 ClosePrinter( hPrinter );
772 pi->Fonts = NULL;
774 LIST_FOR_EACH_ENTRY( font, &pi->ppd->InstalledFonts, FONTNAME, entry )
776 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
777 if(!afm) {
778 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
779 "ignoring\n", font->Name);
781 else {
782 BOOL added;
783 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
784 PSDRV_FreeAFMList(pi->Fonts);
785 goto cleanup;
790 HeapFree( GetProcessHeap(), 0, nameA );
791 if (ppd) unlink(ppd);
792 list_add_head( &printer_list, &pi->entry );
793 return pi;
795 closeprinter:
796 ClosePrinter(hPrinter);
797 cleanup:
798 HeapFree(PSDRV_Heap, 0, ppdFileName);
799 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
800 HeapFree(PSDRV_Heap, 0, pi->friendly_name);
801 HeapFree(PSDRV_Heap, 0, pi->Devmode);
802 fail:
803 HeapFree(PSDRV_Heap, 0, pi);
804 HeapFree( GetProcessHeap(), 0, nameA );
805 if (ppd) unlink(ppd);
806 return NULL;
810 static const struct gdi_dc_funcs psdrv_funcs =
812 NULL, /* pAbortDoc */
813 NULL, /* pAbortPath */
814 NULL, /* pAlphaBlend */
815 NULL, /* pAngleArc */
816 PSDRV_Arc, /* pArc */
817 NULL, /* pArcTo */
818 NULL, /* pBeginPath */
819 NULL, /* pBlendImage */
820 NULL, /* pChoosePixelFormat */
821 PSDRV_Chord, /* pChord */
822 NULL, /* pCloseFigure */
823 NULL, /* pCopyBitmap */
824 NULL, /* pCreateBitmap */
825 PSDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
826 PSDRV_CreateDC, /* pCreateDC */
827 NULL, /* pDeleteBitmap */
828 PSDRV_DeleteDC, /* pDeleteDC */
829 NULL, /* pDeleteObject */
830 NULL, /* pDescribePixelFormat */
831 PSDRV_DeviceCapabilities, /* pDeviceCapabilities */
832 PSDRV_Ellipse, /* pEllipse */
833 PSDRV_EndDoc, /* pEndDoc */
834 PSDRV_EndPage, /* pEndPage */
835 NULL, /* pEndPath */
836 PSDRV_EnumFonts, /* pEnumFonts */
837 NULL, /* pEnumICMProfiles */
838 NULL, /* pExcludeClipRect */
839 PSDRV_ExtDeviceMode, /* pExtDeviceMode */
840 PSDRV_ExtEscape, /* pExtEscape */
841 NULL, /* pExtFloodFill */
842 NULL, /* pExtSelectClipRgn */
843 PSDRV_ExtTextOut, /* pExtTextOut */
844 PSDRV_FillPath, /* pFillPath */
845 NULL, /* pFillRgn */
846 NULL, /* pFlattenPath */
847 NULL, /* pFontIsLinked */
848 NULL, /* pFrameRgn */
849 NULL, /* pGdiComment */
850 NULL, /* pGdiRealizationInfo */
851 NULL, /* pGetCharABCWidths */
852 NULL, /* pGetCharABCWidthsI */
853 PSDRV_GetCharWidth, /* pGetCharWidth */
854 PSDRV_GetDeviceCaps, /* pGetDeviceCaps */
855 NULL, /* pGetDeviceGammaRamp */
856 NULL, /* pGetFontData */
857 NULL, /* pGetFontUnicodeRanges */
858 NULL, /* pGetGlyphIndices */
859 NULL, /* pGetGlyphOutline */
860 NULL, /* pGetICMProfile */
861 NULL, /* pGetImage */
862 NULL, /* pGetKerningPairs */
863 NULL, /* pGetNearestColor */
864 NULL, /* pGetOutlineTextMetrics */
865 NULL, /* pGetPixel */
866 NULL, /* pGetPixelFormat */
867 NULL, /* pGetSystemPaletteEntries */
868 NULL, /* pGetTextCharsetInfo */
869 PSDRV_GetTextExtentExPoint, /* pGetTextExtentExPoint */
870 NULL, /* pGetTextExtentExPointI */
871 NULL, /* pGetTextFace */
872 PSDRV_GetTextMetrics, /* pGetTextMetrics */
873 NULL, /* pGradientFill */
874 NULL, /* pIntersectClipRect */
875 NULL, /* pInvertRgn */
876 PSDRV_LineTo, /* pLineTo */
877 NULL, /* pModifyWorldTransform */
878 NULL, /* pMoveTo */
879 NULL, /* pOffsetClipRgn */
880 NULL, /* pOffsetViewportOrg */
881 NULL, /* pOffsetWindowOrg */
882 PSDRV_PaintRgn, /* pPaintRgn */
883 PSDRV_PatBlt, /* pPatBlt */
884 PSDRV_Pie, /* pPie */
885 PSDRV_PolyBezier, /* pPolyBezier */
886 PSDRV_PolyBezierTo, /* pPolyBezierTo */
887 NULL, /* pPolyDraw */
888 PSDRV_PolyPolygon, /* pPolyPolygon */
889 PSDRV_PolyPolyline, /* pPolyPolyline */
890 NULL, /* pPolygon */
891 NULL, /* pPolyline */
892 NULL, /* pPolylineTo */
893 PSDRV_PutImage, /* pPutImage */
894 NULL, /* pRealizeDefaultPalette */
895 NULL, /* pRealizePalette */
896 PSDRV_Rectangle, /* pRectangle */
897 PSDRV_ResetDC, /* pResetDC */
898 NULL, /* pRestoreDC */
899 PSDRV_RoundRect, /* pRoundRect */
900 NULL, /* pSaveDC */
901 NULL, /* pScaleViewportExt */
902 NULL, /* pScaleWindowExt */
903 NULL, /* pSelectBitmap */
904 PSDRV_SelectBrush, /* pSelectBrush */
905 NULL, /* pSelectClipPath */
906 PSDRV_SelectFont, /* pSelectFont */
907 NULL, /* pSelectPalette */
908 PSDRV_SelectPen, /* pSelectPen */
909 NULL, /* pSetArcDirection */
910 PSDRV_SetBkColor, /* pSetBkColor */
911 NULL, /* pSetBkMode */
912 PSDRV_SetDCBrushColor, /* pSetDCBrushColor */
913 PSDRV_SetDCPenColor, /* pSetDCPenColor */
914 NULL, /* pSetDIBitsToDevice */
915 NULL, /* pSetDeviceClipping */
916 NULL, /* pSetDeviceGammaRamp */
917 NULL, /* pSetLayout */
918 NULL, /* pSetMapMode */
919 NULL, /* pSetMapperFlags */
920 PSDRV_SetPixel, /* pSetPixel */
921 NULL, /* pSetPixelFormat */
922 NULL, /* pSetPolyFillMode */
923 NULL, /* pSetROP2 */
924 NULL, /* pSetRelAbs */
925 NULL, /* pSetStretchBltMode */
926 NULL, /* pSetTextAlign */
927 NULL, /* pSetTextCharacterExtra */
928 PSDRV_SetTextColor, /* pSetTextColor */
929 NULL, /* pSetTextJustification */
930 NULL, /* pSetViewportExt */
931 NULL, /* pSetViewportOrg */
932 NULL, /* pSetWindowExt */
933 NULL, /* pSetWindowOrg */
934 NULL, /* pSetWorldTransform */
935 PSDRV_StartDoc, /* pStartDoc */
936 PSDRV_StartPage, /* pStartPage */
937 NULL, /* pStretchBlt */
938 NULL, /* pStretchDIBits */
939 PSDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
940 PSDRV_StrokePath, /* pStrokePath */
941 NULL, /* pSwapBuffers */
942 NULL, /* pUnrealizePalette */
943 NULL, /* pWidenPath */
944 /* OpenGL not supported */
948 /******************************************************************************
949 * PSDRV_get_gdi_driver
951 const struct gdi_dc_funcs * CDECL PSDRV_get_gdi_driver( unsigned int version )
953 if (version != WINE_GDI_DRIVER_VERSION)
955 ERR( "version mismatch, gdi32 wants %u but wineps has %u\n", version, WINE_GDI_DRIVER_VERSION );
956 return NULL;
958 return &psdrv_funcs;