]> git.sesse.net Git - vlc/blob - modules/misc/qte_main.cpp
* Fix segfault when we have two announces with the same URI but different names on...
[vlc] / modules / misc / qte_main.cpp
1 /*****************************************************************************
2  * qte_main.c : QT Embedded wrapper for gte_main
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: qte_main.cpp,v 1.7 2003/06/09 19:56:26 jpsaman Exp $
6  *
7  * Authors: Jean-Paul Saman <jpsaman@wxs.nl>
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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 Qt/Embedded Gui Server. " \
68         "This option is equivalent to the -qws option from normal Qt.")
69
70 vlc_module_begin();
71     set_description( _("Qt Embedded GUI helper") );
72     set_capability( "gui-helper", 90 );
73     add_bool( "qte-guiserver", 0, NULL, STANDALONE_TEXT, STANDALONE_LONGTEXT, VLC_FALSE );
74     add_shortcut( "qte" );
75     set_callbacks( Open, Close );
76 vlc_module_end();
77
78 } /* extern "C" */
79
80 /*****************************************************************************
81  * Open: initialize and create window
82  *****************************************************************************/
83 static int Open( vlc_object_t *p_this )
84 {
85     vlc_value_t lockval;
86
87     /* FIXME: put this in the module (de)initialization ASAP */
88     var_Create( p_this->p_libvlc, "qte", VLC_VAR_MUTEX );
89
90     var_Get( p_this->p_libvlc, "qte", &lockval );
91     vlc_mutex_lock( (vlc_mutex_t *) lockval.p_address );
92
93     if( i_refcount > 0 )
94     {
95         i_refcount++;
96         vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
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, VLC_TRUE ) )
107     {
108         vlc_object_destroy( p_qte_main );
109         i_refcount--;
110         vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
111         var_Destroy( p_this->p_libvlc, "qte" );
112         return VLC_ETHREAD;
113     }
114
115     i_refcount++;
116     vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
117
118     return VLC_SUCCESS;
119 }
120
121 /*****************************************************************************
122  * Close: destroy interface window
123  *****************************************************************************/
124 static void Close( vlc_object_t *p_this )
125 {
126     vlc_value_t lockval;
127
128     var_Get( p_this->p_libvlc, "qte", &lockval );
129     vlc_mutex_lock( (vlc_mutex_t *) lockval.p_address );
130
131     i_refcount--;
132
133     if( i_refcount > 0 )
134     {
135         vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
136         var_Destroy( p_this->p_libvlc, "qte" );
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     vlc_object_destroy( p_qte_main );
146     p_qte_main = NULL;
147
148     vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
149     var_Destroy( p_this->p_libvlc, "qte" );
150 }
151
152 /*****************************************************************************
153  * QteMain: Qt Embedded thread
154  *****************************************************************************
155  * this part of the interface is in a separate thread so that we can call
156  * qte_main() from within it without annoying the rest of the program.
157  *****************************************************************************/
158 static void QteMain( qte_thread_t *p_this )
159 {
160     int i_argc = 1;
161     char *p_args[] = {"-qws", NULL};
162     char **pp_args = p_args;
163
164     p_this->b_gui_server = VLC_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 = VLC_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();
180     if(pWidget)
181     {
182         p_this->p_qte_widget = pWidget;
183     }
184
185     if (p_this->b_gui_server) {
186         p_this->p_qte_application->desktop()->setFixedSize(240, 320);
187     }
188     /* signal the creation of the window */
189     p_this->p_qte_application->setMainWidget(p_this->p_qte_widget);
190     p_this->p_qte_widget->show();
191
192     vlc_thread_ready( p_this );
193     p_this->p_qte_application->exec();
194 }
195