Debian Codegraph @

samples/qtvidcap/kv4lsetup.cpp

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 kv4lSetup using Bt848 frame grabber driver
3
4 Copyright (C) 1998 Moritz Wenk ([email protected])
5 Original v4l-setup (C) by Gerd Knorr <[email protected]>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 // need this to avoid conflicting type declarations in Xmd.h and qglobal.h
23 //#define QT_CLEAN_NAMESPACE
24
25 #include <config.h>
26 #include <avm_args.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <sys/ioctl.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <math.h>
37
•38 #include <sys/types.h> /* XXX glibc */
39
40 /* Necessary to prevent collisions between <linux/time.h> and <sys/time.h> when V4L2 is installed. */
41 #define _LINUX_TIME_H
42 #include <linux/videodev.h>
43
44 #ifndef X_DISPLAY_MISSING
45 #include <X11/Xlib.h>
46 #include <X11/Xutil.h>
47
48 #ifdef HAVE_LIBXXF86DGA
49 #include <X11/extensions/xf86dga.h>
50 #endif
51
52 #ifdef HAVE_LIBXXF86VM
53 #include <X11/extensions/xf86vmode.h>
54 #include <X11/extensions/xf86vmstr.h>
55 #endif
56
57 #endif /* X_DISPLAY_MISSING */
58
59 #ifndef major
60 #define major(dev) (((dev) >> 8) & 0xff)
61 #endif
62
63 #define V4L_DEVICE "/dev/video"
64
65
66 static int dev_open(const char *device, int major)
67 {
68 int fd;
69 #if 0
70 if (!strncmp(device, "/dev/", 5) || strchr(device + 5, '/')) {
71 fprintf(stderr, "warning: %s is not a /dev file\n", device);
72 exit(1);
73 }
74 #endif
75
76 /* open & check v4l device */
77 fd = open(device,O_RDWR);
78 if (fd != -1)
79 {
80 struct stat stb;
81 if (fstat(fd,&stb) != -1)
82 {
83 if (S_ISCHR(stb.st_mode) && (int(major(stb.st_rdev)) == major))
84 return fd;
85 else
86 fprintf(stderr, "%s: wrong device\n", device);
87 }
88 else
89 fprintf(stderr, "fstat(%s): %s\n", device, strerror(errno));
90 close(fd);
91 }
92 else
93 fprintf(stderr, "can't open %s: %s\n", device, strerror(errno));
94
95 exit(1);
96 }
97
98 int main(int argc, char* argv[])
99 {
100 int bpp = 0;
101 int shift = 0;
102 int verbose = 0;
103 int bequiet = false;
104 const char* display = ":0.0";
105 const char* device = V4L_DEVICE;
106 char* h;
107 int depth;
108 struct video_capability capability;
109 struct video_buffer fbuf;
110 int fd, c;
111 int i, n, v, found;
112 int set_width, set_height, set_bpl;
113 int set_bpp = 0;
114 void* set_base = NULL;
115
116 #ifndef X_DISPLAY_MISSING
117 Display* dpy;
118 Screen* scr;
119 Window root;
120 XVisualInfo* info, xvtemplate;
121 XWindowAttributes wts;
122 XPixmapFormatValues* pf;
123 #if defined(HAVE_LIBXXF86DGA) || defined(HAVE_LIBXXF86VM)
124 int bar,foo,ma,mi;
125 #endif
126 #ifdef HAVE_LIBXXF86DGA
127 int width,flags;
128 void* base = 0;
129 #endif
130 #ifdef HAVE_LIBXXF86VM
131 int vm_count;
132 XF86VidModeModeInfo** vm_modelines;
133 #endif
134 #endif /* X_DISPLAY_MISSING */
135
136 h = getenv("DISPLAY");
137 if (h != NULL) display = h;
138
139 static const avm::Args::Option opts[] =
140 {
141 avm::ARGSOPTION("i", "bpp", "b", "color depth of the display is <%d>", &bpp, 0, 32),
142 avm::ARGSOPTION("i", "palette", "p", "set palette of display (see help for details)"),
143 avm::ARGSOPTION("s", "device", "l", "use <%s> as video4linux device", &device),
144 avm::ARGSOPTION("s", "display", "d", "use X11 display <%s>", &display),
145 avm::ARGSOPTION("i", "shift", "s", "shift frame buffer by <%d> bytes [%d, %d]", &shift, -8192, 8192),
146 avm::ARGSOPTION("b", "quiet", "q", "be quiet", &bequiet),
147 avm::ARGSOPTION("i", "verbose", "t", "be verbose", &verbose),
148 avm::ARGSOPTION("b", "version", "v", "show version"),
149 avm::ARGSOPTION("h", "", "", ""),
150 avm::ARGSOPTION("c", "codec", "c", "use with help for more info"),
151 avm::ARGSOPTION("#", "", "", "")
152 };
153
154 avm::Args(opts, &argc, argv, "[ options ]\n"
155 " Helper tool to figure out video mode and\n"
156 " configures a video4linux driver like bttv");
157
158 /* verbose = pcl.getSwitch('q', false) ? 0 : 1;
159 verbose = pcl.getSwitch('v', false) ? 2 : verbose;
160 shift = pcl.getInt('s', shift, -8192, 8192);
161 */
162 // check display
163 if (display[0] != ':') {
164 const char* od = display;
165 display = strchr(display,':');
166 if (!display)
167 exit(1);
168 fprintf(stderr,"non-local display `%s' not allowed, using `%s' instead\n",od,display);
169 }
170
171 // open display
172 #ifndef X_DISPLAY_MISSING
173 dpy = XOpenDisplay(display);
174 if (dpy == NULL) {
175 if (verbose) fprintf(stderr,"can't open display %s\n",display);
176 exit(1);
177 }
178
179 if (verbose)
180 fprintf(stderr,"using X11 display %s\n",display);
181
182 // and get params
183 scr = DefaultScreenOfDisplay(dpy);
184 root = DefaultRootWindow(dpy);
185 XGetWindowAttributes(dpy, root, &wts);
186
187 if (verbose)
188 fprintf(stderr,"x11: mode=%dx%dx%d\n",wts.width,wts.height,wts.depth);
189
190 /* look for a usable visual */
191 xvtemplate.screen = XDefaultScreen(dpy);
192 info = XGetVisualInfo(dpy, VisualScreenMask, &xvtemplate, &found);
193 /*
194 v = -1;
195 for (i = 0; v == -1 && i < found; i++)
196 if (info[i].class == TrueColor && info[i].depth >= 15)
197 v = i;
198 for (i = 0; v == -1 && i < found; i++)
199 if (info[i].class == StaticGray && info[i].depth == 8)
200 v = i;
201 if (-1 == v) {
202 fprintf(stderr,"no approximate visual available\n");
203 exit(1);
204 }
205 */
206
207 depth = 0;
208 pf = XListPixmapFormats(dpy,&n);
209 for (i = 0; i < n; i++) {
210 for ( v= 0; v < found; v++ ) {
211 if (pf[i].depth == info[v].depth) {
212 depth = pf[i].bits_per_pixel;
213 break;
214 }
215 }
216 }
217 if (0 == depth && 0 == bpp) {
218 fprintf(stderr,"can't autodetect framebuffer depth\n");
219 exit(1);
220 }
221
222 if (verbose)
223 fprintf(stderr,"x11: detected framebuffer depth: %d bpp\n",depth);
224
225 if ( bpp == 8 || bpp == 15 || bpp == 16 || bpp == 24 || bpp == 32 )
226 depth = bpp;
227 set_bpp = (depth+7) & 0xf8;
228
229 if (verbose)
230 fprintf(stderr,"x11: set depth to %d bpp\n", depth);
231
232 #ifdef HAVE_LIBXXF86DGA
233 if (XF86DGAQueryExtension(dpy, &foo, &bar)) {
234 XF86DGAQueryDirectVideo(dpy, XDefaultScreen(dpy),&flags);
235 if (flags & XF86DGADirectPresent) {
236 XF86DGAGetVideoLL(dpy,XDefaultScreen(dpy),(CAST_DGALL int*)&base,&width,&foo,&bar);
237 set_bpl = width * set_bpp/8;
238 set_base = base;
239 if (verbose == 2) {
240 XF86DGAQueryVersion(dpy,&ma,&mi);
241 fprintf(stderr,"x11: X-Server supports DGA extention (version %d.%d)\n",ma,mi);
242 }
243 if (verbose)
244 fprintf(stderr,"dga: base=%p, width=%d\n",base, width);
245 }
246 }
247 #else
248 if (verbose)
249 fprintf(stderr,"no dga available...\n");
250 #endif
251
252 #ifdef HAVE_LIBXXF86VM
253 if (verbose==2) {
254 foo=bar=0;
255 if (XF86VidModeQueryExtension(dpy,&foo,&bar)) {
256 XF86VidModeQueryVersion(dpy,&ma,&mi);
257 fprintf(stderr,"x11: X-Server supports VidMode extention (version %d.%d)\n",
258 ma,mi);
259 if (ma == 0 && mi < 8) {
260 fprintf(stderr,"x11: VidMode v0.8 or newer required\n");
261 } else if ((ma != XF86VIDMODE_MAJOR_VERSION) || (mi != XF86VIDMODE_MINOR_VERSION)) {
262 fprintf(stderr,"main: VidMode server extention version mismatch, disabled");
263 fprintf(stderr,"main: server version %d.%d != included version %d.%d",ma,mi,
264 XF86VIDMODE_MAJOR_VERSION,XF86VIDMODE_MINOR_VERSION);
265 } else {
266 vm_modelines=(XF86VidModeModeInfo **) malloc( sizeof( XF86VidModeModeInfo * ) );
267 fprintf(stderr,"\t available video mode(s):");
268 XF86VidModeGetAllModeLines(dpy,XDefaultScreen(dpy),
269 &vm_count,&vm_modelines);
270 fprintf(stderr," %d ",vm_count);
271 for (i = 0; i < vm_count; i++) {
272 fprintf(stderr," %dx%d",
273 vm_modelines[i]->hdisplay,
274 vm_modelines[i]->vdisplay);
275 }
276 fprintf(stderr,"\n");
277 }
278 }
279 }
280 #endif
281
282 set_width = wts.width;
283 set_height = wts.height;
284 #else
285 set_width = 384;
286 set_height = 288;
287 #endif /* X_DISPLAY_MISSING */
288
289 set_bpl = set_width * set_bpp / 8;
290 /* Open device file, with security checks */
291 fd = dev_open(device, 81 /* VIDEO_MAJOR */);
292 if (-1 == ioctl(fd, VIDIOCGCAP, &capability)) {
293 fprintf(stderr,"%s: ioctl VIDIOCGCAP: %s\n",device,strerror(errno));
294 exit(1);
295 }
296
297 if (-1 == ioctl(fd, VIDIOCGCAP, &capability)) {
298 fprintf(stderr,"%s: ioctl VIDIOC_G_CAP: %s\n",device,strerror(errno));
299 exit(1);
300 }
301 if (!(capability.type & VID_TYPE_OVERLAY)) {
302 fprintf(stderr,"%s: no overlay support\n",device);
303 exit(1);
304 }
305
306 /* read-modify-write v4l screen parameters */
307 if (ioctl(fd, VIDIOCGFBUF, &fbuf) == -1) {
308 fprintf(stderr,"%s: ioctl VIDIOC_G_FBUF: %s\n",device,strerror(errno));
309 exit(1);
310 }
311
312 /* set values */
313 fbuf.width = set_width;
314 fbuf.height = set_height;
315 fbuf.depth = set_bpp;
316 fbuf.bytesperline = set_bpl;
317 if (set_base != NULL)
318 fbuf.base = (void*)((char*)set_base + shift);
319
320 /* XXX bttv confuses color depth and bits/pixel */
321 #ifndef X_DISPLAY_MISSING
322 if (wts.depth == 15)
323 fbuf.depth = 15;
324 if ((bpp == 15 || bpp == 16) && (depth == 16))
325 fbuf.depth = bpp;
326 #endif /* X_DISPLAY_MISSING */
327 if (verbose) {
328 fprintf(stderr,"set video mode: %dx%d, %d bit/pixel, %d byte/scanline\n",
329 fbuf.width,fbuf.height,fbuf.depth,fbuf.bytesperline);
330 if (set_base != NULL)
331 fprintf(stderr,"set framebuffer at %p\n",set_base);
332 }
333
334 if (ioctl(fd,VIDIOCSFBUF,&fbuf) == -1) {
335 fprintf(stderr,"%s: ioctl VIDIOC_S_FBUF: %s\n",device,strerror(errno));
336 fprintf(stderr,"error: framebuffer address could not be set\n");
337 fprintf(stderr," kv4lsetup needs root permissions\n");
338 fprintf(stderr," kv4lsetup not correctly installed\n");
339 exit(1);
340 }
341 if (verbose)
342 fprintf(stderr,"ok\n");
343
344 close(fd);
345
346 return 0;
347 }