11# encoding: utf-8
22
33import os
4+ from collections import namedtuple
45
56from evdev import _input , ecodes , util
67from evdev .events import InputEvent
@@ -30,12 +31,20 @@ def __eq__(self, o):
3031 and self .version == o .version
3132
3233
34+
35+ _AbsInfo = namedtuple ('AbsInfo' ,
36+ ['min' , 'max' , 'fuzz' , 'flat' ])
37+
38+ class AbsInfo (_AbsInfo ):
39+ pass
40+
41+
3342class InputDevice (object ):
3443 '''
3544 A linux input device from which input events can be read.
3645 '''
3746
38- __slots__ = 'fn' , 'nophys' , 'fd' , 'info' , 'name' , 'phys' , '_capabilities '
47+ __slots__ = 'fn' , 'nophys' , 'fd' , 'info' , 'name' , 'phys' , '_rawcapabilities '
3948
4049 def __init__ (self , dev , nophys = False ):
4150 '''
@@ -61,29 +70,56 @@ def __init__(self, dev, nophys=False):
6170
6271 #: The physical topology of the device
6372 self .phys = info_res [5 ] if not nophys else ''
64- self ._capabilities = info_res [6 ]
65-
66- def capabilities (self , verbose = False ):
73+ self ._rawcapabilities = info_res [6 ]
74+
75+ def _capabilities (self , absinfo = True ):
76+ res = {}
77+ for etype , ecodes in self ._rawcapabilities .items ():
78+ for code in ecodes :
79+ l = res .setdefault (etype , [])
80+ if isinstance (code , tuple ):
81+ a = code [1 ] # (0, 0, 255, 0)
82+ i = AbsInfo (min = a [1 ], max = a [2 ], fuzz = a [3 ], flat = a [4 ])
83+ l .append ((code [0 ], i ))
84+ else :
85+ l .append (code )
86+
87+ return res
88+
89+ def capabilities (self , verbose = False , absinfo = True ):
6790 '''
6891 Returns the event types that this device supports as a a mapping of
6992 supported event types to lists of handled event codes. Example::
7093
7194 { 1: [272, 273, 274],
7295 2: [0, 1, 6, 8] }
7396
74- If verbose is `True`, event codes and types will be resolved to their
75- names. Example::
97+ If `` verbose`` is `` True`` , event codes and types will be resolved
98+ to their names. Example::
7699
77100 { ('EV_KEY', 1) : [('BTN_MOUSE', 272), ('BTN_RIGHT', 273), ('BTN_MIDDLE', 273)],
78101 ('EV_REL', 2) : [('REL_X', 0), ('REL_Y', 0), ('REL_HWHEEL', 6), ('REL_WHEEL', 8)] }
79102
80103 Unknown codes or types will be resolved to '?'.
104+
105+ If ``absinfo`` is ``True``, the list of capabilities will also
106+ include absolute axis information (``absmin``, ``absmax``,
107+ ``absfuzz``, ``absflat``) in the following form::
108+
109+ { 3 : [ (0, AbsInfo(min=0, max=255, fuzz=0, flat=0)),
110+ (1, AbsInfo(min=0, max=255, fuzz=0, flat=0)) ]}
111+
112+ Combined with ``verbose`` the above becomes::
113+
114+ { ('EV_ABS', 3) : [ (('ABS_X', 0), AbsInfo(min=0, max=255, fuzz=0, flat=0)),
115+ (('ABS_Y', 1), AbsInfo(min=0, max=255, fuzz=0, flat=0)) ]}
116+
81117 '''
82118
83119 if verbose :
84- return dict (util .resolve_ecodes (self ._capabilities ))
120+ return dict (util .resolve_ecodes (self ._capabilities ( absinfo ) ))
85121 else :
86- return self ._capabilities
122+ return self ._capabilities ( absinfo )
87123
88124 def __eq__ (self , o ):
89125 ''' Two devices are considered equal if their :data:`info` attributes are equal. '''
0 commit comments