@@ -89,20 +89,53 @@ def unlock_unity(unity_proxy_obj=None):
91def lock_unity(unity_proxy_obj=None):
91def lock_unity(unity_proxy_obj=None):
92− """Helper function that attempts to lock the unity greeter."""
92+ """Helper function that attempts to lock unity greeter.
94− import time
94+ If unity_proxy_object is None create a proxy object by querying for the
95− uinput = evdev.UInput(name='unity8-autopilot-power-button',
95+ running unity process.
96− devnode='/dev/autopilot-uinput')
96+ Otherwise re-use the passed proxy object.
97− # One press and release to turn screen off (locking unity)
97+
98− uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 1)
98+ :raises RuntimeError: if the greeter attempts and fails to be locked.
99− uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 0)
99+
100− uinput.syn()
100+ :raises RuntimeWarning: when the greeter is found because it is
101− time.sleep(1)
101+ already locked.
102− # And another press and release to turn screen back on
102+ :raises CannotAccessUnity: if unity is not introspectable or cannot be
103− uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 1)
103+ found on dbus.
104− uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 0)
104+ :raises CannotAccessUnity: if unity's upstart status is not "start" or the
105− uinput.syn()
105+ upstart job cannot be found at all.
108+ if unity_proxy_obj is None:
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."
122+ main_window = unity_proxy_obj.select_single(
123+ main_window_emulator.QQuickView)
125+ greeter = main_window.get_greeter()
126+ if greeter.created is True:
127+ raise RuntimeWarning("Greeter appears to be already locked.")
129+ bus = dbus.SessionBus()
130+ dbus_proxy = bus.get_object("com.canonical.UnityGreeter", "/")
132+ dbus_proxy.ShowGreeter()
133+ except dbus.DBusException:
134+ logger.info("Failed to lock greeter")
137+ greeter.created.wait_for(True)
138+ logger.info("Greeter locked, continuing.")
108def restart_unity_with_testability(*args):
141def restart_unity_with_testability(*args):