2 * X11 graphics driver initialisation functions
4 * Copyright 1996 Alexandre Julliard
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.
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.
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
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
35 Display *gdi_display; /* display to use for all GDI functions */
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;
45 static Pixmap stock_bitmap_pixmap; /* phys bitmap for the default stock bitmap */
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'};
50 static const struct gdi_dc_funcs x11drv_funcs;
51 static const struct gdi_dc_funcs *xrender_funcs;
53 /******************************************************************************
56 * get the dpi from the registry
58 static DWORD get_dpi( void )
63 if (RegOpenKeyW(HKEY_CURRENT_CONFIG, dpi_key_name, &hkey) == ERROR_SUCCESS)
65 DWORD type, size, new_dpi;
67 size = sizeof(new_dpi);
68 if(RegQueryValueExW(hkey, dpi_value_name, NULL, &type, (void *)&new_dpi, &size) == ERROR_SUCCESS)
70 if(type == REG_DWORD && new_dpi != 0)
78 /**********************************************************************
81 * Perform initializations needed upon creation of the first device.
83 static void device_init(void)
85 device_init_done = TRUE;
87 /* Initialize XRender */
88 xrender_funcs = X11DRV_XRender_Init();
91 X11DRV_Xcursor_Init();
93 palette_size = X11DRV_PALETTE_Init();
95 stock_bitmap_pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
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 );
104 static X11DRV_PDEVICE *create_x11_physdev( Drawable drawable )
106 X11DRV_PDEVICE *physDev;
108 if (!device_init_done) device_init();
110 if (!(physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) ))) return NULL;
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 );
120 /**********************************************************************
123 static BOOL X11DRV_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
124 LPCWSTR output, const DEVMODEW* initData )
126 X11DRV_PDEVICE *physDev = create_x11_physdev( root_window );
128 if (!physDev) return FALSE;
130 physDev->depth = default_visual.depth;
131 physDev->color_shifts = &X11DRV_PALETTE_default_shifts;
132 SetRect( &physDev->dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
133 virtual_screen_rect.bottom - virtual_screen_rect.top );
134 push_dc_driver( pdev, &physDev->dev, &x11drv_funcs );
135 if (xrender_funcs && !xrender_funcs->pCreateDC( pdev, driver, device, output, initData )) return FALSE;
140 /**********************************************************************
141 * X11DRV_CreateCompatibleDC
143 static BOOL X11DRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
145 X11DRV_PDEVICE *physDev = create_x11_physdev( stock_bitmap_pixmap );
147 if (!physDev) return FALSE;
150 SetRect( &physDev->dc_rect, 0, 0, 1, 1 );
151 push_dc_driver( pdev, &physDev->dev, &x11drv_funcs );
152 if (orig) return TRUE; /* we already went through Xrender if we have an orig device */
153 if (xrender_funcs && !xrender_funcs->pCreateCompatibleDC( NULL, pdev )) return FALSE;
158 /**********************************************************************
161 static BOOL X11DRV_DeleteDC( PHYSDEV dev )
163 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
165 XFreeGC( gdi_display, physDev->gc );
166 HeapFree( GetProcessHeap(), 0, physDev );
171 void add_device_bounds( X11DRV_PDEVICE *dev, const RECT *rect )
175 if (!dev->bounds) return;
176 if (dev->region && GetRgnBox( dev->region, &rc ))
178 if (IntersectRect( &rc, &rc, rect )) add_bounds_rect( dev->bounds, &rc );
180 else add_bounds_rect( dev->bounds, rect );
183 /***********************************************************************
184 * X11DRV_SetBoundsRect
186 static UINT X11DRV_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
188 X11DRV_PDEVICE *pdev = get_x11drv_dev( dev );
190 if (flags & DCB_DISABLE) pdev->bounds = NULL;
191 else if (flags & DCB_ENABLE) pdev->bounds = rect;
192 return DCB_RESET; /* we don't have device-specific bounds */
196 /***********************************************************************
197 * GetDeviceCaps (X11DRV.@)
199 static INT X11DRV_GetDeviceCaps( PHYSDEV dev, INT cap )
206 return DT_RASDISPLAY;
214 return screen_height;
216 return virtual_screen_rect.right - virtual_screen_rect.left;
218 return virtual_screen_rect.bottom - virtual_screen_rect.top;
232 /* MSDN: Number of entries in the device's color table, if the device has
233 * a color depth of no more than 8 bits per pixel.For devices with greater
234 * color depths, -1 is returned. */
235 return (default_visual.depth > 8) ? -1 : (1 << default_visual.depth);
237 return sizeof(X11DRV_PDEVICE);
239 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
240 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
242 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
243 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
245 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
246 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
248 return (TC_OP_CHARACTER | TC_OP_STROKE | TC_CP_STROKE |
249 TC_CR_ANY | TC_SF_X_YINDEP | TC_SA_DOUBLE | TC_SA_INTEGER |
250 TC_SA_CONTIN | TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE | TC_VA_ABLE);
254 /* The observed correspondence between BITSPIXEL and COLORRES is:
255 * BITSPIXEL: 8 -> COLORRES: 18
256 * BITSPIXEL: 16 -> COLORRES: 16
257 * BITSPIXEL: 24 -> COLORRES: 24
258 * BITSPIXEL: 32 -> COLORRES: 24 */
259 return (screen_bpp <= 8) ? 18 : min( 24, screen_bpp );
261 return (RC_BITBLT | RC_BANDING | RC_SCALING | RC_BITMAP64 | RC_DI_BITMAP |
262 RC_DIBTODEV | RC_BIGFONT | RC_STRETCHBLT | RC_STRETCHDIB | RC_DEVBITS |
263 (palette_size ? RC_PALETTE : 0));
265 return (SB_GRAD_RECT | SB_GRAD_TRI | SB_CONST_ALPHA | SB_PIXEL_ALPHA);
276 FIXME("(%p): CAPS1 is unimplemented, will return 0\n", dev->hdc );
277 /* please see wingdi.h for the possible bit-flag values that need
285 case PHYSICALOFFSETX:
286 case PHYSICALOFFSETY:
293 FIXME("(%p): unsupported capability %d, will return 0\n", dev->hdc, cap );
299 /**********************************************************************
300 * ExtEscape (X11DRV.@)
302 static INT X11DRV_ExtEscape( PHYSDEV dev, INT escape, INT in_count, LPCVOID in_data,
303 INT out_count, LPVOID out_data )
305 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
309 case QUERYESCSUPPORT:
312 switch (*(const INT *)in_data)
315 return DD_HAL_VERSION;
323 if (in_data && in_count >= sizeof(enum x11drv_escape_codes))
325 switch(*(const enum x11drv_escape_codes *)in_data)
327 case X11DRV_SET_DRAWABLE:
328 if (in_count >= sizeof(struct x11drv_escape_set_drawable))
330 const struct x11drv_escape_set_drawable *data = in_data;
331 physDev->dc_rect = data->dc_rect;
332 physDev->drawable = data->drawable;
333 XFreeGC( gdi_display, physDev->gc );
334 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
335 XSetGraphicsExposures( gdi_display, physDev->gc, False );
336 XSetSubwindowMode( gdi_display, physDev->gc, data->mode );
337 TRACE( "SET_DRAWABLE hdc %p drawable %lx dc_rect %s\n",
338 dev->hdc, physDev->drawable, wine_dbgstr_rect(&physDev->dc_rect) );
342 case X11DRV_GET_DRAWABLE:
343 if (out_count >= sizeof(struct x11drv_escape_get_drawable))
345 struct x11drv_escape_get_drawable *data = out_data;
346 data->drawable = physDev->drawable;
350 case X11DRV_FLUSH_GL_DRAWABLE:
351 if (in_count >= sizeof(struct x11drv_escape_flush_gl_drawable))
353 const struct x11drv_escape_flush_gl_drawable *data = in_data;
354 RECT rect = physDev->dc_rect;
356 OffsetRect( &rect, -physDev->dc_rect.left, -physDev->dc_rect.top );
357 /* The GL drawable may be lagged behind if we don't flush first, so
358 * flush the display make sure we copy up-to-date data */
359 XFlush( gdi_display );
360 XSetFunction( gdi_display, physDev->gc, GXcopy );
361 XCopyArea( gdi_display, data->gl_drawable, physDev->drawable, physDev->gc,
362 0, 0, rect.right, rect.bottom,
363 physDev->dc_rect.left, physDev->dc_rect.top );
364 add_device_bounds( physDev, &rect );
368 case X11DRV_START_EXPOSURES:
369 XSetGraphicsExposures( gdi_display, physDev->gc, True );
370 physDev->exposures = 0;
372 case X11DRV_END_EXPOSURES:
373 if (out_count >= sizeof(HRGN))
375 HRGN hrgn = 0, tmp = 0;
377 XSetGraphicsExposures( gdi_display, physDev->gc, False );
378 if (physDev->exposures)
384 XWindowEvent( gdi_display, physDev->drawable, ~0, &event );
385 if (event.type == NoExpose) break;
386 if (event.type == GraphicsExpose)
390 rect.left = event.xgraphicsexpose.x - physDev->dc_rect.left;
391 rect.top = event.xgraphicsexpose.y - physDev->dc_rect.top;
392 rect.right = rect.left + event.xgraphicsexpose.width;
393 rect.bottom = rect.top + event.xgraphicsexpose.height;
394 if (GetLayout( dev->hdc ) & LAYOUT_RTL)
395 mirror_rect( &physDev->dc_rect, &rect );
397 TRACE( "got %s count %d\n", wine_dbgstr_rect(&rect),
398 event.xgraphicsexpose.count );
400 if (!tmp) tmp = CreateRectRgnIndirect( &rect );
401 else SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
402 if (hrgn) CombineRgn( hrgn, hrgn, tmp, RGN_OR );
408 if (!event.xgraphicsexpose.count) break;
412 ERR( "got unexpected event %d\n", event.type );
416 if (tmp) DeleteObject( tmp );
418 *(HRGN *)out_data = hrgn;
431 /**********************************************************************
432 * X11DRV_wine_get_wgl_driver
434 static struct opengl_funcs * X11DRV_wine_get_wgl_driver( PHYSDEV dev, UINT version )
436 struct opengl_funcs *ret;
438 if (!(ret = get_glx_driver( version )))
440 dev = GET_NEXT_PHYSDEV( dev, wine_get_wgl_driver );
441 ret = dev->funcs->wine_get_wgl_driver( dev, version );
447 static const struct gdi_dc_funcs x11drv_funcs =
449 NULL, /* pAbortDoc */
450 NULL, /* pAbortPath */
451 NULL, /* pAlphaBlend */
452 NULL, /* pAngleArc */
453 X11DRV_Arc, /* pArc */
455 NULL, /* pBeginPath */
456 NULL, /* pBlendImage */
457 X11DRV_Chord, /* pChord */
458 NULL, /* pCloseFigure */
459 X11DRV_CreateCompatibleDC, /* pCreateCompatibleDC */
460 X11DRV_CreateDC, /* pCreateDC */
461 X11DRV_DeleteDC, /* pDeleteDC */
462 NULL, /* pDeleteObject */
463 NULL, /* pDeviceCapabilities */
464 X11DRV_Ellipse, /* pEllipse */
468 NULL, /* pEnumFonts */
469 X11DRV_EnumICMProfiles, /* pEnumICMProfiles */
470 NULL, /* pExcludeClipRect */
471 NULL, /* pExtDeviceMode */
472 X11DRV_ExtEscape, /* pExtEscape */
473 X11DRV_ExtFloodFill, /* pExtFloodFill */
474 NULL, /* pExtSelectClipRgn */
475 NULL, /* pExtTextOut */
476 NULL, /* pFillPath */
478 NULL, /* pFlattenPath */
479 NULL, /* pFontIsLinked */
480 NULL, /* pFrameRgn */
481 NULL, /* pGdiComment */
482 NULL, /* pGdiRealizationInfo */
483 NULL, /* pGetBoundsRect */
484 NULL, /* pGetCharABCWidths */
485 NULL, /* pGetCharABCWidthsI */
486 NULL, /* pGetCharWidth */
487 X11DRV_GetDeviceCaps, /* pGetDeviceCaps */
488 X11DRV_GetDeviceGammaRamp, /* pGetDeviceGammaRamp */
489 NULL, /* pGetFontData */
490 NULL, /* pGetFontUnicodeRanges */
491 NULL, /* pGetGlyphIndices */
492 NULL, /* pGetGlyphOutline */
493 X11DRV_GetICMProfile, /* pGetICMProfile */
494 X11DRV_GetImage, /* pGetImage */
495 NULL, /* pGetKerningPairs */
496 X11DRV_GetNearestColor, /* pGetNearestColor */
497 NULL, /* pGetOutlineTextMetrics */
498 NULL, /* pGetPixel */
499 X11DRV_GetSystemPaletteEntries, /* pGetSystemPaletteEntries */
500 NULL, /* pGetTextCharsetInfo */
501 NULL, /* pGetTextExtentExPoint */
502 NULL, /* pGetTextExtentExPointI */
503 NULL, /* pGetTextFace */
504 NULL, /* pGetTextMetrics */
505 X11DRV_GradientFill, /* pGradientFill */
506 NULL, /* pIntersectClipRect */
507 NULL, /* pInvertRgn */
508 X11DRV_LineTo, /* pLineTo */
509 NULL, /* pModifyWorldTransform */
511 NULL, /* pOffsetClipRgn */
512 NULL, /* pOffsetViewportOrg */
513 NULL, /* pOffsetWindowOrg */
514 X11DRV_PaintRgn, /* pPaintRgn */
515 X11DRV_PatBlt, /* pPatBlt */
516 X11DRV_Pie, /* pPie */
517 NULL, /* pPolyBezier */
518 NULL, /* pPolyBezierTo */
519 NULL, /* pPolyDraw */
520 X11DRV_PolyPolygon, /* pPolyPolygon */
521 X11DRV_PolyPolyline, /* pPolyPolyline */
522 X11DRV_Polygon, /* pPolygon */
523 NULL, /* pPolyline */
524 NULL, /* pPolylineTo */
525 X11DRV_PutImage, /* pPutImage */
526 X11DRV_RealizeDefaultPalette, /* pRealizeDefaultPalette */
527 X11DRV_RealizePalette, /* pRealizePalette */
528 X11DRV_Rectangle, /* pRectangle */
530 NULL, /* pRestoreDC */
531 X11DRV_RoundRect, /* pRoundRect */
533 NULL, /* pScaleViewportExt */
534 NULL, /* pScaleWindowExt */
535 NULL, /* pSelectBitmap */
536 X11DRV_SelectBrush, /* pSelectBrush */
537 NULL, /* pSelectClipPath */
538 NULL, /* pSelectFont */
539 NULL, /* pSelectPalette */
540 X11DRV_SelectPen, /* pSelectPen */
541 NULL, /* pSetArcDirection */
542 NULL, /* pSetBkColor */
543 NULL, /* pSetBkMode */
544 X11DRV_SetBoundsRect, /* pSetBoundsRect */
545 X11DRV_SetDCBrushColor, /* pSetDCBrushColor */
546 X11DRV_SetDCPenColor, /* pSetDCPenColor */
547 NULL, /* pSetDIBitsToDevice */
548 X11DRV_SetDeviceClipping, /* pSetDeviceClipping */
549 X11DRV_SetDeviceGammaRamp, /* pSetDeviceGammaRamp */
550 NULL, /* pSetLayout */
551 NULL, /* pSetMapMode */
552 NULL, /* pSetMapperFlags */
553 X11DRV_SetPixel, /* pSetPixel */
554 NULL, /* pSetPolyFillMode */
556 NULL, /* pSetRelAbs */
557 NULL, /* pSetStretchBltMode */
558 NULL, /* pSetTextAlign */
559 NULL, /* pSetTextCharacterExtra */
560 NULL, /* pSetTextColor */
561 NULL, /* pSetTextJustification */
562 NULL, /* pSetViewportExt */
563 NULL, /* pSetViewportOrg */
564 NULL, /* pSetWindowExt */
565 NULL, /* pSetWindowOrg */
566 NULL, /* pSetWorldTransform */
567 NULL, /* pStartDoc */
568 NULL, /* pStartPage */
569 X11DRV_StretchBlt, /* pStretchBlt */
570 NULL, /* pStretchDIBits */
571 NULL, /* pStrokeAndFillPath */
572 NULL, /* pStrokePath */
573 X11DRV_UnrealizePalette, /* pUnrealizePalette */
574 NULL, /* pWidenPath */
575 X11DRV_wine_get_wgl_driver, /* wine_get_wgl_driver */
576 GDI_PRIORITY_GRAPHICS_DRV /* priority */
580 /******************************************************************************
581 * X11DRV_get_gdi_driver
583 const struct gdi_dc_funcs * CDECL X11DRV_get_gdi_driver( unsigned int version )
585 if (version != WINE_GDI_DRIVER_VERSION)
587 ERR( "version mismatch, gdi32 wants %u but winex11 has %u\n", version, WINE_GDI_DRIVER_VERSION );
590 return &x11drv_funcs;