]> git.rmz.io Git - dotfiles.git/blob - awesome/widgets/mpd_widget.lua
692e9a2ba8969a6a21355b2bf541182ca3629b48
[dotfiles.git] / awesome / widgets / mpd_widget.lua
1 local mpc = require("widgets/mpc")
2 local textbox = require("wibox.widget.textbox")
3 local timer = require("gears.timer")
4
5 mpd_widget = textbox()
6
7 local function update_widget()
8 local text = ""
9 if state == "pause" then
10 text = "|| "
11 end
12 text = text .. tostring(artist or "") .. " - " .. tostring(title or "")
13 mpd_widget.text = text
14 end
15
16 local connection
17 local function error_handler(err)
18 mpd_widget:set_text("Error: " .. tostring(err))
19 timer.start_new(10, function()
20 connection:send("ping")
21 end)
22 end
23
24 connection = mpc.new(nil, nil, nil, error_handler,
25 "status", function(_, result)
26 state = result.state
27 end,
28 "currentsong", function(_, result)
29 title, artist, file = result.title, result.artist, result.file
30 pcall(update_widget)
31 end
32 )
33
34 mpd_widget:buttons(gears.table.join(
35 awful.button({}, 1, function() connection:toggle_play() end)
36 , awful.button({}, 4, function() connection:change_volume(5) end)
37 , awful.button({}, 5, function() connection:change_volume(-5) end))
38 )
39
40 globalkeys = gears.table.join(globalkeys,
41 awful.key({ modkey }, "p", function() connection:toggle_play() end,
42 { description = "toogle play", group = "mpd" }),
43 awful.key({ modkey }, ",", function() connection:send("previous") end,
44 { description = "previous track", group = "mpd" }),
45 awful.key({ modkey }, ".", function() connection:send("next") end,
46 { description = "next track", group = "mpd" })
47 )
48 return mpd_widget