crypt32: Add additional path for Solaris 11 Express.
[wine] / dlls / wineps.drv / init.c
1 /*
2  *      PostScript driver initialization functions
3  *
4  *      Copyright 1998 Huw D M Davies
5  *      Copyright 2001 Marcus Meissner
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
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
33
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
36
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"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
48
49 #ifdef SONAME_LIBCUPS
50 static void *cupshandle = NULL;
51 #endif
52
53 static const PSDRV_DEVMODEA DefaultDevmode =
54 {
55   { /* dmPublic */
56 /* dmDeviceName */      "Wine PostScript Driver",
57 /* dmSpecVersion */     0x30a,
58 /* dmDriverVersion */   0x001,
59 /* dmSize */            sizeof(DEVMODEA),
60 /* dmDriverExtra */     sizeof(PSDRV_DEVMODEA)-sizeof(DEVMODEA),
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
74      }
75    },
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
88    },
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
98   },
99   { /* dmDocPrivate */
100     /* dummy */ 0
101   },
102   { /* dmDrvPrivate */
103     /* numInstalledOptions */ 0
104   }
105 };
106
107 HINSTANCE PSDRV_hInstance = 0;
108 HANDLE PSDRV_Heap = 0;
109
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, ""
114 };
115
116 static const CHAR default_devmodeA[] = "Default DevMode";
117
118 /*********************************************************************
119  *           DllMain
120  *
121  * Initializes font metrics and registers driver. wineps dll entry point.
122  *
123  */
124 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
125 {
126     TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
127
128     switch(reason) {
129
130         case DLL_PROCESS_ATTACH:
131             PSDRV_hInstance = hinst;
132             DisableThreadLibraryCalls(hinst);
133
134             PSDRV_Heap = HeapCreate(0, 0x10000, 0);
135             if (PSDRV_Heap == NULL)
136                 return FALSE;
137
138             if (PSDRV_GetFontMetrics() == FALSE) {
139                 HeapDestroy(PSDRV_Heap);
140                 return FALSE;
141             }
142
143             PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
144             if (PSDRV_DefaultFont == NULL) {
145                 HeapDestroy(PSDRV_Heap);
146                 return FALSE;
147             }
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;
153             }
154 #endif
155             break;
156
157         case DLL_PROCESS_DETACH:
158
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;
165             }
166 #endif
167             break;
168     }
169
170     return TRUE;
171 }
172
173
174 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
175 {
176     PAGESIZE *page;
177     INT width = 0, height = 0;
178
179     if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
180         LIST_FOR_EACH_ENTRY(page, &physDev->pi->ppd->PageSizes, PAGESIZE, entry) {
181             if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
182                 break;
183         }
184
185         if(&page->entry == &physDev->pi->ppd->PageSizes) {
186             FIXME("Can't find page\n");
187             physDev->ImageableArea.left = 0;
188             physDev->ImageableArea.right = 0;
189             physDev->ImageableArea.bottom = 0;
190             physDev->ImageableArea.top = 0;
191             physDev->PageSize.cx = 0;
192             physDev->PageSize.cy = 0;
193         } else if(page->ImageableArea) {
194           /* physDev sizes in device units; ppd sizes in 1/72" */
195             physDev->ImageableArea.left = page->ImageableArea->llx *
196               physDev->logPixelsX / 72;
197             physDev->ImageableArea.right = page->ImageableArea->urx *
198               physDev->logPixelsX / 72;
199             physDev->ImageableArea.bottom = page->ImageableArea->lly *
200               physDev->logPixelsY / 72;
201             physDev->ImageableArea.top = page->ImageableArea->ury *
202               physDev->logPixelsY / 72;
203             physDev->PageSize.cx = page->PaperDimension->x *
204               physDev->logPixelsX / 72;
205             physDev->PageSize.cy = page->PaperDimension->y *
206               physDev->logPixelsY / 72;
207         } else {
208             physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
209             physDev->ImageableArea.right = physDev->PageSize.cx =
210               page->PaperDimension->x * physDev->logPixelsX / 72;
211             physDev->ImageableArea.top = physDev->PageSize.cy =
212               page->PaperDimension->y * physDev->logPixelsY / 72;
213         }
214     } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
215               (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
216       /* physDev sizes in device units; Devmode sizes in 1/10 mm */
217         physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
218         physDev->ImageableArea.right = physDev->PageSize.cx =
219           physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
220           physDev->logPixelsX / 254;
221         physDev->ImageableArea.top = physDev->PageSize.cy =
222           physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
223           physDev->logPixelsY / 254;
224     } else {
225         FIXME("Odd dmFields %x\n", physDev->Devmode->dmPublic.dmFields);
226         physDev->ImageableArea.left = 0;
227         physDev->ImageableArea.right = 0;
228         physDev->ImageableArea.bottom = 0;
229         physDev->ImageableArea.top = 0;
230         physDev->PageSize.cx = 0;
231         physDev->PageSize.cy = 0;
232     }
233
234     TRACE("ImageableArea = %d,%d - %d,%d: PageSize = %dx%d\n",
235           physDev->ImageableArea.left, physDev->ImageableArea.bottom,
236           physDev->ImageableArea.right, physDev->ImageableArea.top,
237           physDev->PageSize.cx, physDev->PageSize.cy);
238
239     /* these are in device units */
240     width = physDev->ImageableArea.right - physDev->ImageableArea.left;
241     height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
242
243     if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
244         physDev->horzRes = width;
245         physDev->vertRes = height;
246     } else {
247         physDev->horzRes = height;
248         physDev->vertRes = width;
249     }
250
251     /* these are in mm */
252     physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
253     physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
254
255     TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
256           "horzRes = %d, vertRes = %d\n",
257           physDev->horzSize, physDev->vertSize,
258           physDev->horzRes, physDev->vertRes);
259 }
260
261
262 /***********************************************************
263  *      DEVMODEdupWtoA
264  *
265  * Creates an ascii copy of supplied devmode on heap
266  *
267  * Copied from dlls/winspool/info.c until full unicodification
268  */
269 static LPDEVMODEA DEVMODEdupWtoA(HANDLE heap, const DEVMODEW *dmW)
270 {
271     LPDEVMODEA dmA;
272     DWORD size;
273     BOOL Formname;
274     /* there is no pointer dereference here, if your code checking tool complains it's broken */
275     ptrdiff_t off_formname = (const char *)dmW->dmFormName - (const char *)dmW;
276
277     if(!dmW) return NULL;
278     Formname = (dmW->dmSize > off_formname);
279     size = dmW->dmSize - CCHDEVICENAME - (Formname ? CCHFORMNAME : 0);
280     dmA = HeapAlloc(heap, HEAP_ZERO_MEMORY, size + dmW->dmDriverExtra);
281     WideCharToMultiByte(CP_ACP, 0, dmW->dmDeviceName, -1, (LPSTR)dmA->dmDeviceName,
282                         CCHDEVICENAME, NULL, NULL);
283     if(!Formname) {
284       memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
285              dmW->dmSize - CCHDEVICENAME * sizeof(WCHAR));
286     } else {
287       memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
288              off_formname - CCHDEVICENAME * sizeof(WCHAR));
289       WideCharToMultiByte(CP_ACP, 0, dmW->dmFormName, -1, (LPSTR)dmA->dmFormName,
290                           CCHFORMNAME, NULL, NULL);
291       memcpy(&dmA->dmLogPixels, &dmW->dmLogPixels, dmW->dmSize -
292              (off_formname + CCHFORMNAME * sizeof(WCHAR)));
293     }
294     dmA->dmSize = size;
295     memcpy((char *)dmA + dmA->dmSize, (const char *)dmW + dmW->dmSize,
296            dmW->dmDriverExtra);
297     return dmA;
298 }
299
300
301 /**********************************************************************
302  *           PSDRV_CreateDC
303  */
304 static BOOL PSDRV_CreateDC( HDC hdc, PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
305                      LPCWSTR output, const DEVMODEW* initData )
306 {
307     PSDRV_PDEVICE *physDev;
308     PRINTERINFO *pi;
309
310     /* If no device name was specified, retrieve the device name
311      * from the PRINTERINFO structure from the DC's physDev.
312      * (See CreateCompatibleDC) */
313     if ( !device && *pdev )
314     {
315         pi = PSDRV_FindPrinterInfo(get_psdrv_dev(*pdev)->pi->FriendlyName);
316     }
317     else
318     {
319         DWORD len = WideCharToMultiByte(CP_ACP, 0, device, -1, NULL, 0, NULL, NULL);
320         char *deviceA = HeapAlloc(GetProcessHeap(), 0, len);
321         WideCharToMultiByte(CP_ACP, 0, device, -1, deviceA, len, NULL, NULL);
322         pi = PSDRV_FindPrinterInfo(deviceA);
323         HeapFree(GetProcessHeap(), 0, deviceA);
324     }
325
326     TRACE("(%s %s %s %p)\n", debugstr_w(driver), debugstr_w(device),
327                              debugstr_w(output), initData);
328
329     if(!pi) return FALSE;
330
331     if(!pi->Fonts) {
332         RASTERIZER_STATUS status;
333         if(!GetRasterizerCaps(&status, sizeof(status)) ||
334            !(status.wFlags & TT_AVAILABLE) ||
335            !(status.wFlags & TT_ENABLED)) {
336             MESSAGE("Disabling printer %s since it has no builtin fonts and there are no TrueType fonts available.\n",
337                     debugstr_w(device));
338             return FALSE;
339         }
340     }
341
342     physDev = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*physDev) );
343     if (!physDev) return FALSE;
344     *pdev = &physDev->dev;
345
346     physDev->pi = pi;
347
348     physDev->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(PSDRV_DEVMODEA) );
349     if(!physDev->Devmode) {
350         HeapFree( PSDRV_Heap, 0, physDev );
351         return FALSE;
352     }
353
354     *physDev->Devmode = *pi->Devmode;
355
356     physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
357     physDev->logPixelsY = physDev->pi->ppd->DefaultResolution;
358
359     if (output && *output) {
360         INT len = WideCharToMultiByte( CP_ACP, 0, output, -1, NULL, 0, NULL, NULL );
361         if ((physDev->job.output = HeapAlloc( PSDRV_Heap, 0, len )))
362             WideCharToMultiByte( CP_ACP, 0, output, -1, physDev->job.output, len, NULL, NULL );
363     } else
364         physDev->job.output = NULL;
365     physDev->job.id = 0;
366
367     if(initData) {
368         DEVMODEA *devmodeA = DEVMODEdupWtoA(PSDRV_Heap, initData);
369         PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, pi);
370         HeapFree(PSDRV_Heap, 0, devmodeA);
371     }
372
373     PSDRV_UpdateDevCaps(physDev);
374     SelectObject( hdc, PSDRV_DefaultFont );
375     return TRUE;
376 }
377
378
379
380 /**********************************************************************
381  *           PSDRV_DeleteDC
382  */
383 static BOOL PSDRV_DeleteDC( PHYSDEV dev )
384 {
385     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
386
387     TRACE("\n");
388
389     HeapFree( PSDRV_Heap, 0, physDev->Devmode );
390     HeapFree( PSDRV_Heap, 0, physDev->job.output );
391     HeapFree( PSDRV_Heap, 0, physDev );
392
393     return TRUE;
394 }
395
396
397 /**********************************************************************
398  *           ResetDC   (WINEPS.@)
399  */
400 static HDC PSDRV_ResetDC( PHYSDEV dev, const DEVMODEW *lpInitData )
401 {
402     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
403
404     if(lpInitData) {
405         DEVMODEA *devmodeA = DEVMODEdupWtoA(PSDRV_Heap, lpInitData);
406         PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, physDev->pi);
407         HeapFree(PSDRV_Heap, 0, devmodeA);
408         PSDRV_UpdateDevCaps(physDev);
409     }
410     return dev->hdc;
411 }
412
413 /***********************************************************************
414  *           GetDeviceCaps    (WINEPS.@)
415  */
416 static INT PSDRV_GetDeviceCaps( PHYSDEV dev, INT cap )
417 {
418     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
419
420     switch(cap)
421     {
422     case DRIVERVERSION:
423         return 0;
424     case TECHNOLOGY:
425         return DT_RASPRINTER;
426     case HORZSIZE:
427         return MulDiv(physDev->horzSize, 100,
428                       physDev->Devmode->dmPublic.u1.s1.dmScale);
429     case VERTSIZE:
430         return MulDiv(physDev->vertSize, 100,
431                       physDev->Devmode->dmPublic.u1.s1.dmScale);
432     case HORZRES:
433     case DESKTOPHORZRES:
434         return physDev->horzRes;
435     case VERTRES:
436     case DESKTOPVERTRES:
437         return physDev->vertRes;
438     case BITSPIXEL:
439         return (physDev->pi->ppd->ColorDevice != CD_False) ? 8 : 1;
440     case PLANES:
441         return 1;
442     case NUMBRUSHES:
443         return -1;
444     case NUMPENS:
445         return 10;
446     case NUMMARKERS:
447         return 0;
448     case NUMFONTS:
449         return 39;
450     case NUMCOLORS:
451         return (physDev->pi->ppd->ColorDevice != CD_False) ? 256 : -1;
452     case PDEVICESIZE:
453         return sizeof(PSDRV_PDEVICE);
454     case CURVECAPS:
455         return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
456                 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
457     case LINECAPS:
458         return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
459                 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
460     case POLYGONALCAPS:
461         return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
462                 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
463     case TEXTCAPS:
464         return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
465     case CLIPCAPS:
466         return CP_RECTANGLE;
467     case RASTERCAPS:
468         return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
469                 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
470     /* Are aspect[XY] and logPixels[XY] correct? */
471     /* Need to handle different res in x and y => fix ppd */
472     case ASPECTX:
473     case ASPECTY:
474         return physDev->pi->ppd->DefaultResolution;
475     case ASPECTXY:
476         return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
477                            (double)physDev->pi->ppd->DefaultResolution );
478     case LOGPIXELSX:
479         return MulDiv(physDev->logPixelsX,
480                       physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
481     case LOGPIXELSY:
482         return MulDiv(physDev->logPixelsY,
483                       physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
484     case SIZEPALETTE:
485         return 0;
486     case NUMRESERVED:
487         return 0;
488     case COLORRES:
489         return 0;
490     case PHYSICALWIDTH:
491         return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
492           physDev->PageSize.cy : physDev->PageSize.cx;
493     case PHYSICALHEIGHT:
494         return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
495           physDev->PageSize.cx : physDev->PageSize.cy;
496     case PHYSICALOFFSETX:
497       if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
498           if(physDev->pi->ppd->LandscapeOrientation == -90)
499               return physDev->PageSize.cy - physDev->ImageableArea.top;
500           else
501               return physDev->ImageableArea.bottom;
502       }
503       return physDev->ImageableArea.left;
504
505     case PHYSICALOFFSETY:
506       if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
507           if(physDev->pi->ppd->LandscapeOrientation == -90)
508               return physDev->PageSize.cx - physDev->ImageableArea.right;
509           else
510               return physDev->ImageableArea.left;
511       }
512       return physDev->PageSize.cy - physDev->ImageableArea.top;
513
514     case SCALINGFACTORX:
515     case SCALINGFACTORY:
516     case VREFRESH:
517     case BLTALIGNMENT:
518         return 0;
519     case SHADEBLENDCAPS:
520         return SB_NONE;
521     default:
522         FIXME("(%p): unsupported capability %d, will return 0\n", dev->hdc, cap );
523         return 0;
524     }
525 }
526
527
528 /**********************************************************************
529  *              PSDRV_FindPrinterInfo
530  */
531 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
532 {
533     static PRINTERINFO *PSDRV_PrinterList;
534     DWORD type = REG_BINARY, needed, res, dwPaperSize;
535     PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
536     FONTNAME *font;
537     const AFM *afm;
538     HANDLE hPrinter = 0;
539     const char *ppd = NULL;
540     DWORD ppdType;
541     char* ppdFileName = NULL;
542     HKEY hkey;
543     BOOL using_default_devmode = FALSE;
544
545     TRACE("'%s'\n", name);
546
547     /*
548      *  If this loop completes, last will point to the 'next' element of the
549      *  final PRINTERINFO in the list
550      */
551     for( ; pi; last = &pi->next, pi = pi->next)
552         if(!strcmp(pi->FriendlyName, name))
553             return pi;
554
555     pi = *last = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
556     if (pi == NULL)
557         return NULL;
558
559     if (!(pi->FriendlyName = HeapAlloc( PSDRV_Heap, 0, strlen(name)+1 ))) goto fail;
560     strcpy( pi->FriendlyName, name );
561
562     if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
563         ERR ("OpenPrinterA failed with code %i\n", GetLastError ());
564         goto cleanup;
565     }
566
567     needed = 0;
568     res = GetPrinterDataExA(hPrinter, NULL, default_devmodeA, &type, NULL, 0, &needed);
569
570     if (needed < sizeof(DefaultDevmode)) {
571         pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
572         if (pi->Devmode == NULL)
573             goto closeprinter;
574
575         *pi->Devmode = DefaultDevmode;
576         lstrcpynA((LPSTR)pi->Devmode->dmPublic.dmDeviceName,name,CCHDEVICENAME);
577         using_default_devmode = TRUE;
578     }
579     else {
580         pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
581         if (pi->Devmode == NULL)
582             goto closeprinter;
583
584         GetPrinterDataExA(hPrinter, NULL, default_devmodeA, &type, (LPBYTE)pi->Devmode, needed, &needed);
585     }
586
587
588
589 #ifdef SONAME_LIBCUPS
590     if (cupshandle != (void*)-1) {
591         typeof(cupsGetPPD) * pcupsGetPPD = NULL;
592
593         pcupsGetPPD = wine_dlsym(cupshandle, "cupsGetPPD", NULL, 0);
594         if (pcupsGetPPD) {
595             ppd = pcupsGetPPD(name);
596
597             if (ppd) {
598                 needed=strlen(ppd)+1;
599                 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
600                 memcpy(ppdFileName, ppd, needed);
601                 ppdType=REG_SZ;
602                 res = ERROR_SUCCESS;
603                 /* we should unlink() that file later */
604             } else {
605                 res = ERROR_FILE_NOT_FOUND;
606                 WARN("Did not find ppd for %s\n",name);
607             }
608         }
609     }
610 #endif
611     if (!ppdFileName) {
612         res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "PPD File", NULL, NULL, 0, &needed);
613         if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
614             ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
615             res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "PPD File", &ppdType,
616                                     (LPBYTE)ppdFileName, needed, &needed);
617         }
618     }
619     /* Look for a ppd file for this printer in the config file.
620      * First look under that printer's name, and then under 'generic'
621      */
622     /* @@ Wine registry key: HKCU\Software\Wine\Printing\PPD Files */
623     if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\PPD Files", &hkey))
624     {
625         const char* value_name;
626
627         if (RegQueryValueExA(hkey, name, 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
628             value_name=name;
629         } else if (RegQueryValueExA(hkey, "generic", 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
630             value_name="generic";
631         } else {
632             value_name=NULL;
633         }
634         if (value_name) {
635             HeapFree(PSDRV_Heap, 0, ppdFileName);
636             ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
637             RegQueryValueExA(hkey, value_name, 0, &ppdType, (LPBYTE)ppdFileName, &needed);
638         }
639         RegCloseKey(hkey);
640     }
641
642     if (!ppdFileName)
643     {
644         const char *data_dir, *filename;
645
646         if ((data_dir = wine_get_data_dir())) filename = "/generic.ppd";
647         else if ((data_dir = wine_get_build_dir())) filename = "/dlls/wineps.drv/generic.ppd";
648         else
649         {
650             res = ERROR_FILE_NOT_FOUND;
651             ERR ("Error %i getting PPD file name for printer '%s'\n", res, name);
652             goto closeprinter;
653         }
654         ppdFileName = HeapAlloc( PSDRV_Heap, 0, strlen(data_dir) + strlen(filename) + 1 );
655         strcpy( ppdFileName, data_dir );
656         strcat( ppdFileName, filename );
657     } else {
658         res = ERROR_SUCCESS;
659         if (ppdType==REG_EXPAND_SZ) {
660             char* tmp;
661
662             /* Expand environment variable references */
663             needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
664             tmp=HeapAlloc(PSDRV_Heap, 0, needed);
665             ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
666             HeapFree(PSDRV_Heap, 0, ppdFileName);
667             ppdFileName=tmp;
668         }
669     }
670
671     pi->ppd = PSDRV_ParsePPD(ppdFileName);
672     if(!pi->ppd) {
673         MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
674             ppdFileName);
675         goto closeprinter;
676     }
677
678     /* Some gimp-print ppd files don't contain a DefaultResolution line
679        set it to 300 if it's not specified */
680     if(pi->ppd->DefaultResolution == 0)
681         pi->ppd->DefaultResolution = 300;
682
683     if(using_default_devmode) {
684         DWORD papersize;
685
686         if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
687                           (LPWSTR)&papersize, sizeof(papersize)/sizeof(WCHAR))) {
688             PSDRV_DEVMODEA dm;
689             memset(&dm, 0, sizeof(dm));
690             dm.dmPublic.dmFields = DM_PAPERSIZE;
691             dm.dmPublic.u1.s1.dmPaperSize = papersize;
692             PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
693         }
694
695         SetPrinterDataExA(hPrinter, NULL, default_devmodeA, REG_BINARY,
696                             (LPBYTE)pi->Devmode, sizeof(DefaultDevmode));
697     }
698
699     if(pi->ppd->DefaultPageSize) { /* We'll let the ppd override the devmode */
700         PSDRV_DEVMODEA dm;
701         memset(&dm, 0, sizeof(dm));
702         dm.dmPublic.dmFields = DM_PAPERSIZE;
703         dm.dmPublic.u1.s1.dmPaperSize = pi->ppd->DefaultPageSize->WinPage;
704         PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
705     }
706
707     /*
708      *  This is a hack.  The default paper size should be read in as part of
709      *  the Devmode structure, but Wine doesn't currently provide a convenient
710      *  way to configure printers.
711      */
712     res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "Paper Size", NULL,
713                             (LPBYTE)&dwPaperSize, sizeof(DWORD), &needed);
714     if (res == ERROR_SUCCESS)
715         pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
716     else if (res == ERROR_FILE_NOT_FOUND)
717         TRACE ("No 'Paper Size' for printer '%s'\n", name);
718     else {
719         ERR ("GetPrinterDataA returned %i\n", res);
720         goto closeprinter;
721     }
722
723     /* Duplex is indicated by the setting of the DM_DUPLEX bit in dmFields.
724        WinDuplex == 0 is a special case which means that the ppd has a
725        *DefaultDuplex: NotCapable entry.  In this case we'll try not to confuse
726        apps and set dmDuplex to DMDUP_SIMPLEX but leave the DM_DUPLEX clear.
727        PSDRV_WriteHeader understands this and copes. */
728     pi->Devmode->dmPublic.dmFields &= ~DM_DUPLEX;
729     if(pi->ppd->DefaultDuplex) {
730         pi->Devmode->dmPublic.dmDuplex = pi->ppd->DefaultDuplex->WinDuplex;
731         if(pi->Devmode->dmPublic.dmDuplex != 0)
732             pi->Devmode->dmPublic.dmFields |= DM_DUPLEX;
733         else
734             pi->Devmode->dmPublic.dmDuplex = DMDUP_SIMPLEX;
735     }
736
737     res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable", NULL,
738             0, &needed, &pi->FontSubTableSize);
739     if (res == ERROR_SUCCESS || res == ERROR_FILE_NOT_FOUND) {
740         TRACE ("No 'FontSubTable' for printer '%s'\n", name);
741     }
742     else if (res == ERROR_MORE_DATA) {
743         pi->FontSubTable = HeapAlloc (PSDRV_Heap, 0, needed);
744         if (pi->FontSubTable == NULL) {
745             ERR ("Failed to allocate %i bytes from heap\n", needed);
746             goto closeprinter;
747         }
748
749         res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable",
750                 (LPBYTE) pi->FontSubTable, needed, &needed,
751                 &pi->FontSubTableSize);
752         if (res != ERROR_SUCCESS) {
753             ERR ("EnumPrinterDataExA returned %i\n", res);
754             goto closeprinter;
755         }
756     }
757     else {
758         ERR("EnumPrinterDataExA returned %i\n", res);
759         goto closeprinter;
760     }
761
762     if (ClosePrinter (hPrinter) == 0) {
763         ERR ("ClosePrinter failed with code %i\n", GetLastError ());
764         goto cleanup;
765     }
766
767     pi->next = NULL;
768     pi->Fonts = NULL;
769
770     for(font = pi->ppd->InstalledFonts; font; font = font->next) {
771         afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
772         if(!afm) {
773             TRACE( "Couldn't find AFM file for installed printer font '%s' - "
774                     "ignoring\n", font->Name);
775         }
776         else {
777             BOOL added;
778             if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
779                 PSDRV_FreeAFMList(pi->Fonts);
780                 goto cleanup;
781             }
782         }
783
784     }
785     if (ppd) unlink(ppd);
786     return pi;
787
788 closeprinter:
789     ClosePrinter(hPrinter);
790 cleanup:
791     HeapFree(PSDRV_Heap, 0, ppdFileName);
792     HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
793     HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
794     HeapFree(PSDRV_Heap, 0, pi->Devmode);
795 fail:
796     HeapFree(PSDRV_Heap, 0, pi);
797     if (ppd) unlink(ppd);
798     *last = NULL;
799     return NULL;
800 }
801
802
803 static const struct gdi_dc_funcs psdrv_funcs =
804 {
805     NULL,                               /* pAbortDoc */
806     NULL,                               /* pAbortPath */
807     NULL,                               /* pAlphaBlend */
808     NULL,                               /* pAngleArc */
809     PSDRV_Arc,                          /* pArc */
810     NULL,                               /* pArcTo */
811     NULL,                               /* pBeginPath */
812     NULL,                               /* pChoosePixelFormat */
813     PSDRV_Chord,                        /* pChord */
814     NULL,                               /* pCloseFigure */
815     NULL,                               /* pCreateBitmap */
816     PSDRV_CreateDC,                     /* pCreateDC */
817     NULL,                               /* pCreateDIBSection */
818     NULL,                               /* pDeleteBitmap */
819     PSDRV_DeleteDC,                     /* pDeleteDC */
820     NULL,                               /* pDeleteObject */
821     NULL,                               /* pDescribePixelFormat */
822     PSDRV_DeviceCapabilities,           /* pDeviceCapabilities */
823     PSDRV_Ellipse,                      /* pEllipse */
824     PSDRV_EndDoc,                       /* pEndDoc */
825     PSDRV_EndPage,                      /* pEndPage */
826     NULL,                               /* pEndPath */
827     PSDRV_EnumDeviceFonts,              /* pEnumDeviceFonts */
828     NULL,                               /* pEnumICMProfiles */
829     NULL,                               /* pExcludeClipRect */
830     PSDRV_ExtDeviceMode,                /* pExtDeviceMode */
831     PSDRV_ExtEscape,                    /* pExtEscape */
832     NULL,                               /* pExtFloodFill */
833     NULL,                               /* pExtSelectClipRgn */
834     PSDRV_ExtTextOut,                   /* pExtTextOut */
835     NULL,                               /* pFillPath */
836     NULL,                               /* pFillRgn */
837     NULL,                               /* pFlattenPath */
838     NULL,                               /* pFrameRgn */
839     NULL,                               /* pGdiComment */
840     NULL,                               /* pGetBitmapBits */
841     PSDRV_GetCharWidth,                 /* pGetCharWidth */
842     NULL,                               /* pGetDIBits */
843     PSDRV_GetDeviceCaps,                /* pGetDeviceCaps */
844     NULL,                               /* pGetDeviceGammaRamp */
845     NULL,                               /* pGetICMProfile */
846     NULL,                               /* pGetImage */
847     NULL,                               /* pGetNearestColor */
848     NULL,                               /* pGetPixel */
849     NULL,                               /* pGetPixelFormat */
850     NULL,                               /* pGetSystemPaletteEntries */
851     PSDRV_GetTextExtentExPoint,         /* pGetTextExtentExPoint */
852     PSDRV_GetTextMetrics,               /* pGetTextMetrics */
853     NULL,                               /* pIntersectClipRect */
854     NULL,                               /* pInvertRgn */
855     PSDRV_LineTo,                       /* pLineTo */
856     NULL,                               /* pModifyWorldTransform */
857     NULL,                               /* pMoveTo */
858     NULL,                               /* pOffsetClipRgn */
859     NULL,                               /* pOffsetViewportOrg */
860     NULL,                               /* pOffsetWindowOrg */
861     PSDRV_PaintRgn,                     /* pPaintRgn */
862     PSDRV_PatBlt,                       /* pPatBlt */
863     PSDRV_Pie,                          /* pPie */
864     NULL,                               /* pPolyBezier */
865     NULL,                               /* pPolyBezierTo */
866     NULL,                               /* pPolyDraw */
867     PSDRV_PolyPolygon,                  /* pPolyPolygon */
868     PSDRV_PolyPolyline,                 /* pPolyPolyline */
869     PSDRV_Polygon,                      /* pPolygon */
870     PSDRV_Polyline,                     /* pPolyline */
871     NULL,                               /* pPolylineTo */
872     NULL,                               /* pPutImage */
873     NULL,                               /* pRealizeDefaultPalette */
874     NULL,                               /* pRealizePalette */
875     PSDRV_Rectangle,                    /* pRectangle */
876     PSDRV_ResetDC,                      /* pResetDC */
877     NULL,                               /* pRestoreDC */
878     PSDRV_RoundRect,                    /* pRoundRect */
879     NULL,                               /* pSaveDC */
880     NULL,                               /* pScaleViewportExt */
881     NULL,                               /* pScaleWindowExt */
882     NULL,                               /* pSelectBitmap */
883     PSDRV_SelectBrush,                  /* pSelectBrush */
884     NULL,                               /* pSelectClipPath */
885     PSDRV_SelectFont,                   /* pSelectFont */
886     NULL,                               /* pSelectPalette */
887     PSDRV_SelectPen,                    /* pSelectPen */
888     NULL,                               /* pSetArcDirection */
889     NULL,                               /* pSetBitmapBits */
890     PSDRV_SetBkColor,                   /* pSetBkColor */
891     NULL,                               /* pSetBkMode */
892     PSDRV_SetDCBrushColor,              /* pSetDCBrushColor */
893     PSDRV_SetDCPenColor,                /* pSetDCPenColor */
894     NULL,                               /* pSetDIBColorTable */
895     NULL,                               /* pSetDIBits */
896     NULL,                               /* pSetDIBitsToDevice */
897     NULL,                               /* pSetDeviceClipping */
898     NULL,                               /* pSetDeviceGammaRamp */
899     NULL,                               /* pSetLayout */
900     NULL,                               /* pSetMapMode */
901     NULL,                               /* pSetMapperFlags */
902     PSDRV_SetPixel,                     /* pSetPixel */
903     NULL,                               /* pSetPixelFormat */
904     NULL,                               /* pSetPolyFillMode */
905     NULL,                               /* pSetROP2 */
906     NULL,                               /* pSetRelAbs */
907     NULL,                               /* pSetStretchBltMode */
908     NULL,                               /* pSetTextAlign */
909     NULL,                               /* pSetTextCharacterExtra */
910     PSDRV_SetTextColor,                 /* pSetTextColor */
911     NULL,                               /* pSetTextJustification */
912     NULL,                               /* pSetViewportExt */
913     NULL,                               /* pSetViewportOrg */
914     NULL,                               /* pSetWindowExt */
915     NULL,                               /* pSetWindowOrg */
916     NULL,                               /* pSetWorldTransform */
917     PSDRV_StartDoc,                     /* pStartDoc */
918     PSDRV_StartPage,                    /* pStartPage */
919     NULL,                               /* pStretchBlt */
920     PSDRV_StretchDIBits,                /* pStretchDIBits */
921     NULL,                               /* pStrokeAndFillPath */
922     NULL,                               /* pStrokePath */
923     NULL,                               /* pSwapBuffers */
924     NULL,                               /* pUnrealizePalette */
925     NULL,                               /* pWidenPath */
926     /* OpenGL not supported */
927 };
928
929
930 /******************************************************************************
931  *      PSDRV_get_gdi_driver
932  */
933 const struct gdi_dc_funcs * CDECL PSDRV_get_gdi_driver( unsigned int version )
934 {
935     if (version != WINE_GDI_DRIVER_VERSION)
936     {
937         ERR( "version mismatch, gdi32 wants %u but wineps has %u\n", version, WINE_GDI_DRIVER_VERSION );
938         return NULL;
939     }
940     return &psdrv_funcs;
941 }