From: john muhl <jm@pub.pink>
To: 73538@debbugs.gnu.org
Cc: Stefan Monnier <monnier@iro.umontreal.ca>
Subject: bug#73538: [PATCH] Add notifications support to 'mpc'
Date: Sat, 28 Sep 2024 17:48:21 -0500 [thread overview]
Message-ID: <87frpjtmx6.fsf@pub.pink> (raw)
[-- Attachment #1: Type: text/plain, Size: 86 bytes --]
Tags: patch
This adds support for displaying a notification when the song
changes.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-notifications-support-to-mpc.patch --]
[-- Type: text/x-patch, Size: 4469 bytes --]
From eb495c851dafcf883b0c1cbcd22b2dedc074f226 Mon Sep 17 00:00:00 2001
From: john muhl <jm@pub.pink>
Date: Sun, 15 Sep 2024 19:52:25 -0500
Subject: [PATCH] Add notifications support to 'mpc'
* lisp/mpc.el (mpc-notifications):
(mpc-notifications-function): New option.
(mpc-notifications-id): New variable.
(mpc-notifications-notify):
(mpc-cover-image-find):
(mpc-cover-image-p): New function.
---
lisp/mpc.el | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 79 insertions(+)
diff --git a/lisp/mpc.el b/lisp/mpc.el
index 768c70c2e3a..30e295d4810 100644
--- a/lisp/mpc.el
+++ b/lisp/mpc.el
@@ -95,6 +95,8 @@
(require 'cl-lib)
(require 'subr-x))
+(require 'notifications)
+
(defgroup mpc ()
"Client for the Music Player Daemon (mpd)."
:prefix "mpc-"
@@ -2766,6 +2768,83 @@ mpc-drag-n-drop
(t
(error "Unsupported drag'n'drop gesture"))))))
+;;; Notifications ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(declare-function notifications-notify "notifications")
+(declare-function w32-notification-notify "notifications")
+(declare-function haiku-notifications-notify "notifications")
+(declare-function android-notifications-notify "notifications")
+
+(defcustom mpc-notifications-function 'mpc-notifications-notify
+ "Function called to display notification."
+ :version "31.1"
+ :type 'function)
+
+(defcustom mpc-notifications nil
+ "Non-nil means to display notifications when the song changes."
+ :version "31.1"
+ :type 'boolean
+ :set (lambda (symbol value)
+ (if-let ((cb (cons 'file mpc-notifications-function))
+ value)
+ (add-to-list 'mpc-status-callbacks cb)
+ (setq mpc-status-callbacks (delete cb mpc-status-callbacks)))
+ (set-default-toplevel-value symbol value)))
+
+(defvar mpc-notifications-id nil)
+
+(defun mpc-notifications-notify ()
+ "Display a notification with information about the current song."
+ (interactive)
+ (mpc-status-refresh)
+ (when-let (((string= "play" (alist-get 'state mpc-status)))
+ (title (or (alist-get 'Title mpc-status) "Unknown Title"))
+ (body (or (alist-get 'Artist mpc-status)
+ (alist-get 'AlbumArtist mpc-status)
+ "Unknown Artist"))
+ (icon (or (mpc-cover-image-find (alist-get 'file mpc-status))
+ notifications-application-icon)))
+ (pcase system-type
+ ("windows-nt"
+ (w32-notification-notify :title title :body body :icon icon))
+ ("haiku"
+ (setq mpc-notifications-id
+ (haiku-notifications-notify :title title
+ :body body
+ :app-icon icon
+ :replaces-id mpc-notifications-id)))
+ ("android"
+ (setq mpc-notifications-id
+ (android-notifications-notify :title title
+ :body body
+ :icon icon
+ :replaces-id mpc-notifications-id))
+ (android-notifications-notify :title title :body body :icon icon))
+ (_ (when (notifications-get-server-information)
+ (setq mpc-notifications-id
+ (notifications-notify :title title
+ :body body
+ :app-icon icon
+ :replaces-id mpc-notifications-id)))))))
+
+(defun mpc-cover-image-find (file)
+ "Find cover image for FILE."
+ (and-let* ((default-directory mpc-mpd-music-directory)
+ (dir (file-name-directory file))
+ (files (directory-files (mpc-file-local-copy dir)))
+ (cover (seq-find #'mpc-cover-image-p files))
+ ((expand-file-name cover dir)))))
+
+(defun mpc-cover-image-p (file)
+ "Check if FILE is a cover image."
+ (let ((covers '(".folder.png" "folder.png" "cover.jpg" "folder.jpg")))
+ (or (seq-find (lambda (cover) (string= file cover)) covers)
+ (and mpc-cover-image-re (string-match-p mpc-cover-image-re file)))))
+
+(when mpc-notifications
+ (add-to-list 'mpc-status-callbacks
+ (cons 'file mpc-notifications-function)))
+
;;; Toplevel ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defcustom mpc-frame-alist '((name . "MPC") (tool-bar-lines . 1)
--
2.46.2
reply other threads:[~2024-09-28 22:48 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87frpjtmx6.fsf@pub.pink \
--to=jm@pub.pink \
--cc=73538@debbugs.gnu.org \
--cc=monnier@iro.umontreal.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).