Friday, December 21, 2007

Emulating a click event on XWindow

A couple of weeks ago I found this old peace of code to emulate a mouse click button event on XWindows systems.
If somebody has programmed with IDL or executed a distributed program with its virtual machine, you'll have noticed that you must click obligatory in the eternal IDL splash srceen.
Well, this code makes this for you. You can create an script that launch your IDL program and a few second later execute this little program.


/path_to_idl/idl -vm=you_program.sav
sleep 3
xboton


I want to point that the next code has HTMLified thanks to C++2HTML:


/* Name: xboton
* Author: Antonio Santiago [asantiagop(at)gmail(dot)com]
* Description: xboton emulates a click with a mouse pointer in the middle
* of screen.
* Compile: gcc -o xboton xboton.c -lX11 -L/usr/X11R6/lib
*/


#include <stdio.h>
#include <string.h>

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

int
main() {

Display *display;

Window root, win, subwin;
XEvent event;
XWindowAttributes attrib;
unsigned int
width, height;


/* Open a display connection and get root window size. */

display = XOpenDisplay(NULL);
root = RootWindow(display, DefaultScreen(display));

if
( XGetWindowAttributes(display, root, &attrib) == 0 )

printf("Error: Can't retrieve root window size\n");

width = attrib.width;
height = attrib.height;


/* Move mouse pointer to middle if screen and get the ID of
* the window at this point.
*/

XWarpPointer(display, None, root, 0,0,0,0, width/2, height/2);

bzero(&event,sizeof(XEvent));

event.type = ButtonPress;

event.xbutton.button = Button1;
event.xbutton.same_screen = True;


XQueryPointer(display, root,
&
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;
event.xbutton.state = Button1Mask;



/* Walk down through window hierachy to find youngest child */

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


/* Send a ButtonPress and ButtonRelease (of button 1) to the middle screen window */

if
( XSendEvent(display, event.xbutton.subwindow, True, ButtonPressMask, &event) == 0)

printf("Error: XSendEvent error on ButtonPress event\n");
XFlush(display);

event.type = ButtonRelease;

event.xbutton.state = Button1Mask;
if
( XSendEvent(display, event.xbutton.subwindow, True, ButtonPressMask, &event) == 0)

printf("Error: XSendEvent error on ButtonRelease event\n");
XFlush(display);
}




Be happy don't worry.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.