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

Benjamin Tissoires benjamin.tissoires at gmail.com
Fri Aug 29 13:53:48 PDT 2014


On Thu, Aug 28, 2014 at 7:44 PM, Peter Hutterer
<peter.hutterer at who-t.net> wrote:
> 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))

This is just weird to go back and forth in case we are given an int.

The other patch regarding the prop does the same, and I do not feel
like this is optimal. I'll try to send a fix soon.

Cheers,
Benjamin

> +
> +    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
>
> _______________________________________________
> 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