summaryrefslogtreecommitdiff
path: root/tools/list-local-devices.c
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2021-04-22 15:35:03 +1000
committerPeter Hutterer <peter.hutterer@who-t.net>2021-04-28 14:00:42 +1000
commit68c0ba58aa1e3bc12695aa16330ef0b4836ff217 (patch)
tree9476590ddff951eb15c9a52df2980bd6b01c4d19 /tools/list-local-devices.c
parentc582cb3d07de2adbbf7741f33dc84dbaa78dfc75 (diff)
downloadlibwacom-68c0ba58aa1e3bc12695aa16330ef0b4836ff217.tar.gz
tools: use a YAML-compatible format by default
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>
Diffstat (limited to 'tools/list-local-devices.c')
-rw-r--r--tools/list-local-devices.c59
1 files changed, 39 insertions, 20 deletions
diff --git a/tools/list-local-devices.c b/tools/list-local-devices.c
index 5fffe40..0ba6cce 100644
--- a/tools/list-local-devices.c
+++ b/tools/list-local-devices.c
@@ -38,9 +38,9 @@
#include "libwacom.h"
static enum output_format {
- ONELINE,
+ YAML,
DATAFILE,
-} output_format = ONELINE;
+} output_format = YAML;
static char *database_path;
@@ -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
@@ -129,8 +146,8 @@ 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;
@@ -138,7 +155,7 @@ check_format(const gchar *option_name, const gchar *value, gpointer data, GError
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}
};
@@ -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);