如何从X服务器读取鼠标单击事件

时间:2013-01-28 11:42:16

标签: c mouseevent xlib

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

#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;
}

2 个答案:

答案 0 :(得分:1)

是的,来自x11协议规范:

  

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

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

答案 1 :(得分:0)

尝试使用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;
}