19.01.2013 Views

Creating a SuperKaramba theme - Linux Magazine

Creating a SuperKaramba theme - Linux Magazine

Creating a SuperKaramba theme - Linux Magazine

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

KNOW-HOW<br />

Pretty wallpaper is fine as far as it<br />

goes, but if you want desktop<br />

frills that actually do work for<br />

you, such as displaying weather reports<br />

or monitoring system data, you’ll need a<br />

tool like <strong>SuperKaramba</strong>. <strong>SuperKaramba</strong><br />

[1] is a KDE-based tool that lets you create<br />

helpful widgets for your desktop.<br />

The <strong>SuperKaramba</strong> website refers to<br />

these interactive widgets as “custom eye<br />

candy,” but <strong>SuperKaramba</strong> applets are<br />

more commonly called <strong>theme</strong>s. You’ll<br />

find many pre-built <strong>SuperKaramba</strong><br />

<strong>theme</strong>s on the Internet, but you can also<br />

create your own <strong>theme</strong>s. A Super-<br />

Karamba <strong>theme</strong> can take the form of a<br />

game, a system monitor, a desktop<br />

music directory, or even a custom toolbar.<br />

Previous articles<br />

in <strong>Linux</strong> <strong>Magazine</strong>described<br />

how<br />

<strong>SuperKaramba</strong> Workshop<br />

<strong>Creating</strong> a <strong>SuperKaramba</strong> <strong>theme</strong><br />

WIDGET<br />

BUILDER<br />

If you can’t find the <strong>SuperKaramba</strong> <strong>theme</strong> you’re looking for, you can<br />

always build your own. BY HAGEN HÖPFNER<br />

to get started with <strong>SuperKaramba</strong>. For<br />

additional information, see [2]. (These<br />

articles are currently available through<br />

<strong>Linux</strong> <strong>Magazine</strong>’s convenient online archive.)<br />

This article is a brief workshop<br />

on how to create your own Super-<br />

Karamba <strong>theme</strong>.<br />

Finding the Software<br />

The current <strong>SuperKaramba</strong> version is<br />

number 0.37. If you prefer not to build<br />

<strong>SuperKaramba</strong> yourself, you will find<br />

RPMs and Debian archives at [3] and<br />

[4]. Running superkaramba opens the<br />

<strong>theme</strong> dialog (Figure 1) and drops a control<br />

icon into the KDE kicker.<br />

<strong>SuperKaramba</strong> needs <strong>theme</strong>s to bring<br />

the desktop to life. Designs are available<br />

for many typical applications. Pressing<br />

the New Themes… button<br />

opens a selection dialog,<br />

which offers you a number<br />

of the <strong>theme</strong>s listed at<br />

[5]. When you select an<br />

entry, information about<br />

the functionality and a<br />

screenshot give you a first<br />

glimpse at your options. <strong>SuperKaramba</strong><br />

downloads the<br />

required files and installs them<br />

in the ~/.kde/share/apps/<br />

superkaramba/<strong>theme</strong>s directory.<br />

When you close the <strong>theme</strong> dialog,<br />

the installed <strong>theme</strong>s appear<br />

in the <strong>SuperKaramba</strong> window,<br />

where you can launch them.<br />

You can run multiple parallel<br />

<strong>theme</strong>s on a single desktop, starting<br />

and removing the <strong>theme</strong>s individually<br />

as required. The right-click menu<br />

40 ISSUE 68 JULY 2006 W W W. L I N U X- M A G A Z I N E . C O M<br />

also lets you<br />

specify if you want<br />

to keep the ability to move a <strong>theme</strong>.<br />

Some <strong>theme</strong>s have their own configuration<br />

dialogs. For example, you can configure<br />

the weather report to match your<br />

current location.<br />

<strong>Creating</strong> Your Own Themes<br />

To create your own <strong>theme</strong>, you need to<br />

create a file, say my_<strong>theme</strong>.<strong>theme</strong>, which<br />

you can open in <strong>SuperKaramba</strong> as a<br />

local file. As graphics and Python scripts<br />

can be added to the layout file, you will<br />

probably want to store all of these components<br />

in a single directory.<br />

The <strong>theme</strong> file comprises three components:<br />

general commands specify the<br />

<strong>theme</strong> geometry and define interactive<br />

areas. Sensors help to read system parameters,<br />

such as the current CPU load.<br />

Finally, meters display the measured<br />

values. Listing 1 illustrates the makeup<br />

of a <strong>theme</strong> file.<br />

The first line in Listing 1 positions the<br />

<strong>theme</strong> in the lower left corner of the<br />

desktop, sets a width of 200 pixels and a<br />

height of 400 pixels, specifies that the<br />

Figure 1: Use <strong>SuperKaramba</strong>’s <strong>theme</strong> dialog<br />

to install new <strong>theme</strong>s.<br />

www.sxc.hu


Figure 2: Before you start building your own <strong>SuperKaramba</strong><br />

<strong>theme</strong>, you might like to check out some of the <strong>theme</strong>s available<br />

on the Internet.<br />

<strong>theme</strong> can be moved, and sets a refresh<br />

time of 1000 milliseconds. You could set<br />

the position for the <strong>theme</strong> by specifying<br />

x and y offsets from the top left corner of<br />

the desktop.<br />

The BOTTOM=true parameter overrides<br />

the horizontal position. In a similar<br />

fashion, the RIGHT=true option positions<br />

the <strong>theme</strong> on the right margin.<br />

ONTOP=true prevents other windows<br />

hiding the <strong>theme</strong>. TOPBAR=true and<br />

BOTTOMBAR=true dock the <strong>theme</strong> at<br />

the top or bottom of the screen, just like<br />

the KDE kicker, without allowing maximized<br />

windows to hide it.<br />

The second line in Listing 1 sets the<br />

default font type, size, and color for all<br />

text elements associated with the <strong>theme</strong>.<br />

The color code defines a combination of<br />

values for red, green, and blue with a<br />

maximum value of 255. The example in<br />

Listing 1 uses 255, 255, 255, which is the<br />

<strong>SuperKaramba</strong> Workshop<br />

code that represents<br />

white.<br />

Sensors and<br />

Meters<br />

Lines 5 and 6 in Listing<br />

1 create two text<br />

meters that derive<br />

their content from the<br />

time sensor (sensor<br />

=time). Line 5 outputs<br />

the time and<br />

overrides the default<br />

font size, which was<br />

defined previously. It<br />

is a good idea to use<br />

tags to group similar elements;<br />

this improves readability<br />

and lets you specify a position<br />

for the group.<br />

The first group in our sample<br />

<strong>theme</strong> is located at x=10<br />

and y=10; besides the time<br />

and date meter, it contains a<br />

definition for the interactive<br />

area. Line 7 tells Super-<br />

Karamba to call the KDE time<br />

setting command when the<br />

120 by 34 pixel area is doubleclicked.<br />

The preview=true parameter<br />

draws a frame round<br />

the <strong>theme</strong> to indicate that it is<br />

still undergoing testing.<br />

The format parameter defines<br />

the output content and<br />

format of a field depending on<br />

the sensor you query. The "hh:<br />

mm:ss" format parameter tells<br />

the time sensor to give you hour:minute:<br />

second formatted time output, whereas<br />

format="ddd dd.MM.yyyy" gives you<br />

the weekday, the day of the month, the<br />

month and the year. Besides the time<br />

sensor, <strong>SuperKaramba</strong> has the following<br />

sensors:<br />

• cpu: System load<br />

• disk: Disk usage for mounted file<br />

systems<br />

• memory: Free and used main memory<br />

• network: Incoming and outgoing network<br />

traffic<br />

• noatun: Information from a running<br />

noatun process<br />

• program: Standard output from any<br />

program<br />

• sensor: Processes the<br />

LM sensor [10] output<br />

• textfile: Continually<br />

reads a text file<br />

• uptime: System uptime<br />

• xmms: Information<br />

from an XMMS process<br />

The second group in<br />

Listing 1 illustrates the<br />

use of some of these<br />

sensors in combination<br />

with various output devices.<br />

Line 12 reads you<br />

the free memory in<br />

Megabytes without buffer<br />

and cache memory.<br />

Line 14 uses the bar.png<br />

image to create a bar diagram<br />

that indicates the<br />

current CPU load.<br />

Figure 3: Thanks to the<br />

Python interface, pro-<br />

gramming <strong>theme</strong>s with<br />

extended functionality is<br />

quick and easy.<br />

Table 1: <strong>SuperKaramba</strong> Callback Functions<br />

KNOW-HOW<br />

Function Trigger Parameters passed<br />

initWidget(widget) Create the <strong>SuperKaramba</strong> widget<br />

widgetUpdated(widget) Update the <strong>theme</strong> Update interval from the .<strong>theme</strong> file<br />

widgetClicked(widget, x, y, button) Mouse click on <strong>theme</strong> x and y: coordinates for click relative to <strong>theme</strong>; button:<br />

mouse button used<br />

widgetMouseMoved(widget, x, y, button) Mouse movement within the <strong>theme</strong> x and y: coordinates for click<br />

relative to <strong>theme</strong>; button: mouse button used<br />

menuItemClicked(widget, menu, id) Click on menu item menu: Menu handle; id: Menu item handle<br />

menuOptionChanged(widget, key, value) Configuration menu item called in <strong>theme</strong> key: Menu item handle; value: new value for menu item<br />

(true or false)<br />

meterClicked(widget, meter, button) Click on meter device meter: Meter device handle; button: mouse button used<br />

commandOutput(widget, pid, output) Programm called via executeInteractive(), if output goes to stdout pid: Process<br />

ID for program; output: Output text<br />

itemDropped(widget, dropText) Objects dropped on <strong>theme</strong> in drag & drop operatons dropText: Text for object (e.g. URL).<br />

startupAdded(widget, startup) KDE starts an application After launching completes, the startupRemoved() and<br />

taskAdded() signals follow<br />

startupRemoved(widget, startup) See startupAdded().<br />

taskAdded(widget, task) See startupAdded().<br />

taskRemoved(widget, task) End of application program<br />

activeTaskChanged(widget, task) Moves an application to the foreground<br />

W W W. L I N U X- M A G A Z I N E . C O M<br />

ISSUE 68 JULY 2006<br />

41


KNOW-HOW<br />

01 KARAMBA x=0 BOTTOM=true w=200 h=400 LOCKED=false INTERVAL=1000<br />

02 DEFAULT font="Sans" fontsize=10 shadow=2 color=255,255,255<br />

03<br />

04 x=10 y=10<br />

05 TEXT x=12 y=0 sensor=time fontsize=12 format="hh:mm:ss"<br />

06 TEXT x=12 y=15 sensor=time format="ddd dd.MM.yyyy"<br />

07 CLICKAREA x=0 y=0 w=120 h=34 onclick="kdesu kcmshell clock"<br />

08 <br />

09<br />

10 x=10 y=50<br />

11 TEXT x=12 y=0 value="MEM"<br />

12 TEXT x=45 y=0 sensor=memory format="%fmb MB"<br />

13 TEXT x=12 y=15 value="CPU"<br />

14 BAR x=45 y=15 sensor=cpu path="bar.png"<br />

15 TEXT x=12 y=30 value="IN"<br />

16 GRAPH x=45 y=30 h=12 w=70 color=255,255,255 points=100<br />

sensor=network device="eth0" format="%in"<br />

17 IMAGE x=0 y=50 path="background.png"<br />

18 <br />

A graph is used to evaluate incoming<br />

network traffic in Line 16. The graph is<br />

12 x 70 pixels and white. The last line in<br />

this group uses the IMAGE output device<br />

to display an image. The PATH optionally<br />

accepts a URL, which allows you to<br />

embed an image from the Internet. Documentation<br />

on the formatting options for<br />

the various output devices is at [9].<br />

<strong>SuperKaramba</strong> and Python<br />

The Python interface [11] helps make a<br />

<strong>theme</strong> more flexible. Callback functions<br />

01 import karamba # Imports the Karamba API<br />

02 import string # String manipulation functions<br />

03 image=0<br />

04<br />

05 def initWidget(widget):<br />

06 global image<br />

07 karamba.acceptDrops(widget)<br />

add the ability to react to events triggered<br />

on the KDE desktop (Table 1). To<br />

allow a <strong>theme</strong> to access a Python file,<br />

the file must reside in the same directory<br />

as the <strong>theme</strong> file; it also has to use the<br />

same basic name as the <strong>theme</strong> but with<br />

the .py extension. The template at [12] is<br />

a useful starting point for your own development,<br />

as it integrates a fair number<br />

of callback functions.<br />

Listing 2 shows a Python script that<br />

allows the user to replace the image displayed<br />

in the <strong>SuperKaramba</strong> <strong>theme</strong><br />

08 image=karamba.createImage(widget, 12, 102, "bar")<br />

09 karamba.hideImage(widget,image)<br />

10 karamba.redrawWidget(widget)<br />

11<br />

12 def itemDropped(widget, dropText):<br />

13 global image<br />

Listing 2: my_<strong>theme</strong>.py<br />

14 image_link=string.split(dropText, "file:" ,1)[1]<br />

15 karamba.deleteImage(widget,image)<br />

<strong>SuperKaramba</strong> Workshop<br />

Listing 1: my_<strong>theme</strong>.<strong>theme</strong><br />

16 image=karamba.createImage(widget, 12, 102, image_link)<br />

17 karamba.resizeImage(widget, image, 176, 286)<br />

42 ISSUE 68 JULY 2006 W W W. L I N U X- M A G A Z I N E . C O M<br />

using drag & drop. It starts by importing<br />

additional modules: import karamba<br />

loads the <strong>SuperKaramba</strong> module and<br />

import string adds string manipulation<br />

functions for text variables to the script.<br />

The initWidget(widget) callback<br />

function is automatically called when<br />

the <strong>theme</strong> window is generated.<br />

karamba.acceptDrops(widget) sets up<br />

the widget to accept images that users<br />

drag & drop to the window. At the same<br />

time, karamba.createImage creates an<br />

image that is used as a placeholder,<br />

which explains why it is hidden at first<br />

(karamba.hideImage(widget,image))<br />

until karamba.redrawWidget(widget)<br />

refreshes the display.<br />

The second callback function in Listing<br />

2 is itemDropped(widget, dropText);<br />

it is called when the user drops an object<br />

on the widget. The dropText variable<br />

provides the URL for the object. The<br />

next line strips off the file: prefix and<br />

discovers the path to a local file. Then<br />

the function removes the original image<br />

from the <strong>theme</strong> window and displays the<br />

new image, using the last line to scale<br />

the image (Figure 3). ■<br />

INFO<br />

[1] <strong>SuperKaramba</strong>: http:// netdragon.<br />

sourceforge. net/ ssuperkaramba. html<br />

[2] “KTools: Decorating the Desktop with<br />

<strong>SuperKaramba</strong>,” by Stefanie Teufel,<br />

<strong>Linux</strong> <strong>Magazine</strong>, February 2005, p. 72.<br />

http:// www. linux-magazine. com/<br />

issue/ 51/ Ktools_Superkaramba. pdf<br />

[3] Guru’s RPM Site:<br />

http:// linux01. gwdg. de/ ~pbleser<br />

[4] <strong>SuperKaramba</strong> Debian packages:<br />

http:// archive. linux-peter. de/ debian/<br />

pool/ main/ s/ superkaramba<br />

[5] <strong>SuperKaramba</strong> <strong>theme</strong>s:<br />

http:// www. kde-look. org<br />

[6] Howto on creating a <strong>theme</strong>:<br />

http:// netdragon. sourceforge. net/<br />

screate. html<br />

[7] <strong>Creating</strong> non-rectangular <strong>theme</strong>s:<br />

http:// netdragon. sourceforge. net/<br />

smask. html<br />

[8] Sensor overview: http:// netdragon.<br />

sourceforge. net/ ssensors. html<br />

[9] All support meters: http:// netdragon.<br />

sourceforge. net/ smeters. html<br />

[10] LM sensors:<br />

http:// secure. netroedge. com/ ~lm78<br />

[11] <strong>SuperKaramba</strong> Python API: http://<br />

netdragon. sourceforge. net/ api. html<br />

[12] <strong>SuperKaramba</strong> Python template:<br />

http:// netdragon. sourceforge. net/<br />

template. py

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!