unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#63620: 30.0.50; [Feature Request] run hooks on sleep/wake
@ 2023-05-20 23:19 Andrew Cohen
  2023-05-21  8:12 ` Michael Albinus
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Cohen @ 2023-05-20 23:19 UTC (permalink / raw)
  To: 63620

I am playing around with various network things (mostly vpn related) and
need Emacs to take certain actions when my laptop sleeps and wakes. This
is easy to do using the existing dbus support (the relevant code is
below, which runs hooks on sleeping and waking) The question is whether
this is useful enough to add to Emacs, and if so the best place to put
it.

Not too long ago, Eric Abrahamsen added gnus-dbus.el which is similar,
although for a single purpose: to close gnus server network connections
on sleep. This has a single entry point for the user: set
'gnus-dbus-close-on-sleep to t to enable the feature. 


Options:
1. Do nothing (this isn't useful enough to change anything, and I can just
keep using my own code).

2. Add a small package dbus-sleep.el. I would also remove gnus-dbus.el
but leave the variable 'gnus-dbus-close-on-sleep and use it to control
the installation of appropriate gnus functions to the new hooks.

3. 2. Add the new code to the existing dbus.el. I would also remove
gnus-dbus.el but leave the variable 'gnus-dbus-close-on-sleep and use it
to control the installation of appropriate functions to the new hooks.

I mostly favor adding it to dbus.el. The argument against: dbus.el is
focused on providing language bindings for the D-Bus API rather than a
user-feature. The removal of gnus-dbus.el shouldn't cause any problem
since I would maintain the same functionality enabled by the same
variable, so no user-visible change. 

Advice?


;;; Code:

(require 'dbus)

(defcustom dbus-sleep-sleep-hook nil
  "Hook to run on sleep."
  :group 'dbus-sleep
  :type 'hook)

(defcustom dbus-sleep-wake-hook nil
  "Hook to run on wake."
  :group 'dbus-sleep
  :type 'hook)

(defvar dbus-sleep-registration-object nil
  "Object returned from `dbus-register-signal'.
This is used to unregister the signal.")

;;;###autoload
(defun dbus-sleep-enable ()
  "Use `dbus-register-signal' to close servers on sleep."
  (interactive)
  ;; Don't enable if it's already enabled.
  (when (and (featurep 'dbusbind) (not dbus-sleep-registration-object))
    (setq dbus-sleep-registration-object
          (dbus-register-signal :system
                                "org.freedesktop.login1"
                                "/org/freedesktop/login1"
                                "org.freedesktop.login1.Manager"
                                "PrepareForSleep"
                                #'dbus-sleep-handler))))

(defun dbus-sleep-disable ()
  "Unregister sleep signal."
  (interactive)
  (condition-case nil
      (dbus-unregister-object
       dbus-sleep-registration-object)
    (setq dbus-sleep-registration-object nil)
    (wrong-type-argument nil)))

(defun dbus-sleep-handler (sleep-wake)
  "Handler to execute sleep and wake functions.
SLEEP-WAKE is t on sleeping and nil on waking."
  (ignore-errors
    (if sleep-wake
        (run-hooks 'dbus-sleep-sleep-hook)
      (run-hooks 'dbus-sleep-wake-hook))))

-- 
Andrew Cohen





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-06-10 10:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-20 23:19 bug#63620: 30.0.50; [Feature Request] run hooks on sleep/wake Andrew Cohen
2023-05-21  8:12 ` Michael Albinus
2023-06-05 13:06   ` Andrew Cohen
2023-06-10 10:47     ` Michael Albinus

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).