3

I need to implement a function that monitors the screen lock/unlock. I referred to the following article:
Run script on screen lock/unlock

My python script code works fine in Ubuntu 12.04, but it doesn't work in Ubuntu 14.04:

#!/usr/bin/env python  
import gobject  
import dbus  
from dbus.mainloop.glib import DBusGMainLoop  

def filter_cb(bus, message):
    if message.get_member() != "ActiveChanged":
        return
    args = message.get_args_list()
    if args[0] == True:
        print("Lock Screen")
    else:
        print("Unlock Screen")

DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
bus.add_match_string("type='signal',interface='org.gnome.ScreenSaver'")
bus.add_message_filter(filter_cb)
mainloop = gobject.MainLoop()
mainloop.run()

I also tried the command:

dbus-monitor --session "interface='org.gnome.ScreenSaver'"  

It outputs nothing when I lock/unlock the screen manually.

How can I monitor the screen lock/unlock in the Ubuntu 14.04?

yw5643
  • 151

2 Answers2

2

You are right. So just to have a proper answer here I modified your code to a working one (at least under Ubuntu 15.10, Unify):

#!/usr/bin/env python
import gobject
import dbus
from dbus.mainloop.glib import DBusGMainLoop

def filter_cb(bus, message):
if message.get_member() != "EventEmitted":
    return
args = message.get_args_list()
if args[0] == "desktop-lock":
    print("Lock Screen")
elif args[0] == "desktop-unlock":
    print("Unlock Screen")

DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
bus.add_match_string("type='signal',interface='com.ubuntu.Upstart0_6'")
bus.add_message_filter(filter_cb)
mainloop = gobject.MainLoop()
mainloop.run()
V-Mark
  • 316
  • 2
  • 8
  • 3
    The answer would be improved if the chief differences between original and modified versions were discussed. – Thomas Dickey Nov 11 '15 at 09:10
  • @ThomasDickey Everything is the same, just the "texts", like event type, event string and event arguments has changed. ...anyway I haven't got any votes for this post... – V-Mark Jan 20 '16 at 07:53
0

I think that I have found the answer:
On the Ubuntu 14.014, I should monitor the interface "com.ubuntu.Upstart0_6" instead of "org.gnome.ScreenSaver".

yw5643
  • 151