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