From f44288f792b42c5b7fc3fe979e4915afdd3e2e33 Mon Sep 17 00:00:00 2001 From: jao Date: Tue, 1 Jun 2021 03:12:32 +0100 Subject: mpc: we don't need mpdn/elmpd: mpc has an idleloop --- attic/jao-mpdn.el | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++ init.org | 4 -- lib/media/jao-mpc.el | 64 +++++++++++++++++++++++- lib/media/jao-mpdn.el | 133 -------------------------------------------------- 4 files changed, 196 insertions(+), 138 deletions(-) create mode 100644 attic/jao-mpdn.el delete mode 100644 lib/media/jao-mpdn.el diff --git a/attic/jao-mpdn.el b/attic/jao-mpdn.el new file mode 100644 index 0000000..d707767 --- /dev/null +++ b/attic/jao-mpdn.el @@ -0,0 +1,133 @@ +;;; jao-mpdn.el --- Notifications using elmpd -*- lexical-binding: t; -*- + +;; Copyright (C) 2021 jao + +;; Author: jao +;; Keywords: convenience +;; Version: 0.1 +;; Package-requires: ((emacs "27.1") (elmpd "0.1.9")) +;; URL: https://codeberg.org/jao/lib/media + +;; 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 . + +;;; Commentary: + +;; React to mpd player status changes. + +;;; Code: + +(require 'elmpd) +(require 'jao-minibuffer) + +(defvar jao-mpdn--connection nil) +(defvar jao-mpdn-host "localhost") +(defvar jao-mpdn-port 6600) +(defvar-local jao-mpdn--local-port nil) + +(defun jao-mpdn--disconnect (conn) + (delete-process (elmpd-connection--fd conn))) + +(defun jao-mpdn--connect (name &optional cb) + (elmpd-connect :name name + :host jao-mpdn-host + :port jao-mpdn-port + :subsystems + (when cb `((player) . ,cb)))) + +(defun jao-mpdn-disconnect () + (interactive) + (when jao-mpdn--connection + (jao-mpdn--disconnect jao-mpdn--connection) + (setq jao-mpdn--connection nil))) + +(defun jao-mpdn-connect (&optional force) + (interactive) + (when force (jao-mpdn-disconnect)) + (unless jao-mpdn--connection + (setq jao-mpdn--connection (jao-mpdn--connect "jao-mpc" 'jao-mpdn--watcher)) + (jao-mpdn--watcher jao-mpdn--connection 'player)) + jao-mpdn--connection) + +(defun jao-mpdn--send (cmd cb) + (elmpd-send jao-mpdn--connection cmd cb)) + +(defvar jao-mpdn--play-status '()) +(defvar jao-mpdn--current '()) +(defvar jao-mpdn-minibuffer-str "") + +(defun jao-mpdn--parse-retort (txt) + (let (res) + (dolist (e (split-string txt "\n" t " ") res) + (let ((e (split-string e ": " t " "))) + (when (and (car e) (cadr e)) + (push (cons (car e) (cadr e)) res)))))) + +(defun jao-mpdn--update-status (next) + (let ((cb (lambda (_c ok txt) + (when ok + (setq jao-mpdn--play-status (jao-mpdn--parse-retort txt)) + (when next (funcall next)))))) + (jao-mpdn--send "status" cb))) + +(defun jao-mpdn--current-get (x &optional def) + (alist-get x jao-mpdn--current def nil #'string=)) + +(defun jao-mpdn--status-get (x &optional def) + (alist-get x jao-mpdn--play-status def nil #'string=)) + +(defun jao-mpdn--playing-p () + (string= (jao-mpdn--status-get "state" "") "play")) + +(defun jao-mpdn--current-str () + (let ((title (jao-mpdn--current-get "Title")) + (album (jao-mpdn--current-get "Album")) + (no (string-to-number (jao-mpdn--current-get "Track" "0"))) + (len (string-to-number (jao-mpdn--status-get "playlistlength" "1"))) + (artist (jao-mpdn--current-get "Artist")) + (composer (jao-mpdn--current-get "Composer"))) + (format " %s%s %s%s%s" + (jao--put-face (if (zerop no) "" (format "%02d/%s " no len)) + 'jao-themes-f02) + (jao--put-face title 'jao-themes-f00) + (jao--put-face artist 'jao-themes-f01) + (jao--put-face (if composer (format " [%s]" composer) "") + 'jao-themes-f01) + (jao--put-face (if album (format " (%s)" album) "") 'jao-themes-f11)))) + +(defun jao-mpdn--update-minibuffer () + (setq jao-mpdn-minibuffer-str + (if (jao-mpdn--playing-p) (jao-mpdn--current-str) "")) + (jao-minibuffer-refresh)) + +(defun jao-mpdn--update-current (&optional next) + (let ((cb (lambda (_c ok txt) + (when ok + (setq jao-mpdn--current (jao-mpdn--parse-retort txt)) + (jao-mpdn--update-minibuffer) + (cond (next (funcall next)) + ((and (null jao-mpdn--current) jao-random-album-p) + (jao-random-album-next))))))) + (jao-mpdn--send "currentsong" cb))) + +(defun jao-mpdn--watcher (_conn _subsys) + (jao-mpdn--update-status #'jao-mpdn--update-current)) + + +;;;###autoload +(defun jao-mpdn-setup () + (jao-mpdn-connect t) + (jao-minibuffer-add-msg-variable 'jao-mpdn-minibuffer-str 1)) + +(provide 'jao-mpdn) +;;; jao-mpdn.el ends here diff --git a/init.org b/init.org index 04a3f8d..773b2fa 100644 --- a/init.org +++ b/init.org @@ -2970,10 +2970,6 @@ (defalias 'jao-player-echo 'jao-mpc-echo-current) (defalias 'jao-player-list 'jao-mpc-show-playlist) (defalias 'jao-player-browse 'jao-mpc-show-albums) - - (use-package elmpd :ensure t) - (use-package jao-mpdn :demand t) - (jao-mpdn-setup) #+end_src *** mopidy #+begin_src emacs-lisp diff --git a/lib/media/jao-mpc.el b/lib/media/jao-mpc.el index 59021ca..669883c 100644 --- a/lib/media/jao-mpc.el +++ b/lib/media/jao-mpc.el @@ -41,6 +41,66 @@ (or jao-mpc--local-port jao-mpc-port) cmd))) +(defconst jao-mpc--fields + '(artist album composer originaldate genre title track position time name)) + +(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))) + (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))) + (title (alist-get 'title current (alist-get 'name current ""))) + (album (alist-get 'album current)) + (artist (alist-get 'artist current)) + (composer (alist-get 'composer current)) + (no (string-to-number (alist-get 'track current "0"))) + (time (alist-get 'time current ""))) + (format " %s%s %s%s%s%s" + (jao--put-face (if (zerop no) "" (format "%02d/%s " no len)) + 'jao-themes-f02) + (jao--put-face title 'jao-themes-f00) + (jao--put-face artist 'jao-themes-f01) + (jao--put-face (if composer (format " [%s]" composer) "") + 'jao-themes-f01) + (jao--put-face (if album (format " (%s)" album) "") 'jao-themes-f11) + (if (string-blank-p time) + "" + (jao--put-face (format " [%s]" time) 'jao-themes-dimm))))) + +(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) + (setq jao-mpc-minibuffer-str + (if (jao-mpc--playing-p) + (jao-mpc--current-str) + (when (and 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))) + (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) @@ -167,7 +227,9 @@ (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-stop) + (jao-mpc--idle-loop) + (jao-minibuffer-add-msg-variable 'jao-mpc-minibuffer-str 1)) (provide 'jao-mpc) ;;; jao-mpc.el ends here diff --git a/lib/media/jao-mpdn.el b/lib/media/jao-mpdn.el deleted file mode 100644 index d707767..0000000 --- a/lib/media/jao-mpdn.el +++ /dev/null @@ -1,133 +0,0 @@ -;;; jao-mpdn.el --- Notifications using elmpd -*- lexical-binding: t; -*- - -;; Copyright (C) 2021 jao - -;; Author: jao -;; Keywords: convenience -;; Version: 0.1 -;; Package-requires: ((emacs "27.1") (elmpd "0.1.9")) -;; URL: https://codeberg.org/jao/lib/media - -;; 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 . - -;;; Commentary: - -;; React to mpd player status changes. - -;;; Code: - -(require 'elmpd) -(require 'jao-minibuffer) - -(defvar jao-mpdn--connection nil) -(defvar jao-mpdn-host "localhost") -(defvar jao-mpdn-port 6600) -(defvar-local jao-mpdn--local-port nil) - -(defun jao-mpdn--disconnect (conn) - (delete-process (elmpd-connection--fd conn))) - -(defun jao-mpdn--connect (name &optional cb) - (elmpd-connect :name name - :host jao-mpdn-host - :port jao-mpdn-port - :subsystems - (when cb `((player) . ,cb)))) - -(defun jao-mpdn-disconnect () - (interactive) - (when jao-mpdn--connection - (jao-mpdn--disconnect jao-mpdn--connection) - (setq jao-mpdn--connection nil))) - -(defun jao-mpdn-connect (&optional force) - (interactive) - (when force (jao-mpdn-disconnect)) - (unless jao-mpdn--connection - (setq jao-mpdn--connection (jao-mpdn--connect "jao-mpc" 'jao-mpdn--watcher)) - (jao-mpdn--watcher jao-mpdn--connection 'player)) - jao-mpdn--connection) - -(defun jao-mpdn--send (cmd cb) - (elmpd-send jao-mpdn--connection cmd cb)) - -(defvar jao-mpdn--play-status '()) -(defvar jao-mpdn--current '()) -(defvar jao-mpdn-minibuffer-str "") - -(defun jao-mpdn--parse-retort (txt) - (let (res) - (dolist (e (split-string txt "\n" t " ") res) - (let ((e (split-string e ": " t " "))) - (when (and (car e) (cadr e)) - (push (cons (car e) (cadr e)) res)))))) - -(defun jao-mpdn--update-status (next) - (let ((cb (lambda (_c ok txt) - (when ok - (setq jao-mpdn--play-status (jao-mpdn--parse-retort txt)) - (when next (funcall next)))))) - (jao-mpdn--send "status" cb))) - -(defun jao-mpdn--current-get (x &optional def) - (alist-get x jao-mpdn--current def nil #'string=)) - -(defun jao-mpdn--status-get (x &optional def) - (alist-get x jao-mpdn--play-status def nil #'string=)) - -(defun jao-mpdn--playing-p () - (string= (jao-mpdn--status-get "state" "") "play")) - -(defun jao-mpdn--current-str () - (let ((title (jao-mpdn--current-get "Title")) - (album (jao-mpdn--current-get "Album")) - (no (string-to-number (jao-mpdn--current-get "Track" "0"))) - (len (string-to-number (jao-mpdn--status-get "playlistlength" "1"))) - (artist (jao-mpdn--current-get "Artist")) - (composer (jao-mpdn--current-get "Composer"))) - (format " %s%s %s%s%s" - (jao--put-face (if (zerop no) "" (format "%02d/%s " no len)) - 'jao-themes-f02) - (jao--put-face title 'jao-themes-f00) - (jao--put-face artist 'jao-themes-f01) - (jao--put-face (if composer (format " [%s]" composer) "") - 'jao-themes-f01) - (jao--put-face (if album (format " (%s)" album) "") 'jao-themes-f11)))) - -(defun jao-mpdn--update-minibuffer () - (setq jao-mpdn-minibuffer-str - (if (jao-mpdn--playing-p) (jao-mpdn--current-str) "")) - (jao-minibuffer-refresh)) - -(defun jao-mpdn--update-current (&optional next) - (let ((cb (lambda (_c ok txt) - (when ok - (setq jao-mpdn--current (jao-mpdn--parse-retort txt)) - (jao-mpdn--update-minibuffer) - (cond (next (funcall next)) - ((and (null jao-mpdn--current) jao-random-album-p) - (jao-random-album-next))))))) - (jao-mpdn--send "currentsong" cb))) - -(defun jao-mpdn--watcher (_conn _subsys) - (jao-mpdn--update-status #'jao-mpdn--update-current)) - - -;;;###autoload -(defun jao-mpdn-setup () - (jao-mpdn-connect t) - (jao-minibuffer-add-msg-variable 'jao-mpdn-minibuffer-str 1)) - -(provide 'jao-mpdn) -;;; jao-mpdn.el ends here -- cgit v1.2.3