maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Using zoom-in and zoom-out keys in Python (https://talk.maemo.org/showthread.php?t=45093)

dov 2010-02-19 11:12

Using zoom-in and zoom-out keys in Python
 
I'm writing a small html text reader in python and I would like to use the volume keys for doing page up and page down. To do that I need to release them which according to:

http://wiki.maemo.org/Documentation/...Qt_application

is done by calling Xchangeproperty.

Since I am using python, hildon, and gtk I wonder whether this function has a python binding, or if there is a higher level python interface available for releasing these buttons?

Regards,
Dov

dov 2010-02-28 18:16

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)



All times are GMT. The time now is 12:14.

vBulletin® Version 3.8.8