* bug#73538: [PATCH] Add notifications support to 'mpc'
@ 2024-09-28 22:48 john muhl
0 siblings, 0 replies; only message in thread
From: john muhl @ 2024-09-28 22:48 UTC (permalink / raw)
To: 73538; +Cc: Stefan Monnier
[-- 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
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2024-09-28 22:48 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-28 22:48 bug#73538: [PATCH] Add notifications support to 'mpc' john muhl
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).