summaryrefslogtreecommitdiffhomepage
path: root/media/jao-mpris.el
blob: 195ffceb82bbc4c49b1854004ae8b8c3fc3cd092 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
;;; jao-mpris.el --- mpris players control           -*- lexical-binding: t; -*-

;; Copyright (C) 2020  jao

;; Author: jao <mail@jao.io>
;; Keywords: multimedia

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; controlling and showing info on mpris players

;;; Code:

(require 'dbus)
(require 'jao-minibuffer)
(require 'jao-emms-info-track)

(defun jao-mpris--playerctl (&rest args)
  (shell-command-to-string (format "playerctl %s"
                                   (mapconcat #'shell-quote-argument args " "))))

(defmacro jao-playerctl--def (name &rest args)
  `(defun ,name () (interactive) (jao-mpris--playerctl ,@args)))

(jao-playerctl--def jao-mpris-play-pause "play-pause")
(jao-playerctl--def jao-mpris-next "next")
(jao-playerctl--def jao-mpris-previous "previous")

(defun jao-playerctl--status (&optional sep)
  (let* ((sep (or sep " ||| "))
         (fmt (mapconcat 'identity
                         '("{{status}}"
                           "{{xesam:trackNumber}}"
                           "{{title}}"
                           "{{artist}}"
                           "{{album}}"
                           "{{duration(mpris:length)}}")
                         sep))
         (m (jao-mpris--playerctl "metadata" "--format" fmt)))
    (split-string (car (split-string m "\n")) sep)))

;;;###autoload
(defun jao-mpris-status-times ()
  (interactive)
  (let ((m (jao-mpris--playerctl "metadata" "--format"
                                 (concat "{{duration(position)}}/"
                                         "{{duration(mpris:length)}}"))))
    (jao-notify (string-trim m) "Playing")))

;;;###autoload
(defun jao-mpris-show-osd ()
  (interactive)
  (jao-notify jao-spotify-track-string))

;;;###autoload
(defun jao-mpris-artist-title ()
  (when-let (s (jao-mpris--playerctl "metadata"
                                     "--format"
                                     "{{artist}}|||{{title}}"))
    (let ((s (split-string (string-trim s) "|||")))
      (cons (car s) (cadr s)))))

(defvar jao-mpris-track-string "")

(defun jao-mpris--track (&rest info)
  (let ((cs (or info (jao-playerctl--status))))
    (if (string= "Playing" (or (elt cs 0) ""))
        (let* ((track (format "%s" (elt cs 1)))
               (title (elt cs 2))
               (artist (elt cs 3))
               (album (elt cs 4))
               (length (elt cs 5))
               (duration (cond ((stringp length) length)
                               ((numberp length)
                                (jao-emms--fmt-time (/ length 1e6) "")))))
          (setq jao-mpris-track-string
                (format "♪ %s %s %s%s%s"
                        (jao--put-face track 'jao-emms-font-lock-track)
                        (jao--put-face title 'jao-emms-font-lock-title)
                        (jao--put-face artist 'jao-emms-font-lock-artist)
                        (jao--put-face (if album (format " (%s)" album) "")
                                       'jao-emms-font-lock-album)
                        (if duration (format " [%s]" duration) ""))))
      (setq jao-mpris-track-string "")))
  (jao-minibuffer-refresh))

(jao-minibuffer-add-variable 'jao-mpris-track-string t)

(defun jao-mpris--handler (iname properties &rest args)
  (when properties
    (let ((st (caadr (assoc "PlaybackStatus" properties)))
          (md (caadr (assoc "Metadata" properties))))
      (cond ((and st (not (string= "Playing" st)))
             (setq jao-spotify-track-string ""))
            (md (let ((tno (caadr (assoc "xesam:trackNumber" md)))
                      (tlt (caadr (assoc "xesam:title" md)))
                      (art (caaadr (assoc "xesam:artist" md)))
                      (alb (caadr (assoc "xesam:album" md)))
                      (len (caadr (assoc "mpris:length" md))))
                  (setq jao-debug md)
                  (jao-mpris--track "Playing" tno tlt art alb len)))))))


;;;###autoload
(defun jao-mpris-minibuffer-register (name &optional bus)
  (dbus-register-signal (or bus :session)
                        name
                        "/org/mpris/MediaPlayer2"
                        "org.freedesktop.DBus.Properties"
                        "PropertiesChanged"
                        'jao-mpris--handler))


(provide 'jao-mpris)
;;; jao-mpris.el ends here