[PATCH evemu 10/12] python: add a wrapper around evemu_read_event

Peter Hutterer peter.hutterer at who-t.net
Tue Jul 22 16:42:48 PDT 2014


Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 python/evemu/__init__.py          | 27 +++++++++++++++++++++++++++
 python/evemu/base.py              | 12 +++++++++---
 python/evemu/tests/test_device.py |  6 ++++++
 3 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/python/evemu/__init__.py b/python/evemu/__init__.py
index 65c7cf0..ea0901d 100644
--- a/python/evemu/__init__.py
+++ b/python/evemu/__init__.py
@@ -25,10 +25,21 @@ import stat
 
 import evemu.base
 import evemu.const
+import evemu.event_names
 
 __all__ = ["Device"]
 
 
+class InputEvent(object):
+    __slots__ = 'sec', 'usec', 'type', 'code', 'value'
+
+    def __init__(self, sec, usec, type, code, value):
+        self.sec = sec
+        self.usec = usec
+        self.type = type
+        self.code = code
+        self.value = value
+
 class Device(object):
     """
     Encapsulates a raw kernel input event device, either an existing one as
@@ -125,6 +136,22 @@ class Device(object):
         self._libevemu.evemu_write(self._evemu_device, fs)
         self._libc.fflush(fs)
 
+    def events(self, events_file=None):
+        """
+        Reads the events from the given file and returns them as a list of
+        dicts.
+        """
+        if events_file:
+            if not hasattr(events_file, "read"):
+                raise TypeError("expected file")
+        else:
+            events_file = self._file
+
+        fs = self._libc.fdopen(events_file.fileno(), b"r")
+        event = evemu.base.InputEvent()
+        while self._libevemu.evemu_read_event(fs, ctypes.byref(event)) > 0:
+            yield InputEvent(event.sec, event.usec, event.type, event.code, event.value)
+
     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 6540c01..fc66869 100644
--- a/python/evemu/base.py
+++ b/python/evemu/base.py
@@ -6,7 +6,7 @@ import ctypes.util
 import os
 
 # Import types directly, so they don't have to be prefixed with "ctypes.".
-from ctypes import c_char_p, c_int, c_uint, c_void_p
+from ctypes import c_char_p, c_int, c_uint, c_void_p, c_long, c_int32, c_uint16
 
 import evemu.const
 import evemu.exception
@@ -360,8 +360,7 @@ class LibEvemu(LibraryWrapper):
         #int evemu_read_event(FILE *fp, struct input_event *ev);
         "evemu_read_event": {
             "argtypes": (c_void_p, c_void_p),
-            "restype": c_int,
-            "errcheck": expect_gt_zero
+            "restype": c_int
             },
         #int evemu_read_event_realtime(FILE *fp, struct input_event *ev,
         #			      struct timeval *evtime);
@@ -406,3 +405,10 @@ class LibEvemu(LibraryWrapper):
             "restype": None
             },
         }
+
+class InputEvent(ctypes.Structure):
+    _fields_ = [("sec", c_long),
+		("usec", c_long),
+		("type", c_uint16),
+		("code", c_uint16),
+		("value", c_int32)]
diff --git a/python/evemu/tests/test_device.py b/python/evemu/tests/test_device.py
index 51a0050..c7b6162 100644
--- a/python/evemu/tests/test_device.py
+++ b/python/evemu/tests/test_device.py
@@ -139,6 +139,12 @@ class DeviceActionTestCase(evemu.testing.testcase.BaseTestCase):
             self.assertTrue(rhs)
             self.assertEquals(lhs.group(1), rhs.group(1))
 
+    def test_read_events(self):
+        device = evemu.Device(self.get_device_file(), create=False)
+        events_file = self.get_events_file()
+        with open(events_file) as e:
+            events = [e for e in device.events(e)]
+            self.assertTrue(len(events) > 1)
 
 class DevicePropertiesTestCase(evemu.testing.testcase.BaseTestCase):
     """
-- 
1.9.3



More information about the Input-tools mailing list