X gurus? [OFFTOPIC]
cgw at alum.mit.edu
cgw at alum.mit.edu
Thu Mar 2 00:54:22 EST 2000
In article <slrn8bp5kk.ig.msabin at localhost.localdomain>, msabin at cdc.net (Mike Sabin) wrote:
> I am programming for the X Windows environment
> on Linux, and need to accomplish the following two
> things:
>
> 1. At a certain time, I need to get the x-y coordinates
> of my mouse pointer, no matter where my pointer is at
> the time (it could be over the window for some other
> application, but I want a function call to return
> the coordinates to my application)
>
> 2. At a certain time, I need to artificially generate
> a button 1 mouse click, whereever the mouse pointer is
> currently located (e.g. over another application's
> window). My program must do this, without me actually
> having to click on the mouse or hit any key.
>
> Can it be done in python?
As far as I know, it can't be done with code in the standard Python distribution. And, since
Sjoerd Mullender's X module does not have support for XTEST, that won't help you either,
at least not without some modifications.
However, I just happen to have some simple C programs which do exactly what you want,
and you can call these from a Python script via "os.popen" or the like. These could easily
be modified to be a dynamically-loaded Python module, instead.
Here they are. Hope this comes in useful.
File 1: xpos.c. Gets the current x-y position of the pointer.
Compile with "gcc -o xpos xpos.c -I/usr/X11R6/include -L/usr/X11R6/lib -lX11"
Usage: "xpos", no arguments, prints mouse pointer position on stdout
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdlib.h>
#include <stdio.h>
int
main(int argc,char **argv)
{
Display *display;
Window root, root_return, child_return;
char *display_name;
int screen;
int root_x_return, root_y_return;
int win_x_return, win_y_return;
unsigned int mask_return;
if (! (display_name = getenv("DISPLAY")) ){
fprintf(stderr,"environment variable DISPLAY must be set\n");
exit(-1);
}
if (! (display = XOpenDisplay(display_name)) ){
fprintf(stderr,"%s: Cannot open display %s\n", argv[0],
display_name);
exit(-1);
}
screen = DefaultScreen(display);
root = RootWindow(display,screen);
XQueryPointer(display, root, &root_return, &child_return,
&root_x_return, &root_y_return,
&win_x_return, &win_y_return,
&mask_return);
printf("%d %d\n",root_x_return,root_y_return);
return 0;
}
File 2: "xmouse.c" Simulates a mouse-button event.
Compile with "gcc -o xmouse xmouse.c -I/usr/X11R6/include -L/usr/X11R6/lib -lX11 -lXtst"
Usage: xmouse whichbutton u|d (which button is 1,2, or 3, u for button-up, d for button down. A "click" is a button down followed by a button up)
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/XTest.h>
#include <stdlib.h>
#include <stdio.h>
int
main(int argc,char **argv)
{
Display *display;
Window root;
char *display_name;
Status status;
int screen;
int but;
int event_base,error_base,major_version,minor_version;
int verbose=0;
int press=0;
int release = 0;
if (argc<3){
Usage: fprintf(stderr,"Usage: %s but u|d\n",argv[0]);
exit(-1);
}
but=atoi(argv[1]);
if (argv[2][0]=='u')
release = 1;
else if (argv[2][0]=='d')
press = 1;
else
goto Usage;
if (! (display_name = getenv("DISPLAY")) ){
fprintf(stderr,"environment variable DISPLAY must be set\n");
exit(-1);
}
if (! (display = XOpenDisplay(display_name)) ){
fprintf(stderr,"%s: Cannot open display %s\n",
argv[0], display_name);
exit(-1);
}
screen = DefaultScreen(display);
root = RootWindow(display,screen);
status = XTestQueryExtension(display,&event_base,&error_base,
&major_version,&minor_version);
if (status){
if (verbose)
printf("display %s supports XTEST %d.%d\n",display_name,
major_version,minor_version);
} else {
printf("display %s does not support XTEST\n",display_name);
exit(-1);
}
XTestFakeButtonEvent(display,but,press,10);
XFlush(display);
return 0;
}
More information about the Python-list
mailing list