上周花了两天时间,一直搞不懂为什么,今天接着弄,终于了解了点原因。先上我的测试例子:
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrender.h> //licl2012-06-05
#define WINDOW_SIZE 200
int main (int argc, char *argv[])
{
Display *dpy;
XSetWindowAttributes attributes;
Window win;
GC gc;
XKeyEvent event;
int i;
int nvi;//licl2012-06-05
int j;//licl2012-06-05
XRenderPictFormat *format;//licl2012-06-05
Visual *visual = NULL;//licl2012-06-05
XVisualInfo *xvi;//licl2012-05-30
XVisualInfo temp;//licl2012-05-30
// ... X Server.... X Server ......
dpy = XOpenDisplay(NULL);
// .. 200X200 .......
attributes.background_pixel = XWhitePixel(dpy, 0);
temp.screen = DefaultScreen(dpy);
temp.depth = 32;
temp.class = TrueColor;
if ((xvi = XGetVisualInfo (dpy,
VisualScreenMask |
VisualDepthMask |
VisualClassMask,
&temp,
&nvi)) == NULL)
{
printf(" failed geting VisualInfo\n");
}
else{
printf(" succeeded geting VisualInfo\n");
}
for (j = 0; j < nvi; j++)
{
printf(" %3d: visual 0x%lx class %d (%s) depth %d\n",
j,
xvi[j].visualid,
xvi[j].class,
xvi[j].class == TrueColor ? "TrueColor" : "unknown",
xvi[j].depth);
format = XRenderFindVisualFormat (dpy, xvi[j].visual);
if (format->type == PictTypeDirect && format->direct.alphaMask)
{
visual = xvi[j].visual;
printf(" succeeded geting rgba visual\n");
break;
}
}
win = XCreateWindow(dpy, XRootWindow(dpy, 0),
0, 0, WINDOW_SIZE, WINDOW_SIZE, 0,
32,//DefaultDepth(dpy, 0),
InputOutput,
//DefaultVisual(dpy, 0),
visual,
CWBackPixel,&attributes);
// .......
XSelectInput(dpy, win, ExposureMask | KeyPressMask );
// .......
gc = XCreateGC(dpy, win, 0, NULL);
//Map ..
XMapWindow(dpy, win);
// .......... Expose ... KeyPress ..
while(1)