mpdarc.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. -------------------------------------------------
  2. -- mpd Arc Widget for Awesome Window Manager
  3. -- Modelled after Pavel Makhov's work
  4. -- @author Raphaël Fournier-S'niehotta
  5. -- @copyright 2018 Raphaël Fournier-S'niehotta
  6. -------------------------------------------------
  7. local awful = require("awful")
  8. local beautiful = require("beautiful")
  9. local spawn = require("awful.spawn")
  10. local watch = require("awful.widget.watch")
  11. local wibox = require("wibox")
  12. local naughty = require("naughty")
  13. local GET_MPD_CMD = "mpc status"
  14. local TOGGLE_MPD_CMD = "mpc toggle"
  15. local PAUSE_MPD_CMD = "mpc pause"
  16. local STOP_MPD_CMD = "mpc stop"
  17. local NEXT_MPD_CMD = "mpc next"
  18. local PREV_MPD_CMD = "mpc prev"
  19. local PATH_TO_ICONS = "/usr/share/icons/Arc"
  20. local PAUSE_ICON_NAME = PATH_TO_ICONS .. "/actions/24/player_pause.png"
  21. local PLAY_ICON_NAME = PATH_TO_ICONS .. "/actions/24/player_play.png"
  22. local STOP_ICON_NAME = PATH_TO_ICONS .. "/actions/24/player_stop.png"
  23. local icon = wibox.widget {
  24. id = "icon",
  25. widget = wibox.widget.imagebox,
  26. image = PLAY_ICON_NAME
  27. }
  28. local mirrored_icon = wibox.container.mirror(icon, { horizontal = true })
  29. local mpdarc = wibox.widget {
  30. mirrored_icon,
  31. max_value = 1,
  32. value = 0.75,
  33. thickness = 2,
  34. start_angle = 4.71238898, -- 2pi*3/4
  35. forced_height = 32,
  36. forced_width = 32,
  37. rounded_edge = true,
  38. bg = "#ffffff11",
  39. paddings = 0,
  40. widget = wibox.container.arcchart
  41. }
  42. local mpdarc_icon_widget = wibox.container.mirror(mpdarc, { horizontal = true })
  43. local mpdarc_current_song_widget = wibox.widget {
  44. id = 'current_song',
  45. widget = wibox.widget.textbox,
  46. font = 'Play 9'
  47. }
  48. local update_graphic = function(widget, stdout, _, _, _)
  49. local current_song = string.gmatch(stdout, "[^\r\n]+")()
  50. stdout = string.gsub(stdout, "\n", "")
  51. local mpdpercent = string.match(stdout, "(%d%d)%%")
  52. local mpdstatus = string.match(stdout, "%[(%a+)%]")
  53. if mpdstatus == "playing" then
  54. icon.image = PLAY_ICON_NAME
  55. widget.colors = { beautiful.widget_main_color }
  56. widget.value = tonumber((100-mpdpercent)/100)
  57. mpdarc_current_song_widget.markup = current_song
  58. elseif mpdstatus == "paused" then
  59. icon.image = PAUSE_ICON_NAME
  60. widget.colors = { beautiful.widget_main_color }
  61. widget.value = tonumber(mpdpercent/100)
  62. mpdarc_current_song_widget.markup = current_song
  63. else
  64. icon.image = STOP_ICON_NAME
  65. if string.len(stdout) == 0 then -- MPD is not running
  66. mpdarc_current_song_widget.markup = "MPD is not running"
  67. else
  68. widget.colors = { beautiful.widget_red }
  69. mpdarc_current_song_widget.markup = ""
  70. end
  71. end
  72. end
  73. mpdarc:connect_signal("button::press", function(_, _, _, button)
  74. if (button == 1) then awful.spawn(TOGGLE_MPD_CMD, false) -- left click
  75. elseif (button == 2) then awful.spawn(STOP_MPD_CMD, false)
  76. elseif (button == 3) then awful.spawn(PAUSE_MPD_CMD, false)
  77. elseif (button == 4) then awful.spawn(NEXT_MPD_CMD, false) -- scroll up
  78. elseif (button == 5) then awful.spawn(PREV_MPD_CMD, false) -- scroll down
  79. end
  80. spawn.easy_async(GET_MPD_CMD, function(stdout, stderr, exitreason, exitcode)
  81. update_graphic(mpdarc, stdout, stderr, exitreason, exitcode)
  82. end)
  83. end)
  84. local notification
  85. local function show_MPD_status()
  86. spawn.easy_async(GET_MPD_CMD,
  87. function(stdout, _, _, _)
  88. notification = naughty.notify {
  89. text = stdout,
  90. title = "MPD",
  91. timeout = 5,
  92. hover_timeout = 0.5,
  93. width = 600,
  94. }
  95. end)
  96. end
  97. mpdarc:connect_signal("mouse::enter", function() show_MPD_status() end)
  98. mpdarc:connect_signal("mouse::leave", function() naughty.destroy(notification) end)
  99. watch(GET_MPD_CMD, 1, update_graphic, mpdarc)
  100. local mpdarc_widget = wibox.widget{
  101. mpdarc_icon_widget,
  102. mpdarc_current_song_widget,
  103. layout = wibox.layout.align.horizontal,
  104. }
  105. return mpdarc_widget