winex11: Recreate the graphics context when setting the drawable.
[wine.git] / dlls / winex11.drv / init.c
blobd6b45d8e8fac3c2c2c3b928ba2e5d243aa50d810
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 "ddrawi.h"
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 /******************************************************************************
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 void device_init(void)
85 device_init_done = TRUE;
87 /* Initialize XRender */
88 xrender_funcs = X11DRV_XRender_Init();
90 /* Init Xcursor */
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 );
117 return physDev;
120 /**********************************************************************
121 * X11DRV_CreateDC
123 static BOOL X11DRV_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
124 LPCWSTR output, const DEVMODEW* initData )
126 const struct gdi_dc_funcs *glx_funcs = get_glx_driver();
127 X11DRV_PDEVICE *physDev = create_x11_physdev( root_window );
129 if (!physDev) return FALSE;
131 physDev->depth = default_visual.depth;
132 physDev->color_shifts = &X11DRV_PALETTE_default_shifts;
133 SetRect( &physDev->dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
134 virtual_screen_rect.bottom - virtual_screen_rect.top );
135 push_dc_driver( pdev, &physDev->dev, &x11drv_funcs );
136 if (xrender_funcs && !xrender_funcs->pCreateDC( pdev, driver, device, output, initData )) return FALSE;
137 if (glx_funcs && !glx_funcs->pCreateDC( pdev, driver, device, output, initData )) return FALSE;
138 return TRUE;
142 /**********************************************************************
143 * X11DRV_CreateCompatibleDC
145 static BOOL X11DRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
147 const struct gdi_dc_funcs *glx_funcs = get_glx_driver();
148 X11DRV_PDEVICE *physDev = create_x11_physdev( stock_bitmap_pixmap );
150 if (!physDev) return FALSE;
152 physDev->depth = 1;
153 SetRect( &physDev->dc_rect, 0, 0, 1, 1 );
154 push_dc_driver( pdev, &physDev->dev, &x11drv_funcs );
155 if (orig) return TRUE; /* we already went through Xrender if we have an orig device */
156 if (xrender_funcs && !xrender_funcs->pCreateCompatibleDC( NULL, pdev )) return FALSE;
157 if (glx_funcs && !glx_funcs->pCreateCompatibleDC( NULL, pdev )) return FALSE;
158 return TRUE;
162 /**********************************************************************
163 * X11DRV_DeleteDC
165 static BOOL X11DRV_DeleteDC( PHYSDEV dev )
167 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
169 XFreeGC( gdi_display, physDev->gc );
170 HeapFree( GetProcessHeap(), 0, physDev );
171 return TRUE;
175 void add_device_bounds( X11DRV_PDEVICE *dev, const RECT *rect )
177 RECT rc;
179 if (!dev->bounds) return;
180 if (dev->region && GetRgnBox( dev->region, &rc ))
182 if (IntersectRect( &rc, &rc, rect )) add_bounds_rect( dev->bounds, &rc );
184 else add_bounds_rect( dev->bounds, rect );
187 /***********************************************************************
188 * X11DRV_SetBoundsRect
190 static UINT X11DRV_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
192 X11DRV_PDEVICE *pdev = get_x11drv_dev( dev );
194 if (flags & DCB_DISABLE) pdev->bounds = NULL;
195 else if (flags & DCB_ENABLE) pdev->bounds = rect;
196 return DCB_RESET; /* we don't have device-specific bounds */
200 /***********************************************************************
201 * GetDeviceCaps (X11DRV.@)
203 static INT X11DRV_GetDeviceCaps( PHYSDEV dev, INT cap )
205 switch(cap)
207 case DRIVERVERSION:
208 return 0x300;
209 case TECHNOLOGY:
210 return DT_RASDISPLAY;
211 case HORZSIZE:
212 return horz_size;
213 case VERTSIZE:
214 return vert_size;
215 case HORZRES:
216 return screen_width;
217 case VERTRES:
218 return screen_height;
219 case DESKTOPHORZRES:
220 return virtual_screen_rect.right - virtual_screen_rect.left;
221 case DESKTOPVERTRES:
222 return virtual_screen_rect.bottom - virtual_screen_rect.top;
223 case BITSPIXEL:
224 return screen_bpp;
225 case PLANES:
226 return 1;
227 case NUMBRUSHES:
228 return -1;
229 case NUMPENS:
230 return -1;
231 case NUMMARKERS:
232 return 0;
233 case NUMFONTS:
234 return 0;
235 case NUMCOLORS:
236 /* MSDN: Number of entries in the device's color table, if the device has
237 * a color depth of no more than 8 bits per pixel.For devices with greater
238 * color depths, -1 is returned. */
239 return (default_visual.depth > 8) ? -1 : (1 << default_visual.depth);
240 case PDEVICESIZE:
241 return sizeof(X11DRV_PDEVICE);
242 case CURVECAPS:
243 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
244 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
245 case LINECAPS:
246 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
247 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
248 case POLYGONALCAPS:
249 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
250 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
251 case TEXTCAPS:
252 return (TC_OP_CHARACTER | TC_OP_STROKE | TC_CP_STROKE |
253 TC_CR_ANY | TC_SF_X_YINDEP | TC_SA_DOUBLE | TC_SA_INTEGER |
254 TC_SA_CONTIN | TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE | TC_VA_ABLE);
255 case CLIPCAPS:
256 return CP_REGION;
257 case COLORRES:
258 /* The observed correspondence between BITSPIXEL and COLORRES is:
259 * BITSPIXEL: 8 -> COLORRES: 18
260 * BITSPIXEL: 16 -> COLORRES: 16
261 * BITSPIXEL: 24 -> COLORRES: 24
262 * BITSPIXEL: 32 -> COLORRES: 24 */
263 return (screen_bpp <= 8) ? 18 : min( 24, screen_bpp );
264 case RASTERCAPS:
265 return (RC_BITBLT | RC_BANDING | RC_SCALING | RC_BITMAP64 | RC_DI_BITMAP |
266 RC_DIBTODEV | RC_BIGFONT | RC_STRETCHBLT | RC_STRETCHDIB | RC_DEVBITS |
267 (palette_size ? RC_PALETTE : 0));
268 case SHADEBLENDCAPS:
269 return (SB_GRAD_RECT | SB_GRAD_TRI | SB_CONST_ALPHA | SB_PIXEL_ALPHA);
270 case ASPECTX:
271 case ASPECTY:
272 return 36;
273 case ASPECTXY:
274 return 51;
275 case LOGPIXELSX:
276 return log_pixels_x;
277 case LOGPIXELSY:
278 return log_pixels_y;
279 case CAPS1:
280 FIXME("(%p): CAPS1 is unimplemented, will return 0\n", dev->hdc );
281 /* please see wingdi.h for the possible bit-flag values that need
282 to be returned. */
283 return 0;
284 case SIZEPALETTE:
285 return palette_size;
286 case NUMRESERVED:
287 case PHYSICALWIDTH:
288 case PHYSICALHEIGHT:
289 case PHYSICALOFFSETX:
290 case PHYSICALOFFSETY:
291 case SCALINGFACTORX:
292 case SCALINGFACTORY:
293 case VREFRESH:
294 case BLTALIGNMENT:
295 return 0;
296 default:
297 FIXME("(%p): unsupported capability %d, will return 0\n", dev->hdc, cap );
298 return 0;
303 /**********************************************************************
304 * ExtEscape (X11DRV.@)
306 static INT X11DRV_ExtEscape( PHYSDEV dev, INT escape, INT in_count, LPCVOID in_data,
307 INT out_count, LPVOID out_data )
309 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
311 switch(escape)
313 case QUERYESCSUPPORT:
314 if (in_data)
316 switch (*(const INT *)in_data)
318 case DCICOMMAND:
319 return DD_HAL_VERSION;
320 case X11DRV_ESCAPE:
321 return TRUE;
324 break;
326 case X11DRV_ESCAPE:
327 if (in_data && in_count >= sizeof(enum x11drv_escape_codes))
329 switch(*(const enum x11drv_escape_codes *)in_data)
331 case X11DRV_SET_DRAWABLE:
332 if (in_count >= sizeof(struct x11drv_escape_set_drawable))
334 const struct x11drv_escape_set_drawable *data = in_data;
335 physDev->dc_rect = data->dc_rect;
336 physDev->drawable = data->drawable;
337 XFreeGC( gdi_display, physDev->gc );
338 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
339 XSetGraphicsExposures( gdi_display, physDev->gc, False );
340 XSetSubwindowMode( gdi_display, physDev->gc, data->mode );
341 TRACE( "SET_DRAWABLE hdc %p drawable %lx dc_rect %s\n",
342 dev->hdc, physDev->drawable, wine_dbgstr_rect(&physDev->dc_rect) );
343 return TRUE;
345 break;
346 case X11DRV_GET_DRAWABLE:
347 if (out_count >= sizeof(struct x11drv_escape_get_drawable))
349 struct x11drv_escape_get_drawable *data = out_data;
350 data->drawable = physDev->drawable;
351 return TRUE;
353 break;
354 case X11DRV_START_EXPOSURES:
355 XSetGraphicsExposures( gdi_display, physDev->gc, True );
356 physDev->exposures = 0;
357 return TRUE;
358 case X11DRV_END_EXPOSURES:
359 if (out_count >= sizeof(HRGN))
361 HRGN hrgn = 0, tmp = 0;
363 XSetGraphicsExposures( gdi_display, physDev->gc, False );
364 if (physDev->exposures)
366 for (;;)
368 XEvent event;
370 XWindowEvent( gdi_display, physDev->drawable, ~0, &event );
371 if (event.type == NoExpose) break;
372 if (event.type == GraphicsExpose)
374 RECT rect;
376 rect.left = event.xgraphicsexpose.x - physDev->dc_rect.left;
377 rect.top = event.xgraphicsexpose.y - physDev->dc_rect.top;
378 rect.right = rect.left + event.xgraphicsexpose.width;
379 rect.bottom = rect.top + event.xgraphicsexpose.height;
380 if (GetLayout( dev->hdc ) & LAYOUT_RTL)
381 mirror_rect( &physDev->dc_rect, &rect );
383 TRACE( "got %s count %d\n", wine_dbgstr_rect(&rect),
384 event.xgraphicsexpose.count );
386 if (!tmp) tmp = CreateRectRgnIndirect( &rect );
387 else SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
388 if (hrgn) CombineRgn( hrgn, hrgn, tmp, RGN_OR );
389 else
391 hrgn = tmp;
392 tmp = 0;
394 if (!event.xgraphicsexpose.count) break;
396 else
398 ERR( "got unexpected event %d\n", event.type );
399 break;
402 if (tmp) DeleteObject( tmp );
404 *(HRGN *)out_data = hrgn;
405 return TRUE;
407 break;
408 default:
409 break;
412 break;
414 return 0;
418 static const struct gdi_dc_funcs x11drv_funcs =
420 NULL, /* pAbortDoc */
421 NULL, /* pAbortPath */
422 NULL, /* pAlphaBlend */
423 NULL, /* pAngleArc */
424 X11DRV_Arc, /* pArc */
425 NULL, /* pArcTo */
426 NULL, /* pBeginPath */
427 NULL, /* pBlendImage */
428 X11DRV_Chord, /* pChord */
429 NULL, /* pCloseFigure */
430 X11DRV_CreateCompatibleDC, /* pCreateCompatibleDC */
431 X11DRV_CreateDC, /* pCreateDC */
432 X11DRV_DeleteDC, /* pDeleteDC */
433 NULL, /* pDeleteObject */
434 NULL, /* pDeviceCapabilities */
435 X11DRV_Ellipse, /* pEllipse */
436 NULL, /* pEndDoc */
437 NULL, /* pEndPage */
438 NULL, /* pEndPath */
439 NULL, /* pEnumFonts */
440 X11DRV_EnumICMProfiles, /* pEnumICMProfiles */
441 NULL, /* pExcludeClipRect */
442 NULL, /* pExtDeviceMode */
443 X11DRV_ExtEscape, /* pExtEscape */
444 X11DRV_ExtFloodFill, /* pExtFloodFill */
445 NULL, /* pExtSelectClipRgn */
446 NULL, /* pExtTextOut */
447 NULL, /* pFillPath */
448 NULL, /* pFillRgn */
449 NULL, /* pFlattenPath */
450 NULL, /* pFontIsLinked */
451 NULL, /* pFrameRgn */
452 NULL, /* pGdiComment */
453 NULL, /* pGdiRealizationInfo */
454 NULL, /* pGetBoundsRect */
455 NULL, /* pGetCharABCWidths */
456 NULL, /* pGetCharABCWidthsI */
457 NULL, /* pGetCharWidth */
458 X11DRV_GetDeviceCaps, /* pGetDeviceCaps */
459 X11DRV_GetDeviceGammaRamp, /* pGetDeviceGammaRamp */
460 NULL, /* pGetFontData */
461 NULL, /* pGetFontUnicodeRanges */
462 NULL, /* pGetGlyphIndices */
463 NULL, /* pGetGlyphOutline */
464 X11DRV_GetICMProfile, /* pGetICMProfile */
465 X11DRV_GetImage, /* pGetImage */
466 NULL, /* pGetKerningPairs */
467 X11DRV_GetNearestColor, /* pGetNearestColor */
468 NULL, /* pGetOutlineTextMetrics */
469 NULL, /* pGetPixel */
470 X11DRV_GetSystemPaletteEntries, /* pGetSystemPaletteEntries */
471 NULL, /* pGetTextCharsetInfo */
472 NULL, /* pGetTextExtentExPoint */
473 NULL, /* pGetTextExtentExPointI */
474 NULL, /* pGetTextFace */
475 NULL, /* pGetTextMetrics */
476 X11DRV_GradientFill, /* pGradientFill */
477 NULL, /* pIntersectClipRect */
478 NULL, /* pInvertRgn */
479 X11DRV_LineTo, /* pLineTo */
480 NULL, /* pModifyWorldTransform */
481 NULL, /* pMoveTo */
482 NULL, /* pOffsetClipRgn */
483 NULL, /* pOffsetViewportOrg */
484 NULL, /* pOffsetWindowOrg */
485 X11DRV_PaintRgn, /* pPaintRgn */
486 X11DRV_PatBlt, /* pPatBlt */
487 X11DRV_Pie, /* pPie */
488 NULL, /* pPolyBezier */
489 NULL, /* pPolyBezierTo */
490 NULL, /* pPolyDraw */
491 X11DRV_PolyPolygon, /* pPolyPolygon */
492 X11DRV_PolyPolyline, /* pPolyPolyline */
493 X11DRV_Polygon, /* pPolygon */
494 NULL, /* pPolyline */
495 NULL, /* pPolylineTo */
496 X11DRV_PutImage, /* pPutImage */
497 X11DRV_RealizeDefaultPalette, /* pRealizeDefaultPalette */
498 X11DRV_RealizePalette, /* pRealizePalette */
499 X11DRV_Rectangle, /* pRectangle */
500 NULL, /* pResetDC */
501 NULL, /* pRestoreDC */
502 X11DRV_RoundRect, /* pRoundRect */
503 NULL, /* pSaveDC */
504 NULL, /* pScaleViewportExt */
505 NULL, /* pScaleWindowExt */
506 NULL, /* pSelectBitmap */
507 X11DRV_SelectBrush, /* pSelectBrush */
508 NULL, /* pSelectClipPath */
509 NULL, /* pSelectFont */
510 NULL, /* pSelectPalette */
511 X11DRV_SelectPen, /* pSelectPen */
512 NULL, /* pSetArcDirection */
513 NULL, /* pSetBkColor */
514 NULL, /* pSetBkMode */
515 X11DRV_SetBoundsRect, /* pSetBoundsRect */
516 X11DRV_SetDCBrushColor, /* pSetDCBrushColor */
517 X11DRV_SetDCPenColor, /* pSetDCPenColor */
518 NULL, /* pSetDIBitsToDevice */
519 X11DRV_SetDeviceClipping, /* pSetDeviceClipping */
520 X11DRV_SetDeviceGammaRamp, /* pSetDeviceGammaRamp */
521 NULL, /* pSetLayout */
522 NULL, /* pSetMapMode */
523 NULL, /* pSetMapperFlags */
524 X11DRV_SetPixel, /* pSetPixel */
525 NULL, /* pSetPolyFillMode */
526 NULL, /* pSetROP2 */
527 NULL, /* pSetRelAbs */
528 NULL, /* pSetStretchBltMode */
529 NULL, /* pSetTextAlign */
530 NULL, /* pSetTextCharacterExtra */
531 NULL, /* pSetTextColor */
532 NULL, /* pSetTextJustification */
533 NULL, /* pSetViewportExt */
534 NULL, /* pSetViewportOrg */
535 NULL, /* pSetWindowExt */
536 NULL, /* pSetWindowOrg */
537 NULL, /* pSetWorldTransform */
538 NULL, /* pStartDoc */
539 NULL, /* pStartPage */
540 X11DRV_StretchBlt, /* pStretchBlt */
541 NULL, /* pStretchDIBits */
542 NULL, /* pStrokeAndFillPath */
543 NULL, /* pStrokePath */
544 X11DRV_UnrealizePalette, /* pUnrealizePalette */
545 NULL, /* pWidenPath */
546 NULL, /* wine_get_wgl_driver */
547 GDI_PRIORITY_GRAPHICS_DRV /* priority */
551 /******************************************************************************
552 * X11DRV_get_gdi_driver
554 const struct gdi_dc_funcs * CDECL X11DRV_get_gdi_driver( unsigned int version )
556 if (version != WINE_GDI_DRIVER_VERSION)
558 ERR( "version mismatch, gdi32 wants %u but winex11 has %u\n", version, WINE_GDI_DRIVER_VERSION );
559 return NULL;
561 return &x11drv_funcs;