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
;
46 static const WCHAR dpi_key_name
[] = {'S','o','f','t','w','a','r','e','\\','F','o','n','t','s','\0'};
47 static const WCHAR dpi_value_name
[] = {'L','o','g','P','i','x','e','l','s','\0'};
49 static const struct gdi_dc_funcs x11drv_funcs
;
50 static const struct gdi_dc_funcs
*xrender_funcs
;
52 /******************************************************************************
55 * get the dpi from the registry
57 static DWORD
get_dpi( void )
62 if (RegOpenKeyW(HKEY_CURRENT_CONFIG
, dpi_key_name
, &hkey
) == ERROR_SUCCESS
)
64 DWORD type
, size
, new_dpi
;
66 size
= sizeof(new_dpi
);
67 if(RegQueryValueExW(hkey
, dpi_value_name
, NULL
, &type
, (void *)&new_dpi
, &size
) == ERROR_SUCCESS
)
69 if(type
== REG_DWORD
&& new_dpi
!= 0)
77 /**********************************************************************
80 * Perform initializations needed upon creation of the first device.
82 static void device_init(void)
84 device_init_done
= TRUE
;
86 /* Initialize XRender */
87 xrender_funcs
= X11DRV_XRender_Init();
90 X11DRV_Xcursor_Init();
92 palette_size
= X11DRV_PALETTE_Init();
96 /* Initialize device caps */
97 log_pixels_x
= log_pixels_y
= get_dpi();
98 horz_size
= MulDiv( screen_width
, 254, log_pixels_x
* 10 );
99 vert_size
= MulDiv( screen_height
, 254, log_pixels_y
* 10 );
102 /**********************************************************************
103 * X11DRV_GDI_Finalize
105 void X11DRV_GDI_Finalize(void)
107 X11DRV_PALETTE_Cleanup();
108 /* don't bother to close the display, it often triggers X bugs */
109 /* XCloseDisplay( gdi_display ); */
113 static X11DRV_PDEVICE
*create_x11_physdev( Drawable drawable
)
115 X11DRV_PDEVICE
*physDev
;
117 if (!device_init_done
) device_init();
119 if (!(physDev
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*physDev
) ))) return NULL
;
122 physDev
->drawable
= drawable
;
123 physDev
->gc
= XCreateGC( gdi_display
, drawable
, 0, NULL
);
124 XSetGraphicsExposures( gdi_display
, physDev
->gc
, False
);
125 XSetSubwindowMode( gdi_display
, physDev
->gc
, IncludeInferiors
);
126 XFlush( gdi_display
);
131 /**********************************************************************
134 static BOOL
X11DRV_CreateDC( PHYSDEV
*pdev
, LPCWSTR driver
, LPCWSTR device
,
135 LPCWSTR output
, const DEVMODEW
* initData
)
137 const struct gdi_dc_funcs
*glx_funcs
= get_glx_driver();
138 X11DRV_PDEVICE
*physDev
= create_x11_physdev( root_window
);
140 if (!physDev
) return FALSE
;
142 physDev
->depth
= screen_depth
;
143 physDev
->color_shifts
= &X11DRV_PALETTE_default_shifts
;
144 physDev
->drawable_rect
= virtual_screen_rect
;
145 SetRect( &physDev
->dc_rect
, 0, 0, virtual_screen_rect
.right
- virtual_screen_rect
.left
,
146 virtual_screen_rect
.bottom
- virtual_screen_rect
.top
);
147 push_dc_driver( pdev
, &physDev
->dev
, &x11drv_funcs
);
148 if (xrender_funcs
&& !xrender_funcs
->pCreateDC( pdev
, driver
, device
, output
, initData
)) return FALSE
;
149 if (glx_funcs
&& !glx_funcs
->pCreateDC( pdev
, driver
, device
, output
, initData
)) return FALSE
;
154 /**********************************************************************
155 * X11DRV_CreateCompatibleDC
157 static BOOL
X11DRV_CreateCompatibleDC( PHYSDEV orig
, PHYSDEV
*pdev
)
159 const struct gdi_dc_funcs
*glx_funcs
= get_glx_driver();
160 X11DRV_PDEVICE
*physDev
= create_x11_physdev( BITMAP_stock_phys_bitmap
.pixmap
);
162 if (!physDev
) return FALSE
;
164 if (!BITMAP_stock_phys_bitmap
.hbitmap
)
165 BITMAP_stock_phys_bitmap
.hbitmap
= GetCurrentObject( (*pdev
)->hdc
, OBJ_BITMAP
);
167 physDev
->bitmap
= &BITMAP_stock_phys_bitmap
;
169 SetRect( &physDev
->drawable_rect
, 0, 0, 1, 1 );
170 physDev
->dc_rect
= physDev
->drawable_rect
;
171 push_dc_driver( pdev
, &physDev
->dev
, &x11drv_funcs
);
172 if (orig
) return TRUE
; /* we already went through Xrender if we have an orig device */
173 if (xrender_funcs
&& !xrender_funcs
->pCreateCompatibleDC( NULL
, pdev
)) return FALSE
;
174 if (glx_funcs
&& !glx_funcs
->pCreateCompatibleDC( NULL
, pdev
)) return FALSE
;
179 /**********************************************************************
182 static BOOL
X11DRV_DeleteDC( PHYSDEV dev
)
184 X11DRV_PDEVICE
*physDev
= get_x11drv_dev( dev
);
187 XFreeGC( gdi_display
, physDev
->gc
);
189 HeapFree( GetProcessHeap(), 0, physDev
);
194 void add_device_bounds( X11DRV_PDEVICE
*dev
, const RECT
*rect
)
198 if (!dev
->bounds
) return;
199 if (dev
->region
&& GetRgnBox( dev
->region
, &rc
))
201 if (IntersectRect( &rc
, &rc
, rect
)) add_bounds_rect( dev
->bounds
, &rc
);
203 else add_bounds_rect( dev
->bounds
, rect
);
206 /***********************************************************************
207 * X11DRV_SetBoundsRect
209 static UINT
X11DRV_SetBoundsRect( PHYSDEV dev
, RECT
*rect
, UINT flags
)
211 X11DRV_PDEVICE
*pdev
= get_x11drv_dev( dev
);
213 if (flags
& DCB_DISABLE
) pdev
->bounds
= NULL
;
214 else if (flags
& DCB_ENABLE
) pdev
->bounds
= rect
;
215 return DCB_RESET
; /* we don't have device-specific bounds */
219 /***********************************************************************
220 * GetDeviceCaps (X11DRV.@)
222 static INT
X11DRV_GetDeviceCaps( PHYSDEV dev
, INT cap
)
229 return DT_RASDISPLAY
;
237 return screen_height
;
239 return virtual_screen_rect
.right
- virtual_screen_rect
.left
;
241 return virtual_screen_rect
.bottom
- virtual_screen_rect
.top
;
255 /* MSDN: Number of entries in the device's color table, if the device has
256 * a color depth of no more than 8 bits per pixel.For devices with greater
257 * color depths, -1 is returned. */
258 return (screen_depth
> 8) ? -1 : (1 << screen_depth
);
260 return sizeof(X11DRV_PDEVICE
);
262 return (CC_CIRCLES
| CC_PIE
| CC_CHORD
| CC_ELLIPSES
| CC_WIDE
|
263 CC_STYLED
| CC_WIDESTYLED
| CC_INTERIORS
| CC_ROUNDRECT
);
265 return (LC_POLYLINE
| LC_MARKER
| LC_POLYMARKER
| LC_WIDE
|
266 LC_STYLED
| LC_WIDESTYLED
| LC_INTERIORS
);
268 return (PC_POLYGON
| PC_RECTANGLE
| PC_WINDPOLYGON
| PC_SCANLINE
|
269 PC_WIDE
| PC_STYLED
| PC_WIDESTYLED
| PC_INTERIORS
);
271 return (TC_OP_CHARACTER
| TC_OP_STROKE
| TC_CP_STROKE
|
272 TC_CR_ANY
| TC_SF_X_YINDEP
| TC_SA_DOUBLE
| TC_SA_INTEGER
|
273 TC_SA_CONTIN
| TC_UA_ABLE
| TC_SO_ABLE
| TC_RA_ABLE
| TC_VA_ABLE
);
277 /* The observed correspondence between BITSPIXEL and COLORRES is:
278 * BITSPIXEL: 8 -> COLORRES: 18
279 * BITSPIXEL: 16 -> COLORRES: 16
280 * BITSPIXEL: 24 -> COLORRES: 24
281 * BITSPIXEL: 32 -> COLORRES: 24 */
282 return (screen_bpp
<= 8) ? 18 : min( 24, screen_bpp
);
284 return (RC_BITBLT
| RC_BANDING
| RC_SCALING
| RC_BITMAP64
| RC_DI_BITMAP
|
285 RC_DIBTODEV
| RC_BIGFONT
| RC_STRETCHBLT
| RC_STRETCHDIB
| RC_DEVBITS
|
286 (palette_size
? RC_PALETTE
: 0));
288 return (SB_GRAD_RECT
| SB_GRAD_TRI
| SB_CONST_ALPHA
| SB_PIXEL_ALPHA
);
299 FIXME("(%p): CAPS1 is unimplemented, will return 0\n", dev
->hdc
);
300 /* please see wingdi.h for the possible bit-flag values that need
308 case PHYSICALOFFSETX
:
309 case PHYSICALOFFSETY
:
316 FIXME("(%p): unsupported capability %d, will return 0\n", dev
->hdc
, cap
);
322 /**********************************************************************
323 * ExtEscape (X11DRV.@)
325 static INT
X11DRV_ExtEscape( PHYSDEV dev
, INT escape
, INT in_count
, LPCVOID in_data
,
326 INT out_count
, LPVOID out_data
)
328 X11DRV_PDEVICE
*physDev
= get_x11drv_dev( dev
);
332 case QUERYESCSUPPORT
:
335 switch (*(const INT
*)in_data
)
338 return DD_HAL_VERSION
;
346 if (in_data
&& in_count
>= sizeof(enum x11drv_escape_codes
))
348 switch(*(const enum x11drv_escape_codes
*)in_data
)
350 case X11DRV_SET_DRAWABLE
:
351 if (in_count
>= sizeof(struct x11drv_escape_set_drawable
))
353 const struct x11drv_escape_set_drawable
*data
= in_data
;
354 physDev
->dc_rect
= data
->dc_rect
;
355 physDev
->drawable
= data
->drawable
;
356 physDev
->drawable_rect
= data
->drawable_rect
;
357 physDev
->current_pf
= pixelformat_from_fbconfig_id( data
->fbconfig_id
);
358 physDev
->gl_drawable
= data
->gl_drawable
;
359 physDev
->pixmap
= data
->pixmap
;
360 physDev
->gl_type
= data
->gl_type
;
362 XSetSubwindowMode( gdi_display
, physDev
->gc
, data
->mode
);
364 TRACE( "SET_DRAWABLE hdc %p drawable %lx gl_drawable %lx pf %u gl %u dc_rect %s drawable_rect %s\n",
365 dev
->hdc
, physDev
->drawable
, physDev
->gl_drawable
, physDev
->current_pf
,
366 physDev
->gl_type
, wine_dbgstr_rect(&physDev
->dc_rect
),
367 wine_dbgstr_rect(&physDev
->drawable_rect
) );
371 case X11DRV_START_EXPOSURES
:
373 XSetGraphicsExposures( gdi_display
, physDev
->gc
, True
);
375 physDev
->exposures
= 0;
377 case X11DRV_END_EXPOSURES
:
378 if (out_count
>= sizeof(HRGN
))
380 HRGN hrgn
= 0, tmp
= 0;
383 XSetGraphicsExposures( gdi_display
, physDev
->gc
, False
);
385 if (physDev
->exposures
)
392 XWindowEvent( gdi_display
, physDev
->drawable
, ~0, &event
);
394 if (event
.type
== NoExpose
) break;
395 if (event
.type
== GraphicsExpose
)
399 rect
.left
= event
.xgraphicsexpose
.x
- physDev
->dc_rect
.left
;
400 rect
.top
= event
.xgraphicsexpose
.y
- physDev
->dc_rect
.top
;
401 rect
.right
= rect
.left
+ event
.xgraphicsexpose
.width
;
402 rect
.bottom
= rect
.top
+ event
.xgraphicsexpose
.height
;
403 if (GetLayout( dev
->hdc
) & LAYOUT_RTL
)
404 mirror_rect( &physDev
->dc_rect
, &rect
);
406 TRACE( "got %s count %d\n", wine_dbgstr_rect(&rect
),
407 event
.xgraphicsexpose
.count
);
409 if (!tmp
) tmp
= CreateRectRgnIndirect( &rect
);
410 else SetRectRgn( tmp
, rect
.left
, rect
.top
, rect
.right
, rect
.bottom
);
411 if (hrgn
) CombineRgn( hrgn
, hrgn
, tmp
, RGN_OR
);
417 if (!event
.xgraphicsexpose
.count
) break;
421 ERR( "got unexpected event %d\n", event
.type
);
425 if (tmp
) DeleteObject( tmp
);
427 *(HRGN
*)out_data
= hrgn
;
431 case X11DRV_FLUSH_GL_DRAWABLE
:
432 flush_gl_drawable(physDev
);
442 static inline void opengl_error(void)
445 if (!warned
++) ERR("No OpenGL support compiled in.\n");
448 /***********************************************************************
449 * X11DRV_ChoosePixelFormat
451 static int X11DRV_ChoosePixelFormat( PHYSDEV dev
, const PIXELFORMATDESCRIPTOR
*ppfd
)
457 /***********************************************************************
458 * X11DRV_DescribePixelFormat
460 static int X11DRV_DescribePixelFormat( PHYSDEV dev
, int fmt
, UINT size
, PIXELFORMATDESCRIPTOR
*ppfd
)
466 /***********************************************************************
467 * X11DRV_SetPixelFormat
469 static BOOL
X11DRV_SetPixelFormat( PHYSDEV dev
, int fmt
, const PIXELFORMATDESCRIPTOR
*ppfd
)
475 /***********************************************************************
476 * X11DRV_wglCreateContext
478 static HGLRC
X11DRV_wglCreateContext( PHYSDEV dev
)
484 /***********************************************************************
485 * X11DRV_wglCreateContextAttribsARB
487 static HGLRC
X11DRV_wglCreateContextAttribsARB( PHYSDEV dev
, HGLRC hShareContext
, const int* attribList
)
493 /***********************************************************************
494 * X11DRV_wglGetProcAddress
496 static PROC
X11DRV_wglGetProcAddress( LPCSTR proc
)
502 /***********************************************************************
503 * X11DRV_wglSetPixelFormatWINE
505 static BOOL
X11DRV_wglSetPixelFormatWINE( PHYSDEV dev
, int fmt
, const PIXELFORMATDESCRIPTOR
*ppfd
)
512 static const struct gdi_dc_funcs x11drv_funcs
=
514 NULL
, /* pAbortDoc */
515 NULL
, /* pAbortPath */
516 NULL
, /* pAlphaBlend */
517 NULL
, /* pAngleArc */
518 X11DRV_Arc
, /* pArc */
520 NULL
, /* pBeginPath */
521 NULL
, /* pBlendImage */
522 X11DRV_ChoosePixelFormat
, /* pChoosePixelFormat */
523 X11DRV_Chord
, /* pChord */
524 NULL
, /* pCloseFigure */
525 X11DRV_CopyBitmap
, /* pCopyBitmap */
526 X11DRV_CreateBitmap
, /* pCreateBitmap */
527 X11DRV_CreateCompatibleDC
, /* pCreateCompatibleDC */
528 X11DRV_CreateDC
, /* pCreateDC */
529 X11DRV_DeleteBitmap
, /* pDeleteBitmap */
530 X11DRV_DeleteDC
, /* pDeleteDC */
531 NULL
, /* pDeleteObject */
532 X11DRV_DescribePixelFormat
, /* pDescribePixelFormat */
533 NULL
, /* pDeviceCapabilities */
534 X11DRV_Ellipse
, /* pEllipse */
538 NULL
, /* pEnumFonts */
539 X11DRV_EnumICMProfiles
, /* pEnumICMProfiles */
540 NULL
, /* pExcludeClipRect */
541 NULL
, /* pExtDeviceMode */
542 X11DRV_ExtEscape
, /* pExtEscape */
543 X11DRV_ExtFloodFill
, /* pExtFloodFill */
544 NULL
, /* pExtSelectClipRgn */
545 NULL
, /* pExtTextOut */
546 NULL
, /* pFillPath */
548 NULL
, /* pFlattenPath */
549 NULL
, /* pFontIsLinked */
550 NULL
, /* pFrameRgn */
551 NULL
, /* pGdiComment */
552 NULL
, /* pGdiRealizationInfo */
553 NULL
, /* pGetBoundsRect */
554 NULL
, /* pGetCharABCWidths */
555 NULL
, /* pGetCharABCWidthsI */
556 NULL
, /* pGetCharWidth */
557 X11DRV_GetDeviceCaps
, /* pGetDeviceCaps */
558 X11DRV_GetDeviceGammaRamp
, /* pGetDeviceGammaRamp */
559 NULL
, /* pGetFontData */
560 NULL
, /* pGetFontUnicodeRanges */
561 NULL
, /* pGetGlyphIndices */
562 NULL
, /* pGetGlyphOutline */
563 X11DRV_GetICMProfile
, /* pGetICMProfile */
564 X11DRV_GetImage
, /* pGetImage */
565 NULL
, /* pGetKerningPairs */
566 X11DRV_GetNearestColor
, /* pGetNearestColor */
567 NULL
, /* pGetOutlineTextMetrics */
568 NULL
, /* pGetPixel */
569 NULL
, /* pGetPixelFormat */
570 X11DRV_GetSystemPaletteEntries
, /* pGetSystemPaletteEntries */
571 NULL
, /* pGetTextCharsetInfo */
572 NULL
, /* pGetTextExtentExPoint */
573 NULL
, /* pGetTextExtentExPointI */
574 NULL
, /* pGetTextFace */
575 NULL
, /* pGetTextMetrics */
576 X11DRV_GradientFill
, /* pGradientFill */
577 NULL
, /* pIntersectClipRect */
578 NULL
, /* pInvertRgn */
579 X11DRV_LineTo
, /* pLineTo */
580 NULL
, /* pModifyWorldTransform */
582 NULL
, /* pOffsetClipRgn */
583 NULL
, /* pOffsetViewportOrg */
584 NULL
, /* pOffsetWindowOrg */
585 X11DRV_PaintRgn
, /* pPaintRgn */
586 X11DRV_PatBlt
, /* pPatBlt */
587 X11DRV_Pie
, /* pPie */
588 NULL
, /* pPolyBezier */
589 NULL
, /* pPolyBezierTo */
590 NULL
, /* pPolyDraw */
591 X11DRV_PolyPolygon
, /* pPolyPolygon */
592 X11DRV_PolyPolyline
, /* pPolyPolyline */
593 X11DRV_Polygon
, /* pPolygon */
594 NULL
, /* pPolyline */
595 NULL
, /* pPolylineTo */
596 X11DRV_PutImage
, /* pPutImage */
597 X11DRV_RealizeDefaultPalette
, /* pRealizeDefaultPalette */
598 X11DRV_RealizePalette
, /* pRealizePalette */
599 X11DRV_Rectangle
, /* pRectangle */
601 NULL
, /* pRestoreDC */
602 X11DRV_RoundRect
, /* pRoundRect */
604 NULL
, /* pScaleViewportExt */
605 NULL
, /* pScaleWindowExt */
606 X11DRV_SelectBitmap
, /* pSelectBitmap */
607 X11DRV_SelectBrush
, /* pSelectBrush */
608 NULL
, /* pSelectClipPath */
609 NULL
, /* pSelectFont */
610 NULL
, /* pSelectPalette */
611 X11DRV_SelectPen
, /* pSelectPen */
612 NULL
, /* pSetArcDirection */
613 NULL
, /* pSetBkColor */
614 NULL
, /* pSetBkMode */
615 X11DRV_SetBoundsRect
, /* pSetBoundsRect */
616 X11DRV_SetDCBrushColor
, /* pSetDCBrushColor */
617 X11DRV_SetDCPenColor
, /* pSetDCPenColor */
618 NULL
, /* pSetDIBitsToDevice */
619 X11DRV_SetDeviceClipping
, /* pSetDeviceClipping */
620 X11DRV_SetDeviceGammaRamp
, /* pSetDeviceGammaRamp */
621 NULL
, /* pSetLayout */
622 NULL
, /* pSetMapMode */
623 NULL
, /* pSetMapperFlags */
624 X11DRV_SetPixel
, /* pSetPixel */
625 X11DRV_SetPixelFormat
, /* pSetPixelFormat */
626 NULL
, /* pSetPolyFillMode */
628 NULL
, /* pSetRelAbs */
629 NULL
, /* pSetStretchBltMode */
630 NULL
, /* pSetTextAlign */
631 NULL
, /* pSetTextCharacterExtra */
632 NULL
, /* pSetTextColor */
633 NULL
, /* pSetTextJustification */
634 NULL
, /* pSetViewportExt */
635 NULL
, /* pSetViewportOrg */
636 NULL
, /* pSetWindowExt */
637 NULL
, /* pSetWindowOrg */
638 NULL
, /* pSetWorldTransform */
639 NULL
, /* pStartDoc */
640 NULL
, /* pStartPage */
641 X11DRV_StretchBlt
, /* pStretchBlt */
642 NULL
, /* pStretchDIBits */
643 NULL
, /* pStrokeAndFillPath */
644 NULL
, /* pStrokePath */
645 NULL
, /* pSwapBuffers */
646 X11DRV_UnrealizePalette
, /* pUnrealizePalette */
647 NULL
, /* pWidenPath */
648 NULL
, /* pwglCopyContext */
649 X11DRV_wglCreateContext
, /* pwglCreateContext */
650 X11DRV_wglCreateContextAttribsARB
, /* pwglCreateContextAttribsARB */
651 NULL
, /* pwglDeleteContext */
652 X11DRV_wglGetProcAddress
, /* pwglGetProcAddress */
653 NULL
, /* pwglMakeContextCurrentARB */
654 NULL
, /* pwglMakeCurrent */
655 X11DRV_wglSetPixelFormatWINE
, /* pwglSetPixelFormatWINE */
656 NULL
, /* pwglShareLists */
657 NULL
, /* pwglUseFontBitmapsA */
658 NULL
, /* pwglUseFontBitmapsW */
659 GDI_PRIORITY_GRAPHICS_DRV
/* priority */
663 /******************************************************************************
664 * X11DRV_get_gdi_driver
666 const struct gdi_dc_funcs
* CDECL
X11DRV_get_gdi_driver( unsigned int version
)
668 if (version
!= WINE_GDI_DRIVER_VERSION
)
670 ERR( "version mismatch, gdi32 wants %u but winex11 has %u\n", version
, WINE_GDI_DRIVER_VERSION
);
673 return &x11drv_funcs
;