You are not logged in.
My problem is very simple. I've got to write a python application that can do some crap based on the location of the cursor on the screen. So... how do I find the location of the cursor on the screen? I can use a gtk app, but I'm guessing that the app would require focus, which can't happen. I'm actually hoping to make it a terminal app... hm, something like xkill, actually. ;-) So I'm thinking maybe I need a simple python-xlib call or something like that. Any ideas?
Dusty
Offline
maybe this helps?
XQueryPointer
Offline
Hmm.... I don't seem to find that function in python-xlib, but it seems to have *something* in x-python... whatever the difference is, neither appears to be much maintained now-adays. :-/ Might have to write a C app.
Dusty
Offline
Write a C module using XQueryPointer... it'd probably be like 20-50 LOC max.
Then you can:
import xmouse
x,y = xmouse.position()
Offline
Mr Green
Offline
Here, I got bored: setup.py and xmouse.c follow.
xmouse.position() returns a tuple of (xposition,yposition,window id)
from distutils.core import setup
from distutils.extension import Extension
setup( name = "xmouse",
version = "0.1",
ext_modules=[Extension(name = 'xmouse',
sources = ['xmouse.c'],
libraries = ['X11'])]
)
#include <Python.h>
#include <X11/Xlib.h>
PyObject* xmouse_position(PyObject* self, PyObject* args)
{
Display* dpy;
int screennum = 0;
char* display;
Window rootwin, childwin;
int root_x, root_y;
int child_x, child_y;
unsigned int mask;
PyObject* ret = NULL;
PyArg_ParseTuple(args, "|zi", &display, &screennum);
dpy = XOpenDisplay(display);
if(!dpy)
{
/* TODO is this right?? */
PyErr_SetString(PyExc_Exception, "cannot open display");
return NULL;
}
if(XQueryPointer(dpy, RootWindow(dpy,0), &rootwin, &childwin,
&root_x, &root_y, &child_x, &child_y, &mask) == True)
ret = Py_BuildValue("(i,i,i)", child_x, child_y, (long)childwin);
else
ret = Py_BuildValue("(i,i,i)", root_x, root_y, (long)rootwin);
XCloseDisplay(dpy);
return ret;
}
PyMethodDef methods[] =
{
{"position", xmouse_position, METH_VARARGS},
};
void initxmouse(void)
{
Py_InitModule("xmouse", methods);
}
Offline
Wow, so that's how a C--> Python module looks...
Its Brillant!!
I'll give it a go, I guess, though truthfully I should probably convert the whole works to C anyway.
Thanks phrakture. You are very wise.
Dusty
Offline
WTF?:
gcc -pthread -shared build/temp.linux-i686-2.4/xmouse.o -lX11 -o build/lib.linux-i686-2.4/xmouse.so
/usr/bin/ld: cannot find -lX11
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
/usr/X11R6/lib is in ld.so.conf... I don't get it. How can this error possibly be????
Dusty
Offline
Add a -L/usr/X11R6/lib to your compile line. I think that -lX11 should be at the end of the line.
I *think* ld.so.conf is not used when compiling stuff.
Offline
yeah, that's what I thought, *but* the compile line is created inside setup.py. I'll take a look at python docs, there must be some way to include a directory....
edit:
library_dirs=['/usr/X11R6/lib'],
works like a charm.
I HATE WORKING WITH C APPS. NOTHING EVER WORKS. I REALLY NEED TO FIGURE ALL THOSE COMPILER OPTIONS OUT...... BAH!
*calms down*
Dusty
Offline
Oh yeah, it built fine here because I was using xorg7, heh
Offline
Haha. Its working for me, thanks a bunch phrak.
Dusty
Offline
For the record, I have no idea if that exception handling is correct... I didn't look to far into it, but you can test it by running outside of X.
Also, screennum should go into RootWindow, like so:
RootWindow(dpy,screennum)
Offline