qte_main.cpp 6.51 KB
Newer Older
1 2 3
/*****************************************************************************
 * qte_main.c : QT Embedded wrapper for gte_main
 *****************************************************************************
4
 * Copyright (C) 2003 the VideoLAN team
Jean-Paul Saman's avatar
Jean-Paul Saman committed
5
 * $Id$
6
 *
Jean-Paul Saman's avatar
Jean-Paul Saman committed
7
 * Authors: Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27 28 29 30 31 32 33
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
extern "C"
{
#include <vlc/vlc.h>
#include <stdlib.h>                                              /* atexit() */
}

#include <qapplication.h>
Jean-Paul Saman's avatar
Jean-Paul Saman committed
34
#include <qpainter.h>
35 36 37 38 39 40

extern "C"
{

typedef struct qte_thread_t
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
41
    VLC_COMMON_MEMBERS
42 43

    QApplication*       p_qte_application;
44
    QWidget*            p_qte_widget;
Sam Hocevar's avatar
Sam Hocevar committed
45
    bool                b_gui_server;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

} qte_thread_t;

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
static int  Open    ( vlc_object_t * );
static void Close   ( vlc_object_t * );

static void QteMain ( qte_thread_t * );

/*****************************************************************************
 * Local variables (mutex-protected).
 *****************************************************************************/
static int            i_refcount = 0;
static qte_thread_t * p_qte_main = NULL;

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
66 67 68 69
#define STANDALONE_TEXT N_("Run as standalone Qt/Embedded GUI Server")
#define STANDALONE_LONGTEXT N_("Use this option to run as standalone " \
    "Qt/Embedded GUI Server. This option is equivalent to the -qws option " \
    "from normal Qt.")
70

71
vlc_module_begin();
Gildas Bazin's avatar
 
Gildas Bazin committed
72
    set_description( _("Qt Embedded GUI helper") );
73
    set_capability( "gui-helper", 90 );
74
    add_bool( "qte-guiserver", 0, NULL, STANDALONE_TEXT, STANDALONE_LONGTEXT, VLC_FALSE );
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
    add_shortcut( "qte" );
    set_callbacks( Open, Close );
vlc_module_end();

} /* extern "C" */

/*****************************************************************************
 * Open: initialize and create window
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    vlc_value_t lockval;

    /* FIXME: put this in the module (de)initialization ASAP */
    var_Create( p_this->p_libvlc, "qte", VLC_VAR_MUTEX );

    var_Get( p_this->p_libvlc, "qte", &lockval );
    vlc_mutex_lock( (vlc_mutex_t *) lockval.p_address );

    if( i_refcount > 0 )
    {
        i_refcount++;
        vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );

        return VLC_SUCCESS;
    }

    p_qte_main = (qte_thread_t *) vlc_object_create( p_this, sizeof(qte_thread_t) );

104 105
    /* Launch the QApplication::exec() thread. It will not return until the
     * application is properly initialized, which ensures us thread safety. */
106 107 108 109 110 111 112 113 114 115 116 117 118
    if( vlc_thread_create( p_qte_main, "qte_main", QteMain,
                           VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
    {
        vlc_object_destroy( p_qte_main );
        i_refcount--;
        vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
        var_Destroy( p_this->p_libvlc, "qte" );
        return VLC_ETHREAD;
    }

    i_refcount++;
    vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );

119 120 121
    vlc_object_attach( p_qte_main, p_this );
    msg_Dbg( p_this, "qte_main running" );

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: destroy interface window
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    vlc_value_t lockval;

    var_Get( p_this->p_libvlc, "qte", &lockval );
    vlc_mutex_lock( (vlc_mutex_t *) lockval.p_address );

    i_refcount--;

    if( i_refcount > 0 )
    {
        vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
        var_Destroy( p_this->p_libvlc, "qte" );
        return;
    }
    p_qte_main->p_qte_application->quit();

Jean-Paul Saman's avatar
Jean-Paul Saman committed
145 146
    /* Cleanup allocated classes. */
    delete p_qte_main->p_qte_widget;
147
    delete p_qte_main->p_qte_application;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
148

149 150 151
    msg_Dbg( p_this, "Detaching qte_main" );
    vlc_object_detach( p_qte_main );

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    vlc_object_destroy( p_qte_main );
    p_qte_main = NULL;

    vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
    var_Destroy( p_this->p_libvlc, "qte" );
}

/*****************************************************************************
 * QteMain: Qt Embedded thread
 *****************************************************************************
 * this part of the interface is in a separate thread so that we can call
 * qte_main() from within it without annoying the rest of the program.
 *****************************************************************************/
static void QteMain( qte_thread_t *p_this )
{
167
    int i_argc = 1;
168

169 170 171
    p_this->b_gui_server = VLC_FALSE;
    if( config_GetInt( p_this, "qte-guiserver" ) )
    {
Sam Hocevar's avatar
Sam Hocevar committed
172
        msg_Dbg( p_this, "Running as Qt Embedded standalone GuiServer" );
173 174 175 176 177 178
        p_this->b_gui_server = VLC_TRUE;
    }

    /* Run as standalone GuiServer or as GuiClient. */
    QApplication* pApp = new QApplication(i_argc, NULL,
        (p_this->b_gui_server ? (QApplication::GuiServer):(QApplication::GuiClient)) );
179 180 181 182 183
    if(pApp)
    {
        p_this->p_qte_application = pApp;
    }

184
    QWidget* pWidget = new QWidget(0, _("video") );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
185 186 187 188 189
    if(pWidget)
    {
        p_this->p_qte_widget = pWidget;
    }

190
    /* signal the creation of the window */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
191
    p_this->p_qte_application->setMainWidget(p_this->p_qte_widget);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
192
    p_this->p_qte_widget->show();
Jean-Paul Saman's avatar
Jean-Paul Saman committed
193

194 195 196 197
    vlc_thread_ready( p_this );
    p_this->p_qte_application->exec();
}