;; This buffer is for notes you don't want to save, and for Lisp evaluation.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer.

autosleep

Changes

  • Nov 06 2016: initial version

Description

This is a program that will execute a command after the user has been idle past a given time limit. The main purpose is to execute an OS suspend command, although any command can be supplied. Autosleep continues after each suspend cycle, making it suitable for starting just once in your ~/.xinitrc.

User activity is picked up from X11 and include mouse movement and keyboard activity. In contrast to other techniques, it does not depend on XScreenSaver.

Bigger desktop session packages provide this functionality, but for minimal setups with only X11 and dwm/evilwm/…, I haven't found anything similar. A design goal is not to depend on runtimes that pull in loads of dependencies, and just be a simple C program.

I have only tried this on my laptops, running GNU/Linux Debian Jessie x8664 and i686 - I have no idea if it needs tweaking for other OS versions. This probably only works in GNU/Linux.

Download autosleep here.

Usage

autosleep timeout_seconds keyboard_input_nr suspend_cmd...

- timeout_seconds   - the number of seconds that defines inactivity
- keyboard_input_nr - your keyboard id number (check with xinput -list)
- suspend_cmd...    - the suspend command and arguments. Full path required, including to sudo.

I use this with the s2ram suspend command. I recommend adding the following to a new file in /etc/sudoers.d/ (and chmod 440), to make it executable without sudo password input:

yourusername ALL=(ALL) NOPASSWD: /usr/sbin/s2ram

Examples

Suspend the computer after one hour of inactivity on keyboard ID 9:

autosleep 3600 9 /usr/bin/sudo /usr/sbin/s2ram

Install

  • autoreconf --install
  • ./configure (--prefix...)
  • make all (install)

Todo

Right now it monitors the keyboard and the mouse movements. Monitoring mouse clicks as well would be a natural continuation.

Acknowledgements

For reading the mouse position, I found this useful: https://ubuntuforums.org/showthread.php?t=562087 (user: cwcentral).

For setting up event listening in X11, I found this useful: https://anonscm.debian.org/cgit/pkg-xorg/app/xinput.git/tree/src/test.c (Frederic Lepied, France. <Frederic.Lepied@sugix.frmug.org>).


;; Copyright (c) 2016, 2017
;; Per Weijnitz
;;
;; Verbatim copying and redistribution of this entire page are
;; permitted provided this notice is preserved.
;;
;; Check here for contact information.

-:-- autosleep (org-html-publish-to-html) 0.20      generator:Emacs 25.2.1 (Org mode 8.2.10)      generated:2017-05-05T07:57:28+02:00      ©2016 Per Weijnitz