Fixed some issues found by winapi_check.
[wine] / dlls / wineps / init.c
1 /*
2  *      PostScript driver initialization functions
3  *
4  *      Copyright 1998 Huw D M Davies
5  *
6  */
7 #include <string.h>
8
9 #include "gdi.h"
10 #include "psdrv.h"
11 #include "debugtools.h"
12 #include "heap.h"
13 #include "winreg.h"
14 #include "winspool.h"
15 #include "winerror.h"
16 #include "options.h"
17
18 DEFAULT_DEBUG_CHANNEL(psdrv)
19
20 static BOOL PSDRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
21                                LPCSTR output, const DEVMODEA* initData );
22 static BOOL PSDRV_DeleteDC( DC *dc );
23
24 static const DC_FUNCTIONS PSDRV_Funcs =
25 {
26     NULL,                            /* pAbortDoc */
27     NULL,                            /* pAbortPath */
28     NULL,                            /* pAngleArc */
29     PSDRV_Arc,                       /* pArc */
30     NULL,                            /* pArcTo */
31     NULL,                            /* pBeginPath */
32     NULL,                            /* pBitBlt */
33     NULL,                            /* pBitmapBits */
34     NULL,                            /* pChoosePixelFormat */
35     PSDRV_Chord,                     /* pChord */
36     NULL,                            /* pCloseFigure */
37     NULL,                            /* pCreateBitmap */
38     PSDRV_CreateDC,                  /* pCreateDC */
39     NULL,                            /* pCreateDIBSection */
40     NULL,                            /* pCreateDIBSection16 */
41     PSDRV_DeleteDC,                  /* pDeleteDC */
42     NULL,                            /* pDeleteObject */
43     NULL,                            /* pDescribePixelFormat */
44     PSDRV_DeviceCapabilities,        /* pDeviceCapabilities */
45     PSDRV_Ellipse,                   /* pEllipse */
46     PSDRV_EndDoc,                    /* pEndDoc */
47     PSDRV_EndPage,                   /* pEndPage */
48     NULL,                            /* pEndPath */
49     PSDRV_EnumDeviceFonts,           /* pEnumDeviceFonts */
50     PSDRV_Escape,                    /* pEscape */
51     NULL,                            /* pExcludeClipRect */
52     PSDRV_ExtDeviceMode,             /* pExtDeviceMode */
53     NULL,                            /* pExtFloodFill */
54     PSDRV_ExtTextOut,                /* pExtTextOut */
55     NULL,                            /* pFillPath */
56     NULL,                            /* pFillRgn */
57     NULL,                            /* pFlattenPath */
58     NULL,                            /* pFrameRgn */
59     PSDRV_GetCharWidth,              /* pGetCharWidth */
60     NULL,                            /* pGetDCOrgEx */
61     NULL,                            /* pGetPixel */
62     NULL,                            /* pGetPixelFormat */
63     PSDRV_GetTextExtentPoint,        /* pGetTextExtentPoint */
64     PSDRV_GetTextMetrics,            /* pGetTextMetrics */
65     NULL,                            /* pIntersectClipRect */
66     NULL,                            /* pInvertRgn */
67     PSDRV_LineTo,                    /* pLineTo */
68     NULL,                            /* pLoadOEMResource */
69     NULL,                            /* pMoveToEx */
70     NULL,                            /* pOffsetClipRgn */
71     NULL,                            /* pOffsetViewportOrg (optional) */
72     NULL,                            /* pOffsetWindowOrg (optional) */
73     NULL,                            /* pPaintRgn */
74     PSDRV_PatBlt,                    /* pPatBlt */
75     PSDRV_Pie,                       /* pPie */
76     NULL,                            /* pPolyBezier */
77     NULL,                            /* pPolyBezierTo */
78     NULL,                            /* pPolyDraw */
79     PSDRV_PolyPolygon,               /* pPolyPolygon */
80     PSDRV_PolyPolyline,              /* pPolyPolyline */
81     PSDRV_Polygon,                   /* pPolygon */
82     PSDRV_Polyline,                  /* pPolyline */
83     NULL,                            /* pPolylineTo */
84     NULL,                            /* pRealizePalette */
85     PSDRV_Rectangle,                 /* pRectangle */
86     NULL,                            /* pRestoreDC */
87     PSDRV_RoundRect,                 /* pRoundRect */
88     NULL,                            /* pSaveDC */
89     NULL,                            /* pScaleViewportExt (optional) */
90     NULL,                            /* pScaleWindowExt (optional) */
91     NULL,                            /* pSelectClipPath */
92     NULL,                            /* pSelectClipRgn */
93     PSDRV_SelectObject,              /* pSelectObject */
94     NULL,                            /* pSelectPalette */
95     PSDRV_SetBkColor,                /* pSetBkColor */
96     NULL,                            /* pSetBkMode */
97     PSDRV_SetDeviceClipping,         /* pSetDeviceClipping */
98     NULL,                            /* pSetDIBitsToDevice */
99     NULL,                            /* pSetMapMode (optional) */
100     NULL,                            /* pSetMapperFlags */
101     PSDRV_SetPixel,                  /* pSetPixel */
102     NULL,                            /* pSetPixelFormat */
103     NULL,                            /* pSetPolyFillMode */
104     NULL,                            /* pSetROP2 */
105     NULL,                            /* pSetRelAbs */
106     NULL,                            /* pSetStretchBltMode */
107     NULL,                            /* pSetTextAlign */
108     NULL,                            /* pSetTextCharacterExtra */
109     PSDRV_SetTextColor,              /* pSetTextColor */
110     NULL,                            /* pSetTextJustification */
111     NULL,                            /* pSetViewportExt (optional) */
112     NULL,                            /* pSetViewportOrg (optional) */
113     NULL,                            /* pSetWindowExt (optional) */
114     NULL,                            /* pSetWindowOrg (optional) */
115     PSDRV_StartDoc,                  /* pStartDoc */
116     PSDRV_StartPage,                 /* pStartPage */
117     NULL,                            /* pStretchBlt */
118     PSDRV_StretchDIBits,             /* pStretchDIBits */
119     NULL,                            /* pStrokeAndFillPath */
120     NULL,                            /* pStrokePath */
121     NULL,                            /* pSwapBuffers */
122     NULL                             /* pWidenPath */
123 };
124
125
126 /* Default entries for devcaps */
127
128 static DeviceCaps PSDRV_DevCaps = {
129 /* version */           0, 
130 /* technology */        DT_RASPRINTER,
131 /* horzSize */          210,
132 /* vertSize */          297,
133 /* horzRes */           4961,
134 /* vertRes */           7016, 
135 /* bitsPixel */         1,
136 /* planes */            1,
137 /* numBrushes */        -1,
138 /* numPens */           10,
139 /* numMarkers */        0,
140 /* numFonts */          39,
141 /* numColors */         2,
142 /* pdeviceSize */       0,      
143 /* curveCaps */         CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES |
144                         CC_WIDE | CC_STYLED | CC_WIDESTYLED | CC_INTERIORS |
145                         CC_ROUNDRECT,
146 /* lineCaps */          LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
147                         LC_STYLED | LC_WIDESTYLED | LC_INTERIORS,
148 /* polygoalnCaps */     PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON |
149                         PC_SCANLINE | PC_WIDE | PC_STYLED | PC_WIDESTYLED |
150                         PC_INTERIORS,
151 /* textCaps */          TC_CR_ANY, /* psdrv 0x59f7 */
152 /* clipCaps */          CP_RECTANGLE,
153 /* rasterCaps */        RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT |
154                         RC_DIBTODEV | RC_STRETCHBLT |
155                         RC_STRETCHDIB, /* psdrv 0x6e99 */
156 /* aspectX */           600,
157 /* aspectY */           600,
158 /* aspectXY */          848,
159 /* pad1 */              { 0 },
160 /* logPixelsX */        600,
161 /* logPixelsY */        600, 
162 /* pad2 */              { 0 },
163 /* palette size */      0,
164 /* ..etc */             0, 0 };
165
166 static PSDRV_DEVMODEA DefaultDevmode = 
167 {
168   { /* dmPublic */
169 /* dmDeviceName */      "Wine PostScript Driver",
170 /* dmSpecVersion */     0x30a,
171 /* dmDriverVersion */   0x001,
172 /* dmSize */            sizeof(DEVMODEA),
173 /* dmDriverExtra */     0,
174 /* dmFields */          DM_ORIENTATION | DM_PAPERSIZE | DM_SCALE | 
175                         DM_COPIES | DM_DEFAULTSOURCE | DM_COLOR | 
176                         DM_DUPLEX | DM_YRESOLUTION | DM_TTOPTION,
177    { /* u1 */
178      { /* s1 */
179 /* dmOrientation */     DMORIENT_PORTRAIT,
180 /* dmPaperSize */       DMPAPER_A4,
181 /* dmPaperLength */     2969,
182 /* dmPaperWidth */      2101
183      }
184    },
185 /* dmScale */           100, /* ?? */
186 /* dmCopies */          1,
187 /* dmDefaultSource */   DMBIN_AUTO,
188 /* dmPrintQuality */    0,
189 /* dmColor */           DMCOLOR_MONOCHROME,
190 /* dmDuplex */          0,
191 /* dmYResolution */     0,
192 /* dmTTOption */        DMTT_SUBDEV,
193 /* dmCollate */         0,
194 /* dmFormName */        "",
195 /* dmUnusedPadding */   0,
196 /* dmBitsPerPel */      0,
197 /* dmPelsWidth */       0,
198 /* dmPelsHeight */      0,
199 /* dmDisplayFlags */    0,
200 /* dmDisplayFrequency */ 0
201   },
202   { /* dmDocPrivate */
203     0 /* dummy */
204   },
205   { /* dmDrvPrivate */
206 /* ppdfilename */       "default.ppd"
207   }
208 };
209
210 HANDLE PSDRV_Heap = 0;
211
212 static HANDLE PSDRV_DefaultFont = 0;
213 static LOGFONTA DefaultLogFont = {
214     100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
215     DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
216 };
217
218 /*********************************************************************
219  *           PSDRV_Init
220  *
221  * Initializes font metrics and registers driver. Called from GDI_Init()
222  *
223  */
224 BOOL WINAPI PSDRV_Init( HINSTANCE hinst, DWORD reason, LPVOID reserved )
225 {
226     static int process_count = 0;
227
228     TRACE("(0x%4x, 0x%08lx, %p)\n", hinst, reason, reserved);
229    
230     switch(reason) {
231         case DLL_PROCESS_ATTACH:
232             if (!process_count++) {
233                 /* FIXME: return FALSE if we fail any of these steps */
234                 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
235                 PSDRV_GetFontMetrics();
236                 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
237                 DRIVER_RegisterDriver( "WINEPS", &PSDRV_Funcs );
238             }
239         break;
240         case DLL_PROCESS_DETACH:
241             if (!--process_count) {
242                 DeleteObject( PSDRV_DefaultFont );
243                 HeapDestroy( PSDRV_Heap );
244                 DRIVER_UnregisterDriver( "WINEPS" );
245             }
246         break;
247     }
248  
249     return TRUE;
250 }
251
252
253 /**********************************************************************
254  *           PSDRV_CreateDC
255  */
256 static BOOL PSDRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
257                                LPCSTR output, const DEVMODEA* initData )
258 {
259     PSDRV_PDEVICE *physDev;
260     PRINTERINFO *pi;
261     DeviceCaps *devCaps;
262
263     /* If no device name was specified, retrieve the device name
264      * from the DEVMODE structure from the DC's physDev.
265      * (See CreateCompatibleDC) */
266     if ( !device && dc->physDev )
267     {
268         physDev = (PSDRV_PDEVICE *)dc->physDev;
269         device = physDev->Devmode->dmPublic.dmDeviceName;
270     }
271     pi = PSDRV_FindPrinterInfo(device);
272         
273     TRACE("(%s %s %s %p)\n", driver, device, output, initData);
274
275     if(!pi) return FALSE;
276
277     if(!pi->Fonts) {
278         MESSAGE("To use WINEPS you need to install some AFM files.\n");
279         return FALSE;
280     }
281
282     physDev = (PSDRV_PDEVICE *)HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
283                                                      sizeof(*physDev) );
284     if (!physDev) return FALSE;
285     dc->physDev = physDev;
286
287     physDev->pi = pi;
288
289     physDev->Devmode = (PSDRV_DEVMODEA *)HeapAlloc( PSDRV_Heap, 0,
290                                                      sizeof(PSDRV_DEVMODEA) );
291     if(!physDev->Devmode) {
292         HeapFree( PSDRV_Heap, 0, physDev );
293         return FALSE;
294     }
295     
296     memcpy( physDev->Devmode, pi->Devmode, sizeof(PSDRV_DEVMODEA) );
297
298     if(initData) {
299         PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)initData, pi);
300     }
301
302     
303     devCaps = HeapAlloc( PSDRV_Heap, 0, sizeof(PSDRV_DevCaps) );
304     memcpy(devCaps, &PSDRV_DevCaps, sizeof(PSDRV_DevCaps));
305
306     if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
307         devCaps->horzSize = physDev->Devmode->dmPublic.u1.s1.dmPaperWidth / 10;
308         devCaps->vertSize = physDev->Devmode->dmPublic.u1.s1.dmPaperLength / 10;
309     } else {
310         devCaps->horzSize = physDev->Devmode->dmPublic.u1.s1.dmPaperLength / 10;
311         devCaps->vertSize = physDev->Devmode->dmPublic.u1.s1.dmPaperWidth / 10;
312     }
313
314     devCaps->horzRes = physDev->pi->ppd->DefaultResolution * 
315       devCaps->horzSize / 25.4;
316     devCaps->vertRes = physDev->pi->ppd->DefaultResolution * 
317       devCaps->vertSize / 25.4;
318
319     /* Are aspect[XY] and logPixels[XY] correct? */
320     /* Need to handle different res in x and y => fix ppd */
321     devCaps->aspectX = devCaps->logPixelsX = 
322                                 physDev->pi->ppd->DefaultResolution;
323     devCaps->aspectY = devCaps->logPixelsY = 
324                                 physDev->pi->ppd->DefaultResolution;
325     devCaps->aspectXY = (int)hypot( (double)devCaps->aspectX, 
326                                     (double)devCaps->aspectY );
327
328     if(physDev->pi->ppd->ColorDevice) {
329         devCaps->bitsPixel = 8;
330         devCaps->numColors = 256;
331         /* FIXME are these values OK? */
332     }
333
334     /* etc */
335
336     dc->w.devCaps = devCaps;
337
338     dc->w.hVisRgn = CreateRectRgn(0, 0, dc->w.devCaps->horzRes,
339                             dc->w.devCaps->vertRes);
340     
341     dc->w.hFont = PSDRV_DefaultFont;
342     physDev->job.output = output ?
343       HEAP_strdupA( PSDRV_Heap, 0, output ) :
344       HEAP_strdupA( PSDRV_Heap, 0, "LPT1:" );  /* HACK */
345     physDev->job.hJob = 0;
346     return TRUE;
347 }
348
349
350 /**********************************************************************
351  *           PSDRV_DeleteDC
352  */
353 static BOOL PSDRV_DeleteDC( DC *dc )
354 {
355     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
356     
357     TRACE("\n");
358
359     HeapFree( PSDRV_Heap, 0, physDev->Devmode );
360     HeapFree( PSDRV_Heap, 0, physDev->job.output );
361     HeapFree( PSDRV_Heap, 0, (void *)dc->w.devCaps );
362     HeapFree( PSDRV_Heap, 0, physDev );
363     dc->physDev = NULL;
364
365     return TRUE;
366 }
367
368
369         
370
371 /**********************************************************************
372  *              PSDRV_FindPrinterInfo
373  */
374 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name) 
375 {
376     static PRINTERINFO *PSDRV_PrinterList;
377     DWORD type = REG_BINARY, needed, res;
378     PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
379     FONTNAME *font;
380     AFM *afm;
381
382     TRACE("'%s'\n", name);
383     
384     for( ; pi; last = &pi->next, pi = pi->next) {
385         if(!strcmp(pi->FriendlyName, name))
386             return pi;
387     }
388
389     pi = *last = HeapAlloc( PSDRV_Heap, 0, sizeof(*pi) );
390     pi->FriendlyName = HEAP_strdupA( PSDRV_Heap, 0, name );
391     res = DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
392                             NULL, 0, &needed );
393
394     if(res == ERROR_INVALID_PRINTER_NAME || needed != sizeof(DefaultDevmode)) {
395         pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
396         memcpy(pi->Devmode, &DefaultDevmode, sizeof(DefaultDevmode) );
397         DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
398                  REG_BINARY, (LPBYTE)&DefaultDevmode, sizeof(DefaultDevmode) );
399
400         /* need to do something here AddPrinter?? */
401     } else {
402         pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
403         DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
404                           (LPBYTE)pi->Devmode, needed, &needed);
405     }
406
407     PROFILE_GetWineIniString("psdrv", "ppdfile", "default.ppd",
408                              pi->Devmode->dmDrvPrivate.ppdFileName, 256);
409     pi->ppd = PSDRV_ParsePPD(pi->Devmode->dmDrvPrivate.ppdFileName);
410     if(!pi->ppd) {
411         HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
412         HeapFree(PSDRV_Heap, 0, pi->Devmode);
413         HeapFree(PSDRV_Heap, 0, pi);
414         *last = NULL;
415         MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
416             pi->Devmode->dmDrvPrivate.ppdFileName);
417         return NULL;
418     }
419
420     pi->next = NULL;
421     pi->Fonts = NULL;
422
423     for(font = pi->ppd->InstalledFonts; font; font = font->next) {
424         afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
425         if(!afm) {
426             MESSAGE(
427          "Couldn't find AFM file for installed printer font '%s' - ignoring\n",
428          font->Name);
429         } else {
430             PSDRV_AddAFMtoList(&pi->Fonts, afm);
431         }
432     }
433
434     return pi;
435 }