如何使用Xlib监视Linux中现有窗口上的鼠标指针移动

时间:2012-06-07 12:36:56

标签: c xlib

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

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

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

1 个答案:

答案 0 :(得分:0)

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

  

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

尝试结合您对XQueryTree的调用