| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <fcntl.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <string.h> |
| 8 |
#include <unistd.h> |
| 9 |
#include <sys/ioctl.h> |
| 10 |
#include "v4l2_config.h" |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
int v4l2_cfg_query_dv_timings(int dev_fd, |
| 16 |
struct v4l2_dv_timings *dv_timings) |
| 17 |
{ |
| 18 |
int ret; |
| 19 |
|
| 20 |
ret = ioctl(dev_fd, VIDIOC_QUERY_DV_TIMINGS, dv_timings); |
| 21 |
if (ret == -1) |
| 22 |
return ret; |
| 23 |
|
| 24 |
#ifdef INFO |
| 25 |
printf("detected resolution: %dx%d\n", |
| 26 |
dv_timings->bt.width, dv_timings->bt.height); |
| 27 |
printf("detected pixel clock: %d.%d MHz\n", |
| 28 |
(int)(dv_timings->bt.pixelclock / 1000000), |
| 29 |
(int)((dv_timings->bt.pixelclock % 1000000) / 1000)); |
| 30 |
#endif |
| 31 |
|
| 32 |
return 0; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
int v4l2_cfg_get_dv_timings(int dev_fd, |
| 39 |
struct v4l2_dv_timings *dv_timings) |
| 40 |
{ |
| 41 |
int ret; |
| 42 |
|
| 43 |
ret = ioctl(dev_fd, VIDIOC_G_DV_TIMINGS, dv_timings); |
| 44 |
if (ret == -1) |
| 45 |
return ret; |
| 46 |
|
| 47 |
#ifdef INFO |
| 48 |
printf("configured resolution: %dx%d\n", |
| 49 |
dv_timings->bt.width, dv_timings->bt.height); |
| 50 |
printf("configured pixel clock: %d.%d MHz\n", |
| 51 |
(int)(dv_timings->bt.pixelclock / 1000000), |
| 52 |
(int)((dv_timings->bt.pixelclock % 1000000) / 1000)); |
| 53 |
#endif |
| 54 |
|
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
int v4l2_cfg_set_dv_timings(int dev_fd, |
| 62 |
struct v4l2_dv_timings *dv_timings) |
| 63 |
{ |
| 64 |
int ret; |
| 65 |
|
| 66 |
ret = ioctl(dev_fd, VIDIOC_S_DV_TIMINGS, dv_timings); |
| 67 |
if (ret == -1) |
| 68 |
return ret; |
| 69 |
|
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
int v4l2_cfg_query_ctrls(int dev_fd, |
| 77 |
struct v4l2_queryctrl *ctrls, |
| 78 |
int *ctrls_no) |
| 79 |
{ |
| 80 |
int ret; |
| 81 |
|
| 82 |
*ctrls_no = 0; |
| 83 |
ctrls[*ctrls_no].id = V4L2_CTRL_FLAG_NEXT_CTRL; |
| 84 |
while(1) { |
| 85 |
ret = ioctl(dev_fd, VIDIOC_QUERYCTRL, &ctrls[*ctrls_no]); |
| 86 |
if (ret == -1) |
| 87 |
break; |
| 88 |
ctrls[*ctrls_no + 1].id = |
| 89 |
(ctrls[*ctrls_no].id | V4L2_CTRL_FLAG_NEXT_CTRL); |
| 90 |
*ctrls_no += 1; |
| 91 |
} |
| 92 |
|
| 93 |
if (*ctrls_no == 0) |
| 94 |
return -1; |
| 95 |
else |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
int v4l2_cfg_subscribe_event(int dev_fd, |
| 103 |
unsigned int event_type, |
| 104 |
unsigned int event_id) |
| 105 |
{ |
| 106 |
struct v4l2_event_subscription sub; |
| 107 |
int ret; |
| 108 |
memset(&sub, 0, sizeof(sub)); |
| 109 |
sub.type = event_type; |
| 110 |
sub.id = event_id; |
| 111 |
if (event_type == V4L2_EVENT_CTRL) |
| 112 |
sub.flags = V4L2_EVENT_SUB_FL_SEND_INITIAL; |
| 113 |
|
| 114 |
ret = ioctl(dev_fd, VIDIOC_SUBSCRIBE_EVENT, &sub); |
| 115 |
if (ret == -1) |
| 116 |
return -1; |
| 117 |
|
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
int v4l2_cfg_unsubscribe_event(int dev_fd, |
| 125 |
unsigned int event_type, |
| 126 |
unsigned int event_id) |
| 127 |
{ |
| 128 |
struct v4l2_event_subscription sub; |
| 129 |
int ret; |
| 130 |
memset(&sub, 0, sizeof(sub)); |
| 131 |
sub.type = event_type; |
| 132 |
sub.id = event_id; |
| 133 |
|
| 134 |
ret = ioctl(dev_fd, VIDIOC_UNSUBSCRIBE_EVENT, &sub); |
| 135 |
if (ret == -1) |
| 136 |
return -1; |
| 137 |
|
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
int v4l2_cfg_dequeue_event(int dev_fd, |
| 145 |
struct v4l2_event_new *dqevent) |
| 146 |
{ |
| 147 |
return ioctl(dev_fd, VIDIOC_DQEVENT, dqevent); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
int v4l2_cfg_get_control_value(int dev_fd, |
| 154 |
int ctrl_id, |
| 155 |
int *value) |
| 156 |
{ |
| 157 |
struct v4l2_control ctrl; |
| 158 |
int ret; |
| 159 |
|
| 160 |
ctrl.id = ctrl_id; |
| 161 |
|
| 162 |
ret = ioctl(dev_fd, VIDIOC_G_CTRL, &ctrl); |
| 163 |
if (ret == -1) |
| 164 |
return -1; |
| 165 |
|
| 166 |
*value = ctrl.value; |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
int v4l2_cfg_set_control_value(int dev_fd, |
| 175 |
int ctrl_id, |
| 176 |
int value) |
| 177 |
{ |
| 178 |
struct v4l2_control ctrl; |
| 179 |
int ret; |
| 180 |
|
| 181 |
ctrl.id = ctrl_id; |
| 182 |
ctrl.value = value; |
| 183 |
|
| 184 |
ret = ioctl(dev_fd, VIDIOC_S_CTRL, &ctrl); |
| 185 |
if (ret == -1) |
| 186 |
return -1; |
| 187 |
|
| 188 |
return 0; |
| 189 |
} |