| 1 |
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- |
| 2 |
# |
| 3 |
# Copyright (C) 2012, 2013, 2014, 2015 Canonical Ltd. |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU Lesser General Public License as published by |
| 7 |
# the Free Software Foundation; version 3. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU Lesser General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU Lesser General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
"""Common helpers for Lomiri UI Toolkit Autopilot custom proxy objects.""" |
| 18 |
|
| 19 |
import logging |
| 20 |
import os |
| 21 |
import subprocess |
| 22 |
import time |
| 23 |
from distutils import version |
| 24 |
from gi.repository import Gio |
| 25 |
|
| 26 |
import autopilot |
| 27 |
from autopilot import ( |
| 28 |
input, |
| 29 |
logging as autopilot_logging, |
| 30 |
platform |
| 31 |
) |
| 32 |
from autopilot.introspection import dbus |
| 33 |
|
| 34 |
logger = logging.getLogger(__name__) |
| 35 |
|
| 36 |
MALIIT = 'maliit-server' |
| 37 |
|
| 38 |
|
| 39 |
class ToolkitException(Exception): |
| 40 |
"""Exception raised when there is an error with the custom proxy object.""" |
| 41 |
|
| 42 |
|
| 43 |
def get_pointing_device(): |
| 44 |
"""Return the pointing device depending on the platform. |
| 45 |
|
| 46 |
If the platform is `Desktop`, the pointing device will be a `Mouse`. |
| 47 |
If not, the pointing device will be `Touch`. |
| 48 |
|
| 49 |
""" |
| 50 |
if platform.model() == 'Desktop': |
| 51 |
input_device_class = input.Mouse |
| 52 |
else: |
| 53 |
input_device_class = input.Touch |
| 54 |
return input.Pointer(device=input_device_class.create()) |
| 55 |
|
| 56 |
|
| 57 |
def get_keyboard(): |
| 58 |
"""Return the keyboard device.""" |
| 59 |
if is_maliit_process_running(): |
| 60 |
configure_osk_settings() |
| 61 |
restart_maliit_with_testability() |
| 62 |
return input.Keyboard.create('OSK') |
| 63 |
else: |
| 64 |
return input.Keyboard.create() |
| 65 |
|
| 66 |
|
| 67 |
def restart_maliit_with_testability(): |
| 68 |
"""Restart maliit-server with testability enabled.""" |
| 69 |
pid = get_process_pid(MALIIT) |
| 70 |
if _is_testability_enabled_for_process(pid): |
| 71 |
return |
| 72 |
_stop_process(MALIIT) |
| 73 |
_start_process(MALIIT, 'QT_LOAD_TESTABILITY=1') |
| 74 |
# This is needed to work around https://launchpad.net/bugs/1248913 |
| 75 |
time.sleep(5) |
| 76 |
|
| 77 |
|
| 78 |
def configure_osk_settings(): |
| 79 |
"""Configure OSK ready for testing by turning off all helpers.""" |
| 80 |
gsettings = Gio.Settings.new("com.lomiri.keyboard.maliit") |
| 81 |
gsettings.set_string("active-language", "en") |
| 82 |
gsettings.set_boolean("auto-capitalization", False) |
| 83 |
gsettings.set_boolean("auto-completion", False) |
| 84 |
gsettings.set_boolean("predictive-text", False) |
| 85 |
gsettings.set_boolean("spell-checking", False) |
| 86 |
gsettings.set_boolean("double-space-full-stop", False) |
| 87 |
|
| 88 |
|
| 89 |
def _is_testability_enabled_for_process(pid): |
| 90 |
"""Return True if testability is enabled for specified process.""" |
| 91 |
proc_env = '/proc/{pid}/environ'.format(pid=pid) |
| 92 |
command = ['cat', proc_env] |
| 93 |
try: |
| 94 |
output = subprocess.check_output( |
| 95 |
command, |
| 96 |
stderr=subprocess.STDOUT, |
| 97 |
universal_newlines=True, |
| 98 |
) |
| 99 |
except subprocess.CalledProcessError as e: |
| 100 |
e.args += ('Failed to get environment for pid {}: {}.'.format( |
| 101 |
pid, e.output),) |
| 102 |
raise |
| 103 |
return output.find('QT_LOAD_TESTABILITY=1') >= 0 |
| 104 |
|
| 105 |
|
| 106 |
def _stop_process(proc_name): |
| 107 |
"""Stop process with name proc_name.""" |
| 108 |
logger.info('Stoping process {}.'.format(proc_name)) |
| 109 |
command = ['/sbin/initctl', 'stop', proc_name] |
| 110 |
try: |
| 111 |
output = subprocess.check_output( |
| 112 |
command, |
| 113 |
stderr=subprocess.STDOUT, |
| 114 |
universal_newlines=True, |
| 115 |
) |
| 116 |
logger.info(output) |
| 117 |
except subprocess.CalledProcessError as e: |
| 118 |
e.args += ('Failed to stop {}: {}.'.format(proc_name, e.output),) |
| 119 |
raise |
| 120 |
|
| 121 |
|
| 122 |
def _start_process(proc_name, *args): |
| 123 |
"""Start a process. |
| 124 |
|
| 125 |
:param proc_name: The name of the process. |
| 126 |
:param args: The arguments to be used when starting the job. |
| 127 |
:return: The process id of the started job. |
| 128 |
:raises CalledProcessError: if the job failed to start. |
| 129 |
|
| 130 |
""" |
| 131 |
logger.info('Starting job {} with arguments {}.'.format(proc_name, args)) |
| 132 |
command = ['/sbin/initctl', 'start', proc_name] + list(args) |
| 133 |
try: |
| 134 |
output = subprocess.check_output( |
| 135 |
command, |
| 136 |
stderr=subprocess.STDOUT, |
| 137 |
universal_newlines=True, |
| 138 |
) |
| 139 |
logger.info(output) |
| 140 |
pid = get_process_pid(proc_name) |
| 141 |
except subprocess.CalledProcessError as e: |
| 142 |
e.args += ('Failed to start {}: {}.'.format(proc_name, e.output),) |
| 143 |
raise |
| 144 |
else: |
| 145 |
return pid |
| 146 |
|
| 147 |
|
| 148 |
def get_process_pid(proc_name): |
| 149 |
"""Return the process id of a running job. |
| 150 |
|
| 151 |
:param str name: The name of the job. |
| 152 |
:raises ToolkitException: if the job is not running. |
| 153 |
|
| 154 |
""" |
| 155 |
status = get_process_status(proc_name) |
| 156 |
if "start/" not in status: |
| 157 |
raise ToolkitException( |
| 158 |
'{} is not in the running state.'.format(proc_name)) |
| 159 |
return int(status.split()[-1]) |
| 160 |
|
| 161 |
|
| 162 |
def get_process_status(name): |
| 163 |
""" |
| 164 |
Return the status of a process. |
| 165 |
|
| 166 |
:param str name: The name of the process. |
| 167 |
:return: Status of process as string, or zero length string if process |
| 168 |
is not found. |
| 169 |
|
| 170 |
""" |
| 171 |
try: |
| 172 |
return subprocess.check_output([ |
| 173 |
'/sbin/initctl', |
| 174 |
'status', |
| 175 |
name |
| 176 |
], universal_newlines=True, stderr=subprocess.DEVNULL) |
| 177 |
except subprocess.CalledProcessError: |
| 178 |
return '' |
| 179 |
|
| 180 |
|
| 181 |
def is_process_running(name): |
| 182 |
"""Return True if the process is running. Otherwise, False. |
| 183 |
|
| 184 |
:param str name: The name of the process. |
| 185 |
:raises ToolkitException: if not possible to get status of the process. |
| 186 |
|
| 187 |
""" |
| 188 |
return 'start/' in get_process_status(name) |
| 189 |
|
| 190 |
|
| 191 |
def is_maliit_process_running(): |
| 192 |
"""Return True if malitt-server process is running, False otherwise.""" |
| 193 |
# FIXME: lp#1542224 Use Maliit by default |
| 194 |
if 'UITK_USE_MALIIT' not in os.environ: |
| 195 |
logger.info("Not going to use Maliit\ |
| 196 |
- set UITK_USE_MALIIT to enable it") |
| 197 |
return False |
| 198 |
if is_process_running(MALIIT): |
| 199 |
logger.info('Using Maliit for keyboard input') |
| 200 |
return True |
| 201 |
logger.info('Maliit enabled but not running on the system') |
| 202 |
return False |
| 203 |
|
| 204 |
|
| 205 |
def check_autopilot_version(): |
| 206 |
"""Check that the Autopilot installed version matches the one required. |
| 207 |
|
| 208 |
:raise ToolkitException: If the installed Autopilot version does't |
| 209 |
match the required by the custom proxy objects. |
| 210 |
|
| 211 |
""" |
| 212 |
installed_version = version.LooseVersion(autopilot.version) |
| 213 |
if installed_version < version.LooseVersion('1.4'): |
| 214 |
raise ToolkitException( |
| 215 |
'The custom proxy objects require Autopilot 1.4 or higher.') |
| 216 |
|
| 217 |
|
| 218 |
class LomiriUIToolkitCustomProxyObjectBase(dbus.CustomEmulatorBase): |
| 219 |
"""A base class for all the Lomiri UI Toolkit custom proxy objects.""" |
| 220 |
|
| 221 |
def __init__(self, *args): |
| 222 |
check_autopilot_version() |
| 223 |
super().__init__(*args) |
| 224 |
self.pointing_device = get_pointing_device() |
| 225 |
|
| 226 |
def is_flickable(self): |
| 227 |
"""Check if the object is flickable. |
| 228 |
|
| 229 |
If the object has a flicking attribute, we consider it as a flickable. |
| 230 |
|
| 231 |
:return: True if the object is flickable. False otherwise. |
| 232 |
|
| 233 |
""" |
| 234 |
try: |
| 235 |
self.flicking |
| 236 |
return True |
| 237 |
except AttributeError: |
| 238 |
return False |
| 239 |
|
| 240 |
@autopilot_logging.log_action(logger.info) |
| 241 |
def swipe_into_view(self): |
| 242 |
"""Make the object visible. |
| 243 |
|
| 244 |
Currently it works only when the object needs to be swiped vertically. |
| 245 |
TODO implement horizontal swiping. --elopio - 2014-03-21 |
| 246 |
|
| 247 |
""" |
| 248 |
flickable_parent = self._get_flickable_parent() |
| 249 |
flickable_parent.swipe_child_into_view(self) |
| 250 |
|
| 251 |
def _get_flickable_parent(self): |
| 252 |
parent = self.get_parent() |
| 253 |
root = self.get_root_instance() |
| 254 |
while parent.id != root.id: |
| 255 |
if parent.is_flickable(): |
| 256 |
return parent |
| 257 |
parent = parent.get_parent() |
| 258 |
raise ToolkitException( |
| 259 |
"The element is not contained in a Flickable so it can't be " |
| 260 |
"swiped into view.") |
| 261 |
|
| 262 |
def _get_top_container(self): |
| 263 |
"""Return the top-most container with a globalRect.""" |
| 264 |
root = self.get_root_instance() |
| 265 |
parent = self.get_parent() |
| 266 |
top_container = None |
| 267 |
while parent.id != root.id: |
| 268 |
if hasattr(parent, 'globalRect'): |
| 269 |
top_container = parent |
| 270 |
|
| 271 |
parent = parent.get_parent() |
| 272 |
|
| 273 |
if top_container is None: |
| 274 |
raise ToolkitException('Could not find the top-most container.') |
| 275 |
else: |
| 276 |
return top_container |