]> git.sesse.net Git - vlc/blob - modules/misc/qte_main.cpp
Merge branch 'master' of git@git.videolan.org:vlc
[vlc] / modules / misc / qte_main.cpp
1 /*****************************************************************************
2  * qte_main.c : QT Embedded wrapper for gte_main
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 extern "C"
28 {
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc/vlc.h>
34 }
35
36 #include <qapplication.h>
37 #include <qpainter.h>
38
39 extern "C"
40 {
41
42 typedef struct qte_thread_t
43 {
44     VLC_COMMON_MEMBERS
45
46     QApplication*       p_qte_application;
47     QWidget*            p_qte_widget;
48     bool                b_gui_server;
49
50 } qte_thread_t;
51
52 /*****************************************************************************
53  * Local prototypes.
54  *****************************************************************************/
55 static int  Open    ( vlc_object_t * );
56 static void Close   ( vlc_object_t * );
57
58 static void QteMain ( qte_thread_t * );
59
60 /*****************************************************************************
61  * Local variables (mutex-protected).
62  *****************************************************************************/
63 static int            i_refcount = 0;
64 static qte_thread_t * p_qte_main = NULL;
65
66 /*****************************************************************************
67  * Module descriptor
68  *****************************************************************************/
69 #define STANDALONE_TEXT N_("Run as standalone Qt/Embedded GUI Server")
70 #define STANDALONE_LONGTEXT N_("Use this option to run as standalone " \
71     "Qt/Embedded GUI Server. This option is equivalent to the -qws option " \
72     "from normal Qt.")
73
74 vlc_module_begin();
75     set_description( _("Qt Embedded GUI helper") );
76     set_capability( "gui-helper", 90 );
77     add_bool( "qte-guiserver", 0, NULL, STANDALONE_TEXT, STANDALONE_LONGTEXT, false );
78     add_shortcut( "qte" );
79     set_callbacks( Open, Close );
80 vlc_module_end();
81
82 } /* extern "C" */
83
84 /*****************************************************************************
85  * Open: initialize and create window
86  *****************************************************************************/
87 static int Open( vlc_object_t *p_this )
88 {
89     vlc_mutex_t *lock;
90
91     lock = var_AcquireMutex( "qte" );
92
93     if( i_refcount > 0 )
94     {
95         i_refcount++;
96         vlc_mutex_unlock( lock );
97
98         return VLC_SUCCESS;
99     }
100
101     p_qte_main = (qte_thread_t *) vlc_object_create( p_this, sizeof(qte_thread_t) );
102
103     /* Launch the QApplication::exec() thread. It will not return until the
104      * application is properly initialized, which ensures us thread safety. */
105     if( vlc_thread_create( p_qte_main, "qte_main", QteMain,
106                            VLC_THREAD_PRIORITY_LOW, true ) )
107     {
108         vlc_object_release( p_qte_main );
109         i_refcount--;
110         vlc_mutex_unlock( lock );
111         return VLC_ETHREAD;
112     }
113
114     i_refcount++;
115     vlc_mutex_unlock( lock );
116
117     vlc_object_attach( p_qte_main, p_this );
118     msg_Dbg( p_this, "qte_main running" );
119
120     return VLC_SUCCESS;
121 }
122
123 /*****************************************************************************
124  * Close: destroy interface window
125  *****************************************************************************/
126 static void Close( vlc_object_t *p_this )
127 {
128     vlc_mutex_t *lock;
129
130     lock = var_AcquireMutex( "qte" );
131
132     i_refcount--;
133
134     if( i_refcount > 0 )
135     {
136         vlc_mutex_unlock( lock );
137         return;
138     }
139     p_qte_main->p_qte_application->quit();
140
141     /* Cleanup allocated classes. */
142     delete p_qte_main->p_qte_widget;
143     delete p_qte_main->p_qte_application;
144
145     msg_Dbg( p_this, "Detaching qte_main" );
146     vlc_object_detach( p_qte_main );
147
148     vlc_object_release( p_qte_main );
149     p_qte_main = NULL;
150
151     vlc_mutex_unlock( lock );
152 }
153
154 /*****************************************************************************
155  * QteMain: Qt Embedded thread
156  *****************************************************************************
157  * this part of the interface is in a separate thread so that we can call
158  * qte_main() from within it without annoying the rest of the program.
159  *****************************************************************************/
160 static void QteMain( qte_thread_t *p_this )
161 {
162     int i_argc = 1;
163
164     p_this->b_gui_server = false;
165     if( config_GetInt( p_this, "qte-guiserver" ) )
166     {
167         msg_Dbg( p_this, "Running as Qt Embedded standalone GuiServer" );
168         p_this->b_gui_server = true;
169     }
170
171     /* Run as standalone GuiServer or as GuiClient. */
172     QApplication* pApp = new QApplication(i_argc, NULL,
173         (p_this->b_gui_server ? (QApplication::GuiServer):(QApplication::GuiClient)) );
174     if( pApp )
175     {
176         p_this->p_qte_application = pApp;
177     }
178
179     QWidget* pWidget = new QWidget(0, _("video") );
180     if( pWidget )
181     {
182         p_this->p_qte_widget = pWidget;
183     }
184
185     /* signal the creation of the window */
186     p_this->p_qte_application->setMainWidget(p_this->p_qte_widget);
187     p_this->p_qte_widget->show();
188
189     vlc_thread_ready( p_this );
190     p_this->p_qte_application->exec();
191 }