[PATCH evemu 01/16] python: add InputEvent.matches(type, code) for comparisons

Peter Hutterer peter.hutterer at who-t.net
Mon Aug 11 18:34:35 PDT 2014


Allows for:

	if e.matches("EV_REL", "REL_X"):
	   print("Relative X events")

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 python/evemu/__init__.py          |  9 +++++++++
 python/evemu/tests/test_device.py | 14 ++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/python/evemu/__init__.py b/python/evemu/__init__.py
index ebb4443..1393ca9 100644
--- a/python/evemu/__init__.py
+++ b/python/evemu/__init__.py
@@ -61,6 +61,15 @@ class InputEvent(object):
         self.code = code
         self.value = value
 
+    def matches(self, type, code = None):
+        if event_get_name(self.type) != type:
+            return False
+
+        if code != None and event_get_name(self.type, self.code) != code:
+            return False
+
+        return True
+
     def __str__(self):
         f = tempfile.TemporaryFile()
         libc = evemu.base.LibC()
diff --git a/python/evemu/tests/test_device.py b/python/evemu/tests/test_device.py
index 4c555a4..2873758 100644
--- a/python/evemu/tests/test_device.py
+++ b/python/evemu/tests/test_device.py
@@ -263,5 +263,19 @@ class DevicePropertiesTestCase(evemu.testing.testcase.BaseTestCase):
 
         self.assertEqual(results, self.get_expected_keybits())
 
+    def test_event_matching(self):
+        e = evemu.InputEvent(0, 0, 0x01, 44, 0)
+        self.assertTrue(e.matches("EV_KEY"))
+        self.assertTrue(e.matches("EV_KEY", "KEY_Z"))
+
+        e = evemu.InputEvent(0, 0, 0x02, 0x01, 0)
+        self.assertTrue(e.matches("EV_REL"))
+        self.assertTrue(e.matches("EV_REL", "REL_Y"))
+
+        e = evemu.InputEvent(0, 0, 0x03, 0x00, 0)
+        self.assertTrue(e.matches("EV_ABS"))
+        self.assertTrue(e.matches("EV_ABS", "ABS_X"))
+
+
 if __name__ == "__main__":
     unittest.main()
-- 
1.9.3



More information about the Input-tools mailing list