[PATCH evemu 7/7] python: add InputEvent.matches(type, code) for comparisons
Peter Hutterer
peter.hutterer at who-t.net
Thu Aug 28 16:44:09 PDT 2014
Allows for:
if e.matches("EV_REL", "REL_X"):
print("Relative X events")
but it'll happily take integers as well.
Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
python/evemu/__init__.py | 9 +++++++++
python/evemu/tests/test_device.py | 40 +++++++++++++++++++++++++++++++++++++++
2 files changed, 49 insertions(+)
diff --git a/python/evemu/__init__.py b/python/evemu/__init__.py
index 65e4673..7af5032 100644
--- a/python/evemu/__init__.py
+++ b/python/evemu/__init__.py
@@ -133,6 +133,15 @@ class InputEvent(object):
self.code = code
self.value = value
+ def matches(self, type, code = None):
+ if event_get_value(type) != self.type:
+ return False
+
+ if code != None and event_get_value(self.type, code) != self.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 f2d14db..10bd290 100644
--- a/python/evemu/tests/test_device.py
+++ b/python/evemu/tests/test_device.py
@@ -278,5 +278,45 @@ class DevicePropertiesTestCase(evemu.testing.testcase.BaseTestCase):
self.assertEqual(evemu.input_prop_get_name("foo"), None)
self.assertEqual(evemu.input_prop_get_name(None), None)
+ 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"))
+ self.assertTrue(e.matches(0x01))
+ self.assertTrue(e.matches(0x01, 44))
+ self.assertTrue(e.matches("EV_KEY", 44))
+ self.assertTrue(e.matches(0x01, "KEY_Z"))
+
+ for t in range(-1, 0xff):
+ for c in range(-1, 0xff):
+ if t != e.type or c != e.code:
+ self.assertFalse(e.matches(t, c))
+
+ e = evemu.InputEvent(0, 0, 0x02, 0x01, 0)
+ self.assertTrue(e.matches("EV_REL"))
+ self.assertTrue(e.matches("EV_REL", "REL_Y"))
+ self.assertTrue(e.matches(0x02))
+ self.assertTrue(e.matches(0x02, 0x01))
+ self.assertTrue(e.matches("EV_REL", 0x01))
+ self.assertTrue(e.matches(0x02, "REL_Y"))
+
+ for t in range(-1, 0xff):
+ for c in range(-1, 0xff):
+ if t != e.type or c != e.code:
+ self.assertFalse(e.matches(t, c))
+
+ e = evemu.InputEvent(0, 0, 0x03, 0x00, 0)
+ self.assertTrue(e.matches("EV_ABS"))
+ self.assertTrue(e.matches("EV_ABS", "ABS_X"))
+ self.assertTrue(e.matches(0x03))
+ self.assertTrue(e.matches(0x03, 0x00))
+ self.assertTrue(e.matches("EV_ABS", 0x00))
+ self.assertTrue(e.matches(0x03, "ABS_X"))
+
+ for t in range(-1, 0xff):
+ for c in range(-1, 0xff):
+ if t != e.type or c != e.code:
+ self.assertFalse(e.matches(t, c))
+
if __name__ == "__main__":
unittest.main()
--
1.9.3
More information about the Input-tools
mailing list