Skip to content

Commit

Permalink
Rework the match approach to prefer uniq over name
Browse files Browse the repository at this point in the history
Previously we would match for name first, then uniq. But the name
matches are less reliable than uniq matches so where we can match for
uniq we should prefer that.

This avoids the case where where a name that applies to multiple
devices takes precedence over a more precise uniq match.
  • Loading branch information
whot committed May 3, 2024
1 parent b1b0a08 commit 3132b2b
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions libwacom/libwacom.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,9 @@ libwacom_new_from_path(const WacomDeviceDatabase *db, const char *path, WacomFal
const WacomDevice *device;
WacomDevice *ret = NULL;
WacomIntegrationFlags integration_flags;
char *name, *match_name;
char *uniq, *match_uniq;
char *name, *uniq;
const char *used_match_name = NULL;
const char *used_match_uniq = NULL;
WacomMatch *match;

switch (fallback) {
Expand All @@ -637,20 +638,34 @@ libwacom_new_from_path(const WacomDeviceDatabase *db, const char *path, WacomFal
if (!get_device_info (path, &vendor_id, &product_id, &name, &uniq, &bus, &integration_flags, error))
return NULL;

match_name = name;
match_uniq = uniq;
device = libwacom_new (db, match_name, match_uniq, vendor_id, product_id, bus, error);
if (device == NULL) {
match_uniq = NULL;
/* Uniq (where it exists) is more reliable than the name which may be re-used
* across tablets. So try to find a uniq+name match first, then uniq-only, then
* name-only.
*/
struct match_approach {
const char *name;
const char *uniq;
} approaches[] = {
{ name, uniq },
{ NULL, uniq },
{ name, NULL },
{ NULL, NULL },
};
struct match_approach *approach = approaches;
while (true) {
const char *match_name = approach->name;
const char *match_uniq = approach->uniq;
device = libwacom_new (db, match_name, match_uniq, vendor_id, product_id, bus, error);
if (device == NULL) {
match_name = NULL;
device = libwacom_new (db, match_name, match_uniq, vendor_id, product_id, bus, error);
if (device == NULL) {
match_uniq = uniq;
device = libwacom_new (db, match_name, match_uniq, vendor_id, product_id, bus, error);
}
if (device) {
used_match_name = match_name;
used_match_uniq = match_uniq;
break;
}

if (approach->name == NULL && approach->uniq == NULL)
break;

approach++;
}

if (device == NULL) {
Expand All @@ -673,7 +688,7 @@ libwacom_new_from_path(const WacomDeviceDatabase *db, const char *path, WacomFal
}

/* for multiple-match devices, set to the one we requested */
match = libwacom_match_new(match_name, match_uniq, bus, vendor_id, product_id);
match = libwacom_match_new(used_match_name, used_match_uniq, bus, vendor_id, product_id);
libwacom_set_default_match(ret, match);
libwacom_match_unref(match);

Expand Down

0 comments on commit 3132b2b

Please sign in to comment.