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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
;;; jao-mpris.el --- mpris players control -*- lexical-binding: t; -*-
;; Copyright (C) 2020, 2021, 2022 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-themes)
(require 'jao-minibuffer)
(defvar jao-mpris-player "playerctld")
(defun jao-mpris--playerctl (&rest args)
(shell-command-to-string (format "playerctl -p %s %s"
jao-mpris-player
(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-mpris-seek (secs)
(interactive "p")
(let ((secs (if (zerop secs) 10 secs)))
(jao-mpris--playerctl "position" (format "%d" secs))))
(defun jao-mpris-seek-back (secs)
(interactive "p")
(jao-mpris-seek (- secs)))
(defun jao-mpris-vol (n)
(interactive "p")
(let ((secs (if (zerop n) 10 n)))
(jao-mpris--playerctl "volume" (format "%d" n))))
(defun jao-mpris-vol-down (n)
(interactive "p")
(jao-mpris-vol (- n)))
(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))
(e (split-string (car (split-string m "\n")) sep)))
`((status . ,(car e))
(track . ,(cadr e))
(title . ,(caddr e))
(artist . ,(cadddr e))
(album . ,(elt e 4))
(duration . ,(last e)))))
;;;###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")))
(defvar jao-mpris--current nil)
(defvar jao-mpris-track-string "")
;;;###autoload
(defun jao-mpris-reset ()
(interactive)
(setq jao-mpris-track-string ""))
(defun jao-mpris--get (k &optional l)
(when-let (v (alist-get k (or l jao-mpris--current)))
(if (consp v) (car v) v)))
(defun jao-mpris--set-current (k v)
(when v (setf (alist-get k jao-mpris--current "" t) v)))
(defun jao-mpris--del-current (k)
(setf (alist-get k jao-mpris--current "" t) ""))
(defun jao-mpris--fmt-time (x suffix)
(if x (format "%02d:%02d%s" (/ x 60) (mod x 60) (or suffix "")) ""))
(defun jao-mpris--format (&optional info)
(let* ((artist (jao-mpris--get 'artist info))
(title (jao-mpris--get 'title info))
(track (jao-mpris--get 'track info))
(album (jao-mpris--get 'album info))
(len (jao-mpris--get 'length info))
(duration (jao-mpris--get 'duration info))
(duration (cond (duration duration)
((stringp len) len)
((numberp len) (jao-mpris--fmt-time (/ len 1e6) "")))))
(format "> %s %s %s%s%s"
(jao--put-face (format "%s" (or track "")) 'jao-themes-f00)
(jao--put-face (or title "") 'jao-themes-f01)
(jao--put-face (or artist "") 'jao-themes-f11)
(jao--put-face (if (and album (not (string-blank-p album)))
(format " (%s)" album) "")
'jao-themes-f01)
(if duration (format " - %s" duration) ""))))
(defun jao-mpris--track (&optional info)
(let ((info (or info (jao-playerctl--status))))
(if (string= "Playing" (jao-mpris--get 'status info))
(setq jao-mpris-track-string (jao-mpris--format info))
(setq jao-mpris-track-string "")))
(jao-minibuffer-refresh))
;;;###autoload
(defun jao-mpris-artist-title ()
(when jao-mpris--current
(cons (jao-mpris--get 'artist) (jao-mpris--get 'title))))
;;;###autoload
(defun jao-mpris-show-osd (&optional refresh)
(interactive "P")
(when refresh (jao-mpris--track))
(when jao-mpris--current
(jao-notify (format "%s%s" (if-let (s (jao-mpris--get 'status))
(format "%s: " s)
"")
(jao-mpris--format)))))
(defun jao-mpris-minibuffer-order (order)
(if (< order 0)
(jao-minibuffer-add-msg-variable 'jao-mpris-track-string (- order))
(jao-minibuffer-add-variable 'jao-mpris-track-string order)))
(defun jao-mpris--handler (_iname properties &rest _args)
(let ((inhibit-message t))
(message "Received properties: %S from %s" properties _iname))
(when-let (md (caadr (assoc "Metadata" properties)))
(let ((tno (caadr (assoc "xesam:trackNumber" md)))
(tlt (caadr (assoc "xesam:title" md)))
(art (caadr (assoc "xesam:artist" md)))
(alb (caadr (assoc "xesam:album" md)))
(len (caadr (assoc "mpris:length" md))))
(jao-mpris--set-current 'track tno)
(jao-mpris--set-current 'title tlt)
(jao-mpris--set-current 'artist art)
(jao-mpris--set-current 'album alb)
(jao-mpris--set-current 'length len)))
(when-let (st (caadr (assoc "PlaybackStatus" properties)))
(jao-mpris--set-current 'status st)
(when (string= st "Stopped")
(dolist (k '(track title artist album length))
(jao-mpris--del-current k))))
(jao-mpris--track jao-mpris--current))
;;;###autoload
(defun jao-mpris-register (name &optional bus order)
(setq jao-mpris-player name)
(dbus-register-signal (or bus :session)
name
"/org/mpris/MediaPlayer2"
"org.freedesktop.DBus.Properties"
"PropertiesChanged"
'jao-mpris--handler)
(when order (jao-mpris-minibuffer-order order)))
(provide 'jao-mpris)
;;; jao-mpris.el ends here
|