Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 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 | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 6 | import os |
| 7 | import re |
| 8 | import json |
| 9 | from xorg_conf import XorgInputClassParser |
| 10 | |
| 11 | # path to current script directory |
| 12 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 13 | platforms_dir = os.path.realpath(os.path.join(script_dir, '..', 'platforms')) |
| 14 | xorg_conf_path = os.path.realpath(os.path.join(script_dir, '..', |
| 15 | '..', 'xorg-conf')) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 16 | |
| 17 | class PlatformProperties(object): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 18 | """ A class containing hardware and xorg properties for a platform. |
| 19 | |
| 20 | The class can be created from an activity log or by providing |
| 21 | the name of the platform. Information will then be read from the |
| 22 | 'platforms_dir' directory. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 23 | """ |
| 24 | def __init__(self, platform=None, activity_data=None): |
| 25 | if platform: |
| 26 | basename = os.path.join(platforms_dir, platform) |
| 27 | self.name = platform |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 28 | self.hwprops_file = basename + '.hwprops' |
| 29 | self.props_file = basename + '.props' |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 30 | self.xorg_parser = XorgInputClassParser() |
| 31 | self._ParseHWProperties(open(self.hwprops_file).read()) |
| 32 | self._ParseProperties(open(self.props_file).read()) |
| 33 | self._UpdateDimensions() |
| 34 | elif activity_data: |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 35 | self.name = '' |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 36 | self._ParseActivityLog(activity_data) |
| 37 | |
| 38 | def _ParseActivityLog(self, activity_data): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 39 | """ Parse property information from an activity log """ |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 40 | activity = json.loads(activity_data) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 41 | self.properties = activity['properties'] |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 42 | |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 43 | hwprops = activity['hardwareProperties'] |
| 44 | self.x_min = int(hwprops['left']) |
| 45 | self.x_max = int(hwprops['right']) |
| 46 | self.x_res = int(hwprops['xResolution']) |
| 47 | self.y_min = int(hwprops['top']) |
| 48 | self.y_max = int(hwprops['bottom']) |
| 49 | self.y_res = int(hwprops['yResolution']) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 50 | |
| 51 | def _ParseHWProperties(self, data): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 52 | """ Parse x and y dimensions and resolution from hwprops file """ |
| 53 | xregex = re.compile('A: 35 ([0-9\-]+) ([0-9\-]+) ([0-9\-]+) ' + |
| 54 | '([0-9\-]+) ([0-9\-]+)') |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 55 | xmatch = xregex.search(data); |
| 56 | self.x_min = int(xmatch.group(1)) |
| 57 | self.x_max = int(xmatch.group(2)) |
| 58 | self.x_res = int(xmatch.group(5)) |
| 59 | |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 60 | yregex = re.compile('A: 36 ([0-9\-]+) ([0-9\-]+) ([0-9\-]+) ' + |
| 61 | '([0-9\-]+) ([0-9\-]+)') |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 62 | ymatch = yregex.search(data); |
| 63 | self.y_min = int(ymatch.group(1)) |
| 64 | self.y_max = int(ymatch.group(2)) |
| 65 | self.y_res = int(ymatch.group(5)) |
| 66 | |
| 67 | def _ParseProperties(self, data): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 68 | """ Parse properties from file and inject xorg properties. """ |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 69 | self.properties = {} |
| 70 | data = json.loads(data) |
| 71 | |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 72 | if 'gestures' in data: |
| 73 | self.properties.update(data['gestures']) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 74 | |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 75 | if 'device_class' in data: |
| 76 | self.device_class = data['device_class'] |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 77 | |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 78 | if xorg_conf_path and 'xorg' in data and 'file' in data['xorg']: |
| 79 | filename = os.path.join(xorg_conf_path, data['xorg']['file']) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 80 | input_classes = self.xorg_parser.Parse(file=filename) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 81 | if 'identifier' in data['xorg']: |
| 82 | properties = input_classes[data['xorg']['identifier']] |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 83 | self.properties.update(properties) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 84 | if 'identifiers' in data['xorg']: |
| 85 | for identifier in data['xorg']['identifiers']: |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 86 | properties = input_classes[identifier] |
| 87 | self.properties.update(properties) |
| 88 | |
| 89 | def _UpdateDimensions(self): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 90 | """ Update x/y min/max with xorg properties. |
| 91 | |
| 92 | CMT allows hardware properties to be overwritten by xorg properties. |
| 93 | Do the same in this class. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 94 | """ |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 95 | if 'Active Area Left' in self.properties: |
| 96 | self.x_min = int(self.properties['Active Area Left']) |
| 97 | if 'Active Area Right' in self.properties: |
| 98 | self.x_max = int(self.properties['Active Area Right']) |
| 99 | if 'Horizontal Resolution' in self.properties: |
| 100 | self.x_res = int(self.properties['Horizontal Resolution']) |
| 101 | if 'Active Area Top' in self.properties: |
| 102 | self.y_min = int(self.properties['Active Area Top']) |
| 103 | if 'Active Area Bottom' in self.properties: |
| 104 | self.y_max = int(self.properties['Active Area Bottom']) |
| 105 | if 'Vertical Resolution' in self.properties: |
| 106 | self.y_res = int(self.properties['Vertical Resolution']) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 107 | |
| 108 | def Match(self, other): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 109 | """ Compare properties and return similarity. |
| 110 | |
| 111 | Compare these properties to another PlatformProperties instance. |
| 112 | The return value is a score between 1. 0 meaning there is a big mismatch |
| 113 | and 1 meaning the properties match completely. |
| 114 | Only a selected range of properties are compared in order to |
| 115 | prevent property adjustments to cause platforms to be mismatched. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 116 | """ |
| 117 | scores = [] |
| 118 | def compare(a, b, what): |
| 119 | value = abs(float(a) - float(b)) |
| 120 | if value > 0: |
| 121 | value = min(1, value / abs(float(a)), abs(float(b))) |
| 122 | scores.append(1-value) |
| 123 | def compare_attr(what): |
| 124 | compare(getattr(self, what), getattr(other, what), what) |
| 125 | def compare_prop(what): |
| 126 | if what not in self.properties or what not in other.properties: |
| 127 | return 0 |
| 128 | compare(self.properties[what], other.properties[what], what) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 129 | compare_attr('x_min') |
| 130 | compare_attr('x_max') |
| 131 | compare_attr('x_res') |
| 132 | compare_prop('Pressure Calibration Offset') |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 133 | return reduce(lambda x,y: (x * y), scores) |
| 134 | |
| 135 | class PlatformDatabase(object): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 136 | """ Class for managing platforms. |
| 137 | |
| 138 | This class reads all available platforms from the platforms_dir and allows |
| 139 | to search for matching platforms to an activity log file. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 140 | """ |
| 141 | def __init__(self): |
| 142 | platform_files = [ f for f in os.listdir(platforms_dir) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 143 | if f.endswith('.hwprops') ] |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 144 | self.platforms = {} |
| 145 | for file in platform_files: |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 146 | name = file.replace('.hwprops', '') |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 147 | self.platforms[name] = PlatformProperties(platform=name) |
| 148 | |
| 149 | def FindMatching(self, activity_data): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 150 | """ Find platform matching activity_data. |
| 151 | |
| 152 | Returns the PlatformProperties instance of the platform matching |
| 153 | the activity log data. This method might terminate the program in |
| 154 | case no match can be made, or the match is ambiguous. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 155 | """ |
| 156 | result = None |
| 157 | properties = PlatformProperties(activity_data=activity_data) |
| 158 | for name, platform in self.platforms.items(): |
| 159 | score = platform.Match(properties) |
| 160 | if score > 0.9: |
| 161 | if result: |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 162 | print ('multiple matching platforms:', result.name, |
| 163 | 'and', platform.name) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 164 | sys.exit(-1) |
| 165 | result = platform |
| 166 | if not result: |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame^] | 167 | print 'cannot find matching platform' |
| 168 | sys.exit(-1) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 169 | return result |