dmloader: COM cleanup of IDirectMusicLoader object.
[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     INT width = 0, height = 0;
155
156     if (physDev->Devmode->dmPublic.dmFields & DM_PRINTQUALITY)
157     {
158         physDev->logPixelsX = physDev->Devmode->dmPublic.u1.s1.dmPrintQuality;
159         physDev->logPixelsY = physDev->logPixelsX;
160     }
161     else
162     {
163         physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
164         physDev->logPixelsY = physDev->logPixelsX;
165     }
166     if (physDev->Devmode->dmPublic.dmFields & DM_YRESOLUTION)
167         physDev->logPixelsY = physDev->Devmode->dmPublic.dmYResolution;
168
169     if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
170         LIST_FOR_EACH_ENTRY(page, &physDev->pi->ppd->PageSizes, PAGESIZE, entry) {
171             if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
172                 break;
173         }
174
175         if(&page->entry == &physDev->pi->ppd->PageSizes) {
176             FIXME("Can't find page\n");
177             physDev->ImageableArea.left = 0;
178             physDev->ImageableArea.right = 0;
179             physDev->ImageableArea.bottom = 0;
180             physDev->ImageableArea.top = 0;
181             physDev->PageSize.cx = 0;
182             physDev->PageSize.cy = 0;
183         } else if(page->ImageableArea) {
184           /* physDev sizes in device units; ppd sizes in 1/72" */
185             physDev->ImageableArea.left = page->ImageableArea->llx *
186               physDev->logPixelsX / 72;
187             physDev->ImageableArea.right = page->ImageableArea->urx *
188               physDev->logPixelsX / 72;
189             physDev->ImageableArea.bottom = page->ImageableArea->lly *
190               physDev->logPixelsY / 72;
191             physDev->ImageableArea.top = page->ImageableArea->ury *
192               physDev->logPixelsY / 72;
193             physDev->PageSize.cx = page->PaperDimension->x *
194               physDev->logPixelsX / 72;
195             physDev->PageSize.cy = page->PaperDimension->y *
196               physDev->logPixelsY / 72;
197         } else {
198             physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
199             physDev->ImageableArea.right = physDev->PageSize.cx =
200               page->PaperDimension->x * physDev->logPixelsX / 72;
201             physDev->ImageableArea.top = physDev->PageSize.cy =
202               page->PaperDimension->y * physDev->logPixelsY / 72;
203         }
204     } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
205               (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
206       /* physDev sizes in device units; Devmode sizes in 1/10 mm */
207         physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
208         physDev->ImageableArea.right = physDev->PageSize.cx =
209           physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
210           physDev->logPixelsX / 254;
211         physDev->ImageableArea.top = physDev->PageSize.cy =
212           physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
213           physDev->logPixelsY / 254;
214     } else {
215         FIXME("Odd dmFields %x\n", physDev->Devmode->dmPublic.dmFields);
216         physDev->ImageableArea.left = 0;
217         physDev->ImageableArea.right = 0;
218         physDev->ImageableArea.bottom = 0;
219         physDev->ImageableArea.top = 0;
220         physDev->PageSize.cx = 0;
221         physDev->PageSize.cy = 0;
222     }
223
224     TRACE("ImageableArea = %d,%d - %d,%d: PageSize = %dx%d\n",
225           physDev->ImageableArea.left, physDev->ImageableArea.bottom,
226           physDev->ImageableArea.right, physDev->ImageableArea.top,
227           physDev->PageSize.cx, physDev->PageSize.cy);
228
229     /* these are in device units */
230     width = physDev->ImageableArea.right - physDev->ImageableArea.left;
231     height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
232
233     if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
234         physDev->horzRes = width;
235         physDev->vertRes = height;
236     } else {
237         physDev->horzRes = height;
238         physDev->vertRes = width;
239     }
240
241     /* these are in mm */
242     physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
243     physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
244
245     TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
246           "horzRes = %d, vertRes = %d\n",
247           physDev->horzSize, physDev->vertSize,
248           physDev->horzRes, physDev->vertRes);
249 }
250
251 static PSDRV_PDEVICE *create_psdrv_physdev( PRINTERINFO *pi )
252 {
253     PSDRV_PDEVICE *physDev;
254
255     physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) );
256     if (!physDev) return NULL;
257
258     physDev->Devmode = HeapAlloc( GetProcessHeap(), 0, sizeof(PSDRV_DEVMODE) );
259     if (!physDev->Devmode)
260     {
261         HeapFree( GetProcessHeap(), 0, physDev );
262         return NULL;
263     }
264
265     *physDev->Devmode = *pi->Devmode;
266     physDev->pi = pi;
267     physDev->logPixelsX = pi->ppd->DefaultResolution;
268     physDev->logPixelsY = pi->ppd->DefaultResolution;
269     return physDev;
270 }
271
272 /**********************************************************************
273  *           PSDRV_CreateDC
274  */
275 static BOOL PSDRV_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
276                             LPCWSTR output, const DEVMODEW* initData )
277 {
278     PSDRV_PDEVICE *physDev;
279     PRINTERINFO *pi;
280
281     TRACE("(%s %s %s %p)\n", debugstr_w(driver), debugstr_w(device),
282                              debugstr_w(output), initData);
283
284     if (!device) return FALSE;
285     pi = PSDRV_FindPrinterInfo( device );
286     if(!pi) return FALSE;
287
288     if(!pi->Fonts) {
289         RASTERIZER_STATUS status;
290         if(!GetRasterizerCaps(&status, sizeof(status)) ||
291            !(status.wFlags & TT_AVAILABLE) ||
292            !(status.wFlags & TT_ENABLED)) {
293             MESSAGE("Disabling printer %s since it has no builtin fonts and there are no TrueType fonts available.\n",
294                     debugstr_w(device));
295             return FALSE;
296         }
297     }
298
299     if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
300
301     if (output && *output) physDev->job.output = strdupW( output );
302
303     if(initData)
304         PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODE *)initData, pi);
305
306     PSDRV_UpdateDevCaps(physDev);
307     SelectObject( (*pdev)->hdc, PSDRV_DefaultFont );
308     push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
309     return TRUE;
310 }
311
312
313 /**********************************************************************
314  *           PSDRV_CreateCompatibleDC
315  */
316 static BOOL PSDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
317 {
318     HDC hdc = (*pdev)->hdc;
319     PSDRV_PDEVICE *physDev, *orig_dev = get_psdrv_dev( orig );
320     PRINTERINFO *pi = PSDRV_FindPrinterInfo( orig_dev->pi->friendly_name );
321
322     if (!pi) return FALSE;
323     if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
324     PSDRV_MergeDevmodes( physDev->Devmode, orig_dev->Devmode, pi );
325     PSDRV_UpdateDevCaps(physDev);
326     SelectObject( hdc, PSDRV_DefaultFont );
327     push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
328     return TRUE;
329 }
330
331
332
333 /**********************************************************************
334  *           PSDRV_DeleteDC
335  */
336 static BOOL PSDRV_DeleteDC( PHYSDEV dev )
337 {
338     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
339
340     TRACE("\n");
341
342     HeapFree( GetProcessHeap(), 0, physDev->Devmode );
343     HeapFree( GetProcessHeap(), 0, physDev->job.output );
344     HeapFree( GetProcessHeap(), 0, physDev );
345
346     return TRUE;
347 }
348
349
350 /**********************************************************************
351  *           ResetDC   (WINEPS.@)
352  */
353 static HDC PSDRV_ResetDC( PHYSDEV dev, const DEVMODEW *lpInitData )
354 {
355     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
356
357     if (lpInitData)
358     {
359         PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODE *)lpInitData, physDev->pi);
360         PSDRV_UpdateDevCaps(physDev);
361     }
362     return dev->hdc;
363 }
364
365 /***********************************************************************
366  *           GetDeviceCaps    (WINEPS.@)
367  */
368 static INT PSDRV_GetDeviceCaps( PHYSDEV dev, INT cap )
369 {
370     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
371
372     TRACE("%p,%d\n", dev->hdc, cap);
373
374     switch(cap)
375     {
376     case DRIVERVERSION:
377         return 0;
378     case TECHNOLOGY:
379         return DT_RASPRINTER;
380     case HORZSIZE:
381         return MulDiv(physDev->horzSize, 100,
382                       physDev->Devmode->dmPublic.u1.s1.dmScale);
383     case VERTSIZE:
384         return MulDiv(physDev->vertSize, 100,
385                       physDev->Devmode->dmPublic.u1.s1.dmScale);
386     case HORZRES:
387     case DESKTOPHORZRES:
388         return physDev->horzRes;
389     case VERTRES:
390     case DESKTOPVERTRES:
391         return physDev->vertRes;
392     case BITSPIXEL:
393         return (physDev->pi->ppd->ColorDevice != CD_False) ? 32 : 1;
394     case PLANES:
395         return 1;
396     case NUMBRUSHES:
397         return -1;
398     case NUMPENS:
399         return 10;
400     case NUMMARKERS:
401         return 0;
402     case NUMFONTS:
403         return 39;
404     case NUMCOLORS:
405         return -1;
406     case PDEVICESIZE:
407         return sizeof(PSDRV_PDEVICE);
408     case CURVECAPS:
409         return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
410                 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
411     case LINECAPS:
412         return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
413                 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
414     case POLYGONALCAPS:
415         return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
416                 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
417     case TEXTCAPS:
418         return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
419     case CLIPCAPS:
420         return CP_RECTANGLE;
421     case RASTERCAPS:
422         return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
423                 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
424     case ASPECTX:
425         return physDev->logPixelsX;
426     case ASPECTY:
427         return physDev->logPixelsY;
428     case ASPECTXY:
429         return (int)hypot( (double)physDev->logPixelsX,
430                            (double)physDev->logPixelsY );
431     case LOGPIXELSX:
432         return MulDiv(physDev->logPixelsX,
433                       physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
434     case LOGPIXELSY:
435         return MulDiv(physDev->logPixelsY,
436                       physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
437     case SIZEPALETTE:
438         return 0;
439     case NUMRESERVED:
440         return 0;
441     case COLORRES:
442         return 0;
443     case PHYSICALWIDTH:
444         return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
445           physDev->PageSize.cy : physDev->PageSize.cx;
446     case PHYSICALHEIGHT:
447         return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
448           physDev->PageSize.cx : physDev->PageSize.cy;
449     case PHYSICALOFFSETX:
450       if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
451           if(physDev->pi->ppd->LandscapeOrientation == -90)
452               return physDev->PageSize.cy - physDev->ImageableArea.top;
453           else
454               return physDev->ImageableArea.bottom;
455       }
456       return physDev->ImageableArea.left;
457
458     case PHYSICALOFFSETY:
459       if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
460           if(physDev->pi->ppd->LandscapeOrientation == -90)
461               return physDev->PageSize.cx - physDev->ImageableArea.right;
462           else
463               return physDev->ImageableArea.left;
464       }
465       return physDev->PageSize.cy - physDev->ImageableArea.top;
466
467     case SCALINGFACTORX:
468     case SCALINGFACTORY:
469     case VREFRESH:
470     case BLTALIGNMENT:
471         return 0;
472     case SHADEBLENDCAPS:
473         return SB_NONE;
474     default:
475         FIXME("(%p): unsupported capability %d, will return 0\n", dev->hdc, cap );
476         return 0;
477     }
478 }
479
480 static PRINTER_ENUM_VALUESA *load_font_sub_table( HANDLE printer, DWORD *num_entries )
481 {
482     DWORD res, needed, num;
483     PRINTER_ENUM_VALUESA *table = NULL;
484     static const char fontsubkey[] = "PrinterDriverData\\FontSubTable";
485
486     *num_entries = 0;
487
488     res = EnumPrinterDataExA( printer, fontsubkey, NULL, 0, &needed, &num );
489     if (res != ERROR_MORE_DATA) return NULL;
490
491     table = HeapAlloc( PSDRV_Heap, 0, needed );
492     if (!table) return NULL;
493
494     res = EnumPrinterDataExA( printer, fontsubkey, (LPBYTE)table, needed, &needed, &num );
495     if (res != ERROR_SUCCESS)
496     {
497         HeapFree( PSDRV_Heap, 0, table );
498         return NULL;
499     }
500
501     *num_entries = num;
502     return table;
503 }
504
505 static PSDRV_DEVMODE *get_printer_devmode( HANDLE printer )
506 {
507     DWORD needed, dm_size;
508     BOOL res;
509     PRINTER_INFO_9W *info;
510     PSDRV_DEVMODE *dm;
511
512     GetPrinterW( printer, 9, NULL, 0, &needed );
513     if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return NULL;
514
515     info = HeapAlloc( PSDRV_Heap, 0, needed );
516     res = GetPrinterW( printer, 9, (BYTE *)info, needed, &needed );
517     if (!res || !info->pDevMode)
518     {
519         HeapFree( PSDRV_Heap, 0, info );
520         return NULL;
521     }
522
523     /* sanity check the sizes */
524     dm_size = info->pDevMode->dmSize + info->pDevMode->dmDriverExtra;
525     if ((char *)info->pDevMode - (char *)info + dm_size > needed)
526     {
527         HeapFree( PSDRV_Heap, 0, info );
528         return NULL;
529     }
530
531     dm = (PSDRV_DEVMODE*)info;
532     memmove( dm, info->pDevMode, dm_size );
533     return dm;
534 }
535
536 static PSDRV_DEVMODE *get_devmode( HANDLE printer, const WCHAR *name, BOOL *is_default )
537 {
538     PSDRV_DEVMODE *dm = get_printer_devmode( printer );
539
540     *is_default = FALSE;
541
542     if (dm && dm->dmPublic.dmSize + dm->dmPublic.dmDriverExtra >= sizeof(DefaultDevmode))
543     {
544         TRACE( "Retrieved devmode from winspool\n" );
545         return dm;
546     }
547     HeapFree( PSDRV_Heap, 0, dm );
548
549     TRACE( "Using default devmode\n" );
550     dm = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
551     if (dm)
552     {
553         *dm = DefaultDevmode;
554         lstrcpynW( (WCHAR *)dm->dmPublic.dmDeviceName, name, CCHDEVICENAME );
555         *is_default = TRUE;
556     }
557     return dm;
558 }
559
560 static BOOL set_devmode( HANDLE printer, PSDRV_DEVMODE *dm )
561 {
562     PRINTER_INFO_9W info;
563     info.pDevMode = &dm->dmPublic;
564
565     return SetPrinterW( printer, 9, (BYTE *)&info, 0 );
566 }
567
568 static char *get_ppd_filename( HANDLE printer )
569 {
570     DWORD needed;
571     DRIVER_INFO_2W *info;
572     char *unixname;
573
574     GetPrinterDriverW( printer, NULL, 2, NULL, 0, &needed );
575     if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return NULL;
576     info = HeapAlloc( GetProcessHeap(), 0, needed );
577     if (!info) return NULL;
578     GetPrinterDriverW( printer, NULL, 2, (BYTE*)info, needed, &needed );
579     unixname = wine_get_unix_file_name( info->pDataFile );
580     HeapFree( GetProcessHeap(), 0, info );
581
582     return unixname;
583 }
584
585 static struct list printer_list = LIST_INIT( printer_list );
586
587 /**********************************************************************
588  *              PSDRV_FindPrinterInfo
589  */
590 PRINTERINFO *PSDRV_FindPrinterInfo(LPCWSTR name)
591 {
592     DWORD needed, res, dwPaperSize;
593     PRINTERINFO *pi;
594     FONTNAME *font;
595     const AFM *afm;
596     HANDLE hPrinter = 0;
597     char *ppd_filename = NULL, *nameA = NULL;
598     BOOL using_default_devmode = FALSE;
599     int len;
600
601     TRACE("'%s'\n", debugstr_w(name));
602
603     LIST_FOR_EACH_ENTRY( pi, &printer_list, PRINTERINFO, entry )
604     {
605         if (!strcmpW( pi->friendly_name, name ))
606             return pi;
607     }
608
609     pi = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
610     if (pi == NULL) return NULL;
611
612     if (!(pi->friendly_name = HeapAlloc( PSDRV_Heap, 0, (strlenW(name)+1)*sizeof(WCHAR) ))) goto fail;
613     strcpyW( pi->friendly_name, name );
614
615     if (OpenPrinterW( pi->friendly_name, &hPrinter, NULL ) == 0) {
616         ERR ("OpenPrinter failed with code %i\n", GetLastError ());
617         goto fail;
618     }
619
620     len = WideCharToMultiByte( CP_ACP, 0, name, -1, NULL, 0, NULL, NULL );
621     nameA = HeapAlloc( GetProcessHeap(), 0, len );
622     WideCharToMultiByte( CP_ACP, 0, name, -1, nameA, len, NULL, NULL );
623
624     pi->Devmode = get_devmode( hPrinter, name, &using_default_devmode );
625     if (!pi->Devmode) goto fail;
626
627     ppd_filename = get_ppd_filename( hPrinter );
628     if (!ppd_filename) goto fail;
629
630     pi->ppd = PSDRV_ParsePPD( ppd_filename, hPrinter );
631     if (!pi->ppd)
632     {
633         WARN( "Couldn't parse PPD file '%s'\n", ppd_filename );
634         goto fail;
635     }
636
637     if(using_default_devmode) {
638         DWORD papersize;
639
640         if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
641                           (LPWSTR)&papersize, sizeof(papersize)/sizeof(WCHAR))) {
642             PSDRV_DEVMODE dm;
643             memset(&dm, 0, sizeof(dm));
644             dm.dmPublic.dmFields = DM_PAPERSIZE;
645             dm.dmPublic.u1.s1.dmPaperSize = papersize;
646             PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
647         }
648     }
649
650     if(pi->ppd->DefaultPageSize) { /* We'll let the ppd override the devmode */
651         PSDRV_DEVMODE dm;
652         memset(&dm, 0, sizeof(dm));
653         dm.dmPublic.dmFields = DM_PAPERSIZE;
654         dm.dmPublic.u1.s1.dmPaperSize = pi->ppd->DefaultPageSize->WinPage;
655         PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
656     }
657
658     /*
659      *  This is a hack.  The default paper size should be read in as part of
660      *  the Devmode structure, but Wine doesn't currently provide a convenient
661      *  way to configure printers.
662      */
663     res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "Paper Size", NULL,
664                             (LPBYTE)&dwPaperSize, sizeof(DWORD), &needed);
665     if (res == ERROR_SUCCESS)
666         pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
667     else if (res == ERROR_FILE_NOT_FOUND)
668         TRACE ("No 'Paper Size' for printer '%s'\n", debugstr_w(name));
669     else {
670         ERR ("GetPrinterDataA returned %i\n", res);
671         goto fail;
672     }
673
674     /* Duplex is indicated by the setting of the DM_DUPLEX bit in dmFields.
675        WinDuplex == 0 is a special case which means that the ppd has a
676        *DefaultDuplex: NotCapable entry.  In this case we'll try not to confuse
677        apps and set dmDuplex to DMDUP_SIMPLEX but leave the DM_DUPLEX clear.
678        PSDRV_WriteHeader understands this and copes. */
679     pi->Devmode->dmPublic.dmFields &= ~DM_DUPLEX;
680     if(pi->ppd->DefaultDuplex) {
681         pi->Devmode->dmPublic.dmDuplex = pi->ppd->DefaultDuplex->WinDuplex;
682         if(pi->Devmode->dmPublic.dmDuplex != 0)
683             pi->Devmode->dmPublic.dmFields |= DM_DUPLEX;
684         else
685             pi->Devmode->dmPublic.dmDuplex = DMDUP_SIMPLEX;
686     }
687
688     set_devmode( hPrinter, pi->Devmode );
689
690     pi->FontSubTable = load_font_sub_table( hPrinter, &pi->FontSubTableSize );
691
692     LIST_FOR_EACH_ENTRY( font, &pi->ppd->InstalledFonts, FONTNAME, entry )
693     {
694         afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
695         if(!afm) {
696             TRACE( "Couldn't find AFM file for installed printer font '%s' - "
697                     "ignoring\n", font->Name);
698         }
699         else {
700             BOOL added;
701             if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
702                 PSDRV_FreeAFMList(pi->Fonts);
703                 goto fail;
704             }
705         }
706
707     }
708     ClosePrinter( hPrinter );
709     HeapFree( GetProcessHeap(), 0, nameA );
710     HeapFree( GetProcessHeap(), 0, ppd_filename );
711     list_add_head( &printer_list, &pi->entry );
712     return pi;
713
714 fail:
715     if (hPrinter) ClosePrinter( hPrinter );
716     HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
717     HeapFree(PSDRV_Heap, 0, pi->friendly_name);
718     HeapFree(PSDRV_Heap, 0, pi->Devmode);
719     HeapFree(PSDRV_Heap, 0, pi);
720     HeapFree( GetProcessHeap(), 0, nameA );
721     HeapFree( GetProcessHeap(), 0, ppd_filename );
722     return NULL;
723 }
724
725
726 static const struct gdi_dc_funcs psdrv_funcs =
727 {
728     NULL,                               /* pAbortDoc */
729     NULL,                               /* pAbortPath */
730     NULL,                               /* pAlphaBlend */
731     NULL,                               /* pAngleArc */
732     PSDRV_Arc,                          /* pArc */
733     NULL,                               /* pArcTo */
734     NULL,                               /* pBeginPath */
735     NULL,                               /* pBlendImage */
736     PSDRV_Chord,                        /* pChord */
737     NULL,                               /* pCloseFigure */
738     PSDRV_CreateCompatibleDC,           /* pCreateCompatibleDC */
739     PSDRV_CreateDC,                     /* pCreateDC */
740     PSDRV_DeleteDC,                     /* pDeleteDC */
741     NULL,                               /* pDeleteObject */
742     PSDRV_DeviceCapabilities,           /* pDeviceCapabilities */
743     PSDRV_Ellipse,                      /* pEllipse */
744     PSDRV_EndDoc,                       /* pEndDoc */
745     PSDRV_EndPage,                      /* pEndPage */
746     NULL,                               /* pEndPath */
747     PSDRV_EnumFonts,                    /* pEnumFonts */
748     NULL,                               /* pEnumICMProfiles */
749     NULL,                               /* pExcludeClipRect */
750     PSDRV_ExtDeviceMode,                /* pExtDeviceMode */
751     PSDRV_ExtEscape,                    /* pExtEscape */
752     NULL,                               /* pExtFloodFill */
753     NULL,                               /* pExtSelectClipRgn */
754     PSDRV_ExtTextOut,                   /* pExtTextOut */
755     PSDRV_FillPath,                     /* pFillPath */
756     NULL,                               /* pFillRgn */
757     NULL,                               /* pFlattenPath */
758     NULL,                               /* pFontIsLinked */
759     NULL,                               /* pFrameRgn */
760     NULL,                               /* pGdiComment */
761     NULL,                               /* pGdiRealizationInfo */
762     NULL,                               /* pGetBoundsRect */
763     NULL,                               /* pGetCharABCWidths */
764     NULL,                               /* pGetCharABCWidthsI */
765     PSDRV_GetCharWidth,                 /* pGetCharWidth */
766     PSDRV_GetDeviceCaps,                /* pGetDeviceCaps */
767     NULL,                               /* pGetDeviceGammaRamp */
768     NULL,                               /* pGetFontData */
769     NULL,                               /* pGetFontUnicodeRanges */
770     NULL,                               /* pGetGlyphIndices */
771     NULL,                               /* pGetGlyphOutline */
772     NULL,                               /* pGetICMProfile */
773     NULL,                               /* pGetImage */
774     NULL,                               /* pGetKerningPairs */
775     NULL,                               /* pGetNearestColor */
776     NULL,                               /* pGetOutlineTextMetrics */
777     NULL,                               /* pGetPixel */
778     NULL,                               /* pGetSystemPaletteEntries */
779     NULL,                               /* pGetTextCharsetInfo */
780     PSDRV_GetTextExtentExPoint,         /* pGetTextExtentExPoint */
781     NULL,                               /* pGetTextExtentExPointI */
782     NULL,                               /* pGetTextFace */
783     PSDRV_GetTextMetrics,               /* pGetTextMetrics */
784     NULL,                               /* pGradientFill */
785     NULL,                               /* pIntersectClipRect */
786     NULL,                               /* pInvertRgn */
787     PSDRV_LineTo,                       /* pLineTo */
788     NULL,                               /* pModifyWorldTransform */
789     NULL,                               /* pMoveTo */
790     NULL,                               /* pOffsetClipRgn */
791     NULL,                               /* pOffsetViewportOrg */
792     NULL,                               /* pOffsetWindowOrg */
793     PSDRV_PaintRgn,                     /* pPaintRgn */
794     PSDRV_PatBlt,                       /* pPatBlt */
795     PSDRV_Pie,                          /* pPie */
796     PSDRV_PolyBezier,                   /* pPolyBezier */
797     PSDRV_PolyBezierTo,                 /* pPolyBezierTo */
798     NULL,                               /* pPolyDraw */
799     PSDRV_PolyPolygon,                  /* pPolyPolygon */
800     PSDRV_PolyPolyline,                 /* pPolyPolyline */
801     NULL,                               /* pPolygon */
802     NULL,                               /* pPolyline */
803     NULL,                               /* pPolylineTo */
804     PSDRV_PutImage,                     /* pPutImage */
805     NULL,                               /* pRealizeDefaultPalette */
806     NULL,                               /* pRealizePalette */
807     PSDRV_Rectangle,                    /* pRectangle */
808     PSDRV_ResetDC,                      /* pResetDC */
809     NULL,                               /* pRestoreDC */
810     PSDRV_RoundRect,                    /* pRoundRect */
811     NULL,                               /* pSaveDC */
812     NULL,                               /* pScaleViewportExt */
813     NULL,                               /* pScaleWindowExt */
814     NULL,                               /* pSelectBitmap */
815     PSDRV_SelectBrush,                  /* pSelectBrush */
816     NULL,                               /* pSelectClipPath */
817     PSDRV_SelectFont,                   /* pSelectFont */
818     NULL,                               /* pSelectPalette */
819     PSDRV_SelectPen,                    /* pSelectPen */
820     NULL,                               /* pSetArcDirection */
821     PSDRV_SetBkColor,                   /* pSetBkColor */
822     NULL,                               /* pSetBkMode */
823     NULL,                               /* pSetBoundsRect */
824     PSDRV_SetDCBrushColor,              /* pSetDCBrushColor */
825     PSDRV_SetDCPenColor,                /* pSetDCPenColor */
826     NULL,                               /* pSetDIBitsToDevice */
827     NULL,                               /* pSetDeviceClipping */
828     NULL,                               /* pSetDeviceGammaRamp */
829     NULL,                               /* pSetLayout */
830     NULL,                               /* pSetMapMode */
831     NULL,                               /* pSetMapperFlags */
832     PSDRV_SetPixel,                     /* pSetPixel */
833     NULL,                               /* pSetPolyFillMode */
834     NULL,                               /* pSetROP2 */
835     NULL,                               /* pSetRelAbs */
836     NULL,                               /* pSetStretchBltMode */
837     NULL,                               /* pSetTextAlign */
838     NULL,                               /* pSetTextCharacterExtra */
839     PSDRV_SetTextColor,                 /* pSetTextColor */
840     NULL,                               /* pSetTextJustification */
841     NULL,                               /* pSetViewportExt */
842     NULL,                               /* pSetViewportOrg */
843     NULL,                               /* pSetWindowExt */
844     NULL,                               /* pSetWindowOrg */
845     NULL,                               /* pSetWorldTransform */
846     PSDRV_StartDoc,                     /* pStartDoc */
847     PSDRV_StartPage,                    /* pStartPage */
848     NULL,                               /* pStretchBlt */
849     NULL,                               /* pStretchDIBits */
850     PSDRV_StrokeAndFillPath,            /* pStrokeAndFillPath */
851     PSDRV_StrokePath,                   /* pStrokePath */
852     NULL,                               /* pUnrealizePalette */
853     NULL,                               /* pWidenPath */
854     NULL,                               /* wine_get_wgl_driver */
855     GDI_PRIORITY_GRAPHICS_DRV           /* priority */
856 };
857
858
859 /******************************************************************************
860  *      PSDRV_get_gdi_driver
861  */
862 const struct gdi_dc_funcs * CDECL PSDRV_get_gdi_driver( unsigned int version )
863 {
864     if (version != WINE_GDI_DRIVER_VERSION)
865     {
866         ERR( "version mismatch, gdi32 wants %u but wineps has %u\n", version, WINE_GDI_DRIVER_VERSION );
867         return NULL;
868     }
869     return &psdrv_funcs;
870 }