You are not logged in.

#1 2006-02-04 18:07:33

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Quck, where's the cursor? Can Python tell us?

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

#2 2006-02-04 18:39:04

cmp
Member
Registered: 2005-01-03
Posts: 350

Re: Quck, where's the cursor? Can Python tell us?

maybe this helps?
XQueryPointer

Offline

#3 2006-02-04 21:16:52

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Quck, where's the cursor? Can Python tell us?

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

#4 2006-02-04 23:32:03

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Quck, where's the cursor? Can Python tell us?

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

#5 2006-02-05 00:04:06

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Quck, where's the cursor? Can Python tell us?


Mr Green

Offline

#6 2006-02-05 00:41:17

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Quck, where's the cursor? Can Python tell us?

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

#7 2006-02-05 02:14:49

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Quck, where's the cursor? Can Python tell us?

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

#8 2006-02-05 19:30:44

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Quck, where's the cursor? Can Python tell us?

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

#9 2006-02-05 20:42:10

Snowman
Developer/Forum Fellow
From: Montreal, Canada
Registered: 2004-08-20
Posts: 5,212

Re: Quck, where's the cursor? Can Python tell us?

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

#10 2006-02-05 20:46:12

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Quck, where's the cursor? Can Python tell us?

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

#11 2006-02-07 02:53:32

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Quck, where's the cursor? Can Python tell us?

Oh yeah, it built fine here because I was using xorg7, heh

Offline

#12 2006-02-07 03:59:16

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Quck, where's the cursor? Can Python tell us?

Haha. Its working for me, thanks a bunch phrak. smile

Dusty

Offline

#13 2006-02-07 20:22:44

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Quck, where's the cursor? Can Python tell us?

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

Board footer

Powered by FluxBB