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