1
0
forked from drath/libratbag

ratbagctl: make the device ID more interesting

Take the first 4 bytes of the device id (a sha), parse them as two 2-byte hex
values and mod them by the length of a pre-determined array of rodents and
their vocalisation methods. This gives us an index and a device name
assignement like this:

wailing-squirrel:    Logitech G500s Laser Gaming Mouse
warbling-groundhog:  Logitech MX Anywhere 2S
humming-hutia:       Logitech Gaming Mouse G303

This relies on the sha's first 4 bytes to be unique enough that they don't add
up to the same 2 values. Which is probably safe enough.

This does not affect ratbag-command which still uses the hidraw nodes

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer
2019-02-22 14:25:44 +10:00
committed by Benjamin Tissoires
parent a46c30253d
commit 57ca3086ae
3 changed files with 57 additions and 1 deletions

View File

@@ -1466,10 +1466,48 @@ def get_parser():
return RatbagParserRoot(parser_def)
def on_device_added(ratbagd, device):
device_names = [
'mara', 'capybara', 'porcupine', 'paca',
'vole', 'woodrat', 'gerbil', 'shrew',
'hutia', 'beaver', 'squirrel', 'chinchilla',
'rabbit', 'viscacha', 'hare', 'degu',
'gundi', 'acouchy', 'nutria', 'paca',
'hamster', 'zokor', 'chipmunk', 'gopher',
'marmot', 'groundhog', 'suslik', 'agouti',
'blesmol',
]
device_attr = [
'sobbing', 'whooping', 'barking', 'yapping',
'howling', 'squawking', 'cheering', 'warbling',
'thundering', 'booming', 'blustering', 'humming',
'crying', 'bawling', 'roaring', 'raging',
'chanting', 'crooning', 'murmuring', 'bellowing',
'wailing', 'weeping', 'screaming', 'yelling',
'yodeling', 'singing', 'honking', 'hooting',
'whispering', 'hollering',
]
# Let's convert the sha into something not boring. This takes the first
# 4 characters, creates two different indices from it to generate a
# name. The rest is hope hope that never get a collision here but it's
# unlikely enough.
name = device_names[int(device.id[0:2], 16) % len(device_names)]
attr = device_attr[int(device.id[2:4], 16) % len(device_attr)]
device.id = "-".join([attr, name])
def open_ratbagd(ratbagd_process=None, verbose=0):
try:
r = Ratbagd()
r.verbose = verbose
try:
r.connect('device-added', on_device_added)
for d in r.devices:
on_device_added(r, d)
except AttributeError:
pass # the ratbag-command case
except RatbagdUnavailable as e:
print("Unable to connect to ratbagd: {}".format(e))
return None

View File

@@ -42,6 +42,7 @@ class TestRatbagCtl(unittest.TestCase):
def setUpClass(cls):
super(TestRatbagCtl, cls).setUpClass()
cls.reset_test_device()
cls.find_test_device()
@classmethod
def run_ratbagctl_subprocess(cls, params):
@@ -83,6 +84,7 @@ class TestRatbagCtl(unittest.TestCase):
@classmethod
def run_ratbagctl(cls, params):
params = params.replace('test_device', cls.test_device)
if run_ratbagctl_in_subprocess:
return cls.run_ratbagctl_subprocess(params)
return cls.run_ratbagctl_fast(params)
@@ -93,6 +95,18 @@ class TestRatbagCtl(unittest.TestCase):
ratbagd._dbus_call("ResetTestDevice", "")
toolbox.sync_dbus()
@classmethod
def find_test_device(cls):
rc, stdout, stderr = cls.run_ratbagctl_subprocess('list')
testdevice = None
for line in stdout.split('\n'):
if 'Test device' in line:
testdevice = line.split(':')[0]
break
assert testdevice is not None
cls.test_device = testdevice
def launch_good_test(self, params):
returncode, stdout, stderr = self.run_ratbagctl(params)
self.assertEqual(returncode, 0)
@@ -116,7 +130,7 @@ class TestRatbagCtl(unittest.TestCase):
class TestRatbagCtlList(TestRatbagCtl):
def test_list(self):
r = self.launch_good_test("list")
self.assertIn('test_device', r)
self.assertIn(self.test_device, r)
self.launch_fail_test("test_device list")
self.launch_fail_test("list test_device")

View File

@@ -315,6 +315,10 @@ class RatbagdDevice(_RatbagdDBus):
def id(self):
return self._id
@id.setter
def id(self, id):
self._id = id
@GObject.Property
def model(self):
"""The unique identifier for this device model."""