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