2017-05-14 16:33:17 +02:00
|
|
|
#!/usr/bin/env python3
|
2016-09-13 16:57:25 +10:00
|
|
|
# vim: set expandtab shiftwidth=4:
|
|
|
|
|
# -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
|
|
|
|
|
#
|
|
|
|
|
# ANY MODIFICATIONS TO THIS FILE SHOULD BE MERGED INTO THE SYSTEMD UPSTREAM
|
|
|
|
|
#
|
|
|
|
|
# This file is part of systemd. It is distributed under the MIT license, see
|
|
|
|
|
# below.
|
|
|
|
|
#
|
|
|
|
|
# Copyright 2016 Zbigniew Jędrzejewski-Szmek
|
|
|
|
|
#
|
|
|
|
|
# The MIT License (MIT)
|
|
|
|
|
#
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
|
#
|
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
|
#
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
# SOFTWARE.
|
|
|
|
|
|
|
|
|
|
import functools
|
|
|
|
|
import glob
|
|
|
|
|
import string
|
|
|
|
|
import sys
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from pyparsing import (Word, White, Literal, ParserElement, Regex,
|
2017-07-10 09:38:04 +10:00
|
|
|
LineEnd, OneOrMore, Combine, Or, Optional,
|
|
|
|
|
Suppress, Group, nums, alphanums, printables,
|
2016-09-13 16:57:25 +10:00
|
|
|
stringEnd, pythonStyleComment,
|
|
|
|
|
ParseBaseException)
|
|
|
|
|
except ImportError:
|
|
|
|
|
print('pyparsing is not available')
|
|
|
|
|
sys.exit(77)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from evdev.ecodes import ecodes
|
|
|
|
|
except ImportError:
|
|
|
|
|
ecodes = None
|
|
|
|
|
print('WARNING: evdev is not available')
|
|
|
|
|
|
|
|
|
|
EOL = LineEnd().suppress()
|
2017-01-16 12:57:27 +10:00
|
|
|
EMPTYLINE = LineEnd()
|
2016-09-13 16:57:25 +10:00
|
|
|
COMMENTLINE = pythonStyleComment + EOL
|
|
|
|
|
INTEGER = Word(nums)
|
|
|
|
|
REAL = Combine((INTEGER + Optional('.' + Optional(INTEGER))) ^ ('.' + INTEGER))
|
|
|
|
|
UDEV_TAG = Word(string.ascii_uppercase, alphanums + '_')
|
|
|
|
|
|
|
|
|
|
TYPES = {
|
2017-07-10 09:38:04 +10:00
|
|
|
'libinput': ('name', 'touchpad', 'mouse', 'keyboard'),
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-13 16:57:25 +10:00
|
|
|
|
|
|
|
|
@functools.lru_cache()
|
|
|
|
|
def hwdb_grammar():
|
|
|
|
|
ParserElement.setDefaultWhitespaceChars('')
|
|
|
|
|
|
|
|
|
|
prefix = Or(category + ':' + Or(conn) + ':'
|
|
|
|
|
for category, conn in TYPES.items())
|
|
|
|
|
matchline = Combine(prefix + Word(printables + ' ' + '®')) + EOL
|
2017-07-10 09:38:04 +10:00
|
|
|
propertyline = (
|
|
|
|
|
White(' ', exact=1).suppress()
|
|
|
|
|
+ Combine(UDEV_TAG
|
|
|
|
|
- '='
|
|
|
|
|
- Word(alphanums + '_=:@*.! ')
|
|
|
|
|
- Optional(pythonStyleComment))
|
|
|
|
|
+ EOL
|
|
|
|
|
)
|
2016-09-13 16:57:25 +10:00
|
|
|
propertycomment = White(' ', exact=1) + pythonStyleComment + EOL
|
|
|
|
|
|
2017-07-10 09:38:04 +10:00
|
|
|
group = (
|
|
|
|
|
OneOrMore(matchline('MATCHES*') ^ COMMENTLINE.suppress())
|
|
|
|
|
- OneOrMore(propertyline('PROPERTIES*') ^ propertycomment.suppress())
|
|
|
|
|
- (EMPTYLINE ^ stringEnd()).suppress()
|
|
|
|
|
)
|
2016-09-13 16:57:25 +10:00
|
|
|
commentgroup = OneOrMore(COMMENTLINE).suppress() - EMPTYLINE.suppress()
|
|
|
|
|
|
|
|
|
|
grammar = OneOrMore(group('GROUPS*') ^ commentgroup) + stringEnd()
|
|
|
|
|
|
|
|
|
|
return grammar
|
|
|
|
|
|
2017-07-10 09:38:04 +10:00
|
|
|
|
2016-09-13 16:57:25 +10:00
|
|
|
@functools.lru_cache()
|
|
|
|
|
def property_grammar():
|
|
|
|
|
ParserElement.setDefaultWhitespaceChars(' ')
|
|
|
|
|
|
2017-07-10 09:38:04 +10:00
|
|
|
model_props = [
|
|
|
|
|
Regex(r'LIBINPUT_MODEL_[_0-9A-Z]+')('NAME') -
|
|
|
|
|
Suppress('=') - (Literal('1'))('VALUE')
|
|
|
|
|
]
|
2016-09-13 16:57:25 +10:00
|
|
|
|
|
|
|
|
dimension = INTEGER('X') + Suppress('x') + INTEGER('Y')
|
2017-07-07 15:52:13 +10:00
|
|
|
|
|
|
|
|
crange = INTEGER('X') + Suppress(':') + INTEGER('Y')
|
|
|
|
|
vprops = (
|
2017-07-10 09:38:04 +10:00
|
|
|
('LIBINPUT_ATTR_SIZE_HINT', Group(dimension('SETTINGS*'))),
|
|
|
|
|
('LIBINPUT_ATTR_RESOLUTION_HINT', Group(dimension('SETTINGS*'))),
|
|
|
|
|
('LIBINPUT_ATTR_PRESSURE_RANGE', Group(crange('SETTINGS*'))),
|
2017-03-22 16:16:21 +10:00
|
|
|
('LIBINPUT_ATTR_TOUCH_SIZE_RANGE', Group(crange('SETTINGS*'))),
|
2017-07-10 09:38:04 +10:00
|
|
|
('LIBINPUT_ATTR_TPKBCOMBO_LAYOUT', Or(('below'))),
|
|
|
|
|
('LIBINPUT_ATTR_LID_SWITCH_RELIABILITY',
|
|
|
|
|
Or(('reliable', 'write_open'))),
|
|
|
|
|
('LIBINPUT_ATTR_KEYBOARD_INTEGRATION', Or(('internal', 'external'))),
|
2017-07-07 15:52:13 +10:00
|
|
|
)
|
|
|
|
|
value_props = [Literal(name)('NAME') - Suppress('=') - val('VALUE') for
|
2017-07-10 09:38:04 +10:00
|
|
|
name, val in vprops]
|
2017-07-07 15:52:13 +10:00
|
|
|
|
|
|
|
|
tprops = (
|
2017-07-10 09:38:04 +10:00
|
|
|
('LIBINPUT_ATTR_PALM_PRESSURE_THRESHOLD', INTEGER('X')),
|
2017-07-07 15:52:13 +10:00
|
|
|
)
|
|
|
|
|
typed_props = [Literal(name)('NAME') - Suppress('=') - val
|
2017-07-10 09:38:04 +10:00
|
|
|
for name, val in tprops]
|
2017-07-07 15:52:13 +10:00
|
|
|
|
|
|
|
|
grammar = Or(model_props + value_props + typed_props)
|
2016-09-13 16:57:25 +10:00
|
|
|
|
|
|
|
|
return grammar
|
|
|
|
|
|
|
|
|
|
ERROR = False
|
2017-07-10 09:38:04 +10:00
|
|
|
|
|
|
|
|
|
2016-09-13 16:57:25 +10:00
|
|
|
def error(fmt, *args, **kwargs):
|
|
|
|
|
global ERROR
|
|
|
|
|
ERROR = True
|
|
|
|
|
print(fmt.format(*args, **kwargs))
|
|
|
|
|
|
2017-07-10 09:38:04 +10:00
|
|
|
|
2016-09-13 16:57:25 +10:00
|
|
|
def convert_properties(group):
|
|
|
|
|
matches = [m[0] for m in group.MATCHES]
|
|
|
|
|
props = [p[0] for p in group.PROPERTIES]
|
|
|
|
|
return matches, props
|
|
|
|
|
|
2017-07-10 09:38:04 +10:00
|
|
|
|
2016-09-13 16:57:25 +10:00
|
|
|
def parse(fname):
|
|
|
|
|
grammar = hwdb_grammar()
|
|
|
|
|
try:
|
|
|
|
|
parsed = grammar.parseFile(fname)
|
|
|
|
|
except ParseBaseException as e:
|
|
|
|
|
error('Cannot parse {}: {}', fname, e)
|
|
|
|
|
return []
|
|
|
|
|
return [convert_properties(g) for g in parsed.GROUPS]
|
|
|
|
|
|
2017-07-10 09:38:04 +10:00
|
|
|
|
2016-09-13 16:57:25 +10:00
|
|
|
def check_match_uniqueness(groups):
|
|
|
|
|
matches = sum((group[0] for group in groups), [])
|
|
|
|
|
matches.sort()
|
|
|
|
|
prev = None
|
|
|
|
|
for match in matches:
|
|
|
|
|
if match == prev:
|
|
|
|
|
error('Match {!r} is duplicated', match)
|
|
|
|
|
prev = match
|
|
|
|
|
|
2017-07-10 09:38:04 +10:00
|
|
|
|
2016-09-13 16:57:25 +10:00
|
|
|
def check_one_dimension(prop, value):
|
|
|
|
|
if int(value[0]) <= 0 or int(value[1]) <= 0:
|
|
|
|
|
error('Dimension {} invalid', value)
|
|
|
|
|
|
2017-07-10 09:38:04 +10:00
|
|
|
|
2016-09-13 16:57:25 +10:00
|
|
|
def check_properties(groups):
|
|
|
|
|
grammar = property_grammar()
|
|
|
|
|
for matches, props in groups:
|
|
|
|
|
prop_names = set()
|
|
|
|
|
for prop in props:
|
|
|
|
|
# print('--', prop)
|
|
|
|
|
prop = prop.partition('#')[0].rstrip()
|
|
|
|
|
try:
|
|
|
|
|
parsed = grammar.parseString(prop)
|
|
|
|
|
except ParseBaseException as e:
|
|
|
|
|
error('Failed to parse: {!r}', prop)
|
|
|
|
|
continue
|
|
|
|
|
# print('{!r}'.format(parsed))
|
|
|
|
|
if parsed.NAME in prop_names:
|
|
|
|
|
error('Property {} is duplicated', parsed.NAME)
|
|
|
|
|
prop_names.add(parsed.NAME)
|
|
|
|
|
if parsed.NAME == "LIBINPUT_ATTR_SIZE_HINT" or \
|
|
|
|
|
parsed.NAME == "LIBINPUT_ATTR_RESOLUTION_HINT":
|
|
|
|
|
check_one_dimension(prop, parsed.VALUE)
|
|
|
|
|
|
2017-07-10 09:38:04 +10:00
|
|
|
|
2016-09-13 16:57:25 +10:00
|
|
|
def print_summary(fname, groups):
|
|
|
|
|
print('{}: {} match groups, {} matches, {} properties'
|
|
|
|
|
.format(fname,
|
|
|
|
|
len(groups),
|
|
|
|
|
sum(len(matches) for matches, props in groups),
|
|
|
|
|
sum(len(props) for matches, props in groups),
|
2017-07-10 09:38:04 +10:00
|
|
|
))
|
|
|
|
|
|
2016-09-13 16:57:25 +10:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
args = sys.argv[1:] or glob.glob(os.path.dirname(sys.argv[0]) + '/*.hwdb')
|
|
|
|
|
|
|
|
|
|
for fname in args:
|
|
|
|
|
groups = parse(fname)
|
|
|
|
|
print_summary(fname, groups)
|
|
|
|
|
check_match_uniqueness(groups)
|
|
|
|
|
check_properties(groups)
|
|
|
|
|
|
|
|
|
|
sys.exit(ERROR)
|