August 1st, 2007 @ 13:10

Worse and worse… 12 days and no news. I hate holidays, heh. Nevertheless, I have written more than 2500 lines during these 12 days. Let’s have a quick overview over all this code ;)

What’s new?
Two Compiz plugins!
The first one is something that I missed from Beryl, bottom cube caps. The new “cubecaps” plugin supports top and bottom caps, correctly blends caps color with the images (e.g. if you select a black cap color, the result won’t be completely black whatever the image is as with the previous “cube” plugin caps, but will be your image on a black background), and supports caps when there are more than 4 viewports (actually one could also enable them for 3 viewports by changing a single integer in the source file (currently at line 288 of cubecaps.c at commit 6d463e3), but it might look wrong if image is too big). Finally, clamping is done using clamp_to_border extension if available, which avoids weird lines when using non scaled images with alpha.

I’ll add some screenshots when I’ll be able to upload them… I am currently using my cellphone to post this, sadly.

The second one is a lot more specific : it displays some visual feedback of what you are doing with your mouse and your keyboards. More specifically, when you hit one of your mouse buttons it displays (when enabled) a little mouse in the bottom right of the screen with the button you hit highlighted, until you release it. Likewise, when you press a modifier key (Alt, Shift…) it displays a nice rounded rectangle in which the name of the modifier is written in the bottom left corner of the screen. It’s pretty hard to describe how it actually looks with words, but I hope you get it anyway. The main use case if for showing used key bindings when demonstrating/teaching the usage of an application.

Again, screenshots will come ;)

I began working on a third plugin that would handle metacity -> Compiz -> metacity switch and move windows from the workspace they were on to the corresponding viewport upon startup and do the opposite upon shutdown, but there’s another core problem that blocks it (plugins are initialized after screen and windows were initialized, during which windows are all moved to current workspace).

I also happened to need a quick way to test some Cairo drawing before using it for the final application (which could, for instance, require compiling before testing, which is kinda annoying when you are trying to find right values for a simple drawing). I wrote a “cairoDrawingArea” PyGTK widget which handles the drawing of a Cairo surface (plus a patterned background and/or borders). One just has to write a child of this function and write the draw () method to do the actual Cairo drawing. I included the whole thing at the end of this post. I know I could/should have just used the write_to_png function on the Cairo surface, but dealing with nautilus/eog is just so unhumane and slow, while I needed (again) a fast way to view the result of my changes.

Most of the code for this script comes from a quick PyGTK Tic Tac Toe game I wrote to show a friend how one could write a fully working “software” in less than 2 hours. By the way, I am wondering if it would be possible to write an AI that would have no clue on the rules of a simple game such as Tic Tac Toe, and learn them round after round.


Last but not least, I fixed some bugs and mem leaks in my colorfilter plugin and cleaned the code a lot. There is now only one remaining thing to do for this plugin before it gets considered as done : fragment program parameters. Shouldn’t be too hard, but I am not sure of the way to cumulate filters using parameters (maybe it is just impossible).

What’s wrong?
I definitely can’t fix Filters Manager problems :/ I tried writing the base rgb -> hsv -> rgb fragment program using Cg, but it resulted in a 120 lines long fragment program, while poor Intel 900 GMA chipsets only support 96. Pretty sad, heh. The other problems (randomly borked cumulative filters preview and a strange OpenGL Invalid Operation error on first action on one of the previews when they are both displayed) are still there as well, but I think I could probably workaround them by having just one OpenGL drawing area which would process a queue of drawing operations

What’s next?
I’m quite unsure… I’d like to finish my Summer of Code, but it’s sadly held by the Filters Manager problems and by some bugs in Compiz Core that won’t be fixed before next release (which should hopefully happen pretty soon… I guess I’ll have to wear back my release roller hat for Fusion). I have done a lot of Compiz coding (some patches which I discussed with David and finally got a wider solution, fixing numerous other problems, some other patches that might make it after next release, and the two plugins), and I currently don’t have much ideas on what I could do (even though I would be very happy to find a good reason to learn more about OpenGL ;)). I have some mathematics related things planned as well (involving convex hulls, hypercubes and such), and some web coding which is becoming really high priority. Getting CamlUI ready for Windows and fixing the handling of toplevels must also be done (I would like to write a similar gedit plugin, if I can find some time to do so). And I do need to finish cairo-swissknife. So much things to do and so little time :p

Off topic.
I am looking for a real touch screen bigger than 10″. I would really really like to try to code some touch screen specific apps, but all I found was either fake touchscreens or unavailable laptops (which would actually be better than a simple touchscreen, I don’t really care about money as long as it’s less than 2000€). Hints welcome :)

#!/usr/bin/env python
# coding=utf8
Author : Guillaume "iXce" Seguin
Email  :
 # PyGTK Cairo Drawing Area #
Copyright (C) 2007 Guillaume Seguin
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
import pygtk
pygtk.require ('2.0')
import gtk
import cairo
import pangocairo
from math import pi
class cairoDrawingArea (gtk.DrawingArea):
    '''A very simple gtk.DrawingArea widget using Cairo, childs of this class
just have to play with the Cairo context given to their draw () function.
A white/grey pattern background will be drawn if pattern_background parameter
is True, while black borders will be drawn if borders is True.
The write () function is available to write text on the surface.'''
    _surface        = None
    _pattern_background    = False
    _borders        = False
    def __init__ (self, width = 300, height = 300, pattern_background = False,
          borders = False):
        '''Prepare widget'''
        super (cairoDrawingArea, self).__init__ ()
        self._pattern_background = pattern_background
        self._borders = borders
        self.connect ("expose_event", self.expose)
        if self._borders:
            width += 4
            height += 4
        self.set_size_request (width, height)
    def write (self, cr, text, x = 0, y = 0, font = "Sans 12"):
        '''Write some text on the context. Text must be valid Pango markup,
and font must be a valid Pango font. Current point is not changed by this
        markup = '''<span font_desc="%s">%s</span>''' % (font, text)
        pcr = pangocairo.CairoContext (cr)
        layout = pcr.create_layout ()
        layout.set_markup (markup) ()
        cr.move_to (x, y)
        pcr.show_layout (layout)
        cr.restore ()
    def draw_rounded_rectangle (self, cr, x, y, width, height, radius = 30):
        '''Draw a rounded rectangle path'''
        offset = radius / 3
        x0 = x + offset
        y0 = y + offset
        x1 = x + width - offset
        y1 = y + height - offset
        cr.new_path ()
        cr.arc (x0 + radius, y1 - radius, radius, pi / 2, pi)
        cr.line_to (x0, y0 + radius)
        cr.arc (x0 + radius, y0 + radius, radius, pi, 3 * pi / 2)
        cr.line_to (x1 - radius, y0)
        cr.arc (x1 - radius, y0 + radius, radius, 3 * pi / 2, 2 * pi)
        cr.line_to (x1, y1 - radius)
        cr.arc (x1 - radius, y1 - radius, radius, 0, pi / 2)
        cr.close_path ()
    def draw_background_pattern (self, side):
        '''Draw background pattern Cairo surface'''
        surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, side, side)
        cr = cairo.Context (surface)
        cr.set_source_rgb (0.9, 0.9, 0.9)
        # Top left
        cr.rectangle (0, 0, side / 2, side / 2)
        cr.fill ()
        # Bottom right
        cr.rectangle (side / 2, side / 2, side / 2, side / 2)
        cr.fill ()
        cr.set_source_rgb (0.8, 0.8, 0.8)
        # Top right
        cr.rectangle (side / 2, 0, side / 2, side / 2)
        cr.fill ()
        # Bottom left
        cr.rectangle (0, side / 2, side / 2, side / 2)
        cr.fill ()
        # Prepare pattern
        pattern = cairo.SurfacePattern (surface)
        pattern.set_extend (cairo.EXTEND_REPEAT)
        return pattern
    def draw (self, cr, width, height):
        '''The actual drawing function'''
    def redraw (self, queue = False):
        '''Redraw internal surface'''
        alloc = self.get_allocation ()
        # Prepare drawing surface
        width, height = alloc.width, alloc.height
        if self._borders:
            width -= 4
            height -= 4
            surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, width, height)
            cr = cairo.Context (surface)
            # Draw background
        if self._pattern_background:
            pattern_width = alloc.width / 10 * 2
            pattern = self.draw_background_pattern (pattern_width)
            cr.set_source (pattern)
            cr.set_source_rgb (1, 1, 1)
            cr.paint ()
        # Draw
        self.draw (cr, alloc.width, alloc.height)
        # Now paint that surface to the actual surface if needed
        if self._borders:
            self._surface = cairo.ImageSurface (cairo.FORMAT_ARGB32,
            cr = cairo.Context (self._surface)
            cr.set_source_rgb (0, 0, 0)
            cr.set_line_width (4)
            cr.rectangle (1, 1, alloc.width - 1, alloc.height - 1)
            cr.stroke ()
            cr.set_source_surface (surface, 2, 2)
            cr.paint ()
            self._surface = surface
        # Queue expose event if required
        if queue:
            self.queue_draw ()
    def expose (self, widget, event):
        '''Expose event handler'''
        cr = self.window.cairo_create ()
        if not self._surface:
            self.redraw ()
        cr.set_source_surface (self._surface)
        cr.rectangle (event.area.x, event.area.y,
                      event.area.width, event.area.height)
        cr.clip ()
        cr.paint ()
        return False
class myCairoDrawingArea (cairoDrawingArea):
    '''Example child of cairoDrawingArea class'''
    def draw (self, cr, width, height):
        '''Just derivate this'''
        def fill (preserve = False):
            cr.set_source_rgb (1, 1, 1)
            if preserve: cr.fill_preserve ()
            else: cr.fill ()
        def stroke (preserve = False):
            cr.set_source_rgb (0, 0, 0)
            if preserve: cr.stroke_preserve ()
            else: cr.stroke ()
        def fill_stroke (preserve = False):
            fill (preserve = True)
            stroke (preserve)
        # Drawing a line
        cr.set_source_rgb (0, 0, 1)
        cr.move_to (0, 0)
        cr.line_to (width, height)
        cr.stroke ()
        # Drawing a mouse using cairo - that's a bonus ;)
        x0 = 50  # Left of the mouse
        x1 = 250 # Right of the mouse
        y0 = 110 # Top of the mouse
        y1 = 250 # Middle of the mouse (top of the corpse)
        y2 = 450 # Bottom of the mouse
        mx = (x0 + x1) / 2 # Horizontal middle of the mouse
        my = (y0 + y2) / 2 # Vertical middle of the mouse
        my1 = (y0 + y1) / 2 # Middle of the top section
        my2 = (y1 + y2) / 2 # Middle of the bottom section
        w = x1 - x0 # Width of the mouse
        h = y2 - y0 # Height of the mouse
        # Bottom corpse
        cr.move_to (x0, y1)
        cr.curve_to (x0, my2, x0, y2, mx, y2)
        cr.curve_to (x1, y2, x1, my2, x1, y1)
        fill_stroke ()
        # Left button
        cr.move_to (mx, y1)
        cr.line_to (x0, y1)
        cr.curve_to (x0, my1, x0, y0, mx, y0)
        cr.line_to (mx, y1)
        fill_stroke ()
        # Right button
        cr.move_to (mx, y1)
        cr.line_to (x1, y1)
        cr.curve_to (x1, my1, x1, y0, mx, y0)
        cr.line_to (mx, y1)
        fill_stroke ()
        # Middle button
        mbw = 40           # Middle button width
        mbh = 80           # Middle button height
        mbx = x0 + w / 2 - mbw / 2 # Middle button left position
        mby = y1 - mbh / 4 * 3       # Middle button top position
        self.draw_rounded_rectangle (cr, mbx, mby, mbw, mbh, mbw / 4)
        fill_stroke ()
        # Drawing some text
        cr.set_source_rgb (1, 0, 0)
        self.write (cr, "Sample text", width / 5, height / 1.8, "Sans 22")
        # Drawing a pretty curve
        cr.set_source_rgb (0, 1, 0)
        cr.move_to (10, 10)
        cr.curve_to (100, 5, 240, 200, 280, 20)
        cr.fill_preserve ()
        stroke ()
        # Final fun
        cr.set_source_rgb (0.5, 0.5, 1)
        self.write (cr, "Oh noes, a mouse!", 6, height - 42, "Sans 22")
        cr.set_line_cap (cairo.LINE_CAP_ROUND)
        cr.set_source_rgb (1, 0.4, 1)
        cr.set_line_width (4)
        cr.move_to (50, height - 40)
        cr.line_to (128, height - 118)
        cr.move_to (130, height - 120)
        cr.line_to (130, height - 90)
        cr.move_to (130, height - 120)
        cr.line_to (100, height - 120)
        cr.stroke ()
w = gtk.Window (gtk.WINDOW_TOPLEVEL)
w.set_title ("My Cairo Drawing Area")
    icon = gtk.gdk.pixbuf_new_from_file ("cairo-scarab.png")
    w.set_icon (icon)
    print "Icon unavailable"
w.connect ("destroy", gtk.main_quit)
a = gtk.Alignment ()
a.set_padding (4, 0, 0, 0)
w.add (a)
'''Your very own drawing area'''
area = myCairoDrawingArea (height = 500, pattern_background = True,
               borders = True)
a.add (area)
w.show_all ()
w.set_position (gtk.WIN_POS_CENTER_ALWAYS)
gtk.main ()