summaryrefslogtreecommitdiffstats
path: root/src/part/videoWindow.cpp
blob: 53cd0dd4f7593c4df4f9a7020847bae5dd7067ba (plain)
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
// (C) 2005 Max Howell (max.howell@methylblue.com)
// See COPYING file for licensing information

#define CODEINE_DEBUG_PREFIX "videoWindow"

#include <cstdlib>
#include "debug.h"
#include <ntqapplication.h> //sendEvent()
#include <ntqcursor.h>
#include <ntqevent.h>
#include "videoWindow.h"
#include <X11/Xlib.h>     //TODO this breaks compile for lots of people due to excessive macro content
#include <xine.h>         //x11_visual_t


namespace Codeine {


VideoWindow *VideoWindow::s_instance = 0;


namespace X
{
   Display *d;
   int s, w;
}


VideoWindow::VideoWindow( TQWidget *parent, const char *name )
      : TQWidget( parent, name )
      , m_osd( 0 )
      , m_stream( 0 )
      , m_eventQueue( 0 )
      , m_videoPort( 0 )
      , m_audioPort( 0 )
      , m_xine( 0 )
      , m_displayRatio( 1 )
{
   s_instance = this;

   // with this Konqueror would crash on exit
   // without this we may be unstable!
   //XInitThreads();

   show();

   setWFlags( TQt::WNoAutoErase );
   setMouseTracking( true );
   setAcceptDrops( true );
   setUpdatesEnabled( false ); //to stop TQt drawing over us
   setPaletteBackgroundColor( TQt::black );

   X::d = XOpenDisplay( std::getenv("DISPLAY") );
   X::s = DefaultScreen( X::d );
   X::w = winId();

   XLockDisplay( X::d );
   XSelectInput( X::d, X::w, ExposureMask );

   {
      using X::d; using X::s;

      //these are Xlib macros
      double w = DisplayWidth( d, s ) * 1000 / DisplayWidthMM( d, s );
      double h = DisplayHeight( d, s ) * 1000 / DisplayHeightMM( d, s );

      m_displayRatio = w / h;
   }

   XUnlockDisplay( X::d );

   connect( &m_timer, SIGNAL(timeout()), SLOT(hideCursor()) );
}

VideoWindow::~VideoWindow()
{
   DEBUG_BLOCK

   if( m_osd )        xine_osd_free( m_osd );
   if( m_stream )     xine_close( m_stream );
   if( m_eventQueue ) xine_event_dispose_queue( m_eventQueue );
   if( m_stream )     xine_dispose( m_stream );
   if( m_videoPort )  xine_close_video_driver( m_xine, m_videoPort );
   if( m_audioPort )  xine_close_audio_driver( m_xine, m_audioPort );
   if( m_xine )       xine_exit( m_xine );

   XCloseDisplay( X::d );
}

void*
VideoWindow::x11Visual() const
{
   x11_visual_t* visual = new x11_visual_t;

   visual->display          = X::d;
   visual->screen           = X::s;
   visual->d                = X::w;
   visual->dest_size_cb     = &VideoWindow::destSizeCallBack;
   visual->frame_output_cb  = &VideoWindow::frameOutputCallBack;
   visual->user_data        = (void*)this;

   return visual;
}

void
VideoWindow::destSizeCallBack(
      void* p, int /*video_width*/, int /*video_height*/,
      double /*video_aspect*/, int* dest_width,
      int* dest_height, double* dest_aspect )
{
   if( !p )
      return;

   #define vw static_cast<VideoWindow*>(p)

   *dest_width  = vw->width();
   *dest_height = vw->height();
   *dest_aspect = vw->m_displayRatio;
}

void
VideoWindow::frameOutputCallBack(
      void* p, int video_width, int video_height, double video_aspect,
      int* dest_x, int* dest_y, int* dest_width, int* dest_height,
      double* dest_aspect, int* win_x, int* win_y )
{
   if( !p )
      return;

   *dest_x = 0;
   *dest_y = 0 ;
   *dest_width  = vw->width();
   *dest_height = vw->height();
   *win_x = vw->x();
   *win_y = vw->y();
   *dest_aspect = vw->m_displayRatio;

   // correct size with video aspect
   // TODO what's this about?
   if( video_aspect >= vw->m_displayRatio )
      video_width  = int( double(video_width * video_aspect / vw->m_displayRatio + 0.5) );
   else
      video_height = int( double(video_height * vw->m_displayRatio / video_aspect) + 0.5 );

   #undef vw
}

bool
VideoWindow::event( TQEvent *e )
{
   switch( e->type() )
   {
   case TQEvent::MouseMove:
   case TQEvent::MouseButtonPress:
      unsetCursor();
      m_timer.start( CURSOR_HIDE_TIMEOUT, true );
      break;

   case TQEvent::Close:
   case TQEvent::Hide:
      xine_stop( m_stream );
      break;

   case TQEvent::Leave:
      m_timer.stop();
      break;

   default:
      ;
   }

   return TQWidget::event( e );
}

bool
VideoWindow::x11Event( XEvent *e )
{
   if( e->type == Expose && e->xexpose.count == 0 ) {
      xine_port_send_gui_data( m_videoPort, XINE_GUI_SEND_EXPOSE_EVENT, e );
      return true;
   }

   return false;
}

void
VideoWindow::hideCursor()
{
   setCursor( TQt::BlankCursor );
}

} //namespace Codeine