[PATCH evemu 4/4] python: automatically rewind the fd at the end of the event sequence

Peter Hutterer peter.hutterer at who-t.net
Wed Nov 2 22:05:07 UTC 2016


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 at who-t.net>
---
 python/evemu/__init__.py          | 2 ++
 python/evemu/base.py              | 4 ++++
 python/evemu/tests/test_device.py | 9 +++++++++
 3 files changed, 15 insertions(+)

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.
-- 
2.9.3



More information about the Input-tools mailing list