The Complete Computing Environment

Keep Screen Awake with Systemd-Inhibit-Mode


When I watch a full-screen Video in Firefox while using XMonad, it doesn't properly disable DPMS so my screen blanks every five minutes in to the video. Rather than try to figure out why that was, I would invoke systemd-inhibit to inhibit screen blanking. This was fine, but I'd run it in a terminal emulator which I would promptly forget about. Cue my laptop having a dead battery or my monitor burning in the lock-screen text overnight when I would manually lock the desktop and head to bed.

I whipped this Emacs global minor mode up so that I would have a modeline entry and a simple toggle. Enabling systemd-inhibit-mode will, by default prevent any of the "idle" actions from happening. No screen blanking, no lock screen, no sleep, just video. If you want to use it like Caffeine to prevent sleep while your code compiles or something you can customize systemd-inhibit-what.

(defcustom systemd-inhibit-what (list "idle")
  "Type of operation to inhibit with systemd-inhibit"
  :type '(repeat string)
  :options '("shutdown" "sleep" "idle"
             "handle-power-key" "handle-suspend-key"
             "handle-hibernate-key" "handle-lid-switch"))

(defvar systemd-inhibit-mode-process nil)

(define-minor-mode systemd-inhibit-mode
  "When active, SystemD will not lock or blank the screen depending on configuration."
  :global t
  :init-value nil
  :lighter "[INHIBIT]"
  (if systemd-inhibit-mode
      (setq systemd-inhibit-mode-process
            (start-process "systemd-inhibit" " *systemd-inhibit*"
                           (concat "--what=" (s-join ":" systemd-inhibit-what))
                           "watch" "-n" "45" "date"))
      (kill-process systemd-inhibit-mode-process)
      (setq systemd-inhibit-mode-process nil))))

(provide 'cce/systemd-inhibit)