2 # pylint: disable=line-too-long,invalid-name,global-statement,redefined-outer-name
3 # pylint: disable=missing-function-docstring,missing-class-docstring,missing-module-docstring
4 # SPDX-License-Identifier: MIT
6 # This file is distributed under the MIT license, see below.
8 # The MIT License (MIT)
10 # Permission is hereby granted, free of charge, to any person obtaining a copy
11 # of this software and associated documentation files (the "Software"), to deal
12 # in the Software without restriction, including without limitation the rights
13 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 # copies of the Software, and to permit persons to whom the Software is
15 # furnished to do so, subject to the following conditions:
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 from pyparsing
import (Word
, White
, Literal
, ParserElement
, Regex
, LineEnd
,
35 OneOrMore
, Combine
, Or
, Optional
, Suppress
, Group
,
36 nums
, alphanums
, printables
,
37 stringEnd
, pythonStyleComment
,
40 print('pyparsing is not available')
44 from pyparsing
import __diag__
46 __diag__
.warn_multiple_tokens_in_named_alternation
= True
47 __diag__
.warn_ungrouped_named_tokens_in_collection
= True
48 __diag__
.warn_name_set_on_empty_Forward
= True
49 __diag__
.warn_on_multiple_string_args_to_oneof
= True
50 __diag__
.enable_debug_on_named_expressions
= True
55 from evdev
.ecodes
import ecodes
58 print('WARNING: evdev is not available')
61 from functools
import lru_cache
63 # don't do caching on old python
64 lru_cache
= lambda: (lambda f
: f
)
66 EOL
= LineEnd().suppress()
68 COMMENTLINE
= pythonStyleComment
+ EOL
70 REAL
= Combine((INTEGER
+ Optional('.' + Optional(INTEGER
))) ^
('.' + INTEGER
))
71 SIGNED_REAL
= Combine(Optional(Word('-+')) + REAL
)
72 UDEV_TAG
= Word(string
.ascii_uppercase
, alphanums
+ '_')
74 # Those patterns are used in type-specific matches
75 TYPES
= {'mouse': ('usb', 'bluetooth', 'ps2', '*'),
76 'evdev': ('name', 'atkbd', 'input'),
78 'id-input': ('modalias'),
79 'touchpad': ('i8042', 'rmi', 'bluetooth', 'usb'),
80 'joystick': ('i8042', 'rmi', 'bluetooth', 'usb'),
81 'keyboard': ('name', ),
82 'sensor': ('modalias',
87 'proximity-palmrest-left',
88 'proximity-palmrest-right',
93 'proximity-wifi-left',
94 'proximity-wifi-right',
96 'ieee1394-unit-function' : ('node', ),
100 # Patterns that are used to set general properties on a device
101 GENERAL_MATCHES
= {'acpi',
111 def upperhex_word(length
):
112 return Word(nums
+ 'ABCDEF', exact
=length
)
116 ParserElement
.setDefaultWhitespaceChars('')
118 prefix
= Or(category
+ ':' + Or(conn
) + ':'
119 for category
, conn
in TYPES
.items())
121 matchline_typed
= Combine(prefix
+ Word(printables
+ ' ' + '®'))
122 matchline_general
= Combine(Or(GENERAL_MATCHES
) + ':' + Word(printables
+ ' ' + '®'))
123 matchline
= (matchline_typed | matchline_general
) + EOL
125 propertyline
= (White(' ', exact
=1).suppress() +
126 Combine(UDEV_TAG
- '=' - Optional(Word(alphanums
+ '_=:@*.!-;, "/'))
127 - Optional(pythonStyleComment
)) +
129 propertycomment
= White(' ', exact
=1) + pythonStyleComment
+ EOL
131 group
= (OneOrMore(matchline('MATCHES*') ^ COMMENTLINE
.suppress()) -
132 OneOrMore(propertyline('PROPERTIES*') ^ propertycomment
.suppress()) -
133 (EMPTYLINE ^
stringEnd()).suppress())
134 commentgroup
= OneOrMore(COMMENTLINE
).suppress() - EMPTYLINE
.suppress()
136 grammar
= OneOrMore(Group(group
)('GROUPS*') ^ commentgroup
) + stringEnd()
141 def property_grammar():
142 ParserElement
.setDefaultWhitespaceChars(' ')
144 dpi_setting
= Group(Optional('*')('DEFAULT') + INTEGER('DPI') + Optional(Suppress('@') + INTEGER('HZ')))('SETTINGS*')
145 mount_matrix_row
= SIGNED_REAL
+ ',' + SIGNED_REAL
+ ',' + SIGNED_REAL
146 mount_matrix
= Group(mount_matrix_row
+ ';' + mount_matrix_row
+ ';' + mount_matrix_row
)('MOUNT_MATRIX')
147 xkb_setting
= Optional(Word(alphanums
+ '+-/@._'))
149 # Although this set doesn't cover all of characters in database entries, it's enough for test targets.
150 name_literal
= Word(printables
+ ' ')
152 props
= (('MOUSE_DPI', Group(OneOrMore(dpi_setting
))),
153 ('MOUSE_WHEEL_CLICK_ANGLE', INTEGER
),
154 ('MOUSE_WHEEL_CLICK_ANGLE_HORIZONTAL', INTEGER
),
155 ('MOUSE_WHEEL_CLICK_COUNT', INTEGER
),
156 ('MOUSE_WHEEL_CLICK_COUNT_HORIZONTAL', INTEGER
),
157 ('ID_AUTOSUSPEND', Or((Literal('0'), Literal('1')))),
158 ('ID_AUTOSUSPEND_DELAY_MS', INTEGER
),
159 ('ID_AV_PRODUCTION_CONTROLLER', Or((Literal('0'), Literal('1')))),
160 ('ID_PERSIST', Or((Literal('0'), Literal('1')))),
161 ('ID_PDA', Or((Literal('0'), Literal('1')))),
162 ('ID_INPUT', Or((Literal('0'), Literal('1')))),
163 ('ID_INPUT_ACCELEROMETER', Or((Literal('0'), Literal('1')))),
164 ('ID_INPUT_JOYSTICK', Or((Literal('0'), Literal('1')))),
165 ('ID_INPUT_KEY', Or((Literal('0'), Literal('1')))),
166 ('ID_INPUT_KEYBOARD', Or((Literal('0'), Literal('1')))),
167 ('ID_INPUT_MOUSE', Or((Literal('0'), Literal('1')))),
168 ('ID_INPUT_POINTINGSTICK', Or((Literal('0'), Literal('1')))),
169 ('ID_INPUT_SWITCH', Or((Literal('0'), Literal('1')))),
170 ('ID_INPUT_TABLET', Or((Literal('0'), Literal('1')))),
171 ('ID_INPUT_TABLET_PAD', Or((Literal('0'), Literal('1')))),
172 ('ID_INPUT_TOUCHPAD', Or((Literal('0'), Literal('1')))),
173 ('ID_INPUT_TOUCHSCREEN', Or((Literal('0'), Literal('1')))),
174 ('ID_INPUT_TRACKBALL', Or((Literal('0'), Literal('1')))),
175 ('ID_SIGNAL_ANALYZER', Or((Literal('0'), Literal('1')))),
176 ('POINTINGSTICK_SENSITIVITY', INTEGER
),
177 ('ID_INPUT_JOYSTICK_INTEGRATION', Or(('internal', 'external'))),
178 ('ID_INPUT_TOUCHPAD_INTEGRATION', Or(('internal', 'external'))),
179 ('XKB_FIXED_LAYOUT', xkb_setting
),
180 ('XKB_FIXED_VARIANT', xkb_setting
),
181 ('XKB_FIXED_MODEL', xkb_setting
),
182 ('KEYBOARD_LED_NUMLOCK', Literal('0')),
183 ('KEYBOARD_LED_CAPSLOCK', Literal('0')),
184 ('ACCEL_MOUNT_MATRIX', mount_matrix
),
185 ('ACCEL_LOCATION', Or(('display', 'base'))),
186 ('PROXIMITY_NEAR_LEVEL', INTEGER
),
187 ('IEEE1394_UNIT_FUNCTION_MIDI', Or((Literal('0'), Literal('1')))),
188 ('IEEE1394_UNIT_FUNCTION_AUDIO', Or((Literal('0'), Literal('1')))),
189 ('IEEE1394_UNIT_FUNCTION_VIDEO', Or((Literal('0'), Literal('1')))),
190 ('ID_VENDOR_FROM_DATABASE', name_literal
),
191 ('ID_MODEL_FROM_DATABASE', name_literal
),
192 ('ID_TAG_MASTER_OF_SEAT', Literal('1')),
193 ('ID_INFRARED_CAMERA', Or((Literal('0'), Literal('1')))),
194 ('ID_CAMERA_DIRECTION', Or(('front', 'rear'))),
195 ('SOUND_FORM_FACTOR', Or(('internal', 'webcam', 'speaker', 'headphone', 'headset', 'handset', 'microphone'))),
197 fixed_props
= [Literal(name
)('NAME') - Suppress('=') - val('VALUE')
198 for name
, val
in props
]
199 kbd_props
= [Regex(r
'KEYBOARD_KEY_[0-9a-f]+')('NAME')
201 ('!' ^
(Optional('!') - Word(alphanums
+ '_')))('VALUE')
203 abs_props
= [Regex(r
'EVDEV_ABS_[0-9a-f]{2}')('NAME')
205 Word('-' + nums
+ ':')('VALUE')
208 grammar
= Or(fixed_props
+ kbd_props
+ abs_props
) + EOL
213 def error(fmt
, *args
, **kwargs
):
216 print(fmt
.format(*args
, **kwargs
))
218 def convert_properties(group
):
219 matches
= [m
[0] for m
in group
.MATCHES
]
220 props
= [p
[0] for p
in group
.PROPERTIES
]
221 return matches
, props
224 grammar
= hwdb_grammar()
226 with
open(fname
, 'r', encoding
='UTF-8') as f
:
227 parsed
= grammar
.parseFile(f
)
228 except ParseBaseException
as e
:
229 error('Cannot parse {}: {}', fname
, e
)
231 return [convert_properties(g
) for g
in parsed
.GROUPS
]
233 def check_matches(groups
):
234 matches
= sum((group
[0] for group
in groups
), [])
236 # This is a partial check. The other cases could be also done, but those
237 # two are most commonly wrong.
238 grammars
= { 'usb' : 'v' + upperhex_word(4) + Optional('p' + upperhex_word(4) + Optional(':')) + '*',
239 'pci' : 'v' + upperhex_word(8) + Optional('d' + upperhex_word(8) + Optional(':')) + '*',
242 for match
in matches
:
243 prefix
, rest
= match
.split(':', maxsplit
=1)
244 gr
= grammars
.get(prefix
)
246 # we check this first to provide an easy error message
247 if rest
[-1] not in '*:':
248 error('pattern {} does not end with "*" or ":"', match
)
252 except ParseBaseException
as e
:
253 error('Pattern {!r} is invalid: {}', rest
, e
)
258 for match
in matches
:
260 error('Match {!r} is duplicated', match
)
263 def check_one_default(prop
, settings
):
264 defaults
= [s
for s
in settings
if s
.DEFAULT
]
265 if len(defaults
) > 1:
266 error('More than one star entry: {!r}', prop
)
268 def check_one_mount_matrix(prop
, value
):
269 numbers
= [s
for s
in value
if s
not in {';', ','}]
270 if len(numbers
) != 9:
271 error('Wrong accel matrix: {!r}', prop
)
273 numbers
= [abs(float(number
)) for number
in numbers
]
275 error('Wrong accel matrix: {!r}', prop
)
276 bad_x
, bad_y
, bad_z
= max(numbers
[0:3]) == 0, max(numbers
[3:6]) == 0, max(numbers
[6:9]) == 0
277 if bad_x
or bad_y
or bad_z
:
278 error('Mount matrix is all zero in {} row: {!r}',
279 'x' if bad_x
else ('y' if bad_y
else 'z'),
282 def check_one_keycode(value
):
283 if value
!= '!' and ecodes
is not None:
284 key
= 'KEY_' + value
.upper()
285 if not (key
in ecodes
or
286 value
.upper() in ecodes
or
287 # new keys added in kernel 5.5
288 'KBD_LCD_MENU' in key
):
289 error('Keycode {} unknown', key
)
291 def check_wheel_clicks(properties
):
292 pairs
= (('MOUSE_WHEEL_CLICK_COUNT_HORIZONTAL', 'MOUSE_WHEEL_CLICK_COUNT'),
293 ('MOUSE_WHEEL_CLICK_ANGLE_HORIZONTAL', 'MOUSE_WHEEL_CLICK_ANGLE'),
294 ('MOUSE_WHEEL_CLICK_COUNT_HORIZONTAL', 'MOUSE_WHEEL_CLICK_ANGLE_HORIZONTAL'),
295 ('MOUSE_WHEEL_CLICK_COUNT', 'MOUSE_WHEEL_CLICK_ANGLE'))
297 if pair
[0] in properties
and pair
[1] not in properties
:
298 error('{} requires {} to be specified', *pair
)
300 def check_properties(groups
):
301 grammar
= property_grammar()
302 for _
, props
in groups
:
306 prop
= prop
.partition('#')[0].rstrip()
308 parsed
= grammar
.parseString(prop
)
309 except ParseBaseException
:
310 error('Failed to parse: {!r}', prop
)
312 # print('{!r}'.format(parsed))
313 if parsed
.NAME
in seen_props
:
314 error('Property {} is duplicated', parsed
.NAME
)
315 seen_props
[parsed
.NAME
] = parsed
.VALUE
316 if parsed
.NAME
== 'MOUSE_DPI':
317 check_one_default(prop
, parsed
.VALUE
.SETTINGS
)
318 elif parsed
.NAME
== 'ACCEL_MOUNT_MATRIX':
319 check_one_mount_matrix(prop
, parsed
.VALUE
)
320 elif parsed
.NAME
.startswith('KEYBOARD_KEY_'):
321 val
= parsed
.VALUE
if isinstance(parsed
.VALUE
, str) else parsed
.VALUE
[0]
322 check_one_keycode(val
)
324 check_wheel_clicks(seen_props
)
326 def print_summary(fname
, groups
):
327 n_matches
= sum(len(matches
) for matches
, props
in groups
)
328 n_props
= sum(len(props
) for matches
, props
in groups
)
329 print(f
'{fname}: {len(groups)} match groups, {n_matches} matches, {n_props} properties')
331 if n_matches
== 0 or n_props
== 0:
332 error(f
'{fname}: no matches or props')
334 if __name__
== '__main__':
335 args
= sys
.argv
[1:] or sorted(glob
.glob(os
.path
.dirname(sys
.argv
[0]) + '/[678][0-9]-*.hwdb'))
338 groups
= parse(fname
)
339 print_summary(fname
, groups
)
340 check_matches(groups
)
341 check_properties(groups
)