1# Copyright (c) 2013 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 5"""A simple script to play an mtplot device data file. 6 7Usage: python tools/mtplot_play.py file 8 9Example: 10 1. Replay a touchpad raw data file: 11 $ python tools/mtplot_play.py \ 12 /tmp/two_finger_tap.vertical-link-fw_1.0.AA-robot-20130806_224733.dat 13 2. Replay a touchscreen raw data file: 14 $ python tools/mtplot_play.py -d touchscreen \ 15 /tmp/two_finger_tap.horizontal-link-fw_1.0.AA-robot-20130806_220011.dat 16""" 17 18import argparse 19import os 20import subprocess 21import sys 22 23import common 24import mtb 25 26from common_util import print_and_exit, simple_system 27from firmware_utils import ScreenShot, SimpleX 28from touch_device import TouchDevice 29 30 31def generate_mtplot_image_from_log(device_node, mtplot_file): 32 """Convert the mtplot file to evemu format, and play it with evemu-play. 33 34 @param device_node: the touch device node on which to play the mtplot_file 35 @param mtplot_file: a device file in mtplot format 36 """ 37 # Convert the mtplot file to evemu file. 38 evemu_file = mtb.convert_mtplot_file_to_evemu_file(mtplot_file, 39 evemu_dir='/tmp', 40 force=True) 41 42 if not evemu_file: 43 msg = 'Error to convert data from mtplot format to evemu format: %s' 44 print msg % mtplot_file 45 return 46 47 # Launch mtplot in order to capture the image. 48 mtplot_cmd = 'mtplot -d :0 %s' % device_node 49 devnull = open(os.devnull, 'w') 50 proc = subprocess.Popen(mtplot_cmd.split(), stdout=devnull) 51 52 play_cmd = 'evemu-play --insert-slot0 %s < %s' % (device_node, evemu_file) 53 print 'Executing: %s\n' % play_cmd 54 simple_system(play_cmd) 55 56 # evemu_file looks like drumroll.fast-link-fw_1.0-robot-20130829.evemu.dat 57 # image_file looks like drumroll.fast-link-fw_1.0-robot-20130829 58 image_file = evemu_file.rsplit('.', 2)[0] 59 60 # Dump the screen shot to the image file. 61 width, height = SimpleX('aura').get_screen_size() 62 geometry_str = '%dx%d+%d+%d' % (width, height, 0, 0) 63 ScreenShot(geometry_str).dump_root(image_file) 64 65 # Terminate mtplot. 66 proc.poll() 67 if proc.returncode is None: 68 proc.terminate() 69 proc.wait() 70 devnull.close() 71 72 print 'Files saved:' 73 print 'The evemu file: %s' % evemu_file 74 print 'The mtplot image file: %s\n' % image_file 75 76 77def _parse(): 78 """Parse the command line options.""" 79 parser = argparse.ArgumentParser( 80 description='Play a raw data file and capture its image.') 81 parser.add_argument('filename', help='a raw data file in mtplot format') 82 parser.add_argument('-d', '--device', 83 help='the device type (default: touchpad)', 84 choices=['touchpad', 'touchscreen'], 85 default='touchpad') 86 args = parser.parse_args() 87 88 # Get the touchpad/touchscreen device node from the device option 89 is_ts = (args.device == 'touchscreen') 90 args.device_node = TouchDevice.get_device_node(is_touchscreen=is_ts) 91 if args.device_node is None: 92 print_and_exit('Error: fail to get device node for %s.' % args.device) 93 94 # Check the existence of the raw data file. 95 if not os.path.isfile(args.filename): 96 print_and_exit('Error: The file "%s" does not exist.' % args.filename) 97 98 print '\nthe device node of the %s: %s\n' % (args.device, args.device_node) 99 print 'the raw data file: %s\n' % args.filename 100 101 return args 102 103 104if __name__ == '__main__': 105 args = _parse() 106 generate_mtplot_image_from_log(args.device_node, args.filename) 107