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

Benjamin Tissoires benjamin.tissoires at gmail.com
Tue Aug 12 10:47:37 PDT 2014


On Mon, Aug 11, 2014 at 9:34 PM, Peter Hutterer
<peter.hutterer at who-t.net> wrote:
> Allows for:
>
>         if e.matches("EV_REL", "REL_X"):
>            print("Relative X events")

It should be nice to also have
  if e.matches("EV_REL", 0):
    print("Relative 0 events")

(not quite sure it would be a benefit and not dumb, but we allow both
string and event code anywhere else).

Cheers,
Benjamin

>
> 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
>
> _______________________________________________
> Input-tools mailing list
> Input-tools at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/input-tools


More information about the Input-tools mailing list