diff --git a/src/xrt/targets/cli/CMakeLists.txt b/src/xrt/targets/cli/CMakeLists.txt index b3ebe501a..cf2f445d6 100644 --- a/src/xrt/targets/cli/CMakeLists.txt +++ b/src/xrt/targets/cli/CMakeLists.txt @@ -12,6 +12,7 @@ include_directories( ) set(SOURCE_FILES + cli_cmd_calibrate.c cli_cmd_test.c cli_common.h cli_main.c diff --git a/src/xrt/targets/cli/cli_cmd_calibrate.c b/src/xrt/targets/cli/cli_cmd_calibrate.c new file mode 100644 index 000000000..6d95197e3 --- /dev/null +++ b/src/xrt/targets/cli/cli_cmd_calibrate.c @@ -0,0 +1,144 @@ +// Copyright 2019, Collabora, Ltd. +// SPDX-License-Identifier: BSL-1.0 +/*! + * @file + * @brief Prints a list of found devices and tests opening some of them. + * @author Jakob Bornecrantz + */ + +#include +#include +#include + +#include "xrt/xrt_prober.h" +#include "util/u_misc.h" +#include "cli_common.h" + + +struct program +{ + struct xrt_prober *xp; + + int index; + int selected; +}; + +static int +init(struct program *p) +{ + int ret; + + // Fist initialize the prober. + ret = xrt_prober_create(&p->xp); + if (ret != 0) { + fprintf(stderr, "Failed to create prober\n"); + return ret; + } + + // Need to prime the prober before listing devices. + ret = p->xp->probe(p->xp); + if (ret != 0) { + fprintf(stderr, "Failed to probe for devices.\n"); + return ret; + } + + return 0; +} + +static void +list_cb(struct xrt_prober *xp, + struct xrt_prober_device *pdev, + const char *name, + void *ptr) +{ + struct program *p = ptr; + if (p->selected <= 0) { + printf(" %i) %s\n", ++p->index, name); + } else if (p->selected == ++p->index) { + // Do stuff + printf(" :: Doing calibrartion\n"); + printf(" Pretending to calibrarating camera '%s'\n", name); + } +} + +static int +print_cameras(struct program *p) +{ + char *buffer = NULL; + size_t buffer_size = 0; + int selected = -1; + ssize_t len; + int ret; + + p->index = 0; + ret = p->xp->list_video_devices(p->xp, list_cb, p); + if (ret != 0) { + return ret; + } + + if (p->index <= 0) { + printf("\tNo video devices found!\n"); + return -1; + } + + printf("Please select camera: "); + fflush(stdout); + + len = getline(&buffer, &buffer_size, stdin); + + if (buffer && len >= 1) { + selected = (int)strtol(buffer, NULL, 10); + } + + if (selected < 1 || selected > p->index) { + printf("Invalid camera! %*.s", (int)len, buffer); + free(buffer); + return -1; + } + free(buffer); + + p->index = 0; + p->selected = selected; + ret = p->xp->list_video_devices(p->xp, list_cb, p); + if (ret != 0) { + return ret; + } + + + return 0; +} + +static int +do_exit(struct program *p, int ret) +{ + if (p->xp != NULL) { + p->xp->destroy(&p->xp); + } + + printf(" :: Exiting '%i'\n", ret); + + return ret; +} + +int +cli_cmd_calibrate(int argc, const char **argv) +{ + struct program p = {0}; + int ret; + + printf(" :: Starting!\n"); + + // Init the prober and other things. + ret = init(&p); + if (ret != 0) { + return do_exit(&p, ret); + } + + // List the cameras found. + ret = print_cameras(&p); + if (ret != 0) { + return do_exit(&p, ret); + } + + return do_exit(&p, 0); +} diff --git a/src/xrt/targets/cli/cli_common.h b/src/xrt/targets/cli/cli_common.h index 3db326fb1..a7e4b522c 100644 --- a/src/xrt/targets/cli/cli_common.h +++ b/src/xrt/targets/cli/cli_common.h @@ -9,5 +9,8 @@ #pragma once +int +cli_cmd_calibrate(int argc, const char **argv); + int cli_cmd_test(int argc, const char **argv); diff --git a/src/xrt/targets/cli/cli_main.c b/src/xrt/targets/cli/cli_main.c index 9425711af..4b4aa2ab9 100644 --- a/src/xrt/targets/cli/cli_main.c +++ b/src/xrt/targets/cli/cli_main.c @@ -26,6 +26,7 @@ cli_print_help(int argc, const char **argv) P("\n"); P("Commands:\n"); P(" test - List found devices, for prober testing.\n"); + P(" calibrate - Calibrate a camera and save config.\n"); return 1; } @@ -39,6 +40,8 @@ main(int argc, const char **argv) if (strcmp(argv[1], "test") == 0) { return cli_cmd_test(argc, argv); + } else if (strcmp(argv[1], "calibrate") == 0) { + return cli_cmd_calibrate(argc, argv); } else { return cli_print_help(argc, argv); }