1

我想记录我的鼠标点击位置。我试过这个;

#include <stdio.h>
#include <stddef.h>
#include <X11/Xlib.h>
#include <assert.h>
#include <unistd.h>
#include <signal.h>


int working = 1;

void signal_callback_handler(int signum) {
    working = 0;
}

int main () {
    signal(SIGINT, signal_callback_handler);
    signal(SIGTSTP, signal_callback_handler);
    signal(SIGTERM, signal_callback_handler);

    Display *d = XOpenDisplay(NULL);
    assert(d);

    XSelectInput(d, DefaultRootWindow(d), ButtonPressMask);
    while(working) {
        XEvent e;
        XNextEvent(d,&e);
        if (e.type == ButtonPress) {
            printf("%dx%d",e.xbutton.x,e.xbutton.y);
        }
    }


    return 0;
}

但我看到了这个错误:

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: 7

我的代码有什么问题,我该如何解决?

更新

我对此进行了更多研究,并从#xorg-dev 中的人们那里得到了一些帮助。使用常规 Xlib 似乎是不可能的,因为只有一个客户端可以注册窗口上的按钮按下。在这种情况下,我的 WM 已经注册,因此我无法访问。似乎这可以使用 X 输入扩展并通过侦听 XI_RawButtonPress 事件来完成,我仍在试图弄清楚如何去做。这是我到目前为止所拥有的;

#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/extensions/XInput2.h>
#include <signal.h>
#include <assert.h>

int working = 1;

void signal_callback_handler(int signum) {
    working = 0;
}

int main() {


    signal(SIGINT, signal_callback_handler);
    signal(SIGTSTP, signal_callback_handler);
    signal(SIGTERM, signal_callback_handler);

    /* Connect to the X server */
    Display *dpy = XOpenDisplay(NULL);
    assert(dpy);

    /* XInput Extension available? */
    int opcode, event, error;
    if (!XQueryExtension(dpy, "XInputExtension", &opcode, &event, &error)) {
        printf("X Input extension not available.\n");
        return -1;
    }

    /* Which version of XI2? We support 2.0 */
    int major = 2, minor = 0;
    if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
        printf("XI2 not available. Server supports %d.%d\n", major, minor);
        return -1;
    }


    XIEventMask eventmask;

    unsigned char mask[1] = { 0 }; /* the actual mask */

    eventmask.deviceid = 2;
    eventmask.mask_len = sizeof(mask); /* always in bytes */
    eventmask.mask = mask;

    /* now set the mask */
    XISetMask(mask, XI_RawButtonPress);

    /* select on the window */
    XISelectEvents(dpy, DefaultRootWindow(dpy), &eventmask, 1);

    while(working) {
        XEvent ev;
        XNextEvent(dpy, &ev);
        if (ev.xcookie.type == GenericEvent &&
                ev.xcookie.extension == opcode &&
                XGetEventData(dpy, &ev.xcookie))
        {
            switch(ev.xcookie.evtype)
            {
                case XI_RawButtonPress:
                    printf("RawButtonPress");
                    break;
            }
        }
        XFreeEventData(dpy, &ev.xcookie);
    }
}

但是,我收到此错误;

X Error of failed request:  XI_BadDevice (invalid Device parameter)
  Major opcode of failed request:  131 (XInputExtension)
  Minor opcode of failed request:  46 ()
  Device id in failed request: 0xad
  Serial number of failed request:  15
  Current serial number in output stream:  15

更新 2

我曾尝试使用 ButtonRelaseEvent 来执行此操作,但我没有收到任何事件。XNextEvent 永远阻塞,无论我在哪里单击/释放按钮。这是代码;

#include <stdio.h>
#include <stddef.h>
#include <X11/Xlib.h>
#include <assert.h>
#include <unistd.h>
#include <signal.h>


int working = 1;

void signal_callback_handler(int signum) {
    working = 0;
}

int main () {
    signal(SIGINT, signal_callback_handler);
    signal(SIGTSTP, signal_callback_handler);
    signal(SIGTERM, signal_callback_handler);

    Display *d = XOpenDisplay(NULL);
    assert(d);

    XSelectInput(d,DefaultRootWindow(d), ButtonReleaseMask);

    while(working) {
        XEvent e;
        XNextEvent(d, &e);
    printf("Something Occured");
        if (e.type == ButtonRelease) {
            printf("%dx%d",e.xbutton.x,e.xbutton.y);
        }
    }


    return 0;
}
4

2 回答 2

2

尝试XWindowEvent代替XNextEvent.

例如要抓取鼠标,您可以这样做:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>

int main(){
    Display* display;
    int screen_num;
    Screen *screen;
    Window root_win;
    XEvent report;
    XButtonEvent *xb = (XButtonEvent *)&report;
    int i;
    Cursor cursor;
    display = XOpenDisplay(0);
    if (display == NULL){
        perror("Cannot connect to X server");
        exit (-1);
    }
    screen_num = DefaultScreen(display);
    screen = XScreenOfDisplay(display, screen_num);
    root_win = RootWindow(display, XScreenNumberOfScreen(screen));
    cursor = XCreateFontCursor(display, XC_crosshair);
    i = XGrabPointer(display, root_win, False,
                ButtonReleaseMask | ButtonPressMask|Button1MotionMask, GrabModeSync,
                GrabModeAsync, root_win, cursor, CurrentTime);
    if(i != GrabSuccess){
        perror("Can't grab the mouse");
        exit(-1);
    }
    for(i = 0; i < 10; i++){
        XAllowEvents(display, SyncPointer, CurrentTime);
        XWindowEvent(display, root_win, ButtonPressMask | ButtonReleaseMask, &report);
        switch(report.type){
            case ButtonPress:
                printf("Press @ (%d, %d)\n", xb->x_root, xb->y_root);
            break;
            case ButtonRelease:
                printf("Release @ (%d, %d)\n", xb->x_root, xb->y_root);
            break;
        }
    }
    XFlush(display);
    XUngrabServer(display);
    XCloseDisplay( display );
    return 0;
}
于 2013-01-28T12:30:11.513 回答
1

是的,来自 x11 协议规范:

多个客户端可以在同一个窗口中选择输入;他们的事件掩码是不相交的。当一个事件产生时,它会报告给所有感兴趣的客户。但是,SubstructureRedirect 一次只能选择一个客户端,ResizeRedirect 一次只能选择一个客户端,ButtonPress 一次只能选择一个客户端。尝试违反这些限制会导致访问错误。

但是,允许多个客户端选择 ButtonRelease 事件 - 我刚刚检查了两个客户端并且都接收事件。

于 2013-01-29T11:49:10.467 回答