ntdll: Use an __ms_va_list in sscanf (Clang).
[wine/multimedia.git] / dlls / winex11.drv / init.c
blob6bc4fb36ad877825085cb9d72c90a5d6cb8d2015
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 palette_size;
41 static Pixmap stock_bitmap_pixmap; /* phys bitmap for the default stock bitmap */
43 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
45 static const WCHAR dpi_key_name[] = {'S','o','f','t','w','a','r','e','\\','F','o','n','t','s','\0'};
46 static const WCHAR dpi_value_name[] = {'L','o','g','P','i','x','e','l','s','\0'};
48 static const struct gdi_dc_funcs x11drv_funcs;
49 static const struct gdi_dc_funcs *xrender_funcs;
51 /******************************************************************************
52 * get_dpi
54 * get the dpi from the registry
56 static DWORD get_dpi( void )
58 DWORD dpi = 96;
59 HKEY hkey;
61 if (RegOpenKeyW(HKEY_CURRENT_CONFIG, dpi_key_name, &hkey) == ERROR_SUCCESS)
63 DWORD type, size, new_dpi;
65 size = sizeof(new_dpi);
66 if(RegQueryValueExW(hkey, dpi_value_name, NULL, &type, (void *)&new_dpi, &size) == ERROR_SUCCESS)
68 if(type == REG_DWORD && new_dpi != 0)
69 dpi = new_dpi;
71 RegCloseKey(hkey);
73 return dpi;
76 /**********************************************************************
77 * device_init
79 * Perform initializations needed upon creation of the first device.
81 static BOOL WINAPI device_init( INIT_ONCE *once, void *param, void **context )
83 /* Initialize XRender */
84 xrender_funcs = X11DRV_XRender_Init();
86 /* Init Xcursor */
87 X11DRV_Xcursor_Init();
89 palette_size = X11DRV_PALETTE_Init();
91 stock_bitmap_pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
93 /* Initialize device caps */
94 log_pixels_x = log_pixels_y = get_dpi();
95 return TRUE;
99 static X11DRV_PDEVICE *create_x11_physdev( Drawable drawable )
101 X11DRV_PDEVICE *physDev;
103 InitOnceExecuteOnce( &init_once, device_init, NULL, NULL );
105 if (!(physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) ))) return NULL;
107 physDev->drawable = drawable;
108 physDev->gc = XCreateGC( gdi_display, drawable, 0, NULL );
109 XSetGraphicsExposures( gdi_display, physDev->gc, False );
110 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
111 XFlush( gdi_display );
112 return physDev;
115 /**********************************************************************
116 * X11DRV_CreateDC
118 static BOOL X11DRV_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
119 LPCWSTR output, const DEVMODEW* initData )
121 X11DRV_PDEVICE *physDev = create_x11_physdev( root_window );
123 if (!physDev) return FALSE;
125 physDev->depth = default_visual.depth;
126 physDev->color_shifts = &X11DRV_PALETTE_default_shifts;
127 physDev->dc_rect = get_virtual_screen_rect();
128 OffsetRect( &physDev->dc_rect, -physDev->dc_rect.left, -physDev->dc_rect.top );
129 push_dc_driver( pdev, &physDev->dev, &x11drv_funcs );
130 if (xrender_funcs && !xrender_funcs->pCreateDC( pdev, driver, device, output, initData )) return FALSE;
131 return TRUE;
135 /**********************************************************************
136 * X11DRV_CreateCompatibleDC
138 static BOOL X11DRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
140 X11DRV_PDEVICE *physDev = create_x11_physdev( stock_bitmap_pixmap );
142 if (!physDev) return FALSE;
144 physDev->depth = 1;
145 SetRect( &physDev->dc_rect, 0, 0, 1, 1 );
146 push_dc_driver( pdev, &physDev->dev, &x11drv_funcs );
147 if (orig) return TRUE; /* we already went through Xrender if we have an orig device */
148 if (xrender_funcs && !xrender_funcs->pCreateCompatibleDC( NULL, pdev )) return FALSE;
149 return TRUE;
153 /**********************************************************************
154 * X11DRV_DeleteDC
156 static BOOL X11DRV_DeleteDC( PHYSDEV dev )
158 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
160 XFreeGC( gdi_display, physDev->gc );
161 HeapFree( GetProcessHeap(), 0, physDev );
162 return TRUE;
166 void add_device_bounds( X11DRV_PDEVICE *dev, const RECT *rect )
168 RECT rc;
170 if (!dev->bounds) return;
171 if (dev->region && GetRgnBox( dev->region, &rc ))
173 if (IntersectRect( &rc, &rc, rect )) add_bounds_rect( dev->bounds, &rc );
175 else add_bounds_rect( dev->bounds, rect );
178 /***********************************************************************
179 * X11DRV_SetBoundsRect
181 static UINT X11DRV_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
183 X11DRV_PDEVICE *pdev = get_x11drv_dev( dev );
185 if (flags & DCB_DISABLE) pdev->bounds = NULL;
186 else if (flags & DCB_ENABLE) pdev->bounds = rect;
187 return DCB_RESET; /* we don't have device-specific bounds */
191 /***********************************************************************
192 * GetDeviceCaps (X11DRV.@)
194 static INT X11DRV_GetDeviceCaps( PHYSDEV dev, INT cap )
196 switch(cap)
198 case DRIVERVERSION:
199 return 0x300;
200 case TECHNOLOGY:
201 return DT_RASDISPLAY;
202 case HORZSIZE:
204 RECT primary_rect = get_primary_monitor_rect();
205 return MulDiv( primary_rect.right - primary_rect.left, 254, log_pixels_x * 10 );
207 case VERTSIZE:
209 RECT primary_rect = get_primary_monitor_rect();
210 return MulDiv( primary_rect.bottom - primary_rect.top, 254, log_pixels_y * 10 );
212 case HORZRES:
214 RECT primary_rect = get_primary_monitor_rect();
215 return primary_rect.right - primary_rect.left;
217 case VERTRES:
219 RECT primary_rect = get_primary_monitor_rect();
220 return primary_rect.bottom - primary_rect.top;
222 case DESKTOPHORZRES:
224 RECT virtual_rect = get_virtual_screen_rect();
225 return virtual_rect.right - virtual_rect.left;
227 case DESKTOPVERTRES:
229 RECT virtual_rect = get_virtual_screen_rect();
230 return virtual_rect.bottom - virtual_rect.top;
232 case BITSPIXEL:
233 return screen_bpp;
234 case PLANES:
235 return 1;
236 case NUMBRUSHES:
237 return -1;
238 case NUMPENS:
239 return -1;
240 case NUMMARKERS:
241 return 0;
242 case NUMFONTS:
243 return 0;
244 case NUMCOLORS:
245 /* MSDN: Number of entries in the device's color table, if the device has
246 * a color depth of no more than 8 bits per pixel.For devices with greater
247 * color depths, -1 is returned. */
248 return (default_visual.depth > 8) ? -1 : (1 << default_visual.depth);
249 case PDEVICESIZE:
250 return sizeof(X11DRV_PDEVICE);
251 case CURVECAPS:
252 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
253 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
254 case LINECAPS:
255 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
256 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
257 case POLYGONALCAPS:
258 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
259 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
260 case TEXTCAPS:
261 return (TC_OP_CHARACTER | TC_OP_STROKE | TC_CP_STROKE |
262 TC_CR_ANY | TC_SF_X_YINDEP | TC_SA_DOUBLE | TC_SA_INTEGER |
263 TC_SA_CONTIN | TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE | TC_VA_ABLE);
264 case CLIPCAPS:
265 return CP_REGION;
266 case COLORRES:
267 /* The observed correspondence between BITSPIXEL and COLORRES is:
268 * BITSPIXEL: 8 -> COLORRES: 18
269 * BITSPIXEL: 16 -> COLORRES: 16
270 * BITSPIXEL: 24 -> COLORRES: 24
271 * BITSPIXEL: 32 -> COLORRES: 24 */
272 return (screen_bpp <= 8) ? 18 : min( 24, screen_bpp );
273 case RASTERCAPS:
274 return (RC_BITBLT | RC_BANDING | RC_SCALING | RC_BITMAP64 | RC_DI_BITMAP |
275 RC_DIBTODEV | RC_BIGFONT | RC_STRETCHBLT | RC_STRETCHDIB | RC_DEVBITS |
276 (palette_size ? RC_PALETTE : 0));
277 case SHADEBLENDCAPS:
278 return (SB_GRAD_RECT | SB_GRAD_TRI | SB_CONST_ALPHA | SB_PIXEL_ALPHA);
279 case ASPECTX:
280 case ASPECTY:
281 return 36;
282 case ASPECTXY:
283 return 51;
284 case LOGPIXELSX:
285 return log_pixels_x;
286 case LOGPIXELSY:
287 return log_pixels_y;
288 case CAPS1:
289 FIXME("(%p): CAPS1 is unimplemented, will return 0\n", dev->hdc );
290 /* please see wingdi.h for the possible bit-flag values that need
291 to be returned. */
292 return 0;
293 case SIZEPALETTE:
294 return palette_size;
295 case NUMRESERVED:
296 case PHYSICALWIDTH:
297 case PHYSICALHEIGHT:
298 case PHYSICALOFFSETX:
299 case PHYSICALOFFSETY:
300 case SCALINGFACTORX:
301 case SCALINGFACTORY:
302 case VREFRESH:
303 case BLTALIGNMENT:
304 return 0;
305 default:
306 FIXME("(%p): unsupported capability %d, will return 0\n", dev->hdc, cap );
307 return 0;
312 /***********************************************************************
313 * SelectFont
315 static HFONT X11DRV_SelectFont( PHYSDEV dev, HFONT hfont, UINT *aa_flags )
317 if (default_visual.depth <= 8) *aa_flags = GGO_BITMAP; /* no anti-aliasing on <= 8bpp */
318 dev = GET_NEXT_PHYSDEV( dev, pSelectFont );
319 return dev->funcs->pSelectFont( dev, hfont, aa_flags );
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 );
330 switch(escape)
332 case QUERYESCSUPPORT:
333 if (in_data && in_count >= sizeof(DWORD))
335 switch (*(const INT *)in_data)
337 case X11DRV_ESCAPE:
338 return TRUE;
341 break;
343 case X11DRV_ESCAPE:
344 if (in_data && in_count >= sizeof(enum x11drv_escape_codes))
346 switch(*(const enum x11drv_escape_codes *)in_data)
348 case X11DRV_SET_DRAWABLE:
349 if (in_count >= sizeof(struct x11drv_escape_set_drawable))
351 const struct x11drv_escape_set_drawable *data = in_data;
352 physDev->dc_rect = data->dc_rect;
353 physDev->drawable = data->drawable;
354 XFreeGC( gdi_display, physDev->gc );
355 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
356 XSetGraphicsExposures( gdi_display, physDev->gc, False );
357 XSetSubwindowMode( gdi_display, physDev->gc, data->mode );
358 TRACE( "SET_DRAWABLE hdc %p drawable %lx dc_rect %s\n",
359 dev->hdc, physDev->drawable, wine_dbgstr_rect(&physDev->dc_rect) );
360 return TRUE;
362 break;
363 case X11DRV_GET_DRAWABLE:
364 if (out_count >= sizeof(struct x11drv_escape_get_drawable))
366 struct x11drv_escape_get_drawable *data = out_data;
367 data->drawable = physDev->drawable;
368 return TRUE;
370 break;
371 case X11DRV_FLUSH_GL_DRAWABLE:
372 if (in_count >= sizeof(struct x11drv_escape_flush_gl_drawable))
374 const struct x11drv_escape_flush_gl_drawable *data = in_data;
375 RECT rect = physDev->dc_rect;
377 OffsetRect( &rect, -physDev->dc_rect.left, -physDev->dc_rect.top );
378 /* The GL drawable may be lagged behind if we don't flush first, so
379 * flush the display make sure we copy up-to-date data */
380 XFlush( gdi_display );
381 XSetFunction( gdi_display, physDev->gc, GXcopy );
382 XCopyArea( gdi_display, data->gl_drawable, physDev->drawable, physDev->gc,
383 0, 0, rect.right, rect.bottom,
384 physDev->dc_rect.left, physDev->dc_rect.top );
385 add_device_bounds( physDev, &rect );
386 return TRUE;
388 break;
389 case X11DRV_START_EXPOSURES:
390 XSetGraphicsExposures( gdi_display, physDev->gc, True );
391 physDev->exposures = 0;
392 return TRUE;
393 case X11DRV_END_EXPOSURES:
394 if (out_count >= sizeof(HRGN))
396 HRGN hrgn = 0, tmp = 0;
398 XSetGraphicsExposures( gdi_display, physDev->gc, False );
399 if (physDev->exposures)
401 for (;;)
403 XEvent event;
405 XWindowEvent( gdi_display, physDev->drawable, ~0, &event );
406 if (event.type == NoExpose) break;
407 if (event.type == GraphicsExpose)
409 RECT rect;
411 rect.left = event.xgraphicsexpose.x - physDev->dc_rect.left;
412 rect.top = event.xgraphicsexpose.y - physDev->dc_rect.top;
413 rect.right = rect.left + event.xgraphicsexpose.width;
414 rect.bottom = rect.top + event.xgraphicsexpose.height;
415 if (GetLayout( dev->hdc ) & LAYOUT_RTL)
416 mirror_rect( &physDev->dc_rect, &rect );
418 TRACE( "got %s count %d\n", wine_dbgstr_rect(&rect),
419 event.xgraphicsexpose.count );
421 if (!tmp) tmp = CreateRectRgnIndirect( &rect );
422 else SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
423 if (hrgn) CombineRgn( hrgn, hrgn, tmp, RGN_OR );
424 else
426 hrgn = tmp;
427 tmp = 0;
429 if (!event.xgraphicsexpose.count) break;
431 else
433 ERR( "got unexpected event %d\n", event.type );
434 break;
437 if (tmp) DeleteObject( tmp );
439 *(HRGN *)out_data = hrgn;
440 return TRUE;
442 break;
443 default:
444 break;
447 break;
449 return 0;
452 /**********************************************************************
453 * X11DRV_wine_get_wgl_driver
455 static struct opengl_funcs * X11DRV_wine_get_wgl_driver( PHYSDEV dev, UINT version )
457 struct opengl_funcs *ret;
459 if (!(ret = get_glx_driver( version )))
461 dev = GET_NEXT_PHYSDEV( dev, wine_get_wgl_driver );
462 ret = dev->funcs->wine_get_wgl_driver( dev, version );
464 return ret;
468 static const struct gdi_dc_funcs x11drv_funcs =
470 NULL, /* pAbortDoc */
471 NULL, /* pAbortPath */
472 NULL, /* pAlphaBlend */
473 NULL, /* pAngleArc */
474 X11DRV_Arc, /* pArc */
475 NULL, /* pArcTo */
476 NULL, /* pBeginPath */
477 NULL, /* pBlendImage */
478 X11DRV_Chord, /* pChord */
479 NULL, /* pCloseFigure */
480 X11DRV_CreateCompatibleDC, /* pCreateCompatibleDC */
481 X11DRV_CreateDC, /* pCreateDC */
482 X11DRV_DeleteDC, /* pDeleteDC */
483 NULL, /* pDeleteObject */
484 NULL, /* pDeviceCapabilities */
485 X11DRV_Ellipse, /* pEllipse */
486 NULL, /* pEndDoc */
487 NULL, /* pEndPage */
488 NULL, /* pEndPath */
489 NULL, /* pEnumFonts */
490 X11DRV_EnumICMProfiles, /* pEnumICMProfiles */
491 NULL, /* pExcludeClipRect */
492 NULL, /* pExtDeviceMode */
493 X11DRV_ExtEscape, /* pExtEscape */
494 X11DRV_ExtFloodFill, /* pExtFloodFill */
495 NULL, /* pExtSelectClipRgn */
496 NULL, /* pExtTextOut */
497 NULL, /* pFillPath */
498 NULL, /* pFillRgn */
499 NULL, /* pFlattenPath */
500 NULL, /* pFontIsLinked */
501 NULL, /* pFrameRgn */
502 NULL, /* pGdiComment */
503 NULL, /* pGetBoundsRect */
504 NULL, /* pGetCharABCWidths */
505 NULL, /* pGetCharABCWidthsI */
506 NULL, /* pGetCharWidth */
507 X11DRV_GetDeviceCaps, /* pGetDeviceCaps */
508 X11DRV_GetDeviceGammaRamp, /* pGetDeviceGammaRamp */
509 NULL, /* pGetFontData */
510 NULL, /* pGetFontRealizationInfo */
511 NULL, /* pGetFontUnicodeRanges */
512 NULL, /* pGetGlyphIndices */
513 NULL, /* pGetGlyphOutline */
514 X11DRV_GetICMProfile, /* pGetICMProfile */
515 X11DRV_GetImage, /* pGetImage */
516 NULL, /* pGetKerningPairs */
517 X11DRV_GetNearestColor, /* pGetNearestColor */
518 NULL, /* pGetOutlineTextMetrics */
519 NULL, /* pGetPixel */
520 X11DRV_GetSystemPaletteEntries, /* pGetSystemPaletteEntries */
521 NULL, /* pGetTextCharsetInfo */
522 NULL, /* pGetTextExtentExPoint */
523 NULL, /* pGetTextExtentExPointI */
524 NULL, /* pGetTextFace */
525 NULL, /* pGetTextMetrics */
526 X11DRV_GradientFill, /* pGradientFill */
527 NULL, /* pIntersectClipRect */
528 NULL, /* pInvertRgn */
529 X11DRV_LineTo, /* pLineTo */
530 NULL, /* pModifyWorldTransform */
531 NULL, /* pMoveTo */
532 NULL, /* pOffsetClipRgn */
533 NULL, /* pOffsetViewportOrg */
534 NULL, /* pOffsetWindowOrg */
535 X11DRV_PaintRgn, /* pPaintRgn */
536 X11DRV_PatBlt, /* pPatBlt */
537 X11DRV_Pie, /* pPie */
538 NULL, /* pPolyBezier */
539 NULL, /* pPolyBezierTo */
540 NULL, /* pPolyDraw */
541 X11DRV_PolyPolygon, /* pPolyPolygon */
542 X11DRV_PolyPolyline, /* pPolyPolyline */
543 X11DRV_Polygon, /* pPolygon */
544 NULL, /* pPolyline */
545 NULL, /* pPolylineTo */
546 X11DRV_PutImage, /* pPutImage */
547 X11DRV_RealizeDefaultPalette, /* pRealizeDefaultPalette */
548 X11DRV_RealizePalette, /* pRealizePalette */
549 X11DRV_Rectangle, /* pRectangle */
550 NULL, /* pResetDC */
551 NULL, /* pRestoreDC */
552 X11DRV_RoundRect, /* pRoundRect */
553 NULL, /* pSaveDC */
554 NULL, /* pScaleViewportExt */
555 NULL, /* pScaleWindowExt */
556 NULL, /* pSelectBitmap */
557 X11DRV_SelectBrush, /* pSelectBrush */
558 NULL, /* pSelectClipPath */
559 X11DRV_SelectFont, /* pSelectFont */
560 NULL, /* pSelectPalette */
561 X11DRV_SelectPen, /* pSelectPen */
562 NULL, /* pSetArcDirection */
563 NULL, /* pSetBkColor */
564 NULL, /* pSetBkMode */
565 X11DRV_SetBoundsRect, /* pSetBoundsRect */
566 X11DRV_SetDCBrushColor, /* pSetDCBrushColor */
567 X11DRV_SetDCPenColor, /* pSetDCPenColor */
568 NULL, /* pSetDIBitsToDevice */
569 X11DRV_SetDeviceClipping, /* pSetDeviceClipping */
570 X11DRV_SetDeviceGammaRamp, /* pSetDeviceGammaRamp */
571 NULL, /* pSetLayout */
572 NULL, /* pSetMapMode */
573 NULL, /* pSetMapperFlags */
574 X11DRV_SetPixel, /* pSetPixel */
575 NULL, /* pSetPolyFillMode */
576 NULL, /* pSetROP2 */
577 NULL, /* pSetRelAbs */
578 NULL, /* pSetStretchBltMode */
579 NULL, /* pSetTextAlign */
580 NULL, /* pSetTextCharacterExtra */
581 NULL, /* pSetTextColor */
582 NULL, /* pSetTextJustification */
583 NULL, /* pSetViewportExt */
584 NULL, /* pSetViewportOrg */
585 NULL, /* pSetWindowExt */
586 NULL, /* pSetWindowOrg */
587 NULL, /* pSetWorldTransform */
588 NULL, /* pStartDoc */
589 NULL, /* pStartPage */
590 X11DRV_StretchBlt, /* pStretchBlt */
591 NULL, /* pStretchDIBits */
592 NULL, /* pStrokeAndFillPath */
593 NULL, /* pStrokePath */
594 X11DRV_UnrealizePalette, /* pUnrealizePalette */
595 NULL, /* pWidenPath */
596 X11DRV_wine_get_wgl_driver, /* wine_get_wgl_driver */
597 GDI_PRIORITY_GRAPHICS_DRV /* priority */
601 /******************************************************************************
602 * X11DRV_get_gdi_driver
604 const struct gdi_dc_funcs * CDECL X11DRV_get_gdi_driver( unsigned int version )
606 if (version != WINE_GDI_DRIVER_VERSION)
608 ERR( "version mismatch, gdi32 wants %u but winex11 has %u\n", version, WINE_GDI_DRIVER_VERSION );
609 return NULL;
611 return &x11drv_funcs;