使用 xlib XChangeWindowAttributes 检索所有 XWindow 事件

发布于 2024-10-03 06:44:10 字数 1494 浏览 2 评论 0原文

我正在尝试通过 xlib 检索所有 Windows 事件。我使用 XChangeWindowAttributes 将 SubstructRedirectMask 添加到根窗口。但是当我执行这个程序时,它显示 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;
}

如果我删除 SubstructRedirectMask,则不会显示错误。 有人知道这是怎么回事吗?

I'm trying to retrieve all windows's events by xlib. I used XChangeWindowAttributes to add SubstructureRedirectMask to root window. But when I executed this program, it showed BadAccess as following:

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

The program is following:

/*
   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;
}

If I remove SubstructureRedirectMask, there are not error shown.
Anyone know what's wrong with that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何必那么矫情 2024-10-10 06:44:10

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

Only one client at a time is allowed to do it. You already have such client, it's your window manager.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文