Don't load the WINEPS driver until it's needed.
[wine] / graphics / win16drv / init.c
1 /*
2  * Windows Device Context initialisation functions
3  *
4  * Copyright 1996 John Harvey
5  *           1998 Huw Davies
6  */
7
8 #include <ctype.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "win16drv.h"
13 #include "gdi.h"
14 #include "bitmap.h"
15 #include "heap.h"
16 #include "font.h"
17 #include "options.h"
18 #include "debugtools.h"
19
20 DEFAULT_DEBUG_CHANNEL(win16drv);
21
22 #define SUPPORT_REALIZED_FONTS 1
23 #include "pshpack1.h"
24 typedef struct
25 {
26   SHORT nSize;
27   SEGPTR lpindata;
28   SEGPTR lpFont;
29   SEGPTR lpXForm;
30   SEGPTR lpDrawMode;
31 } EXTTEXTDATA, *LPEXTTEXTDATA;
32 #include "poppack.h"
33
34 SEGPTR          win16drv_SegPtr_TextXForm;
35 LPTEXTXFORM16   win16drv_TextXFormP;
36 SEGPTR          win16drv_SegPtr_DrawMode;
37 LPDRAWMODE      win16drv_DrawModeP;
38
39
40 static BOOL WIN16DRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
41                                  LPCSTR output, const DEVMODEA* initData );
42 static INT WIN16DRV_Escape( DC *dc, INT nEscape, INT cbInput, 
43                               SEGPTR lpInData, SEGPTR lpOutData );
44
45 static const DC_FUNCTIONS WIN16DRV_Funcs =
46 {
47     NULL,                            /* pAbortDoc */
48     NULL,                            /* pAbortPath */
49     NULL,                            /* pAngleArc */
50     NULL,                            /* pArc */
51     NULL,                            /* pArcTo */
52     NULL,                            /* pBeginPath */
53     NULL,                            /* pBitBlt */
54     NULL,                            /* pBitmapBits */
55     NULL,                            /* pChoosePixelFormat */
56     NULL,                            /* pChord */
57     NULL,                            /* pCloseFigure */
58     NULL,                            /* pCreateBitmap */
59     WIN16DRV_CreateDC,               /* pCreateDC */
60     NULL,                            /* pCreateDIBSection */
61     NULL,                            /* pCreateDIBSection16 */
62     NULL,                            /* pDeleteDC */
63     NULL,                            /* pDeleteObject */
64     NULL,                            /* pDescribePixelFormat */
65     WIN16DRV_DeviceCapabilities,     /* pDeviceCapabilities */
66     WIN16DRV_Ellipse,                /* pEllipse */
67     NULL,                            /* pEndDoc */
68     NULL,                            /* pEndPage */
69     NULL,                            /* pEndPath */
70     WIN16DRV_EnumDeviceFonts,        /* pEnumDeviceFonts */
71     WIN16DRV_Escape,                 /* pEscape */
72     NULL,                            /* pExcludeClipRect */
73     WIN16DRV_ExtDeviceMode,          /* pExtDeviceMode */
74     NULL,                            /* pExtFloodFill */
75     WIN16DRV_ExtTextOut,             /* pExtTextOut */
76     NULL,                            /* pFillPath */
77     NULL,                            /* pFillRgn */
78     NULL,                            /* pFlattenPath */
79     NULL,                            /* pFrameRgn */
80     WIN16DRV_GetCharWidth,           /* pGetCharWidth */
81     NULL,                            /* pGetDCOrgEx */
82     NULL,                            /* pGetPixel */
83     NULL,                            /* pGetPixelFormat */
84     WIN16DRV_GetTextExtentPoint,     /* pGetTextExtentPoint */
85     WIN16DRV_GetTextMetrics,         /* pGetTextMetrics */
86     NULL,                            /* pIntersectClipRect */
87     NULL,                            /* pInvertRgn */
88     WIN16DRV_LineTo,                 /* pLineTo */
89     NULL,                            /* pMoveTo */
90     NULL,                            /* pOffsetClipRgn */
91     NULL,                            /* pOffsetViewportOrgEx */
92     NULL,                            /* pOffsetWindowOrgEx */
93     NULL,                            /* pPaintRgn */
94     WIN16DRV_PatBlt,                 /* pPatBlt */
95     NULL,                            /* pPie */
96     NULL,                            /* pPolyBezier */
97     NULL,                            /* pPolyBezierTo */
98     NULL,                            /* pPolyDraw */
99     NULL,                            /* pPolyPolygon */
100     NULL,                            /* pPolyPolyline */
101     WIN16DRV_Polygon,                /* pPolygon */
102     WIN16DRV_Polyline,               /* pPolyline */
103     NULL,                            /* pPolylineTo */
104     NULL,                            /* pRealizePalette */
105     WIN16DRV_Rectangle,              /* pRectangle */
106     NULL,                            /* pRestoreDC */
107     NULL,                            /* pRoundRect */
108     NULL,                            /* pSaveDC */
109     NULL,                            /* pScaleViewportExtEx */
110     NULL,                            /* pScaleWindowExtEx */
111     NULL,                            /* pSelectClipPath */
112     NULL,                            /* pSelectClipRgn */
113     WIN16DRV_SelectObject,           /* pSelectObject */
114     NULL,                            /* pSelectPalette */
115     NULL,                            /* pSetBkColor */
116     NULL,                            /* pSetBkMode */
117     NULL,                            /* pSetDeviceClipping */
118     NULL,                            /* pSetDIBitsToDevice */
119     NULL,                            /* pSetMapMode */
120     NULL,                            /* pSetMapperFlags */
121     NULL,                            /* pSetPixel */
122     NULL,                            /* pSetPixelFormat */
123     NULL,                            /* pSetPolyFillMode */
124     NULL,                            /* pSetROP2 */
125     NULL,                            /* pSetRelAbs */
126     NULL,                            /* pSetStretchBltMode */
127     NULL,                            /* pSetTextAlign */
128     NULL,                            /* pSetTextCharacterExtra */
129     NULL,                            /* pSetTextColor */
130     NULL,                            /* pSetTextJustification */
131     NULL,                            /* pSetViewportExtEx */
132     NULL,                            /* pSetViewportOrgEx */
133     NULL,                            /* pSetWindowExtEx */
134     NULL,                            /* pSetWindowOrgEx */
135     NULL,                            /* pStartDoc */
136     NULL,                            /* pStartPage */
137     NULL,                            /* pStretchBlt */
138     NULL,                            /* pStretchDIBits */
139     NULL,                            /* pStrokeAndFillPath */
140     NULL,                            /* pStrokePath */
141     NULL,                            /* pSwapBuffers */
142     NULL                             /* pWidenPath */
143 };
144
145
146
147
148
149 /**********************************************************************
150  *           WIN16DRV_Init
151  */
152 BOOL WIN16DRV_Init(void)
153 {
154     return DRIVER_RegisterDriver( NULL /* generic driver */, &WIN16DRV_Funcs );
155         
156 }
157
158 /* Tempory functions, for initialising structures */
159 /* These values should be calculated, not hardcoded */
160 void InitTextXForm(LPTEXTXFORM16 lpTextXForm)
161 {
162     lpTextXForm->txfHeight      = 0x0001;
163     lpTextXForm->txfWidth       = 0x000c;
164     lpTextXForm->txfEscapement  = 0x0000;
165     lpTextXForm->txfOrientation = 0x0000;
166     lpTextXForm->txfWeight      = 0x0190;
167     lpTextXForm->txfItalic      = 0x00;
168     lpTextXForm->txfUnderline   = 0x00;
169     lpTextXForm->txfStrikeOut   = 0x00; 
170     lpTextXForm->txfOutPrecision = 0x02;
171     lpTextXForm->txfClipPrecision = 0x01;
172     lpTextXForm->txfAccelerator = 0x0001;
173     lpTextXForm->txfOverhang    = 0x0000;
174 }
175
176
177 void InitDrawMode(LPDRAWMODE lpDrawMode)
178 {
179     lpDrawMode->Rop2            = 0x000d;       
180     lpDrawMode->bkMode          = 0x0001;     
181     lpDrawMode->bkColor         = 0x3fffffff;    
182     lpDrawMode->TextColor       = 0x20000000;  
183     lpDrawMode->TBreakExtra     = 0x0000;
184     lpDrawMode->BreakExtra      = 0x0000; 
185     lpDrawMode->BreakErr        = 0x0000;   
186     lpDrawMode->BreakRem        = 0x0000;   
187     lpDrawMode->BreakCount      = 0x0000; 
188     lpDrawMode->CharExtra       = 0x0000;  
189     lpDrawMode->LbkColor        = 0x00ffffff;   
190     lpDrawMode->LTextColor      = 0x00000000;     
191     lpDrawMode->ICMCXform       = 0; /* ? */
192     lpDrawMode->StretchBltMode  = STRETCH_ANDSCANS;
193     lpDrawMode->eMiterLimit     = 1;
194 }
195
196 BOOL WIN16DRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device, LPCSTR output,
197                           const DEVMODEA* initData )
198 {
199     LOADED_PRINTER_DRIVER *pLPD;
200     WORD wRet;
201     DeviceCaps *printerDevCaps;
202     int nPDEVICEsize;
203     PDEVICE_HEADER *pPDH;
204     WIN16DRV_PDEVICE *physDev;
205     char printerEnabled[20];
206     PROFILE_GetWineIniString( "wine", "printer", "off",
207                              printerEnabled, sizeof(printerEnabled) );
208     if (strcasecmp(printerEnabled,"on"))
209     {
210         MESSAGE("Printing disabled in wine.conf or .winerc file\n");
211         MESSAGE("Use \"printer=on\" in the \"[wine]\" section to enable it.\n");
212         return FALSE;
213     }
214
215     TRACE("In creatdc for (%s,%s,%s) initData 0x%p\n",
216           driver, device, output, initData);
217
218     physDev = (WIN16DRV_PDEVICE *)HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev) );
219     if (!physDev) return FALSE;
220     dc->physDev = physDev;
221
222     pLPD = LoadPrinterDriver(driver);
223     if (pLPD == NULL)
224     {
225         WARN("Failed to find printer driver\n");
226         HeapFree( GetProcessHeap(), 0, physDev );
227         return FALSE;
228     }
229     TRACE("windevCreateDC pLPD 0x%p\n", pLPD);
230
231     /* Now Get the device capabilities from the printer driver */
232     
233     printerDevCaps = (DeviceCaps *) calloc(1, sizeof(DeviceCaps));
234     if(printerDevCaps == NULL) {
235         ERR("No memory to read the device capabilities!");
236         HeapFree( GetProcessHeap(), 0, physDev );
237         return FALSE;
238     }
239
240     if(!output) output = "LPT1:";
241     /* Get GDIINFO which is the same as a DeviceCaps structure */
242     wRet = PRTDRV_Enable(printerDevCaps, GETGDIINFO, device, driver, output,NULL); 
243
244     /* Add this to the DC */
245     dc->devCaps = printerDevCaps;
246     dc->hVisRgn = CreateRectRgn(0, 0, dc->devCaps->horzRes, dc->devCaps->vertRes);
247     dc->bitsPerPixel = dc->devCaps->bitsPixel;
248     
249     TRACE("Got devcaps width %d height %d bits %d planes %d\n",
250           dc->devCaps->horzRes, dc->devCaps->vertRes, 
251           dc->devCaps->bitsPixel, dc->devCaps->planes);
252
253     /* Now we allocate enough memory for the PDEVICE structure */
254     /* The size of this varies between printer drivers */
255     /* This PDEVICE is used by the printer DRIVER not by the GDI so must */
256     /* be accessable from 16 bit code */
257     nPDEVICEsize = dc->devCaps->pdeviceSize + sizeof(PDEVICE_HEADER);
258
259     /* TTD Shouldn't really do pointer arithmetic on segment points */
260     physDev->segptrPDEVICE = K32WOWGlobalLock16(GlobalAlloc16(GHND, nPDEVICEsize))+sizeof(PDEVICE_HEADER);
261     *((BYTE *)MapSL(physDev->segptrPDEVICE)+0) = 'N'; 
262     *((BYTE *)MapSL(physDev->segptrPDEVICE)+1) = 'B'; 
263
264     /* Set up the header */
265     pPDH = (PDEVICE_HEADER *)((BYTE*)MapSL(physDev->segptrPDEVICE) - sizeof(PDEVICE_HEADER)); 
266     pPDH->pLPD = pLPD;
267     
268     TRACE("PDEVICE allocated %08lx\n",(DWORD)(physDev->segptrPDEVICE));
269     
270     /* Now get the printer driver to initialise this data */
271     wRet = PRTDRV_Enable((LPVOID)physDev->segptrPDEVICE, INITPDEVICE, device, driver, output, NULL); 
272
273     physDev->FontInfo = NULL;
274     physDev->BrushInfo = NULL;
275     physDev->PenInfo = NULL;
276     win16drv_SegPtr_TextXForm = K32WOWGlobalLock16(GlobalAlloc16(GHND, sizeof(TEXTXFORM16)));
277     win16drv_TextXFormP = MapSL(win16drv_SegPtr_TextXForm);
278     
279     InitTextXForm(win16drv_TextXFormP);
280
281     /* TTD Lots more to do here */
282     win16drv_SegPtr_DrawMode = K32WOWGlobalLock16(GlobalAlloc16(GHND, sizeof(DRAWMODE)));
283     win16drv_DrawModeP = MapSL(win16drv_SegPtr_DrawMode);
284     
285     InitDrawMode(win16drv_DrawModeP);
286
287     return TRUE;
288 }
289
290 BOOL WIN16DRV_PatBlt( struct tagDC *dc, INT left, INT top,
291                         INT width, INT height, DWORD rop )
292 {
293   
294     WIN16DRV_PDEVICE *physDev = (WIN16DRV_PDEVICE *)dc->physDev;
295     BOOL bRet = 0;
296
297     bRet = PRTDRV_StretchBlt( physDev->segptrPDEVICE, left, top, width, height, (SEGPTR)NULL, 0, 0, width, height,
298                        PATCOPY, physDev->BrushInfo, win16drv_SegPtr_DrawMode, NULL);
299
300     return bRet;
301 }
302 /* 
303  * Escape (GDI.38)
304  */
305 static INT WIN16DRV_Escape( DC *dc, INT nEscape, INT cbInput, 
306                               SEGPTR lpInData, SEGPTR lpOutData )
307 {
308     WIN16DRV_PDEVICE *physDev = (WIN16DRV_PDEVICE *)dc->physDev;
309     int nRet = 0;
310
311     /* We should really process the nEscape parameter, but for now just
312        pass it all to the driver */
313     if (dc != NULL && physDev->segptrPDEVICE != 0)
314     {
315         switch(nEscape)
316           {
317           case ENABLEPAIRKERNING:
318             FIXME("Escape: ENABLEPAIRKERNING ignored.\n");
319             nRet = 1;
320             break;
321           case GETPAIRKERNTABLE:
322             FIXME("Escape: GETPAIRKERNTABLE ignored.\n");
323             nRet = 0;
324             break;
325           case SETABORTPROC: {
326                 /* FIXME: The AbortProc should be called:
327                 - After every write to printer port or spool file
328                 - Several times when no more disk space
329                 - Before every metafile record when GDI does banding
330                 */ 
331
332        /* Call Control with hdc as lpInData */
333             HDC16 *seghdc = SEGPTR_NEW(HDC16);
334             *seghdc = dc->hSelf;
335             nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
336                                   SEGPTR_GET(seghdc), lpOutData);
337             SEGPTR_FREE(seghdc);
338             break;
339           }
340
341           case NEXTBAND:
342             {
343               LPPOINT16 newInData =  SEGPTR_NEW(POINT16);
344
345               nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
346                                     SEGPTR_GET(newInData), lpOutData);
347               SEGPTR_FREE(newInData);
348               break;
349             }
350
351           case GETEXTENDEDTEXTMETRICS:
352             {
353               EXTTEXTDATA *textData = SEGPTR_NEW(EXTTEXTDATA);
354
355               textData->nSize = cbInput;
356               textData->lpindata = lpInData;
357               textData->lpFont = SEGPTR_GET( physDev->FontInfo );
358               textData->lpXForm = win16drv_SegPtr_TextXForm;
359               textData->lpDrawMode = win16drv_SegPtr_DrawMode;
360               nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
361                                     SEGPTR_GET(textData), lpOutData);
362               SEGPTR_FREE(textData);
363             }
364           break;
365           case STARTDOC:
366             {
367               /* lpInData is not necessarily \0 terminated so make it so */
368               char *cp = SEGPTR_ALLOC(cbInput + 1);
369               memcpy(cp, MapSL(lpInData), cbInput);
370               cp[cbInput] = '\0';
371             
372               nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
373                                     SEGPTR_GET(cp), lpOutData);
374               SEGPTR_FREE(cp);
375               if (nRet != -1)
376                 {
377                   HDC *tmpHdc = SEGPTR_NEW(HDC);
378
379 #define SETPRINTERDC SETABORTPROC
380
381                   *tmpHdc = dc->hSelf;
382                   PRTDRV_Control(physDev->segptrPDEVICE, SETPRINTERDC,
383                                  SEGPTR_GET(tmpHdc), (SEGPTR)NULL);
384                   SEGPTR_FREE(tmpHdc);
385                 }
386             }
387             break;
388           default:
389             nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
390                                   lpInData, lpOutData);
391             break;
392         }
393     }
394     else
395         WARN("Escape(nEscape = %04x) - ???\n", nEscape);      
396     return nRet;
397 }
398
399