6
from gi.repository import GObject
8
from checkbox_support.dbus import connect_to_system_bus
13
GObject.threads_init()
15
class SceenSaverStatusHelper(threading.Thread):
17
def __init__(self, loop):
18
super(SceenSaverStatusHelper, self).__init__()
23
p = subprocess.Popen(["gnome-screensaver-command", "-q"], stdout=subprocess.PIPE)
24
stdout, stderr = p.communicate()
25
# parse the stdout string from the command "gnome-screensaver-command -q"
26
# the result should be "active" or "inactive"
27
if "active" == stdout.decode().split(" ")[-1][0:-1] :
28
print("the screensaver is active")
33
GObject.idle_add(self.query)
37
class HotkeyFunctionListener:
39
def __init__(self, system_bus, loop):
40
self._bus = system_bus
42
# Assume the test passes, this is changed when timeout expires
45
def _on_timeout_expired(self):
47
Internal function called when the timer expires.
49
Basically it's just here to tell the user the test failed or that the
50
user was unable to pressed the hot key during the allowed time.
52
print("You have failed to perform the required manipulation in time")
53
# Fail the test when the timeout was reached
58
def check(self, timeout):
60
Run the configured test and return the result
62
The result is False if the test has failed. The timeout, when
63
non-zero, will make the test fail after the specified seconds have
64
elapsed without conclusive result.
66
# Setup a timeout if requested
68
GObject.timeout_add_seconds(timeout, self._on_timeout_expired)
70
# helper to listen the functionality is triggered or not
71
query_thread = SceenSaverStatusHelper(self._loop)
75
query_thread.quit = True
76
# Return the outcome of the test
80
description = "Wait for the specified hotkey to be pressed."
81
parser = argparse.ArgumentParser(description=description)
82
parser.add_argument('--timeout', type=int, default=30)
84
args = parser.parse_args()
86
# Connect to the system bus, we also get the event
87
# loop as we need it to start listening for signals.
88
system_bus, loop = connect_to_system_bus()
90
listener = HotkeyFunctionListener(system_bus, loop)
92
# Run the actual listener and wait till it either times out or discovers
93
# the specific hot key pressed.
95
return listener.check(args.timeout)
96
except KeyboardInterrupt:
99
if __name__ == "__main__":