From 8498186e6c79a5a64f7dd7a89f8611bd0d9bc4b5 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 13 May 2019 22:30:11 +0300 Subject: Pretty-print driver and device from JSON --- drm_info.c | 12 ------- drm_info.h | 9 +++++ json.c | 13 ++----- main.c | 36 +++++++++++++++++++ meson.build | 2 +- pretty.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 161 insertions(+), 24 deletions(-) create mode 100644 drm_info.h create mode 100644 main.c create mode 100644 pretty.c diff --git a/drm_info.c b/drm_info.c index f96c8f4..95c2044 100644 --- a/drm_info.c +++ b/drm_info.c @@ -933,15 +933,3 @@ static void node_info(const char *path) close(fd); } - -/*int main(void) -{ - char path[PATH_MAX]; - for (int i = 0;; ++i) { - snprintf(path, sizeof path, DRM_DEV_NAME, DRM_DIR_NAME, i); - if (access(path, R_OK) < 0) - break; - - node_info(path); - } -}*/ diff --git a/drm_info.h b/drm_info.h new file mode 100644 index 0000000..7c7fa98 --- /dev/null +++ b/drm_info.h @@ -0,0 +1,9 @@ +#ifndef DRM_INFO_H +#define DRM_INFO_H + +struct json_object; + +struct json_object *drm_info(void); +void print_drm(struct json_object *obj); + +#endif diff --git a/json.c b/json.c index fabc63d..f95cb45 100644 --- a/json.c +++ b/json.c @@ -9,11 +9,11 @@ #include #include -#include #include #include #include "config.h" +#include "drm_info.h" // Defines for comaptibility with old libdrm @@ -600,7 +600,7 @@ static struct json_object *node_info(const char *path) return obj; } -static struct json_object *drm_info(void) +struct json_object *drm_info(void) { struct json_object *obj = json_object_new_object(); @@ -616,12 +616,3 @@ static struct json_object *drm_info(void) return obj; } - -int main(void) -{ - struct json_object *obj = drm_info(); - json_object_to_fd(STDOUT_FILENO, obj, - JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED); - json_object_put(obj); - return 0; -} diff --git a/main.c b/main.c new file mode 100644 index 0000000..b404192 --- /dev/null +++ b/main.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include + +#include + +#include "drm_info.h" + +int main(int argc, char *argv[]) +{ + bool json = false; + + int opt; + while ((opt = getopt(argc, argv, "j")) != -1) { + switch (opt) { + case 'j': + json = true; + break; + default: + fprintf(stderr, "usage: drm_info [-j]\n"); + exit(opt == '?' ? EXIT_SUCCESS : EXIT_FAILURE); + } + } + + struct json_object *obj = drm_info(); + if (json) { + json_object_to_fd(STDOUT_FILENO, obj, + JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED); + } else { + print_drm(obj); + } + json_object_put(obj); + return EXIT_SUCCESS; +} diff --git a/meson.build b/meson.build index fdfa42c..eaaa433 100644 --- a/meson.build +++ b/meson.build @@ -27,7 +27,7 @@ config_h = configure_file( ) executable('drm_info', - files('drm_info.c', 'json.c'), + files('main.c', 'json.c', 'pretty.c'), dependencies: [libdrm, jsonc], install: true, ) diff --git a/pretty.c b/pretty.c new file mode 100644 index 0000000..f942559 --- /dev/null +++ b/pretty.c @@ -0,0 +1,113 @@ +#include + +#include +#include +#include + +#include "config.h" +#include "drm_info.h" + +#define L_LINE "│ " +#define L_VAL "├───" +#define L_LAST "└───" +#define L_GAP " " + +static uint64_t get_object_uint64(struct json_object *obj) +{ + return (uint64_t)json_object_get_int64(obj); +} + +static const char *get_object_object_string(struct json_object *obj, + const char *key) +{ + struct json_object *str_obj = json_object_object_get(obj, key); + if (!str_obj) { + return NULL; + } + return json_object_get_string(str_obj); +} + +static uint64_t get_object_object_uint64(struct json_object *obj, + const char *key) +{ + struct json_object *uint64_obj = json_object_object_get(obj, key); + if (!uint64_obj) { + return 0; + } + return get_object_uint64(uint64_obj); +} + +void print_driver(struct json_object *obj) +{ + const char *name = get_object_object_string(obj, "name"); + const char *desc = get_object_object_string(obj, "desc"); + struct json_object *version_obj = json_object_object_get(obj, "version"); + int version_major = get_object_object_uint64(version_obj, "major"); + int version_minor = get_object_object_uint64(version_obj, "minor"); + int version_patch = get_object_object_uint64(version_obj, "patch"); + const char *version_date = get_object_object_string(version_obj, "date"); + + printf(L_VAL "Driver: %s (%s) version %d.%d.%d (%s)\n", name, desc, + version_major, version_minor, version_patch, version_date); + + struct json_object_iter iter; + struct json_object *client_caps_obj = + json_object_object_get(obj, "client_caps"); + json_object_object_foreachC(client_caps_obj, iter) { + json_bool supported = json_object_get_boolean(iter.val); + printf(L_LINE L_VAL "DRM_CLIENT_CAP_%s %s\n", iter.key, + supported ? "supported" : "not supported"); + } + + struct json_object *caps_obj = json_object_object_get(obj, "caps"); + json_object_object_foreachC(caps_obj, iter) { + printf(!iter.entry->next ? L_LINE L_LAST : L_LINE L_VAL); + if (iter.val == NULL) { + printf("DRM_CAP_%s not supported\n", iter.key); + } else { + printf("DRM_CAP_%s = %"PRIu64"\n", iter.key, + get_object_uint64(iter.val)); + } + } +} + +static const char *bustype_str(int type) +{ + switch (type) { + case DRM_BUS_PCI: return "PCI"; + case DRM_BUS_USB: return "USB"; + case DRM_BUS_PLATFORM: return "platform"; + case DRM_BUS_HOST1X: return "host1x"; + default: return "unknown"; + } +} + +void print_device(struct json_object *obj) +{ + int bus_type = get_object_object_uint64(obj, "bus_type"); + struct json_object *data_obj = json_object_object_get(obj, "device_data"); + + printf(L_VAL "Device: %s", bustype_str(bus_type)); + switch (bus_type) { + case DRM_BUS_PCI:; + uint16_t vendor = get_object_object_uint64(data_obj, "vendor"); + uint16_t device = get_object_object_uint64(data_obj, "device"); + printf(" %04x:%04x", vendor, device); + break; + } + printf("\n"); +} + +void print_node(const char *path, struct json_object *obj) +{ + printf("Node: %s\n", path); + print_driver(json_object_object_get(obj, "driver")); + print_device(json_object_object_get(obj, "device")); +} + +void print_drm(struct json_object *obj) +{ + json_object_object_foreach(obj, path, node_obj) { + print_node(path, node_obj); + } +} -- cgit v1.2.3