]> git.sesse.net Git - vlc/blob - modules/gui/qt4/qt4.cpp
Fix a refcount issue
[vlc] / modules / gui / qt4 / qt4.cpp
1 /*****************************************************************************
2  * qt4.cpp : QT4 interface
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.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 #include <QApplication>
24 #include "qt4.hpp"
25 #include "dialogs_provider.hpp"
26 #include "input_manager.hpp"
27 #include "main_interface.hpp"
28
29 /*****************************************************************************
30  * Local prototypes.
31  *****************************************************************************/
32 static int  Open         ( vlc_object_t * );
33 static void Close        ( vlc_object_t * );
34 static int  OpenDialogs  ( vlc_object_t * );
35 static void Run          ( intf_thread_t * );
36 static void Init         ( intf_thread_t * );
37 static void ShowDialog   ( intf_thread_t *, int, int, intf_dialog_args_t * );
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 vlc_module_begin();
43     set_shortname( (char*)"QT" );
44     set_description( (char*)_("QT interface") );
45     set_category( CAT_INTERFACE) ;
46     set_subcategory( SUBCAT_INTERFACE_MAIN );
47     set_capability( "interface", 100 );
48     set_callbacks( Open, Close );
49
50     set_program( "qvlc" );
51     add_shortcut("qt");
52
53     add_submodule();
54         set_description( "Dialogs provider" );
55         set_capability( "dialogs provider", 51 );
56         add_bool( "qt-always-video", VLC_FALSE, NULL, "", "", VLC_TRUE );
57         set_callbacks( OpenDialogs, Close );
58 vlc_module_end();
59
60
61 /*****************************************************************************
62  * Module callbacks
63  *****************************************************************************/
64 static int Open( vlc_object_t *p_this )
65 {
66     intf_thread_t *p_intf = (intf_thread_t *)p_this;
67     p_intf->pf_run = Run;
68     p_intf->p_sys = (intf_sys_t *)malloc(sizeof( intf_sys_t ) );
69     memset( p_intf->p_sys, 0, sizeof( intf_sys_t ) );
70
71     p_intf->p_sys->p_playlist = (playlist_t *)vlc_object_find( p_intf,
72                             VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
73     if( !p_intf->p_sys->p_playlist )
74         return VLC_EGENERIC;
75
76     p_intf->p_sys->p_sub = msg_Subscribe( p_intf, MSG_QUEUE_NORMAL );
77
78     return VLC_SUCCESS;
79 }
80
81 static int OpenDialogs( vlc_object_t *p_this )
82 {
83     intf_thread_t *p_intf = (intf_thread_t *)p_this;
84     Open( p_this );
85     p_intf->pf_show_dialog = ShowDialog;
86     return VLC_SUCCESS;
87 }
88
89 static void Close( vlc_object_t *p_this )
90 {
91     intf_thread_t *p_intf = (intf_thread_t *)p_this;
92     vlc_mutex_lock( &p_intf->object_lock );
93     p_intf->b_dead = VLC_TRUE;
94     vlc_mutex_unlock( &p_intf->object_lock );
95
96     vlc_object_release( p_intf->p_sys->p_playlist );
97     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
98     free( p_intf->p_sys );
99 }
100
101
102 /*****************************************************************************
103  * Initialize the interface or the dialogs provider
104  *****************************************************************************/
105 static void Run( intf_thread_t *p_intf )
106 {
107     if( p_intf->pf_show_dialog )
108     {
109         if( vlc_thread_create( p_intf, "QT dialogs", Init, 0, VLC_TRUE ) )
110         {
111             msg_Err( p_intf, "failed to create QT dialogs thread" );
112         }
113     }
114     else
115     {
116         Init( p_intf );
117     }
118 }
119
120 static void Init( intf_thread_t *p_intf )
121 {
122     char *argv[] = { "" };
123     int argc = 1;
124     Q_INIT_RESOURCE( vlc );
125
126     QApplication *app = new QApplication( argc, argv , true );
127     p_intf->p_sys->p_app = app;
128
129     // Initialize timers
130     DialogsProvider::getInstance( p_intf );
131
132     /* Normal interface */
133     if( !p_intf->pf_show_dialog )
134     {
135         MainInterface *p_mi = new MainInterface( p_intf );
136         p_intf->p_sys->p_mi = p_mi;
137         p_mi->show();
138     }
139
140     if( p_intf->pf_show_dialog )
141         vlc_thread_ready( p_intf );
142
143     app->setQuitOnLastWindowClosed( false );
144     app->exec();
145     MainInputManager::killInstance();
146     delete p_intf->p_sys->p_mi;
147 }
148
149 /*****************************************************************************
150  * Callback to show a dialog
151  *****************************************************************************/
152 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
153                         intf_dialog_args_t *p_arg )
154 {
155     DialogEvent *event = new DialogEvent( i_dialog_event, i_arg, p_arg );
156     QApplication::postEvent( DialogsProvider::getInstance( p_intf ),
157                              static_cast<QEvent*>(event) );
158 }
159
160 /*****************************************************************************
161  * PopupMenuCB: callback to show the popupmenu.
162  *  We don't show the menu directly here because we don't want the
163  *  caller to block for a too long time.
164  *****************************************************************************/
165 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
166                         vlc_value_t old_val, vlc_value_t new_val, void *param )
167 {
168     intf_thread_t *p_intf = (intf_thread_t *)param;
169     ShowDialog( p_intf, INTF_DIALOG_POPUPMENU, new_val.b_bool, 0 );
170     return VLC_SUCCESS;
171 }