winex11: Add a helper function to return the virtual screen rectangle.
[wine.git] / dlls / winex11.drv / init.c
blobfc6b8ee6f5ca4f79d65cd37dd1f0b8f7d1703339
1 /*
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
21 #include "config.h"
23 #include <stdarg.h>
24 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "x11drv.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
34 Display *gdi_display; /* display to use for all GDI functions */
36 /* a few dynamic device caps */
37 static int log_pixels_x; /* pixels per logical inch in x direction */
38 static int log_pixels_y; /* pixels per logical inch in y direction */
39 static int horz_size; /* horz. size of screen in millimeters */
40 static int vert_size; /* vert. size of screen in millimeters */
41 static int palette_size;
43 static Pixmap stock_bitmap_pixmap; /* phys bitmap for the default stock bitmap */
45 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
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 /******************************************************************************
54 * get_dpi
56 * get the dpi from the registry
58 static DWORD get_dpi( void )
60 DWORD dpi = 96;
61 HKEY hkey;
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)
71 dpi = new_dpi;
73 RegCloseKey(hkey);
75 return dpi;
78 /**********************************************************************
79 * device_init
81 * Perform initializations needed upon creation of the first device.
83 static BOOL WINAPI device_init( INIT_ONCE *once, void *param, void **context )
85 /* Initialize XRender */
86 xrender_funcs = X11DRV_XRender_Init();
88 /* Init Xcursor */
89 X11DRV_Xcursor_Init();
91 palette_size = X11DRV_PALETTE_Init();
93 stock_bitmap_pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
95 /* Initialize device caps */
96 log_pixels_x = log_pixels_y = get_dpi();
97 horz_size = MulDiv( screen_width, 254, log_pixels_x * 10 );
98 vert_size = MulDiv( screen_height, 254, log_pixels_y * 10 );
99 return TRUE;
103 static X11DRV_PDEVICE *create_x11_physdev( Drawable drawable )
105 X11DRV_PDEVICE *physDev;
107 InitOnceExecuteOnce( &init_once, device_init, NULL, NULL );
109 if (!(physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) ))) return NULL;
111 physDev->drawable = drawable;
112 physDev->gc = XCreateGC( gdi_display, drawable, 0, NULL );
113 XSetGraphicsExposures( gdi_display, physDev->gc, False );
114 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
115 XFlush( gdi_display );
116 return physDev;
119 /**********************************************************************
120 * X11DRV_CreateDC
122 static BOOL X11DRV_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
123 LPCWSTR output, const DEVMODEW* initData )
125 X11DRV_PDEVICE *physDev = create_x11_physdev( root_window );
127 if (!physDev) return FALSE;
129 physDev->depth = default_visual.depth;
130 physDev->color_shifts = &X11DRV_PALETTE_default_shifts;
131 physDev->dc_rect = get_virtual_screen_rect();
132 OffsetRect( &physDev->dc_rect, -physDev->dc_rect.left, -physDev->dc_rect.top );
133 push_dc_driver( pdev, &physDev->dev, &x11drv_funcs );
134 if (xrender_funcs && !xrender_funcs->pCreateDC( pdev, driver, device, output, initData )) return FALSE;
135 return TRUE;
139 /**********************************************************************
140 * X11DRV_CreateCompatibleDC
142 static BOOL X11DRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
144 X11DRV_PDEVICE *physDev = create_x11_physdev( stock_bitmap_pixmap );
146 if (!physDev) return FALSE;
148 physDev->depth = 1;
149 SetRect( &physDev->dc_rect, 0, 0, 1, 1 );
150 push_dc_driver( pdev, &physDev->dev, &x11drv_funcs );
151 if (orig) return TRUE; /* we already went through Xrender if we have an orig device */
152 if (xrender_funcs && !xrender_funcs->pCreateCompatibleDC( NULL, pdev )) return FALSE;
153 return TRUE;
157 /**********************************************************************
158 * X11DRV_DeleteDC
160 static BOOL X11DRV_DeleteDC( PHYSDEV dev )
162 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
164 XFreeGC( gdi_display, physDev->gc );
165 HeapFree( GetProcessHeap(), 0, physDev );
166 return TRUE;
170 void add_device_bounds( X11DRV_PDEVICE *dev, const RECT *rect )
172 RECT rc;
174 if (!dev->bounds) return;
175 if (dev->region && GetRgnBox( dev->region, &rc ))
177 if (IntersectRect( &rc, &rc, rect )) add_bounds_rect( dev->bounds, &rc );
179 else add_bounds_rect( dev->bounds, rect );
182 /***********************************************************************
183 * X11DRV_SetBoundsRect
185 static UINT X11DRV_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
187 X11DRV_PDEVICE *pdev = get_x11drv_dev( dev );
189 if (flags & DCB_DISABLE) pdev->bounds = NULL;
190 else if (flags & DCB_ENABLE) pdev->bounds = rect;
191 return DCB_RESET; /* we don't have device-specific bounds */
195 /***********************************************************************
196 * GetDeviceCaps (X11DRV.@)
198 static INT X11DRV_GetDeviceCaps( PHYSDEV dev, INT cap )
200 switch(cap)
202 case DRIVERVERSION:
203 return 0x300;
204 case TECHNOLOGY:
205 return DT_RASDISPLAY;
206 case HORZSIZE:
207 return horz_size;
208 case VERTSIZE:
209 return vert_size;
210 case HORZRES:
211 return screen_width;
212 case VERTRES:
213 return screen_height;
214 case DESKTOPHORZRES:
216 RECT virtual_rect = get_virtual_screen_rect();
217 return virtual_rect.right - virtual_rect.left;
219 case DESKTOPVERTRES:
221 RECT virtual_rect = get_virtual_screen_rect();
222 return virtual_rect.bottom - virtual_rect.top;
224 case BITSPIXEL:
225 return screen_bpp;
226 case PLANES:
227 return 1;
228 case NUMBRUSHES:
229 return -1;
230 case NUMPENS:
231 return -1;
232 case NUMMARKERS:
233 return 0;
234 case NUMFONTS:
235 return 0;
236 case NUMCOLORS:
237 /* MSDN: Number of entries in the device's color table, if the device has
238 * a color depth of no more than 8 bits per pixel.For devices with greater
239 * color depths, -1 is returned. */
240 return (default_visual.depth > 8) ? -1 : (1 << default_visual.depth);
241 case PDEVICESIZE:
242 return sizeof(X11DRV_PDEVICE);
243 case CURVECAPS:
244 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
245 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
246 case LINECAPS:
247 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
248 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
249 case POLYGONALCAPS:
250 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
251 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
252 case TEXTCAPS:
253 return (TC_OP_CHARACTER | TC_OP_STROKE | TC_CP_STROKE |
254 TC_CR_ANY | TC_SF_X_YINDEP | TC_SA_DOUBLE | TC_SA_INTEGER |
255 TC_SA_CONTIN | TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE | TC_VA_ABLE);
256 case CLIPCAPS:
257 return CP_REGION;
258 case COLORRES:
259 /* The observed correspondence between BITSPIXEL and COLORRES is:
260 * BITSPIXEL: 8 -> COLORRES: 18
261 * BITSPIXEL: 16 -> COLORRES: 16
262 * BITSPIXEL: 24 -> COLORRES: 24
263 * BITSPIXEL: 32 -> COLORRES: 24 */
264 return (screen_bpp <= 8) ? 18 : min( 24, screen_bpp );
265 case RASTERCAPS:
266 return (RC_BITBLT | RC_BANDING | RC_SCALING | RC_BITMAP64 | RC_DI_BITMAP |
267 RC_DIBTODEV | RC_BIGFONT | RC_STRETCHBLT | RC_STRETCHDIB | RC_DEVBITS |
268 (palette_size ? RC_PALETTE : 0));
269 case SHADEBLENDCAPS:
270 return (SB_GRAD_RECT | SB_GRAD_TRI | SB_CONST_ALPHA | SB_PIXEL_ALPHA);
271 case ASPECTX:
272 case ASPECTY:
273 return 36;
274 case ASPECTXY:
275 return 51;
276 case LOGPIXELSX:
277 return log_pixels_x;
278 case LOGPIXELSY:
279 return log_pixels_y;
280 case CAPS1:
281 FIXME("(%p): CAPS1 is unimplemented, will return 0\n", dev->hdc );
282 /* please see wingdi.h for the possible bit-flag values that need
283 to be returned. */
284 return 0;
285 case SIZEPALETTE:
286 return palette_size;
287 case NUMRESERVED:
288 case PHYSICALWIDTH:
289 case PHYSICALHEIGHT:
290 case PHYSICALOFFSETX:
291 case PHYSICALOFFSETY:
292 case SCALINGFACTORX:
293 case SCALINGFACTORY:
294 case VREFRESH:
295 case BLTALIGNMENT:
296 return 0;
297 default:
298 FIXME("(%p): unsupported capability %d, will return 0\n", dev->hdc, cap );
299 return 0;
304 /***********************************************************************
305 * SelectFont
307 static HFONT X11DRV_SelectFont( PHYSDEV dev, HFONT hfont, UINT *aa_flags )
309 if (default_visual.depth <= 8) *aa_flags = GGO_BITMAP; /* no anti-aliasing on <= 8bpp */
310 dev = GET_NEXT_PHYSDEV( dev, pSelectFont );
311 return dev->funcs->pSelectFont( dev, hfont, aa_flags );
314 /**********************************************************************
315 * ExtEscape (X11DRV.@)
317 static INT X11DRV_ExtEscape( PHYSDEV dev, INT escape, INT in_count, LPCVOID in_data,
318 INT out_count, LPVOID out_data )
320 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
322 switch(escape)
324 case QUERYESCSUPPORT:
325 if (in_data && in_count >= sizeof(DWORD))
327 switch (*(const INT *)in_data)
329 case X11DRV_ESCAPE:
330 return TRUE;
333 break;
335 case X11DRV_ESCAPE:
336 if (in_data && in_count >= sizeof(enum x11drv_escape_codes))
338 switch(*(const enum x11drv_escape_codes *)in_data)
340 case X11DRV_SET_DRAWABLE:
341 if (in_count >= sizeof(struct x11drv_escape_set_drawable))
343 const struct x11drv_escape_set_drawable *data = in_data;
344 physDev->dc_rect = data->dc_rect;
345 physDev->drawable = data->drawable;
346 XFreeGC( gdi_display, physDev->gc );
347 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
348 XSetGraphicsExposures( gdi_display, physDev->gc, False );
349 XSetSubwindowMode( gdi_display, physDev->gc, data->mode );
350 TRACE( "SET_DRAWABLE hdc %p drawable %lx dc_rect %s\n",
351 dev->hdc, physDev->drawable, wine_dbgstr_rect(&physDev->dc_rect) );
352 return TRUE;
354 break;
355 case X11DRV_GET_DRAWABLE:
356 if (out_count >= sizeof(struct x11drv_escape_get_drawable))
358 struct x11drv_escape_get_drawable *data = out_data;
359 data->drawable = physDev->drawable;
360 return TRUE;
362 break;
363 case X11DRV_FLUSH_GL_DRAWABLE:
364 if (in_count >= sizeof(struct x11drv_escape_flush_gl_drawable))
366 const struct x11drv_escape_flush_gl_drawable *data = in_data;
367 RECT rect = physDev->dc_rect;
369 OffsetRect( &rect, -physDev->dc_rect.left, -physDev->dc_rect.top );
370 /* The GL drawable may be lagged behind if we don't flush first, so
371 * flush the display make sure we copy up-to-date data */
372 XFlush( gdi_display );
373 XSetFunction( gdi_display, physDev->gc, GXcopy );
374 XCopyArea( gdi_display, data->gl_drawable, physDev->drawable, physDev->gc,
375 0, 0, rect.right, rect.bottom,
376 physDev->dc_rect.left, physDev->dc_rect.top );
377 add_device_bounds( physDev, &rect );
378 return TRUE;
380 break;
381 case X11DRV_START_EXPOSURES:
382 XSetGraphicsExposures( gdi_display, physDev->gc, True );
383 physDev->exposures = 0;
384 return TRUE;
385 case X11DRV_END_EXPOSURES:
386 if (out_count >= sizeof(HRGN))
388 HRGN hrgn = 0, tmp = 0;
390 XSetGraphicsExposures( gdi_display, physDev->gc, False );
391 if (physDev->exposures)
393 for (;;)
395 XEvent event;
397 XWindowEvent( gdi_display, physDev->drawable, ~0, &event );
398 if (event.type == NoExpose) break;
399 if (event.type == GraphicsExpose)
401 RECT rect;
403 rect.left = event.xgraphicsexpose.x - physDev->dc_rect.left;
404 rect.top = event.xgraphicsexpose.y - physDev->dc_rect.top;
405 rect.right = rect.left + event.xgraphicsexpose.width;
406 rect.bottom = rect.top + event.xgraphicsexpose.height;
407 if (GetLayout( dev->hdc ) & LAYOUT_RTL)
408 mirror_rect( &physDev->dc_rect, &rect );
410 TRACE( "got %s count %d\n", wine_dbgstr_rect(&rect),
411 event.xgraphicsexpose.count );
413 if (!tmp) tmp = CreateRectRgnIndirect( &rect );
414 else SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
415 if (hrgn) CombineRgn( hrgn, hrgn, tmp, RGN_OR );
416 else
418 hrgn = tmp;
419 tmp = 0;
421 if (!event.xgraphicsexpose.count) break;
423 else
425 ERR( "got unexpected event %d\n", event.type );
426 break;
429 if (tmp) DeleteObject( tmp );
431 *(HRGN *)out_data = hrgn;
432 return TRUE;
434 break;
435 default:
436 break;
439 break;
441 return 0;
444 /**********************************************************************
445 * X11DRV_wine_get_wgl_driver
447 static struct opengl_funcs * X11DRV_wine_get_wgl_driver( PHYSDEV dev, UINT version )
449 struct opengl_funcs *ret;
451 if (!(ret = get_glx_driver( version )))
453 dev = GET_NEXT_PHYSDEV( dev, wine_get_wgl_driver );
454 ret = dev->funcs->wine_get_wgl_driver( dev, version );
456 return ret;
460 static const struct gdi_dc_funcs x11drv_funcs =
462 NULL, /* pAbortDoc */
463 NULL, /* pAbortPath */
464 NULL, /* pAlphaBlend */
465 NULL, /* pAngleArc */
466 X11DRV_Arc, /* pArc */
467 NULL, /* pArcTo */
468 NULL, /* pBeginPath */
469 NULL, /* pBlendImage */
470 X11DRV_Chord, /* pChord */
471 NULL, /* pCloseFigure */
472 X11DRV_CreateCompatibleDC, /* pCreateCompatibleDC */
473 X11DRV_CreateDC, /* pCreateDC */
474 X11DRV_DeleteDC, /* pDeleteDC */
475 NULL, /* pDeleteObject */
476 NULL, /* pDeviceCapabilities */
477 X11DRV_Ellipse, /* pEllipse */
478 NULL, /* pEndDoc */
479 NULL, /* pEndPage */
480 NULL, /* pEndPath */
481 NULL, /* pEnumFonts */
482 X11DRV_EnumICMProfiles, /* pEnumICMProfiles */
483 NULL, /* pExcludeClipRect */
484 NULL, /* pExtDeviceMode */
485 X11DRV_ExtEscape, /* pExtEscape */
486 X11DRV_ExtFloodFill, /* pExtFloodFill */
487 NULL, /* pExtSelectClipRgn */
488 NULL, /* pExtTextOut */
489 NULL, /* pFillPath */
490 NULL, /* pFillRgn */
491 NULL, /* pFlattenPath */
492 NULL, /* pFontIsLinked */
493 NULL, /* pFrameRgn */
494 NULL, /* pGdiComment */
495 NULL, /* pGdiRealizationInfo */
496 NULL, /* pGetBoundsRect */
497 NULL, /* pGetCharABCWidths */
498 NULL, /* pGetCharABCWidthsI */
499 NULL, /* pGetCharWidth */
500 X11DRV_GetDeviceCaps, /* pGetDeviceCaps */
501 X11DRV_GetDeviceGammaRamp, /* pGetDeviceGammaRamp */
502 NULL, /* pGetFontData */
503 NULL, /* pGetFontUnicodeRanges */
504 NULL, /* pGetGlyphIndices */
505 NULL, /* pGetGlyphOutline */
506 X11DRV_GetICMProfile, /* pGetICMProfile */
507 X11DRV_GetImage, /* pGetImage */
508 NULL, /* pGetKerningPairs */
509 X11DRV_GetNearestColor, /* pGetNearestColor */
510 NULL, /* pGetOutlineTextMetrics */
511 NULL, /* pGetPixel */
512 X11DRV_GetSystemPaletteEntries, /* pGetSystemPaletteEntries */
513 NULL, /* pGetTextCharsetInfo */
514 NULL, /* pGetTextExtentExPoint */
515 NULL, /* pGetTextExtentExPointI */
516 NULL, /* pGetTextFace */
517 NULL, /* pGetTextMetrics */
518 X11DRV_GradientFill, /* pGradientFill */
519 NULL, /* pIntersectClipRect */
520 NULL, /* pInvertRgn */
521 X11DRV_LineTo, /* pLineTo */
522 NULL, /* pModifyWorldTransform */
523 NULL, /* pMoveTo */
524 NULL, /* pOffsetClipRgn */
525 NULL, /* pOffsetViewportOrg */
526 NULL, /* pOffsetWindowOrg */
527 X11DRV_PaintRgn, /* pPaintRgn */
528 X11DRV_PatBlt, /* pPatBlt */
529 X11DRV_Pie, /* pPie */
530 NULL, /* pPolyBezier */
531 NULL, /* pPolyBezierTo */
532 NULL, /* pPolyDraw */
533 X11DRV_PolyPolygon, /* pPolyPolygon */
534 X11DRV_PolyPolyline, /* pPolyPolyline */
535 X11DRV_Polygon, /* pPolygon */
536 NULL, /* pPolyline */
537 NULL, /* pPolylineTo */
538 X11DRV_PutImage, /* pPutImage */
539 X11DRV_RealizeDefaultPalette, /* pRealizeDefaultPalette */
540 X11DRV_RealizePalette, /* pRealizePalette */
541 X11DRV_Rectangle, /* pRectangle */
542 NULL, /* pResetDC */
543 NULL, /* pRestoreDC */
544 X11DRV_RoundRect, /* pRoundRect */
545 NULL, /* pSaveDC */
546 NULL, /* pScaleViewportExt */
547 NULL, /* pScaleWindowExt */
548 NULL, /* pSelectBitmap */
549 X11DRV_SelectBrush, /* pSelectBrush */
550 NULL, /* pSelectClipPath */
551 X11DRV_SelectFont, /* pSelectFont */
552 NULL, /* pSelectPalette */
553 X11DRV_SelectPen, /* pSelectPen */
554 NULL, /* pSetArcDirection */
555 NULL, /* pSetBkColor */
556 NULL, /* pSetBkMode */
557 X11DRV_SetBoundsRect, /* pSetBoundsRect */
558 X11DRV_SetDCBrushColor, /* pSetDCBrushColor */
559 X11DRV_SetDCPenColor, /* pSetDCPenColor */
560 NULL, /* pSetDIBitsToDevice */
561 X11DRV_SetDeviceClipping, /* pSetDeviceClipping */
562 X11DRV_SetDeviceGammaRamp, /* pSetDeviceGammaRamp */
563 NULL, /* pSetLayout */
564 NULL, /* pSetMapMode */
565 NULL, /* pSetMapperFlags */
566 X11DRV_SetPixel, /* pSetPixel */
567 NULL, /* pSetPolyFillMode */
568 NULL, /* pSetROP2 */
569 NULL, /* pSetRelAbs */
570 NULL, /* pSetStretchBltMode */
571 NULL, /* pSetTextAlign */
572 NULL, /* pSetTextCharacterExtra */
573 NULL, /* pSetTextColor */
574 NULL, /* pSetTextJustification */
575 NULL, /* pSetViewportExt */
576 NULL, /* pSetViewportOrg */
577 NULL, /* pSetWindowExt */
578 NULL, /* pSetWindowOrg */
579 NULL, /* pSetWorldTransform */
580 NULL, /* pStartDoc */
581 NULL, /* pStartPage */
582 X11DRV_StretchBlt, /* pStretchBlt */
583 NULL, /* pStretchDIBits */
584 NULL, /* pStrokeAndFillPath */
585 NULL, /* pStrokePath */
586 X11DRV_UnrealizePalette, /* pUnrealizePalette */
587 NULL, /* pWidenPath */
588 X11DRV_wine_get_wgl_driver, /* wine_get_wgl_driver */
589 GDI_PRIORITY_GRAPHICS_DRV /* priority */
593 /******************************************************************************
594 * X11DRV_get_gdi_driver
596 const struct gdi_dc_funcs * CDECL X11DRV_get_gdi_driver( unsigned int version )
598 if (version != WINE_GDI_DRIVER_VERSION)
600 ERR( "version mismatch, gdi32 wants %u but winex11 has %u\n", version, WINE_GDI_DRIVER_VERSION );
601 return NULL;
603 return &x11drv_funcs;