2010-09-05 46 views
6

我試圖創建一個位深度爲32的X11窗口,以便我可以使用ARGB顏色。這是我做的:如何創建一個位深度爲32的窗口

XVisualInfo vinfo; 
int depth = 32; 
XMatchVisualInfo(dpy, XDefaultScreen(dpy), depth, TrueColor, &vinfo); 
XCreateWindow(dpy, XDefaultRootWindow(dpy), 0, 0, 150, 100, 0, depth, InputOutput, 
    vinfo.visual, 0, NULL); 

這裏發生了什麼:

X Error of failed request: BadMatch (invalid parameter attributes) 
    Major opcode of failed request: 1 (X_CreateWindow) 
    Serial number of failed request: 7 
    Current serial number in output stream: 7

上爲什麼有一個BadMatch錯誤任何指針?

回答

12

問題是這樣的代碼在X服務器http://cgit.freedesktop.org/xorg/xserver/tree/dix/window.c#n615

if (((vmask & (CWBorderPixmap | CWBorderPixel)) == 0) && 
    (class != InputOnly) && 
    (depth != pParent->drawable.depth)) 
    { 
    *error = BadMatch; 
    return NullWindow; 
    } 

即「如果深度不一樣父深度,你必須設置邊界像素或像素」

這裏是一個整體例如

#include <X11/Xlib.h> 
#include <X11/Xutil.h> 
#include <X11/extensions/Xcomposite.h> 

#include <stdio.h> 

int main(int argc, char **argv) 
{ 
    Display *dpy; 
    XVisualInfo vinfo; 
    int depth; 
    XVisualInfo *visual_list; 
    XVisualInfo visual_template; 
    int nxvisuals; 
    int i; 
    XSetWindowAttributes attrs; 
    Window parent; 
    Visual *visual; 

    dpy = XOpenDisplay(NULL); 

    nxvisuals = 0; 
    visual_template.screen = DefaultScreen(dpy); 
    visual_list = XGetVisualInfo (dpy, VisualScreenMask, &visual_template, &nxvisuals); 

    for (i = 0; i < nxvisuals; ++i) 
    { 
     printf(" %3d: visual 0x%lx class %d (%s) depth %d\n", 
      i, 
      visual_list[i].visualid, 
      visual_list[i].class, 
      visual_list[i].class == TrueColor ? "TrueColor" : "unknown", 
      visual_list[i].depth); 
    } 

    if (!XMatchVisualInfo(dpy, XDefaultScreen(dpy), 32, TrueColor, &vinfo)) 
    { 
     fprintf(stderr, "no such visual\n"); 
     return 1; 
    } 

    printf("Matched visual 0x%lx class %d (%s) depth %d\n", 
     vinfo.visualid, 
     vinfo.class, 
     vinfo.class == TrueColor ? "TrueColor" : "unknown", 
     vinfo.depth); 

    parent = XDefaultRootWindow(dpy); 

    XSync(dpy, True); 

    printf("creating RGBA child\n"); 

    visual = vinfo.visual; 
    depth = vinfo.depth; 

    attrs.colormap = XCreateColormap(dpy, XDefaultRootWindow(dpy), visual, AllocNone); 
    attrs.background_pixel = 0; 
    attrs.border_pixel = 0; 

    XCreateWindow(dpy, parent, 10, 10, 150, 100, 0, depth, InputOutput, 
       visual, CWBackPixel | CWColormap | CWBorderPixel, &attrs); 

    XSync(dpy, True); 

    printf("No error\n"); 

    return 0; 
} 
+1

當我設置一個邊界像素,我還得到一個壞匹配誤差(是的,我在同XCreateWindow()調用這樣做)。 – 2010-09-05 15:26:48

+1

我想我的測試程序也設置了顏色表。 – 2010-09-07 15:10:50

+1

謝謝,它似乎需要一個backpixel,colormap *和* borderpixel。 – 2010-09-07 20:08:26

相關問題