contrib: cargo: use the 0.6.13 cargo-c version
[vlc.git] / modules / access / v4l2 / lib.c
blobdc46b1acfe2ffa9728bb904305639af7bf85d9a5
1 /*****************************************************************************
2 * lib.c : libv4l2 run-time
3 *****************************************************************************
4 * Copyright (C) 2012 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <dlfcn.h>
26 #include <unistd.h>
27 #include <sys/ioctl.h>
28 #include <sys/mman.h>
30 #include <vlc_common.h>
32 #include "v4l2.h"
34 static int fd_open (int fd, int flags)
36 (void) flags;
37 return fd;
40 static void *v4l2_handle = NULL;
42 static int (*v4l2_fd_open_cb)(int, int) = fd_open;
43 //int (*v4l2_open) (const char *, int, ...) = open;
44 //int (*v4l2_dup) (const char *, int, ...) = dup;
45 int (*v4l2_close) (int) = close;
46 int (*v4l2_ioctl) (int, unsigned long int, ...) = ioctl;
47 ssize_t (*v4l2_read) (int, void *, size_t) = read;
48 //ssize_t (*v4l2_write) (int, const void *, size_t) = write;
49 void * (*v4l2_mmap) (void *, size_t, int, int, int, int64_t) = mmap;
50 int (*v4l2_munmap) (void *, size_t) = munmap;
52 static void v4l2_lib_load (void)
54 void *h;
56 h = dlopen ("libmediaclient.so", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
57 if (h == NULL)
58 h = dlopen ("libv4l2.so.0", RTLD_LAZY | RTLD_LOCAL);
59 if (h == NULL)
60 return;
62 void *sym = dlsym(h, "v4l2_fd_open");
63 if (sym != NULL)
64 v4l2_fd_open_cb = sym;
66 #define SYM(name) \
67 sym = dlsym (h, "v4l2_"#name); \
68 if (sym != NULL) v4l2_##name = sym
70 /*SYM(open); SYM(dup);*/ SYM(close); SYM(ioctl);
71 SYM(read); /*SYM(write);*/ SYM(mmap); SYM(munmap);
73 v4l2_handle = h;
76 __attribute__((destructor))
77 static void v4l2_lib_unload (void)
79 if (v4l2_handle != NULL)
80 dlclose (v4l2_handle);
83 int v4l2_fd_open(int fd, int flags)
85 static pthread_once_t once = PTHREAD_ONCE_INIT;
87 pthread_once(&once, v4l2_lib_load);
88 return v4l2_fd_open_cb(fd, flags);