Skip to content

Instantly share code, notes, and snippets.

@jcyamo
Last active August 28, 2024 17:54
Show Gist options
  • Save jcyamo/5675359 to your computer and use it in GitHub Desktop.
Save jcyamo/5675359 to your computer and use it in GitHub Desktop.
Exit script for the i3 window manager based off of exit script from CrunchBang Linux.
#!/usr/bin/env python
# based on cb-exit used in CrunchBang Linux <http://crunchbanglinux.org/>
import pygtk
pygtk.require('2.0')
import gtk
import os
import getpass
class i3_exit:
def disable_buttons(self):
self.cancel.set_sensitive(False)
self.logout.set_sensitive(False)
self.suspend.set_sensitive(False)
self.reboot.set_sensitive(False)
self.shutdown.set_sensitive(False)
def cancel_action(self,btn):
self.disable_buttons()
gtk.main_quit()
def logout_action(self,btn):
self.disable_buttons()
self.status.set_label("Exiting i3, please standby...")
os.system("i3-msg exit")
def suspend_action(self,btn):
self.disable_buttons()
self.status.set_label("Suspending, please standby...")
os.system("i3-lock")
os.system("dbus-send --system --print-reply \
--dest=\"org.freedesktop.UPower\" \
/org/freedesktop/UPower \
org.freedesktop.UPower.Suspend")
gtk.main_quit()
def reboot_action(self,btn):
self.disable_buttons()
self.status.set_label("Rebooting, please standby...")
os.system("dbus-send --system --print-reply \
--dest=\"org.freedesktop.ConsoleKit\" \
/org/freedesktop/ConsoleKit/Manager \
org.freedesktop.ConsoleKit.Manager.Restart")
def shutdown_action(self,btn):
self.disable_buttons()
self.status.set_label("Shutting down, please standby...")
os.system("dbus-send --system --print-reply \
--dest=\"org.freedesktop.ConsoleKit\" \
/org/freedesktop/ConsoleKit/Manager \
org.freedesktop.ConsoleKit.Manager.Stop")
def create_window(self):
self.window = gtk.Window()
title = "Log out " + getpass.getuser() + "? Choose an option:"
self.window.set_title(title)
self.window.set_border_width(5)
self.window.set_size_request(500, 80)
self.window.set_resizable(False)
self.window.set_keep_above(True)
self.window.stick
self.window.set_position(1)
self.window.connect("delete_event", gtk.main_quit)
windowicon = self.window.render_icon(gtk.STOCK_QUIT, gtk.ICON_SIZE_MENU)
self.window.set_icon(windowicon)
#Create HBox for buttons
self.button_box = gtk.HBox()
self.button_box.show()
#Cancel button
self.cancel = gtk.Button(stock = gtk.STOCK_CANCEL)
self.cancel.set_border_width(4)
self.cancel.connect("clicked", self.cancel_action)
self.button_box.pack_start(self.cancel)
self.cancel.show()
#Logout button
self.logout = gtk.Button("_Log out")
self.logout.set_border_width(4)
self.logout.connect("clicked", self.logout_action)
self.button_box.pack_start(self.logout)
self.logout.show()
#Suspend button
self.suspend = gtk.Button("_Suspend")
self.suspend.set_border_width(4)
self.suspend.connect("clicked", self.suspend_action)
self.button_box.pack_start(self.suspend)
self.suspend.show()
#Reboot button
self.reboot = gtk.Button("_Reboot")
self.reboot.set_border_width(4)
self.reboot.connect("clicked", self.reboot_action)
self.button_box.pack_start(self.reboot)
self.reboot.show()
#Shutdown button
self.shutdown = gtk.Button("_Power off")
self.shutdown.set_border_width(4)
self.shutdown.connect("clicked", self.shutdown_action)
self.button_box.pack_start(self.shutdown)
self.shutdown.show()
#Create HBox for status label
self.label_box = gtk.HBox()
self.label_box.show()
self.status = gtk.Label()
self.status.show()
self.label_box.pack_start(self.status)
#Create VBox and pack the above HBox's
self.vbox = gtk.VBox()
self.vbox.pack_start(self.button_box)
self.vbox.pack_start(self.label_box)
self.vbox.show()
self.window.add(self.vbox)
self.window.show()
def __init__(self):
self.create_window()
def main():
gtk.main()
if __name__ == "__main__":
go = i3_exit()
main()
@driscoll
Copy link

To force this window to float, bind it like this in your ~/.i3/config:

bindsym $mod+Shift+E exec ~/.i3/i3-exit
for_window [class="I3-exit" instance="i3-exit"] floating enable

@patrickbolle
Copy link

No go here. It worked on my last system. Just reinstalled (debian net install with i3wm) and nothing happens after clicking reboot/shutdown.
Get this error - Error org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.ConsoleKit was not provided by any .service files

@commonquail
Copy link

As noted by Ruben G the service changed in Ubuntu 14.04. The following are current as of today:

dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 \
    "org.freedesktop.login1.Manager.Suspend" boolean:true

dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 \
    "org.freedesktop.login1.Manager.Reboot" boolean:true

dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 \
    "org.freedesktop.login1.Manager.PowerOff" boolean:true

@azzamsa
Copy link

azzamsa commented Jul 26, 2017

@commonquail, do you have the updated source ? I try to change the value of (foo) to your (new foo boolean:true) , but I got eror "wrong syntax" .

but when executing your script directly in terminal, it works.

Copy link

ghost commented Nov 16, 2017

I am running this from a hotkey in terminal but it is not actually shutting down anything. Anyone else experiencing this problem?

@47647538
Copy link

If the above code doesn't solve your problem, would you try this: https://sourceforge.net/projects/logout-for-i3-window-manager/

Just installed it on Debian 10. Works great, thanks a lot!
How would I get the screen locked after "suspend"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment