The Journalist's Cage

And this gray spirit yearning in desire to follow knowledge like a sinking star...

HomeBlogTagsArticles

Calendar

January 2007
SuMoTuWeThFrSa
 123456
78910111213
14151617181920
21222324252627
28293031

Recent Bookmarks

Tags

Archives


RSS

Global key bindings with Python and GTK

I recently purchased a Logitech G15 gaming keyboard, which features a nifty integrated LCD and an assortment of macro keys. Unfortunately, Beryl only allows users to bind arbitrary commands to 11 keys. Since the G15 has 18 separate macro keys in addition to multimedia buttons, I had to find an alternate solution. I finally decided to implement my own binding manager so that I can associate keys with commands and Python expressions.

Logitech G15

Obviously, the first step was to figure out how to set up global key bindings with Python. I started by poking around the GTK/GDK documentation in search of relevant functionality, but I didn't really find anything there. I asked in the #linux channel on the Ars Technica IRC server and got some useful suggestions, but nothing that seemed to do exactly what I wanted. It finally occurred to me to investigate the source code of a Python application that has global key bindings.

Deskbar, a really useful panel applet that I use on a daily basis, uses a global key binding for activation. The deskbar/Keybinder.py file in the Python site-packages directory reveals that Deskbar is actually using a file called _keybinder.so, which appears to have been created for the Tomboy note-taking program. The tomboy_keybinder_bind function in _keybinder.so makes it possible to associate a binding with a Python function. The binding is described as strings consisting of modifiers and single letters. The following is a simple example that shows how to associate a key binding with a function:

#!/usr/bin/env python

# Import the GTK library
import gtk

# Import the key binding function from the module
from _keybinder import tomboy_keybinder_bind as bindkey

# Define a function to execute when the binding is activated
def onBindingPress(arg):
  print "Binding initiated and parameter received: %s" % arg

# Bind to the onBindingPress function and pass "Test" as an argument
bindkey("<Ctrl><Alt>b", onBindingPress, "Test")

# Establish a binding to quit the program
bindkey("<Ctrl><Alt>q", gtk.main_quit)

# Start a GTK main loop that will run until the quit binding is used
gtk.main()

I find it a bit odd that this functionality had to be borrowed from Tomboy, and it makes me wonder how it is implemented in _keybinder.so. I'm not entirely sure why global key binding support isn't provided by the Python and Ruby GTK bindings, but I'm pleased that I found a way to do it with minimal hassle.


Posted on 2007-01-100 comments



XEmbed with Ruby and Gtk::Socket

I recently discovered a Python IDE called PIDA that provides support for embedded Vim. I'm not a big fan of Python IDEs, but being able to use Vim as the editing component is definitely a killer feature. I haven't seen Vim embedded in a GNOME program since the unfortunate demise of the GVim Bonobo component and I had no idea it was even possible with current versions of Vim. A quick inspection of PIDA's source code revealed that it launches a new instance of GVim and then uses XEmbed to incorporate it into the user interface.

XEmbed experiment I had never heard of XEmbed before, but seeing it used in PIDA compelled me to pursue further investigation. Apparently, XEmbed makes it possible to embed virtually any X11 window inside of another application. GTK provides support for this with the Plug and Socket widgets, which are available in GTK's Python and Ruby bindings. XEmbed client support is also available in Qt with the QtXEmbedContainer object.

For the sake of experimentation, I wrote a simple Ruby script that uses my window management library and the Gtk::Socket object to facilitate window embedding. It provides two combo boxes which each contain a complete list of windows. The user selects a window in each one, and then clicks the Grab button to grab those windows and reparent them in Socket objects separated by a resizeable splitter. The source code for this experiment is relatively simple:

#!/usr/bin/env ruby

require "wmlib"
require "gtk2"

win = Gtk::Window.new("Embed Test")
win.signal_connect("destroy") {|w| Gtk.main_quit}

win.add vb = Gtk::VBox.new
vb.pack_start(hb = Gtk::HBox.new, false, false)

lst = Gtk::ListStore.new String, Integer

hb.pack_start(combo1 = Gtk::ComboBox.new)
hb.pack_start(combo2 = Gtk::ComboBox.new)
hb.pack_start(btnGrab = Gtk::Button.new("Grab"), false)

combo1.model = lst
combo2.model = lst

for w in WM::Window
  i = lst.append
  i[0], i[1] = w.title[0..30], w.id if w.title
end

vb.pack_start(pane = Gtk::HPaned.new)
pane.pack1 socket = Gtk::Socket.new, true, false
pane.pack2 socket2 = Gtk::Socket.new, true, false

## Use tabs instead of panes
#vb.pack_start(tabs = Gtk::Notebook.new)
#tabs.append_page(socket = Gtk::Socket.new)
#tabs.append_page(socket2 = Gtk::Socket.new)

win.show_all()

btnGrab.signal_connect("clicked") {|*a|
  socket.add_id combo1.active_iter[1]
  socket2.add_id combo2.active_iter[1]
}

Gtk.main

While experimenting with panes and XEmbed, one is reminded of tiling window managers like Ion. The ability to place arbitrary windows into tabs or panes is very compelling, and I'm certain I'll find some uses for the feature in the future.


Posted on 2007-01-060 comments



Timed events in Ruby/GTK

Wii Watch Screenshot I've been trying to get my hands on a Nintendo Wii since the release last month. I haven't had much luck, and I'm not willing to pay a higher price to get one from e-bay or from the numerous retailers that offer overpriced bundles. To help me keep an eye on Wii availability, I made a simple notification utility that uses xpBargain's Wii Locator RSS feed. My utility checks the RSS feed every five minutes and displays the results in a compact notification window.

I was initially just going to keep a browser window open at the Wii Locator page and use the Firefox ReloadEvery extension to refresh it every five minutes, but that would waste a lot of screen space and be harder to read at a glance. I decided to use Ruby and GTK just to keep things simple. Coding simple data lists with a GTK treeview is more trouble than it is worth, so I decided to generate simple html and dump it into a GtkMozEmbed widget.

Making this sort of utility is relatively trivial in GTK, and I managed to write most of it without having to refer to the API documentation. I used REXML to parse the RSS feed and then I generate the HTML by hand. I could have converted the RSS to HTML with XSL, but that probably would have been overkill for such a simple throw-away app.

Everything seemed to work, but I ran into a small problem. Ruby's simple threading system doesn't work with GTK's main loop. To make a Ruby script perform an action every 300 seconds without stalling the rest of the program, I would typically do something like this:

Thread.new do
  loop do
    process_data
    sleep 300
  end
end

Unfortunately, that particular approach makes the program hang. After a little bit of research, I figured out how to perform timed events on a loop with GTK. The timeout_add method will evaluate a block at intervals when the program is otherwise idle. The block will continue to repeat at roughly the specified interval as long as the block returns the boolean true value when it is evaluated. In my simple Wii availability tracker, I used the following code:

Gtk.timeout_add(300 * 1000) do
  process_data
  true
end

It seems like the only places that have Wiis available right now are selling overpriced bundles. I probably wont be able to get one at a reasonable price until January, but at least my new utility will make it easier.


Posted on 2006-12-233 comments



GNOME 2.16 Released

GNOME 2.16 promo GNOME 2.16 has been released! With new features for many of my favorite programs, numerous bugfixes, and many aesthetic improvements, GNOME 2.16 is very polished and ready for widespread deployment. Although I tested it extensively in a virtualized VMware environment (the VMware image was kindly provided by Brent Smith), I think I'll probably wait until the official release of Ubuntu Edgy before using it on my main desktop computer.

I'm very pleased with the new features in the Evolution mail client, particularly the newly added support for a vertical message pane view. Evolution got a lot of really nice performance enhancements for this release, and the developers say that IMAP loading time is up to 48 percent faster! Maybe now Jorge will complain less often about Evolution's IMAP deficiencies. Gedit now has a sidebar file browser plugin as well as support for LaTeX authoring. With a built-in snippets system and support for scripting with Python, Gedit is quickly becoming a powerful tool.

For a more comprehensive overview of new features in GNOME 2.16, check out the review I wrote for Ars Technica. Published a day before the official release, my review attracted significant attention, and has been featured on most of the open source news sites that I read on a daily basis, including: Slasdhot, OS News, Footnotes, Linux Today, and Linux Weekly News. I also submitted it to Digg, where it rapidly climbed onto the top ten list for the technology section. Needless to say, my review has spawned inevitable GNOME vs. KDE arguments in discussion threads on several sites.

The 2.16 release is the first to include Mono as an official dependency. Support for C# applications and open source .NET technology became a controversial and highly divisive issue within the GNOME community in the weeks leading up to the official module decisions. Although I was initially a sceptic when Mono was still in its infancy (at the time, I felt that it would be more productive for the open source community to focus on Parrot), the steady stream of highly impressive C# applications developed by the GNOME community quickly changed my mind. In the past year, I have been a very vocal advocate of Mono because I see .NET as the future of desktop software development. I hope that the Mono developers can keep pace with Microsoft, and I hope to see compelling new .NET features like xlinq included in Mono in the future. Released yesterday, the latest version of MonoDevelop has a lot of exciting new features, including support for inline menu editing in the Stetic GUI builder, and integrated support for ASP.net development.

My Contributions

My contributions to 2.16 were minor but much-needed. I started rewriting the documentation for various applications in the gnome-games module. I have temporarily postponed additional work on gnome-games because my professional life is quite demanding at the moment, and because at least one of the games will be removed at some point in the next cycle and I don't really know which one yet.

I also helped with the release notes for 2.16. The release notes are typically done by Davyd and Murray, both of whom are relatively articulate. For whatever reason, they weren't available to it this time, and the responsibility fell upon other volunteers from the GNOME marketing list. The whole thing was done at the last minute and Claus Schwarm ended up writing most of the initial draft. I went through it and cleaned up a lot of minor problems. I primarily fixed spelling and grammatical errors, but I also rewrote several paragraphs for the sake of clarity. Others did the same, and the end result was more or less acceptable. Brent Smith converted it to DocBook practically overnight, and included screenshots augmented by my utility. The entire process felt very rushed, and I hope that it is planned better next time. If I hadn't noticed Quim's blog entry about the release notes on PGO and posted it to the documentation mailing list, fewer members of the GNOME documentation team would have been involved. Despite the minor problems, the release notes turned out fine. Claus and Brent deserve a lot of credit for their efforts. As a aside note, now that I have seen the effects in action on numerous screenshots, I think that drop shadows should only be used on screenshots with a single window, and edge fading should only be used on screenshots that include an arbitrary portion of the screen.

I want to take this opportunity to thank everybody that was involved in making GNOME 2.16 a reality. Everybody did a great job, and I'm looking forward to contributing as we march towards GNOME 2.18!


Posted on 2006-09-070 comments



The Evolution of a Screenshot Utility

In the dynamic world of technology journalism, there are very few universal truisms. One of the most important things I have learned is that screenshots almost always improve the quality of an article. In written works about software, particularly in reviews or tutorials, screenshots fulfill a number of crucial functions and imbue content with elegance and authority that would otherwise be absent. Failure to include a screenshot in a news article about a new program, for instance, elicits criticism and rebuke from irritated readers. They don't just appreciate screenshots, they expect screenshots.

The screenshot rule isn't entirely applicable to documentation. Although screenshots are still important in documentation, it is a context in which they should be used sparingly. There are many reasons for this, but I think the most important one is that screenshots are more likely to be superfluous in task-oriented reference material that is meant to be used alongside the actual program. In many cases, screenshots in the GNOME documentation are used to help the user confirm that they are reading about the right program.

Although I routinely use screenshots to break up large blocks of text in articles in order to increase readability, screenshots in documentation actually have the opposite effect. As the GNOME documentation style guide points out, breaking up documentation text with screenshots inconveniences readers by making them do additional scrolling. There are also some pragmatic reasons. Each screenshot included in the documentation has to be replicated by translators in each of the 42 languages supported by GNOME (Update: the GDP is evaluating ways to automate this process). Screenshots also have to be updated constantly as continued development alters the appearance of applications.

One of the points mentioned in the style guide has been of particular concern to the GDP lately. Usability studies have shown that users confuse screenshots in documentation with real windows. Although this may seem far fetched, it is apparently a very real concern, and one that we are actively trying to address. Joachim suggests fading the edges of screenshots to make the distinction between windows and images more apparent to users. I think this is a great idea, but as Joachim points out in a comment on bug #348495, there is no way to ensure that the fading effect will be consistent in all screenshots. Additionally, opening up the GIMP and doing that sort of operation for every image could become very time consuming.

For the sake of simplicity and consistency, I think we need to automate the process. In order to facilitate discussion on the possibility of leveraging automated image effects, I have developed a simple proof-of-concept utility that automatically blurs the edges of PNG images and applies a drop shadow.

The New Utility

Written in Python, my new utility leverages Cairo, PIL, and GTK. I initially wanted to do all of the image processing with ImageMagick, but the ImageMagick bindings for Python don't appear to be maintained or widely distributed. When I discovered the binding deficiency, I thought about using Ruby instead of Python, but the utility really needs to be built with a language that is used and understood by other GNOME developers. I also want to avoid foisting Ruby/GNOME dependency headaches on other documentation writers. It would have been nice to be able to avoid using PIL, but Cairo doesn't seem to have a working guassian blur filter yet, and I need that in order to produce a drop shadow. There is actually a value in Cairo::Filter called FILTER_GAUSSIAN, but apparently it doesn't do anything yet.

Using PIL to render the drop shadow is problematic because there is no simple way to convert Cairo surfaces into PIL image instances with version 1.0.2 of Cairo. Right now, the only way to do it is to save the Cairo surface to disk and load it back into a PIL image instance. Since the Python Cairo bindings don't use Python's file interface conventions, I can't just save to a StringIO instance like I do when I want to get a PIL image into a GDK pixbuf. There are some nasty hacks that involve wrapping a Cairo surface around a binary array, but that particular trick causes some peculiar problems and distorts the images in some cases. Fortunately, a new surface method called to_rgba() is already in CVS, and possibly even in the next major version of Cairo. For now, I have the script automatically save the image and load it back in again, but hopefully I'll be able to eliminate that step in the future.

My working prototype isn't particularly elegant, but it is effective. I spent an extra hour adding a user interface that allows the user to customize the parameters of the operation. Users can drag sliders to change several values:

  • border - specifies the size of the faded regions
  • filled - specifies how far from the edges the fades should end
  • offset - specifies the drop shadow offset

The UI is currently very kludgy, but it will make it easy to compare various configurations and determine exactly which values should be used by default for documentation screenshots. If there is sufficient interest, I plan to eventually produce a complete, streamlined screenshot utility specifically for use by documentation writers. It will include support for screen region selection and it will make it possible to capture multiple images and then choose which ones to save. (Update: It looks like other members of the GDP team are also interested in automation and have some very cool ideas of their own. I particularly like the idea of using DogTail for automatically grabbing the images.)

The current implementation of my utility can be run as a stand-alone script or loaded into other Python scripts as a module. To execute it in stand-alone mode, simply run it from the command line and provide the path to a png file as a parameter:

python edge_fader.py screenshot.png

The following example shows how to use the edge fader as a module:

#!/usr/bin/env python

import edge_fader

img = edge_fader.fade_edges("original.png",
    border = 30, filled = 2, offset = 6, show_shadow = True)

edge_fader.save_to_disk(img, "output.png")

View the source code here, and download it here. In order to run it, you will need PIL, and Python bindings for Cairo and GTK. An example of the program's output can be found here, and this is a screenshot of the program itself. Comments and criticism are greatly appreciated.


Posted on 2006-08-020 comments



A Screenshot Utility Is Born

GNOME's built-in screenshot utility really sucks. It must be invoked every time a single screenshot is taken, the interface doesn't facilitate capture of specific windows or screen regions, there is no easy way to save the same capture to more than one location, and the tiny preview makes it difficult to evaluate the captured image before I choose to save it. When I need to make screenshots for my articles, I typically use the import command provided by ImageMagick. I finally got tired of putting up with suboptimal screen capture tools and decided to write my own.

My new screenshot utility features a full-size preview, the ability to capture individual windows or specific screen regions, an integrated file selection component, support for saving the captured image directly to a remote location, and support for saving the same capture to multiple locations. The entire utility took me about an hour to build, including time spent researching the various APIs. The current version is approximately 25 lines code, not including comments or blank lines. I designed the interface with Glade and wrote the code with Ruby. The network transparency features are all implemented with GNOME's VFS library and the screen capture functionality is provided by Ruby ImageMagick bindings.

The effectiveness of my utility, the relative ease with which I developed it, and the brevity of the code are all a testement to the power of Ruby and GNOME. I'm convinced that Ruby and Glade provide the best solution available for rapid application development on Linux. The GNOME bindings for Ruby are excellent and relatively complete, but there are still problems in a few places. The GnomeVFS bindings are currently undocumented, and the API does not behave the way one would expect it to. GnomeVFS::File diverges from the traditional File class in several respects. The most frustrating difference relates to file creation. It should be possible to create a new remote file thusly:

GnomeVFS::File.new("sftp://cixar.com/home/segphault/test.txt", 2)

Unfortunately, the above yields a "File not found" error. I queried the folks in the relevant IRC channel, but didn't get much help. After a great deal of experimentation, I filed a bug report. I then proceeded to examine the relevant source code to see if I could find out why file creation was raising a "File not found" error. Careful examination of the file_initialize function revealed that file creation only transpires when the File.new method is passed three arguments. The missing argument indicates whether or not the program should refuse to overwrite existing files. The following successfully performs the creation operation:

GnomeVFS::File.new("sftp://cixar.com/home/segphault/test.txt", 2, false)

In any event, my new utility works quite well, and you can benefit from it too, because I am distributing it under the GPL. You can read the code or download the utility. Keep in mind that it has quite a few dependencies, so you might not be able to run it if you can't get the necessary libraries.


Posted on 2005-08-241 comments