]> git.sesse.net Git - vlc/blob - modules/misc/qte_main.cpp
* modules/gui/wxwindows/*: misc fixes.
[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.6 2003/03/30 18:14:38 gbazin 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
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 vlc_module_begin();
66     set_description( _("Qt Embedded GUI helper") );
67     set_capability( "gui-helper", 90 );
68     add_shortcut( "qte" );
69     set_callbacks( Open, Close );
70 vlc_module_end();
71
72 } /* extern "C" */
73
74 /*****************************************************************************
75  * Open: initialize and create window
76  *****************************************************************************/
77 static int Open( vlc_object_t *p_this )
78 {
79     vlc_value_t lockval;
80
81     /* FIXME: put this in the module (de)initialization ASAP */
82     var_Create( p_this->p_libvlc, "qte", VLC_VAR_MUTEX );
83
84     var_Get( p_this->p_libvlc, "qte", &lockval );
85     vlc_mutex_lock( (vlc_mutex_t *) lockval.p_address );
86
87     if( i_refcount > 0 )
88     {
89         i_refcount++;
90         vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
91
92         return VLC_SUCCESS;
93     }
94
95     p_qte_main = (qte_thread_t *) vlc_object_create( p_this, sizeof(qte_thread_t) );
96
97     /* Launch the QApplication::exec() thread. It will not return until the
98      * application is properly initialized, which ensures us thread safety. */
99     if( vlc_thread_create( p_qte_main, "qte_main", QteMain,
100                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
101     {
102         vlc_object_destroy( p_qte_main );
103         i_refcount--;
104         vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
105         var_Destroy( p_this->p_libvlc, "qte" );
106         return VLC_ETHREAD;
107     }
108
109     i_refcount++;
110     vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
111
112     return VLC_SUCCESS;
113 }
114
115 /*****************************************************************************
116  * Close: destroy interface window
117  *****************************************************************************/
118 static void Close( vlc_object_t *p_this )
119 {
120     vlc_value_t lockval;
121
122     var_Get( p_this->p_libvlc, "qte", &lockval );
123     vlc_mutex_lock( (vlc_mutex_t *) lockval.p_address );
124
125     i_refcount--;
126
127     if( i_refcount > 0 )
128     {
129         vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
130         var_Destroy( p_this->p_libvlc, "qte" );
131         return;
132     }
133     p_qte_main->p_qte_application->quit();
134
135     /* Cleanup allocated classes. */
136     delete p_qte_main->p_qte_widget;
137     delete p_qte_main->p_qte_application;
138
139     vlc_object_destroy( p_qte_main );
140     p_qte_main = NULL;
141
142     vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address );
143     var_Destroy( p_this->p_libvlc, "qte" );
144 }
145
146 /*****************************************************************************
147  * QteMain: Qt Embedded thread
148  *****************************************************************************
149  * this part of the interface is in a separate thread so that we can call
150  * qte_main() from within it without annoying the rest of the program.
151  *****************************************************************************/
152 static void QteMain( qte_thread_t *p_this )
153 {
154     int argc = 0;
155
156     QApplication* pApp = new QApplication(argc, NULL);
157     if(pApp)
158     {
159         p_this->p_qte_application = pApp;
160     }
161
162     QWidget* pWidget = new QWidget();
163     if(pWidget)
164     {
165         p_this->p_qte_widget = pWidget;
166     }
167
168     /* signal the creation of the window */
169     p_this->p_qte_application->setMainWidget(p_this->p_qte_widget);
170     p_this->p_qte_widget->show();
171
172     vlc_thread_ready( p_this );
173     p_this->p_qte_application->exec();
174 }
175