Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | # |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 5 | """ This module manages the platform properties in mttools/platforms """ |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 6 | import json |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 7 | import os |
| 8 | import re |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 9 | import sys |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 10 | from xorg_conf import XorgInputClassParser |
Dennis Kempin | d0b722a | 2014-04-15 11:54:48 -0700 | [diff] [blame] | 11 | from cros_remote import CrOSRemote |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 12 | |
| 13 | # path to current script directory |
| 14 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 15 | platforms_dir = os.path.realpath(os.path.join(script_dir, '..', 'platforms')) |
Chung-yih Wang | 35613f1 | 2014-04-25 13:57:23 +0800 | [diff] [blame^] | 16 | xorg_conf_project_path = 'src/platform/xorg-conf' |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 17 | |
Dennis Kempin | d0b722a | 2014-04-15 11:54:48 -0700 | [diff] [blame] | 18 | props_template = """\ |
| 19 | { |
| 20 | "gestures": { |
| 21 | }, |
| 22 | "xorg": { |
| 23 | "file": "%s", |
| 24 | "identifiers": %s |
| 25 | }, |
| 26 | "ignore": [ |
| 27 | ] |
| 28 | }""" |
| 29 | |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 30 | class PlatformProperties(object): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 31 | """ A class containing hardware and xorg properties for a platform. |
| 32 | |
| 33 | The class can be created from an activity log or by providing |
| 34 | the name of the platform. Information will then be read from the |
| 35 | 'platforms_dir' directory. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 36 | """ |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 37 | def __init__(self, platform=None, log=None): |
| 38 | self.required_axis = [] |
| 39 | self.has_axis = [] |
| 40 | self.device_class = "touchpad" |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 41 | if platform: |
| 42 | basename = os.path.join(platforms_dir, platform) |
| 43 | self.name = platform |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 44 | self.hwprops_file = basename + '.hwprops' |
| 45 | self.props_file = basename + '.props' |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 46 | self.xorg_parser = XorgInputClassParser() |
| 47 | self._ParseHWProperties(open(self.hwprops_file).read()) |
| 48 | self._ParseProperties(open(self.props_file).read()) |
| 49 | self._UpdateDimensions() |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 50 | elif log: |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 51 | self.name = '' |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 52 | if log.evdev: |
| 53 | self._ParseEvdevLog(log.evdev) |
Dennis Kempin | cd7caba | 2014-04-16 13:37:18 -0700 | [diff] [blame] | 54 | self._ParseActivityLog(log.activity) |
| 55 | |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 56 | |
| 57 | def _ParseActivityLog(self, activity_data): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 58 | """ Parse property information from an activity log """ |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 59 | activity = json.loads(activity_data) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 60 | self.properties = activity['properties'] |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 61 | |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 62 | hwprops = activity['hardwareProperties'] |
| 63 | self.x_min = int(hwprops['left']) |
| 64 | self.x_max = int(hwprops['right']) |
| 65 | self.x_res = int(hwprops['xResolution']) |
| 66 | self.y_min = int(hwprops['top']) |
| 67 | self.y_max = int(hwprops['bottom']) |
| 68 | self.y_res = int(hwprops['yResolution']) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 69 | |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 70 | def _ParseEvdevLog(self, evdev_data): |
Dennis Kempin | 143ef16 | 2014-04-09 13:46:50 -0700 | [diff] [blame] | 71 | # Look for embedded hwproperties in header. Format: |
| 72 | # absinfo: axis min max 0 0 res |
| 73 | abs_regex = 5 * ' ([0-9]+)' |
| 74 | xregex = re.compile('# absinfo: 53' + abs_regex) |
| 75 | xmatch = xregex.search(evdev_data); |
| 76 | self.x_min = int(xmatch.group(1)) |
| 77 | self.x_max = int(xmatch.group(2)) |
| 78 | self.x_res = int(xmatch.group(5)) |
| 79 | |
| 80 | yregex = re.compile('# absinfo: 54' + abs_regex) |
| 81 | ymatch = yregex.search(evdev_data); |
| 82 | self.y_min = int(ymatch.group(1)) |
| 83 | self.y_max = int(ymatch.group(2)) |
| 84 | self.y_res = int(ymatch.group(5)) |
| 85 | |
| 86 | axis_regex = re.compile('# absinfo: ([0-9]+)') |
| 87 | for match in axis_regex.finditer(evdev_data): |
| 88 | self.has_axis.append(int(match.group(1))) |
| 89 | |
| 90 | # look for axes used in the log itself. |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 91 | # The format of ABS (0003) reports is: |
| 92 | # timestamp 0003 axis value |
| 93 | report_regex = re.compile(' 0003 ([0-9a-f]{4}) ([0-9a-f]+)') |
| 94 | for match in report_regex.finditer(evdev_data): |
| 95 | axis = int(match.group(1), 16) |
| 96 | if axis not in self.required_axis: |
| 97 | self.required_axis.append(axis) |
| 98 | |
Dennis Kempin | 143ef16 | 2014-04-09 13:46:50 -0700 | [diff] [blame] | 99 | |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 100 | def _ParseHWProperties(self, data): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 101 | """ Parse x and y dimensions and resolution from hwprops file """ |
Dennis Kempin | 143ef16 | 2014-04-09 13:46:50 -0700 | [diff] [blame] | 102 | abs_regex = 5 * ' ([0-9\-]+)' |
| 103 | xregex = re.compile('A: 35' + abs_regex) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 104 | xmatch = xregex.search(data); |
| 105 | self.x_min = int(xmatch.group(1)) |
| 106 | self.x_max = int(xmatch.group(2)) |
| 107 | self.x_res = int(xmatch.group(5)) |
| 108 | |
Dennis Kempin | 143ef16 | 2014-04-09 13:46:50 -0700 | [diff] [blame] | 109 | yregex = re.compile('A: 36' + abs_regex) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 110 | ymatch = yregex.search(data); |
| 111 | self.y_min = int(ymatch.group(1)) |
| 112 | self.y_max = int(ymatch.group(2)) |
| 113 | self.y_res = int(ymatch.group(5)) |
| 114 | |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 115 | axis_regex = re.compile('A: ([0-9a-f]+)') |
| 116 | for match in axis_regex.finditer(data): |
| 117 | self.has_axis.append(int(match.group(1), 16)) |
| 118 | |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 119 | def _ParseProperties(self, data): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 120 | """ Parse properties from file and inject xorg properties. """ |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 121 | self.properties = {} |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 122 | self.ignore_properties = [] |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 123 | data = json.loads(data) |
| 124 | |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 125 | if 'gestures' in data: |
| 126 | self.properties.update(data['gestures']) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 127 | |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 128 | if 'device_class' in data: |
| 129 | self.device_class = data['device_class'] |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 130 | |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 131 | if 'ignore' in data: |
| 132 | self.ignore_properties.extend(data['ignore']) |
| 133 | |
Chung-yih Wang | 35613f1 | 2014-04-25 13:57:23 +0800 | [diff] [blame^] | 134 | # Sanity check: Make sure it is not used inside ebuild. |
| 135 | if os.environ.get('PN') and os.environ.get('S'): |
| 136 | raise Exception("Should not be executed inside ebuild") |
| 137 | |
| 138 | # If run on a Chromebook device, access xorg-conf files from their normal |
| 139 | # installed location. If run from inside chroot, access xorg-conf files |
| 140 | # from the xorg-conf project repository. |
| 141 | src_root = os.environ.get('CROS_WORKON_SRCROOT') |
| 142 | if src_root: |
| 143 | xorg_conf_path = os.path.join(src_root, xorg_conf_project_path) |
| 144 | else: |
| 145 | xorg_conf_path = '/etc/X11/xorg.conf.d' |
| 146 | |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 147 | if xorg_conf_path and 'xorg' in data and 'file' in data['xorg']: |
| 148 | filename = os.path.join(xorg_conf_path, data['xorg']['file']) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 149 | input_classes = self.xorg_parser.Parse(file=filename) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 150 | if 'identifier' in data['xorg']: |
| 151 | properties = input_classes[data['xorg']['identifier']] |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 152 | self.properties.update(properties) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 153 | if 'identifiers' in data['xorg']: |
| 154 | for identifier in data['xorg']['identifiers']: |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 155 | properties = input_classes[identifier] |
| 156 | self.properties.update(properties) |
| 157 | |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 158 | for prop in self.ignore_properties: |
| 159 | if prop in self.properties: |
| 160 | del self.properties[prop] |
| 161 | |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 162 | def _UpdateDimensions(self): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 163 | """ Update x/y min/max with xorg properties. |
| 164 | |
| 165 | CMT allows hardware properties to be overwritten by xorg properties. |
| 166 | Do the same in this class. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 167 | """ |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 168 | if 'Active Area Left' in self.properties: |
| 169 | self.x_min = int(self.properties['Active Area Left']) |
| 170 | if 'Active Area Right' in self.properties: |
| 171 | self.x_max = int(self.properties['Active Area Right']) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 172 | if 'Active Area Top' in self.properties: |
| 173 | self.y_min = int(self.properties['Active Area Top']) |
| 174 | if 'Active Area Bottom' in self.properties: |
| 175 | self.y_max = int(self.properties['Active Area Bottom']) |
Dennis Kempin | 55af9cc | 2013-06-20 15:07:21 -0700 | [diff] [blame] | 176 | |
| 177 | if 'Horizontal Resolution' in self.properties: |
| 178 | self.x_res = int(self.properties['Horizontal Resolution']) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 179 | if 'Vertical Resolution' in self.properties: |
| 180 | self.y_res = int(self.properties['Vertical Resolution']) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 181 | |
Dennis Kempin | 55af9cc | 2013-06-20 15:07:21 -0700 | [diff] [blame] | 182 | if 'SemiMT Non Linear Area Left' in self.properties: |
| 183 | self.x_min = int(self.properties['SemiMT Non Linear Area Left']) |
| 184 | if 'SemiMT Non Linear Area Right' in self.properties: |
| 185 | self.x_max = int(self.properties['SemiMT Non Linear Area Right']) |
| 186 | if 'SemiMT Non Linear Area Top' in self.properties: |
| 187 | self.y_min = int(self.properties['SemiMT Non Linear Area Top']) |
| 188 | if 'SemiMT Non Linear Area Bottom' in self.properties: |
| 189 | self.y_max = int(self.properties['SemiMT Non Linear Area Bottom']) |
| 190 | |
| 191 | |
| 192 | def Match(self, other, loose, debug=False): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 193 | """ Compare properties and return similarity. |
| 194 | |
| 195 | Compare these properties to another PlatformProperties instance. |
| 196 | The return value is a score between 1. 0 meaning there is a big mismatch |
| 197 | and 1 meaning the properties match completely. |
| 198 | Only a selected range of properties are compared in order to |
| 199 | prevent property adjustments to cause platforms to be mismatched. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 200 | """ |
| 201 | scores = [] |
| 202 | def compare(a, b, what): |
| 203 | value = abs(float(a) - float(b)) |
| 204 | if value > 0: |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 205 | value = min(1, value / max(abs(float(a)), abs(float(b)))) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 206 | scores.append(1-value) |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 207 | if debug: |
| 208 | print "%s: %s == %s" % (what, str(a), str(b)) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 209 | def compare_attr(what): |
| 210 | compare(getattr(self, what), getattr(other, what), what) |
| 211 | def compare_prop(what): |
| 212 | if what not in self.properties or what not in other.properties: |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 213 | scores.append(0) |
| 214 | else: |
| 215 | compare(self.properties[what], other.properties[what], what) |
| 216 | def check_axis(required, available): |
| 217 | for axis in required: |
| 218 | if axis not in available: |
| 219 | scores.append(0) |
| 220 | return |
| 221 | |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 222 | compare_attr('x_min') |
| 223 | compare_attr('x_max') |
| 224 | compare_attr('x_res') |
Dennis Kempin | 55af9cc | 2013-06-20 15:07:21 -0700 | [diff] [blame] | 225 | compare_attr('y_min') |
| 226 | compare_attr('y_max') |
| 227 | compare_attr('y_res') |
| 228 | if not loose: |
| 229 | compare_prop('Pressure Calibration Offset') |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 230 | |
| 231 | if self.required_axis: |
| 232 | if debug: |
| 233 | print "axis:", self.required_axis, "in", other.has_axis |
| 234 | check_axis(self.required_axis, other.has_axis) |
| 235 | |
| 236 | if other.required_axis: |
| 237 | if debug: |
| 238 | print "axis:", other.required_axis, "in", self.has_axis |
| 239 | check_axis(other.required_axis, self.has_axis) |
| 240 | |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 241 | return reduce(lambda x,y: (x * y), scores) |
| 242 | |
| 243 | class PlatformDatabase(object): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 244 | """ Class for managing platforms. |
| 245 | |
| 246 | This class reads all available platforms from the platforms_dir and allows |
| 247 | to search for matching platforms to an activity log file. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 248 | """ |
| 249 | def __init__(self): |
| 250 | platform_files = [ f for f in os.listdir(platforms_dir) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 251 | if f.endswith('.hwprops') ] |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 252 | self.platforms = {} |
| 253 | for file in platform_files: |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 254 | name = file.replace('.hwprops', '') |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 255 | self.platforms[name] = PlatformProperties(platform=name) |
| 256 | |
Dennis Kempin | 55af9cc | 2013-06-20 15:07:21 -0700 | [diff] [blame] | 257 | def FindMatching(self, log, loose=True, debug=False): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 258 | """ Find platform matching activity_data. |
| 259 | |
| 260 | Returns the PlatformProperties instance of the platform matching |
| 261 | the activity log data. This method might terminate the program in |
| 262 | case no match can be made, or the match is ambiguous. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 263 | """ |
| 264 | result = None |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 265 | properties = PlatformProperties(log=log) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 266 | for name, platform in self.platforms.items(): |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 267 | if debug: |
| 268 | print "#" * 10, name |
Dennis Kempin | 55af9cc | 2013-06-20 15:07:21 -0700 | [diff] [blame] | 269 | score = platform.Match(properties, loose, debug) |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 270 | if debug: |
| 271 | print name, "score =", score |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 272 | if score > 0.9: |
| 273 | if result: |
Dennis Kempin | 55af9cc | 2013-06-20 15:07:21 -0700 | [diff] [blame] | 274 | if loose: |
| 275 | if debug: |
| 276 | print "-" * 10, "Multiple matches. Try strict" |
| 277 | return self.FindMatching(log, False, debug) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 278 | print ('multiple matching platforms:', result.name, |
| 279 | 'and', platform.name) |
Dennis Kempin | 19e972b | 2013-06-20 13:21:38 -0700 | [diff] [blame] | 280 | return None |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 281 | result = platform |
| 282 | if not result: |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 283 | print 'cannot find matching platform' |
Dennis Kempin | 19e972b | 2013-06-20 13:21:38 -0700 | [diff] [blame] | 284 | return None |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 285 | return result |
Dennis Kempin | d0b722a | 2014-04-15 11:54:48 -0700 | [diff] [blame] | 286 | |
| 287 | @staticmethod |
| 288 | def RegisterPlatformFromDevice(ip): |
| 289 | # get list of multitouch devices |
| 290 | remote = CrOSRemote(ip) |
| 291 | devices = remote.ReadCommand( |
| 292 | "/opt/google/input/inputcontrol -t multitouch --names", |
| 293 | verbose=True) |
| 294 | if devices is False: |
| 295 | return None |
| 296 | |
| 297 | # Each line has the format: |
| 298 | # id: Device Name |
| 299 | # devices[*][0] will have the id |
| 300 | # devices[*][1] will have the name |
| 301 | devices = devices.splitlines() |
| 302 | devices = [l.split(":", 1) for l in devices] |
| 303 | |
| 304 | # select one device from list |
| 305 | idx = UserSelection([d[1] for d in devices], |
| 306 | "Which device would you like to register?") |
| 307 | device_id = devices[idx][0] |
| 308 | |
| 309 | # read hardware properties |
| 310 | hwprops = remote.ReadCommand( |
| 311 | "/opt/google/input/inputcontrol --id %s --hwprops" % device_id, |
| 312 | verbose=True) |
| 313 | if not hwprops: |
| 314 | print "Please update your device to latest canary or:" |
| 315 | print " emerge-${BOARD} inputcontrol" |
| 316 | print " cros deploy $DEVICE_IP inputcontrol" |
| 317 | return None |
| 318 | |
Dennis Kempin | cd7caba | 2014-04-16 13:37:18 -0700 | [diff] [blame] | 319 | xorg_files = [ |
| 320 | "/etc/X11/xorg.conf.d/60-touchpad-cmt-*.conf", |
| 321 | "/etc/X11/xorg.conf.d/50-touchpad-cmt-*.conf", |
| 322 | "/etc/X11/xorg.conf.d/40-touchpad-cmt.conf" |
| 323 | ] |
| 324 | |
| 325 | for pattern in xorg_files: |
| 326 | # find filename of xorg configuration file |
| 327 | xorg_file = remote.ReadCommand("ls " + pattern, verbose=False) |
| 328 | if not xorg_file: |
| 329 | continue |
| 330 | xorg_file = xorg_file.strip() |
| 331 | |
| 332 | # extract identifiers |
Dennis Kempin | d0b722a | 2014-04-15 11:54:48 -0700 | [diff] [blame] | 333 | print "Selecting Xorg identifiers from", xorg_file |
| 334 | conf = remote.Read(xorg_file) |
Dennis Kempin | cd7caba | 2014-04-16 13:37:18 -0700 | [diff] [blame] | 335 | all_ids = [] |
| 336 | for match in re.finditer("Identifier\s+\"([a-zA-Z0-9-_ ]+)\"", conf): |
| 337 | all_ids.append(match.group(1)) |
Dennis Kempin | d0b722a | 2014-04-15 11:54:48 -0700 | [diff] [blame] | 338 | |
Dennis Kempin | cd7caba | 2014-04-16 13:37:18 -0700 | [diff] [blame] | 339 | # ask user to select |
| 340 | idxs = UserSelection(all_ids, |
| 341 | "Which xorg identifiers apply to this device?", |
Dennis Kempin | d0b722a | 2014-04-15 11:54:48 -0700 | [diff] [blame] | 342 | allow_multi=True, allow_zero=True) |
Dennis Kempin | cd7caba | 2014-04-16 13:37:18 -0700 | [diff] [blame] | 343 | ids = [all_ids[i] for i in idxs] |
| 344 | if ids: |
| 345 | break |
Dennis Kempin | d0b722a | 2014-04-15 11:54:48 -0700 | [diff] [blame] | 346 | |
Dennis Kempin | d0b722a | 2014-04-15 11:54:48 -0700 | [diff] [blame] | 347 | if not ids: |
Dennis Kempin | cd7caba | 2014-04-16 13:37:18 -0700 | [diff] [blame] | 348 | print "Please configure the platform properties manually" |
| 349 | xorg_file = "todo: add correct xorg conf file" |
| 350 | ids = ["todo: add correct xorg identifier"] |
Dennis Kempin | d0b722a | 2014-04-15 11:54:48 -0700 | [diff] [blame] | 351 | |
| 352 | ids_string = "[" + ", ".join(["\"%s\"" % id for id in ids]) + "]" |
| 353 | xorg_file = os.path.basename(xorg_file) |
| 354 | |
| 355 | sys.stdout.write("Please name this platform: ") |
| 356 | sys.stdout.flush() |
| 357 | platform_name = sys.stdin.readline().strip() |
| 358 | |
| 359 | # write platform info to files |
| 360 | hwprops_file = os.path.join(platforms_dir, platform_name + ".hwprops") |
| 361 | props_file = os.path.join(platforms_dir, platform_name + ".props") |
| 362 | |
| 363 | open(hwprops_file, "w").write(hwprops) |
| 364 | open(props_file, "w").write(props_template % (xorg_file, ids_string)) |
| 365 | |
| 366 | print "Created files: " |
| 367 | print " ", hwprops_file |
| 368 | print " ", props_file |
| 369 | |
| 370 | return platform_name |
| 371 | |
| 372 | |
| 373 | def UserSelection(list, msg, allow_multi=False, allow_zero=False): |
| 374 | idx = [0] |
Dennis Kempin | cd7caba | 2014-04-16 13:37:18 -0700 | [diff] [blame] | 375 | if len(list) == 1 and not allow_zero: |
Dennis Kempin | d0b722a | 2014-04-15 11:54:48 -0700 | [diff] [blame] | 376 | return [0] if allow_multi else 0 |
| 377 | |
| 378 | # repeat until user made a valid selection |
| 379 | while True: |
| 380 | |
| 381 | # get user input |
| 382 | print msg |
| 383 | if allow_zero: |
| 384 | print " 0: None" |
| 385 | for i, item in enumerate(list): |
| 386 | print " ", str(i + 1) + ":", item |
| 387 | if allow_multi: |
| 388 | print "(Separate multiple selections with spaces)" |
| 389 | sys.stdout.write('> ') |
| 390 | sys.stdout.flush() |
| 391 | selection = sys.stdin.readline() |
| 392 | |
| 393 | # validates single input value |
| 394 | def CheckSelection(selection): |
| 395 | try: |
| 396 | idx = int(selection) - 1 |
| 397 | if allow_zero and idx == -1: |
| 398 | return True |
| 399 | if idx < 0 or idx >= len(list): |
| 400 | print 'Number out of range' |
| 401 | return False |
| 402 | except: |
| 403 | print 'Not a number' |
| 404 | return False |
| 405 | return True |
| 406 | |
| 407 | if allow_multi: |
| 408 | # validate list of values |
| 409 | valid = True |
| 410 | selections = selection.split(" ") |
| 411 | for selection in selections: |
| 412 | if not CheckSelection(selection): |
| 413 | valid = False |
| 414 | break |
| 415 | if valid: |
| 416 | selections = [int(s) - 1 for s in selections] |
| 417 | if -1 in selections: |
| 418 | return [] |
| 419 | return selections |
| 420 | else: |
| 421 | if CheckSelection(selection): |
| 422 | return int(selection) - 1 |