# Copyright 2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import os import struct from datetime import datetime from collections import namedtuple GENERAL_MODE = 0 GAMEPAD_MODE = 1 u16_struct = struct.Struct(' self.version >= 0: self._read_devices(fh) else: raise ValueError('Invalid recording version: {}'.format(self.version)) def _read_devices(self, fh): num_devices, = read_struct(fh, u32_struct) for _ in xrange(num_devices): self.device_paths.append(read_string(fh)) def _read_gamepad_info(self, fh): self.gamepad_device = UinputDeviceInfo(fh) self.device_paths.append('[GAMEPAD]') def _iter_events(self): if self.fh is None: raise RuntimeError('Attempting to iterate over events of a closed recording') self.fh.seek(self._events_start) if self.version >= 2: for _ in xrange(self.num_events): yield ReventEvent(self.fh) else: file_size = os.path.getsize(self.filepath) while self.fh.tell() < file_size: yield ReventEvent(self.fh, legacy=True) def __iter__(self): for event in self.events: yield event def __enter__(self): return self def __exit__(self, *args): self.close() def __del__(self): self.close()