1
|
1 /* Capture and store a 24 bit RGB image as a PPM file */
|
|
2 /* Copyright (c) 2000 Randall Hopper
|
|
3 *
|
|
4 * Based on a grab.c from Roger Hardiman, which was
|
|
5 * based on an original program by Mark Tinguely and Jim Lowe .
|
|
6 *
|
|
7 * All rights reserved.
|
|
8 *
|
|
9 * Redistribution and use in source and binary forms, with or without
|
|
10 * modification, are permitted provided that the following conditions
|
|
11 * are met:
|
|
12 * 1. Redistributions of source code must retain the above copyright
|
|
13 * notice, this list of conditions and the following disclaimer.
|
|
14 * 2. Redistributions in binary form must reproduce the above copyright
|
|
15 * notice, this list of conditions and the following disclaimer in the
|
|
16 * documentation and/or other materials provided with the distribution.
|
|
17 * 3. All advertising materials mentioning features or use of this software
|
|
18 * must display the following acknowledgement:
|
|
19 * This product includes software developed by Mark Tinguely and Jim Lowe
|
|
20 * 4. The name of the author may not be used to endorse or promote products
|
|
21 * derived from this software without specific prior written permission.
|
|
22 *
|
|
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
26 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
33 * POSSIBILITY OF SUCH DAMAGE.
|
|
34 */
|
|
35
|
|
36 #include <assert.h>
|
|
37 #include <stdio.h>
|
|
38 #include <unistd.h>
|
|
39 #include <string.h>
|
|
40 #include <stdlib.h>
|
|
41 #include <errno.h>
|
|
42 #include <sys/types.h>
|
|
43 #include <poll.h>
|
|
44 #include <sys/mman.h>
|
|
45 #include <sys/fcntl.h>
|
|
46 #include <sys/time.h>
|
|
47 #include <libgen.h>
|
|
48 #include <sys/soundcard.h>
|
5
|
49 #include <dev/bktr/ioctl_bt848.h>
|
|
50 #include <dev/bktr/ioctl_meteor.h>
|
1
|
51 #include <machine/param.h>
|
|
52 #include <sys/types.h>
|
|
53 #include <sys/ipc.h>
|
|
54 #include <sys/shm.h>
|
|
55 #include <X11/Xlib.h>
|
|
56 #include <X11/Xutil.h>
|
|
57 #include <X11/extensions/XShm.h>
|
|
58 #include <X11/extensions/Xvlib.h>
|
|
59 #ifndef USE_XVIMAGES
|
|
60 #include "Hermes/Hermes.h"
|
|
61 #endif
|
|
62
|
|
63 #include <lirc/lirc_client.h>
|
|
64
|
|
65 /* Some BT848/BT878 cards have gain control hardware which takes
|
|
66 1 or 2 seconds to settle. If you get a washed out / too bright
|
|
67 image, change the GRABBER_SETTLE_TIME from 0 to either 1 or 2
|
|
68 */
|
|
69 /*#define GRABBER_SETTLE_TIME 10*/
|
|
70 #define GRABBER_SETTLE_TIME 0
|
|
71
|
|
72
|
|
73 #define PAL 1
|
|
74 #define NTSC 2
|
|
75
|
|
76 /* PAL is 768 x 576. NTSC is 640 x 480 */
|
|
77 #define PAL_HEIGHT 576
|
|
78 #define NTSC_HEIGHT 480
|
|
79
|
3
|
80 int bktr_fd;
|
|
81 int tuner_fd;
|
|
82 unsigned char *bktr_buffer;
|
1
|
83 #if 1
|
3
|
84 int width = 320;
|
|
85 int height = 240;
|
1
|
86 #else
|
3
|
87 int width = 640;
|
|
88 int height = 480;
|
1
|
89 #endif
|
|
90
|
|
91 #ifndef USE_XVIMAGES
|
3
|
92 static XImage *rgb_image;
|
|
93 static Pixmap pmap;
|
1
|
94 #endif
|
|
95 static XShmSegmentInfo shminfo;
|
|
96 static Display *disp;
|
3
|
97 static Window win;
|
|
98 static Visual *vis;
|
|
99 static int scr;
|
|
100 static GC gc;
|
|
101 static int depth;
|
|
102 static Colormap cmap;
|
1
|
103 static XVisualInfo *vi;
|
3
|
104 static int bits_per_pixel;
|
1
|
105 static XvImage *yuv_image;
|
|
106 static XvAdaptorInfo *xv_adaptors;
|
3
|
107 static int xv_num_adaptors;
|
|
108 static int xv_adaptor;
|
|
109 static int xv_format_id;
|
1
|
110
|
3
|
111 static int channel;
|
1
|
112
|
|
113 #define DO_IOCTL_GERR(str) fprintf(stderr, "ioctl(%s) failed: %s\n", \
|
|
114 str, strerror(errno) )
|
|
115 #define DO_IOCTL_SERR(str,arg) fprintf(stderr, "ioctl(%s, %ld) failed: %s\n",\
|
|
116 str, (long)arg, strerror(errno) )
|
|
117 /*--------------------------------------------------------------------------*/
|
|
118
|
3
|
119 void
|
|
120 Close() {
|
|
121 close(tuner_fd);
|
|
122 close(bktr_fd);
|
1
|
123 }
|
|
124
|
3
|
125 void
|
|
126 Open() {
|
|
127 struct meteor_geomet geo;
|
|
128 int buffer_size, format, source, c;
|
|
129 char *device_name;
|
1
|
130
|
3
|
131 format = PAL; /* default value */
|
|
132 source = 1; /* default value */
|
|
133 device_name = "/dev/bktr0"; /* default value */
|
1
|
134
|
3
|
135 /* Open the Meteor or Bt848/Bt878 grabber */
|
|
136 if ((bktr_fd = open(device_name, O_RDONLY)) < 0) {
|
|
137 printf("open failed: %d\n", errno);
|
|
138 exit(1);
|
|
139 }
|
|
140 if ((tuner_fd = open("/dev/tuner0", O_RDONLY)) < 0) {
|
|
141 printf("open failed: %d\n", errno);
|
|
142 exit(1);
|
|
143 }
|
|
144 /* set up the capture type and size */
|
|
145 geo.rows = height;
|
|
146 geo.columns = width;
|
|
147 geo.frames = 1;
|
|
148 #ifdef USE_XVIMAGES
|
|
149 /* Should be YUV_12, but 422 actually gives a synced picture. Though */
|
|
150 geo.oformat = METEOR_GEO_YUV_422 | METEOR_GEO_YUV_12;
|
1
|
151 #else
|
3
|
152 geo.oformat = METEOR_GEO_RGB24;
|
1
|
153 #endif
|
|
154
|
3
|
155 /* switch from interlaced capture to single field capture if */
|
|
156 /* the grab height is less than half the normal TV height */
|
|
157 /* this gives better quality captures when the object in the TV */
|
|
158 /* picture is moving */
|
|
159 if ((format == PAL) && (height <= (PAL_HEIGHT / 2)))
|
|
160 geo.oformat |= METEOR_GEO_ODD_ONLY;
|
|
161 if ((format == NTSC) && (height <= (NTSC_HEIGHT / 2)))
|
|
162 geo.oformat |= METEOR_GEO_ODD_ONLY;
|
1
|
163
|
3
|
164 if (ioctl(bktr_fd, METEORSETGEO, &geo) < 0) {
|
|
165 printf("METEORSETGEO ioctl failed: %d\n", errno);
|
|
166 exit(1);
|
|
167 }
|
|
168 /* Select PAL or NTSC */
|
|
169 switch (format) {
|
|
170 case PAL:
|
|
171 c = METEOR_FMT_PAL;
|
|
172 break;
|
|
173 case NTSC:
|
|
174 c = METEOR_FMT_NTSC;
|
|
175 break;
|
|
176 default:
|
|
177 c = METEOR_FMT_NTSC;
|
|
178 break;
|
|
179 }
|
1
|
180
|
3
|
181 c = BT848_IFORM_F_PALBDGHI;
|
|
182 if (ioctl(bktr_fd, BT848SFMT, &c) < 0) {
|
|
183 DO_IOCTL_SERR("BT848SFMT", c);
|
|
184 return;
|
|
185 }
|
|
186 c = AUDIO_TUNER;
|
|
187 if (ioctl(tuner_fd, BT848_SAUDIO, &c) < 0) {
|
|
188 DO_IOCTL_SERR("BT848SFMT", c);
|
|
189 return;
|
|
190 }
|
|
191 c = CHNLSET_AUSTRALIA;
|
|
192 if (ioctl(tuner_fd, TVTUNER_SETTYPE, &c) < 0) {
|
|
193 DO_IOCTL_SERR("TVTUNER_SETTYPE", c);
|
|
194 return;
|
|
195 }
|
|
196 if (ioctl(tuner_fd, TVTUNER_SETCHNL, &channel) < 0) {
|
|
197 DO_IOCTL_SERR("TVTUNER_SETCHNL", channel);
|
|
198 return;
|
|
199 }
|
|
200 /* Select the Video Source */
|
|
201 /* Video In, Tuner, S-Video */
|
|
202 switch (source) {
|
|
203 case 0:
|
|
204 c = METEOR_INPUT_DEV0;
|
|
205 break;
|
|
206 case 1:
|
|
207 c = METEOR_INPUT_DEV1;
|
|
208 break;
|
|
209 case 2:
|
|
210 c = METEOR_INPUT_DEV2;
|
|
211 break;
|
|
212 case 3:
|
|
213 c = METEOR_INPUT_DEV3;
|
|
214 break;
|
|
215 default:
|
|
216 c = METEOR_INPUT_DEV0;
|
|
217 break;
|
|
218 }
|
1
|
219
|
3
|
220 printf("Input - %x\n", c);
|
|
221 if (ioctl(bktr_fd, METEORSINPUT, &c) < 0) {
|
|
222 printf("ioctl failed: %d\n", errno);
|
|
223 exit(1);
|
|
224 }
|
|
225 /* Use mmap to Map the drivers grab buffer */
|
|
226 buffer_size = width * height * 4; /* R,G,B,spare */
|
|
227 bktr_buffer = (unsigned char *)mmap((caddr_t) 0, buffer_size, PROT_READ,
|
|
228 MAP_SHARED, bktr_fd, (off_t) 0);
|
1
|
229
|
3
|
230 if (bktr_buffer == (unsigned char *)MAP_FAILED)
|
|
231 exit(1);
|
1
|
232
|
|
233
|
3
|
234 /* We may need to wait for a short time to allow the grabber */
|
|
235 /* brightness to settle down */
|
|
236 sleep(GRABBER_SETTLE_TIME);
|
1
|
237 }
|
|
238
|
|
239 /*--------------------------------------------------------------------------*/
|
|
240
|
3
|
241 void
|
|
242 Capture() {
|
|
243 int c;
|
|
244
|
|
245 /* Perform a single frame capture */
|
|
246 c = METEOR_CAP_SINGLE;
|
|
247 ioctl(bktr_fd, METEORCAPTUR, &c);
|
1
|
248 }
|
|
249
|
|
250 /*--------------------------------------------------------------------------*/
|
|
251
|
3
|
252 void
|
|
253 SaveImage() {
|
|
254 unsigned char *line_buffer;
|
|
255 int o , w, h;
|
|
256 unsigned char *p;
|
|
257 unsigned char header[30];
|
|
258 char *filename = "t.ppm" /* argv[3] */ ;
|
1
|
259
|
3
|
260 /* Create the output file */
|
|
261 if ((o = open(filename, O_WRONLY | O_CREAT, 0644)) < 0) {
|
|
262 printf("ppm open failed: %d\n", errno);
|
|
263 exit(1);
|
|
264 }
|
|
265 /* make PPM header and save to file */
|
|
266 sprintf(header, "P6\n%d\n%d\n255\n", width, height);
|
|
267 write(o, header, strlen(header));
|
1
|
268
|
3
|
269 /* save the RGB data to PPM file */
|
|
270 /* save this one line at a time */
|
|
271 line_buffer = (unsigned char *)malloc(width * 3 * sizeof(unsigned char));
|
|
272 p = bktr_buffer;
|
|
273 for (h = 0; h < height; h++) {
|
|
274 for (w = 0; w < width; w++) {
|
|
275 line_buffer[(w * 3) + 2] = *p++; /* blue */
|
|
276 line_buffer[(w * 3) + 1] = *p++; /* green */
|
|
277 line_buffer[(w * 3) + 0] = *p++; /* red */
|
|
278 p++; /* NULL byte */
|
|
279 }
|
|
280 write(o, line_buffer, width * 3);
|
1
|
281 }
|
3
|
282 close(o);
|
|
283 free(line_buffer);
|
1
|
284 }
|
|
285
|
3
|
286 void
|
|
287 X_ShowCursor(void) {
|
|
288 XDefineCursor(disp, win, 0);
|
1
|
289 }
|
|
290
|
3
|
291 void
|
|
292 X_HideCursor(void) {
|
|
293 Cursor no_ptr;
|
|
294 Pixmap bm_no;
|
|
295 XColor black , dummy;
|
|
296 Colormap colormap;
|
|
297 static unsigned char bm_no_data[] = {0, 0, 0, 0, 0, 0, 0, 0};
|
1
|
298
|
3
|
299 colormap = DefaultColormap(disp, DefaultScreen(disp));
|
|
300 XAllocNamedColor(disp, colormap, "black", &black, &dummy);
|
|
301 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
|
|
302 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
|
|
303
|
|
304 XDefineCursor(disp, win, no_ptr);
|
|
305 XFreeCursor(disp, no_ptr);
|
|
306 if (bm_no != None)
|
|
307 XFreePixmap(disp, bm_no);
|
|
308
|
1
|
309 }
|
|
310
|
|
311 /*
|
|
312 * Sends the EWMH fullscreen state event.
|
3
|
313 *
|
1
|
314 * action: could be on of _NET_WM_STATE_REMOVE -- remove state
|
|
315 * _NET_WM_STATE_ADD -- add state
|
|
316 * _NET_WM_STATE_TOGGLE -- toggle
|
|
317 */
|
3
|
318 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
|
|
319 #define _NET_WM_STATE_ADD 1 /* add/set property */
|
|
320 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
|
1
|
321 void
|
|
322 vo_x11_ewmh_fullscreen(int action) {
|
3
|
323 XEvent xev;
|
1
|
324
|
3
|
325 assert(action == _NET_WM_STATE_REMOVE ||
|
|
326 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
|
1
|
327
|
|
328
|
3
|
329 /* init X event structure for _NET_WM_FULLSCREEN client msg */
|
|
330 xev.xclient.type = ClientMessage;
|
|
331 xev.xclient.serial = 0;
|
|
332 xev.xclient.send_event = True;
|
|
333 xev.xclient.message_type = XInternAtom(disp, "_NET_WM_STATE", False);
|
|
334 xev.xclient.window = win;
|
|
335 xev.xclient.format = 32;
|
|
336 xev.xclient.data.l[0] = action;
|
|
337 xev.xclient.data.l[1] = XInternAtom(disp, "_NET_WM_STATE_FULLSCREEN", False);
|
|
338 xev.xclient.data.l[2] = 0;
|
|
339 xev.xclient.data.l[3] = 0;
|
|
340 xev.xclient.data.l[4] = 0;
|
1
|
341
|
3
|
342 /* finally send that damn thing */
|
|
343 if (!XSendEvent(disp, DefaultRootWindow(disp), False,
|
|
344 SubstructureRedirectMask | SubstructureNotifyMask,
|
|
345 &xev)) {
|
|
346 fprintf(stderr, "Failed to send fullscreen command\n");
|
|
347 }
|
1
|
348 }
|
|
349
|
|
350
|
3
|
351 void
|
|
352 X_Setup(int w, int h) {
|
|
353 XGCValues gcvals;
|
|
354 Window root;
|
|
355 XVisualInfo vinfo_pref;
|
|
356 int num_vis;
|
|
357 XPixmapFormatValues *pf;
|
|
358 int num_pf , pfi, i, j;
|
5
|
359 XSizeHints sz_hint;
|
|
360
|
3
|
361 disp = XOpenDisplay(NULL);
|
|
362 if (!disp) {
|
|
363 fprintf(stderr, "X-Error: unable to connect to display\n");
|
|
364 exit(1);
|
|
365 }
|
|
366 XSynchronize(disp, True);
|
|
367
|
|
368 scr = DefaultScreen(disp);
|
|
369 vis = DefaultVisual(disp, scr);
|
|
370 root = DefaultRootWindow(disp);
|
|
371 depth = DefaultDepth(disp, scr);
|
1
|
372
|
3
|
373 vinfo_pref.screen = scr;
|
|
374 vinfo_pref.visualid = XVisualIDFromVisual(vis);
|
|
375 vi = XGetVisualInfo(disp, VisualScreenMask | VisualIDMask,
|
|
376 &vinfo_pref, &num_vis);
|
|
377 assert(num_vis == 1);
|
|
378
|
|
379 win = XCreateSimpleWindow(disp, root, 0, 0, w, h, 0, 0, 0);
|
|
380 gc = XCreateGC(disp, win, (unsigned long)0, &gcvals);
|
|
381 XSetForeground(disp, gc, 0);
|
|
382 XSetBackground(disp, gc, 1);
|
1
|
383
|
3
|
384 XMapWindow(disp, win);
|
|
385 cmap = DefaultColormap(disp, scr);
|
5
|
386
|
|
387 sz_hint.flags = PAspect;
|
|
388 sz_hint.min_aspect.x = width;
|
|
389 sz_hint.min_aspect.y = height;
|
|
390 sz_hint.max_aspect.x = width;
|
|
391 sz_hint.max_aspect.y = height;
|
|
392
|
|
393 /* set min height/width to 4 to avoid off by one errors */
|
|
394 sz_hint.min_width = sz_hint.min_height = 4;
|
|
395 sz_hint.flags |= PMinSize;
|
|
396 XSetWMNormalHints(disp, win, &sz_hint);
|
|
397
|
3
|
398 XSync(disp, False);
|
1
|
399
|
3
|
400 /* Setup with Xv extension */
|
|
401 xv_adaptor = -1;
|
|
402 xv_format_id = -1;
|
|
403 XvQueryAdaptors(disp, root, &xv_num_adaptors, &xv_adaptors);
|
|
404 for (i = 0; i < xv_num_adaptors; i++) {
|
|
405 XvAdaptorInfo *adaptor = &xv_adaptors[i];
|
|
406 int takes_images;
|
1
|
407
|
3
|
408 takes_images = adaptor->type & (XvInputMask | XvImageMask);
|
|
409 if (takes_images) {
|
|
410 XvImageFormatValues *formats;
|
|
411 int num_formats;
|
1
|
412
|
3
|
413 formats = XvListImageFormats(disp, adaptor->base_id, &num_formats);
|
|
414 for (j = 0; j < num_formats; j++)
|
|
415 if (formats[j].type == XvYUV && formats[j].format == XvPlanar &&
|
|
416 strcmp(formats[j].guid, "YV12") == 0)
|
|
417 break;
|
|
418 if (j < num_formats) {
|
|
419 xv_adaptor = i;
|
|
420 xv_format_id = formats[j].id;
|
|
421 break;
|
|
422 }
|
|
423 }
|
1
|
424 }
|
3
|
425 assert(xv_adaptor >= 0);
|
1
|
426
|
3
|
427 /* Create an image to captured frames */
|
1
|
428 #ifdef USE_XVIMAGES
|
5
|
429 yuv_image = XvShmCreateImage(disp, xv_adaptors[xv_adaptor].base_id,
|
|
430 xv_format_id, 0, w, h, &shminfo);
|
3
|
431 if (!yuv_image)
|
|
432 fprintf(stderr, "X-Error: unable to create XvShm XImage\n");
|
1
|
433
|
3
|
434 shminfo.shmid = shmget(IPC_PRIVATE, yuv_image->data_size, IPC_CREAT | 0777);
|
|
435 if (shminfo.shmid == -1)
|
|
436 fprintf(stderr, "SharedMemory Error: unable to get identifier\n");
|
1
|
437
|
3
|
438 shminfo.shmaddr = yuv_image->data = shmat(shminfo.shmid, 0, 0);
|
5
|
439 yuv_image->data = shminfo.shmaddr;
|
1
|
440 #else
|
3
|
441 rgb_image = XShmCreateImage(disp, vis, depth, ZPixmap, NULL, &shminfo, w, h);
|
|
442 if (!rgb_image)
|
|
443 fprintf(stderr, "X-Error: unable to create XShm XImage\n");
|
1
|
444
|
3
|
445 shminfo.shmid = shmget(IPC_PRIVATE,
|
|
446 rgb_image->bytes_per_line * rgb_image->height,
|
|
447 IPC_CREAT | 0777);
|
|
448 if (shminfo.shmid == -1)
|
|
449 fprintf(stderr, "SharedMemory Error: unable to get identifier\n");
|
1
|
450
|
3
|
451 shminfo.shmaddr = rgb_image->data = shmat(shminfo.shmid, 0, 0);
|
1
|
452 #endif
|
|
453
|
3
|
454 if (!XShmAttach(disp, &shminfo))
|
|
455 fprintf(stderr, "X-Error: unable to attach XShm Shared Memory Segment\n");
|
1
|
456
|
3
|
457 /* Create a pixmap for the window background */
|
1
|
458 #ifdef OLD
|
3
|
459 pmap = XShmCreatePixmap(disp, win, shminfo.shmaddr, &shminfo, w, h, depth);
|
|
460 if (!pmap)
|
|
461 fprintf(stderr, "Unable to create pixmap\n");
|
1
|
462 #endif
|
|
463
|
3
|
464 /* Determine bits-per-pixel for pixmaps */
|
|
465 pf = XListPixmapFormats(disp, &num_pf);
|
|
466 assert(pf);
|
|
467 for (pfi = 0; pfi < num_pf; pfi++)
|
|
468 if (pf[pfi].depth == vi->depth)
|
|
469 break;
|
|
470 assert(pfi < num_pf);
|
|
471 bits_per_pixel = pf[pfi].bits_per_pixel;
|
|
472 XFree(pf);
|
1
|
473
|
|
474 #ifdef OLD
|
3
|
475 XSetWindowBackgroundPixmap(disp, win, pmap);
|
1
|
476 #endif
|
|
477 }
|
|
478
|
|
479 /*--------------------------------------------------------------------------*/
|
|
480
|
3
|
481 void
|
|
482 X_Shutdown() {
|
5
|
483 #ifdef USE_XVIMAGES
|
3
|
484 XShmDetach(disp, &shminfo);
|
5
|
485 shmctl(shminfo.shmid, IPC_RMID, 0);
|
|
486 shmdt(shminfo.shmaddr);
|
|
487 XFree(yuv_image);
|
1
|
488 #else
|
3
|
489 XDestroyImage(rgb_image);
|
1
|
490 #endif
|
3
|
491 shmdt(shminfo.shmaddr);
|
|
492 shmctl(shminfo.shmid, IPC_RMID, 0);
|
1
|
493 }
|
|
494
|
|
495
|
|
496 /*--------------------------------------------------------------------------*/
|
|
497
|
3
|
498 void
|
|
499 X_Display(void) {
|
|
500 int _w , _h, _d;
|
|
501 Window _dw;
|
1
|
502
|
|
503 #ifdef USE_XVIMAGES
|
3
|
504 XGetGeometry(disp, win, &_dw, &_d, &_d, &_w, &_h, &_d, &_d);
|
|
505 XvShmPutImage(disp, xv_adaptors[xv_adaptor].base_id, win,
|
|
506 gc, yuv_image, 0, 0, yuv_image->width, yuv_image->height,
|
|
507 0, 0, _w, _h, True);
|
1
|
508 #else
|
3
|
509 XShmPutImage(disp, win, gc, rgb_image, 0, 0, 0, 0,
|
|
510 rgb_image->width, rgb_image->height, False);
|
1
|
511 #endif
|
3
|
512 XSync(disp, False);
|
1
|
513 }
|
|
514
|
|
515 #define CMD_NONE 0
|
|
516 #define CMD_CHNUP 1
|
|
517 #define CMD_CHNDN 2
|
|
518 #define CMD_MUTE 3
|
|
519 #define CMD_QUIT 4
|
|
520 #define CMD_RELOAD 5
|
|
521 #define CMD_CURSOR 6
|
|
522 #define CMD_VOLDN 7
|
|
523 #define CMD_VOLUP 8
|
|
524 #define CMD_FSTOGGLE 9
|
|
525
|
|
526 /*--------------------------------------------------------------------------*/
|
|
527
|
3
|
528 int
|
|
529 main(int argc, char *argv[]) {
|
1
|
530 #ifndef USE_XVIMAGES
|
3
|
531 HermesHandle conv;
|
|
532 HermesFormat fmt_source, fmt_dest;
|
1
|
533 #endif
|
3
|
534 XEvent e;
|
|
535 char *scratch_buf, *code, *c, *mixerdev;
|
|
536 int frames , channelidx, oldchan, mute, cursor, fd, ret, cmd;
|
|
537 int exitnow , mfd, uselirc, curvol;
|
|
538 struct timeval then, now, diff, lastmove;
|
|
539 float rate;
|
|
540 KeySym key;
|
|
541 char text [255];
|
|
542 int channellist[] = {2, 7, 9, 10, 28};
|
|
543 struct lirc_config *config;
|
|
544 struct pollfd fds[1];
|
1
|
545
|
3
|
546 #define NUMCHANS (sizeof(channellist) / sizeof(channellist[0]))
|
1
|
547
|
3
|
548 channelidx = mute = cursor = 0;
|
1
|
549
|
3
|
550 oldchan = channel = channellist[channelidx];
|
1
|
551
|
|
552 #ifndef USE_XVIMAGES
|
3
|
553 if (!Hermes_Init()) {
|
|
554 printf("Couldn't initialise Hermes!\n");
|
|
555 exit(1);
|
|
556 }
|
|
557 conv = Hermes_ConverterInstance(HERMES_CONVERT_NORMAL);
|
|
558 if (!conv) {
|
|
559 printf("Could not obtain converter instance from Hermes!\n");
|
|
560 exit(1);
|
|
561 }
|
1
|
562 #endif
|
|
563
|
3
|
564 X_Setup(width, height);
|
|
565 XSelectInput(disp, win, KeyReleaseMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask);
|
1
|
566
|
3
|
567 X_HideCursor();
|
1
|
568
|
3
|
569 /* Open Capture device */
|
|
570 Open();
|
1
|
571
|
3
|
572 /* Open audio mixer */
|
|
573 mixerdev = "/dev/mixer";
|
|
574 if ((mfd = open(mixerdev, O_RDWR)) == -1)
|
|
575 fprintf(stderr, "Unable to open %s - %s\n", mixerdev, strerror(errno));
|
1
|
576
|
3
|
577 /* Talk to LIRC */
|
|
578 if ((fd = lirc_init(basename(argv[0]), 1)) == -1) {
|
|
579 fprintf(stderr, "Unable to init lirc client library\n");
|
|
580 uselirc = 0;
|
|
581 } else {
|
|
582 if (lirc_readconfig(NULL, &config, NULL) != 0) {
|
|
583 fprintf(stderr, "Unable to parse config file\n");
|
|
584 uselirc = 0;
|
|
585 } else
|
|
586 uselirc = 1;
|
|
587 fds[0].fd = fd;
|
|
588 fds[0].events = POLLRDNORM;
|
1
|
589 }
|
|
590
|
3
|
591 #ifndef USE_XVIMAGES
|
|
592 /* Conversion from and to formats */
|
|
593 fmt_source.indexed = 0;
|
|
594 fmt_source.bits = 32;
|
|
595 fmt_source.r = 0xff0000;
|
|
596 fmt_source.g = 0x00ff00;
|
|
597 fmt_source.b = 0x0000ff;
|
|
598 fmt_source.a = 0;
|
|
599
|
|
600 fmt_dest.indexed = 0;
|
|
601 fmt_dest.bits = bits_per_pixel;
|
|
602 fmt_dest.r = vi->red_mask;
|
|
603 fmt_dest.g = vi->green_mask;
|
|
604 fmt_dest.b = vi->blue_mask;
|
|
605 fmt_dest.a = 0;
|
|
606 #else
|
|
607 scratch_buf = malloc(width * height);
|
|
608 #endif
|
|
609
|
|
610 frames = 0;
|
|
611 gettimeofday(&then, NULL);
|
|
612 gettimeofday(&lastmove, NULL);
|
|
613
|
|
614 exitnow = 0;
|
|
615
|
|
616 /* Capture loop */
|
|
617 while (1) {
|
|
618 if (frames == 50) {
|
|
619 gettimeofday(&now, NULL);
|
|
620 timersub(&now, &then, &diff);
|
|
621
|
|
622 rate = (float)frames / (float)(diff.tv_usec / 1000000.0 + diff.tv_sec);
|
|
623
|
|
624 printf("%d frames in %.2f seconds, rate %.2f\n", frames, (float)(diff.tv_usec / 1000000.0) + diff.tv_sec, rate);
|
|
625 gettimeofday(&then, NULL);
|
|
626 frames = 0;
|
|
627 XResetScreenSaver(disp);
|
1
|
628 }
|
3
|
629 frames++;
|
1
|
630
|
3
|
631 timersub(&now, &lastmove, &diff);
|
|
632 if (((float)diff.tv_usec / 1000000.0 + (float)diff.tv_sec) > 2.0) {
|
|
633 if (cursor) {
|
|
634 X_HideCursor();
|
|
635 cursor = 0;
|
|
636 }
|
|
637 } else {
|
|
638 if (!cursor) {
|
|
639 X_ShowCursor();
|
|
640 cursor = 1;
|
|
641 }
|
|
642 }
|
|
643
|
|
644 if (XCheckMaskEvent(disp, PointerMotionMask, &e) && e.type == MotionNotify) {
|
|
645 gettimeofday(&lastmove, NULL);
|
|
646 }
|
|
647 cmd = CMD_NONE;
|
1
|
648
|
3
|
649 if (XCheckMaskEvent(disp, ButtonReleaseMask, &e)) {
|
|
650 printf("e.type = %d\n", e.type);
|
|
651 cmd = CMD_MUTE;
|
|
652 }
|
|
653 if (XCheckMaskEvent(disp, KeyReleaseMask, &e) && e.type == KeyRelease) {
|
|
654 gettimeofday(&lastmove, NULL);
|
|
655
|
|
656 XLookupString(&e.xkey, text, 255, &key, 0);
|
|
657 printf("Press - %c\n", text[0]);
|
1
|
658
|
3
|
659 switch (text[0]) {
|
|
660 case 'q':
|
|
661 cmd = CMD_QUIT;
|
|
662 break;
|
|
663
|
|
664 case '=':
|
|
665 case '+':
|
1
|
666 cmd = CMD_CHNUP;
|
3
|
667 break;
|
|
668
|
|
669 case '-':
|
1
|
670 cmd = CMD_CHNDN;
|
3
|
671 break;
|
|
672
|
|
673 case 'h':
|
|
674 cmd = CMD_CURSOR;
|
|
675 break;
|
|
676
|
|
677 case 'm':
|
|
678 cmd = CMD_MUTE;
|
|
679 break;
|
|
680
|
|
681 case 'r':
|
|
682 cmd = CMD_RELOAD;
|
|
683 break;
|
|
684
|
|
685 case ',':
|
|
686 cmd = CMD_VOLDN;
|
|
687 break;
|
|
688
|
|
689 case '.':
|
1
|
690 cmd = CMD_VOLUP;
|
3
|
691 break;
|
|
692
|
|
693 case 'f':
|
1
|
694 cmd = CMD_FSTOGGLE;
|
3
|
695 break;
|
1
|
696 }
|
3
|
697 }
|
|
698 /* Poll for IR events */
|
|
699 if (uselirc) {
|
|
700 fds[0].revents = 0;
|
|
701
|
|
702 while (1) {
|
|
703 if (poll(fds, 1, 0) == -1) {
|
|
704 fprintf(stderr, "Poll failed - %s\n", strerror(errno));
|
|
705 exit(EXIT_FAILURE);
|
|
706 }
|
|
707 if ((fds[0].revents & POLLRDNORM) != 0) {
|
|
708 fprintf(stderr, "Processing IR..\n");
|
|
709
|
|
710 if (lirc_nextcode(&code) == 0) {
|
|
711 if (code == NULL)
|
|
712 continue;
|
|
713
|
|
714 while ((ret = lirc_code2char(config, code, &c)) == 0 &&
|
|
715 c != NULL) {
|
|
716 fprintf(stderr, "Got command \"%s\"\n", c);
|
1
|
717
|
3
|
718 if (!strcmp(c, "Mute"))
|
|
719 cmd = CMD_MUTE;
|
|
720 else if (!strcmp(c, "CH_UP"))
|
|
721 cmd = CMD_CHNUP;
|
|
722 else if (!strcmp(c, "CH_DOWN"))
|
|
723 cmd = CMD_CHNDN;
|
|
724 else if (!strcmp(c, "Power"))
|
|
725 cmd = CMD_QUIT;
|
|
726 else if (!strcmp(c, "VOL_UP"))
|
|
727 cmd = CMD_VOLUP;
|
|
728 else if (!strcmp(c, "VOL_DOWN"))
|
|
729 cmd = CMD_VOLDN;
|
|
730 else if (!strcmp(c, "AV/TV"))
|
|
731 cmd = CMD_FSTOGGLE;
|
|
732
|
|
733 free(code);
|
|
734 }
|
|
735 }
|
|
736 } else
|
|
737 break;
|
|
738 }
|
1
|
739 }
|
3
|
740 switch (cmd) {
|
|
741 case CMD_QUIT:
|
|
742 exitnow = 1;
|
|
743 break;
|
|
744
|
|
745 case CMD_CHNUP:
|
|
746 case CMD_CHNDN:
|
|
747 if (cmd == CMD_CHNUP)
|
|
748 channelidx++;
|
|
749 else
|
|
750 channelidx--;
|
|
751
|
|
752 if (channelidx < 0)
|
|
753 channelidx = NUMCHANS - 1;
|
|
754 if (channelidx > NUMCHANS - 1)
|
|
755 channelidx = 0;
|
|
756
|
|
757 channel = channellist[channelidx];
|
1
|
758
|
3
|
759 printf("Channel - %d\n", channel);
|
|
760 if (oldchan != channel) {
|
|
761 oldchan = channel;
|
|
762
|
|
763 if (ioctl(tuner_fd, TVTUNER_SETCHNL, &channel) < 0) {
|
|
764 DO_IOCTL_SERR("TVTUNER_SETCHNL", channel);
|
|
765 exit(1);
|
|
766 }
|
|
767 }
|
|
768 break;
|
|
769
|
|
770 case CMD_MUTE:
|
|
771 if (mute)
|
|
772 mute = 0;
|
|
773 else
|
|
774 mute = 1;
|
|
775
|
|
776 printf("Mute - %d\n", mute);
|
|
777 if (ioctl(tuner_fd, BT848_SAUDIO, &mute) < 0) {
|
|
778 DO_IOCTL_SERR("BT848_SAUDIO", mute);
|
|
779 exit(1);
|
|
780 }
|
|
781 break;
|
1
|
782
|
3
|
783 case CMD_CURSOR:
|
|
784 if (cursor) {
|
|
785 printf("Cursor hidden\n");
|
|
786 X_HideCursor();
|
|
787 cursor = 0;
|
|
788 } else {
|
|
789 printf("Cursor revealed\n");
|
|
790 X_ShowCursor();
|
|
791 cursor = 1;
|
|
792 }
|
|
793 break;
|
|
794
|
|
795 case CMD_RELOAD:
|
|
796 printf("Reloading\n");
|
|
797 Close();
|
|
798 Open();
|
|
799 break;
|
|
800
|
|
801 case CMD_VOLUP:
|
|
802 case CMD_VOLDN:
|
|
803 if (ioctl(mfd, MIXER_READ(SOUND_MIXER_VOLUME), &curvol) == -1) {
|
|
804 fprintf(stderr, "Unable to read current volume - %s\n", strerror(errno));
|
|
805 break;
|
|
806 }
|
|
807 curvol = curvol & 0x7f;
|
|
808
|
|
809 if (cmd == CMD_VOLUP)
|
|
810 curvol += 5;
|
|
811 else
|
|
812 curvol -= 5;
|
|
813
|
|
814 if (curvol < 0)
|
|
815 curvol = 0;
|
|
816 if (curvol > 100)
|
|
817 curvol = 100;
|
|
818
|
|
819 printf("Setting volume to %d\n", curvol);
|
|
820 curvol |= curvol << 8;
|
1
|
821
|
3
|
822 if (ioctl(mfd, MIXER_WRITE(SOUND_MIXER_VOLUME), &curvol) == -1) {
|
|
823 fprintf(stderr, "Unable to write volume - %s\n", strerror(errno));
|
|
824 break;
|
|
825 }
|
|
826 break;
|
|
827
|
|
828 case CMD_FSTOGGLE:
|
|
829 vo_x11_ewmh_fullscreen(_NET_WM_STATE_TOGGLE);
|
|
830 break;
|
|
831
|
|
832 }
|
|
833
|
|
834 Capture();
|
1
|
835
|
3
|
836 #ifdef USE_XVIMAGES
|
|
837 /* bktr's YUV_12 is planar W*H bytes Y, W/2*H/2 bytes U, */
|
|
838 /* W/2*H/2 bytes V. Xv's YV12 is the same with U and V */
|
|
839 /* planes reversed. */
|
|
840 {
|
|
841 int y_off , u_off, v_off;
|
|
842 y_off = 0;
|
|
843 u_off = width * height;
|
|
844 v_off = u_off + width * height / 4;
|
|
845
|
|
846 assert(yuv_image->data_size == width * height * 3 / 2);
|
|
847 memcpy(yuv_image->data, bktr_buffer, u_off - y_off);
|
|
848 memcpy(yuv_image->data + u_off, bktr_buffer + v_off, v_off - u_off);
|
|
849 memcpy(yuv_image->data + v_off, bktr_buffer + u_off, v_off - u_off);
|
|
850 }
|
|
851 #else
|
|
852 /* SaveImage(); */
|
|
853
|
|
854 Hermes_ConverterRequest(conv, &fmt_source, &fmt_dest);
|
|
855 Hermes_ConverterCopy(conv, bktr_buffer, 0, 0, width, height, width * 4,
|
|
856 rgb_image->data, 0, 0, width, height,
|
|
857 rgb_image->bytes_per_line);
|
|
858 #endif
|
|
859
|
|
860 X_Display();
|
|
861 if (exitnow) {
|
|
862 printf("quitting\n");
|
|
863 break;
|
|
864 }
|
1
|
865 }
|
|
866
|
3
|
867 #ifndef USE_XVIMAGES
|
|
868 Hermes_ConverterReturn(conv);
|
|
869 Hermes_Done();
|
1
|
870 #endif
|
|
871
|
3
|
872 X_Shutdown();
|
|
873 lirc_freeconfig(config);
|
|
874 lirc_deinit();
|
1
|
875
|
3
|
876 return 0;
|
1
|
877 }
|