From e20ea203aa16811deccb8c2add1d90d654f35f07 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Filipe=20La=C3=ADns?= <lains@archlinux.org>
Date: Tue, 10 Mar 2020 10:14:12 +0000
Subject: [PATCH] clib: add support for libevdev_event_value_{get,from}_name
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Fixes #1

Signed-off-by: Filipe Laíns <lains@archlinux.org>
---
 libevdev/_clib.py | 36 ++++++++++++++++++++++++++++--------
 test/test_clib.py | 15 +++++++++++++++
 2 files changed, 43 insertions(+), 8 deletions(-)

diff --git a/libevdev/_clib.py b/libevdev/_clib.py
index 12a9162..8397874 100644
--- a/libevdev/_clib.py
+++ b/libevdev/_clib.py
@@ -143,6 +143,16 @@ class Libevdev(_LibraryWrapper):
             "argtypes": (c_uint, c_char_p,),
             "restype": c_int
         },
+        # const char *libevdev_event_value_get_name(unsigned int type, unsigned int code, int value);
+        "libevdev_event_value_get_name": {
+            "argtypes": (c_uint, c_uint, c_int,),
+            "restype": c_char_p
+        },
+        # int libevdev_event_value_from_name(unsigned int type, unsigned int code, const char *name);
+        "libevdev_event_value_from_name": {
+            "argtypes": (c_uint, c_uint, c_char_p,),
+            "restype": c_int
+        },
         # const char *libevdev_property_get_name(unsigned int prop);
         "libevdev_property_get_name": {
             "argtypes": (c_uint,),
@@ -644,17 +654,20 @@ class Libevdev(_LibraryWrapper):
         return m if m > -1 else None
 
     @classmethod
-    def event_to_name(cls, event_type, event_code=None):
+    def event_to_name(cls, event_type, event_code=None, event_value=None):
         """
         :param event_type: the numerical event type value
         :param event_code: optional, the numerical event code value
+        :param event_value: optional, the numerical event value
         :return: the event code name if a code is given otherwise the event
                  type name.
 
-        This function is the equivalent to ``libevdev_event_code_get_name()``
-        and ``libevdev_event_type_get_name()``
+        This function is the equivalent to ``libevdev_event_value_get_name()``,
+        ``libevdev_event_code_get_name()``, and ``libevdev_event_type_get_name()``
         """
-        if event_code is not None:
+        if event_code is not None and event_value is not None:
+            name = cls._event_value_get_name(event_type, event_code, event_value)
+        elif event_code is not None:
             name = cls._event_code_get_name(event_type, event_code)
         else:
             name = cls._event_type_get_name(event_type)
@@ -663,17 +676,24 @@ class Libevdev(_LibraryWrapper):
         return name.decode("iso8859-1")
 
     @classmethod
-    def event_to_value(cls, event_type, event_code=None):
+    def event_to_value(cls, event_type, event_code=None, event_value=None):
         """
         :param event_type: the event type as string
         :param event_code: optional, the event code as string
+        :param event_value: optional, the numerical event value
         :return: the event code value if a code is given otherwise the event
                  type value.
 
-        This function is the equivalent to ``libevdev_event_code_from_name()``
-        and ``libevdev_event_type_from_name()``
+        This function is the equivalent to ``libevdev_event_value_from_name()``,
+        ``libevdev_event_code_from_name()`` and ``libevdev_event_type_from_name()``
         """
-        if event_code is not None:
+        if event_code is not None and event_value is not None:
+            if not isinstance(event_type, int):
+                event_type = cls.event_to_value(event_type)
+            v = cls._event_value_from_name(event_type,
+                                           event_code.encode("iso8859-1"),
+                                           event_value.encode("iso8859-1"))
+        elif event_code is not None:
             if not isinstance(event_type, int):
                 event_type = cls.event_to_value(event_type)
             v = cls._event_code_from_name(event_type, event_code.encode("iso8859-1"))
diff --git a/test/test_clib.py b/test/test_clib.py
index 87125f9..165a821 100644
--- a/test/test_clib.py
+++ b/test/test_clib.py
@@ -76,6 +76,21 @@ class TestNameConversion(unittest.TestCase):
         with self.assertRaises(ctypes.ArgumentError):
             name = Libevdev.event_to_name(0, "foo")
 
+    def test_value_to_name(self):
+        name = Libevdev.event_to_name(3, 0x37, 0)
+        self.assertEqual(name, "MT_TOOL_FINGER")
+
+        name = Libevdev.event_to_name(3, 0x37, 1)
+        self.assertEqual(name, "MT_TOOL_PEN")
+
+    def test_value_to_name_invalid(self):
+        name = Libevdev.event_to_name(3, 0x37, 1000)
+        self.assertIsNone(name)
+        name = Libevdev.event_to_name(3, 0x37, -1)
+        self.assertIsNone(name)
+        with self.assertRaises(ctypes.ArgumentError):
+            name = Libevdev.event_to_name(0, "foo")
+
     def test_type_to_value(self):
         v = Libevdev.event_to_value("EV_REL")
         self.assertEqual(v, 2)
-- 
GitLab