使用xlib XChangeWindowAttributes检索所有XWindow事件

时间:2010-11-18 09:21:05

标签: events x11 xlib

我正在尝试通过xlib检索所有窗口的事件。我使用XChangeWindowAttributes将SubstructureRedirectMask添加到根窗口。但是当我执行这个程序时,它显示BadAccess如下:

dorowu@dorowu-F3JP:~/src/xwindow$ sudo ./tmp
X Error of failed request:  BadAccess (attempt to access private resource denied)
  Major opcode of failed request:  2 (X_ChangeWindowAttributes)
  Serial number of failed request:  7
  Current serial number in output stream:  8

该计划如下:

/*
   Simple Xlib application drawing a box in a window.
   To Compile: gcc -O2 -Wall -o test test.c -L /usr/X11R6/lib -lX11 -lm
   */

#include<X11/Xlib.h>
#include<stdio.h>
#include<stdlib.h> // prevents error for exit on line 18 when compiling with gcc

int main() {
    Display *d;
    XEvent e;

    /* open connection with the server */
    d = XOpenDisplay(NULL);
    if (d == NULL) {
        printf("Cannot open display\n");
        exit(1);
    }

    // sniffer events
    XSetWindowAttributes attr;

    attr.override_redirect = 1;
    attr.event_mask = SubstructureRedirectMask | SubstructureNotifyMask |
        KeyReleaseMask | PointerMotionMask ;
    XChangeWindowAttributes(d, XDefaultRootWindow(d), CWEventMask , &attr);
    XSync(d, False);

    /* event loop */
    while(1) {
        XNextEvent(d, &e);
        printf("event: %d\n", e.type);
    }

    /* close connection to server */
    XCloseDisplay(d);

    return 0;
}

如果我删除SubstructureRedirectMask,则不会显示错误。 有谁知道这有什么问题?

1 个答案:

答案 0 :(得分:0)

一次只允许一个客户端执行此操作。你已经有了这样的客户,这是你的窗口管理员。