@@ -89,20 +89,53 @@ def unlock_unity(unity_proxy_obj=None):
8989
9090
9191def lock_unity(unity_proxy_obj=None):
92− """Helper function that attempts to lock the unity greeter."""
93− import evdev
94− import time
95− uinput = evdev.UInput(name='unity8-autopilot-power-button',
96− devnode='/dev/autopilot-uinput')
97− # One press and release to turn screen off (locking unity)
98− uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 1)
99− uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 0)
100− uinput.syn()
101− time.sleep(1)
102− # And another press and release to turn screen back on
103− uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 1)
104− uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 0)
105− uinput.syn()
92+ """Helper function that attempts to lock unity greeter.
93+
94+ If unity_proxy_object is None create a proxy object by querying for the
95+ running unity process.
96+ Otherwise re-use the passed proxy object.
97+
98+ :raises RuntimeError: if the greeter attempts and fails to be locked.
99+
100+ :raises RuntimeWarning: when the greeter is found because it is
101+ already locked.
102+ :raises CannotAccessUnity: if unity is not introspectable or cannot be
103+ found on dbus.
104+ :raises CannotAccessUnity: if unity's upstart status is not "start" or the
105+ upstart job cannot be found at all.
106+
107+ """
108+ if unity_proxy_obj is None:
109+ try:
110+ pid = _get_unity_pid()
111+ unity = _get_unity_proxy_object(pid)
112+ main_window = unity.select_single(main_window_emulator.QQuickView)
113+ except ProcessSearchError as e:
114+ raise CannotAccessUnity(
115+ "Cannot introspect unity, make sure that it has been started "
116+ "with testability. Perhaps use the function "
117+ "'restart_unity_with_testability' this module provides."
118+ "(%s)"
119+ % str(e)
120+ )
121+ else:
122+ main_window = unity_proxy_obj.select_single(
123+ main_window_emulator.QQuickView)
124+
125+ greeter = main_window.get_greeter()
126+ if greeter.created is True:
127+ raise RuntimeWarning("Greeter appears to be already locked.")
128+
129+ bus = dbus.SessionBus()
130+ dbus_proxy = bus.get_object("com.canonical.UnityGreeter", "/")
131+ try:
132+ dbus_proxy.ShowGreeter()
133+ except dbus.DBusException:
134+ logger.info("Failed to lock greeter")
135+ raise
136+ else:
137+ greeter.created.wait_for(True)
138+ logger.info("Greeter locked, continuing.")
106139
107140
108141def restart_unity_with_testability(*args):