KAworu MPD Widget

From awesome
Jump to: navigation, search

We will create a MPD widget using a MPD client written in Lua (that doesn't use mpc/netcat/telnet).

Contents

Install Luasocket

First, you'll need to install luasocket.

FreeBSD users:

cd /usr/ports/net/luasocket && make install clean

Gentoo users, you can add the akoya overlay (from geekounet). This overlay has a lot of nice other awesome related ebuilds (like wicked for example):

layman -a akoya

Then emerge luasocket (NOTE: it's a ~arch ebuild, you'll need to keyword it if you have a stable profile, see the Gentoo documentation).

Debian users, as root :

apt-get install liblua5.1-socket2

Arch users either, as root :

pacman -S luasocket

or, as user :

yaourt -S luasocket

  • add here your distro package for luasocket :)

The MPD lua Library

Save the lua MPD lib (lib/mpd.lua) to $HOME/.config/awesome/lib/mpd.lua, you can get it here

How to use

Now we can bind key and get MPD infos. All changes are now in your main configuration file ($HOME/.config/awesome/rc.lua). First, require the mpd.lua file:

require("lib/mpd")

Then you can get infos from MPD and display them in a widget. Here a quick demo:

mpc = mpd.new() -- will use default values, localhost:6600 without password
mpc:send("status") -- get the MPD status. You can send any mpd message, and it will return a table with key:value given by mpd
mpc:stop() -- stop MPD. shortcut for mpc:send("play")
mpc:next() -- next song, shortcut for mpc:send("next")
mpc:toogle_repeat()
mpc:toggle_play()
-- etc. see mpd.lua or config.

handy lua code

I'm using a table to handle more than one MPD server:

mpds = {
  add      = function (self, c) table.insert(self.list, c) end,
  current  = function (self) return self.list[self.current_idx] end,
  next     = function (self) self.current_idx = math.fmod(self.current_idx, #self.list) + 1; self.last_songid = nil end,
  previous = function (self) self.current_idx = math.fmod(self.current_idx, #self.list) - 1; self.last_songid = nil end,
  -- private stuff
  list        = { mpd.new() }, -- one (local) server by default
  current_idx = 1
}

Then, you can add MPD server:

mpds:add(mpd.new { hostname="bla" })

Switch the current MPD server:

mpds:next()
mpds:previous()

And use mpds:current() in your key bindings and widgets.

Full example

see my config here for a complete example of use. Check also other users config files.

Personal tools