init.c 19.2 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * X11 graphics driver initialisation functions
 *
 * Copyright 1996 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

Patrik Stridvall's avatar
Patrik Stridvall committed
21 22
#include "config.h"

23
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
24
#include <string.h>
25

26
#include "windef.h"
27
#include "winbase.h"
28
#include "winreg.h"
29
#include "x11drv.h"
30
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
31

32
WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
33

34 35
Display *gdi_display;  /* display to use for all GDI functions */

36 37
static int palette_size;

38
static Pixmap stock_bitmap_pixmap;  /* phys bitmap for the default stock bitmap */
39

40 41
static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;

42
static const struct gdi_dc_funcs x11drv_funcs;
43
static const struct gdi_dc_funcs *xrender_funcs;
44

Alexandre Julliard's avatar
Alexandre Julliard committed
45
/**********************************************************************
46 47 48
 *	     device_init
 *
 * Perform initializations needed upon creation of the first device.
Alexandre Julliard's avatar
Alexandre Julliard committed
49
 */
50
static BOOL WINAPI device_init( INIT_ONCE *once, void *param, void **context )
Alexandre Julliard's avatar
Alexandre Julliard committed
51
{
52
    /* Initialize XRender */
53
    xrender_funcs = X11DRV_XRender_Init();
54

55 56 57
    /* Init Xcursor */
    X11DRV_Xcursor_Init();

58
    palette_size = X11DRV_PALETTE_Init();
Alexandre Julliard's avatar
Alexandre Julliard committed
59

60
    stock_bitmap_pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
Alexandre Julliard's avatar
Alexandre Julliard committed
61

62
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
63 64
}

65 66

static X11DRV_PDEVICE *create_x11_physdev( Drawable drawable )
Alexandre Julliard's avatar
Alexandre Julliard committed
67 68 69
{
    X11DRV_PDEVICE *physDev;

70
    InitOnceExecuteOnce( &init_once, device_init, NULL, NULL );
71

72
    if (!(physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) ))) return NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
73

74 75
    physDev->drawable = drawable;
    physDev->gc = XCreateGC( gdi_display, drawable, 0, NULL );
76 77 78
    XSetGraphicsExposures( gdi_display, physDev->gc, False );
    XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
    XFlush( gdi_display );
79 80 81 82 83 84
    return physDev;
}

/**********************************************************************
 *	     X11DRV_CreateDC
 */
85 86
static BOOL CDECL X11DRV_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
                                   LPCWSTR output, const DEVMODEW* initData )
87 88 89 90 91
{
    X11DRV_PDEVICE *physDev = create_x11_physdev( root_window );

    if (!physDev) return FALSE;

92
    physDev->depth         = default_visual.depth;
93
    physDev->color_shifts  = &X11DRV_PALETTE_default_shifts;
94 95
    physDev->dc_rect       = get_virtual_screen_rect();
    OffsetRect( &physDev->dc_rect, -physDev->dc_rect.left, -physDev->dc_rect.top );
96
    push_dc_driver( pdev, &physDev->dev, &x11drv_funcs );
97 98
    if (xrender_funcs && !xrender_funcs->pCreateDC( pdev, driver, device, output, initData )) return FALSE;
    return TRUE;
99 100 101 102 103 104
}


/**********************************************************************
 *	     X11DRV_CreateCompatibleDC
 */
105
static BOOL CDECL X11DRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
106
{
107
    X11DRV_PDEVICE *physDev = create_x11_physdev( stock_bitmap_pixmap );
108 109 110 111

    if (!physDev) return FALSE;

    physDev->depth  = 1;
112
    SetRect( &physDev->dc_rect, 0, 0, 1, 1 );
113
    push_dc_driver( pdev, &physDev->dev, &x11drv_funcs );
114
    if (orig) return TRUE;  /* we already went through Xrender if we have an orig device */
115 116
    if (xrender_funcs && !xrender_funcs->pCreateCompatibleDC( NULL, pdev )) return FALSE;
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
117 118 119 120 121 122
}


/**********************************************************************
 *	     X11DRV_DeleteDC
 */
123
static BOOL CDECL X11DRV_DeleteDC( PHYSDEV dev )
Alexandre Julliard's avatar
Alexandre Julliard committed
124
{
125 126
    X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );

127
    XFreeGC( gdi_display, physDev->gc );
128
    HeapFree( GetProcessHeap(), 0, physDev );
Alexandre Julliard's avatar
Alexandre Julliard committed
129 130
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
131

132

133 134 135 136
void add_device_bounds( X11DRV_PDEVICE *dev, const RECT *rect )
{
    RECT rc;

137
    if (!dev->bounds) return;
138 139
    if (dev->region && GetRgnBox( dev->region, &rc ))
    {
140
        if (IntersectRect( &rc, &rc, rect )) add_bounds_rect( dev->bounds, &rc );
141
    }
142
    else add_bounds_rect( dev->bounds, rect );
143 144
}

145 146 147
/***********************************************************************
 *           X11DRV_SetBoundsRect
 */
148
static UINT CDECL X11DRV_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
149 150 151
{
    X11DRV_PDEVICE *pdev = get_x11drv_dev( dev );

152 153 154
    if (flags & DCB_DISABLE) pdev->bounds = NULL;
    else if (flags & DCB_ENABLE) pdev->bounds = rect;
    return DCB_RESET;  /* we don't have device-specific bounds */
155 156 157
}


158 159 160
/***********************************************************************
 *           GetDeviceCaps    (X11DRV.@)
 */
161
static INT CDECL X11DRV_GetDeviceCaps( PHYSDEV dev, INT cap )
162 163 164 165 166 167
{
    switch(cap)
    {
    case SIZEPALETTE:
        return palette_size;
    default:
168 169
        dev = GET_NEXT_PHYSDEV( dev, pGetDeviceCaps );
        return dev->funcs->pGetDeviceCaps( dev, cap );
170 171 172 173
    }
}


174 175 176
/***********************************************************************
 *           SelectFont
 */
177
static HFONT CDECL X11DRV_SelectFont( PHYSDEV dev, HFONT hfont, UINT *aa_flags )
178 179 180 181 182 183
{
    if (default_visual.depth <= 8) *aa_flags = GGO_BITMAP;  /* no anti-aliasing on <= 8bpp */
    dev = GET_NEXT_PHYSDEV( dev, pSelectFont );
    return dev->funcs->pSelectFont( dev, hfont, aa_flags );
}

Alexandre Julliard's avatar
Alexandre Julliard committed
184
/**********************************************************************
185
 *           ExtEscape  (X11DRV.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
186
 */
187 188
static INT CDECL X11DRV_ExtEscape( PHYSDEV dev, INT escape, INT in_count, LPCVOID in_data,
                                   INT out_count, LPVOID out_data )
Alexandre Julliard's avatar
Alexandre Julliard committed
189
{
190 191
    X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );

192
    switch(escape)
Alexandre Julliard's avatar
Alexandre Julliard committed
193
    {
194
    case QUERYESCSUPPORT:
195
        if (in_data && in_count >= sizeof(DWORD))
196
        {
Eric Pouech's avatar
Eric Pouech committed
197
            switch (*(const INT *)in_data)
198
            {
199 200
            case X11DRV_ESCAPE:
                return TRUE;
201 202 203 204
            }
        }
        break;

205 206 207
    case X11DRV_ESCAPE:
        if (in_data && in_count >= sizeof(enum x11drv_escape_codes))
        {
Eric Pouech's avatar
Eric Pouech committed
208
            switch(*(const enum x11drv_escape_codes *)in_data)
209
            {
210 211 212
            case X11DRV_SET_DRAWABLE:
                if (in_count >= sizeof(struct x11drv_escape_set_drawable))
                {
213
                    const struct x11drv_escape_set_drawable *data = in_data;
214
                    physDev->dc_rect = data->dc_rect;
215
                    physDev->drawable = data->drawable;
216 217 218
                    XFreeGC( gdi_display, physDev->gc );
                    physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
                    XSetGraphicsExposures( gdi_display, physDev->gc, False );
219
                    XSetSubwindowMode( gdi_display, physDev->gc, data->mode );
220 221
                    TRACE( "SET_DRAWABLE hdc %p drawable %lx dc_rect %s\n",
                           dev->hdc, physDev->drawable, wine_dbgstr_rect(&physDev->dc_rect) );
222 223 224
                    return TRUE;
                }
                break;
225 226 227 228 229 230 231 232
            case X11DRV_GET_DRAWABLE:
                if (out_count >= sizeof(struct x11drv_escape_get_drawable))
                {
                    struct x11drv_escape_get_drawable *data = out_data;
                    data->drawable = physDev->drawable;
                    return TRUE;
                }
                break;
233 234 235 236 237 238 239
            case X11DRV_FLUSH_GL_DRAWABLE:
                if (in_count >= sizeof(struct x11drv_escape_flush_gl_drawable))
                {
                    const struct x11drv_escape_flush_gl_drawable *data = in_data;
                    RECT rect = physDev->dc_rect;

                    OffsetRect( &rect, -physDev->dc_rect.left, -physDev->dc_rect.top );
240
                    if (data->flush) XFlush( gdi_display );
241 242 243 244 245 246 247 248
                    XSetFunction( gdi_display, physDev->gc, GXcopy );
                    XCopyArea( gdi_display, data->gl_drawable, physDev->drawable, physDev->gc,
                               0, 0, rect.right, rect.bottom,
                               physDev->dc_rect.left, physDev->dc_rect.top );
                    add_device_bounds( physDev, &rect );
                    return TRUE;
                }
                break;
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
            case X11DRV_START_EXPOSURES:
                XSetGraphicsExposures( gdi_display, physDev->gc, True );
                physDev->exposures = 0;
                return TRUE;
            case X11DRV_END_EXPOSURES:
                if (out_count >= sizeof(HRGN))
                {
                    HRGN hrgn = 0, tmp = 0;

                    XSetGraphicsExposures( gdi_display, physDev->gc, False );
                    if (physDev->exposures)
                    {
                        for (;;)
                        {
                            XEvent event;

                            XWindowEvent( gdi_display, physDev->drawable, ~0, &event );
                            if (event.type == NoExpose) break;
                            if (event.type == GraphicsExpose)
                            {
269
                                RECT rect;
270

271 272 273 274
                                rect.left   = event.xgraphicsexpose.x - physDev->dc_rect.left;
                                rect.top    = event.xgraphicsexpose.y - physDev->dc_rect.top;
                                rect.right  = rect.left + event.xgraphicsexpose.width;
                                rect.bottom = rect.top + event.xgraphicsexpose.height;
275
                                if (GetLayout( dev->hdc ) & LAYOUT_RTL)
276 277 278
                                    mirror_rect( &physDev->dc_rect, &rect );

                                TRACE( "got %s count %d\n", wine_dbgstr_rect(&rect),
279 280
                                       event.xgraphicsexpose.count );

281 282
                                if (!tmp) tmp = CreateRectRgnIndirect( &rect );
                                else SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
                                if (hrgn) CombineRgn( hrgn, hrgn, tmp, RGN_OR );
                                else
                                {
                                    hrgn = tmp;
                                    tmp = 0;
                                }
                                if (!event.xgraphicsexpose.count) break;
                            }
                            else
                            {
                                ERR( "got unexpected event %d\n", event.type );
                                break;
                            }
                        }
                        if (tmp) DeleteObject( tmp );
                    }
                    *(HRGN *)out_data = hrgn;
                    return TRUE;
                }
                break;
303 304
            default:
                break;
305 306 307
            }
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
308 309 310
    }
    return 0;
}
311

312 313 314
/**********************************************************************
 *           X11DRV_wine_get_wgl_driver
 */
315
static struct opengl_funcs * CDECL X11DRV_wine_get_wgl_driver( PHYSDEV dev, UINT version )
316 317 318 319 320 321 322 323 324 325 326
{
    struct opengl_funcs *ret;

    if (!(ret = get_glx_driver( version )))
    {
        dev = GET_NEXT_PHYSDEV( dev, wine_get_wgl_driver );
        ret = dev->funcs->wine_get_wgl_driver( dev, version );
    }
    return ret;
}

327 328 329
/**********************************************************************
 *           X11DRV_wine_get_vulkan_driver
 */
330
static const struct vulkan_funcs * CDECL X11DRV_wine_get_vulkan_driver( PHYSDEV dev, UINT version )
331 332 333 334 335 336 337 338 339 340 341
{
    const struct vulkan_funcs *ret;

    if (!(ret = get_vulkan_driver( version )))
    {
        dev = GET_NEXT_PHYSDEV( dev, wine_get_vulkan_driver );
        ret = dev->funcs->wine_get_vulkan_driver( dev, version );
    }
    return ret;
}

342 343 344 345 346

static const struct gdi_dc_funcs x11drv_funcs =
{
    NULL,                               /* pAbortDoc */
    NULL,                               /* pAbortPath */
347
    NULL,                               /* pAlphaBlend */
348 349 350 351
    NULL,                               /* pAngleArc */
    X11DRV_Arc,                         /* pArc */
    NULL,                               /* pArcTo */
    NULL,                               /* pBeginPath */
352
    NULL,                               /* pBlendImage */
353 354
    X11DRV_Chord,                       /* pChord */
    NULL,                               /* pCloseFigure */
355
    X11DRV_CreateCompatibleDC,          /* pCreateCompatibleDC */
356 357 358 359 360 361 362 363
    X11DRV_CreateDC,                    /* pCreateDC */
    X11DRV_DeleteDC,                    /* pDeleteDC */
    NULL,                               /* pDeleteObject */
    NULL,                               /* pDeviceCapabilities */
    X11DRV_Ellipse,                     /* pEllipse */
    NULL,                               /* pEndDoc */
    NULL,                               /* pEndPage */
    NULL,                               /* pEndPath */
364
    NULL,                               /* pEnumFonts */
365 366 367 368
    X11DRV_EnumICMProfiles,             /* pEnumICMProfiles */
    NULL,                               /* pExtDeviceMode */
    X11DRV_ExtEscape,                   /* pExtEscape */
    X11DRV_ExtFloodFill,                /* pExtFloodFill */
369
    NULL,                               /* pExtTextOut */
370
    X11DRV_FillPath,                    /* pFillPath */
371 372
    NULL,                               /* pFillRgn */
    NULL,                               /* pFlattenPath */
373
    NULL,                               /* pFontIsLinked */
374 375
    NULL,                               /* pFrameRgn */
    NULL,                               /* pGdiComment */
376
    NULL,                               /* pGetBoundsRect */
377 378
    NULL,                               /* pGetCharABCWidths */
    NULL,                               /* pGetCharABCWidthsI */
379
    NULL,                               /* pGetCharWidth */
380
    NULL,                               /* pGetCharWidthInfo */
381 382
    X11DRV_GetDeviceCaps,               /* pGetDeviceCaps */
    X11DRV_GetDeviceGammaRamp,          /* pGetDeviceGammaRamp */
383
    NULL,                               /* pGetFontData */
384
    NULL,                               /* pGetFontRealizationInfo */
385 386 387
    NULL,                               /* pGetFontUnicodeRanges */
    NULL,                               /* pGetGlyphIndices */
    NULL,                               /* pGetGlyphOutline */
388
    X11DRV_GetICMProfile,               /* pGetICMProfile */
389
    X11DRV_GetImage,                    /* pGetImage */
390
    NULL,                               /* pGetKerningPairs */
391
    X11DRV_GetNearestColor,             /* pGetNearestColor */
392
    NULL,                               /* pGetOutlineTextMetrics */
393
    NULL,                               /* pGetPixel */
394
    X11DRV_GetSystemPaletteEntries,     /* pGetSystemPaletteEntries */
395
    NULL,                               /* pGetTextCharsetInfo */
396
    NULL,                               /* pGetTextExtentExPoint */
397 398
    NULL,                               /* pGetTextExtentExPointI */
    NULL,                               /* pGetTextFace */
399
    NULL,                               /* pGetTextMetrics */
400
    X11DRV_GradientFill,                /* pGradientFill */
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
    NULL,                               /* pInvertRgn */
    X11DRV_LineTo,                      /* pLineTo */
    NULL,                               /* pModifyWorldTransform */
    NULL,                               /* pMoveTo */
    NULL,                               /* pOffsetViewportOrg */
    NULL,                               /* pOffsetWindowOrg */
    X11DRV_PaintRgn,                    /* pPaintRgn */
    X11DRV_PatBlt,                      /* pPatBlt */
    X11DRV_Pie,                         /* pPie */
    NULL,                               /* pPolyBezier */
    NULL,                               /* pPolyBezierTo */
    NULL,                               /* pPolyDraw */
    X11DRV_PolyPolygon,                 /* pPolyPolygon */
    X11DRV_PolyPolyline,                /* pPolyPolyline */
    NULL,                               /* pPolylineTo */
416
    X11DRV_PutImage,                    /* pPutImage */
417 418 419 420 421 422 423 424
    X11DRV_RealizeDefaultPalette,       /* pRealizeDefaultPalette */
    X11DRV_RealizePalette,              /* pRealizePalette */
    X11DRV_Rectangle,                   /* pRectangle */
    NULL,                               /* pResetDC */
    NULL,                               /* pRestoreDC */
    X11DRV_RoundRect,                   /* pRoundRect */
    NULL,                               /* pScaleViewportExt */
    NULL,                               /* pScaleWindowExt */
425
    NULL,                               /* pSelectBitmap */
426 427
    X11DRV_SelectBrush,                 /* pSelectBrush */
    NULL,                               /* pSelectClipPath */
428
    X11DRV_SelectFont,                  /* pSelectFont */
429 430
    NULL,                               /* pSelectPalette */
    X11DRV_SelectPen,                   /* pSelectPen */
431
    NULL,                               /* pSetBkColor */
432
    X11DRV_SetBoundsRect,               /* pSetBoundsRect */
433 434
    X11DRV_SetDCBrushColor,             /* pSetDCBrushColor */
    X11DRV_SetDCPenColor,               /* pSetDCPenColor */
435
    NULL,                               /* pSetDIBitsToDevice */
436 437 438 439 440 441 442
    X11DRV_SetDeviceClipping,           /* pSetDeviceClipping */
    X11DRV_SetDeviceGammaRamp,          /* pSetDeviceGammaRamp */
    NULL,                               /* pSetLayout */
    NULL,                               /* pSetMapMode */
    NULL,                               /* pSetMapperFlags */
    X11DRV_SetPixel,                    /* pSetPixel */
    NULL,                               /* pSetTextCharacterExtra */
443
    NULL,                               /* pSetTextColor */
444 445 446 447 448 449 450 451 452 453
    NULL,                               /* pSetTextJustification */
    NULL,                               /* pSetViewportExt */
    NULL,                               /* pSetViewportOrg */
    NULL,                               /* pSetWindowExt */
    NULL,                               /* pSetWindowOrg */
    NULL,                               /* pSetWorldTransform */
    NULL,                               /* pStartDoc */
    NULL,                               /* pStartPage */
    X11DRV_StretchBlt,                  /* pStretchBlt */
    NULL,                               /* pStretchDIBits */
454 455
    X11DRV_StrokeAndFillPath,           /* pStrokeAndFillPath */
    X11DRV_StrokePath,                  /* pStrokePath */
456 457
    X11DRV_UnrealizePalette,            /* pUnrealizePalette */
    NULL,                               /* pWidenPath */
458
    X11DRV_D3DKMTCheckVidPnExclusiveOwnership, /* pD3DKMTCheckVidPnExclusiveOwnership */
459
    X11DRV_D3DKMTSetVidPnSourceOwner,   /* pD3DKMTSetVidPnSourceOwner */
460
    X11DRV_wine_get_wgl_driver,         /* wine_get_wgl_driver */
461
    X11DRV_wine_get_vulkan_driver,      /* wine_get_vulkan_driver */
462
    GDI_PRIORITY_GRAPHICS_DRV           /* priority */
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
};


/******************************************************************************
 *      X11DRV_get_gdi_driver
 */
const struct gdi_dc_funcs * CDECL X11DRV_get_gdi_driver( unsigned int version )
{
    if (version != WINE_GDI_DRIVER_VERSION)
    {
        ERR( "version mismatch, gdi32 wants %u but winex11 has %u\n", version, WINE_GDI_DRIVER_VERSION );
        return NULL;
    }
    return &x11drv_funcs;
}