summaryrefslogtreecommitdiff
path: root/mpdarc-widget/mpdarc.lua
blob: a6a8e759814b9864446cf3d789add68c270b5a33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
-------------------------------------------------
-- mpd Arc Widget for Awesome Window Manager
-- Modelled after Pavel Makhov's work

-- @author Raphaël Fournier-S'niehotta
-- @copyright 2018 Raphaël Fournier-S'niehotta
-------------------------------------------------

local awful = require("awful")
local beautiful = require("beautiful")
local spawn = require("awful.spawn")
local watch = require("awful.widget.watch")
local wibox = require("wibox")
local naughty = require("naughty")

local GET_MPD_CMD = "mpc status"
local TOGGLE_MPD_CMD = "mpc toggle"
local PAUSE_MPD_CMD = "mpc pause"
local STOP_MPD_CMD = "mpc stop"
local NEXT_MPD_CMD = "mpc next"
local PREV_MPD_CMD = "mpc prev"

local PATH_TO_ICONS = "/usr/share/icons/Arc"
local PAUSE_ICON_NAME = PATH_TO_ICONS .. "/actions/24/player_pause.png"
local PLAY_ICON_NAME = PATH_TO_ICONS .. "/actions/24/player_play.png"
local STOP_ICON_NAME = PATH_TO_ICONS .. "/actions/24/player_stop.png"

local icon = wibox.widget {
        id = "icon",
        widget = wibox.widget.imagebox,
        image = PLAY_ICON_NAME
    }
local mirrored_icon = wibox.container.mirror(icon, { horizontal = true })

local mpdarc = wibox.widget {
    mirrored_icon,
    max_value = 1,
    value = 0.75,
    thickness = 2,
    start_angle = 4.71238898, -- 2pi*3/4
    forced_height = 32,
    forced_width = 32,
    rounded_edge = true,
    bg = "#ffffff11",
    paddings = 0,
    widget = wibox.container.arcchart
}

local mpdarc_icon_widget = wibox.container.mirror(mpdarc, { horizontal = true })
local mpdarc_current_song_widget = wibox.widget {
    id = 'current_song',
    widget = wibox.widget.textbox,
    font = beautiful.font2 or beautiful.font
}

local update_graphic = function(widget, stdout, _, _, _)
    local current_song = string.gmatch(stdout, "[^\r\n]+")()
    stdout = string.gsub(stdout, "\n", "")
    local mpdpercent = string.match(stdout, "(%d%d)%%")
    local mpdstatus = string.match(stdout, "%[(%a+)%]")
    if mpdstatus == "playing" then
      icon.image = PLAY_ICON_NAME
      widget.colors = { beautiful.widget_main_color }
      widget.value = tonumber((100-mpdpercent)/100)
      mpdarc_current_song_widget.markup = current_song
    elseif mpdstatus == "paused" then
      icon.image = PAUSE_ICON_NAME
      widget.colors = { beautiful.widget_main_color }
      widget.value = tonumber(mpdpercent/100)
      mpdarc_current_song_widget.markup = current_song
    else
      icon.image = STOP_ICON_NAME
      if string.len(stdout) == 0 then -- MPD is not running
        mpdarc_current_song_widget.markup = "MPD is not running"
      else
        widget.colors = { beautiful.widget_red }
        mpdarc_current_song_widget.markup = ""
      end
    end
end

mpdarc:connect_signal("button::press", function(_, _, _, button)
    if (button == 1) then awful.spawn(TOGGLE_MPD_CMD, false)      -- left click
    elseif (button == 2) then awful.spawn(STOP_MPD_CMD, false)
    elseif (button == 3) then awful.spawn(PAUSE_MPD_CMD, false)
    elseif (button == 4) then awful.spawn(NEXT_MPD_CMD, false)  -- scroll up
    elseif (button == 5) then awful.spawn(PREV_MPD_CMD, false)  -- scroll down
    end

    spawn.easy_async(GET_MPD_CMD, function(stdout, stderr, exitreason, exitcode)
        update_graphic(mpdarc, stdout, stderr, exitreason, exitcode)
    end)
end)

local notification
local function show_MPD_status()
    spawn.easy_async(GET_MPD_CMD,
        function(stdout, _, _, _)
            notification = naughty.notify {
                text = stdout,
                title = "MPD",
                timeout = 5,
                hover_timeout = 0.5,
                width = 600,
            }
        end)
end

mpdarc:connect_signal("mouse::enter", function() show_MPD_status() end)
mpdarc:connect_signal("mouse::leave", function() naughty.destroy(notification) end)

watch(GET_MPD_CMD, 1, update_graphic, mpdarc)

local mpdarc_widget = wibox.widget{
    mpdarc_icon_widget,
    mpdarc_current_song_widget,
    layout = wibox.layout.align.horizontal,
    }
return mpdarc_widget