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