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