winex11: Add window data structure locking to the ConfigureNotify event handler.
[wine] / dlls / winex11.drv / init.c
1 /*
2  * X11 graphics driver initialisation functions
3  *
4  * Copyright 1996 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #include <string.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "x11drv.h"
30 #include "ddrawi.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
34
35 Display *gdi_display;  /* display to use for all GDI functions */
36
37 /* a few dynamic device caps */
38 static int log_pixels_x;  /* pixels per logical inch in x direction */
39 static int log_pixels_y;  /* pixels per logical inch in y direction */
40 static int horz_size;     /* horz. size of screen in millimeters */
41 static int vert_size;     /* vert. size of screen in millimeters */
42 static int palette_size;
43 static int device_init_done;
44
45 static Pixmap stock_bitmap_pixmap;  /* phys bitmap for the default stock bitmap */
46
47 static const WCHAR dpi_key_name[] = {'S','o','f','t','w','a','r','e','\\','F','o','n','t','s','\0'};
48 static const WCHAR dpi_value_name[] = {'L','o','g','P','i','x','e','l','s','\0'};
49
50 static const struct gdi_dc_funcs x11drv_funcs;
51 static const struct gdi_dc_funcs *xrender_funcs;
52
53 /******************************************************************************
54  *      get_dpi
55  *
56  * get the dpi from the registry
57  */
58 static DWORD get_dpi( void )
59 {
60     DWORD dpi = 96;
61     HKEY hkey;
62
63     if (RegOpenKeyW(HKEY_CURRENT_CONFIG, dpi_key_name, &hkey) == ERROR_SUCCESS)
64     {
65         DWORD type, size, new_dpi;
66
67         size = sizeof(new_dpi);
68         if(RegQueryValueExW(hkey, dpi_value_name, NULL, &type, (void *)&new_dpi, &size) == ERROR_SUCCESS)
69         {
70             if(type == REG_DWORD && new_dpi != 0)
71                 dpi = new_dpi;
72         }
73         RegCloseKey(hkey);
74     }
75     return dpi;
76 }
77
78 /**********************************************************************
79  *           device_init
80  *
81  * Perform initializations needed upon creation of the first device.
82  */
83 static void device_init(void)
84 {
85     device_init_done = TRUE;
86
87     /* Initialize XRender */
88     xrender_funcs = X11DRV_XRender_Init();
89
90     /* Init Xcursor */
91     X11DRV_Xcursor_Init();
92
93     palette_size = X11DRV_PALETTE_Init();
94
95     stock_bitmap_pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
96
97     /* Initialize device caps */
98     log_pixels_x = log_pixels_y = get_dpi();
99     horz_size = MulDiv( screen_width, 254, log_pixels_x * 10 );
100     vert_size = MulDiv( screen_height, 254, log_pixels_y * 10 );
101 }
102
103
104 static X11DRV_PDEVICE *create_x11_physdev( Drawable drawable )
105 {
106     X11DRV_PDEVICE *physDev;
107
108     if (!device_init_done) device_init();
109
110     if (!(physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) ))) return NULL;
111
112     physDev->drawable = drawable;
113     physDev->gc = XCreateGC( gdi_display, drawable, 0, NULL );
114     XSetGraphicsExposures( gdi_display, physDev->gc, False );
115     XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
116     XFlush( gdi_display );
117     return physDev;
118 }
119
120 /**********************************************************************
121  *           X11DRV_CreateDC
122  */
123 static BOOL X11DRV_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
124                              LPCWSTR output, const DEVMODEW* initData )
125 {
126     const struct gdi_dc_funcs *glx_funcs = get_glx_driver();
127     X11DRV_PDEVICE *physDev = create_x11_physdev( root_window );
128
129     if (!physDev) return FALSE;
130
131     physDev->depth         = screen_depth;
132     physDev->color_shifts  = &X11DRV_PALETTE_default_shifts;
133     SetRect( &physDev->dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
134              virtual_screen_rect.bottom - virtual_screen_rect.top );
135     push_dc_driver( pdev, &physDev->dev, &x11drv_funcs );
136     if (xrender_funcs && !xrender_funcs->pCreateDC( pdev, driver, device, output, initData )) return FALSE;
137     if (glx_funcs && !glx_funcs->pCreateDC( pdev, driver, device, output, initData )) return FALSE;
138     return TRUE;
139 }
140
141
142 /**********************************************************************
143  *           X11DRV_CreateCompatibleDC
144  */
145 static BOOL X11DRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
146 {
147     const struct gdi_dc_funcs *glx_funcs = get_glx_driver();
148     X11DRV_PDEVICE *physDev = create_x11_physdev( stock_bitmap_pixmap );
149
150     if (!physDev) return FALSE;
151
152     physDev->depth  = 1;
153     SetRect( &physDev->dc_rect, 0, 0, 1, 1 );
154     push_dc_driver( pdev, &physDev->dev, &x11drv_funcs );
155     if (orig) return TRUE;  /* we already went through Xrender if we have an orig device */
156     if (xrender_funcs && !xrender_funcs->pCreateCompatibleDC( NULL, pdev )) return FALSE;
157     if (glx_funcs && !glx_funcs->pCreateCompatibleDC( NULL, pdev )) return FALSE;
158     return TRUE;
159 }
160
161
162 /**********************************************************************
163  *           X11DRV_DeleteDC
164  */
165 static BOOL X11DRV_DeleteDC( PHYSDEV dev )
166 {
167     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
168
169     XFreeGC( gdi_display, physDev->gc );
170     HeapFree( GetProcessHeap(), 0, physDev );
171     return TRUE;
172 }
173
174
175 void add_device_bounds( X11DRV_PDEVICE *dev, const RECT *rect )
176 {
177     RECT rc;
178
179     if (!dev->bounds) return;
180     if (dev->region && GetRgnBox( dev->region, &rc ))
181     {
182         if (IntersectRect( &rc, &rc, rect )) add_bounds_rect( dev->bounds, &rc );
183     }
184     else add_bounds_rect( dev->bounds, rect );
185 }
186
187 /***********************************************************************
188  *           X11DRV_SetBoundsRect
189  */
190 static UINT X11DRV_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
191 {
192     X11DRV_PDEVICE *pdev = get_x11drv_dev( dev );
193
194     if (flags & DCB_DISABLE) pdev->bounds = NULL;
195     else if (flags & DCB_ENABLE) pdev->bounds = rect;
196     return DCB_RESET;  /* we don't have device-specific bounds */
197 }
198
199
200 /***********************************************************************
201  *           GetDeviceCaps    (X11DRV.@)
202  */
203 static INT X11DRV_GetDeviceCaps( PHYSDEV dev, INT cap )
204 {
205     switch(cap)
206     {
207     case DRIVERVERSION:
208         return 0x300;
209     case TECHNOLOGY:
210         return DT_RASDISPLAY;
211     case HORZSIZE:
212         return horz_size;
213     case VERTSIZE:
214         return vert_size;
215     case HORZRES:
216         return screen_width;
217     case VERTRES:
218         return screen_height;
219     case DESKTOPHORZRES:
220         return virtual_screen_rect.right - virtual_screen_rect.left;
221     case DESKTOPVERTRES:
222         return virtual_screen_rect.bottom - virtual_screen_rect.top;
223     case BITSPIXEL:
224         return screen_bpp;
225     case PLANES:
226         return 1;
227     case NUMBRUSHES:
228         return -1;
229     case NUMPENS:
230         return -1;
231     case NUMMARKERS:
232         return 0;
233     case NUMFONTS:
234         return 0;
235     case NUMCOLORS:
236         /* MSDN: Number of entries in the device's color table, if the device has
237          * a color depth of no more than 8 bits per pixel.For devices with greater
238          * color depths, -1 is returned. */
239         return (screen_depth > 8) ? -1 : (1 << screen_depth);
240     case PDEVICESIZE:
241         return sizeof(X11DRV_PDEVICE);
242     case CURVECAPS:
243         return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
244                 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
245     case LINECAPS:
246         return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
247                 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
248     case POLYGONALCAPS:
249         return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
250                 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
251     case TEXTCAPS:
252         return (TC_OP_CHARACTER | TC_OP_STROKE | TC_CP_STROKE |
253                 TC_CR_ANY | TC_SF_X_YINDEP | TC_SA_DOUBLE | TC_SA_INTEGER |
254                 TC_SA_CONTIN | TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE | TC_VA_ABLE);
255     case CLIPCAPS:
256         return CP_REGION;
257     case COLORRES:
258         /* The observed correspondence between BITSPIXEL and COLORRES is:
259          * BITSPIXEL: 8  -> COLORRES: 18
260          * BITSPIXEL: 16 -> COLORRES: 16
261          * BITSPIXEL: 24 -> COLORRES: 24
262          * BITSPIXEL: 32 -> COLORRES: 24 */
263         return (screen_bpp <= 8) ? 18 : min( 24, screen_bpp );
264     case RASTERCAPS:
265         return (RC_BITBLT | RC_BANDING | RC_SCALING | RC_BITMAP64 | RC_DI_BITMAP |
266                 RC_DIBTODEV | RC_BIGFONT | RC_STRETCHBLT | RC_STRETCHDIB | RC_DEVBITS |
267                 (palette_size ? RC_PALETTE : 0));
268     case SHADEBLENDCAPS:
269         return (SB_GRAD_RECT | SB_GRAD_TRI | SB_CONST_ALPHA | SB_PIXEL_ALPHA);
270     case ASPECTX:
271     case ASPECTY:
272         return 36;
273     case ASPECTXY:
274         return 51;
275     case LOGPIXELSX:
276         return log_pixels_x;
277     case LOGPIXELSY:
278         return log_pixels_y;
279     case CAPS1:
280         FIXME("(%p): CAPS1 is unimplemented, will return 0\n", dev->hdc );
281         /* please see wingdi.h for the possible bit-flag values that need
282            to be returned. */
283         return 0;
284     case SIZEPALETTE:
285         return palette_size;
286     case NUMRESERVED:
287     case PHYSICALWIDTH:
288     case PHYSICALHEIGHT:
289     case PHYSICALOFFSETX:
290     case PHYSICALOFFSETY:
291     case SCALINGFACTORX:
292     case SCALINGFACTORY:
293     case VREFRESH:
294     case BLTALIGNMENT:
295         return 0;
296     default:
297         FIXME("(%p): unsupported capability %d, will return 0\n", dev->hdc, cap );
298         return 0;
299     }
300 }
301
302
303 /**********************************************************************
304  *           ExtEscape  (X11DRV.@)
305  */
306 static INT X11DRV_ExtEscape( PHYSDEV dev, INT escape, INT in_count, LPCVOID in_data,
307                       INT out_count, LPVOID out_data )
308 {
309     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
310
311     switch(escape)
312     {
313     case QUERYESCSUPPORT:
314         if (in_data)
315         {
316             switch (*(const INT *)in_data)
317             {
318             case DCICOMMAND:
319                 return DD_HAL_VERSION;
320             case X11DRV_ESCAPE:
321                 return TRUE;
322             }
323         }
324         break;
325
326     case X11DRV_ESCAPE:
327         if (in_data && in_count >= sizeof(enum x11drv_escape_codes))
328         {
329             switch(*(const enum x11drv_escape_codes *)in_data)
330             {
331             case X11DRV_SET_DRAWABLE:
332                 if (in_count >= sizeof(struct x11drv_escape_set_drawable))
333                 {
334                     const struct x11drv_escape_set_drawable *data = in_data;
335                     physDev->dc_rect = data->dc_rect;
336                     physDev->drawable = data->drawable;
337                     XSetSubwindowMode( gdi_display, physDev->gc, data->mode );
338                     TRACE( "SET_DRAWABLE hdc %p drawable %lx dc_rect %s\n",
339                            dev->hdc, physDev->drawable, wine_dbgstr_rect(&physDev->dc_rect) );
340                     return TRUE;
341                 }
342                 break;
343             case X11DRV_GET_DRAWABLE:
344                 if (out_count >= sizeof(struct x11drv_escape_get_drawable))
345                 {
346                     struct x11drv_escape_get_drawable *data = out_data;
347                     data->drawable = physDev->drawable;
348                     return TRUE;
349                 }
350                 break;
351             case X11DRV_START_EXPOSURES:
352                 XSetGraphicsExposures( gdi_display, physDev->gc, True );
353                 physDev->exposures = 0;
354                 return TRUE;
355             case X11DRV_END_EXPOSURES:
356                 if (out_count >= sizeof(HRGN))
357                 {
358                     HRGN hrgn = 0, tmp = 0;
359
360                     XSetGraphicsExposures( gdi_display, physDev->gc, False );
361                     if (physDev->exposures)
362                     {
363                         for (;;)
364                         {
365                             XEvent event;
366
367                             XWindowEvent( gdi_display, physDev->drawable, ~0, &event );
368                             if (event.type == NoExpose) break;
369                             if (event.type == GraphicsExpose)
370                             {
371                                 RECT rect;
372
373                                 rect.left   = event.xgraphicsexpose.x - physDev->dc_rect.left;
374                                 rect.top    = event.xgraphicsexpose.y - physDev->dc_rect.top;
375                                 rect.right  = rect.left + event.xgraphicsexpose.width;
376                                 rect.bottom = rect.top + event.xgraphicsexpose.height;
377                                 if (GetLayout( dev->hdc ) & LAYOUT_RTL)
378                                     mirror_rect( &physDev->dc_rect, &rect );
379
380                                 TRACE( "got %s count %d\n", wine_dbgstr_rect(&rect),
381                                        event.xgraphicsexpose.count );
382
383                                 if (!tmp) tmp = CreateRectRgnIndirect( &rect );
384                                 else SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
385                                 if (hrgn) CombineRgn( hrgn, hrgn, tmp, RGN_OR );
386                                 else
387                                 {
388                                     hrgn = tmp;
389                                     tmp = 0;
390                                 }
391                                 if (!event.xgraphicsexpose.count) break;
392                             }
393                             else
394                             {
395                                 ERR( "got unexpected event %d\n", event.type );
396                                 break;
397                             }
398                         }
399                         if (tmp) DeleteObject( tmp );
400                     }
401                     *(HRGN *)out_data = hrgn;
402                     return TRUE;
403                 }
404                 break;
405             default:
406                 break;
407             }
408         }
409         break;
410     }
411     return 0;
412 }
413
414
415 static const struct gdi_dc_funcs x11drv_funcs =
416 {
417     NULL,                               /* pAbortDoc */
418     NULL,                               /* pAbortPath */
419     NULL,                               /* pAlphaBlend */
420     NULL,                               /* pAngleArc */
421     X11DRV_Arc,                         /* pArc */
422     NULL,                               /* pArcTo */
423     NULL,                               /* pBeginPath */
424     NULL,                               /* pBlendImage */
425     X11DRV_Chord,                       /* pChord */
426     NULL,                               /* pCloseFigure */
427     X11DRV_CreateCompatibleDC,          /* pCreateCompatibleDC */
428     X11DRV_CreateDC,                    /* pCreateDC */
429     X11DRV_DeleteDC,                    /* pDeleteDC */
430     NULL,                               /* pDeleteObject */
431     NULL,                               /* pDeviceCapabilities */
432     X11DRV_Ellipse,                     /* pEllipse */
433     NULL,                               /* pEndDoc */
434     NULL,                               /* pEndPage */
435     NULL,                               /* pEndPath */
436     NULL,                               /* pEnumFonts */
437     X11DRV_EnumICMProfiles,             /* pEnumICMProfiles */
438     NULL,                               /* pExcludeClipRect */
439     NULL,                               /* pExtDeviceMode */
440     X11DRV_ExtEscape,                   /* pExtEscape */
441     X11DRV_ExtFloodFill,                /* pExtFloodFill */
442     NULL,                               /* pExtSelectClipRgn */
443     NULL,                               /* pExtTextOut */
444     NULL,                               /* pFillPath */
445     NULL,                               /* pFillRgn */
446     NULL,                               /* pFlattenPath */
447     NULL,                               /* pFontIsLinked */
448     NULL,                               /* pFrameRgn */
449     NULL,                               /* pGdiComment */
450     NULL,                               /* pGdiRealizationInfo */
451     NULL,                               /* pGetBoundsRect */
452     NULL,                               /* pGetCharABCWidths */
453     NULL,                               /* pGetCharABCWidthsI */
454     NULL,                               /* pGetCharWidth */
455     X11DRV_GetDeviceCaps,               /* pGetDeviceCaps */
456     X11DRV_GetDeviceGammaRamp,          /* pGetDeviceGammaRamp */
457     NULL,                               /* pGetFontData */
458     NULL,                               /* pGetFontUnicodeRanges */
459     NULL,                               /* pGetGlyphIndices */
460     NULL,                               /* pGetGlyphOutline */
461     X11DRV_GetICMProfile,               /* pGetICMProfile */
462     X11DRV_GetImage,                    /* pGetImage */
463     NULL,                               /* pGetKerningPairs */
464     X11DRV_GetNearestColor,             /* pGetNearestColor */
465     NULL,                               /* pGetOutlineTextMetrics */
466     NULL,                               /* pGetPixel */
467     X11DRV_GetSystemPaletteEntries,     /* pGetSystemPaletteEntries */
468     NULL,                               /* pGetTextCharsetInfo */
469     NULL,                               /* pGetTextExtentExPoint */
470     NULL,                               /* pGetTextExtentExPointI */
471     NULL,                               /* pGetTextFace */
472     NULL,                               /* pGetTextMetrics */
473     X11DRV_GradientFill,                /* pGradientFill */
474     NULL,                               /* pIntersectClipRect */
475     NULL,                               /* pInvertRgn */
476     X11DRV_LineTo,                      /* pLineTo */
477     NULL,                               /* pModifyWorldTransform */
478     NULL,                               /* pMoveTo */
479     NULL,                               /* pOffsetClipRgn */
480     NULL,                               /* pOffsetViewportOrg */
481     NULL,                               /* pOffsetWindowOrg */
482     X11DRV_PaintRgn,                    /* pPaintRgn */
483     X11DRV_PatBlt,                      /* pPatBlt */
484     X11DRV_Pie,                         /* pPie */
485     NULL,                               /* pPolyBezier */
486     NULL,                               /* pPolyBezierTo */
487     NULL,                               /* pPolyDraw */
488     X11DRV_PolyPolygon,                 /* pPolyPolygon */
489     X11DRV_PolyPolyline,                /* pPolyPolyline */
490     X11DRV_Polygon,                     /* pPolygon */
491     NULL,                               /* pPolyline */
492     NULL,                               /* pPolylineTo */
493     X11DRV_PutImage,                    /* pPutImage */
494     X11DRV_RealizeDefaultPalette,       /* pRealizeDefaultPalette */
495     X11DRV_RealizePalette,              /* pRealizePalette */
496     X11DRV_Rectangle,                   /* pRectangle */
497     NULL,                               /* pResetDC */
498     NULL,                               /* pRestoreDC */
499     X11DRV_RoundRect,                   /* pRoundRect */
500     NULL,                               /* pSaveDC */
501     NULL,                               /* pScaleViewportExt */
502     NULL,                               /* pScaleWindowExt */
503     NULL,                               /* pSelectBitmap */
504     X11DRV_SelectBrush,                 /* pSelectBrush */
505     NULL,                               /* pSelectClipPath */
506     NULL,                               /* pSelectFont */
507     NULL,                               /* pSelectPalette */
508     X11DRV_SelectPen,                   /* pSelectPen */
509     NULL,                               /* pSetArcDirection */
510     NULL,                               /* pSetBkColor */
511     NULL,                               /* pSetBkMode */
512     X11DRV_SetBoundsRect,               /* pSetBoundsRect */
513     X11DRV_SetDCBrushColor,             /* pSetDCBrushColor */
514     X11DRV_SetDCPenColor,               /* pSetDCPenColor */
515     NULL,                               /* pSetDIBitsToDevice */
516     X11DRV_SetDeviceClipping,           /* pSetDeviceClipping */
517     X11DRV_SetDeviceGammaRamp,          /* pSetDeviceGammaRamp */
518     NULL,                               /* pSetLayout */
519     NULL,                               /* pSetMapMode */
520     NULL,                               /* pSetMapperFlags */
521     X11DRV_SetPixel,                    /* pSetPixel */
522     NULL,                               /* pSetPolyFillMode */
523     NULL,                               /* pSetROP2 */
524     NULL,                               /* pSetRelAbs */
525     NULL,                               /* pSetStretchBltMode */
526     NULL,                               /* pSetTextAlign */
527     NULL,                               /* pSetTextCharacterExtra */
528     NULL,                               /* pSetTextColor */
529     NULL,                               /* pSetTextJustification */
530     NULL,                               /* pSetViewportExt */
531     NULL,                               /* pSetViewportOrg */
532     NULL,                               /* pSetWindowExt */
533     NULL,                               /* pSetWindowOrg */
534     NULL,                               /* pSetWorldTransform */
535     NULL,                               /* pStartDoc */
536     NULL,                               /* pStartPage */
537     X11DRV_StretchBlt,                  /* pStretchBlt */
538     NULL,                               /* pStretchDIBits */
539     NULL,                               /* pStrokeAndFillPath */
540     NULL,                               /* pStrokePath */
541     X11DRV_UnrealizePalette,            /* pUnrealizePalette */
542     NULL,                               /* pWidenPath */
543     NULL,                               /* wine_get_wgl_driver */
544     GDI_PRIORITY_GRAPHICS_DRV           /* priority */
545 };
546
547
548 /******************************************************************************
549  *      X11DRV_get_gdi_driver
550  */
551 const struct gdi_dc_funcs * CDECL X11DRV_get_gdi_driver( unsigned int version )
552 {
553     if (version != WINE_GDI_DRIVER_VERSION)
554     {
555         ERR( "version mismatch, gdi32 wants %u but winex11 has %u\n", version, WINE_GDI_DRIVER_VERSION );
556         return NULL;
557     }
558     return &x11drv_funcs;
559 }