al-emms-notification.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ;;; al-emms-notification.el --- EMMS notifications -*- lexical-binding: t -*-
  2. ;; Copyright © 2013-2016 Alex Kost
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;;
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (require 'emms)
  17. (require 'emms-state)
  18. (require 'notifications)
  19. (require 'xml)
  20. (require 'al-emms)
  21. (require 'al-emms-mpv)
  22. (defvar al/emms-notification-artist-format "%s")
  23. (defvar al/emms-notification-title-format "%s")
  24. (defvar al/emms-notification-album-format "%s")
  25. (defvar al/emms-notification-year-format "%s")
  26. (defun al/emms-notification-format (value &optional format-str)
  27. "Return VALUE (string or nil) formatted with FORMAT-STR."
  28. (when (stringp value)
  29. (let ((val (xml-escape-string value)))
  30. (if format-str
  31. (format format-str val)
  32. val))))
  33. (defun al/emms-notification-track-name (&optional artist title album
  34. year track-number)
  35. "Return track description suitable for (dunst) notifications."
  36. (let ((artist (al/emms-notification-format
  37. artist al/emms-notification-artist-format))
  38. (title (al/emms-notification-format
  39. title al/emms-notification-title-format))
  40. (album (al/emms-notification-format
  41. album al/emms-notification-album-format))
  42. (year (al/emms-notification-format
  43. year al/emms-notification-year-format)))
  44. (let ((title (if track-number
  45. (format "%02d. %s"
  46. (string-to-number track-number) title)
  47. title))
  48. (album (cond ((and album year)
  49. (format "%s – %s" year album))
  50. (year (format "%s" year))
  51. (album (format "%s" album)))))
  52. (mapconcat #'identity
  53. (delq nil (list artist title album))
  54. "\n"))))
  55. (defun al/emms-notification-radio-description (metadata)
  56. "Return description of TRACK ."
  57. (cl-multiple-value-bind (artist title)
  58. (al/emms-split-track-name (cdr (assq 'icy-title metadata)))
  59. (al/emms-notification-track-name
  60. artist title (cdr (assq 'icy-name metadata)))))
  61. (defun al/emms-notification-track-description (track)
  62. "Return description of TRACK suitable for (dunst) notifications."
  63. (al/emms-notification-track-name
  64. (emms-track-get track 'info-artist)
  65. (or (emms-track-get track 'info-title)
  66. (emms-track-simple-description track))
  67. (emms-track-get track 'info-album)
  68. (emms-track-get track 'info-year)
  69. (emms-track-get track 'info-tracknumber)))
  70. (defun al/emms-notification-notify (state time string)
  71. "Notify about STATE, TIME and STRING using `notifications-notify'."
  72. (notifications-notify
  73. :app-name "emms"
  74. :title (format "%s %s" state time)
  75. :body string))
  76. ;;;###autoload
  77. (defun al/emms-notify ()
  78. "Notify about the current track using `notifications-notify'."
  79. (interactive)
  80. (let ((track (emms-playlist-current-selected-track)))
  81. (when track
  82. (let ((state (emms-state))
  83. (time (concat emms-state-current-playing-time
  84. (and emms-state-total-playing-time
  85. (concat " ("
  86. emms-state-total-playing-time
  87. ")")))))
  88. (if (al/emms-mpv-playing-radio?)
  89. (al/emms-mpv-call-with-metadata
  90. (lambda (data)
  91. (al/emms-notification-notify
  92. state time
  93. (al/emms-notification-radio-description data))))
  94. (al/emms-notification-notify
  95. state time
  96. (al/emms-notification-track-description track)))))))
  97. ;;;###autoload
  98. (define-minor-mode al/emms-notification-mode
  99. "Minor mode for EMMS notifications."
  100. :global t
  101. :init-value nil
  102. (if al/emms-notification-mode
  103. (add-hook 'emms-player-started-hook 'al/emms-notify t)
  104. (remove-hook 'emms-player-started-hook 'al/emms-notify)))
  105. (provide 'al-emms-notification)
  106. ;;; al-emms-notification.el ends here