Various background processes must be launched detached from current console (if any).
[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
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
34 #include "winreg.h"
35 #include "winnls.h"
36 #include "psdrv.h"
37 #include "winspool.h"
38 #include "wine/library.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
42
43 static const PSDRV_DEVMODE DefaultDevmode =
44 {
45   { /* dmPublic */
46 /* dmDeviceName */      {'W','i','n','e',' ','P','o','s','t','S','c','r','i','p','t',' ','D','r','i','v','e','r',0},
47 /* dmSpecVersion */     0x30a,
48 /* dmDriverVersion */   0x001,
49 /* dmSize */            sizeof(DEVMODEW),
50 /* dmDriverExtra */     sizeof(PSDRV_DEVMODE)-sizeof(DEVMODEW),
51 /* dmFields */          DM_ORIENTATION | DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH |
52                         DM_SCALE | DM_COPIES | DM_DEFAULTSOURCE | DM_PRINTQUALITY |
53                         DM_COLOR | DM_DUPLEX | DM_YRESOLUTION | DM_TTOPTION |
54                         DM_COLLATE | DM_FORMNAME,
55    { /* u1 */
56      { /* s1 */
57 /* dmOrientation */     DMORIENT_PORTRAIT,
58 /* dmPaperSize */       DMPAPER_LETTER,
59 /* dmPaperLength */     2794,
60 /* dmPaperWidth */      2159,
61 /* dmScale */           100,
62 /* dmCopies */          1,
63 /* dmDefaultSource */   DMBIN_AUTO,
64 /* dmPrintQuality */    300
65      }
66    },
67 /* dmColor */           DMCOLOR_COLOR,
68 /* dmDuplex */          DMDUP_SIMPLEX,
69 /* dmYResolution */     300,
70 /* dmTTOption */        DMTT_SUBDEV,
71 /* dmCollate */         DMCOLLATE_FALSE,
72 /* dmFormName */        {'L','e','t','t','e','r',0},
73 /* dmLogPixels */       0,
74 /* dmBitsPerPel */      0,
75 /* dmPelsWidth */       0,
76 /* dmPelsHeight */      0,
77    { /* u2 */
78 /* dmDisplayFlags */    0
79    },
80 /* dmDisplayFrequency */ 0,
81 /* dmICMMethod */       0,
82 /* dmICMIntent */       0,
83 /* dmMediaType */       0,
84 /* dmDitherType */      0,
85 /* dmReserved1 */       0,
86 /* dmReserved2 */       0,
87 /* dmPanningWidth */    0,
88 /* dmPanningHeight */   0
89   },
90   { /* dmDocPrivate */
91     /* dummy */ 0
92   },
93   { /* dmDrvPrivate */
94     /* numInstalledOptions */ 0
95   }
96 };
97
98 HINSTANCE PSDRV_hInstance = 0;
99 HANDLE PSDRV_Heap = 0;
100
101 static HFONT PSDRV_DefaultFont = 0;
102 static const LOGFONTA DefaultLogFont = {
103     100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
104     DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
105 };
106
107 static const struct gdi_dc_funcs psdrv_funcs;
108
109 /*********************************************************************
110  *           DllMain
111  *
112  * Initializes font metrics and registers driver. wineps dll entry point.
113  *
114  */
115 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
116 {
117     TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
118
119     switch(reason) {
120
121         case DLL_PROCESS_ATTACH:
122             PSDRV_hInstance = hinst;
123             DisableThreadLibraryCalls(hinst);
124
125             PSDRV_Heap = HeapCreate(0, 0x10000, 0);
126             if (PSDRV_Heap == NULL)
127                 return FALSE;
128
129             if (PSDRV_GetFontMetrics() == FALSE) {
130                 HeapDestroy(PSDRV_Heap);
131                 return FALSE;
132             }
133
134             PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
135             if (PSDRV_DefaultFont == NULL) {
136                 HeapDestroy(PSDRV_Heap);
137                 return FALSE;
138             }
139             break;
140
141         case DLL_PROCESS_DETACH:
142
143             DeleteObject( PSDRV_DefaultFont );
144             HeapDestroy( PSDRV_Heap );
145             break;
146     }
147
148     return TRUE;
149 }
150
151 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
152 {
153     PAGESIZE *page;
154     RESOLUTION *res;
155     INT width = 0, height = 0, resx = 0, resy = 0;
156
157     if (physDev->Devmode->dmPublic.dmFields & (DM_PRINTQUALITY | DM_YRESOLUTION | DM_LOGPIXELS))
158     {
159         if (physDev->Devmode->dmPublic.dmFields & DM_PRINTQUALITY)
160             resx = resy = physDev->Devmode->dmPublic.u1.s1.dmPrintQuality;
161
162         if (physDev->Devmode->dmPublic.dmFields & DM_YRESOLUTION)
163             resy = physDev->Devmode->dmPublic.dmYResolution;
164
165         if (physDev->Devmode->dmPublic.dmFields & DM_LOGPIXELS)
166             resx = resy = physDev->Devmode->dmPublic.dmLogPixels;
167
168         LIST_FOR_EACH_ENTRY(res, &physDev->pi->ppd->Resolutions, RESOLUTION, entry)
169         {
170             if (res->resx == resx && res->resy == resy)
171             {
172                 physDev->logPixelsX = resx;
173                 physDev->logPixelsY = resy;
174                 break;
175             }
176         }
177
178         if (&res->entry == &physDev->pi->ppd->Resolutions)
179         {
180             WARN("Requested resolution %dx%d is not supported by device\n", resx, resy);
181             physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
182             physDev->logPixelsY = physDev->logPixelsX;
183         }
184     }
185     else
186     {
187         WARN("Using default device resolution %d\n", physDev->pi->ppd->DefaultResolution);
188         physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
189         physDev->logPixelsY = physDev->logPixelsX;
190     }
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 static PSDRV_PDEVICE *create_psdrv_physdev( PRINTERINFO *pi )
275 {
276     PSDRV_PDEVICE *physDev;
277
278     physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) );
279     if (!physDev) return NULL;
280
281     physDev->Devmode = HeapAlloc( GetProcessHeap(), 0, sizeof(PSDRV_DEVMODE) );
282     if (!physDev->Devmode)
283     {
284         HeapFree( GetProcessHeap(), 0, physDev );
285         return NULL;
286     }
287
288     *physDev->Devmode = *pi->Devmode;
289     physDev->pi = pi;
290     physDev->logPixelsX = pi->ppd->DefaultResolution;
291     physDev->logPixelsY = pi->ppd->DefaultResolution;
292     return physDev;
293 }
294
295 /**********************************************************************
296  *           PSDRV_CreateDC
297  */
298 static BOOL PSDRV_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
299                             LPCWSTR output, const DEVMODEW* initData )
300 {
301     PSDRV_PDEVICE *physDev;
302     PRINTERINFO *pi;
303
304     TRACE("(%s %s %s %p)\n", debugstr_w(driver), debugstr_w(device),
305                              debugstr_w(output), initData);
306
307     if (!device) return FALSE;
308     pi = PSDRV_FindPrinterInfo( device );
309     if(!pi) return FALSE;
310
311     if(!pi->Fonts) {
312         RASTERIZER_STATUS status;
313         if(!GetRasterizerCaps(&status, sizeof(status)) ||
314            !(status.wFlags & TT_AVAILABLE) ||
315            !(status.wFlags & TT_ENABLED)) {
316             MESSAGE("Disabling printer %s since it has no builtin fonts and there are no TrueType fonts available.\n",
317                     debugstr_w(device));
318             return FALSE;
319         }
320     }
321
322     if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
323
324     if (output && *output) physDev->job.output = strdupW( output );
325
326     if(initData)
327         PSDRV_MergeDevmodes(physDev->Devmode, (const PSDRV_DEVMODE *)initData, pi);
328
329     PSDRV_UpdateDevCaps(physDev);
330     SelectObject( (*pdev)->hdc, PSDRV_DefaultFont );
331     push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
332     return TRUE;
333 }
334
335
336 /**********************************************************************
337  *           PSDRV_CreateCompatibleDC
338  */
339 static BOOL PSDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
340 {
341     HDC hdc = (*pdev)->hdc;
342     PSDRV_PDEVICE *physDev, *orig_dev = get_psdrv_dev( orig );
343     PRINTERINFO *pi = PSDRV_FindPrinterInfo( orig_dev->pi->friendly_name );
344
345     if (!pi) return FALSE;
346     if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
347     PSDRV_MergeDevmodes( physDev->Devmode, orig_dev->Devmode, pi );
348     PSDRV_UpdateDevCaps(physDev);
349     SelectObject( hdc, PSDRV_DefaultFont );
350     push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
351     return TRUE;
352 }
353
354
355
356 /**********************************************************************
357  *           PSDRV_DeleteDC
358  */
359 static BOOL PSDRV_DeleteDC( PHYSDEV dev )
360 {
361     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
362
363     TRACE("\n");
364
365     HeapFree( GetProcessHeap(), 0, physDev->Devmode );
366     HeapFree( GetProcessHeap(), 0, physDev->job.output );
367     HeapFree( GetProcessHeap(), 0, physDev );
368
369     return TRUE;
370 }
371
372
373 /**********************************************************************
374  *           ResetDC   (WINEPS.@)
375  */
376 static HDC PSDRV_ResetDC( PHYSDEV dev, const DEVMODEW *lpInitData )
377 {
378     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
379
380     if (lpInitData)
381     {
382         PSDRV_MergeDevmodes(physDev->Devmode, (const PSDRV_DEVMODE *)lpInitData, physDev->pi);
383         PSDRV_UpdateDevCaps(physDev);
384     }
385     return dev->hdc;
386 }
387
388 /***********************************************************************
389  *           GetDeviceCaps    (WINEPS.@)
390  */
391 static INT PSDRV_GetDeviceCaps( PHYSDEV dev, INT cap )
392 {
393     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
394
395     TRACE("%p,%d\n", dev->hdc, cap);
396
397     switch(cap)
398     {
399     case DRIVERVERSION:
400         return 0;
401     case TECHNOLOGY:
402         return DT_RASPRINTER;
403     case HORZSIZE:
404         return MulDiv(physDev->horzSize, 100,
405                       physDev->Devmode->dmPublic.u1.s1.dmScale);
406     case VERTSIZE:
407         return MulDiv(physDev->vertSize, 100,
408                       physDev->Devmode->dmPublic.u1.s1.dmScale);
409     case HORZRES:
410     case DESKTOPHORZRES:
411         return physDev->horzRes;
412     case VERTRES:
413     case DESKTOPVERTRES:
414         return physDev->vertRes;
415     case BITSPIXEL:
416         return (physDev->pi->ppd->ColorDevice != CD_False) ? 32 : 1;
417     case PLANES:
418         return 1;
419     case NUMBRUSHES:
420         return -1;
421     case NUMPENS:
422         return 10;
423     case NUMMARKERS:
424         return 0;
425     case NUMFONTS:
426         return 39;
427     case NUMCOLORS:
428         return -1;
429     case PDEVICESIZE:
430         return sizeof(PSDRV_PDEVICE);
431     case CURVECAPS:
432         return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
433                 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
434     case LINECAPS:
435         return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
436                 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
437     case POLYGONALCAPS:
438         return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
439                 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
440     case TEXTCAPS:
441         return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
442     case CLIPCAPS:
443         return CP_RECTANGLE;
444     case RASTERCAPS:
445         return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
446                 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
447     case ASPECTX:
448         return physDev->logPixelsX;
449     case ASPECTY:
450         return physDev->logPixelsY;
451     case ASPECTXY:
452         return (int)hypot( (double)physDev->logPixelsX,
453                            (double)physDev->logPixelsY );
454     case LOGPIXELSX:
455         return MulDiv(physDev->logPixelsX,
456                       physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
457     case LOGPIXELSY:
458         return MulDiv(physDev->logPixelsY,
459                       physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
460     case SIZEPALETTE:
461         return 0;
462     case NUMRESERVED:
463         return 0;
464     case COLORRES:
465         return 0;
466     case PHYSICALWIDTH:
467         return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
468           physDev->PageSize.cy : physDev->PageSize.cx;
469     case PHYSICALHEIGHT:
470         return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
471           physDev->PageSize.cx : physDev->PageSize.cy;
472     case PHYSICALOFFSETX:
473       if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
474           if(physDev->pi->ppd->LandscapeOrientation == -90)
475               return physDev->PageSize.cy - physDev->ImageableArea.top;
476           else
477               return physDev->ImageableArea.bottom;
478       }
479       return physDev->ImageableArea.left;
480
481     case PHYSICALOFFSETY:
482       if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
483           if(physDev->pi->ppd->LandscapeOrientation == -90)
484               return physDev->PageSize.cx - physDev->ImageableArea.right;
485           else
486               return physDev->ImageableArea.left;
487       }
488       return physDev->PageSize.cy - physDev->ImageableArea.top;
489
490     case SCALINGFACTORX:
491     case SCALINGFACTORY:
492     case VREFRESH:
493     case BLTALIGNMENT:
494         return 0;
495     case SHADEBLENDCAPS:
496         return SB_NONE;
497     default:
498         FIXME("(%p): unsupported capability %d, will return 0\n", dev->hdc, cap );
499         return 0;
500     }
501 }
502
503 static PRINTER_ENUM_VALUESA *load_font_sub_table( HANDLE printer, DWORD *num_entries )
504 {
505     DWORD res, needed, num;
506     PRINTER_ENUM_VALUESA *table = NULL;
507     static const char fontsubkey[] = "PrinterDriverData\\FontSubTable";
508
509     *num_entries = 0;
510
511     res = EnumPrinterDataExA( printer, fontsubkey, NULL, 0, &needed, &num );
512     if (res != ERROR_MORE_DATA) return NULL;
513
514     table = HeapAlloc( PSDRV_Heap, 0, needed );
515     if (!table) return NULL;
516
517     res = EnumPrinterDataExA( printer, fontsubkey, (LPBYTE)table, needed, &needed, &num );
518     if (res != ERROR_SUCCESS)
519     {
520         HeapFree( PSDRV_Heap, 0, table );
521         return NULL;
522     }
523
524     *num_entries = num;
525     return table;
526 }
527
528 static PSDRV_DEVMODE *get_printer_devmode( HANDLE printer )
529 {
530     DWORD needed, dm_size;
531     BOOL res;
532     PRINTER_INFO_9W *info;
533     PSDRV_DEVMODE *dm;
534
535     GetPrinterW( printer, 9, NULL, 0, &needed );
536     if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return NULL;
537
538     info = HeapAlloc( PSDRV_Heap, 0, needed );
539     res = GetPrinterW( printer, 9, (BYTE *)info, needed, &needed );
540     if (!res || !info->pDevMode)
541     {
542         HeapFree( PSDRV_Heap, 0, info );
543         return NULL;
544     }
545
546     /* sanity check the sizes */
547     dm_size = info->pDevMode->dmSize + info->pDevMode->dmDriverExtra;
548     if ((char *)info->pDevMode - (char *)info + dm_size > needed)
549     {
550         HeapFree( PSDRV_Heap, 0, info );
551         return NULL;
552     }
553
554     dm = (PSDRV_DEVMODE*)info;
555     memmove( dm, info->pDevMode, dm_size );
556     return dm;
557 }
558
559 static PSDRV_DEVMODE *get_devmode( HANDLE printer, const WCHAR *name, BOOL *is_default )
560 {
561     PSDRV_DEVMODE *dm = get_printer_devmode( printer );
562
563     *is_default = FALSE;
564
565     if (dm && dm->dmPublic.dmSize + dm->dmPublic.dmDriverExtra >= sizeof(DefaultDevmode))
566     {
567         TRACE( "Retrieved devmode from winspool\n" );
568         return dm;
569     }
570     HeapFree( PSDRV_Heap, 0, dm );
571
572     TRACE( "Using default devmode\n" );
573     dm = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
574     if (dm)
575     {
576         *dm = DefaultDevmode;
577         lstrcpynW( (WCHAR *)dm->dmPublic.dmDeviceName, name, CCHDEVICENAME );
578         *is_default = TRUE;
579     }
580     return dm;
581 }
582
583 static BOOL set_devmode( HANDLE printer, PSDRV_DEVMODE *dm )
584 {
585     PRINTER_INFO_9W info;
586     info.pDevMode = &dm->dmPublic;
587
588     return SetPrinterW( printer, 9, (BYTE *)&info, 0 );
589 }
590
591 static char *get_ppd_filename( HANDLE printer )
592 {
593     DWORD needed;
594     DRIVER_INFO_2W *info;
595     char *unixname;
596
597     GetPrinterDriverW( printer, NULL, 2, NULL, 0, &needed );
598     if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return NULL;
599     info = HeapAlloc( GetProcessHeap(), 0, needed );
600     if (!info) return NULL;
601     GetPrinterDriverW( printer, NULL, 2, (BYTE*)info, needed, &needed );
602     unixname = wine_get_unix_file_name( info->pDataFile );
603     HeapFree( GetProcessHeap(), 0, info );
604
605     return unixname;
606 }
607
608 static struct list printer_list = LIST_INIT( printer_list );
609
610 /**********************************************************************
611  *              PSDRV_FindPrinterInfo
612  */
613 PRINTERINFO *PSDRV_FindPrinterInfo(LPCWSTR name)
614 {
615     PRINTERINFO *pi;
616     FONTNAME *font;
617     const AFM *afm;
618     HANDLE hPrinter = 0;
619     char *ppd_filename = NULL, *nameA = NULL;
620     BOOL using_default_devmode = FALSE;
621     int len;
622
623     TRACE("'%s'\n", debugstr_w(name));
624
625     LIST_FOR_EACH_ENTRY( pi, &printer_list, PRINTERINFO, entry )
626     {
627         if (!strcmpW( pi->friendly_name, name ))
628             return pi;
629     }
630
631     pi = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
632     if (pi == NULL) return NULL;
633
634     if (!(pi->friendly_name = HeapAlloc( PSDRV_Heap, 0, (strlenW(name)+1)*sizeof(WCHAR) ))) goto fail;
635     strcpyW( pi->friendly_name, name );
636
637     if (OpenPrinterW( pi->friendly_name, &hPrinter, NULL ) == 0) {
638         ERR ("OpenPrinter failed with code %i\n", GetLastError ());
639         goto fail;
640     }
641
642     len = WideCharToMultiByte( CP_ACP, 0, name, -1, NULL, 0, NULL, NULL );
643     nameA = HeapAlloc( GetProcessHeap(), 0, len );
644     WideCharToMultiByte( CP_ACP, 0, name, -1, nameA, len, NULL, NULL );
645
646     pi->Devmode = get_devmode( hPrinter, name, &using_default_devmode );
647     if (!pi->Devmode) goto fail;
648
649     ppd_filename = get_ppd_filename( hPrinter );
650     if (!ppd_filename) goto fail;
651
652     pi->ppd = PSDRV_ParsePPD( ppd_filename, hPrinter );
653     if (!pi->ppd)
654     {
655         WARN( "Couldn't parse PPD file '%s'\n", ppd_filename );
656         goto fail;
657     }
658
659     if(using_default_devmode) {
660         DWORD papersize;
661
662         if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
663                           (LPWSTR)&papersize, sizeof(papersize)/sizeof(WCHAR))) {
664             PSDRV_DEVMODE dm;
665             memset(&dm, 0, sizeof(dm));
666             dm.dmPublic.dmFields = DM_PAPERSIZE;
667             dm.dmPublic.u1.s1.dmPaperSize = papersize;
668             PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
669         }
670     }
671
672     if(pi->ppd->DefaultPageSize) { /* We'll let the ppd override the devmode */
673         PSDRV_DEVMODE dm;
674         memset(&dm, 0, sizeof(dm));
675         dm.dmPublic.dmFields = DM_PAPERSIZE;
676         dm.dmPublic.u1.s1.dmPaperSize = pi->ppd->DefaultPageSize->WinPage;
677         PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
678     }
679
680     /* Duplex is indicated by the setting of the DM_DUPLEX bit in dmFields.
681        WinDuplex == 0 is a special case which means that the ppd has a
682        *DefaultDuplex: NotCapable entry.  In this case we'll try not to confuse
683        apps and set dmDuplex to DMDUP_SIMPLEX but leave the DM_DUPLEX clear.
684        PSDRV_WriteHeader understands this and copes. */
685     pi->Devmode->dmPublic.dmFields &= ~DM_DUPLEX;
686     if(pi->ppd->DefaultDuplex) {
687         pi->Devmode->dmPublic.dmDuplex = pi->ppd->DefaultDuplex->WinDuplex;
688         if(pi->Devmode->dmPublic.dmDuplex != 0)
689             pi->Devmode->dmPublic.dmFields |= DM_DUPLEX;
690         else
691             pi->Devmode->dmPublic.dmDuplex = DMDUP_SIMPLEX;
692     }
693
694     set_devmode( hPrinter, pi->Devmode );
695
696     pi->FontSubTable = load_font_sub_table( hPrinter, &pi->FontSubTableSize );
697
698     LIST_FOR_EACH_ENTRY( font, &pi->ppd->InstalledFonts, FONTNAME, entry )
699     {
700         afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
701         if(!afm) {
702             TRACE( "Couldn't find AFM file for installed printer font '%s' - "
703                     "ignoring\n", font->Name);
704         }
705         else {
706             BOOL added;
707             if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
708                 PSDRV_FreeAFMList(pi->Fonts);
709                 goto fail;
710             }
711         }
712
713     }
714     ClosePrinter( hPrinter );
715     HeapFree( GetProcessHeap(), 0, nameA );
716     HeapFree( GetProcessHeap(), 0, ppd_filename );
717     list_add_head( &printer_list, &pi->entry );
718     return pi;
719
720 fail:
721     if (hPrinter) ClosePrinter( hPrinter );
722     HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
723     HeapFree(PSDRV_Heap, 0, pi->friendly_name);
724     HeapFree(PSDRV_Heap, 0, pi->Devmode);
725     HeapFree(PSDRV_Heap, 0, pi);
726     HeapFree( GetProcessHeap(), 0, nameA );
727     HeapFree( GetProcessHeap(), 0, ppd_filename );
728     return NULL;
729 }
730
731
732 static const struct gdi_dc_funcs psdrv_funcs =
733 {
734     NULL,                               /* pAbortDoc */
735     NULL,                               /* pAbortPath */
736     NULL,                               /* pAlphaBlend */
737     NULL,                               /* pAngleArc */
738     PSDRV_Arc,                          /* pArc */
739     NULL,                               /* pArcTo */
740     NULL,                               /* pBeginPath */
741     NULL,                               /* pBlendImage */
742     PSDRV_Chord,                        /* pChord */
743     NULL,                               /* pCloseFigure */
744     PSDRV_CreateCompatibleDC,           /* pCreateCompatibleDC */
745     PSDRV_CreateDC,                     /* pCreateDC */
746     PSDRV_DeleteDC,                     /* pDeleteDC */
747     NULL,                               /* pDeleteObject */
748     PSDRV_DeviceCapabilities,           /* pDeviceCapabilities */
749     PSDRV_Ellipse,                      /* pEllipse */
750     PSDRV_EndDoc,                       /* pEndDoc */
751     PSDRV_EndPage,                      /* pEndPage */
752     NULL,                               /* pEndPath */
753     PSDRV_EnumFonts,                    /* pEnumFonts */
754     NULL,                               /* pEnumICMProfiles */
755     NULL,                               /* pExcludeClipRect */
756     PSDRV_ExtDeviceMode,                /* pExtDeviceMode */
757     PSDRV_ExtEscape,                    /* pExtEscape */
758     NULL,                               /* pExtFloodFill */
759     NULL,                               /* pExtSelectClipRgn */
760     PSDRV_ExtTextOut,                   /* pExtTextOut */
761     PSDRV_FillPath,                     /* pFillPath */
762     NULL,                               /* pFillRgn */
763     NULL,                               /* pFlattenPath */
764     NULL,                               /* pFontIsLinked */
765     NULL,                               /* pFrameRgn */
766     NULL,                               /* pGdiComment */
767     NULL,                               /* pGdiRealizationInfo */
768     NULL,                               /* pGetBoundsRect */
769     NULL,                               /* pGetCharABCWidths */
770     NULL,                               /* pGetCharABCWidthsI */
771     PSDRV_GetCharWidth,                 /* pGetCharWidth */
772     PSDRV_GetDeviceCaps,                /* pGetDeviceCaps */
773     NULL,                               /* pGetDeviceGammaRamp */
774     NULL,                               /* pGetFontData */
775     NULL,                               /* pGetFontUnicodeRanges */
776     NULL,                               /* pGetGlyphIndices */
777     NULL,                               /* pGetGlyphOutline */
778     NULL,                               /* pGetICMProfile */
779     NULL,                               /* pGetImage */
780     NULL,                               /* pGetKerningPairs */
781     NULL,                               /* pGetNearestColor */
782     NULL,                               /* pGetOutlineTextMetrics */
783     NULL,                               /* pGetPixel */
784     NULL,                               /* pGetSystemPaletteEntries */
785     NULL,                               /* pGetTextCharsetInfo */
786     PSDRV_GetTextExtentExPoint,         /* pGetTextExtentExPoint */
787     NULL,                               /* pGetTextExtentExPointI */
788     NULL,                               /* pGetTextFace */
789     PSDRV_GetTextMetrics,               /* pGetTextMetrics */
790     NULL,                               /* pGradientFill */
791     NULL,                               /* pIntersectClipRect */
792     NULL,                               /* pInvertRgn */
793     PSDRV_LineTo,                       /* pLineTo */
794     NULL,                               /* pModifyWorldTransform */
795     NULL,                               /* pMoveTo */
796     NULL,                               /* pOffsetClipRgn */
797     NULL,                               /* pOffsetViewportOrg */
798     NULL,                               /* pOffsetWindowOrg */
799     PSDRV_PaintRgn,                     /* pPaintRgn */
800     PSDRV_PatBlt,                       /* pPatBlt */
801     PSDRV_Pie,                          /* pPie */
802     PSDRV_PolyBezier,                   /* pPolyBezier */
803     PSDRV_PolyBezierTo,                 /* pPolyBezierTo */
804     NULL,                               /* pPolyDraw */
805     PSDRV_PolyPolygon,                  /* pPolyPolygon */
806     PSDRV_PolyPolyline,                 /* pPolyPolyline */
807     NULL,                               /* pPolygon */
808     NULL,                               /* pPolyline */
809     NULL,                               /* pPolylineTo */
810     PSDRV_PutImage,                     /* pPutImage */
811     NULL,                               /* pRealizeDefaultPalette */
812     NULL,                               /* pRealizePalette */
813     PSDRV_Rectangle,                    /* pRectangle */
814     PSDRV_ResetDC,                      /* pResetDC */
815     NULL,                               /* pRestoreDC */
816     PSDRV_RoundRect,                    /* pRoundRect */
817     NULL,                               /* pSaveDC */
818     NULL,                               /* pScaleViewportExt */
819     NULL,                               /* pScaleWindowExt */
820     NULL,                               /* pSelectBitmap */
821     PSDRV_SelectBrush,                  /* pSelectBrush */
822     NULL,                               /* pSelectClipPath */
823     PSDRV_SelectFont,                   /* pSelectFont */
824     NULL,                               /* pSelectPalette */
825     PSDRV_SelectPen,                    /* pSelectPen */
826     NULL,                               /* pSetArcDirection */
827     PSDRV_SetBkColor,                   /* pSetBkColor */
828     NULL,                               /* pSetBkMode */
829     NULL,                               /* pSetBoundsRect */
830     PSDRV_SetDCBrushColor,              /* pSetDCBrushColor */
831     PSDRV_SetDCPenColor,                /* pSetDCPenColor */
832     NULL,                               /* pSetDIBitsToDevice */
833     NULL,                               /* pSetDeviceClipping */
834     NULL,                               /* pSetDeviceGammaRamp */
835     NULL,                               /* pSetLayout */
836     NULL,                               /* pSetMapMode */
837     NULL,                               /* pSetMapperFlags */
838     PSDRV_SetPixel,                     /* pSetPixel */
839     NULL,                               /* pSetPolyFillMode */
840     NULL,                               /* pSetROP2 */
841     NULL,                               /* pSetRelAbs */
842     NULL,                               /* pSetStretchBltMode */
843     NULL,                               /* pSetTextAlign */
844     NULL,                               /* pSetTextCharacterExtra */
845     PSDRV_SetTextColor,                 /* pSetTextColor */
846     NULL,                               /* pSetTextJustification */
847     NULL,                               /* pSetViewportExt */
848     NULL,                               /* pSetViewportOrg */
849     NULL,                               /* pSetWindowExt */
850     NULL,                               /* pSetWindowOrg */
851     NULL,                               /* pSetWorldTransform */
852     PSDRV_StartDoc,                     /* pStartDoc */
853     PSDRV_StartPage,                    /* pStartPage */
854     NULL,                               /* pStretchBlt */
855     NULL,                               /* pStretchDIBits */
856     PSDRV_StrokeAndFillPath,            /* pStrokeAndFillPath */
857     PSDRV_StrokePath,                   /* pStrokePath */
858     NULL,                               /* pUnrealizePalette */
859     NULL,                               /* pWidenPath */
860     NULL,                               /* wine_get_wgl_driver */
861     GDI_PRIORITY_GRAPHICS_DRV           /* priority */
862 };
863
864
865 /******************************************************************************
866  *      PSDRV_get_gdi_driver
867  */
868 const struct gdi_dc_funcs * CDECL PSDRV_get_gdi_driver( unsigned int version )
869 {
870     if (version != WINE_GDI_DRIVER_VERSION)
871     {
872         ERR( "version mismatch, gdi32 wants %u but wineps has %u\n", version, WINE_GDI_DRIVER_VERSION );
873         return NULL;
874     }
875     return &psdrv_funcs;
876 }