如何创建一个位深度为32的窗口

13
我正在尝试创建一个具有32位色深的X11窗口,以便可以使用ARGB颜色。 我的操作如下:
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错误:BadMatch(无效的参数属性)
失败请求的主要操作码:1(X_CreateWindow)
失败请求的序列号:7
输出流中的当前序列号:7
请问为什么会出现BadMatch错误?
1个回答

19

问题出在 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;
}

3
当我设置一个边界像素时,仍然会出现“bad match”错误(是的,我在同一个XCreateWindow()调用中执行此操作)。 - Uli Schlachter
1
我猜测我的测试程序也设置了色彩映射表。 - Havoc P
1
谢谢,看起来需要一个背景像素、颜色映射 边框像素。 - Uli Schlachter
1
实际上,根本不需要backpixel。 - Uli Schlachter
一定要记得执行 XFree(visual_list);,否则会出现内存泄漏。 - Eric Stdlib

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接