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();
96 stock_bitmap_pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
99 /* Initialize device caps */
100 log_pixels_x = log_pixels_y = get_dpi();
101 horz_size = MulDiv( screen_width, 254, log_pixels_x * 10 );
102 vert_size = MulDiv( screen_height, 254, log_pixels_y * 10 );
105 /**********************************************************************
106 * X11DRV_GDI_Finalize
108 void X11DRV_GDI_Finalize(void)
110 X11DRV_PALETTE_Cleanup();
111 /* don't bother to close the display, it often triggers X bugs */
112 /* XCloseDisplay( gdi_display ); */
116 static X11DRV_PDEVICE *create_x11_physdev( Drawable drawable )
118 X11DRV_PDEVICE *physDev;
120 if (!device_init_done) device_init();
122 if (!(physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) ))) return NULL;
125 physDev->drawable = drawable;
126 physDev->gc = XCreateGC( gdi_display, drawable, 0, NULL );
127 XSetGraphicsExposures( gdi_display, physDev->gc, False );
128 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
129 XFlush( gdi_display );
134 /**********************************************************************
137 static BOOL X11DRV_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
138 LPCWSTR output, const DEVMODEW* initData )
140 const struct gdi_dc_funcs *glx_funcs = get_glx_driver();
141 X11DRV_PDEVICE *physDev = create_x11_physdev( root_window );
143 if (!physDev) return FALSE;
145 physDev->depth = screen_depth;
146 physDev->color_shifts = &X11DRV_PALETTE_default_shifts;
147 SetRect( &physDev->dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
148 virtual_screen_rect.bottom - virtual_screen_rect.top );
149 push_dc_driver( pdev, &physDev->dev, &x11drv_funcs );
150 if (xrender_funcs && !xrender_funcs->pCreateDC( pdev, driver, device, output, initData )) return FALSE;
151 if (glx_funcs && !glx_funcs->pCreateDC( pdev, driver, device, output, initData )) return FALSE;
156 /**********************************************************************
157 * X11DRV_CreateCompatibleDC
159 static BOOL X11DRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
161 const struct gdi_dc_funcs *glx_funcs = get_glx_driver();
162 X11DRV_PDEVICE *physDev = create_x11_physdev( stock_bitmap_pixmap );
164 if (!physDev) return FALSE;
167 SetRect( &physDev->dc_rect, 0, 0, 1, 1 );
168 push_dc_driver( pdev, &physDev->dev, &x11drv_funcs );
169 if (orig) return TRUE; /* we already went through Xrender if we have an orig device */
170 if (xrender_funcs && !xrender_funcs->pCreateCompatibleDC( NULL, pdev )) return FALSE;
171 if (glx_funcs && !glx_funcs->pCreateCompatibleDC( NULL, pdev )) return FALSE;
176 /**********************************************************************
179 static BOOL X11DRV_DeleteDC( PHYSDEV dev )
181 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
184 XFreeGC( gdi_display, physDev->gc );
186 HeapFree( GetProcessHeap(), 0, physDev );
191 void add_device_bounds( X11DRV_PDEVICE *dev, const RECT *rect )
195 if (!dev->bounds) return;
196 if (dev->region && GetRgnBox( dev->region, &rc ))
198 if (IntersectRect( &rc, &rc, rect )) add_bounds_rect( dev->bounds, &rc );
200 else add_bounds_rect( dev->bounds, rect );
203 /***********************************************************************
204 * X11DRV_SetBoundsRect
206 static UINT X11DRV_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
208 X11DRV_PDEVICE *pdev = get_x11drv_dev( dev );
210 if (flags & DCB_DISABLE) pdev->bounds = NULL;
211 else if (flags & DCB_ENABLE) pdev->bounds = rect;
212 return DCB_RESET; /* we don't have device-specific bounds */
216 /***********************************************************************
217 * GetDeviceCaps (X11DRV.@)
219 static INT X11DRV_GetDeviceCaps( PHYSDEV dev, INT cap )
226 return DT_RASDISPLAY;
234 return screen_height;
236 return virtual_screen_rect.right - virtual_screen_rect.left;
238 return virtual_screen_rect.bottom - virtual_screen_rect.top;
252 /* MSDN: Number of entries in the device's color table, if the device has
253 * a color depth of no more than 8 bits per pixel.For devices with greater
254 * color depths, -1 is returned. */
255 return (screen_depth > 8) ? -1 : (1 << screen_depth);
257 return sizeof(X11DRV_PDEVICE);
259 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
260 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
262 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
263 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
265 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
266 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
268 return (TC_OP_CHARACTER | TC_OP_STROKE | TC_CP_STROKE |
269 TC_CR_ANY | TC_SF_X_YINDEP | TC_SA_DOUBLE | TC_SA_INTEGER |
270 TC_SA_CONTIN | TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE | TC_VA_ABLE);
274 /* The observed correspondence between BITSPIXEL and COLORRES is:
275 * BITSPIXEL: 8 -> COLORRES: 18
276 * BITSPIXEL: 16 -> COLORRES: 16
277 * BITSPIXEL: 24 -> COLORRES: 24
278 * BITSPIXEL: 32 -> COLORRES: 24 */
279 return (screen_bpp <= 8) ? 18 : min( 24, screen_bpp );
281 return (RC_BITBLT | RC_BANDING | RC_SCALING | RC_BITMAP64 | RC_DI_BITMAP |
282 RC_DIBTODEV | RC_BIGFONT | RC_STRETCHBLT | RC_STRETCHDIB | RC_DEVBITS |
283 (palette_size ? RC_PALETTE : 0));
285 return (SB_GRAD_RECT | SB_GRAD_TRI | SB_CONST_ALPHA | SB_PIXEL_ALPHA);
296 FIXME("(%p): CAPS1 is unimplemented, will return 0\n", dev->hdc );
297 /* please see wingdi.h for the possible bit-flag values that need
305 case PHYSICALOFFSETX:
306 case PHYSICALOFFSETY:
313 FIXME("(%p): unsupported capability %d, will return 0\n", dev->hdc, cap );
319 /**********************************************************************
320 * ExtEscape (X11DRV.@)
322 static INT X11DRV_ExtEscape( PHYSDEV dev, INT escape, INT in_count, LPCVOID in_data,
323 INT out_count, LPVOID out_data )
325 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
329 case QUERYESCSUPPORT:
332 switch (*(const INT *)in_data)
335 return DD_HAL_VERSION;
343 if (in_data && in_count >= sizeof(enum x11drv_escape_codes))
345 switch(*(const enum x11drv_escape_codes *)in_data)
347 case X11DRV_SET_DRAWABLE:
348 if (in_count >= sizeof(struct x11drv_escape_set_drawable))
350 const struct x11drv_escape_set_drawable *data = in_data;
351 physDev->dc_rect = data->dc_rect;
352 physDev->drawable = data->drawable;
354 XSetSubwindowMode( gdi_display, physDev->gc, data->mode );
356 TRACE( "SET_DRAWABLE hdc %p drawable %lx dc_rect %s\n",
357 dev->hdc, physDev->drawable, wine_dbgstr_rect(&physDev->dc_rect) );
361 case X11DRV_GET_DRAWABLE:
362 if (out_count >= sizeof(struct x11drv_escape_get_drawable))
364 struct x11drv_escape_get_drawable *data = out_data;
365 data->drawable = physDev->drawable;
369 case X11DRV_START_EXPOSURES:
371 XSetGraphicsExposures( gdi_display, physDev->gc, True );
373 physDev->exposures = 0;
375 case X11DRV_END_EXPOSURES:
376 if (out_count >= sizeof(HRGN))
378 HRGN hrgn = 0, tmp = 0;
381 XSetGraphicsExposures( gdi_display, physDev->gc, False );
383 if (physDev->exposures)
390 XWindowEvent( gdi_display, physDev->drawable, ~0, &event );
392 if (event.type == NoExpose) break;
393 if (event.type == GraphicsExpose)
397 rect.left = event.xgraphicsexpose.x - physDev->dc_rect.left;
398 rect.top = event.xgraphicsexpose.y - physDev->dc_rect.top;
399 rect.right = rect.left + event.xgraphicsexpose.width;
400 rect.bottom = rect.top + event.xgraphicsexpose.height;
401 if (GetLayout( dev->hdc ) & LAYOUT_RTL)
402 mirror_rect( &physDev->dc_rect, &rect );
404 TRACE( "got %s count %d\n", wine_dbgstr_rect(&rect),
405 event.xgraphicsexpose.count );
407 if (!tmp) tmp = CreateRectRgnIndirect( &rect );
408 else SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
409 if (hrgn) CombineRgn( hrgn, hrgn, tmp, RGN_OR );
415 if (!event.xgraphicsexpose.count) break;
419 ERR( "got unexpected event %d\n", event.type );
423 if (tmp) DeleteObject( tmp );
425 *(HRGN *)out_data = hrgn;
439 static const struct gdi_dc_funcs x11drv_funcs =
441 NULL, /* pAbortDoc */
442 NULL, /* pAbortPath */
443 NULL, /* pAlphaBlend */
444 NULL, /* pAngleArc */
445 X11DRV_Arc, /* pArc */
447 NULL, /* pBeginPath */
448 NULL, /* pBlendImage */
449 X11DRV_Chord, /* pChord */
450 NULL, /* pCloseFigure */
451 X11DRV_CreateCompatibleDC, /* pCreateCompatibleDC */
452 X11DRV_CreateDC, /* pCreateDC */
453 X11DRV_DeleteDC, /* pDeleteDC */
454 NULL, /* pDeleteObject */
455 NULL, /* pDeviceCapabilities */
456 X11DRV_Ellipse, /* pEllipse */
460 NULL, /* pEnumFonts */
461 X11DRV_EnumICMProfiles, /* pEnumICMProfiles */
462 NULL, /* pExcludeClipRect */
463 NULL, /* pExtDeviceMode */
464 X11DRV_ExtEscape, /* pExtEscape */
465 X11DRV_ExtFloodFill, /* pExtFloodFill */
466 NULL, /* pExtSelectClipRgn */
467 NULL, /* pExtTextOut */
468 NULL, /* pFillPath */
470 NULL, /* pFlattenPath */
471 NULL, /* pFontIsLinked */
472 NULL, /* pFrameRgn */
473 NULL, /* pGdiComment */
474 NULL, /* pGdiRealizationInfo */
475 NULL, /* pGetBoundsRect */
476 NULL, /* pGetCharABCWidths */
477 NULL, /* pGetCharABCWidthsI */
478 NULL, /* pGetCharWidth */
479 X11DRV_GetDeviceCaps, /* pGetDeviceCaps */
480 X11DRV_GetDeviceGammaRamp, /* pGetDeviceGammaRamp */
481 NULL, /* pGetFontData */
482 NULL, /* pGetFontUnicodeRanges */
483 NULL, /* pGetGlyphIndices */
484 NULL, /* pGetGlyphOutline */
485 X11DRV_GetICMProfile, /* pGetICMProfile */
486 X11DRV_GetImage, /* pGetImage */
487 NULL, /* pGetKerningPairs */
488 X11DRV_GetNearestColor, /* pGetNearestColor */
489 NULL, /* pGetOutlineTextMetrics */
490 NULL, /* pGetPixel */
491 X11DRV_GetSystemPaletteEntries, /* pGetSystemPaletteEntries */
492 NULL, /* pGetTextCharsetInfo */
493 NULL, /* pGetTextExtentExPoint */
494 NULL, /* pGetTextExtentExPointI */
495 NULL, /* pGetTextFace */
496 NULL, /* pGetTextMetrics */
497 X11DRV_GradientFill, /* pGradientFill */
498 NULL, /* pIntersectClipRect */
499 NULL, /* pInvertRgn */
500 X11DRV_LineTo, /* pLineTo */
501 NULL, /* pModifyWorldTransform */
503 NULL, /* pOffsetClipRgn */
504 NULL, /* pOffsetViewportOrg */
505 NULL, /* pOffsetWindowOrg */
506 X11DRV_PaintRgn, /* pPaintRgn */
507 X11DRV_PatBlt, /* pPatBlt */
508 X11DRV_Pie, /* pPie */
509 NULL, /* pPolyBezier */
510 NULL, /* pPolyBezierTo */
511 NULL, /* pPolyDraw */
512 X11DRV_PolyPolygon, /* pPolyPolygon */
513 X11DRV_PolyPolyline, /* pPolyPolyline */
514 X11DRV_Polygon, /* pPolygon */
515 NULL, /* pPolyline */
516 NULL, /* pPolylineTo */
517 X11DRV_PutImage, /* pPutImage */
518 X11DRV_RealizeDefaultPalette, /* pRealizeDefaultPalette */
519 X11DRV_RealizePalette, /* pRealizePalette */
520 X11DRV_Rectangle, /* pRectangle */
522 NULL, /* pRestoreDC */
523 X11DRV_RoundRect, /* pRoundRect */
525 NULL, /* pScaleViewportExt */
526 NULL, /* pScaleWindowExt */
527 NULL, /* pSelectBitmap */
528 X11DRV_SelectBrush, /* pSelectBrush */
529 NULL, /* pSelectClipPath */
530 NULL, /* pSelectFont */
531 NULL, /* pSelectPalette */
532 X11DRV_SelectPen, /* pSelectPen */
533 NULL, /* pSetArcDirection */
534 NULL, /* pSetBkColor */
535 NULL, /* pSetBkMode */
536 X11DRV_SetBoundsRect, /* pSetBoundsRect */
537 X11DRV_SetDCBrushColor, /* pSetDCBrushColor */
538 X11DRV_SetDCPenColor, /* pSetDCPenColor */
539 NULL, /* pSetDIBitsToDevice */
540 X11DRV_SetDeviceClipping, /* pSetDeviceClipping */
541 X11DRV_SetDeviceGammaRamp, /* pSetDeviceGammaRamp */
542 NULL, /* pSetLayout */
543 NULL, /* pSetMapMode */
544 NULL, /* pSetMapperFlags */
545 X11DRV_SetPixel, /* pSetPixel */
546 NULL, /* pSetPolyFillMode */
548 NULL, /* pSetRelAbs */
549 NULL, /* pSetStretchBltMode */
550 NULL, /* pSetTextAlign */
551 NULL, /* pSetTextCharacterExtra */
552 NULL, /* pSetTextColor */
553 NULL, /* pSetTextJustification */
554 NULL, /* pSetViewportExt */
555 NULL, /* pSetViewportOrg */
556 NULL, /* pSetWindowExt */
557 NULL, /* pSetWindowOrg */
558 NULL, /* pSetWorldTransform */
559 NULL, /* pStartDoc */
560 NULL, /* pStartPage */
561 X11DRV_StretchBlt, /* pStretchBlt */
562 NULL, /* pStretchDIBits */
563 NULL, /* pStrokeAndFillPath */
564 NULL, /* pStrokePath */
565 NULL, /* pSwapBuffers */
566 X11DRV_UnrealizePalette, /* pUnrealizePalette */
567 NULL, /* pWidenPath */
568 NULL, /* wine_get_wgl_driver */
569 GDI_PRIORITY_GRAPHICS_DRV /* priority */
573 /******************************************************************************
574 * X11DRV_get_gdi_driver
576 const struct gdi_dc_funcs * CDECL X11DRV_get_gdi_driver( unsigned int version )
578 if (version != WINE_GDI_DRIVER_VERSION)
580 ERR( "version mismatch, gdi32 wants %u but winex11 has %u\n", version, WINE_GDI_DRIVER_VERSION );
583 return &x11drv_funcs;