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 *****************************************************************************/
28 #include <sys/ioctl.h>
31 #include <vlc_common.h>
35 static void *v4l2_handle = NULL;
36 static int (*v4l2_fd_open_) (int, int);
37 int (*v4l2_close) (int);
38 int (*v4l2_ioctl) (int, unsigned long int, ...);
39 ssize_t (*v4l2_read) (int, void *, size_t);
40 void * (*v4l2_mmap) (void *, size_t, int, int, int, int64_t);
41 int (*v4l2_munmap) (void *, size_t);
43 static int fd_open (int fd, int flags)
49 static void v4l2_lib_load (void)
51 void *h = dlopen ("libv4l2.so", RTLD_LAZY | RTLD_LOCAL);
55 v4l2_fd_open_ = dlsym (h, "v4l2_fd_open");
56 v4l2_close = dlsym (h, "v4l2_close");
57 v4l2_ioctl = dlsym (h, "v4l2_ioctl");
58 v4l2_read = dlsym (h, "v4l2_read");
59 v4l2_mmap = dlsym (h, "v4l2_mmap");
60 v4l2_munmap = dlsym (h, "v4l2_munmap");
62 if (v4l2_fd_open_ != NULL && v4l2_close != NULL && v4l2_ioctl != NULL
63 && v4l2_read != NULL && v4l2_mmap != NULL && v4l2_munmap != NULL)
71 v4l2_fd_open_ = fd_open;
79 __attribute__((destructor))
80 static void v4l2_lib_unload (void)
82 if (v4l2_handle != NULL)
83 dlclose (v4l2_handle);
86 int v4l2_fd_open (int fd, int flags)
88 static pthread_once_t once = PTHREAD_ONCE_INIT;
90 pthread_once (&once, v4l2_lib_load);
91 return v4l2_fd_open_ (fd, flags);