summaryrefslogtreecommitdiff
path: root/mpris-widget
diff options
context:
space:
mode:
Diffstat (limited to 'mpris-widget')
-rw-r--r--mpris-widget/README.md26
-rw-r--r--mpris-widget/init.lua131
2 files changed, 157 insertions, 0 deletions
diff --git a/mpris-widget/README.md b/mpris-widget/README.md
new file mode 100644
index 0000000..317385f
--- /dev/null
+++ b/mpris-widget/README.md
@@ -0,0 +1,26 @@
+# MPRIS Widget
+
+Music Player Info widget cy @mgabs
+
+# Prerequisite
+
+Install `playerctl` (mpris implementation), should be available in repo, e.g for Ubuntu:
+
+```bash
+sudo apt-get install playerctl
+```
+
+## Installation
+
+To use this widget clone repo under **~/.config/awesome/** and then add it in **rc.lua**:
+
+```lua
+local mpris_widget = require("awesome-wm-widgets.mpris-widget")
+...
+s.mytasklist, -- Middle widget
+ { -- Right widgets
+ layout = wibox.layout.fixed.horizontal,
+ ...
+ mpris_widget,
+ ...
+```
diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua
new file mode 100644
index 0000000..c9b2389
--- /dev/null
+++ b/mpris-widget/init.lua
@@ -0,0 +1,131 @@
+-------------------------------------------------
+-- mpris based Arc Widget for Awesome Window Manager
+-- Modelled after Pavel Makhov's work
+-- @author Mohammed Gaber
+-- requires - playerctl
+-- @copyright 2020
+-------------------------------------------------
+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 =
+ "playerctl -f '{{lc(status)}};{{xesam:artist}};{{xesam:title}}' metadata"
+
+local TOGGLE_MPD_CMD = "playerctl play-pause"
+local PAUSE_MPD_CMD = "playerctl pause"
+local STOP_MPD_CMD = "playerctl stop"
+local NEXT_MPD_CMD = "playerctl next"
+local PREV_MPD_CMD = "playerctl 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 LIBRARY_ICON_NAME = PATH_TO_ICONS .. "/actions/24/music-library.png"
+
+-- retriving song info
+current_song, artist = nil, nil
+
+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,
+ thickness = 2,
+ start_angle = 4.71238898, -- 2pi*3/4
+ forced_height = 24,
+ forced_width = 24,
+ 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 = 'Play 10'
+}
+
+local update_graphic = function(widget, stdout, _, _, _)
+ mpdstatus, artist, current_song = stdout:match("(%w+)%;+(.-)%;(.*)")
+ if current_song ~= nil then
+ if current_song.len == 18 then
+ current_song = string.sub(current_song, 0, 9) .. ".."
+ end
+ end
+
+ if mpdstatus == "playing" then
+ icon.image = PLAY_ICON_NAME
+ widget.colors = {beautiful.widget_main_color}
+ mpdarc_current_song_widget.markup = current_song
+ elseif mpdstatus == "paused" then
+ icon.image = PAUSE_ICON_NAME
+ widget.colors = {beautiful.widget_main_color}
+ mpdarc_current_song_widget.markup = current_song
+ elseif mpdstatus == "stopped" then
+ icon.image = STOP_ICON_NAME
+ mpdarc_current_song_widget.markup = ""
+ else -- no player is running
+ icon.image = LIBRARY_ICON_NAME
+ mpdarc_current_song_widget.markup = ""
+ widget.colors = {beautiful.widget_red}
+ 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
+function show_MPD_status()
+ spawn.easy_async(GET_MPD_CMD, function(stdout, _, _, _)
+ notification = naughty.notify {
+ text = current_song .. " by " .. artist,
+ title = "Now Playing",
+ timeout = 5,
+ hover_timeout = 0.5,
+ width = 600
+ }
+ end)
+end
+
+mpdarc:connect_signal("mouse::enter", function()
+ if current_song ~= nil and artist ~= nil then show_MPD_status() end
+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