Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A number of cleanup patches #755

Merged
merged 8 commits into from
Aug 19, 2024
99 changes: 42 additions & 57 deletions libwacom/libwacom-database.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ libwacom_parse_stylus_keyfile(WacomDeviceDatabase *db, const char *path)
for (i = 0; groups[i]; i++) {
WacomStylus *stylus;
GError *error = NULL;
char *type;
char *eraser_type, *type;
int id;
char **string_list;

Expand All @@ -285,27 +285,22 @@ libwacom_parse_stylus_keyfile(WacomDeviceDatabase *db, const char *path)
stylus->name = g_key_file_get_string(keyfile, groups[i], "Name", NULL);
stylus->group = g_key_file_get_string(keyfile, groups[i], "Group", NULL);

type = g_key_file_get_string(keyfile, groups[i], "EraserType", NULL);
stylus->eraser_type = eraser_type_from_str (type);
g_free (type);
eraser_type = g_key_file_get_string(keyfile, groups[i], "EraserType", NULL);
stylus->eraser_type = eraser_type_from_str (eraser_type);
g_clear_pointer(&eraser_type, g_free);

string_list = g_key_file_get_string_list (keyfile, groups[i], "PairedStylusIds", NULL, NULL);
stylus->paired_ids = g_array_new (FALSE, FALSE, sizeof(int));
if (string_list) {
guint j;

for (j = 0; string_list[j]; j++) {
int val;
string_list = g_key_file_get_string_list (keyfile, groups[i], "PairedStylusIds", NULL, NULL);
for (guint j = 0; string_list && string_list[j]; j++) {
int val;

if (safe_atoi_base (string_list[j], &val, 16)) {
g_array_append_val (stylus->paired_ids, val);
} else {
g_warning ("Stylus %s (%s) Ignoring invalid PairedStylusIds value\n", stylus->name, groups[i]);
}
if (safe_atoi_base (string_list[j], &val, 16)) {
g_array_append_val (stylus->paired_ids, val);
} else {
g_warning ("Stylus %s (%s) Ignoring invalid PairedStylusIds value\n", stylus->name, groups[i]);
}

g_strfreev (string_list);
}
g_clear_pointer(&string_list, g_strfreev);

stylus->has_lens = g_key_file_get_boolean(keyfile, groups[i], "HasLens", &error);
if (error && error->code == G_KEY_FILE_ERROR_INVALID_VALUE)
Expand All @@ -322,49 +317,42 @@ libwacom_parse_stylus_keyfile(WacomDeviceDatabase *db, const char *path)
g_clear_error (&error);
}

stylus->axes = WACOM_AXIS_TYPE_NONE;
string_list = g_key_file_get_string_list (keyfile, groups[i], "Axes", NULL, NULL);
if (string_list) {
WacomAxisTypeFlags axes = WACOM_AXIS_TYPE_NONE;
guint j;

for (j = 0; string_list[j]; j++) {
WacomAxisTypeFlags flag = WACOM_AXIS_TYPE_NONE;
if (g_str_equal(string_list[j], "Tilt")) {
flag = WACOM_AXIS_TYPE_TILT;
} else if (g_str_equal(string_list[j], "RotationZ")) {
flag = WACOM_AXIS_TYPE_ROTATION_Z;
} else if (g_str_equal(string_list[j], "Distance")) {
flag = WACOM_AXIS_TYPE_DISTANCE;
} else if (g_str_equal(string_list[j], "Pressure")) {
flag = WACOM_AXIS_TYPE_PRESSURE;
} else if (g_str_equal(string_list[j], "Slider")) {
flag = WACOM_AXIS_TYPE_SLIDER;
} else {
g_warning ("Invalid axis %s for stylus ID %s\n",
string_list[j], groups[i]);
}
if (axes & flag)
g_warning ("Duplicate axis %s for stylus ID %s\n",
string_list[j], groups[i]);
axes |= flag;
for (guint j = 0; string_list && string_list[j]; j++) {
WacomAxisTypeFlags flag = WACOM_AXIS_TYPE_NONE;
if (g_str_equal(string_list[j], "Tilt")) {
flag = WACOM_AXIS_TYPE_TILT;
} else if (g_str_equal(string_list[j], "RotationZ")) {
flag = WACOM_AXIS_TYPE_ROTATION_Z;
} else if (g_str_equal(string_list[j], "Distance")) {
flag = WACOM_AXIS_TYPE_DISTANCE;
} else if (g_str_equal(string_list[j], "Pressure")) {
flag = WACOM_AXIS_TYPE_PRESSURE;
} else if (g_str_equal(string_list[j], "Slider")) {
flag = WACOM_AXIS_TYPE_SLIDER;
} else {
g_warning ("Invalid axis %s for stylus ID %s\n",
string_list[j], groups[i]);
}

stylus->axes = axes;
g_strfreev (string_list);
if (stylus->axes & flag)
g_warning ("Duplicate axis %s for stylus ID %s\n",
string_list[j], groups[i]);
stylus->axes |= flag;
}
g_clear_pointer(&string_list, g_strfreev);

type = g_key_file_get_string(keyfile, groups[i], "Type", NULL);
stylus->type = type_from_str (type);
g_free (type);
g_clear_pointer(&type, g_free);

if (g_hash_table_lookup (db->stylus_ht, GINT_TO_POINTER (id)) != NULL)
g_warning ("Duplicate definition for stylus ID '%#x'", id);

g_hash_table_insert (db->stylus_ht, GINT_TO_POINTER (id), stylus);
}
g_strfreev (groups);
if (keyfile)
g_key_file_free (keyfile);
g_clear_pointer(&groups, g_strfreev);
g_clear_pointer(&keyfile, g_key_file_free);
}

static void
Expand Down Expand Up @@ -900,16 +888,13 @@ libwacom_parse_tablet_keyfile(WacomDeviceDatabase *db,
g_free(class);

string_list = g_key_file_get_string_list(keyfile, DEVICE_GROUP, "Styli", NULL, NULL);
if (string_list) {
libwacom_parse_styli_list(db, device, string_list);
g_strfreev (string_list);
} else {
int fallback_eraser = WACOM_ERASER_FALLBACK_ID;
int fallback_stylus = WACOM_STYLUS_FALLBACK_ID;
device->styli = g_array_new(FALSE, FALSE, sizeof(int));
g_array_append_val(device->styli, fallback_eraser);
g_array_append_val(device->styli, fallback_stylus);
if (!string_list) {
string_list = g_new0(char*, 3);
string_list[0] = g_strdup_printf("0x%x", WACOM_ERASER_FALLBACK_ID);
string_list[1] = g_strdup_printf("0x%x", WACOM_STYLUS_FALLBACK_ID);
}
libwacom_parse_styli_list(db, device, string_list);
g_strfreev (string_list);

device->num_strips = g_key_file_get_integer(keyfile, FEATURES_GROUP, "NumStrips", NULL);
device->num_dials = g_key_file_get_integer(keyfile, FEATURES_GROUP, "NumDials", NULL);
Expand Down
24 changes: 5 additions & 19 deletions libwacom/libwacom.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,10 @@ libwacom_copy(const WacomDevice *device)
d->height = device->height;
d->integration_flags = device->integration_flags;
d->layout = g_strdup(device->layout);
d->matches = g_array_sized_new(TRUE, TRUE, sizeof(WacomDevice*),
device->matches->len);
d->matches = g_array_copy(device->matches);
for (guint i = 0; i < device->matches->len; i++) {
WacomMatch *m = g_array_index(device->matches, WacomMatch*, i);
WacomMatch *m = g_array_index(d->matches, WacomMatch*, i);
libwacom_match_ref(m);
g_array_append_val(d->matches, m);
}
d->match = libwacom_match_ref(device->match);
if (device->paired)
Expand All @@ -420,20 +418,8 @@ libwacom_copy(const WacomDevice *device)
d->dial2_num_modes = device->dial2_num_modes;
d->ring_num_modes = device->ring_num_modes;
d->ring2_num_modes = device->ring2_num_modes;
d->styli = g_array_sized_new(FALSE, FALSE, sizeof(int),
device->styli->len);
for (guint i = 0; i < device->styli->len; i++) {
int id = g_array_index(device->styli, int, i);
g_array_append_val(d->styli, id);
}
d->status_leds = g_array_sized_new(FALSE, FALSE,
sizeof(WacomStatusLEDs),
device->status_leds->len);
for (guint i = 0; i < device->status_leds->len; i++) {
g_array_append_val(d->status_leds,
g_array_index(device->status_leds, WacomStatusLEDs, i));
}

d->styli = g_array_copy(device->styli);
d->status_leds = g_array_copy(device->status_leds);
d->buttons = g_hash_table_new_full(g_direct_hash, g_direct_equal,
NULL, g_free);
g_hash_table_iter_init(&iter, device->buttons);
Expand Down Expand Up @@ -569,7 +555,7 @@ libwacom_compare(const WacomDevice *a, const WacomDevice *b, WacomCompareFlags f

if (a->status_leds->len > 0 &&
memcmp(a->status_leds->data, b->status_leds->data,
sizeof(WacomStatusLEDs) * a->status_leds->len) != 0)
g_array_get_element_size(a->status_leds) * a->status_leds->len) != 0)
return 1;

g_hash_table_iter_init(&iter, a->buttons);
Expand Down
2 changes: 1 addition & 1 deletion run-full-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ date=`date +"%Y-%m-%d-%H.%M.%S"`
builddir="build.$date"

echo "####################################### running test suite"
meson $builddir
meson setup $builddir
ninja -C $builddir test

echo "####################################### running valgrind"
Expand Down
41 changes: 9 additions & 32 deletions test/test-stylus-validity.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,8 @@ static const WacomStylus **
assemble_styli(WacomDeviceDatabase *db)
{
WacomDevice **devices = libwacom_list_devices_from_database(db, NULL);
const WacomStylus **styli;
int *ids = NULL;
int nids = 0;
int sz = 0;
GHashTable *all = g_hash_table_new(g_direct_hash, g_direct_equal);
const WacomStylus **all_styli = NULL;

g_assert(devices);

Expand All @@ -457,39 +455,18 @@ assemble_styli(WacomDeviceDatabase *db)
int nstyli;

styli = libwacom_get_supported_styli(*d, &nstyli);

/* Make sure our array is large enough to accommodate for
all new styli. Simpler than reallocing after every entry */
if (nstyli > sz - nids) {
sz = nids + nstyli;
ids = realloc(ids, sz * sizeof(*ids));
g_assert(ids);
}

/* For each stylus in the current device, add it to ids[] if
it's not already in there */
for (int i = 0; i < nstyli; i++) {
gboolean found = FALSE;

for (int j = 0; j < nids && !found; j++) {
if (ids[j] == styli[i])
found = TRUE;
}

if (!found)
ids[nids++] = styli[i];
const WacomStylus *stylus = libwacom_stylus_get_for_id (db, styli[i]);
g_hash_table_add(all, (gpointer)stylus);
}
}

styli = calloc(nids + 1, sizeof(*styli));
for (int i = 0; i < nids; i++) {
styli[i] = libwacom_stylus_get_for_id (db, ids[i]);
g_assert(styli[i]);
}
all_styli = (const WacomStylus**)g_hash_table_get_keys_as_array(all, NULL);
g_hash_table_steal_all(all);
g_clear_pointer(&all, g_hash_table_unref);
g_clear_pointer(&devices, g_free);

free(devices);
free(ids);
return styli;
return all_styli;
}

static WacomDeviceDatabase *
Expand Down
4 changes: 4 additions & 0 deletions tools/list-local-devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ tablet_print_yaml(gpointer data, gpointer user_data)
WacomEraserType eraser_type = libwacom_stylus_get_eraser_type(stylus);
const char *etype= "unknown";

/* warning: Value stored to 'type' during its initialization is never read [deadcode.DeadStores] */
(void)type;
(void)etype;

switch (libwacom_stylus_get_type(stylus)) {
case WSTYLUS_UNKNOWN: type = "unknown"; break;
case WSTYLUS_GENERAL: type = "general"; break;
Expand Down