]> git.sesse.net Git - vlc/blob - modules/misc/qte_main.cpp
For consistency, remove references to vlc from libvlc
[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 #include <vlc/vlc.h>
30 #include <stdlib.h>                                              /* atexit() */
31 }
32
33 #include <qapplication.h>
34 #include <qpainter.h>
35
36 extern "C"
37 {
38
39 typedef struct qte_thread_t
40 {
41     VLC_COMMON_MEMBERS
42
43     QApplication*       p_qte_application;
44     QWidget*            p_qte_widget;
45     bool                b_gui_server;
46
47 } qte_thread_t;
48
49 /*****************************************************************************
50  * Local prototypes.
51  *****************************************************************************/
52 static int  Open    ( vlc_object_t * );
53 static void Close   ( vlc_object_t * );
54
55 static void QteMain ( qte_thread_t * );
56
57 /*****************************************************************************
58  * Local variables (mutex-protected).
59  *****************************************************************************/
60 static int            i_refcount = 0;
61 static qte_thread_t * p_qte_main = NULL;
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66 #define STANDALONE_TEXT N_("Run as standalone Qt/Embedded GUI Server")
67 #define STANDALONE_LONGTEXT N_("Use this option to run as standalone " \
68     "Qt/Embedded GUI Server. This option is equivalent to the -qws option " \
69     "from normal Qt.")
70
71 vlc_module_begin();
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     add_shortcut( "qte" );
76     set_callbacks( Open, Close );
77 vlc_module_end();
78
79 } /* extern "C" */
80
81 /*****************************************************************************
82  * Open: initialize and create window
83  *****************************************************************************/
84 static int Open( vlc_object_t *p_this )
85 {
86     vlc_value_t lockval;
87
88     /* FIXME: put this in the module (de)initialization ASAP */
89     var_Create( p_this->p_libvlc_global, "qte", VLC_VAR_MUTEX );
90
91     var_Get( p_this->p_libvlc_global, "qte", &lockval );
92     vlc_mutex_lock( (vlc_mutex_t *) lockval.p_address );
93
94     if( i_refcount > 0 )
95     {
96         i_refcount++;
97         vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
98
99         return VLC_SUCCESS;
100     }
101
102     p_qte_main = (qte_thread_t *) vlc_object_create( p_this, sizeof(qte_thread_t) );
103
104     /* Launch the QApplication::exec() thread. It will not return until the
105      * application is properly initialized, which ensures us thread safety. */
106     if( vlc_thread_create( p_qte_main, "qte_main", QteMain,
107                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
108     {
109         vlc_object_destroy( p_qte_main );
110         i_refcount--;
111         vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
112         var_Destroy( p_this->p_libvlc_global, "qte" );
113         return VLC_ETHREAD;
114     }
115
116     i_refcount++;
117     vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
118
119     vlc_object_attach( p_qte_main, p_this );
120     msg_Dbg( p_this, "qte_main running" );
121
122     return VLC_SUCCESS;
123 }
124
125 /*****************************************************************************
126  * Close: destroy interface window
127  *****************************************************************************/
128 static void Close( vlc_object_t *p_this )
129 {
130     vlc_value_t lockval;
131
132     var_Get( p_this->p_libvlc_global, "qte", &lockval );
133     vlc_mutex_lock( (vlc_mutex_t *) lockval.p_address );
134
135     i_refcount--;
136
137     if( i_refcount > 0 )
138     {
139         vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
140         var_Destroy( p_this->p_libvlc_global, "qte" );
141         return;
142     }
143     p_qte_main->p_qte_application->quit();
144
145     /* Cleanup allocated classes. */
146     delete p_qte_main->p_qte_widget;
147     delete p_qte_main->p_qte_application;
148
149     msg_Dbg( p_this, "Detaching qte_main" );
150     vlc_object_detach( p_qte_main );
151
152     vlc_object_destroy( p_qte_main );
153     p_qte_main = NULL;
154
155     vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
156     var_Destroy( p_this->p_libvlc_global, "qte" );
157 }
158
159 /*****************************************************************************
160  * QteMain: Qt Embedded thread
161  *****************************************************************************
162  * this part of the interface is in a separate thread so that we can call
163  * qte_main() from within it without annoying the rest of the program.
164  *****************************************************************************/
165 static void QteMain( qte_thread_t *p_this )
166 {
167     int i_argc = 1;
168
169     p_this->b_gui_server = VLC_FALSE;
170     if( config_GetInt( p_this, "qte-guiserver" ) )
171     {
172         msg_Dbg( p_this, "Running as Qt Embedded standalone GuiServer" );
173         p_this->b_gui_server = VLC_TRUE;
174     }
175
176     /* Run as standalone GuiServer or as GuiClient. */
177     QApplication* pApp = new QApplication(i_argc, NULL,
178         (p_this->b_gui_server ? (QApplication::GuiServer):(QApplication::GuiClient)) );
179     if(pApp)
180     {
181         p_this->p_qte_application = pApp;
182     }
183
184     QWidget* pWidget = new QWidget(0, _("video") );
185     if(pWidget)
186     {
187         p_this->p_qte_widget = pWidget;
188     }
189
190     /* signal the creation of the window */
191     p_this->p_qte_application->setMainWidget(p_this->p_qte_widget);
192     p_this->p_qte_widget->show();
193
194     vlc_thread_ready( p_this );
195     p_this->p_qte_application->exec();
196 }
197