summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlastair Poole <netstar@gmail.com>2020-07-17 22:39:49 +0100
committerAlastair Poole <netstar@gmail.com>2020-07-17 22:39:49 +0100
commit7d68cb2ab61b2226cd56aba1b189a27e990456f7 (patch)
tree0af8e0737d6bd0cd9e89110bd719fc3e314dfa72
parentdcc0ec5aa7df89d2667cca3bedb2c94b30cbd9ee (diff)
select: n > status
-rw-r--r--cam.c43
-rw-r--r--vid.c14
2 files changed, 31 insertions, 26 deletions
diff --git a/cam.c b/cam.c
index 340a6a0..e3ba57e 100644
--- a/cam.c
+++ b/cam.c
@@ -134,7 +134,7 @@ typedef struct {
static int
mmap_camera(int fd, int w, int h)
{
- fd_set fds;
+ fd_set fds, readset;
struct timeval tv;
struct v4l2_buffer buf;
struct v4l2_requestbuffers req;
@@ -199,18 +199,18 @@ mmap_camera(int fd, int w, int h)
tcsetattr(STDIN_FILENO, TCSANOW, &ntio);
ts = time(NULL);
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ FD_SET(STDIN_FILENO, &fds);
while (1) {
- FD_ZERO(&fds);
- FD_SET(fd, &fds);
- FD_SET(STDIN_FILENO, &fds);
-
- int status = select(fd + 1, &fds, NULL, NULL, NULL);
- if (status == -1 && errno == EINTR) continue;
- else if (status == 0)
+ readset = fds;
+ int n = select(fd + 1, &readset, NULL, NULL, NULL);
+ if (n == -1 && errno == EINTR) continue;
+ else if (n == 0)
borked("timeout");
- if (FD_ISSET(STDIN_FILENO, &fds))
+ if (FD_ISSET(STDIN_FILENO, &readset))
break;
memset(&buf, 0, sizeof(buf));
@@ -254,7 +254,7 @@ static int
read_camera(int fd, int w, int h)
{
char *out, *data;
- fd_set fds;
+ fd_set fds, readset;
struct timeval tv;
struct termios ntio, otio;
time_t ts, te;
@@ -275,21 +275,22 @@ read_camera(int fd, int w, int h)
tcsetattr(STDIN_FILENO, TCSANOW, &ntio);
ts = time(NULL);
+ FD_ZERO(&fds);
+ FD_SET(STDIN_FILENO, &fds);
+ FD_SET(fd, &fds);
while (1) {
- FD_ZERO(&fds);
- FD_SET(STDIN_FILENO, &fds);
- FD_SET(fd, &fds);
+ readset = fds;
tv.tv_sec = 2;
tv.tv_usec = 0;
- status = select(fd + 1, &fds, NULL, NULL, &tv);
+ status = select(fd + 1, &readset, NULL, NULL, &tv);
if (status == -1 && errno == EINTR) continue;
else if (status == 0)
borked("timeout");
- if (FD_ISSET(STDIN_FILENO, &fds))
+ if (FD_ISSET(STDIN_FILENO, &readset))
break;
if ((read(fd, data, (w * h) * 2)) == -1) {
@@ -323,8 +324,8 @@ int main(int argc, char **argv)
{
struct v4l2_capability cap;
struct v4l2_format format;
- int fd, ret = 1;
uint32_t w, h;
+ int fd, ret = 1;
ecore_evas_init();
@@ -335,7 +336,7 @@ int main(int argc, char **argv)
fd = open(argv[1], O_RDWR | O_NONBLOCK);
if (fd == -1) {
fprintf(stderr, "Unable to open %s - %s\n", argv[1], strerror(errno));
- exit(1);
+ goto cleanup;
}
if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == -1) {
@@ -344,7 +345,8 @@ int main(int argc, char **argv)
}
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
- borked("NO STREAMING CAPABILITY");
+ fprintf(stderr, "NO STREAMING CAPABILITY\n");
+ goto cleanup;
}
memset(&format, 0, sizeof(format));
@@ -368,14 +370,15 @@ int main(int argc, char **argv)
switch (format.fmt.pix.pixelformat) {
case V4L2_PIX_FMT_YUYV:
- mmap_camera(fd, w, h);
+ ret = mmap_camera(fd, w, h);
break;
default:
break;
}
cleanup:
- close(fd);
+ if (fd != -1)
+ close(fd);
ecore_evas_shutdown();
return ret;
diff --git a/vid.c b/vid.c
index ebedea5..a5c7c4d 100644
--- a/vid.c
+++ b/vid.c
@@ -150,7 +150,7 @@ typedef struct {
static int
mmap_camera(Cam *cam)
{
- fd_set fds;
+ fd_set fds, readset;
struct timeval tv;
struct v4l2_buffer buf;
struct v4l2_requestbuffers req;
@@ -220,13 +220,15 @@ mmap_camera(Cam *cam)
count = 0;
ts = time(NULL);
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+
while (1) {
- FD_ZERO(&fds);
- FD_SET(fd, &fds);
+ readset = fds;
- int status = select(fd + 1, &fds, NULL, NULL, NULL);
- if (status == -1 && errno == EINTR) continue;
- else if (status == 0) {
+ int n = select(fd + 1, &readset, NULL, NULL, NULL);
+ if (n == -1 && errno == EINTR) continue;
+ else if (n == 0) {
fprintf(stderr, "select timeout\n");
exit(EXIT_FAILURE);
}