summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorjao <jao@gnu.org>2021-06-02 02:08:49 +0100
committerjao <jao@gnu.org>2021-06-02 02:08:49 +0100
commite8f15c0b4df43c8d57b623c2c73053b3bdd09930 (patch)
tree3565afd82fc193707f6c1830652b3e171bbd815e
parentf1d177c63964f5cf9bf65093461a43772dbf5a27 (diff)
downloadelibs-e8f15c0b4df43c8d57b623c2c73053b3bdd09930.tar.gz
elibs-e8f15c0b4df43c8d57b623c2c73053b3bdd09930.tar.bz2
jao-mpc: much better handling of secondary port
-rw-r--r--lib/media/jao-mpc.el138
1 files changed, 75 insertions, 63 deletions
diff --git a/lib/media/jao-mpc.el b/lib/media/jao-mpc.el
index 4602a9d..68d6d20 100644
--- a/lib/media/jao-mpc.el
+++ b/lib/media/jao-mpc.el
@@ -1,4 +1,4 @@
-;;; jao-mpc.el --- Using mpc client -*- lexical-binding: t; -*-
+;;; jao-mpc.el --- Using mpc to interact with mpd -*- lexical-binding: t; -*-
;; Copyright (C) 2021 jao
@@ -23,23 +23,21 @@
;;; Commentary:
-;; Simple mpd interaction using elmpd and mpc.
+;; Simple mpd interaction using mpc.
;;; Code:
+(require 'jao-themes)
(require 'jao-lyrics)
(require 'jao-random-album)
(defconst jao-mpc--albums "*MPC Albums*")
(defconst jao-mpc--playlist "*MPC Playlist*")
-(defvar jao-mpc-port 6600)
-(defvar-local jao-mpc--local-port nil)
+(defvar-local jao-mpc-port 6600)
-(defun jao-mpc--cmd (cmd)
- (shell-command-to-string (format "mpc -p %s %s"
- (or jao-mpc--local-port jao-mpc-port)
- cmd)))
+(defun jao-mpc--cmd (cmd &optional port)
+ (shell-command-to-string (format "mpc -p %s %s" (or port jao-mpc-port) cmd)))
(defconst jao-mpc--fields
'(artist album composer originaldate genre title track position time name))
@@ -47,16 +45,26 @@
(defconst jao-mpc--stfmt
(mapconcat (lambda (f) (format "%s:::%%%s%%" f f)) jao-mpc--fields "\n"))
-(defun jao-mpc--current ()
- (let ((s (jao-mpc--cmd (format "-f '%s' current" jao-mpc--stfmt)))
+(defun jao-mpc--current (&optional port)
+ (let ((s (jao-mpc--cmd (format "-f '%s' current" jao-mpc--stfmt) port))
(res))
(dolist (s (split-string s "\n" t " ") res)
(when (string-match "\\(.+\\):::\\(.+\\)" s)
(push (cons (intern (match-string 1 s)) (match-string 2 s)) res)))))
-(defun jao-mpc--current-str (&optional current len)
- (let* ((current (or current (jao-mpc--current)))
- (len (or len (jao-mpc--queue-len)))
+(defun jao-mpc--playing-p (&optional port)
+ (not (string-blank-p (jao-mpc--cmd "status|grep '\\[playing\\]'" port))))
+
+(defun jao-mpc--queue-len (&optional port)
+ (string-to-number (jao-mpc--cmd "playlist|wc -l" port)))
+
+(defsubst jao--put-face (str face)
+ (put-text-property 0 (length str) 'face face str)
+ str)
+
+(defun jao-mpc--current-str (&optional port current len)
+ (let* ((current (or current (jao-mpc--current port)))
+ (len (or len (jao-mpc--queue-len port)))
(title (alist-get 'title current (alist-get 'name current "")))
(album (alist-get 'album current))
(artist (alist-get 'artist current))
@@ -77,43 +85,36 @@
(defvar jao-mpc-minibuffer-str "")
-(defun jao-mpc--playing-p ()
- (not (string-blank-p (jao-mpc--cmd "status|grep '\\[playing\\]'"))))
-
-(defun jao-mpc--queue-len ()
- (string-to-number (jao-mpc--cmd "playlist|wc -l")))
-
-(defun jao-mpc--set-current-str (&optional _proc _str)
+(defun jao-mpc--set-current-str (&optional port)
(setq jao-mpc-minibuffer-str
- (if (jao-mpc--playing-p)
- (jao-mpc--current-str)
- (when (and jao-random-album-p (not (jao-mpc--current)))
+ (if (jao-mpc--playing-p port)
+ (jao-mpc--current-str port)
+ (when (and (null port) jao-random-album-p (not (jao-mpc--current)))
(jao-random-album-next))
- "")))
-
-(defvar jao-mpc--idle-proc nil)
-
-(defun jao-mpc--idle-loop ()
- (when jao-mpc--idle-proc (kill-process jao-mpc--idle-proc))
- (setq jao-mpc--idle-proc
- (make-process :name "jao-mpc-idleloop"
- :buffer "*jao-mpc-idleloop*"
- :command '("mpc" "idleloop" "player")
- :filter #'jao-mpc--set-current-str)))
+ ""))
+ (jao-minibuffer-refresh))
+
+(defvar jao-mpc--idle-procs nil)
+
+(defun jao-mpc--idle-loop (&optional port)
+ (when-let (proc (alist-get port jao-mpc--idle-procs))
+ (ignore-errors (kill-process proc)))
+ (setf (alist-get port jao-mpc--idle-procs nil t)
+ (make-process :name (format "jao-mpc-idleloop (%s)" port)
+ :buffer nil
+ :noquery t
+ :command `("mpc" "-p" ,(format "%s" (or port jao-mpc-port))
+ "idleloop" "player")
+ :filter (lambda (_p _s) (jao-mpc--set-current-str port)))))
(define-derived-mode jao-mpc-albums-mode fundamental-mode "MPC Albums"
"Mode to display the list of albums known by mpd."
(read-only-mode -1)
(delete-region (point-min) (point-max))
- (insert (shell-command-to-string "mpc list album"))
+ (insert (jao-mpc--cmd "list album"))
(goto-char (point-min))
(read-only-mode 1))
-(define-key jao-mpc-albums-mode-map (kbd "n") #'next-line)
-(define-key jao-mpc-albums-mode-map (kbd "p") #'previous-line)
-(define-key jao-mpc-albums-mode-map (kbd "RET") #'jao-mpc-add-and-play)
-(define-key jao-mpc-albums-mode-map (kbd "q") #'bury-buffer)
-
(defun jao-mpc--album-buffer ()
(if-let (b (get-buffer jao-mpc--albums))
b
@@ -128,11 +129,16 @@
(jao-mpc--cmd (format "findadd album \"%s\"" album))
(jao-mpc--cmd "play")))
+(define-key jao-mpc-albums-mode-map (kbd "n") #'next-line)
+(define-key jao-mpc-albums-mode-map (kbd "p") #'previous-line)
+(define-key jao-mpc-albums-mode-map (kbd "RET") #'jao-mpc--add-and-play)
+(define-key jao-mpc-albums-mode-map (kbd "q") #'bury-buffer)
+
(define-derived-mode jao-mpc-playlist-mode fundamental-mode "MPC Playlist"
"Mode to display the list of playlist known by mpd."
(read-only-mode -1)
(delete-region (point-min) (point-max))
- (setq-local jao-mpc--local-port jao-mpc-port)
+ (setq-local jao-mpc-port jao-mpc-port)
(insert (jao-mpc--cmd "playlist"))
(goto-char (point-min))
(display-line-numbers-mode)
@@ -157,50 +163,51 @@
(define-key jao-mpc-playlist-mode-map (kbd "RET") #'jao-mpc--playlist-play)
(define-key jao-mpc-playlist-mode-map (kbd "C") #'jao-mpc-clear)
-(defun jao-mpc--playlist-buffer ()
+(defun jao-mpc--playlist-buffer (&optional port)
(with-current-buffer (get-buffer-create jao-mpc--playlist)
+ (setq-local jao-mpc-port (or port jao-mpc-port))
(jao-mpc-playlist-mode)
(current-buffer)))
;;;###autoload
-(defun jao-mpc-stop ()
+(defun jao-mpc-stop (&optional port)
(interactive)
- (jao-mpc--cmd "stop"))
+ (jao-mpc--cmd "stop" port))
;;;###autoload
-(defun jao-mpc-toggle ()
+(defun jao-mpc-toggle (&optional port)
(interactive)
- (jao-mpc--cmd "toggle"))
+ (jao-mpc--cmd "toggle" port))
;;;###autoload
-(defun jao-mpc-play ()
+(defun jao-mpc-play (&optional port)
(interactive)
- (jao-mpc--cmd "play"))
+ (jao-mpc--cmd "play" port))
;;;###autoload
-(defun jao-mpc-next ()
+(defun jao-mpc-next (&optional port)
(interactive)
- (jao-mpc--cmd "next"))
+ (jao-mpc--cmd "next" port))
;;;###autoload
-(defun jao-mpc-previous ()
+(defun jao-mpc-previous (&optional port)
(interactive)
- (jao-mpc--cmd "previous"))
+ (jao-mpc--cmd "previous" port))
;;;###autoload
-(defun jao-mpc-seek (delta)
+(defun jao-mpc-seek (delta &optional port)
(interactive "nDelta: ")
- (jao-mpc--cmd (format "seek %s%s" (if (> delta 0) "+" "") delta)))
+ (jao-mpc--cmd (format "seek %s%s" (if (> delta 0) "+" "") delta) port))
;;;###autoload
-(defun jao-mpc-clear ()
+(defun jao-mpc-clear (&optional port)
(interactive)
- (jao-mpc--cmd "clear"))
+ (jao-mpc--cmd "clear" port))
;;;###autoload
-(defun jao-mpc-echo-current ()
+(defun jao-mpc-echo-current (&optional port)
(interactive)
- (jao-notify (jao-mpc--current-str)))
+ (jao-notify (jao-mpc--current-str port)))
;;;###autoload
(defun jao-mpc-add-url (url)
@@ -214,26 +221,31 @@
(pop-to-buffer (jao-mpc--album-buffer)))
;;;###autoload
-(defun jao-mpc-show-playlist ()
+(defun jao-mpc-show-playlist (&optional port)
"Show current playlist."
(interactive)
- (pop-to-buffer (jao-mpc--playlist-buffer))
+ (pop-to-buffer (jao-mpc--playlist-buffer port))
(jao-mpc--playlist-goto-current))
;;;###autoload
-(defun jao-mpc-lyrics-track-data ()
- (let ((c (string-trim (jao-mpc--cmd "current"))))
+(defun jao-mpc-lyrics-track-data (&optional port)
+ (let ((c (string-trim (jao-mpc--cmd "current" port))))
(unless (string-blank-p c)
(when (string-match "\\(.\\) - \\(.+\\)" c)
(cons (match-string 2 c) (match-string 1 c))))))
;;;###autoload
-(defun jao-mpc-setup ()
+(defun jao-mpc-setup (&optional secondary-port)
(setq jao-lyrics-info-function #'jao-mpc-lyrics-track-data)
(jao-random-album-setup #'jao-mpc--album-buffer
#'jao-mpc--add-and-play
#'jao-mpc-stop)
(jao-mpc--idle-loop)
+ (when (jao-mpc--playing-p) (jao-mpc--set-current-str))
+ (when secondary-port
+ (jao-mpc--idle-loop secondary-port)
+ (when (jao-mpc--playing-p secondary-port)
+ (jao-mpc--set-current-str secondary-port)))
(jao-minibuffer-add-msg-variable 'jao-mpc-minibuffer-str 1))
(provide 'jao-mpc)