jao-sleep.el 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. ;;; jao-sleep.el --- Actions upon sleep/awake -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2020 jao
  3. ;; Author: jao <mail@jao.io>
  4. ;; Keywords: hardware
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (require 'dbus)
  17. (defvar jao-sleep-sleep-functions nil)
  18. (defvar jao-sleep-awake-functions nil)
  19. (defvar jao-sleep--dbus-registration-object nil)
  20. (defun jao-sleep--dbus-sleep-handler (sleep-start)
  21. (condition-case nil
  22. (if sleep-start
  23. (progn (message "Running on sleep functions")
  24. (run-hooks 'jao-sleep-sleep-functions))
  25. (message "Running on awake functions")
  26. (run-hooks 'jao-sleep-awake-functions))
  27. (error (message "There was an error running %s" sleep-start))))
  28. ;;;###autoload
  29. (defun jao-sleep-dbus-register (&optional session-dbus)
  30. "Register actions to take on sleep and on awake, using the system D-BUS."
  31. (when (featurep 'dbusbind)
  32. (setq jao-sleep--dbus-sleep-registration-object
  33. (dbus-register-signal (if session-dbus :session :system)
  34. "org.freedesktop.login1"
  35. "/org/freedesktop/login1"
  36. "org.freedesktop.login1.Manager"
  37. "PrepareForSleep"
  38. #'jao-sleep--dbus-sleep-handler))))
  39. ;;;###autoload
  40. (defun jao-sleep-dbus-unregister ()
  41. (condition-case nil
  42. (dbus-unregister-object jao-sleep--dbus-sleep-registration-object)
  43. (wrong-type-argument nil)))
  44. (provide 'jao-sleep)
  45. ;;; jao-sleep.el ends here