1#!/usr/bin/env python2
2# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Description:
7#
8# Class for injecting input events to linux 'evdev' input devices.
9#
10# Provides evemu-play-like functionality if run from the command line:
11# $ input_event_player.py -d /dev/input/event6
12
13""" Playback input events on a linux input device. """
14
15from __future__ import division
16from __future__ import print_function
17
18import glob
19import os.path
20import re
21import time
22
23from input_device import InputDevice, InputEvent
24from optparse import OptionParser
25
26
27class InputEventPlayer:
28    """ Linux evdev input event player.
29
30    An "evdev" input event player injects a stream of "input events" to kernel
31    evdev driver. With this player, we could easily playback an input event file
32    which was recorded with the tool evemu-record previously.
33
34    """
35
36    def __init__(self):
37        self.tv_sec = None
38        self.tv_usec = None
39
40    def playback(self, device, gesture_file):
41        """ Play the events in gesture_file on device.
42
43        Keyword arguments:
44        device -- the InputDevice device object
45        gesture_file -- the name of the event file recorded previously
46        """
47        if not device:
48            raise
49        event_str = 'E: (\d+)\.(\d+) ([0-9a-f]{4}) ([0-9a-f]{4}) ([-]?\d+)'
50        event_pattern = re.compile(event_str)
51        for line in open(gesture_file, 'rt'):
52            m = event_pattern.match(line)
53            if not m:
54                raise
55            event = InputEvent(int(m.group(1)),
56                               int(m.group(2)),
57                               int(m.group(3), 16),
58                               int(m.group(4), 16),
59                               int(m.group(5)))
60            if not self.tv_sec:
61                self.tv_sec = event.tv_sec
62                self.tv_usec = event.tv_usec
63            delta = event.tv_sec - self.tv_sec
64            delta += ((event.tv_usec - self.tv_usec) / 1000000.0)
65            # Sleep only if the event is 0.05 ms later than the previous one
66            if delta > 0.0000500:
67                time.sleep(delta)
68            self.tv_sec = event.tv_sec
69            self.tv_usec = event.tv_usec
70            event.write(device.f)
71
72
73if __name__ == '__main__':
74    parser = OptionParser()
75
76    parser.add_option('-d', '--devpath', dest='devpath', default='',
77                      help='device path (/dev/input/event0)')
78    parser.add_option('-t', '--touchpad', action='store_true', dest='touchpad',
79                      default=False, help='Find and use first touchpad device')
80    parser.add_option('-f', '--file', action='store', dest='gesture_file',
81                      help='Event file to playback')
82    (options, args) = parser.parse_args()
83
84    if options.touchpad:
85        for evdev in glob.glob('/dev/input/event*'):
86            device = InputDevice(evdev)
87            if device.is_touchpad():
88                break
89        else:
90            print('Can not find a touchpad device')
91            exit()
92    elif not os.path.exists(options.devpath):
93        print('Can not find the input device "%s".' % options.devpath)
94        exit()
95    else:
96        device = InputDevice(options.devpath)
97    if not options.gesture_file:
98        print('Gesture file is not specified.')
99        exit()
100    if not os.path.exists(options.gesture_file):
101        print('Can not find the gesture file %s.' % options.gesture_file)
102        exit()
103
104    InputEventPlayer().playback(device, options.gesture_file)
105    print('Gesture file %s has been played.' % options.gesture_file)
106