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