Outils pour utilisateurs

Outils du site


issue47:command_conquor_pp._5-8

Ceci est une ancienne révision du document !


I realize that last month I said I was going to do an article on Zenity within a script. However, I couldn't think of a script that would benefit from Zenity - without getting extremely complicated. Instead, I decided I would go further into Conky, specifically the ability to use lua scripts to draw graphical items to the desktop (in this case, rings, but I'm sure other objects are possible). Before we get started, I'd like to make the disclaimer that I am in no way a lua coder, and there is a good chance that there are easier ways to make the changes I've made, but it's what I came up with.

For those of you who aren't sure what I'm talking about, this screenshot on my DeviantArt profile is an example of what is possible with Conky: http://lswest.deviantart.com/#/d3ay5fb

First and foremost, make sure you have installed Conky (1.7.2 is in the repositories as of version 9.10 Karmic Koala). I will assume that everyone is using version 9.10 or higher. If you're using an older version, launchpad will probably have a PPA for you.

As for the widgets we'll be creating, I'm going for a simple MPD music widget, and a clock (the same widgets visible in the above screenshot). Also, since I felt no need to re-create the wheel (or, in this case, the ring), I will be using the following script as a basis for what we are doing now: http://londonali1010.deviantart.com/art/quot-Rings-quot-Meters-for-Conky-141961783. For those of you who would like the complete scripts (for reference/correction), see the Scripts section at the end of this article.

Pre-coding

Before we get started on the actual script, I ask you to decide if you want it in two separate Conky instances (my choice) or within a single instance. The reason why I use two is quite simply because I have two other Conky instances on my desktop, and merging the widgets into one would have resulted in overlaps between Conky instances. If you wish to use only one, you will need to increase the minimum size, and adjust the x and y values for each widget to place them within the Conky window. The x and y relate to the relative position of Conky. For example, if Conky starts at (400,200) 1) then a widget with placement (100,85) will actually be at (500,285) on your monitor. Keep this in mind.

Also, to use the lua scripts, you must add in the following to your .conkyrc:

lua_load /home/lswest/conky_testing/rings-v1.2.lua

lua_draw_hook_pre ring_stats

…where the top line is, of course, the actual absolute path to the lua script, and the name below is the name of your main function (if you write conky_ring_stats, or ring_stats, it will find the function regardless of which variation you use within the actual script).

The script has comments to clarify the entries, but I'll quickly explain each as well. The name is actually the name of the Conky variable (i.e. ${time }), the args are the arguments (i.e. ${time %I}), and it is parsed by the script in lines 121-131 (on pastebin), within the local function setup_ring. It basically sends the command (after formatting it into ${name args}) to Conky, gets the result, and parses it. Then it's casted into a number, and the deviations (entered into the max variable) of the ring are calculated (so, if you say 360 divisions, then each division is exactly 1° of the ring, or if you have 12, then it's 2*pi/max (in radians)). It's not important if you don't understand this, just keep in mind that to have 12 hours within the ring, you must make 12 divisions. The 4 following variables are simply background and foreground colors, and their alpha (transparency) levels. The x and y variables make up your position vector, radius is the width of the ring, thickness is the size of the line, start_angle is where the circle starts (0°), and end_angle is where it stops (360°), so that we get a complete circle.

For those of you who know the formatting for the date command, you'll know that %I is the format for the hours with leading 0s (so 01…12). The format for a 12-hour clock, without leading 0s, is %l, but it doesn't matter for this clock - I also had it working fine with %H (0…23). The next two rings I made smaller and made the seconds ring 2 pixels thinner. In the end, you should have something along the lines (after the hours ring) of the code shown right.

As you can see, it's fairly straightforward. If you're fine with the seconds ring counting off the seconds, and without the date in the center, you're finished. If you, like me, want the seconds to fill the innermost circle, then you'll need to add the following line before “cairo_arc(cr, xc, yc, ring_r, t_arc-arc_w, t_arc+arc_w)”:

if pt['arg'] == '%S' then cairo_arc(cr, xc, yc, ring_r, angle_0, t_arc+arc_w) end

What this does is simply start at the angle_0 (12 o'clock on the ring), and extends the line. My first reaction is to put the original line into an else statement, but, it works without it, and it's a little less typing, so we'll forgo good formatting in this case. If you want to place the date within the center of the ring, it's a bit of guess-work for the positioning, but here is what you need to add to your .conkyrc:

${goto 115}${voffset 150}${time %A}

${goto 115}${time %b %d %Y}

The goto line shifts it over to the right (you can also use ${offset <pixels>}), and voffset is the vertical offset (i.e. pixels moved down from the top of the conky window). What I did was display the day on the top line (${time %A}), and the date on the line below it. If you want to change the way it's displayed, checking the manpage of date will give you the formatting options you need.

MPD Widget

Now before we start this, the widget I describe here works only for MPD (Music Player Daemon), since Conky lacks variables for other music players. I'm sure you could get it working the same with some tricky coding, but I don't think it's worth it as most other music players have a “now-playing” widget of sorts.

The settings_table entry for this one looks like the code shown on the next page, top left.

As you can see, we're working with 100 divisions (since it's a percent, it will be a value between 0 and 100). Also, the arg variable is empty, which is important, since leaving it out entirely makes it incompatible with the functions we use later (missing argument). Once you've done this, I also altered the script so that the widget disappears when the music is paused. To do this, you need to make the following changes to the script:

Add this function to the beginning or end of the file:

function conky_my_flag(my_arg)

  flag = my_arg
  return ""

end

Then, place the following text from the original script:

local updates=conky_parse('${updates}')

  update_num=tonumber(updates)

if update_num>5 then

      for i in pairs(settings_table) do
          setup_rings(cr,settings_table[i])
      end
  end

inside of the following if statement:

if tonumber(flag) == 1 then <text from above> end cairo_destroy(cr)

So that the last 11 or so lines of the file read as shown above right.

What the above changes do is simply to destroy the widget if MPD isn't running, and otherwise to run as normal. The function we created is so we can assign a value to the global variable flag that we use within the if-statement. Now, before this script works, you'll need to add in ${lua my_flag 0} and ${lua my_flag 1} into your .conkyrc so that the function is called and the flag variable is set to 0 or 1, depending on if MPD is stopped (0), or not (1). The TEXT section of my .conkyrc looks like the code shown above.

What this does is set the flag variable to 0 when if_mpd_playing is false. Otherwise it gets set to 1. The rest of the settings display and position the album art, display “Paused” if MPD is paused, or the Artist and Song Title on two lines to the right of the ring if MPD is playing. The ${scroll 38 ${mpd_title}} section causes the title to scroll (so the text moves from right to left) if it's longer than 38 pixels. You can leave this out, but I put it in there to prevent the text from being longer than my Conky is wide. In order to display the image, you'll need to add the following two settings above the TEXT marker somewhere:

imlib_cache_size 0

Also, the mpd-cover script is below, in the Scripts section. The mpd-cover script is written for python 2.X, but you can always use the 2to3 program to re-write it for python 3. If you have issues, let me know. Be aware that some symbols can cause problems with the script. I have done very little editing (if any) to it, and it was originally from here: https://bbs.archlinux.org/viewtopic.php?id=112708

Hopefully the majority of you have found this interesting, and, as always, I'm open to requests, suggestions, general feedback, and questions. You can reach me at lswest34@gmail.com, and remember to put C&C or FCM into the subject line, so I don't overlook it. Also, English or German are my preferred languages, because otherwise I will have to rely on Google Translate. If anyone improves the scripts I have listed/used here, feel free to send a copy to me with an explanation of additions/changes, and I will note it at the beginning of the next article for anyone who is interested.

Scripts: http://pastebin.com/SpC6bcn7 Lua clock ring http://pastebin.com/iZFdZAeg Conky mpd http://pastebin.com/zkVVHkYk .conkyl_mpd http://pastebin.com/BDa5MHuR conkyrc for clock http://pastebin.com/ZX4pLbta mpd-cover script

1)
x,y
issue47/command_conquor_pp._5-8.1304510063.txt.gz · Dernière modification : 2011/05/04 13:54 de auntiee