video_widget.cpp 7.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * video_widget.cpp : Embedded video output
 ****************************************************************************
4 5
 * Copyright (C) 2006 the VideoLAN team
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

zorglub's avatar
zorglub committed
24
#include "dialogs_provider.hpp"
25 26 27 28
#include <vlc/vout.h>
#include "qt4.hpp"
#include "components/video_widget.hpp"
#include "main_interface.hpp"
29
#include "input_manager.hpp"
30
#include <QHBoxLayout>
31

zorglub's avatar
zorglub committed
32 33
#define ICON_SIZE 128

34 35 36 37 38
static void *DoRequest( intf_thread_t *, vout_thread_t *, int*,int*,
                        unsigned int *, unsigned int * );
static void DoRelease( intf_thread_t *, void * );
static int DoControl( intf_thread_t *, void *, int, va_list );

zorglub's avatar
zorglub committed
39
bool need_update;
40 41 42

VideoWidget::VideoWidget( intf_thread_t *_p_i, bool _always ) : QFrame( NULL ),
                                                               p_intf( _p_i )
43 44
{
    vlc_mutex_init( p_intf, &lock );
45
    always = _always;
46 47 48 49 50 51 52 53

    p_intf->pf_request_window  = ::DoRequest;
    p_intf->pf_release_window  = ::DoRelease;
    p_intf->pf_control_window  = ::DoControl;
    p_intf->p_sys->p_video = this;
    p_vout = NULL;

    setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
zorglub's avatar
zorglub committed
54

zorglub's avatar
zorglub committed
55
    ON_TIMEOUT( update() );
zorglub's avatar
zorglub committed
56

57
    if( always )
zorglub's avatar
zorglub committed
58
    {
zorglub's avatar
zorglub committed
59 60 61
        DrawBackground();
        CONNECT( THEMIM->getIM(), audioStarted(), this, hasAudio() );
        CONNECT( THEMIM->getIM(), audioStarted(), this, hasVideo() );
zorglub's avatar
zorglub committed
62
    }
zorglub's avatar
zorglub committed
63 64 65 66 67 68 69 70 71 72
    need_update = false;
}

void VideoWidget::update()
{
    if( need_update )
    {
        p_intf->p_sys->p_mi->resize( p_intf->p_sys->p_mi->sizeHint() );
        need_update = false;
    }
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
}

VideoWidget::~VideoWidget()
{
    vlc_mutex_lock( &lock );
    if( p_vout )
    {
        if( !p_intf->psz_switch_intf )
        {
            if( vout_Control( p_vout, VOUT_CLOSE ) != VLC_SUCCESS )
                vout_Control( p_vout, VOUT_REPARENT );
        }
        else
        {
            if( vout_Control( p_vout, VOUT_REPARENT ) != VLC_SUCCESS )
                vout_Control( p_vout, VOUT_CLOSE );
        }
    }
    p_intf->pf_request_window = NULL;
    p_intf->pf_release_window = NULL;
    p_intf->pf_control_window = NULL;
    vlc_mutex_unlock( &lock );
    vlc_mutex_destroy( &lock );
zorglub's avatar
zorglub committed
96 97 98

    if( always )
        CleanBackground();
99 100 101 102
}

QSize VideoWidget::sizeHint() const
{
zorglub's avatar
zorglub committed
103
    return p_intf->p_sys->p_mi->videoSize;
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
}

static void *DoRequest( intf_thread_t *p_intf, vout_thread_t *p_vout,
                        int *pi1, int *pi2, unsigned int*pi3,unsigned int*pi4)
{
    return p_intf->p_sys->p_video->Request( p_vout, pi1, pi2, pi3, pi4 );
}

void *VideoWidget::Request( vout_thread_t *p_nvout, int *pi_x, int *pi_y,
                           unsigned int *pi_width, unsigned int *pi_height )
{
    if( p_vout )
    {
        msg_Dbg( p_intf, "embedded video already in use" );
        return NULL;
    }
    p_vout = p_nvout;

122 123 124 125 126 127 128 129 130 131 132 133 134 135
    // if( THEMIM->getIM()->b_has_video )
    // {
    //      We are really running a video
    //      Close the existing vout
    //      Set visual to disabled
    // }
    // else
    // {
    //      We are getting a request for visual
    //      Just go on.
    // }
    // Check THEMIM->b_has_audio. If true, hide audio.

    setMinimumSize( 1,1 );
zorglub's avatar
zorglub committed
136
    p_intf->p_sys->p_mi->videoSize = QSize( *pi_width, *pi_height );
137
    updateGeometry();
zorglub's avatar
zorglub committed
138
    need_update = true;
139 140 141 142 143 144 145 146
    return  (void*)winId();
}

static void DoRelease( intf_thread_t *p_intf, void *p_win )
{
    return p_intf->p_sys->p_video->Release( p_win );
}

zorglub's avatar
zorglub committed
147 148
void VideoWidget::resizeEvent( QResizeEvent *e )
{
zorglub's avatar
zorglub committed
149
    if( !always )return;
zorglub's avatar
zorglub committed
150 151 152 153 154 155
    if( e->size().height() < ICON_SIZE -1 )
        label->setMaximumWidth( e->size().height() );
    else
        label->setMaximumWidth( ICON_SIZE );
}

156 157
void VideoWidget::Release( void *p_win )
{
158 159
    p_vout = NULL;
    if( config_GetInt( p_intf, "qt-always-video" ) == 0 )
zorglub's avatar
zorglub committed
160 161
    {
        p_intf->p_sys->p_mi->videoSize = QSize ( 1,1 );
162 163 164
        updateGeometry();
        need_update = true;
    }
165 166
}

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
void VideoWidget::hasAudio()
{
    /* We have video already, do nothing */
    if( THEMIM->getIM()->b_has_video )
    {

    }
    else
    {
        /* Show the panel to the user */
        fprintf( stderr, "Showing panel\n" );
    }
}

void VideoWidget::hasVideo()
{
    // if panel is shown, hide it
}

186 187 188
static int DoControl( intf_thread_t *p_intf, void *p_win, int i_q, va_list a )
{
    return p_intf->p_sys->p_video->Control( p_win, i_q, a );
189 190 191 192
}

int VideoWidget::DrawBackground()
{
zorglub's avatar
zorglub committed
193 194 195 196 197 198 199 200 201 202 203 204
    setAutoFillBackground( true );
    plt =  palette();
    plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
    plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
    setPalette( plt );

    backgroundLayout = new QHBoxLayout;
    label = new QLabel( "" );
    label->setMaximumHeight( ICON_SIZE );
    label->setMaximumWidth( ICON_SIZE );
    label->setScaledContents( true );
    label->setPixmap( QPixmap( ":/vlc128.png" ) );
205 206 207 208 209 210 211 212 213 214 215 216
    backgroundLayout = new QHBoxLayout;
    backgroundLayout->addWidget( label );
    setLayout( backgroundLayout );
    return 0;
}

int VideoWidget::CleanBackground()
{
    backgroundLayout->takeAt(0);
    delete backgroundLayout;
    return 0;
}
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

int VideoWidget::Control( void *p_window, int i_query, va_list args )
{
    int i_ret = VLC_EGENERIC;
    vlc_mutex_lock( &lock );
    switch( i_query )
    {
        case VOUT_GET_SIZE:
        {
            unsigned int *pi_width  = va_arg( args, unsigned int * );
            unsigned int *pi_height = va_arg( args, unsigned int * );
            *pi_width = frame->width();
            *pi_height = frame->height();
            i_ret = VLC_SUCCESS;
            break;
        }
        case VOUT_SET_SIZE:
        {
            unsigned int i_width  = va_arg( args, unsigned int );
            unsigned int i_height = va_arg( args, unsigned int );

            if( !i_width && p_vout ) i_width = p_vout->i_window_width;
            if( !i_height && p_vout ) i_height = p_vout->i_window_height;
240 241 242
            p_intf->p_sys->p_mi->videoSize = QSize( i_width, i_height );
            updateGeometry();
            need_update = true;
243
            i_ret = VLC_SUCCESS;
244
            break;
245 246 247 248 249
        }
        case VOUT_SET_STAY_ON_TOP:
        {
            /// \todo
            break;
250
        }
251 252 253 254 255 256 257
        default:
            msg_Warn( p_intf, "unsupported control query" );
            break;
    }
    vlc_mutex_unlock( &lock );
    return i_ret;
}