5

I wish to have a simple locking feature in Awesome on Ubuntu 13.10, both on demand and on inactivity.

1 Answer 1

6

Get recipe inredients:

sudo apt-get install xautolock slock
  • xautolock - executes a locker after a period of inactivity.
  • slock - locks the screen wit a black only UI, type password and hit Enter.

try the command to lock the session first and see if you like it:

slock

This can be hooked into Awesome menu by modifying it's config:

# make awesome config dir:
mkdir -p ~/.config/awesome

# copy default config file to your home folder:
cp /etc/xdg/awesome/rc.lua ~/.config/awesome

# edit the custom config:
nano ~/.config/awesome/rc.lua

Find where the mymainmenu is being built and adjust it. My section now looks like this (note the "Lock" line):

lockscreen = function() awful.util.spawn("slock") end

mymainmenu = awful.menu({ items = {
            { "Lock", lockscreen },
            { "Awesome", myawesomemenu, beautiful.awesome_icon },
            { "Debian", debian.menu.Debian_menu.Debian },
            { "Guest", terminal .. " -e /usr/bin/dm-tool switch-to-guest" },
            { "Greet", terminal .. " -e /usr/bin/dm-tool switch-to-greeter" },
            { "Logout", awesome.quit },
         } 
      })

And make a Meta+Escape shortcut for it too. Originally Escape is bound to tag history. I don't use that feature though.

-- it was:
--    awful.key({ modkey,           }, "Escape", awful.tag.history.restore),
    awful.key({ modkey,           }, "Escape", lockscreen               ),

Then comes the automatic locking. I created an executable file to make sure that xautolock only executes once per session:

# make a bin folder if not present:
mkdir -p ~/bin/

# make the file, set permissions and edit:
touch ~/bin/session-daemon
chmod ug+x ~/bin/session-daemon
nano ~/bin/session-daemon

I used an environment variable "XDG_SEAT" which is set by the DM to distinguish sessions and store a PID and log of the command. It is only executed if not running already:

#!/bin/bash

if [ "$#" == "0" ] ;
then
    echo "Session daemon missing command" >&2
    echo "USAGE: $0 command [args...]"
    echo "Executes given command with args only once per XDG seat (session)."
    echo "Keeps PID and log of the executed command."
    exit 1
fi

COMMAND=$1
shift

NAME="${COMMAND##*/}"
PIDFILE=~/.cache/session-daemon/$NAME-$XDG_SEAT.pid
LOGFILE=~/.cache/session-daemon/$NAME-$XDG_SEAT.log
PID=""
PIDDIR="${PIDFILE%/*}"
LOGDIR="${LOGFILE%/*}"

mkdir -p $PIDDIR
if [ $? -ne 0 ];
then
    echo "Session daemon PIDDIR not there: $PIDDIR" >&2
    exit 2
fi

mkdir -p $LOGDIR
if [ $? -ne 0 ];
then
    echo "Session daemon LOGDIR not there: $LOGDIR" >&2
    exit 2
fi

if [ -e "$PIDFILE" ] ; 
then 
    echo "Session daemon '$NAME' PIDFILE found: $PIDFILE"
    PID=`cat "$PIDFILE"` 
    echo "Session daemon '$NAME' PID found: $PID"
else
    echo "Session daemon '$NAME' PIDFILE NOT found: $PIDFILE"
fi

if [ "$PID" ] && kill -0 "$PID" 2>/dev/null ; 
then
    echo "Session daemon '$NAME' was already running with PID $PID" >&2
    exit 3
fi

echo "Session daemon running $COMMAND $* > $LOGFILE 2>&1"
$COMMAND $* > "$LOGFILE" 2>&1 &
PID=$!
ERR=$?
sleep 1

if [ "$PID" ] && [  $ERR == 0 ] && kill -0 "$PID" 2>/dev/null;
then
    echo -n $PID > "$PIDFILE"
    echo "Session daemon '$NAME' started with PID $PID"
else
    echo "Session daemon '$NAME' did not start or finished early. PID: $PID, ERR: $ERR" >&2
    cat "$LOGFILE" >&2
    exit 4
fi

That session-daemon script will be used by the xautolock-session:

# make the file, set permissions and edit:
touch ~/bin/xautolock-session
chmod ug+x ~/bin/xautolock-session
nano ~/bin/xautolock-session

xautolock-session content:

#!/bin/bash

session-daemon xautolock -locker slock -time 5 -detectsleep -secure -corners "-00+"

That will be executed by Awesome on restart. Edit the Awesome config again:

nano ~/.config/awesome/rc.lua

And add this line at the bottom:

awful.util.spawn_with_shell("~/bin/xautolock-session")

Now when you log out and in again, your session will be automatically locked after 5 minutes.

Note: We could use dm-tool lock, too, but there's a freaky bug that actually breaks the whole idea: Lock can be circumvented by switching to console

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.