Release 20010305.
[wine] / graphics / x11drv / init.c
1 /*
2  * X11 graphics driver initialisation functions
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #include "ts_xlib.h"
10
11 #include <string.h>
12
13 #include "bitmap.h"
14 #include "color.h"
15 #include "debugtools.h"
16 #include "winnt.h"
17 #include "x11drv.h"
18
19 DEFAULT_DEBUG_CHANNEL(x11drv);
20
21 static BOOL X11DRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
22                                LPCSTR output, const DEVMODEA* initData );
23 static BOOL X11DRV_DeleteDC( DC *dc );
24
25 static INT X11DRV_Escape( DC *dc, INT nEscape, INT cbInput,
26                             SEGPTR lpInData, SEGPTR lpOutData );
27
28 const DC_FUNCTIONS X11DRV_DC_Funcs =
29 {
30     NULL,                            /* pAbortDoc */
31     NULL,                            /* pAbortPath */
32     NULL,                            /* pAngleArc */
33     X11DRV_Arc,                      /* pArc */
34     NULL,                            /* pArcTo */
35     NULL,                            /* pBeginPath */
36     X11DRV_BitBlt,                   /* pBitBlt */
37     X11DRV_BitmapBits,               /* pBitmapBits */
38     X11DRV_ChoosePixelFormat,        /* pChoosePixelFormat */
39     X11DRV_Chord,                    /* pChord */
40     NULL,                            /* pCloseFigure */
41     X11DRV_CreateBitmap,             /* pCreateBitmap */
42     X11DRV_CreateDC,                 /* pCreateDC */
43     X11DRV_DIB_CreateDIBSection,     /* pCreateDIBSection */
44     X11DRV_DIB_CreateDIBSection16,   /* pCreateDIBSection16 */
45     X11DRV_DeleteDC,                 /* pDeleteDC */
46     X11DRV_DeleteObject,             /* pDeleteObject */
47     X11DRV_DescribePixelFormat,      /* pDescribePixelFormat */
48     NULL,                            /* pDeviceCapabilities */
49     X11DRV_Ellipse,                  /* pEllipse */
50     NULL,                            /* pEndDoc */
51     NULL,                            /* pEndPage */
52     NULL,                            /* pEndPath */
53     X11DRV_EnumDeviceFonts,          /* pEnumDeviceFonts */
54     X11DRV_Escape,                   /* pEscape */
55     NULL,                            /* pExcludeClipRect */
56     NULL,                            /* pExtDeviceMode */
57     X11DRV_ExtFloodFill,             /* pExtFloodFill */
58     X11DRV_ExtTextOut,               /* pExtTextOut */
59     NULL,                            /* pFillPath */
60     NULL,                            /* pFillRgn */
61     NULL,                            /* pFlattenPath */
62     NULL,                            /* pFrameRgn */
63     X11DRV_GetCharWidth,             /* pGetCharWidth */
64     X11DRV_GetDCOrgEx,               /* pGetDCOrgEx */
65     X11DRV_GetPixel,                 /* pGetPixel */
66     X11DRV_GetPixelFormat,           /* pGetPixelFormat */
67     X11DRV_GetTextExtentPoint,       /* pGetTextExtentPoint */
68     X11DRV_GetTextMetrics,           /* pGetTextMetrics */
69     NULL,                            /* pIntersectClipRect */
70     NULL,                            /* pInvertRgn */
71     X11DRV_LineTo,                   /* pLineTo */
72     NULL,                            /* pMoveTo */
73     NULL,                            /* pOffsetClipRgn */
74     NULL,                            /* pOffsetViewportOrg (optional) */
75     NULL,                            /* pOffsetWindowOrg (optional) */
76     X11DRV_PaintRgn,                 /* pPaintRgn */
77     X11DRV_PatBlt,                   /* pPatBlt */
78     X11DRV_Pie,                      /* pPie */
79     NULL,                            /* pPolyBezier */
80     NULL,                            /* pPolyBezierTo */
81     NULL,                            /* pPolyDraw */
82     X11DRV_PolyPolygon,              /* pPolyPolygon */
83     X11DRV_PolyPolyline,             /* pPolyPolyline */
84     X11DRV_Polygon,                  /* pPolygon */
85     X11DRV_Polyline,                 /* pPolyline */
86     NULL,                            /* pPolylineTo */
87     NULL,                            /* pRealizePalette */
88     X11DRV_Rectangle,                /* pRectangle */
89     NULL,                            /* pRestoreDC */
90     X11DRV_RoundRect,                /* pRoundRect */
91     NULL,                            /* pSaveDC */
92     NULL,                            /* pScaleViewportExt (optional) */
93     NULL,                            /* pScaleWindowExt (optional) */
94     NULL,                            /* pSelectClipPath */
95     NULL,                            /* pSelectClipRgn */
96     X11DRV_SelectObject,             /* pSelectObject */
97     NULL,                            /* pSelectPalette */
98     X11DRV_SetBkColor,               /* pSetBkColor */
99     NULL,                            /* pSetBkMode */
100     X11DRV_SetDeviceClipping,        /* pSetDeviceClipping */
101     X11DRV_SetDIBitsToDevice,        /* pSetDIBitsToDevice */
102     NULL,                            /* pSetMapMode (optional) */
103     NULL,                            /* pSetMapperFlags */
104     X11DRV_SetPixel,                 /* pSetPixel */
105     X11DRV_SetPixelFormat,           /* pSetPixelFormat */
106     NULL,                            /* pSetPolyFillMode */
107     NULL,                            /* pSetROP2 */
108     NULL,                            /* pSetRelAbs */
109     NULL,                            /* pSetStretchBltMode */
110     NULL,                            /* pSetTextAlign */
111     NULL,                            /* pSetTextCharacterExtra */
112     X11DRV_SetTextColor,             /* pSetTextColor */
113     NULL,                            /* pSetTextJustification */
114     NULL,                            /* pSetViewportExt (optional) */
115     NULL,                            /* pSetViewportOrg (optional) */
116     NULL,                            /* pSetWindowExt (optional) */
117     NULL,                            /* pSetWindowOrg (optional) */
118     NULL,                            /* pStartDoc */
119     NULL,                            /* pStartPage */
120     X11DRV_StretchBlt,               /* pStretchBlt */
121     NULL,                            /* pStretchDIBits */
122     NULL,                            /* pStrokeAndFillPath */
123     NULL,                            /* pStrokePath */
124     X11DRV_SwapBuffers,              /* pSwapBuffers */
125     NULL                             /* pWidenPath */
126 };
127
128 BITMAP_DRIVER X11DRV_BITMAP_Driver =
129 {
130   X11DRV_DIB_SetDIBits,
131   X11DRV_DIB_GetDIBits,
132   X11DRV_DIB_DeleteDIBSection,
133   X11DRV_DIB_SetDIBColorTable,
134   X11DRV_DIB_GetDIBColorTable,
135   X11DRV_DIB_Lock,
136   X11DRV_DIB_Unlock
137 };
138
139 PALETTE_DRIVER X11DRV_PALETTE_Driver =
140 {
141   X11DRV_PALETTE_SetMapping,
142   X11DRV_PALETTE_UpdateMapping,
143   X11DRV_PALETTE_IsDark
144 };
145
146 DeviceCaps X11DRV_DevCaps = {
147 /* version */           0, 
148 /* technology */        DT_RASDISPLAY,
149 /* size, resolution */  0, 0, 0, 0, 0, 
150 /* device objects */    1, -1, -1, 0, 0, -1, 1152,      
151 /* curve caps */        CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES |
152                         CC_WIDE | CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT,
153 /* line caps */         LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
154                         LC_STYLED | LC_WIDESTYLED | LC_INTERIORS,
155 /* polygon caps */      PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON |
156                         PC_SCANLINE | PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS,
157 /* text caps */         0,
158 /* regions */           CP_REGION,
159 /* raster caps */       RC_BITBLT | RC_BANDING | RC_SCALING | RC_BITMAP64 |
160                         RC_DI_BITMAP | RC_DIBTODEV | RC_BIGFONT | RC_STRETCHBLT | RC_STRETCHDIB | RC_DEVBITS,
161 /* aspects */           36, 36, 51,
162 /* pad1 */              { 0 },
163 /* log pixels */        0, 0, 
164 /* pad2 */              { 0 },
165 /* palette size */      0,
166 /* ..etc */             0, 0 };
167
168 /**********************************************************************
169  *           X11DRV_GDI_Initialize
170  */
171 BOOL X11DRV_GDI_Initialize(void)
172 {
173     BITMAP_Driver = &X11DRV_BITMAP_Driver;
174     PALETTE_Driver = &X11DRV_PALETTE_Driver;
175
176     /* FIXME: colormap management should be merged with the X11DRV */
177
178     if( !X11DRV_DIB_Init() ) return FALSE;
179
180     if( !X11DRV_PALETTE_Init() ) return FALSE;
181
182     if( !X11DRV_OBM_Init() ) return FALSE;
183
184     /* Finish up device caps */
185
186 #if 0
187     TRACE("Height = %-4i pxl, %-4i mm, Width  = %-4i pxl, %-4i mm\n",
188           HeightOfScreen(X11DRV_GetXScreen()), HeightMMOfScreen(X11DRV_GetXScreen()),
189           WidthOfScreen(X11DRV_GetXScreen()), WidthMMOfScreen(X11DRV_GetXScreen()) );
190 #endif
191
192     X11DRV_DevCaps.version   = 0x300;
193     X11DRV_DevCaps.horzSize  = WidthMMOfScreen(screen) * screen_width / WidthOfScreen(screen);
194     X11DRV_DevCaps.vertSize  = HeightMMOfScreen(screen) * screen_height / HeightOfScreen(screen);
195     X11DRV_DevCaps.horzRes   = screen_width;
196     X11DRV_DevCaps.vertRes   = screen_height;
197     X11DRV_DevCaps.bitsPixel = screen_depth;
198
199     /* MSDN: Number of entries in the device's color table, if the device has
200      * a color depth of no more than 8 bits per pixel.For devices with greater
201      * color depths, -1 is returned.
202      */
203     X11DRV_DevCaps.numColors = (screen_depth>8)?-1:(1<<screen_depth);
204  
205     /* Resolution will be adjusted during the font init */
206
207     X11DRV_DevCaps.logPixelsX = (int)(X11DRV_DevCaps.horzRes * 25.4 / X11DRV_DevCaps.horzSize);
208     X11DRV_DevCaps.logPixelsY = (int)(X11DRV_DevCaps.vertRes * 25.4 / X11DRV_DevCaps.vertSize);
209
210     /* Create default bitmap */
211
212     if (!X11DRV_BITMAP_Init()) return FALSE;
213
214     /* Initialize brush dithering */
215
216     if (!X11DRV_BRUSH_Init()) return FALSE;
217
218     /* Initialize fonts and text caps */
219
220     if (!X11DRV_FONT_Init( &X11DRV_DevCaps )) return FALSE;
221
222     return DRIVER_RegisterDriver( "DISPLAY", &X11DRV_DC_Funcs );
223 }
224
225 /**********************************************************************
226  *           X11DRV_GDI_Finalize
227  */
228 void X11DRV_GDI_Finalize(void)
229 {
230   X11DRV_PALETTE_Cleanup();
231 }
232
233 /**********************************************************************
234  *           X11DRV_CreateDC
235  */
236 static BOOL X11DRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
237                                LPCSTR output, const DEVMODEA* initData )
238 {
239     X11DRV_PDEVICE *physDev;
240
241     dc->physDev = physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
242                                        sizeof(*physDev) );
243     if(!physDev) {
244         ERR("Can't allocate physDev\n");
245         return FALSE;
246     }
247
248     dc->devCaps      = &X11DRV_DevCaps;
249     if (dc->flags & DC_MEMORY)
250     {
251         BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( dc->hBitmap, BITMAP_MAGIC );
252         if (!bmp) 
253         {
254             HeapFree( GetProcessHeap(), 0, physDev );
255             return FALSE;
256         }
257         if (!bmp->physBitmap) X11DRV_CreateBitmap( dc->hBitmap );
258         physDev->drawable  = (Pixmap)bmp->physBitmap;
259         physDev->gc        = TSXCreateGC(display, physDev->drawable, 0, NULL);
260         dc->bitsPerPixel = bmp->bitmap.bmBitsPixel;
261
262         dc->totalExtent.left   = 0;
263         dc->totalExtent.top    = 0;
264         dc->totalExtent.right  = bmp->bitmap.bmWidth;
265         dc->totalExtent.bottom = bmp->bitmap.bmHeight;
266         dc->hVisRgn            = CreateRectRgnIndirect( &dc->totalExtent );
267
268         GDI_ReleaseObj( dc->hBitmap );
269     }
270     else
271     {
272         physDev->drawable  = X11DRV_GetXRootWindow();
273         physDev->gc        = TSXCreateGC( display, physDev->drawable, 0, NULL );
274         dc->bitsPerPixel = screen_depth;
275
276         dc->totalExtent.left   = 0;
277         dc->totalExtent.top    = 0;
278         dc->totalExtent.right  = screen_width;
279         dc->totalExtent.bottom = screen_height;
280         dc->hVisRgn            = CreateRectRgnIndirect( &dc->totalExtent );
281     }
282
283     physDev->current_pf   = 0;
284     physDev->used_visuals = 0;
285     
286     if (!dc->hVisRgn)
287     {
288         TSXFreeGC( display, physDev->gc );
289         HeapFree( GetProcessHeap(), 0, physDev );
290         return FALSE;
291     }
292
293     TSXSetGraphicsExposures( display, physDev->gc, False );
294     TSXSetSubwindowMode( display, physDev->gc, IncludeInferiors );
295
296     return TRUE;
297 }
298
299
300 /**********************************************************************
301  *           X11DRV_DeleteDC
302  */
303 static BOOL X11DRV_DeleteDC( DC *dc )
304 {
305     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
306     TSXFreeGC( display, physDev->gc );
307     while (physDev->used_visuals-- > 0)
308       TSXFree(physDev->visuals[physDev->used_visuals]);
309     HeapFree( GetProcessHeap(), 0, physDev );
310     dc->physDev = NULL;
311     return TRUE;
312 }
313
314 /**********************************************************************
315  *           X11DRV_Escape
316  */
317 static INT X11DRV_Escape( DC *dc, INT nEscape, INT cbInput,
318                             SEGPTR lpInData, SEGPTR lpOutData )
319 {
320     switch( nEscape )
321     {
322         case GETSCALINGFACTOR:
323              if( lpOutData )
324              {
325                  LPPOINT16 lppt = MapSL(lpOutData);
326                  lppt->x = lppt->y = 0; /* no device scaling */
327                  return 1;
328              }
329              break;
330     }
331     return 0;
332 }
333