Added config file parser and meson script
Includes style fixes
This commit is contained in:
parent
f40f17a588
commit
94d437df30
|
@ -89,3 +89,10 @@ _deps
|
||||||
/cmake-build-debug
|
/cmake-build-debug
|
||||||
/cmake-build-release
|
/cmake-build-release
|
||||||
*~
|
*~
|
||||||
|
|
||||||
|
/.ninja_deps
|
||||||
|
/.ninja_log
|
||||||
|
/build.ninja
|
||||||
|
/meson-info
|
||||||
|
/meson-private
|
||||||
|
/builddir
|
|
@ -15,5 +15,5 @@ LINK_DIRECTORIES(${GTK3_LIBRARY_DIRS})
|
||||||
# Add other flags to the compiler
|
# Add other flags to the compiler
|
||||||
ADD_DEFINITIONS(${GTK3_CFLAGS_OTHER})
|
ADD_DEFINITIONS(${GTK3_CFLAGS_OTHER})
|
||||||
|
|
||||||
add_executable(GTKCamera main.c)
|
add_executable(GTKCamera main.c ini.c ini.h)
|
||||||
target_link_libraries(GTKCamera ${GTK3_LIBRARIES})
|
target_link_libraries(GTKCamera ${GTK3_LIBRARIES})
|
|
@ -0,0 +1,195 @@
|
||||||
|
/* inih -- simple .INI file parser
|
||||||
|
|
||||||
|
inih is released under the New BSD license (see LICENSE.txt). Go to the project
|
||||||
|
home page for more info:
|
||||||
|
|
||||||
|
https://github.com/benhoyt/inih
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
|
||||||
|
#define _CRT_SECURE_NO_WARNINGS
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "ini.h"
|
||||||
|
|
||||||
|
#if !INI_USE_STACK
|
||||||
|
#include <stdlib.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MAX_SECTION 50
|
||||||
|
#define MAX_NAME 50
|
||||||
|
|
||||||
|
/* Strip whitespace chars off end of given string, in place. Return s. */
|
||||||
|
static char* rstrip(char* s)
|
||||||
|
{
|
||||||
|
char* p = s + strlen(s);
|
||||||
|
while (p > s && isspace((unsigned char)(*--p)))
|
||||||
|
*p = '\0';
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Return pointer to first non-whitespace char in given string. */
|
||||||
|
static char* lskip(const char* s)
|
||||||
|
{
|
||||||
|
while (*s && isspace((unsigned char)(*s)))
|
||||||
|
s++;
|
||||||
|
return (char*)s;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Return pointer to first char (of chars) or inline comment in given string,
|
||||||
|
or pointer to null at end of string if neither found. Inline comment must
|
||||||
|
be prefixed by a whitespace character to register as a comment. */
|
||||||
|
static char* find_chars_or_comment(const char* s, const char* chars)
|
||||||
|
{
|
||||||
|
#if INI_ALLOW_INLINE_COMMENTS
|
||||||
|
int was_space = 0;
|
||||||
|
while (*s && (!chars || !strchr(chars, *s)) &&
|
||||||
|
!(was_space && strchr(INI_INLINE_COMMENT_PREFIXES, *s))) {
|
||||||
|
was_space = isspace((unsigned char)(*s));
|
||||||
|
s++;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
while (*s && (!chars || !strchr(chars, *s))) {
|
||||||
|
s++;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return (char*)s;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Version of strncpy that ensures dest (size bytes) is null-terminated. */
|
||||||
|
static char* strncpy0(char* dest, const char* src, size_t size)
|
||||||
|
{
|
||||||
|
strncpy(dest, src, size-1);
|
||||||
|
dest[size - 1] = '\0';
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* See documentation in header file. */
|
||||||
|
int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
|
||||||
|
void* user)
|
||||||
|
{
|
||||||
|
/* Uses a fair bit of stack (use heap instead if you need to) */
|
||||||
|
#if INI_USE_STACK
|
||||||
|
char line[INI_MAX_LINE];
|
||||||
|
#else
|
||||||
|
char* line;
|
||||||
|
#endif
|
||||||
|
char section[MAX_SECTION] = "";
|
||||||
|
char prev_name[MAX_NAME] = "";
|
||||||
|
|
||||||
|
char* start;
|
||||||
|
char* end;
|
||||||
|
char* name;
|
||||||
|
char* value;
|
||||||
|
int lineno = 0;
|
||||||
|
int error = 0;
|
||||||
|
|
||||||
|
#if !INI_USE_STACK
|
||||||
|
line = (char*)malloc(INI_MAX_LINE);
|
||||||
|
if (!line) {
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Scan through stream line by line */
|
||||||
|
while (reader(line, INI_MAX_LINE, stream) != NULL) {
|
||||||
|
lineno++;
|
||||||
|
|
||||||
|
start = line;
|
||||||
|
#if INI_ALLOW_BOM
|
||||||
|
if (lineno == 1 && (unsigned char)start[0] == 0xEF &&
|
||||||
|
(unsigned char)start[1] == 0xBB &&
|
||||||
|
(unsigned char)start[2] == 0xBF) {
|
||||||
|
start += 3;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
start = lskip(rstrip(start));
|
||||||
|
|
||||||
|
if (*start == ';' || *start == '#') {
|
||||||
|
/* Per Python configparser, allow both ; and # comments at the
|
||||||
|
start of a line */
|
||||||
|
}
|
||||||
|
#if INI_ALLOW_MULTILINE
|
||||||
|
else if (*prev_name && *start && start > line) {
|
||||||
|
/* Non-blank line with leading whitespace, treat as continuation
|
||||||
|
of previous name's value (as per Python configparser). */
|
||||||
|
if (!handler(user, section, prev_name, start) && !error)
|
||||||
|
error = lineno;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
else if (*start == '[') {
|
||||||
|
/* A "[section]" line */
|
||||||
|
end = find_chars_or_comment(start + 1, "]");
|
||||||
|
if (*end == ']') {
|
||||||
|
*end = '\0';
|
||||||
|
strncpy0(section, start + 1, sizeof(section));
|
||||||
|
*prev_name = '\0';
|
||||||
|
}
|
||||||
|
else if (!error) {
|
||||||
|
/* No ']' found on section line */
|
||||||
|
error = lineno;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (*start) {
|
||||||
|
/* Not a comment, must be a name[=:]value pair */
|
||||||
|
end = find_chars_or_comment(start, "=:");
|
||||||
|
if (*end == '=' || *end == ':') {
|
||||||
|
*end = '\0';
|
||||||
|
name = rstrip(start);
|
||||||
|
value = lskip(end + 1);
|
||||||
|
#if INI_ALLOW_INLINE_COMMENTS
|
||||||
|
end = find_chars_or_comment(value, NULL);
|
||||||
|
if (*end)
|
||||||
|
*end = '\0';
|
||||||
|
#endif
|
||||||
|
rstrip(value);
|
||||||
|
|
||||||
|
/* Valid name[=:]value pair found, call handler */
|
||||||
|
strncpy0(prev_name, name, sizeof(prev_name));
|
||||||
|
if (!handler(user, section, name, value) && !error)
|
||||||
|
error = lineno;
|
||||||
|
memset(value, 0, strlen(value));
|
||||||
|
}
|
||||||
|
else if (!error) {
|
||||||
|
/* No '=' or ':' found on name[=:]value line */
|
||||||
|
error = lineno;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if INI_STOP_ON_FIRST_ERROR
|
||||||
|
if (error)
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !INI_USE_STACK
|
||||||
|
free(line);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* See documentation in header file. */
|
||||||
|
int ini_parse_file(FILE* file, ini_handler handler, void* user)
|
||||||
|
{
|
||||||
|
return ini_parse_stream((ini_reader)fgets, file, handler, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* See documentation in header file. */
|
||||||
|
int ini_parse(const char* filename, ini_handler handler, void* user)
|
||||||
|
{
|
||||||
|
FILE* file;
|
||||||
|
int error;
|
||||||
|
|
||||||
|
file = fopen(filename, "r");
|
||||||
|
if (!file)
|
||||||
|
return -1;
|
||||||
|
error = ini_parse_file(file, handler, user);
|
||||||
|
fclose(file);
|
||||||
|
return error;
|
||||||
|
}
|
|
@ -0,0 +1,93 @@
|
||||||
|
/* inih -- simple .INI file parser
|
||||||
|
|
||||||
|
inih is released under the New BSD license (see LICENSE.txt). Go to the project
|
||||||
|
home page for more info:
|
||||||
|
|
||||||
|
https://github.com/benhoyt/inih
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __INI_H__
|
||||||
|
#define __INI_H__
|
||||||
|
|
||||||
|
/* Make this header file easier to include in C++ code */
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* Typedef for prototype of handler function. */
|
||||||
|
typedef int (*ini_handler)(void* user, const char* section,
|
||||||
|
const char* name, const char* value);
|
||||||
|
|
||||||
|
/* Typedef for prototype of fgets-style reader function. */
|
||||||
|
typedef char* (*ini_reader)(char* str, int num, void* stream);
|
||||||
|
|
||||||
|
/* Parse given INI-style file. May have [section]s, name=value pairs
|
||||||
|
(whitespace stripped), and comments starting with ';' (semicolon). Section
|
||||||
|
is "" if name=value pair parsed before any section heading. name:value
|
||||||
|
pairs are also supported as a concession to Python's configparser.
|
||||||
|
|
||||||
|
For each name=value pair parsed, call handler function with given user
|
||||||
|
pointer as well as section, name, and value (data only valid for duration
|
||||||
|
of handler call). Handler should return nonzero on success, zero on error.
|
||||||
|
|
||||||
|
Returns 0 on success, line number of first error on parse error (doesn't
|
||||||
|
stop on first error), -1 on file open error, or -2 on memory allocation
|
||||||
|
error (only when INI_USE_STACK is zero).
|
||||||
|
*/
|
||||||
|
int ini_parse(const char* filename, ini_handler handler, void* user);
|
||||||
|
|
||||||
|
/* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't
|
||||||
|
close the file when it's finished -- the caller must do that. */
|
||||||
|
int ini_parse_file(FILE* file, ini_handler handler, void* user);
|
||||||
|
|
||||||
|
/* Same as ini_parse(), but takes an ini_reader function pointer instead of
|
||||||
|
filename. Used for implementing custom or string-based I/O. */
|
||||||
|
int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
|
||||||
|
void* user);
|
||||||
|
|
||||||
|
/* Nonzero to allow multi-line value parsing, in the style of Python's
|
||||||
|
configparser. If allowed, ini_parse() will call the handler with the same
|
||||||
|
name for each subsequent line parsed. */
|
||||||
|
#ifndef INI_ALLOW_MULTILINE
|
||||||
|
#define INI_ALLOW_MULTILINE 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of
|
||||||
|
the file. See http://code.google.com/p/inih/issues/detail?id=21 */
|
||||||
|
#ifndef INI_ALLOW_BOM
|
||||||
|
#define INI_ALLOW_BOM 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Nonzero to allow inline comments (with valid inline comment characters
|
||||||
|
specified by INI_INLINE_COMMENT_PREFIXES). Set to 0 to turn off and match
|
||||||
|
Python 3.2+ configparser behaviour. */
|
||||||
|
#ifndef INI_ALLOW_INLINE_COMMENTS
|
||||||
|
#define INI_ALLOW_INLINE_COMMENTS 1
|
||||||
|
#endif
|
||||||
|
#ifndef INI_INLINE_COMMENT_PREFIXES
|
||||||
|
#define INI_INLINE_COMMENT_PREFIXES ";"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Nonzero to use stack, zero to use heap (malloc/free). */
|
||||||
|
#ifndef INI_USE_STACK
|
||||||
|
#define INI_USE_STACK 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Stop parsing on first error (default is to keep parsing). */
|
||||||
|
#ifndef INI_STOP_ON_FIRST_ERROR
|
||||||
|
#define INI_STOP_ON_FIRST_ERROR 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Maximum line length for any line in INI file. */
|
||||||
|
#ifndef INI_MAX_LINE
|
||||||
|
#define INI_MAX_LINE 2000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* __INI_H__ */
|
361
main.c
361
main.c
|
@ -5,8 +5,7 @@
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <asm/errno.h>
|
#include <asm/errno.h>
|
||||||
#include <gtk/gtk.h>
|
#include <gtk/gtk.h>
|
||||||
|
#include "ini.h"
|
||||||
#define CLEAR(x) memset(&(x), 0, sizeof(x))
|
|
||||||
|
|
||||||
enum io_method {
|
enum io_method {
|
||||||
IO_METHOD_READ,
|
IO_METHOD_READ,
|
||||||
|
@ -19,88 +18,97 @@ struct buffer {
|
||||||
size_t length;
|
size_t length;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct buffer *buffers;
|
struct buffer *buffers;
|
||||||
static unsigned int n_buffers;
|
static unsigned int n_buffers;
|
||||||
|
static char *rear_dev_name;
|
||||||
|
static char *front_dev_name;
|
||||||
static char *dev_name;
|
static char *dev_name;
|
||||||
static enum io_method io = IO_METHOD_MMAP;
|
static enum io_method io = IO_METHOD_MMAP;
|
||||||
static int force_format_width = 640;
|
|
||||||
static int force_format_height = 480;
|
static int preview_width = -1;
|
||||||
|
static int preview_height = -1;
|
||||||
|
static int preview_fmt = V4L2_PIX_FMT_RGB24;
|
||||||
|
|
||||||
GObject *preview_image;
|
GObject *preview_image;
|
||||||
|
|
||||||
|
static int
|
||||||
static int xioctl(int fd, int request, void *arg) {
|
xioctl(int fd, int request, void *arg)
|
||||||
|
{
|
||||||
int r;
|
int r;
|
||||||
do r = ioctl(fd, request, arg);
|
do {
|
||||||
while (-1 == r && EINTR == errno);
|
r = ioctl(fd, request, arg);
|
||||||
|
} while (r == -1 && errno == EINTR);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void errno_exit(const char *s) {
|
static void
|
||||||
|
errno_exit(const char *s)
|
||||||
|
{
|
||||||
fprintf(stderr, "%s error %d, %s\\n", s, errno, strerror(errno));
|
fprintf(stderr, "%s error %d, %s\\n", s, errno, strerror(errno));
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void start_capturing(int fd) {
|
static void
|
||||||
unsigned int i;
|
start_capturing(int fd)
|
||||||
|
{
|
||||||
enum v4l2_buf_type type;
|
enum v4l2_buf_type type;
|
||||||
|
|
||||||
switch (io) {
|
switch (io) {
|
||||||
case IO_METHOD_READ:
|
case IO_METHOD_READ:
|
||||||
/* Nothing to do. */
|
/* Nothing to do. */
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IO_METHOD_MMAP:
|
case IO_METHOD_MMAP:
|
||||||
for (i = 0; i < n_buffers; ++i) {
|
for (int i = 0; i < n_buffers; ++i) {
|
||||||
struct v4l2_buffer buf;
|
struct v4l2_buffer buf = {
|
||||||
|
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
|
||||||
|
.memory = V4L2_MEMORY_MMAP,
|
||||||
|
.index = i,
|
||||||
|
};
|
||||||
|
|
||||||
CLEAR(buf);
|
if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
|
||||||
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
||||||
buf.memory = V4L2_MEMORY_MMAP;
|
|
||||||
buf.index = i;
|
|
||||||
|
|
||||||
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
|
|
||||||
errno_exit("VIDIOC_QBUF");
|
errno_exit("VIDIOC_QBUF");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||||
if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
|
if (xioctl(fd, VIDIOC_STREAMON, &type) == -1) {
|
||||||
errno_exit("VIDIOC_STREAMON");
|
errno_exit("VIDIOC_STREAMON");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IO_METHOD_USERPTR:
|
case IO_METHOD_USERPTR:
|
||||||
for (i = 0; i < n_buffers; ++i) {
|
for (int i = 0; i < n_buffers; ++i) {
|
||||||
struct v4l2_buffer buf;
|
struct v4l2_buffer buf = {
|
||||||
|
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
|
||||||
CLEAR(buf);
|
.memory = V4L2_MEMORY_USERPTR,
|
||||||
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
.index = i,
|
||||||
buf.memory = V4L2_MEMORY_USERPTR;
|
};
|
||||||
buf.index = i;
|
buf.m.userptr = (unsigned long)buffers[i].start;
|
||||||
buf.m.userptr = (unsigned long) buffers[i].start;
|
|
||||||
buf.length = buffers[i].length;
|
buf.length = buffers[i].length;
|
||||||
|
|
||||||
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
|
if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
|
||||||
errno_exit("VIDIOC_QBUF");
|
errno_exit("VIDIOC_QBUF");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||||
if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
|
if (xioctl(fd, VIDIOC_STREAMON, &type) == -1) {
|
||||||
errno_exit("VIDIOC_STREAMON");
|
errno_exit("VIDIOC_STREAMON");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init_mmap(int fd) {
|
static void
|
||||||
struct v4l2_requestbuffers req;
|
init_mmap(int fd)
|
||||||
|
{
|
||||||
CLEAR(req);
|
struct v4l2_requestbuffers req = { 0 };
|
||||||
|
|
||||||
req.count = 4;
|
req.count = 4;
|
||||||
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||||
req.memory = V4L2_MEMORY_MMAP;
|
req.memory = V4L2_MEMORY_MMAP;
|
||||||
|
|
||||||
if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
|
if (xioctl(fd, VIDIOC_REQBUFS, &req) == -1) {
|
||||||
if (EINVAL == errno) {
|
if (errno == EINVAL) {
|
||||||
fprintf(stderr, "%s does not support "
|
fprintf(stderr, "%s does not support memory mapping",
|
||||||
"memory mappingn", dev_name);
|
dev_name);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
} else {
|
} else {
|
||||||
errno_exit("VIDIOC_REQBUFS");
|
errno_exit("VIDIOC_REQBUFS");
|
||||||
|
@ -108,12 +116,12 @@ static void init_mmap(int fd) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.count < 2) {
|
if (req.count < 2) {
|
||||||
fprintf(stderr, "Insufficient buffer memory on %s\\n",
|
fprintf(stderr, "Insufficient buffer memory on %s\n",
|
||||||
dev_name);
|
dev_name);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
buffers = calloc(req.count, sizeof(*buffers));
|
buffers = calloc(req.count, sizeof(buffers[0]));
|
||||||
|
|
||||||
if (!buffers) {
|
if (!buffers) {
|
||||||
fprintf(stderr, "Out of memory\\n");
|
fprintf(stderr, "Out of memory\\n");
|
||||||
|
@ -121,40 +129,36 @@ static void init_mmap(int fd) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
|
for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
|
||||||
struct v4l2_buffer buf;
|
struct v4l2_buffer buf = {
|
||||||
|
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
|
||||||
|
.memory = V4L2_MEMORY_MMAP,
|
||||||
|
.index = n_buffers,
|
||||||
|
};
|
||||||
|
|
||||||
CLEAR(buf);
|
if (xioctl(fd, VIDIOC_QUERYBUF, &buf) == -1) {
|
||||||
|
|
||||||
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
||||||
buf.memory = V4L2_MEMORY_MMAP;
|
|
||||||
buf.index = n_buffers;
|
|
||||||
|
|
||||||
if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))
|
|
||||||
errno_exit("VIDIOC_QUERYBUF");
|
errno_exit("VIDIOC_QUERYBUF");
|
||||||
|
}
|
||||||
|
|
||||||
buffers[n_buffers].length = buf.length;
|
buffers[n_buffers].length = buf.length;
|
||||||
buffers[n_buffers].start =
|
buffers[n_buffers].start = mmap(NULL /* start anywhere */,
|
||||||
mmap(NULL /* start anywhere */,
|
|
||||||
buf.length,
|
buf.length,
|
||||||
PROT_READ | PROT_WRITE /* required */,
|
PROT_READ | PROT_WRITE /* required */,
|
||||||
MAP_SHARED /* recommended */,
|
MAP_SHARED /* recommended */,
|
||||||
fd, buf.m.offset);
|
fd, buf.m.offset);
|
||||||
|
|
||||||
if (MAP_FAILED == buffers[n_buffers].start)
|
if (MAP_FAILED == buffers[n_buffers].start) {
|
||||||
errno_exit("mmap");
|
errno_exit("mmap");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init_device(int fd) {
|
static void
|
||||||
|
init_device(int fd)
|
||||||
|
{
|
||||||
struct v4l2_capability cap;
|
struct v4l2_capability cap;
|
||||||
struct v4l2_cropcap cropcap;
|
if (xioctl(fd, VIDIOC_QUERYCAP, &cap) == -1) {
|
||||||
struct v4l2_crop crop;
|
if (errno == EINVAL) {
|
||||||
struct v4l2_format fmt;
|
fprintf(stderr, "%s is no V4L2 device\n",
|
||||||
unsigned int min;
|
|
||||||
|
|
||||||
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
|
|
||||||
if (EINVAL == errno) {
|
|
||||||
fprintf(stderr, "%s is no V4L2 device\\n",
|
|
||||||
dev_name);
|
dev_name);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
} else {
|
} else {
|
||||||
|
@ -163,7 +167,7 @@ static void init_device(int fd) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
|
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
|
||||||
fprintf(stderr, "%s is no video capture device\\n",
|
fprintf(stderr, "%s is no video capture device\n",
|
||||||
dev_name);
|
dev_name);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
@ -171,35 +175,32 @@ static void init_device(int fd) {
|
||||||
switch (io) {
|
switch (io) {
|
||||||
case IO_METHOD_READ:
|
case IO_METHOD_READ:
|
||||||
if (!(cap.capabilities & V4L2_CAP_READWRITE)) {
|
if (!(cap.capabilities & V4L2_CAP_READWRITE)) {
|
||||||
fprintf(stderr, "%s does not support read i/o\\n",
|
fprintf(stderr, "%s does not support read i/o\n",
|
||||||
dev_name);
|
dev_name);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IO_METHOD_MMAP:
|
case IO_METHOD_MMAP:
|
||||||
case IO_METHOD_USERPTR:
|
case IO_METHOD_USERPTR:
|
||||||
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
|
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
|
||||||
fprintf(stderr, "%s does not support streaming i/o\\n",
|
fprintf(stderr, "%s does not support streaming i/o\n",
|
||||||
dev_name);
|
dev_name);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Select video input, video standard and tune here. */
|
/* Select video input, video standard and tune here. */
|
||||||
|
struct v4l2_cropcap cropcap = {
|
||||||
|
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct v4l2_crop crop = { 0 };
|
||||||
CLEAR(cropcap);
|
if (xioctl(fd, VIDIOC_CROPCAP, &cropcap) == 0) {
|
||||||
|
|
||||||
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
||||||
|
|
||||||
if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) {
|
|
||||||
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||||
crop.c = cropcap.defrect; /* reset to default */
|
crop.c = cropcap.defrect; /* reset to default */
|
||||||
|
|
||||||
if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) {
|
if (xioctl(fd, VIDIOC_S_CROP, &crop) == -1) {
|
||||||
switch (errno) {
|
switch (errno) {
|
||||||
case EINVAL:
|
case EINVAL:
|
||||||
/* Cropping not supported. */
|
/* Cropping not supported. */
|
||||||
|
@ -214,33 +215,36 @@ static void init_device(int fd) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CLEAR(fmt);
|
struct v4l2_format fmt = {
|
||||||
|
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
|
||||||
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
};
|
||||||
if (force_format_width > 0) {
|
if (preview_width > 0) {
|
||||||
fmt.fmt.pix.width = force_format_width;
|
fmt.fmt.pix.width = preview_width;
|
||||||
fmt.fmt.pix.height = force_format_height;
|
fmt.fmt.pix.height = preview_height;
|
||||||
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
|
fmt.fmt.pix.pixelformat = preview_fmt;
|
||||||
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
|
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
|
||||||
printf("Requesting a cooler format!\n");
|
|
||||||
|
|
||||||
if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
|
if (xioctl(fd, VIDIOC_S_FMT, &fmt) == -1) {
|
||||||
errno_exit("VIDIOC_S_FMT");
|
errno_exit("VIDIOC_S_FMT");
|
||||||
|
}
|
||||||
|
|
||||||
/* Note VIDIOC_S_FMT may change width and height. */
|
/* Note VIDIOC_S_FMT may change width and height. */
|
||||||
} else {
|
} else {
|
||||||
/* Preserve original settings as set by v4l2-ctl for example */
|
/* Preserve original settings as set by v4l2-ctl for example */
|
||||||
if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt))
|
if (xioctl(fd, VIDIOC_G_FMT, &fmt) == -1) {
|
||||||
errno_exit("VIDIOC_G_FMT");
|
errno_exit("VIDIOC_G_FMT");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Buggy driver paranoia. */
|
/* Buggy driver paranoia. */
|
||||||
min = fmt.fmt.pix.width * 2;
|
unsigned int min = fmt.fmt.pix.width * 2;
|
||||||
if (fmt.fmt.pix.bytesperline < min)
|
if (fmt.fmt.pix.bytesperline < min) {
|
||||||
fmt.fmt.pix.bytesperline = min;
|
fmt.fmt.pix.bytesperline = min;
|
||||||
|
}
|
||||||
min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
|
min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
|
||||||
if (fmt.fmt.pix.sizeimage < min)
|
if (fmt.fmt.pix.sizeimage < min) {
|
||||||
fmt.fmt.pix.sizeimage = min;
|
fmt.fmt.pix.sizeimage = min;
|
||||||
|
}
|
||||||
|
|
||||||
switch (io) {
|
switch (io) {
|
||||||
case IO_METHOD_READ:
|
case IO_METHOD_READ:
|
||||||
|
@ -257,54 +261,48 @@ static void init_device(int fd) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void process_image(const void *p, int size) {
|
static void
|
||||||
GdkPixbuf *pixbuf;
|
process_image(const void *p, int size)
|
||||||
pixbuf = gdk_pixbuf_new_from_data(p, GDK_COLORSPACE_RGB, FALSE, 8, 640, 480, 2 * 640, NULL, NULL);
|
{
|
||||||
|
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data(p, GDK_COLORSPACE_RGB,
|
||||||
|
FALSE, 8, 640, 480, 2 * 640, NULL, NULL);
|
||||||
gtk_image_set_from_pixbuf(preview_image, pixbuf);
|
gtk_image_set_from_pixbuf(preview_image, pixbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int read_frame(int fd) {
|
static int
|
||||||
struct v4l2_buffer buf;
|
read_frame(int fd)
|
||||||
unsigned int i;
|
{
|
||||||
|
struct v4l2_buffer buf = { 0 };
|
||||||
|
|
||||||
switch (io) {
|
switch (io) {
|
||||||
case IO_METHOD_READ:
|
case IO_METHOD_READ:
|
||||||
if (-1 == read(fd, buffers[0].start, buffers[0].length)) {
|
if (read(fd, buffers[0].start, buffers[0].length) == -1) {
|
||||||
switch (errno) {
|
switch (errno) {
|
||||||
case EAGAIN:
|
case EAGAIN:
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
case EIO:
|
case EIO:
|
||||||
/* Could ignore EIO, see spec. */
|
/* Could ignore EIO, see spec. */
|
||||||
|
/* fallthrough */
|
||||||
/* fall through */
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
errno_exit("read");
|
errno_exit("read");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
process_image(buffers[0].start, buffers[0].length);
|
process_image(buffers[0].start, buffers[0].length);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IO_METHOD_MMAP:
|
case IO_METHOD_MMAP:
|
||||||
CLEAR(buf);
|
|
||||||
|
|
||||||
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||||
buf.memory = V4L2_MEMORY_MMAP;
|
buf.memory = V4L2_MEMORY_MMAP;
|
||||||
|
if (xioctl(fd, VIDIOC_DQBUF, &buf) == -1) {
|
||||||
if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
|
|
||||||
switch (errno) {
|
switch (errno) {
|
||||||
case EAGAIN:
|
case EAGAIN:
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
case EIO:
|
case EIO:
|
||||||
/* Could ignore EIO, see spec. */
|
/* Could ignore EIO, see spec. */
|
||||||
|
/* fallthrough */
|
||||||
/* fall through */
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
errno_exit("VIDIOC_DQBUF");
|
errno_exit("VIDIOC_DQBUF");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -312,50 +310,49 @@ static int read_frame(int fd) {
|
||||||
|
|
||||||
process_image(buffers[buf.index].start, buf.bytesused);
|
process_image(buffers[buf.index].start, buf.bytesused);
|
||||||
|
|
||||||
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
|
if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
|
||||||
errno_exit("VIDIOC_QBUF");
|
errno_exit("VIDIOC_QBUF");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IO_METHOD_USERPTR:
|
case IO_METHOD_USERPTR:
|
||||||
CLEAR(buf);
|
|
||||||
|
|
||||||
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||||
buf.memory = V4L2_MEMORY_USERPTR;
|
buf.memory = V4L2_MEMORY_USERPTR;
|
||||||
|
if (xioctl(fd, VIDIOC_DQBUF, &buf) == -1) {
|
||||||
if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
|
|
||||||
switch (errno) {
|
switch (errno) {
|
||||||
case EAGAIN:
|
case EAGAIN:
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
case EIO:
|
case EIO:
|
||||||
/* Could ignore EIO, see spec. */
|
/* Could ignore EIO, see spec. */
|
||||||
|
/* fallthrough */
|
||||||
/* fall through */
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
errno_exit("VIDIOC_DQBUF");
|
errno_exit("VIDIOC_DQBUF");
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < n_buffers; ++i)
|
|
||||||
if (buf.m.userptr == (unsigned long) buffers[i].start
|
|
||||||
&& buf.length == buffers[i].length)
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unsigned int i;
|
||||||
|
for (i = 0; i < n_buffers; ++i) {
|
||||||
|
if (buf.m.userptr == (unsigned long)buffers[i].start
|
||||||
|
&& buf.length == buffers[i].length) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//assert(i < n_buffers);
|
//assert(i < n_buffers);
|
||||||
|
|
||||||
process_image((void *) buf.m.userptr, buf.bytesused);
|
process_image((void *)buf.m.userptr, buf.bytesused);
|
||||||
|
if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
|
||||||
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
|
|
||||||
errno_exit("VIDIOC_QBUF");
|
errno_exit("VIDIOC_QBUF");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void get_frame(int fd) {
|
static void
|
||||||
for (;;) {
|
get_frame(int fd)
|
||||||
|
{
|
||||||
|
while (1) {
|
||||||
fd_set fds;
|
fd_set fds;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
int r;
|
int r;
|
||||||
|
@ -369,67 +366,107 @@ static void get_frame(int fd) {
|
||||||
|
|
||||||
r = select(fd + 1, &fds, NULL, NULL, &tv);
|
r = select(fd + 1, &fds, NULL, NULL, &tv);
|
||||||
|
|
||||||
if (-1 == r) {
|
if (r == -1) {
|
||||||
if (EINTR == errno)
|
if (EINTR == errno) {
|
||||||
continue;
|
continue;
|
||||||
errno_exit("select");
|
|
||||||
}
|
}
|
||||||
|
errno_exit("select");
|
||||||
if (0 == r) {
|
} else if (r == 0) {
|
||||||
fprintf(stderr, "select timeout\\n");
|
fprintf(stderr, "select timeout\\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (read_frame(fd))
|
if (read_frame(fd)) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
/* EAGAIN - continue select loop. */
|
/* EAGAIN - continue select loop. */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc,
|
static int
|
||||||
char *argv[]) {
|
config_ini_handler(void *user, const char *section, const char *name,
|
||||||
GtkBuilder *builder;
|
const char *value) {
|
||||||
GObject *window;
|
if (strcmp(section, "preview") == 0) {
|
||||||
GObject *preview_box;
|
if (strcmp(name, "width") == 0) {
|
||||||
|
preview_width = strtol(value, NULL, 10);
|
||||||
|
} else if (strcmp(name, "height") == 0) {
|
||||||
|
preview_height = strtol(value, NULL, 10);
|
||||||
|
} else if (strcmp(name, "fmt") == 0) {
|
||||||
|
preview_fmt = strtol(value, NULL, 10);
|
||||||
|
} else {
|
||||||
|
g_printerr("Unknown key '%s' in [preview]", name);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if (strcmp(section, "device") == 0) {
|
||||||
|
if (strcmp(name, "rear") == 0) {
|
||||||
|
rear_dev_name = strdup(value);
|
||||||
|
} else if (strcmp(name, "front") == 0) {
|
||||||
|
front_dev_name = strdup(value);
|
||||||
|
} else {
|
||||||
|
g_printerr("Unknown key '%s' in [device]", name);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
g_printerr("Unknown section '%s' in config file", section);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
if (argc != 2) {
|
||||||
|
g_printerr("Usage: camera configfile\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
|
|
||||||
gtk_init(&argc, &argv);
|
gtk_init(&argc, &argv);
|
||||||
|
GtkBuilder *builder = gtk_builder_new();
|
||||||
/* Construct a GtkBuilder instance and load our UI description */
|
|
||||||
builder = gtk_builder_new();
|
|
||||||
if (gtk_builder_add_from_file(builder, "camera.glade", &error) == 0) {
|
if (gtk_builder_add_from_file(builder, "camera.glade", &error) == 0) {
|
||||||
g_printerr("Error loading file: %s\n", error->message);
|
g_printerr("Error loading file: %s\n", error->message);
|
||||||
g_clear_error(&error);
|
g_clear_error(&error);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Connect signal handlers to the constructed widgets. */
|
GObject *window = gtk_builder_get_object(builder, "window");
|
||||||
window = gtk_builder_get_object(builder, "window");
|
GObject *preview_box = gtk_builder_get_object(builder, "preview_box");
|
||||||
preview_box = gtk_builder_get_object(builder, "preview_box");
|
|
||||||
preview_image = gtk_builder_get_object(builder, "preview");
|
preview_image = gtk_builder_get_object(builder, "preview");
|
||||||
g_signal_connect (window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
|
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
|
||||||
|
|
||||||
/* Load the css */
|
|
||||||
GtkCssProvider *provider = gtk_css_provider_new();
|
GtkCssProvider *provider = gtk_css_provider_new();
|
||||||
gtk_css_provider_load_from_path(provider, "camera.css", NULL);
|
gtk_css_provider_load_from_path(provider, "camera.css", NULL);
|
||||||
GtkStyleContext *context;
|
GtkStyleContext *context = gtk_widget_get_style_context(preview_box);
|
||||||
context = gtk_widget_get_style_context(preview_box);
|
gtk_style_context_add_provider(context,
|
||||||
gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
|
GTK_STYLE_PROVIDER(provider),
|
||||||
|
GTK_STYLE_PROVIDER_PRIORITY_USER);
|
||||||
|
|
||||||
/* Grab a frame from the camera */
|
int result = ini_parse(argv[1], config_ini_handler, NULL);
|
||||||
int fd;
|
if (result == -1) {
|
||||||
fd = open("/dev/video0", O_RDWR);
|
g_printerr("Config file not found\n");
|
||||||
if (fd == -1) {
|
return 1;
|
||||||
g_printerr("Error opening video device: /dev/video0\n");
|
} else if (result == -2) {
|
||||||
|
g_printerr("Could not allocate memory to parse config file\n");
|
||||||
|
return 1;
|
||||||
|
} else if (result != 0) {
|
||||||
|
g_printerr("Could not parse config file\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dev_name = rear_dev_name;
|
||||||
|
|
||||||
|
int fd = open(dev_name, O_RDWR);
|
||||||
|
if (fd == -1) {
|
||||||
|
g_printerr("Error opening video device: %s\n", dev_name);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
init_device(fd);
|
init_device(fd);
|
||||||
start_capturing(fd);
|
start_capturing(fd);
|
||||||
get_frame(fd);
|
get_frame(fd);
|
||||||
|
|
||||||
/* Show application */
|
|
||||||
gtk_widget_show(window);
|
gtk_widget_show(window);
|
||||||
gtk_main();
|
gtk_main();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
project('camera', 'c')
|
||||||
|
gtkdep = dependency('gtk+-3.0')
|
||||||
|
executable('camera', 'main.c', 'ini.c', dependencies : gtkdep)
|
Loading…
Reference in New Issue