The Conky Blog The Official Blog for Conky, a Lightweight System Monitor

28Sep/0912

Lua Cairo Bindings for Conky 1.7.2+…Getting Started

If you saw my last post about Lua Cairo bindings for Conky 1.7.2+, but didn't go any further with it, then you're not alone! It's pretty daunting at first, especially if you haven't programmed in Lua or Cairo before. Perhaps I can persuade you...?

The images in the following two screenshots use only Conky. They were written in Lua, using Cairo bindings:
"Air" Clock Screenshot

"Rings" Screenshot

If I've tickled your fancy, here are a few tips to get you started!

Before you start

First and foremost, grab yourself a copy of the latest Conky release.  If you are running Ubuntu 9.04 or higher, you can get the .deb here.  If you are running Ubuntu 9.10 Karmic Koala, version 1.7.2 is in the Karmic repos.

Read through the Cairo tutorial, and familiarise yourself with the Lua documentation.  Not to say that you should do every exercise word-for-word or read every entry in the Lua reference manual, but it'll give you an idea of the kinds of things you can do, and where to find help if you need it.

Starting out

The easiest way to start is to modify someone else's work, just to get used to the syntax. Just find a script you like and start tweaking. Maybe change colours or shapes, placement of objects, or combine elements from different scripts. Try the following:

If you're ready to start making your own ideas come to life, have a go recreating the Cairo snippets on the Cairo website. Then start breaking out your own designs!

Working with Conky

There are a couple of bits you'll definitely need to get your Lua script to work with Conky. First, to define your Cairo surface:

if conky_window==nil then return end
local cs=cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, onky_window.width, conky_window.height)
cr=cairo_create(cs)

Next, the best bit: you can use the function conky_parse() to evaluate any Conky expression. For instance, you can use:

str=conky_parse('${cpu cpu0}')

to grab the total CPU percent usage of your system. It is saved as a string, so in order to get it as a numerical value, you will need to use:

value=tonumber(str)

conky_parse() and tonumber() together are a powerful combination, because it means that anything you can get in Conky, you can get in your Lua script.

And finally

Don't forget, if you come up with a new weird and wonderful way to use Conky, post it here!

Comments (12) Trackbacks (0)
  1. the second one is very nice. I want try cairo too but it seems to be hard for me. I will just use your great work :)

  2. I think I found a bug in your Rings script at: http://conky.linux-hardcore.com/?page_id=2800.

    name=’time’,
    arg=’%I.%M’,

    The problem is the maximum value of %M will be 59. so if the time is 4:59, the hour ring will only be 59% of the way between 4 and 5. The next second, it will jump directly to 5 oclock. It’s not a smooth transition.

    This will also not look nice if you have clock hands on top of this. The second and minute hand work alright but there is a visible difference between the hour hand and the hour ring.

    I haven’t done anything with Lua before so I have no idea how to remedy this. I’m guessing that it would be better to link to a script that calculates the minutes/60 instead of using the Conky time variable.

    Other than that, great work! It makes my Conky setup more interesting.

  3. @Vlad: yeah, I know about that problem! But with the current rings script, that’s the best I have seen. Otherwise, it will only ever point directly at the hour, and it’ll be very confusing around the turn of the hour. The better solution would be to write a separate script for a clock!

    Thanks for your interest, and I’m glad to see you’ve been able to use some of the new features :)

  4. Well i must have been doing something wrong…
    I’ve been tinkering with conky for a while now and posting my ideas on the crunchbanglinux conky config forum
    But recently it became clear that my ambitions regarding conky were limited using the native commands in conky also. This page was one of the first i came across when i wanted to start using lua (I have no programming background so i was starting from scratch). I’ve been looking through your lua scripts londonali1010, trying to decipher what they did and then trying to apply the bits i understood myself.
    http://omploader.org/vM2R0cQimghttp://omploader.org/tM2R0cQ

  5. and after a bit more tinkering…
    http://omploader.org/vM2Z6bAimghttp://omploader.org/tM2Z6bA

    now i just need a good idea to work on

  6. @mrpeachy: Looking great! Lua/Cairo (and Lua/Imlib2) isn’t tough to get a grip on once you get started, as you seem to be finding! You should be sure and enter our Conky of the Month Competition…See the main blog page for details :)

  7. thanks londonali1010!
    i took a look at imlib2 but i couldn’t get it working.
    do you have to have lua-imlib2 http://luaforge.net/projects/lua-imlib2/?
    or is the ability to use imlib2 already present in lua 5.1?
    do you have an example script uising imlib2?
    thanks

  8. @mrpeachy: Imlib2 isn’t that much different from Cairo, just different function names! If you’ve got the current Karmic conky-all package, it will have Imlib2 bindings for Lua already compiled. If you compile from source, you need enable it in the configure stage.

    As for the code, you can get started here: http://blog.conky.be/2009/11/04/feature-feature-imlib2-cairo-bindings-for-lua/

    Good luck!

  9. Hello londonali!

    I was playing with your lua examples for few hours and encountered a little memory leak (about 1MB / 15min). My conky is constantly consuming more resources when displaying ring meters or air clock.

    I am using scripts downloaded from :
    http://conkyhardcore.com/beginners/how-did-he-do-that/by-londonali1010/conky-widgets-for-conky-1-7-2/

    Have you reported that issue?

    PS. Without lua / cairo bindings the memory consumption is constant.

    I’ll try to look into it, but didn’t write anything in lua before. Anyway… rings are great! :)

  10. Okey… I will post here some tips how to avoid memory leaks…
    1. Use local variables or remember to ‘=nil’ every non local variable before main function finishes (main function – function called from conky).
    2. Try to declare functions inside main function scope. Remember to keep it local so the garbage collector will take care of it when it becomes unreachable. I have no idea how conky handles global functions. Perhaps to handle them properly it needs the ‘conky_’ prefix like ‘function conky_round_rect()’.
    3. When using cairo struktures remember to free resources using cairo_destroy, or cairo_pattern_destroy functions (lack of the second one creates memory leak in ‘Air clock’)

  11. links broken on both rings & air clock


Leave a comment


No trackbacks yet.

Essential Links

Categories

Archive Posts