-
Notifications
You must be signed in to change notification settings - Fork 0
/
shutdown-manager-plain
executable file
·118 lines (104 loc) · 4.76 KB
/
shutdown-manager-plain
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
**************************************************************
* Description: A gtk GUI with buttons bound to commands. *
* It can be used as a logoff/shutdown interface *
* for windows managers that lack one. *
* *
* Licence : Public Domain. *
* *
* Author : Antonios Tsolis (2016) *
**************************************************************
'''
import os
import gtk
from collections import OrderedDict
# We define a get_resource_path function to help us find
# the path of our icons in the system:
def get_resource_path(rel_path):
dir_of_py_file = os.path.dirname(__file__)
rel_path_to_resource = os.path.join(dir_of_py_file, rel_path)
abs_path_to_resource = os.path.abspath(rel_path_to_resource)
return abs_path_to_resource
class SystemDialog (gtk.Window):
def __init__(self):
super(SystemDialog, self).__init__()
# We define a dictionary with button label-command key-value
# pairs. The reason we use an OrderedDict is that in python
# the simple dict does not keep the order of the keys and we
# do not want our buttons to appear in arbitrary order.
# Keep in mind that you have to edit the commands according
# to your system and your needs.
self.actions = OrderedDict([
("Cancel", None),
("Lock", "slock &"),
("Restart WM", "sudo killall dwm &"),
("Sleep", "sudo pm-suspend &"),
("Hibernate", "sudo pm-hibernate &"),
("Sleep + Hibernate", "sudo pm-suspend-hybrid &"),
("Logout", "sudo killall X &"),
("Reboot", "sudo reboot &"),
("Shutdown", "sudo poweroff &")])
self.buttons = {}
# We make our window undecorated, centered on screen
# and we keep it on top.
self.set_border_width(0)
self.set_decorated(False)
self.set_position(gtk.WIN_POS_CENTER)
self.set_keep_above(True)
# We define one vertical box and three horizontal boxes.
# The vertical box will contain and align vertical the 3
# horizontal boxes while each horizontal box while keep and align
# horizontally 3 of our buttons. Therefore, in the end, we will
# have a nice 3x3 buttons square,
self.vbox = gtk.VBox(True, 5)
self.hboxes = [gtk.HBox(True, 5), gtk.HBox(True, 5), gtk.HBox(True, 5)]
# We create our buttons, put some icons and labels on them,
# ‘connect’ their click event with a callback handler and pack
# them inside the empty horizontal boxes (3 buttons in each
# horizontal box).
c = 0
boxIndex = 0
for label in self.actions.keys():
self.buttons[label] = gtk.Button(label)
ico = gtk.Image()
ico.set_from_file(get_resource_path("images/"+label+".png"))
self.buttons[label].set_image(ico)
self.buttons[label].set_image_position(gtk.POS_TOP)
self.hboxes[boxIndex].pack_start(self.buttons[label])
self.buttons[label].connect('clicked', self.callback, label)
c += 1
if not (c % 3):
boxIndex += 1
# We pack the horizontal boxes inside the vertical box and
# the vertical box inside our window. Do not forget to show
# all our widgets with self.show_all().
for hbox in self.hboxes:
self.vbox.pack_start(hbox)
self.add(self.vbox)
self.show_all()
# If our window is deleted/destroyed call self.callback (to exit)
self.connect("delete-event", self.callback)
# If a key is pressed call self.key_press_event
self.connect("key-press-event", self.key_press_event)
# This is the our keyboard callback/event handler function.
# If the user has pressed Escape, we quit.
def key_press_event(self, widget=None, event=None):
keyval = event.keyval
keyval_name = gtk.gdk.keyval_name(keyval)
# state = event.state
# ctrl = (state & gtk.gdk.CONTROL_MASK)
if keyval_name == "Escape":
gtk.main_quit()
return False
# This is the our generic callback function
def callback(self, widget=None, data=None):
if (data is not None and
data in self.actions and
self.actions[data] is not None):
os.system(self.actions[data])
gtk.main_quit()
if __name__ == "__main__":
SystemDialog()
gtk.main()