Estou tentando simular um click do mouse usando a biblioteca X11 no linux.
Usando o comando xev
no terminal tenho as seguintes saidas.
Para um click real:
ButtonPress event, serial 32, synthetic NO, window 0x2c00001,
root 0xdb, subw 0x0, time 1776350, (302,342), root:(306,365),
state 0x10, button 1, same_screen YES
ButtonRelease event, serial 32, synthetic NO, window 0x2c00001,
root 0xdb, subw 0x0, time 1776470, (302,342), root:(306,365),
state 0x110, button 1, same_screen YES
Para o click simulado:
ButtonPress event, serial 32, synthetic YES, window 0x2c00001,
root 0xdb, subw 0x0, time 0, (302,342), root:(306,365),
state 0x10, button 1, same_screen YES
ButtonRelease event, serial 32, synthetic YES, window 0x2c00001,
root 0xdb, subw 0x0, time 0, (302,342), root:(306,365),
state 0x10, button 1, same_screen YES
O problema é que o click não acontece, o evento aparece mas o mouse não clica. Estou deixando o código abaixo, realmente não acho onde pode ser o problema.
void MouseClick(int button)
{
Display *display = XOpenDisplay(NULL);
// Create and setting up the event
XEvent event;
memset(&event, 0, sizeof(event));
event.xbutton.button = button;
event.xbutton.same_screen = True;
event.xbutton.subwindow = DefaultRootWindow(display);
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);
}
// Press
event.type = ButtonPress;
if (XSendEvent(display, PointerWindow, True, ButtonPressMask, &event) == 0)
fprintf(stderr, "Error to send the event!\n");
XFlush(display);
usleep(1);
// Release
event.type = ButtonRelease;
if (XSendEvent(display, PointerWindow, True, ButtonReleaseMask, &event) == 0)
fprintf(stderr, "Error to send the event!\n");
XFlush(display);
usleep(1);
XCloseDisplay(display);
}