Skip to content

Instantly share code, notes, and snippets.

@nealmcb
Created July 16, 2012 05:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nealmcb/3120773 to your computer and use it in GitHub Desktop.
Save nealmcb/3120773 to your computer and use it in GitHub Desktop.
Track and print out the apps and clients that are inhibiting Gnome session actions like log out, user switching, suspending or idle/dpms/screensaver, under Linux.
#!/usr/bin/python
"""
Print out the apps and clients that are inhibiting session actions like log out, user switching, suspending or idle.
%InsertOptionParserUsage%
Example:
track_session_inhibitors &
Todo:
Remember info about inhibitors and clients, for display when the are removed
describe flags with mnemonics. Consider using bitstring package:
http://packages.python.org/bitstring/walkthrough.html
Flags:
1: Inhibit logging out
2: Inhibit user switching
4: Inhibit suspending the session or computer
8: Inhibit the session being marked as idle
References:
http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html#org.gnome.SessionManager.Inhibitor
http://www.devtech.com/inhibitapplet
"""
import sys
import traceback
from gi.repository import GObject
import dbus
import dbus.mainloop.glib
import pynotify
import logging
__author__ = "Neal McBurnett <http://neal.mcburnett.org/>"
__version__ = "0.1.0"
__date__ = "2012-07-12"
__copyright__ = "Copyright (c) 2012 Neal McBurnett"
__license__ = "GPL v3"
def inhibitor_dump(inhibitor_id):
status = "%s " % inhibitor_id
inhibitor = bus.get_object('org.gnome.SessionManager', inhibitor_id)
interface = inhibitor.Introspect()
# import pdb; pdb.set_trace() # to figure out if there is anything to show for InhibitorRemoved
if interface.count("org.gnome.SessionManager.Inhibitor") > 0:
try:
clientId = inhibitor.GetClientId()
except dbus.exceptions.DBusException, e:
clientId = "(null)"
status += "flags=%x, x11id=%d: %s \"%s\" %s" % (inhibitor.GetFlags(), inhibitor.GetToplevelXid(), inhibitor.GetAppId(), inhibitor.GetReason(), clientId)
else:
status += "(null)"
if len(sys.argv) <= 1:
n = pynotify.Notification ("SessionManager Inhibitor Watch", status)
n.show ()
return status
def client_dump(client_id):
status = "%s " % client_id
client = bus.get_object('org.gnome.SessionManager', client_id)
interface = client.Introspect()
# import pdb; pdb.set_trace() # to debug
if interface.count("org.gnome.SessionManager.Client") > 0:
# print client.GetAll('org.gnome.SessionManager') # why does this always fail with UnknownMethod: Method "GetAll" with signature "s" on interface "(null)"?
try:
# Why does this always fail?
clientId = client.GetClientId()
except dbus.exceptions.DBusException, e:
clientId = "(null)"
#logging.exception("Exception with GetClientId")
status += "process=%d: %s \"%s\" %s" % (client.GetUnixProcessId(), client.GetAppId(), client.GetStatus(), clientId)
else:
status += "(null)"
if len(sys.argv) <= 1:
n = pynotify.Notification ("SessionManager Client Watch", status)
n.show ()
return status
def session_inhibitor_signal_handler(id):
logging.info(inhibitor_dump(id))
def session_client_signal_handler(id):
logging.info(client_dump(id))
if __name__ == '__main__':
if len(sys.argv) >1:
# for now treat any argument as a request to just print current inhibitors and clients to stdout, and then exit
logfile = None
else:
logfile = "/var/log/session_inhibitors"
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s\t%(message)s', filename = logfile, filemode='a' )
logging.info("Start find_session_inhibitors")
if not pynotify.init ("summary-body"):
logging.error("pynotify.init failed")
sys.exit (1)
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
proxy = bus.get_object('org.gnome.SessionManager','/org/gnome/SessionManager')
blockers = proxy.GetInhibitors()
for objectpath in blockers:
logging.info(inhibitor_dump(objectpath))
clients = proxy.GetClients()
for objectpath in clients:
logging.info(client_dump(objectpath))
if len(sys.argv) >1:
sys.exit(0)
bus.add_signal_receiver(session_inhibitor_signal_handler, "InhibitorAdded", "org.gnome.SessionManager")
bus.add_signal_receiver(session_inhibitor_signal_handler, "InhibitorRemoved", "org.gnome.SessionManager")
bus.add_signal_receiver(session_client_signal_handler, "ClientAdded", "org.gnome.SessionManager")
bus.add_signal_receiver(session_client_signal_handler, "ClientRemoved", "org.gnome.SessionManager")
logging.info("Start listening for signals")
mainloop = GObject.MainLoop()
mainloop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment