Skip to content

Commit

Permalink
tools: use a YAML-compatible format by default
Browse files Browse the repository at this point in the history
Let's output something that is both human- and machine-readable. This makes
it easier to search for specific devices with YAML parsers, e.g. to select all
bluetooth devices one could use:

    $ libwacom-list-devices | yq '.devices[] | select(.bus == "bluetooth") | .name'

Output for list-devices is now:

    devices:
    - { bus: 'i2c', vid: '0x056a', pid: '0x5169', name: 'Wacom ISDv4 5169' }
      ...

Output for list-local-devices is now for example:

    devices:
    - name: 'Wacom Intuos Pro M'
      bus: 'usb'
      vid: '0x056a'
      pid: '0x0357'
      nodes:
      - /dev/input/event24
      - /dev/input/event23
      - /dev/input/event22

Output for list-compatible-stylis is now for example:
   - name: 'ISDv4 0163'
     styli:
       - { id:     '0x1', name: 'AES Pen' }
       - { id:    '0x11', name: 'AES Pen' }
       - { id:    '0x19', name: 'AES Pen' }

Note that as shown above, all hex numbers are encoded as strings. This
prevents automatic conversion to decimal when using yq or other yaml
formatters since the final result of the processing is almost always for human
consumption (and thus needs the hex codes).

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
  • Loading branch information
whot committed Apr 28, 2021
1 parent c582cb3 commit 68c0ba5
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 47 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on: [ push, pull_request ]
env:
CFLAGS: "-Werror -Wall -Wextra -Wno-error=sign-compare -Wno-error=unused-parameter -Wno-error=missing-field-initializers"
UBUNTU_PACKAGES: libgudev-1.0-dev libxml++2.6-dev valgrind tree python3-pip python3-setuptools libevdev2
PIP_PACKAGES: meson ninja libevdev pyudev pytest
PIP_PACKAGES: meson ninja libevdev pyudev pytest yq

jobs:
###
Expand Down Expand Up @@ -197,7 +197,7 @@ jobs:
ninja_args: install
ninja_precmd: sudo
- name: list devices with database in /usr
run: libwacom-list-devices --format=oneline > devicelist.default.txt
run: libwacom-list-devices --format=yaml > devicelist.default.txt
- run: sudo mkdir /etc/libwacom
# We override a Cintiq 27QHD with a single device match, and one of
# the multiple matches of the Intuos Pro L.
Expand All @@ -206,17 +206,17 @@ jobs:
sed -e 's/27QHD/27QHD MODIFIED/' data/cintiq-27hd.tablet | sudo tee /etc/libwacom/modified-cintiq.tablet
sed -e 's/Pro L/Pro L MODIFIED/' -e 's/usb:056a:0358;//' data/intuos-pro-2-l.tablet | sudo tee /etc/libwacom/modified-intuos.tablet
- name: list all devices for debugging
run: libwacom-list-devices --format=oneline
run: libwacom-list-devices --format=yaml

# We expect the modified tablets to be listed
# We expect the remaining match for a modified tablet to be listed
# We expect the overridden match *not* to be listed
- name: check for the expected devices to be present (or not present)
run: |
libwacom-list-devices --format=oneline | grep "usb:056a:032a:Wacom Cintiq 27QHD MODIFIED"
libwacom-list-devices --format=oneline | grep "bluetooth:056a:0361:Wacom Intuos Pro L MODIFIED"
test $(libwacom-list-devices --format=oneline | grep "usb:056a:0358" | wc -l) -eq 1
test $(libwacom-list-devices --format=oneline | grep "usb:056a:032a:Wacom Cintiq 27QHD M" | wc -l) -eq 1
test "$(libwacom-list-devices --format=yaml | yq -r '.devices[] | select(.bus == "usb") | select(.vid == "0x056a") | select(.pid == "0x032a") | .name')" == "Wacom Cintiq 27QHD MODIFIED"
test "$(libwacom-list-devices --format=yaml | yq -r '.devices[] | select(.bus == "bluetooth") | select(.vid == "0x056a") | select(.pid == "0x0361") | .name')" == "Wacom Intuos Pro L MODIFIED"
test $(libwacom-list-devices --format=yaml | yq -r '.devices[] | select(.bus == "usb") | select(.vid == "0x056a") | select(.pid == "0x0358") | .name' | wc -l) -eq 1
test $(libwacom-list-devices --format=yaml | yq -r '.devices[] | select(.bus == "usb") | select(.vid == "0x056a") | select(.pid == "0x032a") | .name' | wc -l) -eq 1
###
#
Expand Down
10 changes: 5 additions & 5 deletions tools/libwacom-list-devices.man
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
libwacom-list-devices - utility to list supported tablet devices

.SH SYNOPSIS
.B libwacom-list-devices [--format=oneline|datafile]
.B libwacom-list-devices [--format=yaml|datafile]

.SH DESCRIPTION
libwacom-list-devices is a debug utility to list all supported tablet
devices identified by libwacom. It is usually used to check whether a
libwacom installation is correct after adding custom data files.
.SH OPTIONS
.TP 8
.B --format=oneline|datafile
Sets the output format to be used. If \fIoneline\fR, the output format is a
one-line format comprising the bus type, vendor and product ID and the
.B --format=yaml|datafile
Sets the output format to be used. If \fIyaml\fR, the output format is
YAML comprising the bus type, vendor and product ID and the
device name. If \fIdatafile\fR, the output format matches
the tablet data files. The default is \fIoneline\fR.
the tablet data files. The default is \fIyaml\fR.
18 changes: 10 additions & 8 deletions tools/list-compatible-styli.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,26 @@ print_device_info(const WacomDeviceDatabase *db, const WacomDevice *device)
const int *styli;
int nstyli;

printf("%s", libwacom_get_name(device));
printf("- name: '%s'\n", libwacom_get_name(device));
if (libwacom_get_model_name(device)) {
printf(" (%s)", libwacom_get_model_name(device));
printf(" model: '%s'\n", libwacom_get_model_name(device));
}
printf(":\n");

if (!libwacom_has_stylus(device)) {
printf("\tno styli defined\n");
printf(" styli: [] # no styli defined\n");
return;
}

printf(" styli:\n");

styli = libwacom_get_supported_styli(device, &nstyli);
for (int i = 0; i < nstyli; i++) {
const WacomStylus *s;
char id[64];

s = libwacom_stylus_get_for_id(db, styli[i]);
printf("\t%#8x:\t%s\n",
libwacom_stylus_get_id(s),
snprintf(id, sizeof(id), "0x%x", libwacom_stylus_get_id(s));
printf(" - { id: %*s'%s', name: '%s' }\n",
(int)(7 - strlen(id)), " ", id,
libwacom_stylus_get_name(s));
}
}
Expand All @@ -68,7 +70,7 @@ int main(int argc, char **argv)
WacomDevice **list, **p;

if (argc > 1) {
printf("Usage: %s [--help] - list all supported devices\n",
printf("Usage: %s [--help] - list compatible styli\n",
basename(argv[0]));
return !!(strcmp(argv[1], "--help"));
}
Expand Down
19 changes: 12 additions & 7 deletions tools/list-devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
#include "libwacom.h"

static enum output_format {
ONELINE,
YAML,
DATAFILE,
} output_format = ONELINE;
} output_format = YAML;

static void print_device_info (WacomDevice *device, WacomBusType bus_type_filter,
enum output_format format)
Expand All @@ -57,7 +57,7 @@ static void print_device_info (WacomDevice *device, WacomBusType bus_type_filter
dprintf(STDOUT_FILENO, "---------------------------------------------------------------\n");
} else {
const char *name = libwacom_get_name(device);
const char *bus = NULL;
const char *bus = "unknown";
int vid = libwacom_match_get_vendor_id(*match);
int pid = libwacom_match_get_product_id(*match);

Expand All @@ -72,7 +72,9 @@ static void print_device_info (WacomDevice *device, WacomBusType bus_type_filter

/* We don't need to print the generic device */
if (vid != 0 || pid != 0 || bus != 0)
printf("%s:%04x:%04x:%s\n", bus ? bus : "unknown", vid, pid, name);
printf("- { bus: '%s',%*svid: '0x%04x', pid: '0x%04x', name: '%s' }\n",
bus, (int)(10 - strlen(bus)), " ",
vid, pid, name);
}
}
}
Expand All @@ -82,15 +84,15 @@ check_format(const gchar *option_name, const gchar *value, gpointer data, GError
{
if (g_str_equal(value, "datafile"))
output_format = DATAFILE;
else if (g_str_equal(value, "oneline"))
output_format = ONELINE;
else if (g_str_equal(value, "yaml"))
output_format = YAML;
else
return FALSE;
return TRUE;
}

static GOptionEntry opts[] = {
{ "format", 0, 0, G_OPTION_ARG_CALLBACK, check_format, N_("Output format, one of 'oneline', 'datafile'"), NULL },
{ "format", 0, 0, G_OPTION_ARG_CALLBACK, check_format, N_("Output format, one of 'yaml', 'datafile'"), NULL },
{ .long_name = NULL}
};

Expand Down Expand Up @@ -124,6 +126,9 @@ int main(int argc, char **argv)
return 1;
}

if (output_format == YAML)
printf("devices:\n");

for (p = list; *p; p++)
print_device_info ((WacomDevice *) *p, WBUSTYPE_USB, output_format);

Expand Down
59 changes: 39 additions & 20 deletions tools/list-local-devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
#include "libwacom.h"

static enum output_format {
ONELINE,
YAML,
DATAFILE,
} output_format = ONELINE;
} output_format = YAML;

static char *database_path;

Expand Down Expand Up @@ -92,17 +92,34 @@ tablet_print(gpointer data, gpointer user_data)
static void
print_str(gpointer data, gpointer user_data)
{
printf("%s ", (char *)data);
printf(" - %s\n", (char *)data);
}

static void
tablet_print_oneline(gpointer data, gpointer user_data)
tablet_print_yaml(gpointer data, gpointer user_data)
{
struct tablet *d = data;
const char *name = libwacom_get_name(d->dev);
const char *bus = "unknown";
int vid = libwacom_get_vendor_id(d->dev);
int pid = libwacom_get_product_id(d->dev);
WacomBusType bustype = libwacom_get_bustype(d->dev);

switch (bustype) {
case WBUSTYPE_USB: bus = "usb"; break;
case WBUSTYPE_SERIAL: bus = "serial"; break;
case WBUSTYPE_BLUETOOTH:bus = "bluetooth"; break;
case WBUSTYPE_I2C: bus = "i2c"; break;
default:
break;
}

printf("%s: ", libwacom_get_name(d->dev));
printf("- name: '%s'\n", name);
printf(" bus: '%s'\n", bus);
printf(" vid: '0x%04x'\n", vid);
printf(" pid: '0x%04x'\n", pid);
printf(" nodes: \n");
g_list_foreach(d->nodes, print_str, NULL);
printf("\n");
}

static void
Expand All @@ -129,16 +146,16 @@ check_format(const gchar *option_name, const gchar *value, gpointer data, GError
{
if (g_str_equal(value, "datafile"))
output_format = DATAFILE;
else if (g_str_equal(value, "oneline"))
output_format = ONELINE;
else if (g_str_equal(value, "yaml"))
output_format = YAML;
else
return FALSE;
return TRUE;
}

static GOptionEntry opts[] = {
{"database", 0, 0, G_OPTION_ARG_FILENAME, &database_path, N_("Path to device database"), NULL },
{ "format", 0, 0, G_OPTION_ARG_CALLBACK, check_format, N_("Output format, one of 'oneline', 'datafile'"), NULL },
{ "format", 0, 0, G_OPTION_ARG_CALLBACK, check_format, N_("Output format, one of 'yaml', 'datafile'"), NULL },
{ .long_name = NULL}
};

Expand Down Expand Up @@ -214,18 +231,20 @@ int main(int argc, char **argv)
}
}

if (!tabletlist)
if (!tabletlist) {
fprintf(stderr, "Failed to find any devices known to libwacom.\n");

switch (output_format) {
case DATAFILE:
g_list_foreach(tabletlist, tablet_print, NULL);
break;
case ONELINE:
g_list_foreach(tabletlist, tablet_print_oneline, NULL);
break;
default:
abort();
} else {
switch (output_format) {
case DATAFILE:
g_list_foreach(tabletlist, tablet_print, NULL);
break;
case YAML:
printf("devices:\n");
g_list_foreach(tabletlist, tablet_print_yaml, NULL);
break;
default:
abort();
}
}

g_list_free_full(tabletlist, tablet_destroy);
Expand Down

0 comments on commit 68c0ba5

Please sign in to comment.