]> ruderich.org/simon Gitweb - config/dotfiles.git/commitdiff
shell/rc,tmux.conf: Auto-logout on TTYs after timeout.
authorSimon Ruderich <simon@ruderich.org>
Tue, 5 Nov 2013 20:41:36 +0000 (21:41 +0100)
committerSimon Ruderich <simon@ruderich.org>
Tue, 5 Nov 2013 20:41:36 +0000 (21:41 +0100)
shell/rc
tmux.conf.in
zsh/rc

index 793093a29fd6d3e21a2f9ee18c8198b2408326f9..aa15002c26c8b1c48f39cdc6ddcb8f30eef93420 100644 (file)
--- a/shell/rc
+++ b/shell/rc
@@ -30,3 +30,83 @@ fi
 if test -t 0; then
     stty -ixon -ixoff
 fi
+
+# Auto-logout after timeout on TTYs. It's easy to forget to logout after
+# switching back to X11 from a TTY.
+timeout_setup_screen_lock() {
+    echo "Locking GNU screen after $timeout seconds (TTY detected)."
+    screen -X idle $timeout lockscreen
+    echo
+}
+timeout_setup_tmux_lock() {
+    echo "Locking Tmux after $timeout seconds (TTY detected)."
+    tmux set-option lock-after-time $timeout
+    echo
+}
+timeout_setup_shell_lock() {
+    echo "Auto-logout after $timeout seconds (TTY detected)."
+    TMOUT=$timeout
+    echo
+}
+# Timeout in seconds.
+local timeout >/dev/null 2>&1
+timeout=600
+case "$TERM" in
+    # `tty` doesn't work inside a pseudo-terminal as provided by GNU
+    # Screen/Tmux.
+    #
+    # The following settings for GNU Screen/Tmux will only auto-lock new
+    # sessions, not when attaching to existing sessions!
+    screen*)
+        if test -n "$STY"; then
+            case "$STY" in
+                *.tty[0-9]*.*)
+                    timeout_setup_screen_lock
+                    ;;
+            esac
+        elif test -n "$TMUX"; then
+            if ! type vlock >/dev/null; then
+                echo "vlock not found, locking won't work in Tmux!" >&2
+            fi
+
+            case "`tmux display -p '#{client_tty}'`" in
+                /dev/tty[0-9]*)
+                    timeout_setup_tmux_lock
+                    ;;
+                /dev/*)
+                    # Not on TTY, nothing to do.
+                    ;;
+                *)
+                    echo 'Tmux < 1.8 found, TTY check might be incorrect!' >&2
+
+                    local client >/dev/null 2>&1
+                    client=`printf '%s' "$TMUX" | sed 's/^.*,\([0-9]*\)$/\1/'`
+
+                    case "`tmux list-clients \
+                            | grep -E "^/dev/.*?: $client "`" in
+                        /dev/tty[0-9]*)
+                            timeout_setup_tmux_lock
+                            ;;
+                    esac
+                    ;;
+            esac
+        else
+            echo 'TERM=screen but neither $STY nor $TMUX found!' >&2
+            timeout_setup_shell_lock
+        fi
+        ;;
+
+    *)
+        case `tty` in
+            /dev/tty[0-9]*)
+                timeout_setup_shell_lock
+                ;;
+        esac
+        ;;
+esac
+unset timeout
+unset timeout_setup_screen_lock
+unset timeout_setup_tmux_lock
+unset timeout_setup_screen_lock
+
+# vim: ft=sh
index 699458a5ab00a7523f774eda7047b769336ff0a2..4b2ba9234f4202f61946fec3e5ab2876c255b6d1 100644 (file)
 
 # GENERAL
 
+# Use vlock to lock the screen. Tmux doesn't have an internal lock program
+# like GNU screen!
+set-option -g lock-command "vlock"
+# Lock sessions individually. Otherwise lock-after-time will wait on all other
+# sessions to idle before it locks this session.
+set-option -g lock-server off
+
 # Use Zsh as shell. Tmux does not display a warning if the shell isn't
 # available!
 set-option -g default-shell "/bin/zsh"
diff --git a/zsh/rc b/zsh/rc
index 2d7331ceaf89c95fcd311f327029f229fc632255..869167906cdc85b64131a945a2ffe38a65eb55cd 100644 (file)
--- a/zsh/rc
+++ b/zsh/rc
@@ -50,6 +50,8 @@ zshrc_resolve_fg_to_resumed_job_name() {
 
 # MISCELLANEOUS SETTINGS
 
+# Prevent warnings.
+typeset -g TMOUT
 # Load general shell setup commands. NOTE: Expand this when publishing the
 # config.
 source_config ~/.shell/rc