The open source OpenXR runtime
1// Copyright 2019, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Prints a list of found devices and tests opening some of them.
6 * @author Jakob Bornecrantz <jakob@collabora.com>
7 */
8
9#include <string.h>
10#include <stdlib.h>
11#include <stdio.h>
12#include <sys/types.h>
13
14#include "xrt/xrt_instance.h"
15#include "xrt/xrt_prober.h"
16#include "util/u_misc.h"
17#include "cli_common.h"
18
19
20struct program
21{
22 struct xrt_instance *xi;
23 struct xrt_prober *xp;
24
25 int index;
26 int selected;
27};
28
29static int
30init(struct program *p)
31{
32 xrt_result_t xret;
33 int ret;
34
35 // Fist initialize the instance.
36 ret = xrt_instance_create(NULL, &p->xi);
37 if (ret != 0) {
38 fprintf(stderr, "Failed to create instance\n");
39 return ret;
40 }
41
42 // Get the prober pointer.
43 // In general, null probers are OK, but this module directly uses the
44 // prober.
45 xret = xrt_instance_get_prober(p->xi, &p->xp);
46 if (xret != XRT_SUCCESS) {
47 fprintf(stderr, "Failed to get prober from instance.\n");
48 return -1;
49 }
50 if (p->xp == NULL) {
51 fprintf(stderr, "Null prober returned - cannot proceed.\n");
52 return -1;
53 }
54
55 // Need to prime the prober before listing devices.
56 xret = xrt_prober_probe(p->xp);
57 if (xret != XRT_SUCCESS) {
58 fprintf(stderr, "Failed to probe for devices.\n");
59 return -1;
60 }
61
62 return 0;
63}
64
65static void
66list_cb(struct xrt_prober *xp,
67 struct xrt_prober_device *pdev,
68 const char *product,
69 const char *manufacturer,
70 const char *serial,
71 void *ptr)
72{
73 struct program *p = (struct program *)ptr;
74 if (p->selected <= 0) {
75 printf(" %i) %s\n", ++p->index, product);
76 } else if (p->selected == ++p->index) {
77 // Do stuff
78 printf(" :: Doing calibration\n");
79 printf(" Pretending to calibrate camera '%s'\n", product);
80 }
81}
82
83static int
84print_cameras(struct program *p)
85{
86 char *buffer = NULL;
87 size_t buffer_size = 0;
88 int selected = -1;
89 ssize_t len;
90 int ret;
91
92 p->index = 0;
93 ret = xrt_prober_list_video_devices(p->xp, list_cb, p);
94 if (ret != 0) {
95 return ret;
96 }
97
98 if (p->index <= 0) {
99 printf("\tNo video devices found!\n");
100 return -1;
101 }
102
103 printf("Please select camera: ");
104 fflush(stdout);
105
106 len = getline(&buffer, &buffer_size, stdin);
107
108 if (buffer && len >= 1) {
109 selected = (int)strtol(buffer, NULL, 10);
110 }
111
112 if (selected < 1 || selected > p->index) {
113 printf("Invalid camera! %*.s", (int)len, buffer);
114 free(buffer);
115 return -1;
116 }
117 free(buffer);
118
119 p->index = 0;
120 p->selected = selected;
121 ret = xrt_prober_list_video_devices(p->xp, list_cb, p);
122 if (ret != 0) {
123 return ret;
124 }
125
126
127 return 0;
128}
129
130static int
131do_exit(struct program *p, int ret)
132{
133 p->xp = NULL;
134 xrt_instance_destroy(&p->xi);
135
136 printf(" :: Exiting '%i'\n", ret);
137
138 return ret;
139}
140
141int
142cli_cmd_calibrate(int argc, const char **argv)
143{
144 struct program p = {0};
145 int ret;
146
147 printf(" :: Starting!\n");
148
149 // Init the prober and other things.
150 ret = init(&p);
151 if (ret != 0) {
152 return do_exit(&p, ret);
153 }
154
155 // List the cameras found.
156 ret = print_cameras(&p);
157 if (ret != 0) {
158 return do_exit(&p, ret);
159 }
160
161 return do_exit(&p, 0);
162}