summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2014-08-29 09:44:09 +1000
committerBenjamin Tissoires <benjamin.tissoires@gmail.com>2014-11-05 09:58:12 -0500
commitbcbdb42752dc53b6910b43999b24cf78f4ccfcd9 (patch)
treea037f5ff7aa77720f963cedb740cc562824cc72d /python
parentc6fa672e8d945b6d685f07ef2a8b288f9e226717 (diff)
python: add InputEvent.matches(type, code) for comparisons
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@who-t.net> Signed-off-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
Diffstat (limited to 'python')
-rw-r--r--python/evemu/__init__.py9
-rw-r--r--python/evemu/tests/test_device.py40
2 files changed, 49 insertions, 0 deletions
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()