File: pv.c

package info (click to toggle)
transcode 3%3A1.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 11,644 kB
  • sloc: ansic: 116,927; sh: 11,468; xml: 2,849; makefile: 1,891; perl: 1,492; pascal: 526; php: 191; python: 144; sed: 43
file content (727 lines) | stat: -rw-r--r-- 18,754 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/*
 *  pv.c
 *
 *  Copyright (C) Thomas Oestreich - October 2002
 *
 *  based on "display.c
 *     Copyright (C) Charles 'Buck' Krasic - April 2000
 *     Copyright (C) Erik Walthinsen - April 2000
 *  part of libdv, a free DV (IEC 61834/SMPTE 314M) codec.
 *
 *  This file is part of transcode, a video stream processing tool
 *
 *  transcode is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  transcode is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#include "pv.h"
#include "src/socket.h"

#include <sys/ipc.h>
#include <sys/shm.h>
#include <math.h>


#define XV_FORMAT_MASK		0x03
#define XV_FORMAT_ASIS		0x00
#define XV_FORMAT_NORMAL	0x01
#define XV_FORMAT_WIDE		0x02

#define XV_SIZE_MASK		0x0c
#define XV_SIZE_NORMAL		0x04
#define XV_SIZE_QUARTER		0x08

#define XV_NOSAWINDOW		0x10	/* not use at the moment	*/

#define DV_FORMAT_UNKNOWN	-1
#define DV_FORMAT_NORMAL	0
#define DV_FORMAT_WIDE		1

static int xv_display_Xv_init (xv_display_t *dv_dpy, char *w_name,
			       char *i_name, int flags, int size);


xv_player_t *xv_player_new(void)
{
  xv_player_t *result;

  if(!(result = calloc(1,sizeof(xv_player_t)))) goto no_mem;
  if(!(result->display = xv_display_new())) goto no_display;

  return(result);

 no_display:
  free(result);
  result = NULL;
 no_mem:
  return(result);
} // xv_player_new


xv_display_t *xv_display_new(void)
{
  xv_display_t *result;

  result = (xv_display_t *)calloc(1,sizeof(xv_display_t));
  if(!result) goto no_mem;

 no_mem:
  return(result);
} // xv_display_new


void xv_display_show(xv_display_t *dv_dpy) {

  xv_display_event(dv_dpy);

  if (!dv_dpy->dontdraw) {
    XvShmPutImage(dv_dpy->dpy, dv_dpy->port,
		  dv_dpy->win, dv_dpy->gc,
		  dv_dpy->xv_image,
		  0, 0,			                /* sx, sy */
		  dv_dpy->swidth, dv_dpy->sheight,	/* sw, sh */
		  dv_dpy->lxoff,  dv_dpy->lyoff,        /* dx, dy */
		  dv_dpy->lwidth, dv_dpy->lheight,	/* dw, dh */
		  True);
    XFlush(dv_dpy->dpy);
  }

} // xv_display_show


void xv_display_exit(xv_display_t *dv_dpy) {

  if(!dv_dpy) return;

  XvStopVideo(dv_dpy->dpy, dv_dpy->port, dv_dpy->win);

  if(dv_dpy->shminfo.shmaddr)
    shmdt(dv_dpy->shminfo.shmaddr);

  if(dv_dpy->shminfo.shmid > 0)
    shmctl(dv_dpy->shminfo.shmid, IPC_RMID, 0);

  if(dv_dpy->xv_image) free(dv_dpy->xv_image);
  dv_dpy->xv_image = NULL;

  free(dv_dpy);
  dv_dpy = NULL;
} // xv_display_exit

static int xv_pause=0;

static void xv_window_close(xv_display_t *dv_dpy)
{
    dv_dpy->dontdraw = 1;

    XvStopVideo(dv_dpy->dpy, dv_dpy->port, dv_dpy->win);
    XDestroyWindow(dv_dpy->dpy, dv_dpy->win);

    tc_socket_submit ("[filter_pv]: preview window close\n");
    //reset
    xv_pause=0;
}


void xv_display_event (xv_display_t *dv_dpy)
{
  int	old_pic_format;
  KeySym keysym;
  char buf[16];
  XButtonEvent *but_event;
  static int x1, y1, x2, y2;
  enum tc_socket_msg_cmd_enum sockcmd;
  int arg;

  pthread_mutex_lock(&tc_socket_msg_lock);
  sockcmd = tc_socket_msg_cmd;
  arg = tc_socket_msg_arg;
  tc_socket_msg_cmd = TC_SOCK_PV_NONE;
  pthread_mutex_unlock(&tc_socket_msg_lock);

  while ( sockcmd || XPending(dv_dpy->dpy) )  {

    // tibit: Poll for a socket message
    if (sockcmd) {
	//tc_log_msg(__FILE__, "Got char (%c)", sockcmd);
	//tc_log_msg(__FILE__, "FILTER: (%d)", arg);
	switch (sockcmd) {
	    case TC_SOCK_PV_DRAW:
		preview_filter_buffer(arg?arg:1);
		break;
	    case TC_SOCK_PV_UNDO:
		preview_cache_undo();
		break;
	    case TC_SOCK_PV_SLOW_FW:
		preview_cache_draw(cache_short_skip);
		break;
	    case TC_SOCK_PV_SLOW_BW:
		preview_cache_draw(-cache_short_skip);
		break;
	    case TC_SOCK_PV_FAST_FW:
		preview_cache_draw(cache_long_skip);
		break;
	    case TC_SOCK_PV_FAST_BW:
		preview_cache_draw(-cache_long_skip);
		break;
	    case TC_SOCK_PV_ROTATE:
		tc_outstream_rotate_request();
		break;
	    case TC_SOCK_PV_FASTER:
		dec_preview_delay();
		break;
	    case TC_SOCK_PV_SLOWER:
		inc_preview_delay();
		break;
	    case TC_SOCK_PV_TOGGLE:
		preview_toggle_skip();
		break;
	    case TC_SOCK_PV_SAVE_JPG:
		preview_grab_jpeg();
		break;
	    case TC_SOCK_PV_DISPLAY:
		xv_pause=0;
		dv_dpy->dontdraw = (dv_dpy->dontdraw) ? 0:1;
		break;
	    case TC_SOCK_PV_PAUSE:
		xv_pause = (xv_pause)?0:1;
		while(xv_pause) {
		    xv_display_event(dv_dpy);
		    usleep(10000);
		}
		break;
	    default:
		break;
	} // switch msg
	sockcmd = TC_SOCK_PV_NONE;
    } else {

    // remove Event
    XNextEvent(dv_dpy->dpy, &dv_dpy->event);


    switch (dv_dpy->event.type) {

	case ClientMessage:
	    // stolen from xmms opengl_plugin  -- tibit
	    if ((Atom)(dv_dpy->event.xclient.data.l[0]) == dv_dpy->wm_delete_window_atom)
	    {
		xv_window_close (dv_dpy);
	    }
	    break;

    case ConfigureNotify:
      dv_dpy->dwidth = dv_dpy->event.xconfigure.width;
      dv_dpy->dheight = dv_dpy->event.xconfigure.height;
      /* --------------------------------------------------------------------
       * set current picture format to unknown, so that .._check_format
       * does some work.
       */
      old_pic_format = dv_dpy->pic_format;
      dv_dpy->pic_format = DV_FORMAT_UNKNOWN;
      xv_display_check_format(dv_dpy, old_pic_format);
      break;

    case ButtonPress:

      but_event = (XButtonEvent *) &dv_dpy->event;
      if (DoSelection(but_event, &x1, &y1, &x2, &y2)) {
	      // min <-> max
	      int xanf = (x1<x2)?x1:x2;
	      int yanf = (y1<y2)?y1:y2;
	      int xend = (x1>x2)?x1:x2;
	      int yend = (y1>y2)?y1:y2;
	      char buf[255];

	      tc_snprintf(buf, sizeof(buf), "[%s]: pos1=%dx%d pos2=%dx%d pos=%dx%d:size=%dx%d -Y %d,%d,%d,%d\n",
			      "filter_pv", xanf, yanf, xend, yend, xanf, yanf, xend-xanf, yend-yanf,
			      yanf, xanf,  dv_dpy->height-yend, dv_dpy->width - xend);

	      //print to socket
	      tc_socket_submit (buf);
	      //print elsewhere
	      tc_log_msg(__FILE__, "%s", buf);
	      // white
	      XSetForeground(dv_dpy->dpy,dv_dpy->gc, 0xFFFFFFFFUL);
	      XDrawRectangle(dv_dpy->dpy,dv_dpy->win,dv_dpy->gc,
			      xanf, yanf, (unsigned int)xend-xanf, (unsigned int)yend-yanf);

      }

      break;

    case KeyPress:

      XLookupString (&dv_dpy->event.xkey, buf, 16, &keysym, NULL);

      switch(keysym) {

      case XK_Escape:
	xv_window_close (dv_dpy);
	break;

      case XK_u:
      case XK_U:
	preview_cache_undo();
	break;

      case XK_Q:
      case XK_q:
	xv_pause=0;
	dv_dpy->dontdraw = (dv_dpy->dontdraw) ? 0:1;
	break;

      case XK_Up:
	preview_cache_draw(cache_long_skip);
	break;

      case XK_Down:
	preview_cache_draw(-cache_long_skip);
	break;

      case XK_Left:
	preview_cache_draw(-cache_short_skip);
	break;

      case XK_Right:
	preview_cache_draw(cache_short_skip);
	break;

      case XK_R:
      case XK_r:
	tc_outstream_rotate_request();
	break;

      case XK_s:
      case XK_S:
	inc_preview_delay();
	break;

      case XK_f:
      case XK_F:
	dec_preview_delay();
	break;

      case XK_y:
      case XK_Y:
	preview_toggle_skip();
	break;

      case XK_j:
      case XK_J:
	preview_grab_jpeg();
	break;

#if 0
      case XK_a:
      case XK_A:
	preview_filter();
	break;
#endif

      case XK_Return:
	xv_display_show(dv_dpy);
	break;

      case XK_space:
	xv_pause = (xv_pause)?0:1;
	while(xv_pause) {
	  xv_display_event(dv_dpy);
	  //xv_display_show(dv_dpy);
	  usleep(10000);
	}
      default:
	break;
      }
    default:
      break;
    } /* switch */
  } /* else */
  } /* while */
} // xv_display_event


void xv_display_check_format(xv_display_t *dv_dpy, int pic_format)
{
  /*  return immediate if ther is no format change or no format
   * specific flag was set upon initialisation
   */
  if (pic_format == dv_dpy->pic_format ||
      !(dv_dpy->flags & XV_FORMAT_MASK))
    return;

  /* --------------------------------------------------------------------
   * check if there are some aspect ratio constraints
   */
  if (dv_dpy->flags & XV_FORMAT_NORMAL) {
    if (pic_format == DV_FORMAT_NORMAL) {
      dv_dpy->lxoff = dv_dpy->lyoff = 0;
      dv_dpy->lwidth = dv_dpy->dwidth;
      dv_dpy->lheight = dv_dpy->dheight;
    } else if (pic_format == DV_FORMAT_WIDE) {
      dv_dpy->lxoff = 0;
      dv_dpy->lyoff = dv_dpy->dheight / 8;
      dv_dpy->lwidth = dv_dpy->dwidth;
      dv_dpy->lheight = (dv_dpy->dheight * 3) / 4;
    }
  } else if (dv_dpy->flags & XV_FORMAT_WIDE) {
    if (pic_format == DV_FORMAT_NORMAL) {
      dv_dpy->lxoff = dv_dpy->dwidth / 8;
      dv_dpy->lyoff = 0;
      dv_dpy->lwidth = (dv_dpy->dwidth * 3) / 4;
      dv_dpy->lheight = dv_dpy->dheight;
    } else if (pic_format == DV_FORMAT_WIDE) {
      dv_dpy->lxoff = dv_dpy->lyoff = 0;
      dv_dpy->lwidth = dv_dpy->dwidth;
      dv_dpy->lheight = dv_dpy->dheight;
    }
  } else {
    dv_dpy->lwidth = dv_dpy->dwidth;
    dv_dpy->lheight = dv_dpy->dheight;
  }
  dv_dpy->pic_format = pic_format;
} // xv_display_check_format


static int xv_display_Xv_init(xv_display_t *dv_dpy, char *w_name, char *i_name,
			      int flags, int size)
{

  int scn_id, ad_cnt, fmt_cnt, got_port, got_fmt;
  int i, k;

  XGCValues	values;
  XSizeHints	hints;
  XWMHints	wmhints;
  XTextProperty	x_wname, x_iname;

  Atom wm_protocols[1];

  XvAdaptorInfo	*ad_info;
  XvImageFormatValues *fmt_info;

  if(!(dv_dpy->dpy = XOpenDisplay(NULL))) return 0;

  dv_dpy->rwin = DefaultRootWindow(dv_dpy->dpy);
  scn_id = DefaultScreen(dv_dpy->dpy);

  /*
   * So let's first check for an available adaptor and port
   */
  /* Note: this is identical to the similar section in display.c  --AC */

  if(Success == XvQueryAdaptors(dv_dpy->dpy, dv_dpy->rwin, &ad_cnt, &ad_info)) {

    for(i = 0, got_port = False; i < ad_cnt; ++i) {
      tc_log_msg(__FILE__,
		 "Xv: %s: ports %ld - %ld",
		 ad_info[i].name,
		 ad_info[i].base_id,
		 ad_info[i].base_id +
		 ad_info[i].num_ports - 1);

      if (dv_dpy->arg_xv_port != 0 &&
	      (dv_dpy->arg_xv_port < ad_info[i].base_id ||
	       dv_dpy->arg_xv_port >= ad_info[i].base_id+ad_info[i].num_ports)) {
	  tc_log_msg(__FILE__,
		    "Xv: %s: skipping (looking for port %i)",
		    ad_info[i].name,
		    dv_dpy->arg_xv_port);
	  continue;
      }

      if (!(ad_info[i].type & XvImageMask)) {
	tc_log_warn(__FILE__,
		    "Xv: %s: XvImage NOT in capabilty list (%s%s%s%s%s )",
		    ad_info[i].name,
		    (ad_info[i].type & XvInputMask) ? " XvInput"  : "",
		    (ad_info[i]. type & XvOutputMask) ? " XvOutput" : "",
		    (ad_info[i]. type & XvVideoMask)  ?  " XvVideo"  : "",
		    (ad_info[i]. type & XvStillMask)  ?  " XvStill"  : "",
		    (ad_info[i]. type & XvImageMask)  ?  " XvImage"  : "");
	continue;
      } /* if */
      fmt_info = XvListImageFormats(dv_dpy->dpy, ad_info[i].base_id,&fmt_cnt);
      if (!fmt_info || fmt_cnt == 0) {
	tc_log_warn(__FILE__, "Xv: %s: NO supported formats", ad_info[i].name);
	continue;
      } /* if */
      for(got_fmt = False, k = 0; k < fmt_cnt; ++k) {
	if (dv_dpy->format == fmt_info[k].id) {
	  got_fmt = True;
	  break;
	} /* if */
      } /* for */
      if (!got_fmt) {
	char tmpbuf[1000];
	*tmpbuf = 0;
	for (k = 0; k < fmt_cnt; ++k) {
	  tc_snprintf(tmpbuf+strlen(tmpbuf), sizeof(tmpbuf)-strlen(tmpbuf),
		      "%s%#08x[%s]", k>0 ? " " : "", fmt_info[k].id,
		      fmt_info[k].guid);
	}
	tc_log_warn(__FILE__,
		    "Xv: %s: format %#08x is NOT in format list (%s)",
		    ad_info[i].name,
		    dv_dpy->format,
		    tmpbuf);
	continue;
      } /* if */

      for(dv_dpy->port = ad_info[i].base_id, k = 0;
	  k < ad_info[i].num_ports;
	  ++k, ++(dv_dpy->port)) {
	if (dv_dpy->arg_xv_port != 0 && dv_dpy->arg_xv_port != dv_dpy->port) continue;
	if(!XvGrabPort(dv_dpy->dpy, dv_dpy->port, CurrentTime)) {
	  tc_log_msg(__FILE__, "Xv: grabbed port %ld",
		     dv_dpy->port);
	  got_port = True;
	  break;
	} /* if */
      } /* for */
      if(got_port)
	break;
    } /* for */

  } else {
    /* Xv extension probably not present */
    return 0;
  } /* else */

  if(!ad_cnt) {
    tc_log_warn(__FILE__, "Xv: (ERROR) no adaptor found!");
    return 0;
  }
  if(!got_port) {
    tc_log_warn(__FILE__, "Xv: (ERROR) could not grab any port!");
    return 0;
  }

  /* --------------------------------------------------------------------------
   * default settings which allow arbitrary resizing of the window
   */
  hints.flags = PSize | PMaxSize | PMinSize;
  hints.min_width = dv_dpy->width / 16;
  hints.min_height = dv_dpy->height / 16;

  /* --------------------------------------------------------------------------
   * maximum dimensions for Xv support are about 2048x2048
   */
  hints.max_width = 2048;
  hints.max_height = 2048;

  wmhints.input = True;
  wmhints.flags = InputHint;

  XStringListToTextProperty(&w_name, 1 ,&x_wname);
  XStringListToTextProperty(&i_name, 1 ,&x_iname);

  /*
   * default settings: source, destination and logical width/height
   * are set to our well known dimensions.
   */

  dv_dpy->lwidth = dv_dpy->dwidth = dv_dpy->swidth = dv_dpy->width;
  dv_dpy->lheight = dv_dpy->dheight = dv_dpy->sheight = dv_dpy->height;
  dv_dpy->lxoff = dv_dpy->lyoff = 0;
  dv_dpy-> flags = flags;

  if (flags & XV_FORMAT_MASK) {
    dv_dpy->lwidth = dv_dpy->dwidth = 768;
    dv_dpy->lheight = dv_dpy->dheight = 576;
    dv_dpy->pic_format = DV_FORMAT_UNKNOWN;
    if (flags & XV_FORMAT_WIDE) {
      dv_dpy->lwidth = dv_dpy->dwidth = 1024;
    }
  }
  if (size) {
    dv_dpy->lwidth  = (int)(((double)dv_dpy->lwidth  * (double)size)/100.0);
    dv_dpy->lheight = (int)(((double)dv_dpy->lheight * (double)size)/100.0);
    dv_dpy->dwidth  = (int)(((double)dv_dpy->dwidth  * (double)size)/100.0);
    dv_dpy->dheight = (int)(((double)dv_dpy->dheight * (double)size)/100.0);
  }
  if (flags & XV_FORMAT_MASK) {
    hints.flags |= PAspect;
    if (flags & XV_FORMAT_WIDE) {
      hints.min_aspect.x = hints.max_aspect.x = 1024;
    } else {
      hints.min_aspect.x = hints.max_aspect.x = 768;
    }
    hints.min_aspect.y = hints.max_aspect.y = 576;
  }

  if (!(flags & XV_NOSAWINDOW)) {

    if(dv_dpy->full_screen) {
      int  screen = XDefaultScreen(dv_dpy->dpy);
      dv_dpy->lwidth = dv_dpy->dwidth =DisplayWidth(dv_dpy->dpy, screen);
      dv_dpy->lheight = dv_dpy->dheight = DisplayHeight(dv_dpy->dpy, screen);
    }

    dv_dpy->win = XCreateSimpleWindow(dv_dpy->dpy,
				       dv_dpy->rwin,
				       0, 0,
				       dv_dpy->dwidth, dv_dpy->dheight,
				       0,
				       XWhitePixel(dv_dpy->dpy, scn_id),
				       XBlackPixel(dv_dpy->dpy, scn_id));

    if(dv_dpy->full_screen) {
      static Atom           XA_WIN_STATE = None;
      long                  propvalue[2];

      XA_WIN_STATE = XInternAtom (dv_dpy->dpy, "_NET_WM_STATE", False);
      propvalue[0] = XInternAtom (dv_dpy->dpy, "_NET_WM_STATE_FULLSCREEN", False);
      propvalue[1] = 0;

      XChangeProperty (dv_dpy->dpy, dv_dpy->win, XA_WIN_STATE, XA_ATOM,
                       32, PropModeReplace, (unsigned char *)propvalue, 1);
    }
  } else {
  }

  XSetWMProperties(dv_dpy->dpy, dv_dpy->win,
		    &x_wname, &x_iname,
		    NULL, 0,
		    &hints, &wmhints, NULL);


  XSelectInput(dv_dpy->dpy, dv_dpy->win, ExposureMask | StructureNotifyMask |
		                         KeyPressMask | ButtonPressMask);

  dv_dpy->wm_delete_window_atom = wm_protocols[0] =
      XInternAtom(dv_dpy->dpy, "WM_DELETE_WINDOW", False);
  XSetWMProtocols(dv_dpy->dpy, dv_dpy->win, wm_protocols, 1);

  XMapRaised(dv_dpy->dpy, dv_dpy->win);
  XNextEvent(dv_dpy->dpy, &dv_dpy->event);

  dv_dpy->gc = XCreateGC(dv_dpy->dpy, dv_dpy->win, 0, &values);

  /*
   * Now we do shared memory allocation etc..
   */

  dv_dpy->xv_image = XvShmCreateImage(dv_dpy->dpy, dv_dpy->port,
					 dv_dpy->format, dv_dpy->pixels[0],
				      	 dv_dpy->width, dv_dpy->height,
					 &dv_dpy->shminfo);

  dv_dpy->shminfo.shmid = shmget(IPC_PRIVATE,
				     dv_dpy->len,
				     IPC_CREAT | 0777);

  dv_dpy->xv_image->data = dv_dpy->pixels[0] = dv_dpy->shminfo.shmaddr =
    shmat(dv_dpy->shminfo.shmid, 0, 0);

  XShmAttach(dv_dpy->dpy, &dv_dpy->shminfo);
  XSync(dv_dpy->dpy, False);



  return 1;
} // xv_display_Xv_init

int xv_display_init(xv_display_t *dv_dpy, int *argc, char ***argv, int width, int height, char *w_name, char *i_name, int yuv422) {

  dv_dpy->width = width;
  dv_dpy->height = height;

  dv_dpy->dontdraw = 0;

  dv_dpy->format = yuv422 ? DV_FOURCC_YUY2 : DV_FOURCC_I420;
  dv_dpy->len = (dv_dpy->width * dv_dpy->height * 3) / 2;
  if (yuv422) dv_dpy->len = dv_dpy->width * dv_dpy->height * 2;

  /* Xv */
  if(xv_display_Xv_init(dv_dpy, w_name, i_name,
			dv_dpy->arg_aspect_val,
			dv_dpy->arg_size_val)) {
    goto Xv_ok;
  } else {
    tc_log_error(__FILE__, "Attempt to display via Xv failed");
    goto fail;
  }

 Xv_ok:
  tc_log_info(__FILE__, "Using Xv for display");
  dv_dpy->lib = e_dv_dpy_Xv;
  dv_dpy->color_space = e_dv_color_yuv;


  switch(dv_dpy->format) {

  case DV_FOURCC_YUY2:
    dv_dpy->pitches[0] = width * 2;
    break;

  case DV_FOURCC_I420:
    dv_dpy->pixels[1] = dv_dpy->pixels[0] + (width * height);
    dv_dpy->pixels[2] = dv_dpy->pixels[1] + (width * height / 4);

    dv_dpy->pitches[0] = width;
    dv_dpy->pitches[1] = width / 2;
    dv_dpy->pitches[2] = width / 2;
    break;
  }

  return(0);

 fail:
  tc_log_error(__FILE__, "Unable to establish a display method");
  return(-1);
} // xv_display_init


// returns 1 if a selection is complete (2nd click)

int DoSelection(XButtonEvent *ev, int *xanf, int *yanf, int *xend, int *yend)
{
  int          rv;
  static Time  lastClickTime   = 0;
  static int   lastClickButton = Button3;


  /* make sure it's even vaguely relevant */
  if (ev->type   != ButtonPress) return 0;

  rv = 0;

  if (ev->button == Button1) {
    /* double clicked B1 ? */
      // save
    if (lastClickButton!=Button1) {
      *xanf = ev->x;
      *yanf = ev->y;
      lastClickButton=Button1;
      rv = 0;
    } else  {
      *xend = ev->x;
      *yend = ev->y;
      lastClickButton=Button3;

      //tc_log_msg(__FILE__, "** x (%d) y (%d) h (%d) w (%d)", *xanf, *yanf, *xend-*xanf, *yend-*yanf);
      rv = 1;
    }

  } else if (ev->button == Button2) {      /* do a drag & drop operation */
    tc_log_msg(__FILE__, "** Button2");
  }

  lastClickTime   = ev->time;
  return rv;
}