Xlib XSendEvent click event do not work inside of some windows on Ubuntu 12.04
Asked Answered
D

1

1

i'm trying to send mouse click event using xlib in a ubuntu 12.04, all works when i do the click in the desktop bar icons and works when i do click in the title bar of each window (close, minimize, maximize window) but in some windows doing a click inside do not work, only work in my qt creator window but when i click in, for example, Home folder icon then move the mouse inside the folder, i can't do any click in folders or menu bar, only works in the title bar of the windows.

Maybe is a bug of Ubuntu Unity desktop? here is my code that i find on internet:

#include <unistd.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

void mouseClick(int button)
{
    Display *display = XOpenDisplay(NULL);

    XEvent event;

    if(display == NULL)
    {
        fprintf(stderr, "Errore nell'apertura del Display !!!\n");
        exit(EXIT_FAILURE);
    }

    memset(&event, 0x00, sizeof(event));

    event.type = ButtonPress;
    event.xbutton.button = button;
    event.xbutton.same_screen = True;

    XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);

    event.xbutton.subwindow = event.xbutton.window;

    while(event.xbutton.subwindow)
    {
        event.xbutton.window = event.xbutton.subwindow;

        XQueryPointer(display, event.xbutton.window, &event.xbutton.root, &event.xbutton.subwindow, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
    }

    if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Errore nell'invio dell'evento !!!\n");

    XFlush(display);

    usleep(100000);

    event.type = ButtonRelease;
    event.xbutton.state = 0x100;

    if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Errore nell'invio dell'evento !!!\n");

    XFlush(display);

    XCloseDisplay(display);
}

My ubuntu is inside of a vmware player, i move the cursor using XWarpPointer of Xlib, thank you for any help.

Daddylonglegs answered 1/2, 2013 at 23:8 Comment(2)
I have read that many (possibly most?) programs ignore events from XSendEvent. Here is a possible workaround: semicomplete.com/blog/geekery/…Biocellate
thank you, this should will be useful when i finds that problem, but in my case i am trying to do a click in a simple folder window and does not work, i tried the same using xdotools and works then the XSendEvent is working, but xdotools is doing something more, what can be?Daddylonglegs
R
1

I had the same problem and I just solved it using Xext and Xtest extensions.

#include <X11/extensions/XTest.h>

int main(int argc, char ** argv)
{
    XEvent event;
    Display *dpy = XOpenDisplay (NULL);

    /* Fake the mouse button Press and Release events */
    XTestFakeButtonEvent (dpy, 1, True,  CurrentTime);
    XTestFakeButtonEvent (dpy, 1, False, CurrentTime);
    XCloseDisplay (dpy);
    return 0;
}
Ryurik answered 18/3, 2017 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.