Archive for the ‘Code’ Category

July 4th, 2007 @ 20:29

One of the things that are particularly boring when working on a Compiz plugin is that you have to reload it all the time, and there is no quick way to do it (I usually unload/reload it through ccsm, which involves some mouse deplacement and needs a ccsm window, or or I just reload compiz, which takes quite a bit of time).

Consequently I thought I could write a little script that would reload one or more plugins for me quickly. It took less than 5 minutes with the neat libcompizconfig python bindings, and works pretty well as far as I can see. (so yes, it depends on libcompizconfig, but heh, that’s worth it)

Here is the code :

#!/usr/bin/env python
 
'''Compiz plugin reloader (through compizconfig)
Copyright (c) 2007 Guillaume Seguin <guillaume@segu.in>
Licensed under GNU GPLv2'''
 
import compizconfig
from sys import argv, exit
from time import sleep
 
if __name__ == "__main__":
    if len (argv) < 2:
        print "Usage : %s plugin1 [plugin2 ... pluginN]" % argv[0]
        exit (2)
    plugins = argv[1:]
    context = compizconfig.Context (basic_metadata = True)
    print "Unloading " + " ".join (plugins)
    for plugin in plugins:
        if plugin not in context.Plugins:
            print "Warning : %s plugin not found" % plugin
            plugins.remove (plugin)
            continue
        context.Plugins[plugin].Enabled = False
    if len (plugins) == 0:
        print "Error : no plugin found"
        exit (1)
    context.Write ()
    print "Waiting for settings update"
    sleep (2)
    print "Loading " + " ".join (plugins)
    for plugin in plugins:
        context.Plugins[plugin].Enabled = True
    context.Write ()
July 2nd, 2007 @ 01:36

After yesterday’s rant, I wrote my very own Hue Picker in pyGTK. It is meant to be very simple and straight forward : it just exposes a “changed” signal and two useful functions (get_hue / set_hue).

The usual preview :
Hue Picker Preview
(the “Source hue : [23 ^]” is just a placeholder and isn’t part of the widget)

Full source included below.
The ring drawing code is a mostly direct python port of C GtkHSV’s one.

(more…)

July 1st, 2007 @ 15:11

pyGTK seriously lacks a good and simple color picker!
The current color picking widget, gtk.ColorSelection, is just way too huge and complicated : it provides several numeric entries for finer selection of HSV or RGB values, or can feature a customizable palette, but I don’t need all that (indeed I just need a Hue picker)
The worst thing is that it looks like there’s already a much simpler widget, which is available in C gtk+ (namely GtkHSV) and that it even has some form of bindings in pyGTK (type (gtk.ColorSelection ().get_children ()[0].get_children ()[0].get_children ()[0]) returns <class ‘__main__.gtk.HSV’>) but it doesn’t expose any of the interesting functions (such as get_color)
Anyway, I guess it’s about time to write another custom widget.

June 18th, 2007 @ 22:03

As part of my courses, I had to learn camllight programming this year. Sadly, all my school’s computers are powered by Windows, a non free closed source operating system, and the default frontend is the less usable thing around : copy pasting is completely broken, dumb default configuration, oldish user interface, no syntax highligting… The worst thing is that the sources aren’t published, I was just able to get those of the Objective Camlui frontend (which is quite the same, but in a much better fashion).

I know that there are already several other ways to have better “frontends”, such as cmdcaml, or using XEmacs with the tuareg mode, but heh, I was willing to provide something all-in-one that looks good. And I needed a good excuse to learn Python and PyGTK.

After a huge rewrite, exploding the original 1500 lines long source, here is a publishable version, embedding the camllight interpreter and using gtksourceview2 for syntax highlighting.

The usual preview :
CamlUI preview (thumbnail)

Feisty debs are published at http://guillaume.segu.in/code/debs/camlui/, with a helper script which fetches the 4 required debs (camlui + gtksourceview2(-common) + python-gtksourceview2) and installs them.
Source is versionned in a git repo at http://guillaume.segu.in/code/camlui.git/

Feedback welcome :)

June 16th, 2007 @ 22:04

As part of a still unpublished (or unadvertised, whatever) project, I was looking for how to do a simple window on top of the screen that would not steal focus but would still be able to receive keyboard events. Furthermore I was also looking for how to easily make this window partly transparent. It’s just all simple code-wise, but when you have no clue on how to do it, it’s not that easy.

I learnt that popup windows aren’t stealing focus, that I could grab the keyboard only (but that I can not grab it while a key is pressed because there is already another active grab then), and that using Composite extension was just about setting the right colormap and using Cairo to draw on the window surface by hooking in the expose events.

Sadly I also learnt that it wasn’t currently possible to draw transparent widgets since they own their region so that what’s under it (in the same window) can not be used for compositing (which results into an awful dark background behind the widget). The only workaround I found was drawing the widget on its own Cairo surface and draw that surface at the right place after painting the window background.

drawing sample - thumbnail

I have included the full sample code featuring all this but the workaround for transparent widgets.

(more…)

June 15th, 2007 @ 22:10

I’ve been a huge fan of the Screenshot with Style GIMP script for a while. The result always looks cool and pretty. Nevertheless, opening the gimp, resizing the picture, finding the script through menus and applying it takes a while (at least a minute or two).
Consequently, I had a bit of fun with Cairo today, and wrote a simple but nice python script that does almost the same in a single command :)
It’s not as pretty as with the GIMP, but it’s good enough for now (I could probably improve it, but I’m a bit lazy)

Quick example (using the screenshot from the previous article) :
screenshot-beautifier sample

If you fancy trying it, just grab the code from git :)
$ git clone http://guillaume.segu.in/code/screenshot-beautifier.git/
$ cd screenshot-beautifier
$ python beautifier.py [source file] [result file]

Have fun :)

June 13th, 2007 @ 01:29

Meh, I’ve been way too busy these days with school. Happily I should be mostly done with this year in a bit more than 40 hours among which 7 hours of examinations.

Anyway, I was able to pack together a simple but nice shader editor. It currently only supports fragment programs, but it works well. It’s built around pygtkglext and python-opengl, and makes use of ctypes to bind all the fragment programs related functions. Supporting vertex programs shouldn’t be too hard, actually most of what’s needed for it is already in place, it just needs some GUIfication.
All the required dependencies are in Gutsy’s Universe (pygtkglext wasn’t in Feisty’s).

Here’s a screenshot (click on the thumbnail to view the full resolution picture) :
gShaderEdit Preview

Fresh-from-git source can be found at http://guillaume.segu.in/code/gshaderedit.git/ :
$ git clone http://guillaume.segu.in/code/gshaderedit.git/
$ cd gshaderedit
$ python gshaderedit.py

Should you find any bug, please report it to me by mail (you can find my address in the AUTHORS file).