]> git.rmz.io Git - dotfiles.git/blobdiff - awesome/widgets/mpd_widget.lua
nvim: don't quit nvim from dashboard
[dotfiles.git] / awesome / widgets / mpd_widget.lua
index d5b72e9dd8716547f1ad4c56670737249adb4eaf..956fa4ca2d927afa44acb24daea9d775d042f316 100644 (file)
@@ -1,13 +1,10 @@
-local mpc = require("widgets/mpc")
-local textbox = require("wibox.widget.textbox")
-local gears = require("gears")
-local awful = require("awful")
+local awful   = require("awful")
+local gears   = require("gears")
+local mpc     = require("widgets/mpc")
 local naughty = require("naughty")
-local timer = require("gears.timer")
-local wibox = require("wibox")
-
---TODO remove
-local dbg = require("gears.debug")
+local textbox = require("wibox.widget.textbox")
+local timer   = require("gears.timer")
+local wibox   = require("wibox")
 
 local widget = {}
 widget._textbox = textbox()
@@ -16,6 +13,7 @@ widget._currentsong = {}
 widget.scroll = wibox.widget {
     layout = wibox.container.scroll.horizontal,
     max_size = 200,
+    extra_space = 10,
     step_function = wibox.container.scroll.step_functions.linear_increase,
     speed = 20,
     {
@@ -24,21 +22,26 @@ widget.scroll = wibox.widget {
 }
 
 function widget:update_widget()
+    if not self._currentsong then
+        self._textbox.text = ""
+        return
+    end
     local text = ""
     local artist = self._currentsong.artist
     local title = self._currentsong.title
     local file = self._currentsong.file
-    text = text .. tostring(artist or "") .. " - " .. tostring(title or "")
-    if not artist then
+    if artist and title then
+        text = string.format("%s - %s", artist, title)
+    elseif title or file then
         text = string.format("%s", title or file )
     else
-        text = string.format("%s - %s", artist, title)
+        text = "-"
     end
     self._textbox.text = text
 end
 
 local function sec_to_min(sec)
-    m, s = math.modf((sec or 0.0) / 60)
+    local m, s = math.modf((sec or 0.0) / 60)
     return string.format('%d:%02d', m, math.floor(s * 60))
 end
 
@@ -46,98 +49,150 @@ local function song_duration(elapsed, duration)
     return sec_to_min(elapsed) .. "/" .. sec_to_min(duration)
 end
 
-function widget:get_info(callback)
-    local status = nil
-    self._connection:send("status", function(_, s) status = s end)
+local dbg = require("gears.debug")
+function widget:get_albumart()
+    local art = self._albumart
+    -- dbg.dump(art)
+    -- dbg.dump(self._albumart.binary)
+    if art and art.binary then
+        local path = '/tmp/test.jpg'
+        local f = io.open(path, 'w')
+        f:write(art.binary)
+        f:close()
+        return path
+    else
+        return beautiful.mpd_default_album
+    end
+end
 
-    local song = nil
-    self._connection:send("currentsong", function(_, s) song = s end)
+function widget:get_info()
+    local status, song = self._status, self._currentsong
+    if not status.state then return nil end
+    if not status.song then return nil end
 
-    self._connection:send("ping", function()
+    local info = {}
+    info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
+    if not song.artist then
+        info.text = string.format("%s", song.title or song.file)
+    else
+        info.text = string.format("%s - %s", song.artist, song.title)
+    end
+    if song.album then
+        info.text = info.text .. "\n" .. tostring(song.album or "")
+    end
 
-        local info = {}
-        info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
-        info.text = tostring(song.artist or "") .. " - " .. tostring(song.title or "") .. "\n"
-            .. tostring(song.album or "")
-        callback(info)
-        return false
-    end)
+    return info
 end
 
 function widget:popup_show()
-    self:get_info(function(table)
-        if self._notification then return end
-        self._notification = naughty.notify(
-            { title = table.title
-            -- , icon = img
-            , text = table.text
-            , timeout = 0
-            , destroy = function() self._timer:stop() end
-            })
-        self._timer:start()
-    end)
+    if self._notification then return end
+
+    local table = self:get_info()
+    if not table then return end
+
+    self._timer:start()
+    self._notification = naughty.notify(
+        { title = table.title
+        , icon = self:get_albumart()
+        , icon_size = 64
+        , text = table.text
+        , timeout = 0
+        , destroy = function() self._timer:stop(); self._notification = nil end
+        })
 end
 
 function widget:popup_hide(delay)
+    local function destroy()
+        if self._hover then return end
+        if not self._notification then return end
+        naughty.destroy(self._notification)
+        self._notification = nil
+    end
+
+    if not delay then
+        destroy()
+        return
+    end
+
     if self._hide_timer and self._hide_timer.started then
+        self._hide_timer.timeout = delay
         self._hide_timer:again()
     else
         self._hide_timer = timer(
             { timeout = delay
             , autostart = true
             , single_shot = true
-            , callback = function()
-                if self._hover then return end
-                if not self._notification then return end
-                naughty.destroy(self._notification)
-                self._notification = nil
-                end
+            , callback = destroy
             })
     end
 end
 
 function widget:popup_update()
-    self:get_info(function(table)
+    if not self._notification then return end
+    self._connection:send("status", function(_, status)
+        self._status = status
+        local table = self:get_info()
         if not self._notification then return end
+        if not table then return end
         naughty.replace_text(self._notification, table.title, table.text)
     end)
 end
 
 function widget:popup_oneshot(timeout)
     if self._notification then
-        self:popup_update()
-        self:popup_hide(5)
+        self:popup_hide()
+        self:popup_show()
     else
         self:popup_show()
-        self:popup_hide(5)
     end
+    self:popup_hide(5)
 end
 
 function widget:error_handler(err)
     self._textbox:set_text("Error: " .. tostring(err))
-    timer.start_new(10, function()
-        self._connection:send("ping")
-    end)
+    self._status = {}
 end
 
 function widget:run()
+    self._status = {}
+
     self._connection = mpc.new(nil, nil, nil, function(err) self:error_handler(err) end,
-        "status", function(_, result)
-            if not self._status or result.state ~= self._status.state then 
-                self._status = result
-                self:update_widget()
-                self:popup_oneshot(5)
-            end
-        end,
-        "currentsong", function(_, result)
-            if not self._currentsong or result.id ~= self._currentsong.id then 
-                self._currentsong = result
-                self:update_widget()
-                self:popup_oneshot(5)
-            end
+        "player", function(conn)
+            conn:send("status", function(err, status)
+                local songchanged = self._status.songid ~= status.songid
+                local statechanged = self._status.state ~= status.state
+                self._status = status
+                if not songchanged then
+                    self:update_widget()
+                    if statechanged then
+                        self:popup_oneshot(5)
+                    end
+                    return
+                end
+
+                conn:send("currentsong", function(_, song)
+                    self._currentsong = song
+                    if not song then
+                        self:update_widget()
+                        return
+                    end
+                    conn:albumart(song.file, function(_, art)
+                        self._albumart = art
+                        self:update_widget()
+                        self:popup_oneshot(5)
+                    end)
+                end)
+            end)
         end
     )
 
+    self._keep_alive_timer = timer {
+      timeout = 1
+    , autostart = true
+    , callback = function() self._connection:connect() end
+    , call_now = true
+    }
+
     self._timer = timer({ timeout = 1 })
     self._timer:connect_signal("timeout", function() self:popup_update() end)
 
@@ -155,6 +210,11 @@ widget.scroll:buttons(gears.table.join(
 )
 
 globalkeys = gears.table.join(globalkeys,
+    --TODO headphone support
+    awful.key({ }, "XF86AudioPlay", function() widget._connection:play() end,
+              { description = "play", group = "mpd" }),
+    awful.key({ }, "XF86AudioPause", function() widget._connection:pause() end,
+              { description = "pause", group = "mpd" }),
     awful.key({ modkey }, "p", function() widget._connection:toggle_play() end,
               { description = "toogle play", group = "mpd" }),
     awful.key({ modkey }, "'", function() widget:popup_oneshot(5) end,