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>
|
|
49 #include <machine/ioctl_bt848.h>
|
|
50 #include <machine/ioctl_meteor.h>
|
|
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 */
|
1
|
150
|
3
|
151 /* geo.oformat = METEOR_GEO_YUV_12; */
|
|
152 geo.oformat = METEOR_GEO_YUV_422 | METEOR_GEO_YUV_12;
|
|
153 /* geo.oformat = METEOR_GEO_YUV_12; */
|
1
|
154 #else
|
3
|
155 geo.oformat = METEOR_GEO_RGB24;
|
1
|
156 #endif
|
|
157
|
3
|
158 /* switch from interlaced capture to single field capture if */
|
|
159 /* the grab height is less than half the normal TV height */
|
|
160 /* this gives better quality captures when the object in the TV */
|
|
161 /* picture is moving */
|
|
162 if ((format == PAL) && (height <= (PAL_HEIGHT / 2)))
|
|
163 geo.oformat |= METEOR_GEO_ODD_ONLY;
|
|
164 if ((format == NTSC) && (height <= (NTSC_HEIGHT / 2)))
|
|
165 geo.oformat |= METEOR_GEO_ODD_ONLY;
|
1
|
166
|
3
|
167 if (ioctl(bktr_fd, METEORSETGEO, &geo) < 0) {
|
|
168 printf("METEORSETGEO ioctl failed: %d\n", errno);
|
|
169 exit(1);
|
|
170 }
|
|
171 /* Select PAL or NTSC */
|
|
172 switch (format) {
|
|
173 case PAL:
|
|
174 c = METEOR_FMT_PAL;
|
|
175 break;
|
|
176 case NTSC:
|
|
177 c = METEOR_FMT_NTSC;
|
|
178 break;
|
|
179 default:
|
|
180 c = METEOR_FMT_NTSC;
|
|
181 break;
|
|
182 }
|
1
|
183
|
3
|
184 c = BT848_IFORM_F_PALBDGHI;
|
|
185 if (ioctl(bktr_fd, BT848SFMT, &c) < 0) {
|
|
186 DO_IOCTL_SERR("BT848SFMT", c);
|
|
187 return;
|
|
188 }
|
|
189 c = AUDIO_TUNER;
|
|
190 if (ioctl(tuner_fd, BT848_SAUDIO, &c) < 0) {
|
|
191 DO_IOCTL_SERR("BT848SFMT", c);
|
|
192 return;
|
|
193 }
|
|
194 c = CHNLSET_AUSTRALIA;
|
|
195 if (ioctl(tuner_fd, TVTUNER_SETTYPE, &c) < 0) {
|
|
196 DO_IOCTL_SERR("TVTUNER_SETTYPE", c);
|
|
197 return;
|
|
198 }
|
|
199 if (ioctl(tuner_fd, TVTUNER_SETCHNL, &channel) < 0) {
|
|
200 DO_IOCTL_SERR("TVTUNER_SETCHNL", channel);
|
|
201 return;
|
|
202 }
|
|
203 /* Select the Video Source */
|
|
204 /* Video In, Tuner, S-Video */
|
|
205 switch (source) {
|
|
206 case 0:
|
|
207 c = METEOR_INPUT_DEV0;
|
|
208 break;
|
|
209 case 1:
|
|
210 c = METEOR_INPUT_DEV1;
|
|
211 break;
|
|
212 case 2:
|
|
213 c = METEOR_INPUT_DEV2;
|
|
214 break;
|
|
215 case 3:
|
|
216 c = METEOR_INPUT_DEV3;
|
|
217 break;
|
|
218 default:
|
|
219 c = METEOR_INPUT_DEV0;
|
|
220 break;
|
|
221 }
|
1
|
222
|
3
|
223 printf("Input - %x\n", c);
|
|
224 if (ioctl(bktr_fd, METEORSINPUT, &c) < 0) {
|
|
225 printf("ioctl failed: %d\n", errno);
|
|
226 exit(1);
|
|
227 }
|
|
228 /* Use mmap to Map the drivers grab buffer */
|
|
229 buffer_size = width * height * 4; /* R,G,B,spare */
|
|
230 bktr_buffer = (unsigned char *)mmap((caddr_t) 0, buffer_size, PROT_READ,
|
|
231 MAP_SHARED, bktr_fd, (off_t) 0);
|
1
|
232
|
3
|
233 if (bktr_buffer == (unsigned char *)MAP_FAILED)
|
|
234 exit(1);
|
1
|
235
|
|
236
|
3
|
237 /* We may need to wait for a short time to allow the grabber */
|
|
238 /* brightness to settle down */
|
|
239 sleep(GRABBER_SETTLE_TIME);
|
1
|
240 }
|
|
241
|
|
242 /*--------------------------------------------------------------------------*/
|
|
243
|
3
|
244 void
|
|
245 Capture() {
|
|
246 int c;
|
|
247
|
|
248 /* Perform a single frame capture */
|
|
249 c = METEOR_CAP_SINGLE;
|
|
250 ioctl(bktr_fd, METEORCAPTUR, &c);
|
1
|
251 }
|
|
252
|
|
253 /*--------------------------------------------------------------------------*/
|
|
254
|
3
|
255 void
|
|
256 SaveImage() {
|
|
257 unsigned char *line_buffer;
|
|
258 int o , w, h;
|
|
259 unsigned char *p;
|
|
260 unsigned char header[30];
|
|
261 char *filename = "t.ppm" /* argv[3] */ ;
|
1
|
262
|
3
|
263 /* Create the output file */
|
|
264 if ((o = open(filename, O_WRONLY | O_CREAT, 0644)) < 0) {
|
|
265 printf("ppm open failed: %d\n", errno);
|
|
266 exit(1);
|
|
267 }
|
|
268 /* make PPM header and save to file */
|
|
269 sprintf(header, "P6\n%d\n%d\n255\n", width, height);
|
|
270 write(o, header, strlen(header));
|
1
|
271
|
3
|
272 /* save the RGB data to PPM file */
|
|
273 /* save this one line at a time */
|
|
274 line_buffer = (unsigned char *)malloc(width * 3 * sizeof(unsigned char));
|
|
275 p = bktr_buffer;
|
|
276 for (h = 0; h < height; h++) {
|
|
277 for (w = 0; w < width; w++) {
|
|
278 line_buffer[(w * 3) + 2] = *p++; /* blue */
|
|
279 line_buffer[(w * 3) + 1] = *p++; /* green */
|
|
280 line_buffer[(w * 3) + 0] = *p++; /* red */
|
|
281 p++; /* NULL byte */
|
|
282 }
|
|
283 write(o, line_buffer, width * 3);
|
1
|
284 }
|
3
|
285 close(o);
|
|
286 free(line_buffer);
|
1
|
287 }
|
|
288
|
3
|
289 void
|
|
290 X_ShowCursor(void) {
|
|
291 XDefineCursor(disp, win, 0);
|
1
|
292 }
|
|
293
|
3
|
294 void
|
|
295 X_HideCursor(void) {
|
|
296 Cursor no_ptr;
|
|
297 Pixmap bm_no;
|
|
298 XColor black , dummy;
|
|
299 Colormap colormap;
|
|
300 static unsigned char bm_no_data[] = {0, 0, 0, 0, 0, 0, 0, 0};
|
1
|
301
|
3
|
302 colormap = DefaultColormap(disp, DefaultScreen(disp));
|
|
303 XAllocNamedColor(disp, colormap, "black", &black, &dummy);
|
|
304 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
|
|
305 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
|
|
306
|
|
307 XDefineCursor(disp, win, no_ptr);
|
|
308 XFreeCursor(disp, no_ptr);
|
|
309 if (bm_no != None)
|
|
310 XFreePixmap(disp, bm_no);
|
|
311
|
1
|
312 }
|
|
313
|
|
314 /*
|
|
315 * Sends the EWMH fullscreen state event.
|
3
|
316 *
|
1
|
317 * action: could be on of _NET_WM_STATE_REMOVE -- remove state
|
|
318 * _NET_WM_STATE_ADD -- add state
|
|
319 * _NET_WM_STATE_TOGGLE -- toggle
|
|
320 */
|
3
|
321 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
|
|
322 #define _NET_WM_STATE_ADD 1 /* add/set property */
|
|
323 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
|
1
|
324 void
|
|
325 vo_x11_ewmh_fullscreen(int action) {
|
3
|
326 XEvent xev;
|
1
|
327
|
3
|
328 assert(action == _NET_WM_STATE_REMOVE ||
|
|
329 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
|
1
|
330
|
|
331
|
3
|
332 /* init X event structure for _NET_WM_FULLSCREEN client msg */
|
|
333 xev.xclient.type = ClientMessage;
|
|
334 xev.xclient.serial = 0;
|
|
335 xev.xclient.send_event = True;
|
|
336 xev.xclient.message_type = XInternAtom(disp, "_NET_WM_STATE", False);
|
|
337 xev.xclient.window = win;
|
|
338 xev.xclient.format = 32;
|
|
339 xev.xclient.data.l[0] = action;
|
|
340 xev.xclient.data.l[1] = XInternAtom(disp, "_NET_WM_STATE_FULLSCREEN", False);
|
|
341 xev.xclient.data.l[2] = 0;
|
|
342 xev.xclient.data.l[3] = 0;
|
|
343 xev.xclient.data.l[4] = 0;
|
1
|
344
|
3
|
345 /* finally send that damn thing */
|
|
346 if (!XSendEvent(disp, DefaultRootWindow(disp), False,
|
|
347 SubstructureRedirectMask | SubstructureNotifyMask,
|
|
348 &xev)) {
|
|
349 fprintf(stderr, "Failed to send fullscreen command\n");
|
|
350 }
|
1
|
351 }
|
|
352
|
|
353
|
3
|
354 void
|
|
355 X_Setup(int w, int h) {
|
|
356 XGCValues gcvals;
|
|
357 Window root;
|
|
358 XVisualInfo vinfo_pref;
|
|
359 int num_vis;
|
|
360 XPixmapFormatValues *pf;
|
|
361 int num_pf , pfi, i, j;
|
|
362
|
|
363 disp = XOpenDisplay(NULL);
|
|
364 if (!disp) {
|
|
365 fprintf(stderr, "X-Error: unable to connect to display\n");
|
|
366 exit(1);
|
|
367 }
|
|
368 XSynchronize(disp, True);
|
|
369
|
|
370 scr = DefaultScreen(disp);
|
|
371 vis = DefaultVisual(disp, scr);
|
|
372 root = DefaultRootWindow(disp);
|
|
373 depth = DefaultDepth(disp, scr);
|
1
|
374
|
3
|
375 vinfo_pref.screen = scr;
|
|
376 vinfo_pref.visualid = XVisualIDFromVisual(vis);
|
|
377 vi = XGetVisualInfo(disp, VisualScreenMask | VisualIDMask,
|
|
378 &vinfo_pref, &num_vis);
|
|
379 assert(num_vis == 1);
|
|
380
|
|
381 win = XCreateSimpleWindow(disp, root, 0, 0, w, h, 0, 0, 0);
|
|
382 gc = XCreateGC(disp, win, (unsigned long)0, &gcvals);
|
|
383 XSetForeground(disp, gc, 0);
|
|
384 XSetBackground(disp, gc, 1);
|
1
|
385
|
3
|
386 XMapWindow(disp, win);
|
|
387 cmap = DefaultColormap(disp, scr);
|
|
388 XSync(disp, False);
|
1
|
389
|
3
|
390 /* Setup with Xv extension */
|
|
391 xv_adaptor = -1;
|
|
392 xv_format_id = -1;
|
|
393 XvQueryAdaptors(disp, root, &xv_num_adaptors, &xv_adaptors);
|
|
394 for (i = 0; i < xv_num_adaptors; i++) {
|
|
395 XvAdaptorInfo *adaptor = &xv_adaptors[i];
|
|
396 int takes_images;
|
1
|
397
|
3
|
398 takes_images = adaptor->type & (XvInputMask | XvImageMask);
|
|
399 if (takes_images) {
|
|
400 XvImageFormatValues *formats;
|
|
401 int num_formats;
|
1
|
402
|
3
|
403 formats = XvListImageFormats(disp, adaptor->base_id, &num_formats);
|
|
404 for (j = 0; j < num_formats; j++)
|
|
405 if (formats[j].type == XvYUV && formats[j].format == XvPlanar &&
|
|
406 strcmp(formats[j].guid, "YV12") == 0)
|
|
407 break;
|
|
408 if (j < num_formats) {
|
|
409 xv_adaptor = i;
|
|
410 xv_format_id = formats[j].id;
|
|
411 break;
|
|
412 }
|
|
413 }
|
1
|
414 }
|
3
|
415 assert(xv_adaptor >= 0);
|
1
|
416
|
3
|
417 /* Create an image to captured frames */
|
1
|
418 #ifdef USE_XVIMAGES
|
3
|
419 yuv_im age = XvShmCreateImage(disp, xv_adaptors[xv_adaptor].base_id,
|
|
420 xv_format_id, 0, w, h, &shminfo);
|
|
421 if (!yuv_image)
|
|
422 fprintf(stderr, "X-Error: unable to create XvShm XImage\n");
|
1
|
423
|
3
|
424 shminfo.shmid = shmget(IPC_PRIVATE, yuv_image->data_size, IPC_CREAT | 0777);
|
|
425 if (shminfo.shmid == -1)
|
|
426 fprintf(stderr, "SharedMemory Error: unable to get identifier\n");
|
1
|
427
|
3
|
428 shminfo.shmaddr = yuv_image->data = shmat(shminfo.shmid, 0, 0);
|
1
|
429 #else
|
3
|
430 rgb_image = XShmCreateImage(disp, vis, depth, ZPixmap, NULL, &shminfo, w, h);
|
|
431 if (!rgb_image)
|
|
432 fprintf(stderr, "X-Error: unable to create XShm XImage\n");
|
1
|
433
|
3
|
434 shminfo.shmid = shmget(IPC_PRIVATE,
|
|
435 rgb_image->bytes_per_line * rgb_image->height,
|
|
436 IPC_CREAT | 0777);
|
|
437 if (shminfo.shmid == -1)
|
|
438 fprintf(stderr, "SharedMemory Error: unable to get identifier\n");
|
1
|
439
|
3
|
440 shminfo.shmaddr = rgb_image->data = shmat(shminfo.shmid, 0, 0);
|
1
|
441 #endif
|
|
442
|
3
|
443 if (!XShmAttach(disp, &shminfo))
|
|
444 fprintf(stderr, "X-Error: unable to attach XShm Shared Memory Segment\n");
|
1
|
445
|
3
|
446 /* Create a pixmap for the window background */
|
1
|
447 #ifdef OLD
|
3
|
448 pmap = XShmCreatePixmap(disp, win, shminfo.shmaddr, &shminfo, w, h, depth);
|
|
449 if (!pmap)
|
|
450 fprintf(stderr, "Unable to create pixmap\n");
|
1
|
451 #endif
|
|
452
|
3
|
453 /* Determine bits-per-pixel for pixmaps */
|
|
454 pf = XListPixmapFormats(disp, &num_pf);
|
|
455 assert(pf);
|
|
456 for (pfi = 0; pfi < num_pf; pfi++)
|
|
457 if (pf[pfi].depth == vi->depth)
|
|
458 break;
|
|
459 assert(pfi < num_pf);
|
|
460 bits_per_pixel = pf[pfi].bits_per_pixel;
|
|
461 XFree(pf);
|
1
|
462
|
|
463 #ifdef OLD
|
3
|
464 XSetWindowBackgroundPixmap(disp, win, pmap);
|
1
|
465 #endif
|
|
466 }
|
|
467
|
|
468 /*--------------------------------------------------------------------------*/
|
|
469
|
3
|
470 void
|
|
471 X_Shutdown() {
|
|
472 XShmDetach(disp, &shminfo);
|
1
|
473 #ifdef USE_XVIMAGES
|
3
|
474 #warning How do we destroy an XvImage?
|
1
|
475 #else
|
3
|
476 XDestroyImage(rgb_image);
|
1
|
477 #endif
|
3
|
478 shmdt(shminfo.shmaddr);
|
|
479 shmctl(shminfo.shmid, IPC_RMID, 0);
|
1
|
480 }
|
|
481
|
|
482
|
|
483 /*--------------------------------------------------------------------------*/
|
|
484
|
3
|
485 void
|
|
486 X_Display(void) {
|
|
487 int _w , _h, _d;
|
|
488 Window _dw;
|
1
|
489
|
|
490 #ifdef USE_XVIMAGES
|
3
|
491 XGetGeometry(disp, win, &_dw, &_d, &_d, &_w, &_h, &_d, &_d);
|
|
492 XvShmPutImage(disp, xv_adaptors[xv_adaptor].base_id, win,
|
|
493 gc, yuv_image, 0, 0, yuv_image->width, yuv_image->height,
|
|
494 0, 0, _w, _h, True);
|
1
|
495 #else
|
3
|
496 XShmPutImage(disp, win, gc, rgb_image, 0, 0, 0, 0,
|
|
497 rgb_image->width, rgb_image->height, False);
|
1
|
498 #endif
|
3
|
499 XSync(disp, False);
|
1
|
500 }
|
|
501
|
|
502 #define CMD_NONE 0
|
|
503 #define CMD_CHNUP 1
|
|
504 #define CMD_CHNDN 2
|
|
505 #define CMD_MUTE 3
|
|
506 #define CMD_QUIT 4
|
|
507 #define CMD_RELOAD 5
|
|
508 #define CMD_CURSOR 6
|
|
509 #define CMD_VOLDN 7
|
|
510 #define CMD_VOLUP 8
|
|
511 #define CMD_FSTOGGLE 9
|
|
512
|
|
513 /*--------------------------------------------------------------------------*/
|
|
514
|
3
|
515 int
|
|
516 main(int argc, char *argv[]) {
|
1
|
517 #ifndef USE_XVIMAGES
|
3
|
518 HermesHandle conv;
|
|
519 HermesFormat fmt_source, fmt_dest;
|
1
|
520 #endif
|
3
|
521 XEvent e;
|
|
522 char *scratch_buf, *code, *c, *mixerdev;
|
|
523 int frames , channelidx, oldchan, mute, cursor, fd, ret, cmd;
|
|
524 int exitnow , mfd, uselirc, curvol;
|
|
525 struct timeval then, now, diff, lastmove;
|
|
526 float rate;
|
|
527 KeySym key;
|
|
528 char text [255];
|
|
529 int channellist[] = {2, 7, 9, 10, 28};
|
|
530 struct lirc_config *config;
|
|
531 struct pollfd fds[1];
|
1
|
532
|
3
|
533 #define NUMCHANS (sizeof(channellist) / sizeof(channellist[0]))
|
1
|
534
|
3
|
535 channelidx = mute = cursor = 0;
|
1
|
536
|
3
|
537 oldchan = channel = channellist[channelidx];
|
1
|
538
|
|
539 #ifndef USE_XVIMAGES
|
3
|
540 if (!Hermes_Init()) {
|
|
541 printf("Couldn't initialise Hermes!\n");
|
|
542 exit(1);
|
|
543 }
|
|
544 conv = Hermes_ConverterInstance(HERMES_CONVERT_NORMAL);
|
|
545 if (!conv) {
|
|
546 printf("Could not obtain converter instance from Hermes!\n");
|
|
547 exit(1);
|
|
548 }
|
1
|
549 #endif
|
|
550
|
3
|
551 X_Setup(width, height);
|
|
552 XSelectInput(disp, win, KeyReleaseMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask);
|
1
|
553
|
3
|
554 X_HideCursor();
|
1
|
555
|
3
|
556 /* Open Capture device */
|
|
557 Open();
|
1
|
558
|
3
|
559 /* Open audio mixer */
|
|
560 mixerdev = "/dev/mixer";
|
|
561 if ((mfd = open(mixerdev, O_RDWR)) == -1)
|
|
562 fprintf(stderr, "Unable to open %s - %s\n", mixerdev, strerror(errno));
|
1
|
563
|
3
|
564 /* Talk to LIRC */
|
|
565 if ((fd = lirc_init(basename(argv[0]), 1)) == -1) {
|
|
566 fprintf(stderr, "Unable to init lirc client library\n");
|
|
567 uselirc = 0;
|
|
568 } else {
|
|
569 if (lirc_readconfig(NULL, &config, NULL) != 0) {
|
|
570 fprintf(stderr, "Unable to parse config file\n");
|
|
571 uselirc = 0;
|
|
572 } else
|
|
573 uselirc = 1;
|
|
574 fds[0].fd = fd;
|
|
575 fds[0].events = POLLRDNORM;
|
1
|
576 }
|
|
577
|
3
|
578 #ifndef USE_XVIMAGES
|
|
579 /* Conversion from and to formats */
|
|
580 fmt_source.indexed = 0;
|
|
581 fmt_source.bits = 32;
|
|
582 fmt_source.r = 0xff0000;
|
|
583 fmt_source.g = 0x00ff00;
|
|
584 fmt_source.b = 0x0000ff;
|
|
585 fmt_source.a = 0;
|
|
586
|
|
587 fmt_dest.indexed = 0;
|
|
588 fmt_dest.bits = bits_per_pixel;
|
|
589 fmt_dest.r = vi->red_mask;
|
|
590 fmt_dest.g = vi->green_mask;
|
|
591 fmt_dest.b = vi->blue_mask;
|
|
592 fmt_dest.a = 0;
|
|
593 #else
|
|
594 scratch_buf = malloc(width * height);
|
|
595 #endif
|
|
596
|
|
597 frames = 0;
|
|
598 gettimeofday(&then, NULL);
|
|
599 gettimeofday(&lastmove, NULL);
|
|
600
|
|
601 exitnow = 0;
|
|
602
|
|
603 /* Capture loop */
|
|
604 while (1) {
|
|
605 if (frames == 50) {
|
|
606 gettimeofday(&now, NULL);
|
|
607 timersub(&now, &then, &diff);
|
|
608
|
|
609 rate = (float)frames / (float)(diff.tv_usec / 1000000.0 + diff.tv_sec);
|
|
610
|
|
611 printf("%d frames in %.2f seconds, rate %.2f\n", frames, (float)(diff.tv_usec / 1000000.0) + diff.tv_sec, rate);
|
|
612 gettimeofday(&then, NULL);
|
|
613 frames = 0;
|
|
614 XResetScreenSaver(disp);
|
1
|
615 }
|
3
|
616 frames++;
|
1
|
617
|
3
|
618 timersub(&now, &lastmove, &diff);
|
|
619 if (((float)diff.tv_usec / 1000000.0 + (float)diff.tv_sec) > 2.0) {
|
|
620 if (cursor) {
|
|
621 X_HideCursor();
|
|
622 cursor = 0;
|
|
623 }
|
|
624 } else {
|
|
625 if (!cursor) {
|
|
626 X_ShowCursor();
|
|
627 cursor = 1;
|
|
628 }
|
|
629 }
|
|
630
|
|
631 if (XCheckMaskEvent(disp, PointerMotionMask, &e) && e.type == MotionNotify) {
|
|
632 gettimeofday(&lastmove, NULL);
|
|
633 }
|
|
634 cmd = CMD_NONE;
|
1
|
635
|
3
|
636 if (XCheckMaskEvent(disp, ButtonReleaseMask, &e)) {
|
|
637 printf("e.type = %d\n", e.type);
|
|
638 cmd = CMD_MUTE;
|
|
639 }
|
|
640 if (XCheckMaskEvent(disp, KeyReleaseMask, &e) && e.type == KeyRelease) {
|
|
641 gettimeofday(&lastmove, NULL);
|
|
642
|
|
643 XLookupString(&e.xkey, text, 255, &key, 0);
|
|
644 printf("Press - %c\n", text[0]);
|
1
|
645
|
3
|
646 switch (text[0]) {
|
|
647 case 'q':
|
|
648 cmd = CMD_QUIT;
|
|
649 break;
|
|
650
|
|
651 case '=':
|
|
652 case '+':
|
1
|
653 cmd = CMD_CHNUP;
|
3
|
654 break;
|
|
655
|
|
656 case '-':
|
1
|
657 cmd = CMD_CHNDN;
|
3
|
658 break;
|
|
659
|
|
660 case 'h':
|
|
661 cmd = CMD_CURSOR;
|
|
662 break;
|
|
663
|
|
664 case 'm':
|
|
665 cmd = CMD_MUTE;
|
|
666 break;
|
|
667
|
|
668 case 'r':
|
|
669 cmd = CMD_RELOAD;
|
|
670 break;
|
|
671
|
|
672 case ',':
|
|
673 cmd = CMD_VOLDN;
|
|
674 break;
|
|
675
|
|
676 case '.':
|
1
|
677 cmd = CMD_VOLUP;
|
3
|
678 break;
|
|
679
|
|
680 case 'f':
|
1
|
681 cmd = CMD_FSTOGGLE;
|
3
|
682 break;
|
1
|
683 }
|
3
|
684 }
|
|
685 /* Poll for IR events */
|
|
686 if (uselirc) {
|
|
687 fds[0].revents = 0;
|
|
688
|
|
689 while (1) {
|
|
690 if (poll(fds, 1, 0) == -1) {
|
|
691 fprintf(stderr, "Poll failed - %s\n", strerror(errno));
|
|
692 exit(EXIT_FAILURE);
|
|
693 }
|
|
694 if ((fds[0].revents & POLLRDNORM) != 0) {
|
|
695 fprintf(stderr, "Processing IR..\n");
|
|
696
|
|
697 if (lirc_nextcode(&code) == 0) {
|
|
698 if (code == NULL)
|
|
699 continue;
|
|
700
|
|
701 while ((ret = lirc_code2char(config, code, &c)) == 0 &&
|
|
702 c != NULL) {
|
|
703 fprintf(stderr, "Got command \"%s\"\n", c);
|
1
|
704
|
3
|
705 if (!strcmp(c, "Mute"))
|
|
706 cmd = CMD_MUTE;
|
|
707 else if (!strcmp(c, "CH_UP"))
|
|
708 cmd = CMD_CHNUP;
|
|
709 else if (!strcmp(c, "CH_DOWN"))
|
|
710 cmd = CMD_CHNDN;
|
|
711 else if (!strcmp(c, "Power"))
|
|
712 cmd = CMD_QUIT;
|
|
713 else if (!strcmp(c, "VOL_UP"))
|
|
714 cmd = CMD_VOLUP;
|
|
715 else if (!strcmp(c, "VOL_DOWN"))
|
|
716 cmd = CMD_VOLDN;
|
|
717 else if (!strcmp(c, "AV/TV"))
|
|
718 cmd = CMD_FSTOGGLE;
|
|
719
|
|
720 free(code);
|
|
721 }
|
|
722 }
|
|
723 } else
|
|
724 break;
|
|
725 }
|
1
|
726 }
|
3
|
727 switch (cmd) {
|
|
728 case CMD_QUIT:
|
|
729 exitnow = 1;
|
|
730 break;
|
|
731
|
|
732 case CMD_CHNUP:
|
|
733 case CMD_CHNDN:
|
|
734 if (cmd == CMD_CHNUP)
|
|
735 channelidx++;
|
|
736 else
|
|
737 channelidx--;
|
|
738
|
|
739 if (channelidx < 0)
|
|
740 channelidx = NUMCHANS - 1;
|
|
741 if (channelidx > NUMCHANS - 1)
|
|
742 channelidx = 0;
|
|
743
|
|
744 channel = channellist[channelidx];
|
1
|
745
|
3
|
746 printf("Channel - %d\n", channel);
|
|
747 if (oldchan != channel) {
|
|
748 oldchan = channel;
|
|
749
|
|
750 if (ioctl(tuner_fd, TVTUNER_SETCHNL, &channel) < 0) {
|
|
751 DO_IOCTL_SERR("TVTUNER_SETCHNL", channel);
|
|
752 exit(1);
|
|
753 }
|
|
754 }
|
|
755 break;
|
|
756
|
|
757 case CMD_MUTE:
|
|
758 if (mute)
|
|
759 mute = 0;
|
|
760 else
|
|
761 mute = 1;
|
|
762
|
|
763 printf("Mute - %d\n", mute);
|
|
764 if (ioctl(tuner_fd, BT848_SAUDIO, &mute) < 0) {
|
|
765 DO_IOCTL_SERR("BT848_SAUDIO", mute);
|
|
766 exit(1);
|
|
767 }
|
|
768 break;
|
1
|
769
|
3
|
770 case CMD_CURSOR:
|
|
771 if (cursor) {
|
|
772 printf("Cursor hidden\n");
|
|
773 X_HideCursor();
|
|
774 cursor = 0;
|
|
775 } else {
|
|
776 printf("Cursor revealed\n");
|
|
777 X_ShowCursor();
|
|
778 cursor = 1;
|
|
779 }
|
|
780 break;
|
|
781
|
|
782 case CMD_RELOAD:
|
|
783 printf("Reloading\n");
|
|
784 Close();
|
|
785 Open();
|
|
786 break;
|
|
787
|
|
788 case CMD_VOLUP:
|
|
789 case CMD_VOLDN:
|
|
790 if (ioctl(mfd, MIXER_READ(SOUND_MIXER_VOLUME), &curvol) == -1) {
|
|
791 fprintf(stderr, "Unable to read current volume - %s\n", strerror(errno));
|
|
792 break;
|
|
793 }
|
|
794 curvol = curvol & 0x7f;
|
|
795
|
|
796 if (cmd == CMD_VOLUP)
|
|
797 curvol += 5;
|
|
798 else
|
|
799 curvol -= 5;
|
|
800
|
|
801 if (curvol < 0)
|
|
802 curvol = 0;
|
|
803 if (curvol > 100)
|
|
804 curvol = 100;
|
|
805
|
|
806 printf("Setting volume to %d\n", curvol);
|
|
807 curvol |= curvol << 8;
|
1
|
808
|
3
|
809 if (ioctl(mfd, MIXER_WRITE(SOUND_MIXER_VOLUME), &curvol) == -1) {
|
|
810 fprintf(stderr, "Unable to write volume - %s\n", strerror(errno));
|
|
811 break;
|
|
812 }
|
|
813 break;
|
|
814
|
|
815 case CMD_FSTOGGLE:
|
|
816 vo_x11_ewmh_fullscreen(_NET_WM_STATE_TOGGLE);
|
|
817 break;
|
|
818
|
|
819 }
|
|
820
|
|
821 Capture();
|
1
|
822
|
3
|
823 #ifdef USE_XVIMAGES
|
|
824 /* bktr's YUV_12 is planar W*H bytes Y, W/2*H/2 bytes U, */
|
|
825 /* W/2*H/2 bytes V. Xv's YV12 is the same with U and V */
|
|
826 /* planes reversed. */
|
|
827 {
|
|
828 int y_off , u_off, v_off;
|
|
829 y_off = 0;
|
|
830 u_off = width * height;
|
|
831 v_off = u_off + width * height / 4;
|
|
832
|
|
833 assert(yuv_image->data_size == width * height * 3 / 2);
|
|
834 memcpy(yuv_image->data, bktr_buffer, u_off - y_off);
|
|
835 memcpy(yuv_image->data + u_off, bktr_buffer + v_off, v_off - u_off);
|
|
836 memcpy(yuv_image->data + v_off, bktr_buffer + u_off, v_off - u_off);
|
|
837 }
|
|
838 #else
|
|
839 /* SaveImage(); */
|
|
840
|
|
841 Hermes_ConverterRequest(conv, &fmt_source, &fmt_dest);
|
|
842 Hermes_ConverterCopy(conv, bktr_buffer, 0, 0, width, height, width * 4,
|
|
843 rgb_image->data, 0, 0, width, height,
|
|
844 rgb_image->bytes_per_line);
|
|
845 #endif
|
|
846
|
|
847 X_Display();
|
|
848 if (exitnow) {
|
|
849 printf("quitting\n");
|
|
850 break;
|
|
851 }
|
1
|
852 }
|
|
853
|
3
|
854 #ifndef USE_XVIMAGES
|
|
855 Hermes_ConverterReturn(conv);
|
|
856 Hermes_Done();
|
1
|
857 #endif
|
|
858
|
3
|
859 X_Shutdown();
|
|
860 lirc_freeconfig(config);
|
|
861 lirc_deinit();
|
1
|
862
|
3
|
863 return 0;
|
1
|
864 }
|