summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2016-11-03 08:05:07 +1000
committerBenjamin Tissoires <benjamin.tissoires@gmail.com>2016-11-08 10:44:10 +0100
commit5d7b1839e8f6b2472008a39f4abfc7d78b0cfd22 (patch)
tree8ac9a0042fc9727af6e0acd0e336001c267337b4
parent438ce839a7ff9cf811db58765d13e5bb5d1db9a4 (diff)
python: automatically rewind the fd at the end of the event sequence
Allows running through the event sequence more than once without any extra handling. In modern recordings the description and the event file is in the same file and there is no extra fd handling for the events. So simple code to check events would look like this: d = evemu.Device("/path/to/file", create=False) for e in d.events(): check_for_something() for e in d.events(): check_for_something_else() Simply rewinding the fd is sufficient here to avoid the caller having to keep a copy of the events. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net> Reviewed-by: Daniel Martin <consume.noise@gmail.com> Signed-off-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
-rw-r--r--python/evemu/__init__.py2
-rw-r--r--python/evemu/base.py4
-rw-r--r--python/evemu/tests/test_device.py9
3 files changed, 15 insertions, 0 deletions
diff --git a/python/evemu/__init__.py b/python/evemu/__init__.py
index 78cf42f..5dfffd9 100644
--- a/python/evemu/__init__.py
+++ b/python/evemu/__init__.py
@@ -284,6 +284,8 @@ class Device(object):
while self._libevemu.evemu_read_event(fs, ctypes.byref(event)) > 0:
yield InputEvent(event.sec, event.usec, event.type, event.code, event.value)
+ self._libc.rewind(fs)
+
def play(self, events_file):
"""
Replays an event sequence, as provided by the events_file,
diff --git a/python/evemu/base.py b/python/evemu/base.py
index 5dd2e89..64ec5cd 100644
--- a/python/evemu/base.py
+++ b/python/evemu/base.py
@@ -156,6 +156,10 @@ class LibC(LibraryWrapper):
"restype": c_int,
"errcheck": expect_eq_zero
},
+ "rewind": {
+ "argtypes": (c_void_p,),
+ "restype": None,
+ },
}
class LibEvdev(LibraryWrapper):
diff --git a/python/evemu/tests/test_device.py b/python/evemu/tests/test_device.py
index be1be81..7d77f05 100644
--- a/python/evemu/tests/test_device.py
+++ b/python/evemu/tests/test_device.py
@@ -146,6 +146,15 @@ class DeviceActionTestCase(evemu.testing.testcase.BaseTestCase):
events = [e for e in device.events(ef)]
self.assertTrue(len(events) > 1)
+ def test_read_events_twice(self):
+ device = evemu.Device(self.get_device_file(), create=False)
+ events_file = self.get_events_file()
+ with open(events_file) as ef:
+ e1 = [(e.type, e.code, e.value) for e in device.events(ef)]
+ e2 = [(e.type, e.code, e.value) for e in device.events(ef)]
+ self.assertEquals(len(e1), len(e2))
+ self.assertEquals(e1, e2)
+
class DevicePropertiesTestCase(evemu.testing.testcase.BaseTestCase):
"""
Verifies the workings of the various device property accessors.