My configuration files for Debian/Ubuntu applications
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

136 linhas
6.0 KiB

  1. --[[
  2. Licensed under GNU General Public License v2
  3. * (c) 2013, Luca CPZ
  4. * (c) 2010, Adrian C. <anrxc@sysphere.org>
  5. --]]
  6. local helpers = require("lain.helpers")
  7. local shell = require("awful.util").shell
  8. local escape_f = require("awful.util").escape
  9. local focused = require("awful.screen").focused
  10. local naughty = require("naughty")
  11. local wibox = require("wibox")
  12. local os = os
  13. local string = string
  14. -- MPD infos
  15. -- lain.widget.mpd
  16. local function factory(args)
  17. args = args or {}
  18. local mpd = { widget = args.widget or wibox.widget.textbox() }
  19. local timeout = args.timeout or 2
  20. local password = (args.password and #args.password > 0 and string.format("password %s\\n", args.password)) or ""
  21. local host = args.host or os.getenv("MPD_HOST") or "127.0.0.1"
  22. local port = args.port or os.getenv("MPD_PORT") or "6600"
  23. local music_dir = args.music_dir or os.getenv("HOME") .. "/Music"
  24. local cover_pattern = args.cover_pattern or "*\\.(jpg|jpeg|png|gif)$"
  25. local cover_size = args.cover_size or 100
  26. local default_art = args.default_art
  27. local notify = args.notify or "on"
  28. local followtag = args.followtag or false
  29. local settings = args.settings or function() end
  30. local mpdh = string.format("telnet://%s:%s", host, port)
  31. local echo = string.format("printf \"%sstatus\\ncurrentsong\\nclose\\n\"", password)
  32. local cmd = string.format("%s | curl --connect-timeout 1 -fsm 3 %s", echo, mpdh)
  33. mpd_notification_preset = { title = "Now playing", timeout = 6 }
  34. helpers.set_map("current mpd track", nil)
  35. function mpd.update()
  36. helpers.async({ shell, "-c", cmd }, function(f)
  37. mpd_now = {
  38. random_mode = false,
  39. single_mode = false,
  40. repeat_mode = false,
  41. consume_mode = false,
  42. pls_pos = "N/A",
  43. pls_len = "N/A",
  44. state = "N/A",
  45. file = "N/A",
  46. name = "N/A",
  47. artist = "N/A",
  48. title = "N/A",
  49. album = "N/A",
  50. genre = "N/A",
  51. track = "N/A",
  52. date = "N/A",
  53. time = "N/A",
  54. elapsed = "N/A",
  55. volume = "N/A"
  56. }
  57. for line in string.gmatch(f, "[^\n]+") do
  58. for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
  59. if k == "state" then mpd_now.state = v
  60. elseif k == "file" then mpd_now.file = v
  61. elseif k == "Name" then mpd_now.name = escape_f(v)
  62. elseif k == "Artist" then mpd_now.artist = escape_f(v)
  63. elseif k == "Title" then mpd_now.title = escape_f(v)
  64. elseif k == "Album" then mpd_now.album = escape_f(v)
  65. elseif k == "Genre" then mpd_now.genre = escape_f(v)
  66. elseif k == "Track" then mpd_now.track = escape_f(v)
  67. elseif k == "Date" then mpd_now.date = escape_f(v)
  68. elseif k == "Time" then mpd_now.time = v
  69. elseif k == "elapsed" then mpd_now.elapsed = string.match(v, "%d+")
  70. elseif k == "song" then mpd_now.pls_pos = v
  71. elseif k == "playlistlength" then mpd_now.pls_len = v
  72. elseif k == "repeat" then mpd_now.repeat_mode = v ~= "0"
  73. elseif k == "single" then mpd_now.single_mode = v ~= "0"
  74. elseif k == "random" then mpd_now.random_mode = v ~= "0"
  75. elseif k == "consume" then mpd_now.consume_mode = v ~= "0"
  76. elseif k == "volume" then mpd_now.volume = v
  77. end
  78. end
  79. end
  80. mpd_notification_preset.text = string.format("%s (%s) - %s\n%s", mpd_now.artist,
  81. mpd_now.album, mpd_now.date, mpd_now.title)
  82. widget = mpd.widget
  83. settings()
  84. if mpd_now.state == "play" then
  85. if notify == "on" and mpd_now.title ~= helpers.get_map("current mpd track") then
  86. helpers.set_map("current mpd track", mpd_now.title)
  87. if followtag then mpd_notification_preset.screen = focused() end
  88. local common = {
  89. preset = mpd_notification_preset,
  90. icon = default_art,
  91. icon_size = cover_size,
  92. replaces_id = mpd.id
  93. }
  94. if not string.match(mpd_now.file, "http.*://") then -- local file instead of http stream
  95. local path = string.format("%s/%s", music_dir, string.match(mpd_now.file, ".*/"))
  96. local cover = string.format("find '%s' -maxdepth 1 -type f | egrep -i -m1 '%s'",
  97. path:gsub("'", "'\\''"), cover_pattern)
  98. helpers.async({ shell, "-c", cover }, function(current_icon)
  99. common.icon = current_icon:gsub("\n", "")
  100. if #common.icon == 0 then common.icon = nil end
  101. mpd.id = naughty.notify(common).id
  102. end)
  103. else
  104. mpd.id = naughty.notify(common).id
  105. end
  106. end
  107. elseif mpd_now.state ~= "pause" then
  108. helpers.set_map("current mpd track", nil)
  109. end
  110. end)
  111. end
  112. mpd.timer = helpers.newtimer("mpd", timeout, mpd.update, true, true)
  113. return mpd
  114. end
  115. return factory