| 1 |
/* |
| 2 |
* Copyright © 2012-2021 Red Hat, Inc. |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, distribute, and sell this software |
| 5 |
* and its documentation for any purpose is hereby granted without |
| 6 |
* fee, provided that the above copyright notice appear in all copies |
| 7 |
* and that both that copyright notice and this permission notice |
| 8 |
* appear in supporting documentation, and that the name of Red Hat |
| 9 |
* not be used in advertising or publicity pertaining to distribution |
| 10 |
* of the software without specific, written prior permission. Red |
| 11 |
* Hat makes no representations about the suitability of this software |
| 12 |
* for any purpose. It is provided "as is" without express or implied |
| 13 |
* warranty. |
| 14 |
* |
| 15 |
* THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 16 |
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN |
| 17 |
* NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 18 |
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
| 19 |
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 20 |
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 |
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 |
* |
| 23 |
* Authors: |
| 24 |
* Peter Hutterer <[email protected]> |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "config.h" |
| 28 |
|
| 29 |
#define _GNU_SOURCE |
| 30 |
#include <assert.h> |
| 31 |
#include <glib.h> |
| 32 |
#include <glib/gi18n.h> |
| 33 |
#include <libgen.h> |
| 34 |
#include <stdio.h> |
| 35 |
#include <string.h> |
| 36 |
#include <unistd.h> |
| 37 |
|
| 38 |
#include "libwacom.h" |
| 39 |
|
| 40 |
static enum output_format { |
| 41 |
YAML, |
| 42 |
DATAFILE, |
| 43 |
} output_format = YAML; |
| 44 |
|
| 45 |
static void |
| 46 |
print_device_info(WacomDevice *device, |
| 47 |
WacomBusType bus_type_filter, |
| 48 |
enum output_format format) |
| 49 |
{ |
| 50 |
const WacomMatch **match; |
| 51 |
|
| 52 |
for (match = libwacom_get_matches(device); *match; match++) { |
| 53 |
WacomBusType type = libwacom_match_get_bustype(*match); |
| 54 |
|
| 55 |
if (type != bus_type_filter) |
| 56 |
continue; |
| 57 |
|
| 58 |
if (format == DATAFILE) { |
| 59 |
libwacom_print_device_description(STDOUT_FILENO, device); |
| 60 |
dprintf(STDOUT_FILENO, |
| 61 |
"---------------------------------------------------------------" |
| 62 |
"\n"); |
| 63 |
} else { |
| 64 |
const char *name = libwacom_get_name(device); |
| 65 |
const char *bus = "unknown"; |
| 66 |
int vid = libwacom_match_get_vendor_id(*match); |
| 67 |
int pid = libwacom_match_get_product_id(*match); |
| 68 |
const char *uniq = libwacom_match_get_uniq(*match); |
| 69 |
|
| 70 |
switch (type) { |
| 71 |
case WBUSTYPE_USB: |
| 72 |
bus = "usb"; |
| 73 |
break; |
| 74 |
case WBUSTYPE_SERIAL: |
| 75 |
bus = "serial"; |
| 76 |
break; |
| 77 |
case WBUSTYPE_BLUETOOTH: |
| 78 |
bus = "bluetooth"; |
| 79 |
break; |
| 80 |
case WBUSTYPE_I2C: |
| 81 |
bus = "i2c"; |
| 82 |
break; |
| 83 |
default: |
| 84 |
break; |
| 85 |
} |
| 86 |
|
| 87 |
/* We don't need to print the generic device */ |
| 88 |
if (vid != 0 || pid != 0 || bus != 0) |
| 89 |
printf("- { bus: '%s',%*svid: '0x%04x', pid: '0x%04x', name: '%s', " |
| 90 |
"uniq: '%s' }\n", |
| 91 |
bus, |
| 92 |
(int)(10 - strlen(bus)), |
| 93 |
" ", |
| 94 |
vid, |
| 95 |
pid, |
| 96 |
name, |
| 97 |
uniq ? uniq : ""); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
static gboolean |
| 103 |
check_format(const gchar *option_name, |
| 104 |
const gchar *value, |
| 105 |
gpointer data, |
| 106 |
GError **error) |
| 107 |
{ |
| 108 |
if (g_str_equal(value, "datafile")) |
| 109 |
output_format = DATAFILE; |
| 110 |
else if (g_str_equal(value, "yaml")) |
| 111 |
output_format = YAML; |
| 112 |
else |
| 113 |
return FALSE; |
| 114 |
return TRUE; |
| 115 |
} |
| 116 |
|
| 117 |
/* clang-format off */ |
| 118 |
static GOptionEntry opts[] = { |
| 119 |
{ "format", 0, 0, G_OPTION_ARG_CALLBACK, check_format, N_("Output format, one of 'yaml', 'datafile'"), NULL }, |
| 120 |
{ .long_name = NULL } |
| 121 |
}; |
| 122 |
/* clang-format on */ |
| 123 |
|
| 124 |
int |
| 125 |
main(int argc, |
| 126 |
char **argv) |
| 127 |
{ |
| 128 |
WacomDeviceDatabase *db; |
| 129 |
g_autofree WacomDevice **list = NULL; |
| 130 |
WacomDevice **p; |
| 131 |
g_autoptr(GOptionContext) context = g_option_context_new(NULL); |
| 132 |
g_autoptr(GError) error = NULL; |
| 133 |
|
| 134 |
g_option_context_add_main_entries(context, opts, NULL); |
| 135 |
if (!g_option_context_parse(context, &argc, &argv, &error)) { |
| 136 |
if (error != NULL) |
| 137 |
fprintf(stderr, "%s\n", error->message); |
| 138 |
return EXIT_FAILURE; |
| 139 |
} |
| 140 |
|
| 141 |
#ifdef DATABASEPATH |
| 142 |
db = libwacom_database_new_for_path(DATABASEPATH); |
| 143 |
#else |
| 144 |
db = libwacom_database_new(); |
| 145 |
#endif |
| 146 |
|
| 147 |
list = libwacom_list_devices_from_database(db, NULL); |
| 148 |
if (!list) { |
| 149 |
fprintf(stderr, "Failed to load device database.\n"); |
| 150 |
return 1; |
| 151 |
} |
| 152 |
|
| 153 |
if (output_format == YAML) |
| 154 |
printf("devices:\n"); |
| 155 |
|
| 156 |
for (p = list; *p; p++) |
| 157 |
print_device_info((WacomDevice *)*p, WBUSTYPE_USB, output_format); |
| 158 |
|
| 159 |
for (p = list; *p; p++) |
| 160 |
print_device_info((WacomDevice *)*p, WBUSTYPE_BLUETOOTH, output_format); |
| 161 |
|
| 162 |
for (p = list; *p; p++) |
| 163 |
print_device_info((WacomDevice *)*p, WBUSTYPE_I2C, output_format); |
| 164 |
|
| 165 |
for (p = list; *p; p++) |
| 166 |
print_device_info((WacomDevice *)*p, WBUSTYPE_SERIAL, output_format); |
| 167 |
|
| 168 |
for (p = list; *p; p++) |
| 169 |
print_device_info((WacomDevice *)*p, WBUSTYPE_UNKNOWN, output_format); |
| 170 |
|
| 171 |
libwacom_database_destroy(db); |
| 172 |
|
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
/* vim: set noexpandtab tabstop=8 shiftwidth=8: */ |