Re: Using zoom-in and zoom-out keys in Python
Since I got no answer, I wrote a python module in C that does the necessary glue. I'm including it verbatim here in case it is useful for someone else:
hardkeys.c:
Code:
//======================================================================
// hardkeys.c - A module for disabling maemo hardware keys.
//
// Dov Grobgeld <dov.grobgeld@gmail.com>
// Sun Feb 28 12:11:51 2010
//----------------------------------------------------------------------
#include <Python.h>
#include <gdk/gdkx.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <pygobject.h>
#include <pygtk/pygtk.h>
static PyTypeObject *PyGObject_Type=NULL;
static PyObject *
hardkeys_grab_zoom_keys(PyObject *self, PyObject *args);
static PyMethodDef HardKeyMethods[] = {
{"grab_zoom_keys", hardkeys_grab_zoom_keys, METH_VARARGS,
"Grab zoom keys."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC
inithardkeys(void)
{
Py_InitModule("hardkeys", HardKeyMethods);
PyObject *module;
init_pygobject();
init_pygtk();
module = PyImport_ImportModule("gobject");
if (module) {
PyGObject_Type =
(PyTypeObject*)PyObject_GetAttrString(module, "GObject");
Py_DECREF(module);
}
}
static PyObject *
hardkeys_grab_zoom_keys(PyObject *self, PyObject *args)
{
PyGObject *py_widget;
GtkWidget *widget;
int grab;
if (!PyArg_ParseTuple(args,
"O!i", PyGObject_Type, &py_widget,
&grab))
return NULL;
widget = GTK_WIDGET(py_widget->obj);
Display *dpy = gdk_x11_drawable_get_xdisplay(widget->window);
Window win = GDK_WINDOW_XID(widget->window);
Atom atom = XInternAtom( dpy, "_HILDON_ZOOM_KEY_ATOM", 0);
unsigned long val = grab?1:0;
XChangeProperty (dpy, win,
XInternAtom( dpy, "_HILDON_ZOOM_KEY_ATOM", 0),
XA_INTEGER, 32,
PropModeReplace, (unsigned char *) &val,
1);
return Py_None;
}
setup.py:
Code:
from distutils.core import setup, Extension, os
class PkgConfig(object):
def __init__(self, names):
def stripfirsttwo(string):
return string[2:]
self.libs = map(stripfirsttwo, os.popen("pkg-config --libs-only-l %s" % names).read().split())
self.libdirs = map(stripfirsttwo, os.popen("pkg-config --libs-only-L %s" % names).read().split())
self.incdirs = map(stripfirsttwo, os.popen("pkg-config --cflags-only-I %s" % names).read().split())
flags = PkgConfig("gtk+-2.0 pygtk-2.0")
module1 = Extension('hardkeys',
sources = ['hardkeys.c'],
include_dirs = flags.incdirs + ['.'],
libraries = flags.libs,
library_dirs = flags.libdirs,
runtime_library_dirs = flags.libdirs
)
setup (name = 'HardKeys',
version = '1.0',
description = 'A package for disabling hardware keys',
ext_modules = [module1],
)
usage example test:
Code:
import hardkeys
w = gtk.Window(gtk.WINDOW_TOPLEVEL)
:
w.show_all()
hardkeys.grab_zoom_keys(w, True)
|