[PATCH evemu 3/7] python: replace event_get_value/event_get_name with the C implementation

Peter Hutterer peter.hutterer at who-t.net
Thu Aug 28 16:44:05 PDT 2014


Relying on an external lib removes the pain to maintain our own input.h
implementation.

The functions accept both strings and integer values in both directions of
conversion.

Signed-off-by: Benjamin Tissoires <benjamin.tissoires at gmail.com>
Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 python/evemu/__init__.py | 48 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 41 insertions(+), 7 deletions(-)

diff --git a/python/evemu/__init__.py b/python/evemu/__init__.py
index a8af50e..aac64c6 100644
--- a/python/evemu/__init__.py
+++ b/python/evemu/__init__.py
@@ -35,6 +35,8 @@ __all__ = ["Device",
            "input_prop_get_value",
            "input_prop_get_name"]
 
+_libevdev = evemu.base.LibEvdev()
+
 def event_get_value(event_type, event_code = None):
     """
     Return the integer-value for the given event type and/or code string
@@ -44,10 +46,27 @@ def event_get_value(event_type, event_code = None):
     If an event code is passed, the event type may be given as integer or
     string.
     """
-    try:
-        return evemu.event_names._event_get_value(event_type, event_code)
-    except KeyError:
-        return None
+    t = -1
+    c = -1
+
+    if isinstance(event_type, int):
+        event_type = _libevdev.libevdev_event_type_get_name(event_type)
+        if event_type == 0: # NULL
+            return None
+
+    t = _libevdev.libevdev_event_type_from_name(str(event_type))
+
+    if event_code == None:
+        return None if t < 0 else t
+
+    if isinstance(event_code, int):
+        event_code = _libevdev.libevdev_event_code_get_name(t, event_code)
+        if event_code == 0: # NULL
+            return None
+
+    c = _libevdev.libevdev_event_code_from_name(t, str(event_code))
+
+    return None if c < 0 else c
 
 def event_get_name(event_type, event_code = None):
     """
@@ -58,11 +77,26 @@ def event_get_name(event_type, event_code = None):
     If an event code is passed, the event type may be given as integer or
     string.
     """
-    try:
-        return evemu.event_names._event_get_name(event_type, event_code)
-    except KeyError:
+    if not isinstance(event_type, int):
+        event_type = event_get_value(event_type)
+
+    if event_type == None:
         return None
 
+    if event_code == None:
+        type_name = _libevdev.libevdev_event_type_get_name(event_type)
+        return None if type_name == 0 else type_name
+
+    if not isinstance(event_code, int):
+        event_code = event_get_value(event_type, event_code)
+
+    if event_code == None:
+        return None
+
+    code_name = _libevdev.libevdev_event_code_get_name(event_type, event_code)
+
+    return None if code_name == 0 else code_name
+
 def input_prop_get_name(prop):
     """
     Return the name of the input property, or None if undefined.
-- 
1.9.3



More information about the Input-tools mailing list