2018-01-07 01:06:30 +01:00
|
|
|
-------------------------------------------------
|
|
|
|
-- Spotify Widget for Awesome Window Manager
|
|
|
|
-- Shows currently playing song on Spotify for Linux client
|
|
|
|
-- More details could be found here:
|
|
|
|
-- https://github.com/streetturtle/awesome-wm-widgets/tree/master/spotify-widget
|
|
|
|
|
|
|
|
-- @author Pavel Makhov
|
|
|
|
-- @copyright 2018 Pavel Makhov
|
|
|
|
-------------------------------------------------
|
|
|
|
|
2017-01-23 17:01:16 +01:00
|
|
|
local awful = require("awful")
|
2017-10-11 20:22:51 +02:00
|
|
|
local wibox = require("wibox")
|
2017-01-26 04:47:50 +01:00
|
|
|
local watch = require("awful.widget.watch")
|
2017-01-23 17:01:16 +01:00
|
|
|
|
2017-12-02 19:50:14 +01:00
|
|
|
local GET_SPOTIFY_STATUS_CMD = 'sp status'
|
2017-10-11 20:22:51 +02:00
|
|
|
local GET_CURRENT_SONG_CMD = 'sp current-oneline'
|
|
|
|
local PATH_TO_ICONS = "/usr/share/icons/Arc"
|
2017-01-23 17:01:16 +01:00
|
|
|
|
2018-01-07 01:06:30 +01:00
|
|
|
local spotify_widget = wibox.widget {
|
2017-06-17 18:48:03 +02:00
|
|
|
{
|
|
|
|
id = "icon",
|
|
|
|
widget = wibox.widget.imagebox,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id = 'current_song',
|
|
|
|
widget = wibox.widget.textbox,
|
|
|
|
font = 'Play 9'
|
|
|
|
},
|
2018-01-07 01:06:30 +01:00
|
|
|
layout = wibox.layout.align.horizontal,
|
|
|
|
set_status = function(self, is_playing)
|
2018-10-05 17:21:57 +02:00
|
|
|
self.icon.image = PATH_TO_ICONS ..
|
|
|
|
(is_playing and "/actions/24/player_play.png"
|
|
|
|
or "/actions/24/player_pause.png")
|
2017-06-17 18:48:03 +02:00
|
|
|
end,
|
|
|
|
set_text = function(self, path)
|
2018-01-07 01:06:30 +01:00
|
|
|
self.current_song.markup = path
|
2017-06-05 22:56:06 +02:00
|
|
|
end,
|
2017-06-17 18:48:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
local update_widget_icon = function(widget, stdout, _, _, _)
|
|
|
|
stdout = string.gsub(stdout, "\n", "")
|
2018-10-05 17:21:57 +02:00
|
|
|
widget:set_status(stdout == 'Playing' and true or false)
|
2017-06-17 18:48:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local update_widget_text = function(widget, stdout, _, _, _)
|
|
|
|
if string.find(stdout, 'Error: Spotify is not running.') ~= nil then
|
|
|
|
widget:set_text('')
|
|
|
|
widget:set_visible(false)
|
|
|
|
else
|
|
|
|
widget:set_text(stdout)
|
|
|
|
widget:set_visible(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-11 20:22:51 +02:00
|
|
|
watch(GET_SPOTIFY_STATUS_CMD, 1, update_widget_icon, spotify_widget)
|
|
|
|
watch(GET_CURRENT_SONG_CMD, 1, update_widget_text, spotify_widget)
|
2017-06-04 02:43:03 +02:00
|
|
|
|
2018-01-07 01:06:30 +01:00
|
|
|
--- Adds mouse controls to the widget:
|
2017-06-09 17:33:37 +02:00
|
|
|
-- - left click - play/pause
|
|
|
|
-- - scroll up - play next song
|
2018-01-07 01:06:30 +01:00
|
|
|
-- - scroll down - play previous song
|
2017-06-17 18:48:03 +02:00
|
|
|
spotify_widget:connect_signal("button::press", function(_, _, _, button)
|
|
|
|
if (button == 1) then awful.spawn("sp play", false) -- left click
|
|
|
|
elseif (button == 4) then awful.spawn("sp next", false) -- scroll up
|
|
|
|
elseif (button == 5) then awful.spawn("sp prev", false) -- scroll down
|
2017-06-09 17:33:37 +02:00
|
|
|
end
|
2017-10-11 20:22:51 +02:00
|
|
|
awful.spawn.easy_async(GET_SPOTIFY_STATUS_CMD, function(stdout, stderr, exitreason, exitcode)
|
2017-06-17 18:48:03 +02:00
|
|
|
update_widget_icon(spotify_widget, stdout, stderr, exitreason, exitcode)
|
|
|
|
end)
|
2018-01-07 01:06:30 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
return spotify_widget
|