Create an X window for every window, including children.
[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 #include "ddrawi.h"
19
20 DEFAULT_DEBUG_CHANNEL(x11drv);
21
22 static BOOL X11DRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
23                                LPCSTR output, const DEVMODEA* initData );
24 static BOOL X11DRV_DeleteDC( DC *dc );
25
26 static INT X11DRV_Escape( DC *dc, INT nEscape, INT cbInput,
27                             SEGPTR lpInData, SEGPTR lpOutData );
28
29 const DC_FUNCTIONS X11DRV_DC_Funcs =
30 {
31     NULL,                            /* pAbortDoc */
32     NULL,                            /* pAbortPath */
33     NULL,                            /* pAngleArc */
34     X11DRV_Arc,                      /* pArc */
35     NULL,                            /* pArcTo */
36     NULL,                            /* pBeginPath */
37     X11DRV_BitBlt,                   /* pBitBlt */
38     X11DRV_BitmapBits,               /* pBitmapBits */
39     X11DRV_ChoosePixelFormat,        /* pChoosePixelFormat */
40     X11DRV_Chord,                    /* pChord */
41     NULL,                            /* pCloseFigure */
42     X11DRV_CreateBitmap,             /* pCreateBitmap */
43     X11DRV_CreateDC,                 /* pCreateDC */
44     X11DRV_DIB_CreateDIBSection,     /* pCreateDIBSection */
45     X11DRV_DIB_CreateDIBSection16,   /* pCreateDIBSection16 */
46     X11DRV_DeleteDC,                 /* pDeleteDC */
47     X11DRV_DeleteObject,             /* pDeleteObject */
48     X11DRV_DescribePixelFormat,      /* pDescribePixelFormat */
49     NULL,                            /* pDeviceCapabilities */
50     X11DRV_Ellipse,                  /* pEllipse */
51     NULL,                            /* pEndDoc */
52     NULL,                            /* pEndPage */
53     NULL,                            /* pEndPath */
54     X11DRV_EnumDeviceFonts,          /* pEnumDeviceFonts */
55     X11DRV_Escape,                   /* pEscape */
56     NULL,                            /* pExcludeClipRect */
57     NULL,                            /* pExtDeviceMode */
58     X11DRV_ExtFloodFill,             /* pExtFloodFill */
59     X11DRV_ExtTextOut,               /* pExtTextOut */
60     NULL,                            /* pFillPath */
61     NULL,                            /* pFillRgn */
62     NULL,                            /* pFlattenPath */
63     NULL,                            /* pFrameRgn */
64     X11DRV_GetCharWidth,             /* pGetCharWidth */
65     X11DRV_GetDCOrgEx,               /* pGetDCOrgEx */
66     X11DRV_GetDeviceGammaRamp,       /* pGetDeviceGammaRamp */
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,                            /* pMoveTo */
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_SetDeviceGammaRamp,       /* pSetDeviceGammaRamp */
104     X11DRV_SetDIBitsToDevice,        /* pSetDIBitsToDevice */
105     NULL,                            /* pSetMapMode (optional) */
106     NULL,                            /* pSetMapperFlags */
107     X11DRV_SetPixel,                 /* pSetPixel */
108     X11DRV_SetPixelFormat,           /* pSetPixelFormat */
109     NULL,                            /* pSetPolyFillMode */
110     NULL,                            /* pSetROP2 */
111     NULL,                            /* pSetRelAbs */
112     NULL,                            /* pSetStretchBltMode */
113     NULL,                            /* pSetTextAlign */
114     NULL,                            /* pSetTextCharacterExtra */
115     X11DRV_SetTextColor,             /* pSetTextColor */
116     NULL,                            /* pSetTextJustification */
117     NULL,                            /* pSetViewportExt (optional) */
118     NULL,                            /* pSetViewportOrg (optional) */
119     NULL,                            /* pSetWindowExt (optional) */
120     NULL,                            /* pSetWindowOrg (optional) */
121     NULL,                            /* pStartDoc */
122     NULL,                            /* pStartPage */
123     X11DRV_StretchBlt,               /* pStretchBlt */
124     NULL,                            /* pStretchDIBits */
125     NULL,                            /* pStrokeAndFillPath */
126     NULL,                            /* pStrokePath */
127     X11DRV_SwapBuffers,              /* pSwapBuffers */
128     NULL                             /* pWidenPath */
129 };
130
131 BITMAP_DRIVER X11DRV_BITMAP_Driver =
132 {
133   X11DRV_DIB_SetDIBits,
134   X11DRV_DIB_GetDIBits,
135   X11DRV_DIB_DeleteDIBSection,
136   X11DRV_DIB_SetDIBColorTable,
137   X11DRV_DIB_GetDIBColorTable,
138   X11DRV_DIB_Lock,
139   X11DRV_DIB_Unlock
140 };
141
142 PALETTE_DRIVER X11DRV_PALETTE_Driver =
143 {
144   X11DRV_PALETTE_SetMapping,
145   X11DRV_PALETTE_UpdateMapping,
146   X11DRV_PALETTE_IsDark
147 };
148
149 DeviceCaps X11DRV_DevCaps = {
150 /* version */           0, 
151 /* technology */        DT_RASDISPLAY,
152 /* size, resolution */  0, 0, 0, 0, 0, 
153 /* device objects */    1, -1, -1, 0, 0, -1, 1152,      
154 /* curve caps */        CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES |
155                         CC_WIDE | CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT,
156 /* line caps */         LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
157                         LC_STYLED | LC_WIDESTYLED | LC_INTERIORS,
158 /* polygon caps */      PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON |
159                         PC_SCANLINE | PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS,
160 /* text caps */         0,
161 /* regions */           CP_REGION,
162 /* raster caps */       RC_BITBLT | RC_BANDING | RC_SCALING | RC_BITMAP64 |
163                         RC_DI_BITMAP | RC_DIBTODEV | RC_BIGFONT | RC_STRETCHBLT | RC_STRETCHDIB | RC_DEVBITS,
164 /* aspects */           36, 36, 51,
165 /* pad1 */              { 0 },
166 /* log pixels */        0, 0, 
167 /* pad2 */              { 0 },
168 /* palette size */      0,
169 /* ..etc */             0, 0 };
170
171
172 Display *gdi_display;  /* display to use for all GDI functions */
173
174
175 /**********************************************************************
176  *           X11DRV_GDI_Initialize
177  */
178 BOOL X11DRV_GDI_Initialize( Display *display )
179 {
180     Screen *screen = DefaultScreenOfDisplay(display);
181
182     gdi_display = display;
183     BITMAP_Driver = &X11DRV_BITMAP_Driver;
184     PALETTE_Driver = &X11DRV_PALETTE_Driver;
185
186     /* FIXME: colormap management should be merged with the X11DRV */
187
188     if( !X11DRV_PALETTE_Init() ) return FALSE;
189
190     if( !X11DRV_OBM_Init() ) return FALSE;
191
192     /* Finish up device caps */
193
194     X11DRV_DevCaps.version   = 0x300;
195     X11DRV_DevCaps.horzSize  = WidthMMOfScreen(screen) * screen_width / WidthOfScreen(screen);
196     X11DRV_DevCaps.vertSize  = HeightMMOfScreen(screen) * screen_height / HeightOfScreen(screen);
197     X11DRV_DevCaps.horzRes   = screen_width;
198     X11DRV_DevCaps.vertRes   = screen_height;
199     X11DRV_DevCaps.bitsPixel = screen_depth;
200
201     /* MSDN: Number of entries in the device's color table, if the device has
202      * a color depth of no more than 8 bits per pixel.For devices with greater
203      * color depths, -1 is returned.
204      */
205     X11DRV_DevCaps.numColors = (screen_depth>8)?-1:(1<<screen_depth);
206  
207     /* Resolution will be adjusted during the font init */
208
209     X11DRV_DevCaps.logPixelsX = (int)(X11DRV_DevCaps.horzRes * 25.4 / X11DRV_DevCaps.horzSize);
210     X11DRV_DevCaps.logPixelsY = (int)(X11DRV_DevCaps.vertRes * 25.4 / X11DRV_DevCaps.vertSize);
211
212     /* Create default bitmap */
213
214     if (!X11DRV_BITMAP_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     XCloseDisplay( gdi_display );
230     gdi_display = NULL;
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( gdi_display, physDev->drawable, 0, NULL );
260         dc->bitsPerPixel       = bmp->bitmap.bmBitsPixel;
261         dc->totalExtent.left   = 0;
262         dc->totalExtent.top    = 0;
263         dc->totalExtent.right  = bmp->bitmap.bmWidth;
264         dc->totalExtent.bottom = bmp->bitmap.bmHeight;
265         GDI_ReleaseObj( dc->hBitmap );
266     }
267     else
268     {
269         physDev->drawable  = root_window;
270         physDev->gc        = TSXCreateGC( gdi_display, physDev->drawable, 0, NULL );
271         dc->bitsPerPixel       = screen_depth;
272         dc->totalExtent.left   = 0;
273         dc->totalExtent.top    = 0;
274         dc->totalExtent.right  = screen_width;
275         dc->totalExtent.bottom = screen_height;
276     }
277
278     physDev->current_pf   = 0;
279     physDev->used_visuals = 0;
280
281     if (!(dc->hVisRgn = CreateRectRgnIndirect( &dc->totalExtent )))
282     {
283         TSXFreeGC( gdi_display, physDev->gc );
284         HeapFree( GetProcessHeap(), 0, physDev );
285         return FALSE;
286     }
287
288     wine_tsx11_lock();
289     XSetGraphicsExposures( gdi_display, physDev->gc, False );
290     XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
291     XFlush( gdi_display );
292     wine_tsx11_unlock();
293     return TRUE;
294 }
295
296
297 /**********************************************************************
298  *           X11DRV_DeleteDC
299  */
300 static BOOL X11DRV_DeleteDC( DC *dc )
301 {
302     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
303     wine_tsx11_lock();
304     XFreeGC( gdi_display, physDev->gc );
305     while (physDev->used_visuals-- > 0)
306         XFree(physDev->visuals[physDev->used_visuals]);
307     wine_tsx11_unlock();
308     HeapFree( GetProcessHeap(), 0, physDev );
309     dc->physDev = NULL;
310     return TRUE;
311 }
312
313 /**********************************************************************
314  *           X11DRV_Escape
315  */
316 static INT X11DRV_Escape( DC *dc, INT nEscape, INT cbInput,
317                             SEGPTR lpInData, SEGPTR lpOutData )
318 {
319     switch( nEscape )
320     {
321         case QUERYESCSUPPORT:
322              if( lpInData )
323              {
324                  LPINT16 lpEscape = MapSL(lpInData);
325                  switch (*lpEscape)
326                  {
327                      case DCICOMMAND:
328                          return DD_HAL_VERSION;
329                  }
330              }
331              break;
332
333         case GETSCALINGFACTOR:
334              if( lpOutData )
335              {
336                  LPPOINT16 lppt = MapSL(lpOutData);
337                  lppt->x = lppt->y = 0; /* no device scaling */
338                  return 1;
339              }
340              break;
341
342         case DCICOMMAND:
343              if( lpInData )
344              {
345                  LPDCICMD lpCmd = MapSL(lpInData);
346                  if (lpCmd->dwVersion != DD_VERSION) break;
347                  return X11DRV_DCICommand(cbInput, lpCmd, MapSL(lpOutData));
348              }
349              break;
350
351     }
352     return 0;
353 }