我正在尝试通过 xlib 检索所有 Windows 的事件。我使用 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,则不会显示错误。有谁知道这有什么问题?