Last active
August 29, 2015 13:56
-
-
Save lhw/9004410 to your computer and use it in GitHub Desktop.
logind power management via dbus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import dbus | |
import sys | |
import gtk | |
from optparse import OptionParser | |
LD_PATH="org.freedesktop.login1" | |
LD_OBJ="/org/freedesktop/login1" | |
LD_IFACE="org.freedesktop.login1.Manager" | |
bus = dbus.SystemBus() | |
login1 = bus.get_object(LD_PATH, LD_OBJ) | |
can_poweroff = login1.CanPowerOff(dbus_interface=LD_IFACE) == "yes" | |
can_reboot = login1.CanReboot(dbus_interface=LD_IFACE) == "yes" | |
can_suspend = login1.CanSuspend(dbus_interface=LD_IFACE) == "yes" | |
parser = OptionParser() | |
parser.add_option("-s", "--suspend", dest="do_suspend", action="store_true", help="Suspend PC", default=False) | |
parser.add_option("-p", "--halt", dest="do_halt", action="store_true", help="Power off the PC", default=False) | |
parser.add_option("-r", "--reboot", dest="do_reboot", action="store_true", help="Reboot the PC", default=False) | |
options, args = parser.parse_args() | |
def ask_first(action): | |
dialog = gtk.Dialog(action, None, 0, | |
(gtk.STOCK_NO, gtk.RESPONSE_NO, gtk.STOCK_YES, gtk.RESPONSE_YES)) | |
label = gtk.Label("Are you sure you want to " + action) | |
label.show() | |
dialog.get_content_area().add(label) | |
response = dialog.run() | |
dialog.destroy() | |
while gtk.events_pending(): | |
gtk.main_iteration(False) | |
return response == gtk.RESPONSE_YES | |
if options.do_suspend and can_suspend and ask_first("Suspend"): | |
login1.Suspend(True, dbus_interface=LD_IFACE) | |
sys.exit(0) | |
elif options.do_reboot and can_reboot and ask_first("Reboot"): | |
login1.Reboot(True, dbus_interface=LD_IFACE) | |
sys.exit(0) | |
elif options.do_halt and can_poweroff and ask_first("Halt"): | |
login1.PowerOff(True, dbus_interface=LD_IFACE) | |
sys.exit(0) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment