Debian Codegraph @

utils/v4l2-ctl/v4l2-ctl-common.cpp

raw
1 #include <algorithm>
2 #include <cctype>
3 #include <cstring>
4 #include <list>
5 #include <map>
6 #include <vector>
7
8 #include <dirent.h>
9 #include <sys/stat.h>
10 #include <sys/sysmacros.h>
11
12 #include <linux/media.h>
13
14 #include "v4l2-ctl.h"
15
16 #ifdef HAVE_SYS_KLOG_H
17 #include <sys/klog.h>
18 #endif
19
20 struct ctrl_subset {
21 unsigned offset[V4L2_CTRL_MAX_DIMS];
22 unsigned size[V4L2_CTRL_MAX_DIMS];
23 };
24
25 using class2ctrls_map = std::map<unsigned int, std::vector<struct v4l2_ext_control> >;
26
27 using ctrl_qmap = std::map<std::string, struct v4l2_query_ext_ctrl>;
28 static ctrl_qmap ctrl_str2q;
29 using ctrl_idmap = std::map<unsigned int, std::string>;
30 static ctrl_idmap ctrl_id2str;
31
32 using ctrl_subset_map = std::map<std::string, ctrl_subset>;
33 static ctrl_subset_map ctrl_subsets;
34
35 using ctrl_get_list = std::list<std::string>;
36 static ctrl_get_list get_ctrls;
37
38 using ctrl_set_pair = std::pair<std::string, std::string>;
39 using ctrl_set_list = std::list<ctrl_set_pair>;
40 static ctrl_set_list set_ctrls;
41
42 using dev_vec = std::vector<std::string>;
43 using dev_map = std::map<std::string, std::string>;
44
45 static std::string match_input, match_output;
46
47 static enum v4l2_priority prio = V4L2_PRIORITY_UNSET;
48
49 static bool have_query_ext_ctrl;
50
51 void common_usage()
52 {
53 printf("\nGeneral/Common options:\n"
54 " -A, --list-devices list all v4l devices. If -z was given, then list just the\n"
55 " devices of the media device with the bus info string as\n"
56 " specified by the -z option.\n"
57 " --all display all information available\n"
58 " -C, --get-ctrl <ctrl>[,<ctrl>...]\n"
59 " get the value of the controls [VIDIOC_G_EXT_CTRLS]\n"
60 " -c, --set-ctrl <ctrl>=<val>[,<ctrl>=<val>...]\n"
61 " set the value of the controls [VIDIOC_S_EXT_CTRLS]\n"
62 " -D, --info show driver info [VIDIOC_QUERYCAP]\n"
63 " -d, --device <dev> use device <dev> instead of /dev/video0\n"
64 " if <dev> starts with a digit, then /dev/video<dev> is used\n"
65 " Otherwise if -z was specified earlier, then <dev> is the entity name\n"
66 " or interface ID (if prefixed with 0x) as found in the topology of the\n"
67 " media device with the bus info string as specified by the -z option.\n"
68 " -e, --out-device <dev> use device <dev> for output streams instead of the\n"
69 " default device as set with --device\n"
70 " if <dev> starts with a digit, then /dev/video<dev> is used\n"
71 " Otherwise if -z was specified earlier, then <dev> is the entity name\n"
72 " or interface ID (if prefixed with 0x) as found in the topology of the\n"
73 " media device with the bus info string as specified by the -z option.\n"
74 " -E, --export-device <dev> use device <dev> for exporting DMA buffers\n"
75 " if <dev> starts with a digit, then /dev/video<dev> is used\n"
76 " Otherwise if -z was specified earlier, then <dev> is the entity name\n"
77 " or interface ID (if prefixed with 0x) as found in the topology of the\n"
78 " media device with the bus info string as specified by the -z option.\n"
79 " -z, --media-bus-info <bus-info>\n"
80 " find the media device with the given bus info string. If set, then\n"
81 " -d, -e and -E options can use the entity name or interface ID to refer\n"
82 " to the device nodes.\n"
83 " -h, --help display this help message\n"
84 " --help-all all options\n"
85 " --help-io input/output options\n"
86 " --help-meta metadata format options\n"
87 " --help-misc miscellaneous options\n"
88 " --help-overlay overlay format options\n"
89 " --help-sdr SDR format options\n"
90 " --help-selection crop/selection options\n"
91 " --help-stds standards and other video timings options\n"
92 " --help-streaming streaming options\n"
93 " --help-subdev sub-device options\n"
94 " --help-tuner tuner/modulator options\n"
95 " --help-vbi VBI format options\n"
96 " --help-vidcap video capture format options\n"
97 " --help-vidout vidout output format options\n"
98 " --help-edid edid handling options\n"
99 " -k, --concise be more concise if possible.\n"
100 " -l, --list-ctrls display all controls and their values [VIDIOC_QUERYCTRL]\n"
101 " -L, --list-ctrls-menus\n"
102 " display all controls and their menus [VIDIOC_QUERYMENU]\n"
103 " -r, --subset <ctrl>[,<offset>,<size>]+\n"
104 " the subset of the N-dimensional array to get/set for control <ctrl>,\n"
105 " for every dimension an (<offset>, <size>) tuple is given.\n"
106 #ifndef NO_LIBV4L2
107 " -w, --wrapper use the libv4l2 wrapper library.\n"
108 #endif
109 " --list-devices-input <name>\n"
110 " same as --list-devices, but only show devices with a current\n"
111 " input name that matches <name>\n"
112 " --list-devices-output <name>\n"
113 " same as --list-devices, but only show devices with a current\n"
114 " output name that matches <name>\n"
115 " --log-status log the board status in the kernel log [VIDIOC_LOG_STATUS]\n"
116 " --get-priority query the current access priority [VIDIOC_G_PRIORITY]\n"
117 " --set-priority <prio>\n"
118 " set the new access priority [VIDIOC_S_PRIORITY]\n"
119 " <prio> is 1 (background), 2 (interactive) or 3 (record)\n"
120 " --silent only set the result code, do not print any messages\n"
121 " --sleep <secs> sleep <secs>, call QUERYCAP and close the file handle\n"
122 " --verbose turn on verbose ioctl status reporting\n"
123 " --version show version information\n"
124 );
125 }
126
127 static const char *prefixes[] = {
128 "video",
129 "radio",
130 "vbi",
131 "swradio",
132 "v4l-subdev",
133 "v4l-touch",
134 "media",
135 nullptr
136 };
137
138 static bool is_v4l_dev(const char *name)
139 {
140 for (unsigned i = 0; prefixes[i]; i++) {
141 unsigned l = strlen(prefixes[i]);
142
143 if (!memcmp(name, prefixes[i], l)) {
144 if (isdigit(name[l]))
145 return true;
146 }
147 }
148 return false;
149 }
150
151 static int calc_node_val(const char *s)
152 {
153 int n = 0;
154
155 s = std::strrchr(s, '/') + 1;
156
157 for (unsigned i = 0; prefixes[i]; i++) {
158 unsigned l = strlen(prefixes[i]);
159
160 if (!memcmp(s, prefixes[i], l)) {
161 n = i << 8;
162 n += atol(s + l);
163 return n;
164 }
165 }
166 return 0;
167 }
168
169 static bool sort_on_device_name(const std::string &s1, const std::string &s2)
170 {
171 int n1 = calc_node_val(s1.c_str());
172 int n2 = calc_node_val(s2.c_str());
173
174 return n1 < n2;
175 }
176
177 static void list_media_devices(const std::string &media_bus_info,
178 const std::string &input, const std::string &output)
179 {
180 DIR *dp;
181 struct dirent *ep;
182 int media_fd = -1;
183 std::map<dev_t, std::string> devices;
184 bool match_io = !input.empty() || !output.empty();
185
186 dp = opendir("/dev");
187 if (dp == nullptr) {
188 perror ("Couldn't open the directory");
189 return;
190 }
191 while ((ep = readdir(dp))) {
192 std::string s("/dev/");
193
194 s += ep->d_name;
195 if (memcmp(ep->d_name, "media", 5)) {
196 if (!is_v4l_dev(ep->d_name))
197 continue;
198 struct stat st;
199 if (stat(s.c_str(), &st))
200 continue;
201 devices[st.st_rdev] = s;
202 continue;
203 }
204 int fd = open(s.c_str(), O_RDWR);
205
206 if (fd < 0)
207 continue;
208 struct media_device_info mdi;
209
210 if (!ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi) &&
211 media_bus_info == mdi.bus_info) {
212 media_fd = fd;
213 if (!match_io)
214 printf("%s\n", s.c_str());
215 } else {
216 close(fd);
217 }
218 }
219 closedir(dp);
220 if (media_fd < 0)
221 return;
222
223 media_v2_topology topology;
224 memset(&topology, 0, sizeof(topology));
225 if (ioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topology)) {
226 close(media_fd);
227 return;
228 }
229
230 auto ifaces = new media_v2_interface[topology.num_interfaces];
231 topology.ptr_interfaces = (uintptr_t)ifaces;
232
233 if (!ioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topology))
234 for (unsigned i = 0; i < topology.num_interfaces; i++) {
235 dev_t dev = makedev(ifaces[i].devnode.major,
236 ifaces[i].devnode.minor);
237
238 if (devices.find(dev) == devices.end())
239 continue;
240
241 std::string extra;
242
243 int fd = open(devices[dev].c_str(), O_RDWR);
244
245 if (fd < 0)
246 continue;
247
248 bool match = !match_io;
249
250 if (verbose || match_io) {
251 unsigned idx;
252
253 if (!ioctl(fd, VIDIOC_G_INPUT, &idx)) {
254 struct v4l2_input in = {
255 .index = idx
256 };
257 if (!ioctl(fd, VIDIOC_ENUMINPUT, &in)) {
258 extra = std::string(" (input: ") + (const char *)in.name + ")";
259 match = !match_io || !strcmp(match_input.c_str(), (const char *)in.name);
260 }
261 } else if (!ioctl(fd, VIDIOC_G_OUTPUT, &idx)) {
262 struct v4l2_output out = {
263 .index = idx
264 };
265 if (!ioctl(fd, VIDIOC_ENUMOUTPUT, &out)) {
266 extra = std::string(" (output: ") + (const char *)out.name + ")";
267 match = !match_io || !strcmp(match_output.c_str(), (const char *)out.name);
268 }
269 }
270 if (!verbose)
271 extra.clear();
272 }
273 close(fd);
274 if (match)
275 printf("%s%s\n", devices[dev].c_str(), extra.c_str());
276 }
277 close(media_fd);
278 }
279
280 static void list_devices(const std::string &input, const std::string &output)
281 {
282 DIR *dp;
283 struct dirent *ep;
284 dev_vec files;
285 dev_map links;
286 dev_map cards;
287 struct v4l2_capability vcap;
288 bool match_io = !input.empty() || !output.empty();
289
290 dp = opendir("/dev");
291 if (dp == nullptr) {
292 perror ("Couldn't open the directory");
293 return;
294 }
295 while ((ep = readdir(dp)))
296 if (is_v4l_dev(ep->d_name))
297 files.push_back(std::string("/dev/") + ep->d_name);
298 closedir(dp);
299
300 /* Find device nodes which are links to other device nodes */
301 for (auto iter = files.begin();
302 iter != files.end(); ) {
303 char link[64+1];
304 int link_len;
305 std::string target;
306
307 link_len = readlink(iter->c_str(), link, 64);
308 if (link_len < 0) { /* Not a link or error */
309 iter++;
310 continue;
311 }
312 link[link_len] = '\0';
313
314 /* Only remove from files list if target itself is in list */
315 if (link[0] != '/') /* Relative link */
316 target = std::string("/dev/");
317 target += link;
318 if (find(files.begin(), files.end(), target) == files.end()) {
319 iter++;
320 continue;
321 }
322
323 /* Move the device node from files to links */
324 if (links[target].empty())
325 links[target] = *iter;
326 else
327 links[target] += ", " + *iter;
328 iter = files.erase(iter);
329 }
330
331 std::sort(files.begin(), files.end(), sort_on_device_name);
332
333 for (const auto &file : files) {
334 int fd = open(file.c_str(), O_RDWR);
335 std::string bus_info;
336 std::string card;
337 std::string extra;
338 bool match = !match_io;
339
340 if (fd < 0)
341 continue;
342 int err = ioctl(fd, VIDIOC_QUERYCAP, &vcap);
343 if (err) {
344 struct media_device_info mdi;
345
346 err = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi);
347 if (!err) {
348 if (mdi.bus_info[0])
349 bus_info = mdi.bus_info;
350 else
351 bus_info = std::string("platform:") + mdi.driver;
352 if (mdi.model[0])
353 card = mdi.model;
354 else
355 card = mdi.driver;
356 }
357 } else {
358 unsigned idx;
359
360 bus_info = reinterpret_cast<const char *>(vcap.bus_info);
361 card = reinterpret_cast<const char *>(vcap.card);
362 if (!ioctl(fd, VIDIOC_G_INPUT, &idx)) {
363 struct v4l2_input in = {
364 .index = idx
365 };
366 if (!ioctl(fd, VIDIOC_ENUMINPUT, &in)) {
367 extra = std::string(" (input: ") + (const char *)in.name + ")";
368 match = !match_io || !strcmp(match_input.c_str(), (const char *)in.name);
369 }
370 } else if (!ioctl(fd, VIDIOC_G_OUTPUT, &idx)) {
371 struct v4l2_output out = {
372 .index = idx
373 };
374 if (!ioctl(fd, VIDIOC_ENUMOUTPUT, &out)) {
375 extra = std::string(" (output: ") + (const char *)out.name + ")";
376 match = !match_io || !strcmp(match_output.c_str(), (const char *)out.name);
377 }
378 }
379 }
380 close(fd);
381 if (err)
382 continue;
383 if (!match) continue;
384 if (cards[bus_info].empty() && !match_io)
385 cards[bus_info] += card + " (" + bus_info + "):\n";
386 if (!match_io)
387 cards[bus_info] += "\t";
388 cards[bus_info] += file;
389 if (!(links[file].empty()))
390 cards[bus_info] += " <- " + links[file];
391 if (verbose)
392 cards[bus_info] += extra;
393 cards[bus_info] += "\n";
394 }
395 bool first = true;
396 for (const auto &card : cards) {
397 if (!first && !match_io)
398 printf("\n");
399 first = false;
400 printf("%s", card.second.c_str());
401 }
402 }
403
404 static std::string name2var(const char *name)
405 {
406 std::string s;
407 int add_underscore = 0;
408
409 while (*name) {
410 if (isalnum(*name)) {
411 if (add_underscore)
412 s += '_';
413 add_underscore = 0;
414 s += std::string(1, tolower(*name));
415 }
416 else if (s.length()) add_underscore = 1;
417 name++;
418 }
419 return s;
420 }
421
422 static std::string safename(const unsigned char *name)
423 {
424 std::string s;
425
426 while (*name) {
427 if (*name == '\n') {
428 s += "\\n";
429 }
430 else if (*name == '\r') {
431 s += "\\r";
432 }
433 else if (*name == '\f') {
434 s += "\\f";
435 }
436 else if (*name == '\\') {
437 s += "\\\\";
438 }
439 else if ((*name & 0x7f) < 0x20) {
440 char buf[3];
441
442 sprintf(buf, "%02x", *name);
443 s += "\\x";
444 s += buf;
445 }
446 else {
447 s += *name;
448 }
449 name++;
450 }
451 return s;
452 }
453
454 static std::string safename(const char *name)
455 {
456 return safename(reinterpret_cast<const unsigned char *>(name));
457 }
458
459 static bool fill_subset(const struct v4l2_query_ext_ctrl &qc, ctrl_subset &subset)
460 {
461 unsigned d;
462
463 if (qc.nr_of_dims == 0)
464 return false;
465
466 for (d = 0; d < qc.nr_of_dims; d++) {
467 subset.offset[d] = 0;
468 subset.size[d] = qc.dims[d];
469 }
470
471 if (qc.flags & V4L2_CTRL_FLAG_DYNAMIC_ARRAY)
472 subset.size[0] = qc.elems;
473
474 std::string s = name2var(qc.name);
475
476 if (ctrl_subsets.find(s) != ctrl_subsets.end()) {
477 unsigned ss_dims;
478
479 subset = ctrl_subsets[s];
480 for (ss_dims = 0; ss_dims < V4L2_CTRL_MAX_DIMS && subset.size[ss_dims]; ss_dims++) ;
481 if (ss_dims != qc.nr_of_dims) {
482 fprintf(stderr, "expected %d dimensions but --subset specified %d\n",
483 qc.nr_of_dims, ss_dims);
484 return true;
485 }
486 for (d = 0; d < qc.nr_of_dims; d++) {
487 if (subset.offset[d] + subset.size[d] > qc.dims[d]) {
488 fprintf(stderr, "the subset offset+size for dimension %d is out of range\n", d);
489 return true;
490 }
491 }
492 }
493 return false;
494 }
495
496 static void print_array(const v4l2_query_ext_ctrl &qc, const v4l2_ext_control &ctrl,
497 const ctrl_subset &subset)
498 {
499 std::string &name = ctrl_id2str[qc.id];
500 unsigned divide[V4L2_CTRL_MAX_DIMS] = { 0 };
501 unsigned from, to;
502 unsigned d, i;
503
504 divide[qc.nr_of_dims - 1] = 1;
505 for (d = 0; d < qc.nr_of_dims - 1; d++) {
506 divide[d] = qc.dims[d + 1];
507 for (i = 0; i < d; i++)
508 divide[i] *= divide[d];
509 }
510
511 from = subset.offset[qc.nr_of_dims - 1];
512 to = subset.offset[qc.nr_of_dims - 1] + subset.size[qc.nr_of_dims - 1] - 1;
513
514 for (unsigned idx = 0; idx < qc.elems; idx += qc.dims[qc.nr_of_dims - 1]) {
515 for (d = 0; d < qc.nr_of_dims - 1; d++) {
516 unsigned i = (idx / divide[d]) % qc.dims[d];
517
518 if (i < subset.offset[d] || i >= subset.offset[d] + subset.size[d])
519 break;
520 }
521 if (d < qc.nr_of_dims - 1)
522 continue;
523
524 printf("%s", name.c_str());
525 for (d = 0; d < qc.nr_of_dims - 1; d++)
526 printf("[%u]", (idx / divide[d]) % qc.dims[d]);
527 printf(": ");
528 switch (qc.type) {
529 case V4L2_CTRL_TYPE_U8:
530 for (i = from; i <= to; i++) {
531 printf("%4u", ctrl.p_u8[idx + i]);
532 if (i < to)
533 printf(", ");
534 }
535 printf("\n");
536 break;
537 case V4L2_CTRL_TYPE_U16:
538 for (i = from; i <= to; i++) {
539 printf("%6u", ctrl.p_u16[idx + i]);
540 if (i < to)
541 printf(", ");
542 }
543 printf("\n");
544 break;
545 case V4L2_CTRL_TYPE_U32:
546 for (i = from; i <= to; i++) {
547 printf("%10u", ctrl.p_u32[idx + i]);
548 if (i < to)
549 printf(", ");
550 }
551 printf("\n");
552 break;
553 case V4L2_CTRL_TYPE_INTEGER:
554 for (i = from; i <= to; i++) {
555 printf("%10i", ctrl.p_s32[idx + i]);
556 if (i < to)
557 printf(", ");
558 }
559 printf("\n");
560 break;
561 case V4L2_CTRL_TYPE_INTEGER64:
562 for (i = from; i <= to; i++) {
563 printf("%12lli", ctrl.p_s64[idx + i]);
564 if (i < to)
565 printf(", ");
566 }
567 printf("\n");
568 break;
569 default:
570 fprintf(stderr, "%s: unsupported array type\n",
571 qc.name);
572 break;
573 }
574 }
575 }
576
577 static void print_value(int fd, const v4l2_query_ext_ctrl &qc, const v4l2_ext_control &ctrl,
578 bool show_payload, bool fill_in_subset)
579 {
580 v4l2_querymenu qmenu;
581
582 if (qc.nr_of_dims) {
583 if (!show_payload)
584 return;
585 ctrl_subset subset;
586 if (fill_in_subset) {
587 if (fill_subset(qc, subset))
588 return;
589 } else {
590 memset(&subset, 0, sizeof(subset));
591 for (unsigned i = 0; i < qc.nr_of_dims; i++)
592 subset.size[i] = qc.dims[i];
593 if (qc.flags & V4L2_CTRL_FLAG_DYNAMIC_ARRAY)
594 subset.size[0] = qc.elems;
595 }
596 print_array(qc, ctrl, subset);
597 return;
598 }
599
600 if (qc.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD) {
601 switch (qc.type) {
602 case V4L2_CTRL_TYPE_U8:
603 printf("%u", *ctrl.p_u8);
604 break;
605 case V4L2_CTRL_TYPE_U16:
606 printf("%u", *ctrl.p_u16);
607 break;
608 case V4L2_CTRL_TYPE_U32:
609 printf("%u", *ctrl.p_u32);
610 break;
611 case V4L2_CTRL_TYPE_STRING:
612 printf("'%s'", safename(ctrl.string).c_str());
613 break;
614 case V4L2_CTRL_TYPE_AREA:
615 printf("%ux%u", ctrl.p_area->width, ctrl.p_area->height);
616 break;
617 case V4L2_CTRL_TYPE_RECT:
618 printf("(%d,%d)/%ux%u",
619 ctrl.p_rect->left, ctrl.p_rect->top,
620 ctrl.p_rect->width, ctrl.p_rect->height);
621 break;
622 default:
623 printf("unsupported payload type");
624 break;
625 }
626 return;
627 }
628 switch (qc.type) {
629 case V4L2_CTRL_TYPE_INTEGER64:
630 printf("%lld", ctrl.value64);
631 break;
632 case V4L2_CTRL_TYPE_MENU:
633 case V4L2_CTRL_TYPE_INTEGER_MENU:
634 memset(&qmenu, 0, sizeof(qmenu));
635 qmenu.id = ctrl.id;
636 qmenu.index = ctrl.value;
637 if (test_ioctl(fd, VIDIOC_QUERYMENU, &qmenu))
638 printf("%d", ctrl.value);
639 else if (qc.type == V4L2_CTRL_TYPE_MENU)
640 printf("%d (%s)", ctrl.value, qmenu.name);
641 else
642 printf("%d (%lld 0x%llx)", ctrl.value, qmenu.value, qmenu.value);
643 break;
644 case V4L2_CTRL_TYPE_BITMASK:
645 printf("0x%08x", ctrl.value);
646 break;
647 default:
648 printf("%d", ctrl.value);
649 break;
650 }
651 }
652
653 static void print_qctrl(int fd, const v4l2_query_ext_ctrl &qc,
654 const v4l2_ext_control &ctrl, bool show_menus)
655 {
656 std::string s = name2var(qc.name);
657 unsigned i;
658
659 switch (qc.type) {
660 case V4L2_CTRL_TYPE_INTEGER:
661 printf("%31s %#8.8x (int) : min=%lld max=%lld step=%lld default=%lld",
662 s.c_str(), qc.id, qc.minimum, qc.maximum,
663 qc.step, qc.default_value);
664 break;
665 case V4L2_CTRL_TYPE_INTEGER64:
666 printf("%31s %#8.8x (int64) : min=%lld max=%lld step=%lld default=%lld",
667 s.c_str(), qc.id, qc.minimum, qc.maximum,
668 qc.step, qc.default_value);
669 break;
670 case V4L2_CTRL_TYPE_STRING:
671 printf("%31s %#8.8x (str) : min=%lld max=%lld step=%lld",
672 s.c_str(), qc.id, qc.minimum, qc.maximum, qc.step);
673 break;
674 case V4L2_CTRL_TYPE_BOOLEAN:
675 printf("%31s %#8.8x (bool) : default=%lld",
676 s.c_str(), qc.id, qc.default_value);
677 break;
678 case V4L2_CTRL_TYPE_MENU:
679 printf("%31s %#8.8x (menu) : min=%lld max=%lld default=%lld",
680 s.c_str(), qc.id, qc.minimum, qc.maximum,
681 qc.default_value);
682 break;
683 case V4L2_CTRL_TYPE_INTEGER_MENU:
684 printf("%31s %#8.8x (intmenu): min=%lld max=%lld default=%lld",
685 s.c_str(), qc.id, qc.minimum, qc.maximum,
686 qc.default_value);
687 break;
688 case V4L2_CTRL_TYPE_BUTTON:
689 printf("%31s %#8.8x (button) :", s.c_str(), qc.id);
690 break;
691 case V4L2_CTRL_TYPE_BITMASK:
692 printf("%31s %#8.8x (bitmask): max=0x%08llx default=0x%08llx",
693 s.c_str(), qc.id, qc.maximum, qc.default_value);
694 break;
695 case V4L2_CTRL_TYPE_U8:
696 printf("%31s %#8.8x (u8) : min=%lld max=%lld step=%lld default=%lld",
697 s.c_str(), qc.id, qc.minimum, qc.maximum,
698 qc.step, qc.default_value);
699 break;
700 case V4L2_CTRL_TYPE_U16:
701 printf("%31s %#8.8x (u16) : min=%lld max=%lld step=%lld default=%lld",
702 s.c_str(), qc.id, qc.minimum, qc.maximum,
703 qc.step, qc.default_value);
704 break;
705 case V4L2_CTRL_TYPE_U32:
706 printf("%31s %#8.8x (u32) : min=%lld max=%lld step=%lld default=%lld",
707 s.c_str(), qc.id, qc.minimum, qc.maximum,
708 qc.step, qc.default_value);
709 break;
710 case V4L2_CTRL_TYPE_AREA:
711 printf("%31s %#8.8x (area) :", s.c_str(), qc.id);
712 break;
713 case V4L2_CTRL_TYPE_RECT:
714 printf("%31s %#8.8x (rect) :", s.c_str(), qc.id);
715 break;
716 case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
717 printf("%31s %#8.8x (hdr10-cll-info):", s.c_str(), qc.id);
718 break;
719 case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
720 printf("%31s %#8.8x (hdr10-mastering-display):", s.c_str(), qc.id);
721 break;
722 case V4L2_CTRL_TYPE_H264_SPS:
723 printf("%31s %#8.8x (h264-sps):", s.c_str(), qc.id);
724 break;
725 case V4L2_CTRL_TYPE_H264_PPS:
726 printf("%31s %#8.8x (h264-pps):", s.c_str(), qc.id);
727 break;
728 case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
729 printf("%31s %#8.8x (h264-scaling-matrix):", s.c_str(), qc.id);
730 break;
731 case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
732 printf("%31s %#8.8x (h264-slice-params):", s.c_str(), qc.id);
733 break;
734 case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
735 printf("%31s %#8.8x (h264-decode-params):", s.c_str(), qc.id);
736 break;
737 case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
738 printf("%31s %#8.8x (h264-pred-weights):", s.c_str(), qc.id);
739 break;
740 case V4L2_CTRL_TYPE_HEVC_SPS:
741 printf("%31s %#8.8x (hevc-sps):", s.c_str(), qc.id);
742 break;
743 case V4L2_CTRL_TYPE_HEVC_PPS:
744 printf("%31s %#8.8x (hevc-sps):", s.c_str(), qc.id);
745 break;
746 case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
747 printf("%31s %#8.8x (hevc-slice-params):", s.c_str(), qc.id);
748 break;
749 case V4L2_CTRL_TYPE_HEVC_SCALING_MATRIX:
750 printf("%31s %#8.8x (hevc-scaling-matrix):", s.c_str(), qc.id);
751 break;
752 case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS:
753 printf("%31s %#8.8x (hevc-decode-params):", s.c_str(), qc.id);
754 break;
755 case V4L2_CTRL_TYPE_VP8_FRAME:
756 printf("%31s %#8.8x (vp8-frame):", s.c_str(), qc.id);
757 break;
758 case V4L2_CTRL_TYPE_VP9_FRAME:
759 printf("%31s %#8.8x (vp9-frame):", s.c_str(), qc.id);
760 break;
761 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION:
762 printf("%31s %#8.8x (mpeg2-quantisation):", s.c_str(), qc.id);
763 break;
764 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE:
765 printf("%31s %#8.8x (mpeg2-sequence):", s.c_str(), qc.id);
766 break;
767 case V4L2_CTRL_TYPE_MPEG2_PICTURE:
768 printf("%31s %#8.8x (mpeg2-picture):", s.c_str(), qc.id);
769 break;
770 case V4L2_CTRL_TYPE_FWHT_PARAMS:
771 printf("%31s %#8.8x (fwht-params):", s.c_str(), qc.id);
772 break;
773 case V4L2_CTRL_TYPE_AV1_SEQUENCE:
774 printf("%31s %#8.8x (av1-sequence):", s.c_str(), qc.id);
775 break;
776 case V4L2_CTRL_TYPE_AV1_TILE_GROUP_ENTRY:
777 printf("%31s %#8.8x (av1-tile-group-entry):", s.c_str(), qc.id);
778 break;
779 case V4L2_CTRL_TYPE_AV1_FRAME:
780 printf("%31s %#8.8x (av1-frame):", s.c_str(), qc.id);
781 break;
782 case V4L2_CTRL_TYPE_AV1_FILM_GRAIN:
783 printf("%31s %#8.8x (av1-film-grain):", s.c_str(), qc.id);
784 break;
785 default:
786 printf("%31s %#8.8x (unknown): type=%x",
787 s.c_str(), qc.id, qc.type);
788 break;
789 }
790 if (qc.nr_of_dims == 0) {
791 printf(" value=");
792 print_value(fd, qc, ctrl, false, false);
793 } else {
794 if (qc.flags & V4L2_CTRL_FLAG_DYNAMIC_ARRAY)
795 printf(" elems=%u", qc.elems);
796 printf(" dims=");
797 for (i = 0; i < qc.nr_of_dims; i++)
798 printf("[%u]", qc.dims[i]);
799 }
800 if (qc.flags)
801 printf(" flags=%s", ctrlflags2s(qc.flags).c_str());
802 printf("\n");
803 if ((qc.type == V4L2_CTRL_TYPE_MENU ||
804 qc.type == V4L2_CTRL_TYPE_INTEGER_MENU) && show_menus) {
805 v4l2_querymenu qmenu;
806
807 memset(&qmenu, 0, sizeof(qmenu));
808 qmenu.id = ctrl.id;
809 for (i = qc.minimum; i <= qc.maximum; i++) {
810 qmenu.index = i;
811 if (test_ioctl(fd, VIDIOC_QUERYMENU, &qmenu))
812 continue;
813 if (qc.type == V4L2_CTRL_TYPE_MENU)
814 printf("\t\t\t\t%d: %s\n", i, qmenu.name);
815 else
816 printf("\t\t\t\t%d: %lld (0x%llx)\n", i, qmenu.value, qmenu.value);
817 }
818 }
819 }
820
821 static int print_control(int fd, struct v4l2_query_ext_ctrl &qctrl, bool show_menus)
822 {
823 struct v4l2_control ctrl;
824 struct v4l2_ext_control ext_ctrl;
825 struct v4l2_ext_controls ctrls;
826
827 memset(&ctrl, 0, sizeof(ctrl));
828 memset(&ext_ctrl, 0, sizeof(ext_ctrl));
829 memset(&ctrls, 0, sizeof(ctrls));
830 if (qctrl.flags & V4L2_CTRL_FLAG_DISABLED)
831 return 1;
832 if (qctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS) {
833 printf("\n%s\n\n", qctrl.name);
834 return 1;
835 }
836 ext_ctrl.id = qctrl.id;
837 if ((qctrl.flags & V4L2_CTRL_FLAG_WRITE_ONLY) ||
838 qctrl.type == V4L2_CTRL_TYPE_BUTTON) {
839 print_qctrl(fd, qctrl, ext_ctrl, show_menus);
840 return 1;
841 }
842 ctrls.which = V4L2_CTRL_ID2WHICH(qctrl.id);
843 ctrls.count = 1;
844 ctrls.controls = &ext_ctrl;
845 if (qctrl.type == V4L2_CTRL_TYPE_INTEGER64 ||
846 qctrl.type == V4L2_CTRL_TYPE_STRING ||
847 qctrl.nr_of_dims ||
848 qctrl.type >= V4L2_CTRL_COMPOUND_TYPES ||
849 (V4L2_CTRL_ID2WHICH(qctrl.id) != V4L2_CTRL_CLASS_USER &&
850 qctrl.id < V4L2_CID_PRIVATE_BASE)) {
851 if (qctrl.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD) {
852 ext_ctrl.size = qctrl.elems * qctrl.elem_size;
853 ext_ctrl.ptr = calloc(1, ext_ctrl.size);
854 }
855 if (test_ioctl(fd, VIDIOC_G_EXT_CTRLS, &ctrls)) {
856 printf("error %d getting ext_ctrl %s\n",
857 errno, qctrl.name);
858 return 0;
859 }
860 }
861 else {
862 ctrl.id = qctrl.id;
863 if (test_ioctl(fd, VIDIOC_G_CTRL, &ctrl)) {
864 printf("error %d getting ctrl %s\n",
865 errno, qctrl.name);
866 return 0;
867 }
868 ext_ctrl.value = ctrl.value;
869 }
870 print_qctrl(fd, qctrl, ext_ctrl, show_menus);
871 if (qctrl.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD)
872 free(ext_ctrl.ptr);
873 return 1;
874 }
875
876 static int query_ext_ctrl_ioctl(int fd, struct v4l2_query_ext_ctrl &qctrl)
877 {
878 struct v4l2_queryctrl qc;
879 int rc;
880
881 if (have_query_ext_ctrl) {
882 rc = test_ioctl(fd, VIDIOC_QUERY_EXT_CTRL, &qctrl);
883 if (rc != ENOTTY)
884 return rc;
885 }
886 qc.id = qctrl.id;
887 rc = test_ioctl(fd, VIDIOC_QUERYCTRL, &qc);
888 if (rc == 0) {
889 qctrl.type = qc.type;
890 memcpy(qctrl.name, qc.name, sizeof(qctrl.name));
891 qctrl.minimum = qc.minimum;
892 if (qc.type == V4L2_CTRL_TYPE_BITMASK) {
893 qctrl.maximum = static_cast<__u32>(qc.maximum);
894 qctrl.default_value = static_cast<__u32>(qc.default_value);
895 } else {
896 qctrl.maximum = qc.maximum;
897 qctrl.default_value = qc.default_value;
898 }
899 qctrl.step = qc.step;
900 qctrl.flags = qc.flags;
901 qctrl.elems = 1;
902 qctrl.nr_of_dims = 0;
903 memset(qctrl.dims, 0, sizeof(qctrl.dims));
904 switch (qctrl.type) {
905 case V4L2_CTRL_TYPE_INTEGER64:
906 qctrl.elem_size = sizeof(__s64);
907 break;
908 case V4L2_CTRL_TYPE_STRING:
909 qctrl.elem_size = qc.maximum + 1;
910 qctrl.flags |= V4L2_CTRL_FLAG_HAS_PAYLOAD;
911 break;
912 default:
913 qctrl.elem_size = sizeof(__s32);
914 break;
915 }
916 memset(qctrl.reserved, 0, sizeof(qctrl.reserved));
917 }
918 qctrl.id = qc.id;
919 return rc;
920 }
921
922 static void list_controls(int fd, bool show_menus)
923 {
924 const unsigned next_fl = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
925 struct v4l2_query_ext_ctrl qctrl;
926 int id;
927
928 memset(&qctrl, 0, sizeof(qctrl));
929 qctrl.id = next_fl;
930 while (query_ext_ctrl_ioctl(fd, qctrl) == 0) {
931 print_control(fd, qctrl, show_menus);
932 qctrl.id |= next_fl;
933 }
934 if (qctrl.id != next_fl)
935 return;
936 for (id = V4L2_CID_USER_BASE; id < V4L2_CID_LASTP1; id++) {
937 qctrl.id = id;
938 if (query_ext_ctrl_ioctl(fd, qctrl) == 0)
939 print_control(fd, qctrl, show_menus);
940 }
941 for (qctrl.id = V4L2_CID_PRIVATE_BASE;
942 query_ext_ctrl_ioctl(fd, qctrl) == 0; qctrl.id++) {
943 print_control(fd, qctrl, show_menus);
944 }
945 }
946
947 static void find_controls(cv4l_fd &_fd)
948 {
949 const unsigned next_fl = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
950 struct v4l2_query_ext_ctrl qctrl;
951 int fd = _fd.g_fd();
952 int id;
953
954 memset(&qctrl, 0, sizeof(qctrl));
955 qctrl.id = next_fl;
956 while (query_ext_ctrl_ioctl(fd, qctrl) == 0) {
957 if (qctrl.type != V4L2_CTRL_TYPE_CTRL_CLASS &&
958 !(qctrl.flags & V4L2_CTRL_FLAG_DISABLED)) {
959 ctrl_str2q[name2var(qctrl.name)] = qctrl;
960 ctrl_id2str[qctrl.id] = name2var(qctrl.name);
961 }
962 qctrl.id |= next_fl;
963 }
964 if (qctrl.id != next_fl)
965 return;
966 for (id = V4L2_CID_USER_BASE; id < V4L2_CID_LASTP1; id++) {
967 qctrl.id = id;
968 if (query_ext_ctrl_ioctl(fd, qctrl) == 0 &&
969 !(qctrl.flags & V4L2_CTRL_FLAG_DISABLED)) {
970 ctrl_str2q[name2var(qctrl.name)] = qctrl;
971 ctrl_id2str[qctrl.id] = name2var(qctrl.name);
972 }
973 }
974 for (qctrl.id = V4L2_CID_PRIVATE_BASE;
975 query_ext_ctrl_ioctl(fd, qctrl) == 0; qctrl.id++) {
976 if (!(qctrl.flags & V4L2_CTRL_FLAG_DISABLED)) {
977 ctrl_str2q[name2var(qctrl.name)] = qctrl;
978 ctrl_id2str[qctrl.id] = name2var(qctrl.name);
979 }
980 }
981 }
982
983 int common_find_ctrl_id(const char *name)
984 {
985 if (ctrl_str2q.find(name) == ctrl_str2q.end())
986 return 0;
987 return ctrl_str2q[name].id;
988 }
989
990 void common_process_controls(cv4l_fd &fd)
991 {
992 struct v4l2_query_ext_ctrl qc = {
993 V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND
994 };
995 int rc;
996
997 rc = test_ioctl(fd.g_fd(), VIDIOC_QUERY_EXT_CTRL, &qc);
998 have_query_ext_ctrl = rc == 0;
999
1000 find_controls(fd);
1001 for (const auto &get_ctrl : get_ctrls) {
1002 std::string s = get_ctrl;
1003 if (isdigit(s[0])) {
1004 __u32 id = strtoul(s.c_str(), nullptr, 0);
1005 if (ctrl_id2str.find(id) != ctrl_id2str.end())
1006 s = ctrl_id2str[id];
1007 }
1008 if (ctrl_str2q.find(s) == ctrl_str2q.end()) {
1009 fprintf(stderr, "unknown control '%s'\n", s.c_str());
1010 std::exit(EXIT_FAILURE);
1011 }
1012 }
1013 for (const auto &set_ctrl : set_ctrls) {
1014 std::string s = set_ctrl.first;
1015 if (isdigit(s[0])) {
1016 __u32 id = strtoul(s.c_str(), nullptr, 0);
1017 if (ctrl_id2str.find(id) != ctrl_id2str.end())
1018 s = ctrl_id2str[id];
1019 }
1020 if (ctrl_str2q.find(s) == ctrl_str2q.end()) {
1021 fprintf(stderr, "unknown control '%s'\n", s.c_str());
1022 std::exit(EXIT_FAILURE);
1023 }
1024 }
1025 }
1026
1027 void common_control_event(int fd, const struct v4l2_event *ev)
1028 {
1029 const struct v4l2_event_ctrl *ctrl;
1030
1031 ctrl = &ev->u.ctrl;
1032 printf("ctrl: %s\n", ctrl_id2str[ev->id].c_str());
1033 if (ctrl->changes & V4L2_EVENT_CTRL_CH_VALUE) {
1034 if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
1035 printf("\tvalue: %lld 0x%llx\n", ctrl->value64, ctrl->value64);
1036 else if (ctrl->type == V4L2_CTRL_TYPE_BITMASK)
1037 printf("\tvalue: %u 0x%08x\n", ctrl->value, ctrl->value);
1038 else
1039 printf("\tvalue: %d 0x%x\n", ctrl->value, ctrl->value);
1040 }
1041 if (ctrl->changes & V4L2_EVENT_CTRL_CH_FLAGS)
1042 printf("\tflags: %s\n", ctrlflags2s(ctrl->flags).c_str());
1043 if (ctrl->changes & V4L2_EVENT_CTRL_CH_RANGE) {
1044 if (ctrl->type == V4L2_CTRL_TYPE_BITMASK)
1045 printf("\trange: max=0x%08x default=0x%08x\n",
1046 ctrl->maximum, ctrl->default_value);
1047 else
1048 printf("\trange: min=%d max=%d step=%d default=%d\n",
1049 ctrl->minimum, ctrl->maximum, ctrl->step, ctrl->default_value);
1050 }
1051 if (ctrl->changes & V4L2_EVENT_CTRL_CH_DIMENSIONS) {
1052 v4l2_query_ext_ctrl qctrl = {};
1053
1054 qctrl.id = ev->id;
1055 if (!query_ext_ctrl_ioctl(fd, qctrl)) {
1056 ctrl_str2q[name2var(qctrl.name)] = qctrl;
1057 printf("\tdimensions: ");
1058 for (unsigned i = 0; i < qctrl.nr_of_dims; i++)
1059 printf("[%u]", qctrl.dims[i]);
1060 printf("\n");
1061 }
1062 }
1063 }
1064
1065 static bool parse_subset(char *optarg)
1066 {
1067 struct ctrl_subset subset;
1068 std::string ctrl_name;
1069 unsigned idx = 0;
1070 char *p;
1071
1072 memset(&subset, 0, sizeof(subset));
1073 while (*optarg) {
1074 p = std::strchr(optarg, ',');
1075 if (p)
1076 *p = 0;
1077 if (optarg[0] == 0) {
1078 fprintf(stderr, "empty string\n");
1079 return true;
1080 }
1081 if (idx == 0) {
1082 ctrl_name = optarg;
1083 } else {
1084 if (idx > V4L2_CTRL_MAX_DIMS * 2) {
1085 fprintf(stderr, "too many dimensions\n");
1086 return true;
1087 }
1088 if (idx & 1)
1089 subset.offset[idx / 2] = strtoul(optarg, nullptr, 0);
1090 else {
1091 subset.size[idx / 2 - 1] = strtoul(optarg, nullptr, 0);
1092 if (subset.size[idx / 2 - 1] == 0) {
1093 fprintf(stderr, "<size> cannot be 0\n");
1094 return true;
1095 }
1096 }
1097 }
1098 idx++;
1099 if (p == nullptr)
1100 break;
1101 optarg = p + 1;
1102 }
1103 if (idx == 1) {
1104 fprintf(stderr, "no <offset>, <size> tuples given\n");
1105 return true;
1106 }
1107 if ((idx & 1) == 0) {
1108 fprintf(stderr, "<offset> without <size>\n");
1109 return true;
1110 }
1111 ctrl_subsets[ctrl_name] = subset;
1112
1113 return false;
1114 }
1115
1116 static bool parse_next_subopt(char **subs, char **value)
1117 {
1118 static char *const subopts[] = {
1119 nullptr
1120 };
1121 int opt = v4l_getsubopt(subs, subopts, value);
1122
1123 if (opt < 0 || *value)
1124 return false;
1125 fprintf(stderr, "Missing suboption value\n");
1126 return true;
1127 }
1128
1129 void common_cmd(int ch, char *optarg)
1130 {
1131 char *value, *subs;
1132
1133 switch (ch) {
1134 case OptGetCtrl:
1135 subs = optarg;
1136 while (*subs != '\0') {
1137 if (parse_next_subopt(&subs, &value)) {
1138 common_usage();
1139 std::exit(EXIT_FAILURE);
1140 }
1141 if (std::strchr(value, '=')) {
1142 common_usage();
1143 std::exit(EXIT_FAILURE);
1144 }
1145 else {
1146 get_ctrls.push_back(value);
1147 }
1148 }
1149 break;
1150 case OptSetCtrl:
1151 subs = optarg;
1152 while (*subs != '\0') {
1153 if (parse_next_subopt(&subs, &value)) {
1154 common_usage();
1155 std::exit(EXIT_FAILURE);
1156 }
1157 if (const char *equal = std::strchr(value, '=')) {
1158 set_ctrls.emplace_back(std::string(value, (equal - value)),
1159 std::string(equal + 1));
1160 }
1161 else {
1162 fprintf(stderr, "control '%s' without '='\n", value);
1163 std::exit(EXIT_FAILURE);
1164 }
1165 }
1166 break;
1167 case OptSubset:
1168 if (parse_subset(optarg)) {
1169 common_usage();
1170 std::exit(EXIT_FAILURE);
1171 }
1172 break;
1173 case OptSetPriority:
1174 prio = static_cast<enum v4l2_priority>(strtoul(optarg, nullptr, 0));
1175 break;
1176 case OptListDevicesInput:
1177 match_input = optarg;
1178 break;
1179 case OptListDevicesOutput:
1180 match_output = optarg;
1181 break;
1182 }
1183 }
1184
1185 static bool idx_in_subset(const struct v4l2_query_ext_ctrl &qc, const ctrl_subset &subset,
1186 const unsigned *divide, unsigned idx)
1187 {
1188 for (unsigned d = 0; d < qc.nr_of_dims; d++) {
1189 unsigned i = (idx / divide[d]) % qc.dims[d];
1190
1191 if (i < subset.offset[d] || i >= subset.offset[d] + subset.size[d])
1192 return false;
1193 }
1194 return true;
1195 }
1196
1197 void common_set(cv4l_fd &_fd)
1198 {
1199 int fd = _fd.g_fd();
1200
1201 if (options[OptSetPriority]) {
1202 if (doioctl(fd, VIDIOC_S_PRIORITY, &prio) >= 0) {
1203 printf("Priority set: %d\n", prio);
1204 }
1205 }
1206
1207 if (options[OptSetCtrl] && !set_ctrls.empty()) {
1208 struct v4l2_ext_controls ctrls;
1209 class2ctrls_map class2ctrls;
1210 bool use_ext_ctrls = false;
1211
1212 memset(&ctrls, 0, sizeof(ctrls));
1213 for (const auto &set_ctrl : set_ctrls) {
1214 std::string s = set_ctrl.first;
1215 if (isdigit(s[0])) {
1216 __u32 id = strtoul(s.c_str(), nullptr, 0);
1217 s = ctrl_id2str[id];
1218 }
1219 struct v4l2_ext_control ctrl;
1220 struct v4l2_query_ext_ctrl &qc = ctrl_str2q[s];
1221
1222 memset(&ctrl, 0, sizeof(ctrl));
1223 ctrl.id = qc.id;
1224 if (qc.type == V4L2_CTRL_TYPE_INTEGER64 ||
1225 qc.flags & V4L2_CTRL_FLAG_UPDATE)
1226 use_ext_ctrls = true;
1227 if (qc.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD) {
1228 struct v4l2_ext_controls ctrls = { { 0 }, 1 };
1229 unsigned divide[V4L2_CTRL_MAX_DIMS] = { 0 };
1230 ctrl_subset subset;
1231 long long v;
1232 unsigned d, i;
1233
1234 use_ext_ctrls = true;
1235 ctrl.size = qc.elems * qc.elem_size;
1236 ctrl.ptr = malloc(ctrl.size);
1237
1238 ctrls.controls = &ctrl;
1239 ioctl(fd, VIDIOC_G_EXT_CTRLS, &ctrls);
1240
1241 if (fill_subset(qc, subset))
1242 return;
1243
1244 if (qc.nr_of_dims) {
1245 divide[qc.nr_of_dims - 1] = 1;
1246 for (d = 0; d < qc.nr_of_dims - 1; d++) {
1247 divide[d] = qc.dims[d + 1];
1248 for (i = 0; i < d; i++)
1249 divide[i] *= divide[d];
1250 }
1251 }
1252
1253
1254 switch (qc.type) {
1255 case V4L2_CTRL_TYPE_U8:
1256 v = strtoul(set_ctrl.second.c_str(), nullptr, 0);
1257 for (i = 0; i < qc.elems; i++)
1258 if (idx_in_subset(qc, subset, divide, i))
1259 ctrl.p_u8[i] = v;
1260 break;
1261 case V4L2_CTRL_TYPE_U16:
1262 v = strtoul(set_ctrl.second.c_str(), nullptr, 0);
1263 for (i = 0; i < qc.elems; i++)
1264 if (idx_in_subset(qc, subset, divide, i))
1265 ctrl.p_u16[i] = v;
1266 break;
1267 case V4L2_CTRL_TYPE_U32:
1268 v = strtoul(set_ctrl.second.c_str(), nullptr, 0);
1269 for (i = 0; i < qc.elems; i++)
1270 if (idx_in_subset(qc, subset, divide, i))
1271 ctrl.p_u32[i] = v;
1272 break;
1273 case V4L2_CTRL_TYPE_INTEGER:
1274 v = strtol(set_ctrl.second.c_str(), nullptr, 0);
1275 for (i = 0; i < qc.elems; i++)
1276 if (idx_in_subset(qc, subset, divide, i))
1277 ctrl.p_s32[i] = v;
1278 break;
1279 case V4L2_CTRL_TYPE_INTEGER64:
1280 v = strtol(set_ctrl.second.c_str(), nullptr, 0);
1281 for (i = 0; i < qc.elems; i++)
1282 if (idx_in_subset(qc, subset, divide, i))
1283 ctrl.p_s64[i] = v;
1284 break;
1285 case V4L2_CTRL_TYPE_STRING:
1286 strncpy(ctrl.string, set_ctrl.second.c_str(), qc.maximum);
1287 ctrl.string[qc.maximum] = 0;
1288 break;
1289 case V4L2_CTRL_TYPE_AREA:
1290 sscanf(set_ctrl.second.c_str(), "%ux%u",
1291 &ctrl.p_area->width, &ctrl.p_area->height);
1292 break;
1293 case V4L2_CTRL_TYPE_RECT:
1294 sscanf(set_ctrl.second.c_str(), "(%d,%d)/%ux%u",
1295 &ctrl.p_rect->left, &ctrl.p_rect->top,
1296 &ctrl.p_rect->width, &ctrl.p_rect->height);
1297 break;
1298 default:
1299 fprintf(stderr, "%s: unsupported payload type\n",
1300 qc.name);
1301 break;
1302 }
1303 } else {
1304 if (V4L2_CTRL_DRIVER_PRIV(ctrl.id))
1305 use_ext_ctrls = true;
1306 if (qc.type == V4L2_CTRL_TYPE_INTEGER64)
1307 ctrl.value64 = strtoll(set_ctrl.second.c_str(), nullptr, 0);
1308 else
1309 ctrl.value = strtol(set_ctrl.second.c_str(), nullptr, 0);
1310 }
1311 class2ctrls[V4L2_CTRL_ID2WHICH(ctrl.id)].push_back(ctrl);
1312 }
1313 for (auto &class2ctrl : class2ctrls) {
1314 if (!use_ext_ctrls &&
1315 (class2ctrl.first == V4L2_CTRL_CLASS_USER ||
1316 class2ctrl.first == V4L2_CID_PRIVATE_BASE)) {
1317 for (const auto &i : class2ctrl.second) {
1318 struct v4l2_control ctrl;
1319
1320 ctrl.id = i.id;
1321 ctrl.value = i.value;
1322 if (doioctl(fd, VIDIOC_S_CTRL, &ctrl)) {
1323 fprintf(stderr, "%s: %s\n",
1324 ctrl_id2str[ctrl.id].c_str(),
1325 strerror(errno));
1326 }
1327 }
1328 continue;
1329 }
1330 if (!class2ctrl.second.empty()) {
1331 ctrls.which = class2ctrl.first;
1332 ctrls.count = class2ctrl.second.size();
1333 ctrls.controls = &class2ctrl.second[0];
1334 if (doioctl(fd, VIDIOC_S_EXT_CTRLS, &ctrls)) {
1335 if (ctrls.error_idx >= ctrls.count) {
1336 fprintf(stderr, "Error setting controls: %s\n",
1337 strerror(errno));
1338 }
1339 else {
1340 fprintf(stderr, "%s: %s\n",
1341 ctrl_id2str[class2ctrl.second[ctrls.error_idx].id].c_str(),
1342 strerror(errno));
1343 }
1344 }
1345 }
1346 }
1347 }
1348 }
1349
1350 void common_get(cv4l_fd &_fd)
1351 {
1352 int fd = _fd.g_fd();
1353
1354 if (options[OptGetCtrl] && !get_ctrls.empty()) {
1355 struct v4l2_ext_controls ctrls;
1356 class2ctrls_map class2ctrls;
1357 bool use_ext_ctrls = false;
1358
1359 memset(&ctrls, 0, sizeof(ctrls));
1360 for (const auto &get_ctrl : get_ctrls) {
1361 std::string s = get_ctrl;
1362 if (isdigit(s[0])) {
1363 __u32 id = strtoul(s.c_str(), nullptr, 0);
1364 s = ctrl_id2str[id];
1365 }
1366 struct v4l2_ext_control ctrl;
1367 struct v4l2_query_ext_ctrl &qc = ctrl_str2q[s];
1368
1369 memset(&ctrl, 0, sizeof(ctrl));
1370 ctrl.id = qc.id;
1371 if (qc.type == V4L2_CTRL_TYPE_INTEGER64 ||
1372 qc.flags & V4L2_CTRL_FLAG_UPDATE)
1373 use_ext_ctrls = true;
1374 if (qc.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD) {
1375 use_ext_ctrls = true;
1376 ctrl.size = qc.elems * qc.elem_size;
1377 ctrl.ptr = calloc(1, ctrl.size);
1378 }
1379 if (V4L2_CTRL_DRIVER_PRIV(ctrl.id))
1380 use_ext_ctrls = true;
1381 class2ctrls[V4L2_CTRL_ID2WHICH(ctrl.id)].push_back(ctrl);
1382 }
1383 for (auto &class2ctrl : class2ctrls) {
1384 if (!use_ext_ctrls &&
1385 (class2ctrl.first == V4L2_CTRL_CLASS_USER ||
1386 class2ctrl.first == V4L2_CID_PRIVATE_BASE)) {
1387 for (const auto &i : class2ctrl.second) {
1388 struct v4l2_control ctrl;
1389
1390 ctrl.id = i.id;
1391 doioctl(fd, VIDIOC_G_CTRL, &ctrl);
1392 printf("%s: %d\n", ctrl_id2str[ctrl.id].c_str(), ctrl.value);
1393 }
1394 continue;
1395 }
1396 if (!class2ctrl.second.empty()) {
1397 ctrls.which = class2ctrl.first;
1398 ctrls.count = class2ctrl.second.size();
1399 ctrls.controls = &class2ctrl.second[0];
1400 doioctl(fd, VIDIOC_G_EXT_CTRLS, &ctrls);
1401 for (auto ctrl : class2ctrl.second) {
1402 std::string &name = ctrl_id2str[ctrl.id];
1403 struct v4l2_query_ext_ctrl &qc = ctrl_str2q[name];
1404
1405 if (qc.nr_of_dims) {
1406 print_value(fd, qc, ctrl, true, true);
1407 continue;
1408 }
1409
1410 printf("%s: ", name.c_str());
1411 print_value(fd, qc, ctrl, true, false);
1412 printf("\n");
1413 }
1414 }
1415 }
1416 }
1417
1418 if (options[OptGetPriority]) {
1419 if (doioctl(fd, VIDIOC_G_PRIORITY, &prio) == 0)
1420 printf("Priority: %d\n", prio);
1421 }
1422
1423 if (options[OptLogStatus]) {
1424 static char buf[40960];
1425 int len = -1;
1426
1427 if (doioctl(fd, VIDIOC_LOG_STATUS, nullptr) == 0) {
1428 printf("\nStatus Log:\n\n");
1429 #ifdef HAVE_KLOGCTL
1430 len = klogctl(3, buf, sizeof(buf) - 1);
1431 #endif
1432 if (len >= 0) {
1433 char *p = buf;
1434 char *q;
1435
1436 buf[len] = 0;
1437 while ((q = strstr(p, "START STATUS"))) {
1438 p = q + 1;
1439 }
1440 if (p) {
1441 while (p > buf && *p != '<') p--;
1442 q = p;
1443 while ((q = strstr(q, "<6>"))) {
1444 memcpy(q, " ", 3);
1445 }
1446 printf("%s", p);
1447 }
1448 }
1449 }
1450 }
1451 }
1452
1453 bool common_list_devices(const std::string &media_bus_info, cv4l_fd &fd)
1454 {
1455 if (options[OptListDevices] || options[OptListDevicesInput] ||
1456 options[OptListDevicesOutput]) {
1457 if (media_bus_info.empty())
1458 list_devices(match_input, match_output);
1459 else
1460 list_media_devices(media_bus_info,match_input, match_output);
1461 return true;
1462 }
1463 return false;
1464 }
1465
1466 void common_list(cv4l_fd &fd)
1467 {
1468 if (options[OptListCtrls] || options[OptListCtrlsMenus]) {
1469 list_controls(fd.g_fd(), options[OptListCtrlsMenus]);
1470 }
1471 }