]> git.sesse.net Git - vlc/blob - modules/misc/qte_main.cpp
Checks pointers before using them
[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 }
31
32 #include <qapplication.h>
33 #include <qpainter.h>
34
35 extern "C"
36 {
37
38 typedef struct qte_thread_t
39 {
40     VLC_COMMON_MEMBERS
41
42     QApplication*       p_qte_application;
43     QWidget*            p_qte_widget;
44     bool                b_gui_server;
45
46 } qte_thread_t;
47
48 /*****************************************************************************
49  * Local prototypes.
50  *****************************************************************************/
51 static int  Open    ( vlc_object_t * );
52 static void Close   ( vlc_object_t * );
53
54 static void QteMain ( qte_thread_t * );
55
56 /*****************************************************************************
57  * Local variables (mutex-protected).
58  *****************************************************************************/
59 static int            i_refcount = 0;
60 static qte_thread_t * p_qte_main = NULL;
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 #define STANDALONE_TEXT N_("Run as standalone Qt/Embedded GUI Server")
66 #define STANDALONE_LONGTEXT N_("Use this option to run as standalone " \
67     "Qt/Embedded GUI Server. This option is equivalent to the -qws option " \
68     "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_mutex_t *lock;
86
87     lock = var_GetGlobalMutex( "qte" );
88     vlc_mutex_lock( lockval );
89
90     if( i_refcount > 0 )
91     {
92         i_refcount++;
93         vlc_mutex_unlock( lock );
94
95         return VLC_SUCCESS;
96     }
97
98     p_qte_main = (qte_thread_t *) vlc_object_create( p_this, sizeof(qte_thread_t) );
99
100     /* Launch the QApplication::exec() thread. It will not return until the
101      * application is properly initialized, which ensures us thread safety. */
102     if( vlc_thread_create( p_qte_main, "qte_main", QteMain,
103                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
104     {
105         vlc_object_destroy( p_qte_main );
106         i_refcount--;
107         vlc_mutex_unlock( lock );
108         return VLC_ETHREAD;
109     }
110
111     i_refcount++;
112     vlc_mutex_unlock( lock );
113
114     vlc_object_attach( p_qte_main, p_this );
115     msg_Dbg( p_this, "qte_main running" );
116
117     return VLC_SUCCESS;
118 }
119
120 /*****************************************************************************
121  * Close: destroy interface window
122  *****************************************************************************/
123 static void Close( vlc_object_t *p_this )
124 {
125     vlc_mutex_t *lock;
126
127     lock = var_GetGlobalMutex( "qte" );
128     vlc_mutex_lock( lock );
129
130     i_refcount--;
131
132     if( i_refcount > 0 )
133     {
134         vlc_mutex_unlock( lock );
135         return;
136     }
137     p_qte_main->p_qte_application->quit();
138
139     /* Cleanup allocated classes. */
140     delete p_qte_main->p_qte_widget;
141     delete p_qte_main->p_qte_application;
142
143     msg_Dbg( p_this, "Detaching qte_main" );
144     vlc_object_detach( p_qte_main );
145
146     vlc_object_destroy( p_qte_main );
147     p_qte_main = NULL;
148
149     vlc_mutex_unlock( lock );
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
162     p_this->b_gui_server = VLC_FALSE;
163     if( config_GetInt( p_this, "qte-guiserver" ) )
164     {
165         msg_Dbg( p_this, "Running as Qt Embedded standalone GuiServer" );
166         p_this->b_gui_server = VLC_TRUE;
167     }
168
169     /* Run as standalone GuiServer or as GuiClient. */
170     QApplication* pApp = new QApplication(i_argc, NULL,
171         (p_this->b_gui_server ? (QApplication::GuiServer):(QApplication::GuiClient)) );
172     if( pApp )
173     {
174         p_this->p_qte_application = pApp;
175     }
176
177     QWidget* pWidget = new QWidget(0, _("video") );
178     if( pWidget )
179     {
180         p_this->p_qte_widget = pWidget;
181     }
182
183     /* signal the creation of the window */
184     p_this->p_qte_application->setMainWidget(p_this->p_qte_widget);
185     p_this->p_qte_widget->show();
186
187     vlc_thread_ready( p_this );
188     p_this->p_qte_application->exec();
189 }