media_players.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. --[[
  2. Copyright 2017 Stefano Mazzucco <stefano AT curso DOT re>
  3. This program is free software: you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation, either version 3 of the License, or (at your option) any later
  6. version.
  7. This program is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
  13. ]]
  14. local table = table
  15. local client = client -- luacheck: ignore
  16. local naughty = require("naughty")
  17. -- Various helpers
  18. local helpers = require("helpers")
  19. local proxy = require("dbus_proxy")
  20. local Proxy = proxy.Proxy
  21. local Bus = proxy.Bus
  22. local MediaPlayer = require("media_player")
  23. local quodlibet = MediaPlayer:new("quodlibet")
  24. local vlc = MediaPlayer:new("vlc")
  25. local function get_info(player)
  26. -- For info about key/value pairs
  27. -- https://www.freedesktop.org/wiki/Specifications/mpris-spec/metadata/
  28. local info = player:info()
  29. local position = player:position_as_str()
  30. if info.length then
  31. position = position .. " / " .. info.length
  32. end
  33. return {info.title,
  34. helpers.concat_non_nil(
  35. {info.artists,
  36. info.album,
  37. info.year,
  38. position},
  39. "\n")
  40. }
  41. end
  42. local function make_notify(player)
  43. local notification = nil
  44. local function notify()
  45. local opacity = 0.87
  46. if notification then
  47. naughty.destroy(
  48. notification,
  49. naughty.notificationClosedReason.dismissedByCommand
  50. )
  51. end
  52. if player.is_connected then
  53. local info = get_info(player)
  54. local title, text = table.unpack(info)
  55. notification = naughty.notify({
  56. title = title,
  57. text = text,
  58. icon = player.Metadata["mpris:artUrl"],
  59. icon_size = 64,
  60. opacity = opacity})
  61. else
  62. notification = naughty.notify({
  63. title = player.name,
  64. text = "not available",
  65. opacity = opacity})
  66. end
  67. end
  68. return notify
  69. end
  70. local function make_wrapper(player, method)
  71. return function()
  72. if player.is_connected then
  73. player[method](player)
  74. else
  75. local opacity = 0.87
  76. naughty.notify({
  77. title = player.name,
  78. text = "not available",
  79. opacity = opacity})
  80. end
  81. end
  82. end
  83. quodlibet.notify = make_notify(quodlibet)
  84. quodlibet.play_pause = make_wrapper(quodlibet, "PlayPause")
  85. quodlibet.stop = make_wrapper(quodlibet, "Stop")
  86. quodlibet.quit = make_wrapper(quodlibet, "Quit")
  87. quodlibet.previous = make_wrapper(quodlibet, "Previous")
  88. quodlibet.next = make_wrapper(quodlibet, "Next")
  89. vlc.notify = make_notify(vlc)
  90. vlc.play_pause = make_wrapper(vlc, "PlayPause")
  91. vlc.stop = make_wrapper(vlc, "Stop")
  92. vlc.quit = make_wrapper(vlc, "Quit")
  93. vlc.previous = make_wrapper(vlc, "Previous")
  94. vlc.next = make_wrapper(vlc, "Next")
  95. -- Add notifications on song change.
  96. do
  97. local dbus = Proxy:new(
  98. {
  99. bus = Bus.SESSION,
  100. name = "org.freedesktop.DBus",
  101. path= "/org/freedesktop/DBus",
  102. interface = "org.freedesktop.DBus"
  103. }
  104. )
  105. dbus:connect_signal(
  106. function(_, name)
  107. local PREVIOUS_TITLE
  108. if name == quodlibet.name and quodlibet.is_connected then
  109. quodlibet:on_properties_changed(function (_, changed)
  110. if changed.Metadata then
  111. local current_title = changed.Metadata["xesam:title"]
  112. if current_title ~= PREVIOUS_TITLE then
  113. -- Avoids firing up the notification twice.
  114. quodlibet.notify()
  115. PREVIOUS_TITLE = current_title
  116. end
  117. end
  118. end)
  119. end
  120. end,
  121. "NameOwnerChanged")
  122. end
  123. return {quodlibet = quodlibet,
  124. vlc = vlc}