Showing battery life in awesome with Lua

logo

On my Asus EEE PC 701, I don’t have a lot of screen real estate (800×480 on a 7″ screen). Also, using the touchpad is not the most efficient method of controlling a window manager, since it requires more precision than I care to use. These factors make a tiling window manager — like awesome — ideal.

I’ve already written about some of my experiences with awesome and Lua, but it’s time to accept the bad with the good and move on. So let’s get my desktop configured the way I like.

My desktop

The only things I really need to show on my screen are a few tags (1-3), the taskbar, the layout indicator (although I’ll go months without using anything other than the “max” layout), and battery life on my laptop. I’ve given in to the default color scheme to make it easier for me to update my .awesomerc.lua, since I download the git HEAD on a weekly basis.

Here is a typical view of my streamlined desktop:

screen_thumb

Click here to view full-size

This layout allows me to navigate quickly from one window to another. Now whenever I use a non-tiling window manager, I get irritated by the time wasted on organizing windows on the screen. Shouldn’t the window manager do that? :)

Displaying battery power

Here is a little bash script that I have in my ~/bin directory to display the current battery life as a percentage.

#!/bin/bash

### BATTERY
REM=`awk '/remaining capacity/ { print $3 }' /proc/acpi/battery/BAT0/state`
LAST=`awk '/last full/ { print $4}' /proc/acpi/battery/BAT0/info`
#STATE=`awk '{print $2}' /proc/acpi/ac_adapter/AC0/state`
#if [ "$STATE" = "on-line" ]; then
  #BAT=$(echo $REM $LAST | awk '{printf "Bat: %.1f%%, AC", ($1/$2)*100'})
#else
  #PRESENT=`awk '/present rate/ { print $3}' /proc/acpi/battery/BAT0/state`
  #BAT=$(echo $REM $LAST $PRESENT | \
    #awk '{printf "Bat: %.1f%%", ($1/$2)*100}')
  #  awk '{printf "Bat: %.1f%%, %d min", ($1/$2)*100, ($1/$3)*60}')
#fi
PERCENT=$(echo $REM $LAST | awk '{printf "%d", ($1/$2)*100'})

echo "$PERCENT%"

Next, we’ll need a way to read the output of a system call in Lua. I’ve added this function in my .awesomerc.lua:

function get_command_output (command)
    local c = io.popen(command)
    local output = {}
    i = 0
   return c:read("*line")
end

This code runs the command you pass in, and returns the first line of output. Now we can retrieve the battery life for display in an awesome widget. In the hook_timer function, which I’ve set to run every 10 seconds, you retrieve the output of the battery command and put it in the textbox, like so:

    function hook_timer ()
        -- For unix time_t lovers
        -- mytextbox:set("text", " " .. os.time() .. " time_t ")
        -- Otherwise use:
        -- mytextbox:set("text", " " .. os.date() .. " ")
        mytextbox:set("text", " " .. get_command_output("battery") .. " ")
    end

My .awesomerc.lua

My latest .awesomerc.lua is always located here. Since I’m keeping up with the git HEAD, I try to minimize the differences against the project configuration file, while at the same time making sure I have a customized environment that is fast and efficient.


Related Posts

Tags:
Posted in linux on June 26th, 2008 | No Comments »


Awesome and Lua - not so awesome

logo

Well, awesome has taken a big turn by including Lua support. I noticed something was up when my .awesomerc was doing nothing. Some of the comments on the developer’s blog about the Lua additions are negative or lukewarm. After downloading the latest git snapshot and playing around with it, I could see why.

Lua is described as a “lightweight, extensible programming language”. It uses tables as its fundamental data type, in much the same way that Lisp uses lists, and Tcl uses strings. This is my first exposure to the language, and I could see it being useable.

Why not have Lua in awesome?

My problem isn’t that there is an extension language, it’s that we’ve no longer have a configuration file for quickly setting up awesome. I think that is a great loss, and an unnecessary one. Even if the configuration language was in python, I would still not like to have to learn a new library and set of functions for setting up my environment.

Personally, I think the best idea is to leave everything as orthogonal as possible and allow external programs to query and set values in awesome, as was being done. If you have to include more power, then I’d probably side with the dwm decision of having to modify the source code yourself.

Ok, enough griping. I may end up rolling back and staying with my last checkout of awesome, while it was still awesome. In the meantime, here’s my latest .awesomerc.lua:

My current .awesomerc.lua

My ~/.awesomerc.lua is here. Shown below is the diff to give an idea of what’s different. The only widget I really need on my laptop is the battery meter so I know how much time I have left. Also, since my EEE has a 7″ screen, I prefer the “max” layout as the default for all my windows (I rarely use any other layout).

--- /usr/share/awesome/awesomerc.lua    2008-06-09 11:18:56.000000000 -0700
+++ .awesomerc.lua  2008-06-09 15:07:38.000000000 -0700
@@ -2,15 +2,16 @@

 -- Include awesome library, with lots of useful function!
 require("awful")
+require("wicked")

 -- {{{ Colors and fonts
-awesome.font_set("sans 8")
+awesome.font_set("terminus 9")
 awesome.colors_set({ fg = "white", bg = "black" })
 -- }}}

 -- {{{ Variables definitions
 -- This is used later as the default terminal to run.
-terminal = "xterm"
+terminal = "urxvt"
 -- Default modkey.
 -- Usually, Mod4 is the key with a logo between Control and Alt.
 -- If you do not like this or do not have such a key,
@@ -25,8 +26,8 @@
 -- Define workspaces table
 workspaces = {}
 -- Create 9 workspaces
-for i = 1, 9 do
-    workspaces[i] = workspace.new({ name = i })
+for i = 1, 4 do
+    workspaces[i] = workspace.new({ name = i, layout = "max" })
     workspaces[i]:add(s)
 end
 -- I'm sure you want to see at least one workspace
@@ -69,16 +70,25 @@
     mylayoutbox[s]:set("image", "/usr/share/awesome/icons/layouts/tilew.png")
 end

+-- Runs 'my_nifty_script.py' every 10 seconds and puts its output into the widget
+wicked.register(mytextbox, 'function', function (widget, args)
+   local filedescriptor = io.popen('battery')
+   local value = filedescriptor:read()
+   filedescriptor:close()
+   return ' ' .. value .. ' '
+end, 10)
+
+
 -- Create a statusbar for each screen and add it
 for s = 1, screen.count() do
     mystatusbar = statusbar.new({ position = "top", name = "mystatusbar" .. s,
                                 fg = "lightblue", bg = "black" })
     -- Add widgets to the statusbar - order matters
     mystatusbar:widget_add(myworkspacelist)
-    mystatusbar:widget_add(myiconbox)
+    --mystatusbar:widget_add(myiconbox)
+    mystatusbar:widget_add(mylayoutbox[s])
     mystatusbar:widget_add(mytasklist)
     mystatusbar:widget_add(mytextbox)
-    mystatusbar:widget_add(mylayoutbox[s])
     mystatusbar:add(s)
 end
 -- }}}
@@ -113,6 +123,7 @@

 -- Standard program
 keybinding.new({ modkey }, "Return", function () awful.spawn(terminal) end):add()
+keybinding.new({ modkey }, "f", function () awful.spawn("firefox3") end):add()

 keybinding.new({ modkey, "Control" }, "r", awesome.restart):add()
 keybinding.new({ modkey, "Shift" }, "q", awesome.quit):add()
@@ -190,7 +201,7 @@
 awful.hooks.newclient(hook_newclient)
 awful.hooks.mouseover(hook_mouseover)
 awful.hooks.arrange(hook_arrange)
-awful.hooks.timer(1, hook_timer)
+--awful.hooks.timer(5, hook_timer)

 -- }}}

Related Posts

Tags:
Posted in linux on June 9th, 2008 | 5 Comments »

My current EEE setup

My Asus EEE PC is running better than ever. Mostly, this is due to following the EEE PC thread on the ArchLinux forum. These are the changes I’ve made.

Kernel

I added the following repository to my /etc/pacman.conf:

[eee]
Server = http://code.toofishes.net/packages/eee

From there, I installed the following packages:

  • kernel-eee: A streamlined kernel for the EEE hardware
  • madwifi-ng-eee: Wifi drivers for the EEE

These packages obviate the need for most of dkite’s packages, except for acpid_eee, which still works great!

/etc/X11/xorg.conf

I decided to go back to the i810 driver and the 915resolution package. That fixes the brightness issues. I think the X restarting problem I was having with an external monitor was due to my window manager more than anything.

In /boot/grub/menu.lst

I changed the default timeout to 1 (I had it on 0 for a while). I also added the following entry, and set it to be the default choice:

# (2) Arch Linux
title  Arch Linux
root   (hd0,0)
kernel /boot/vmlinuzeee root=/dev/sda1 ro quiet

The important things to notice here are:

  1. The absence of an initrd line. It’s not necessary with kernel-eee.
  2. The addition of the quiet parameter to the kernel line. This makes the boot a lot cleaner — and quicker!

Related Posts

Tags:
Posted in linux on March 6th, 2008 | No Comments »

Asus EEE Archlinux setup

I am very pleased with my current Asus EEE setup with Archlinux. I’ve had my system installed for two weeks now, and everything seems to be working well. Here I provide complete details and configurations for my current setup.

» Read the rest of this entry


Related Posts

Tags:
Posted in linux on February 15th, 2008 | 3 Comments »