0

我目前正在编写一个应用程序,它需要监视 Linux 中所有现有窗口上的鼠标指针移动。我正在使用带有 gnome 的 Ubuntu 11.10。我需要的是在屏幕上的任何位置获取有关鼠标指针移动的信息。这是执行相同操作的现有代码:

void *MonitorOffline(void *threaddata)
{
    time_t      sTime, cTime;
    DISPLAY     *dsp = NULL;
    int         iError = 0;

    sTime = time(NULL);

    XSetErrorHandler(_invalid_window_handler);
    while (1) {
        XEvent event;
        cTime = time(NULL);
        if ((cTime - sTime) > OFFLINETIME) {
            log_msg("User %s is offline", cuserid(NULL));
            sTime = cTime;
        }
        iError = RegisterWinEvents(&dsp);
        if (iError) {
            log_quit("%s:%d : Error in RegisterWinEvents", __FUNCTION__,
                    __LINE__);
            break;
        }
        XNextEvent(dsp, &event);
        switch(event.type) {
            case KeyPress:
                printf("KeyPress Encountered\n");
                break;
                printf("KeyRelease Encountered\n");

                break;

            case ButtonPress:

                printf("ButtonPress Encountered\n");

                break;

            case ButtonRelease:

                printf("ButtonRelease Encountered\n");

                break;

            case MotionNotify:

                printf("MotionNotify Encountered\n");

                break;

            case EnterNotify:

                printf("EnterNotify Encountered\n");

                break;
            case LeaveNotify:

                printf("LeaveNotify Encountered\n");

                break;

        }
        XCloseDisplay (dsp);
        fflush(stdout);
    }
}

int RegisterWinEvents(DISPLAY   **dsp)
{
    Window window_id;
    char *win_name;
    int iError = 0;
    XSetWindowAttributes attr;
    XWindowAttributes wattr;
    Window root_return, parent_return;
    Window root;
    Window client;
    Window *children_return = NULL;
    unsigned int num_children = 0;
    Status status;
    int i;
    time_t t;

    iError = WDGetRoot(&root, dsp);

    if (iError == -1) {

        return -1;
    }
    status = XQueryTree (*dsp, root, &root_return,
            &parent_return, &children_return,
            &num_children);
    for(i = 0; i < num_children; i++)
    {
        attr.event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask |
            ButtonReleaseMask | EnterWindowMask |
            LeaveWindowMask | PointerMotionMask |
            Button1MotionMask |
            Button2MotionMask | Button3MotionMask |
            Button4MotionMask | Button5MotionMask |
            ButtonMotionMask | KeymapStateMask |
            ExposureMask | VisibilityChangeMask |
            StructureNotifyMask | /* ResizeRedirectMask | */
            SubstructureNotifyMask | SubstructureRedirectMask |
            FocusChangeMask | PropertyChangeMask |
            ColormapChangeMask;// | OwnerGrabButtonMask;

        status = XGetWindowAttributes(*dsp, children_return[i], &wattr);
        if (wattr.all_event_masks & ButtonPressMask)
            attr.event_mask &= ~ButtonPressMask;
        attr.event_mask &= ~SubstructureRedirectMask;
        XSelectInput(*dsp, children_return[i], attr.event_mask);
    }
    XFree(children_return);
    return 0;
}

WINDOW WDGetRootWindow(DISPLAY* pDisplay, INT iScreen)
{
    return RootWindow(pDisplay,iScreen);
}

int WDGetRoot(Window *root, DISPLAY **pDisplay)
{
    INT iScreen = 0;
    setlocale(LC_CTYPE, "");

    //Initialize the Display
    if((*pDisplay = WDOpenDisplay(NULL))) {
        //Get the sceen associated with Display
        iScreen = WDGetScreenOfDisplay(*pDisplay);
        //Once we have the screen , we need to get the rootwindow associated
        //with the screen so that we can traverse the window tree and get the
        //window with current focus (the active window)
        *root = WDGetRootWindow(*pDisplay, iScreen);
    } else {
        return -1;
    }
    return 0;
}

使用上面的代码,我能够捕捉到所有窗口标题栏上的鼠标指针移动,但是当鼠标指针位于窗口内时(例如办公室作家的文本部分等),我无法捕捉到任何移动。我还需要在代码中添加什么才能让它在整个窗口中捕获鼠标移动?

4

1 回答 1

0

XQueiryPointer声称能够返回窗口中的坐标。

XQueryPointer 函数返回...相对于根窗口原点的指针坐标。

尝试结合您对 XQueryTree 的调用

于 2012-06-07T13:07:51.677 回答