-
Notifications
You must be signed in to change notification settings - Fork 11
/
inhibit.py
executable file
·98 lines (81 loc) · 3.03 KB
/
inhibit.py
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
#!/usr/bin/env python3
# Disable/Inhibit Gnome-Shell screen blanking on idle until this program
# terminates.
#
# SPDX-FileCopyrightText: © 2020 Georg Sauthoff <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
import argparse
import pydbus
import sys
import time
# cf. /usr/include/gtk-3.0/gtk/gtkapplication.h
INHIBIT_LOGOUT = 1
INHIBIT_SWITCH = 2
INHIBIT_SUSPEND = 4
INHIBIT_IDLE = 8
def reasons(x):
rs = []
if x & INHIBIT_LOGOUT:
rs.append('LOGOUT')
if x & INHIBIT_SWITCH:
rs.append('SWITCH')
if x & INHIBIT_SUSPEND:
rs.append('SUSPEND')
if x & INHIBIT_IDLE:
rs.append('IDLE')
return rs
def parse_args(*a):
p = argparse.ArgumentParser(description='Inhibit screen blanking in Gnome-Shell')
p.add_argument('--list', '-l', action='store_true',
help='list active inhibitors')
p.add_argument('--check', '-c', type=int, metavar='FLAGS',
help='check for active inhibitors and set exit status accordingly (1|2|4|8 => LOGOUT|SWITCH|SUSPEND|IDLE)')
p.add_argument('--flags', type=int, metavar='FLAGS', default=INHIBIT_IDLE,
help='flags to use for inhibit action (1|2|4|8 => LOGOUT|SWITCH|SUSPEND|IDLE) (default: %(default)i)')
p.add_argument('--time', '-t', type=int, metavar='SECONDS', default=2**32-1,
help='sleep time, i.e. time the inhibitor is active (default: %(default)i)')
return p.parse_args(*a)
# def inhibit_old():
# import dbus
# bus = dbus.SessionBus()
# sm = bus.get_object('org.gnome.SessionManager','/org/gnome/SessionManager')
# sm.Inhibit("coffee", dbus.UInt32(0), "keeps you busy", dbus.UInt32(8),
# dbus_interface='org.gnome.SessionManager')
# time.sleep(2**32-1)
def inhibit(flags, secs):
bus = pydbus.SessionBus()
# we could leave out object_path because it defaults to bus_name
# with . replaced by / ...
sm = bus.get(bus_name='org.gnome.SessionManager',
object_path='/org/gnome/SessionManager')
# 'org.gnome.SesssionManager' is also the interface name ...
xid = 0
# again, we could leave out interface name, as it defaults to that
sm['org.gnome.SessionManager'].Inhibit('inhibit.py', xid,
'user requested idle-off from terminal', flags)
time.sleep(secs)
def check_inhibited(flags):
bus = pydbus.SessionBus()
sm = bus.get(bus_name='org.gnome.SessionManager')
r = sm.IsInhibited(flags)
return r
def list_inhibitors():
bus = pydbus.SessionBus()
sm = bus.get(bus_name='org.gnome.SessionManager')
xs = sm.GetInhibitors()
for x in xs:
p = bus.get(bus_name='org.gnome.SessionManager', object_path=x)
print(f'{p.GetAppId()} {p.GetReason()} flags={"|".join(reasons(p.GetFlags()))}')
def main():
args = parse_args()
if args.list:
list_inhibitors()
elif args.check:
return(not check_inhibited(args.check))
else:
try:
inhibit(args.flags, args.time)
except KeyboardInterrupt:
pass
if __name__ == '__main__':
sys.exit(main())