0

I'm currently on elementary OS Freya (Ubuntu 16.04 base). I'd like to run a script when the session is locked and when it is unlocked.

I have asked a similar question here for Ubuntu: https://unix.stackexchange.com/questions/28181/run-script-on-screen-lock-unlock

However, none of these solutions appear to catch the lock/unlock of the session manager. I'm basically locking my session using Super+L and then logging back in with my password.

I'm following the dbus main bus via dbus-monitor and the session bus with dbus-monitor --session and grepping for lock/unlock, but I can't seem to find what I'm looking for.

Can one of the elementary developers comment on what DBus event I should be listening for in order to see when the screen has been locked and unlocked?

0

I'm able to capture the lock event with:

dbus-monitor --session "path='/org/gnome/ScreenSaver',interface='org.gnome.ScreenSaver',member='Lock'"

The contents of the lock event looks like:

method call time=1512800651.865886 sender=:1.7 -> destination=:1.33 serial=1326 path=/org/gnome/ScreenSaver; interface=org.gnome.ScreenSaver; member=Lock

The unlock event can be captured using:

dbus-monitor --session "type='signal',path='/org/gnome/SessionManager',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'"

And looks like:

signal time=1512800661.088861 sender=:1.4 -> destination=(null destination) serial=237 path=/org/gnome/SessionManager; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
   string "org.gnome.SessionManager"
   array [
      dict entry(
         string "SessionIsActive"
         variant             boolean true
      )
   ]
   array [
   ]

Of course, you can specify both watch expressions to capture everything:

dbus-monitor "path='/org/gnome/ScreenSaver',interface='org.gnome.ScreenSaver',member='Lock'" "type='signal',path='/org/gnome/SessionManager',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'"

Here's a perl script that will output locked when the screen is locked, and unlocked when the screen is unlocked.

#!/usr/bin/perl

my $capture = 0;
while (<>) {

    # print "locked" to stdout if we see the 'lock screen' event
    if (/path\=\/org\/gnome\/ScreenSaver\; interface\=org\.gnome\.ScreenSaver\; member\=Lock/) {
        print "locked\n";
    }

    # To detect "unlock screen" events, we need to examine the
    # event data. Event data is indent from the rest of the
    # output, so we just need to collect all the indented lines
    # into a buffer and then examine the buffer contents.

    # stop capturing if the line isn't indented
    if (!/^\s+/) {
        $capture = 0;
    }

    # start capturing event data if we see the unlock event
    if ($capture eq 0) {
        next unless /path\=\/org\/gnome\/SessionManager\; interface\=org\.freedesktop\.DBus\.Properties\; member\=PropertiesChanged/;
        $capture = 1;
    }

    # collect lines into a buffer, stripping the line endings
    chomp;
    $buf .= $_;

    # if the event data indicates the session is now active, then
    # the screen was unlocked and we print "unlocked" to stdout
    if ($buf =~ /org\.gnome\.SessionManager/ && $buf =~ /SessionIsActive/ && $buf =~ /variant\s+boolean true/) {
        print "unlocked\n";
        $capture = 0;
        $buf = "";
    }
}

Just pipe the output of dbus-monitor into the script.

dbus-monitor "path='/org/gnome/ScreenSaver',interface='org.gnome.ScreenSaver',member='Lock'" "type='signal',path='/org/gnome/SessionManager',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'" | ./script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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