qt: action manager: Don't disable renderer from UI
[vlc.git] / modules / gui / qt / actions_manager.cpp
blob98f42023ee32d585528c047c5e047fe7eb1d3900
1 /*****************************************************************************
2 * actions_manager.cpp : Controller for the main interface
3 ****************************************************************************
4 * Copyright © 2009-2014 VideoLAN and VLC authors
5 * $Id$
7 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
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.
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.
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <vlc_vout.h>
29 #include <vlc_actions.h>
30 #include <vlc_renderer_discovery.h>
32 #include "actions_manager.hpp"
34 #include "dialogs_provider.hpp" /* Opening Dialogs */
35 #include "input_manager.hpp" /* THEMIM */
36 #include "main_interface.hpp" /* Show playlist */
37 #include "components/controller.hpp" /* Toggle FSC controller width */
38 #include "components/extended_panels.hpp"
39 #include "menus.hpp"
41 ActionsManager::ActionsManager( intf_thread_t * _p_i )
42 : p_intf( _p_i )
43 { }
45 ActionsManager::~ActionsManager()
47 foreach ( vlc_renderer_discovery_t* p_rd, m_rds )
48 vlc_rd_release( p_rd );
51 void ActionsManager::doAction( int id_action )
53 switch( id_action )
55 case PLAY_ACTION:
56 play(); break;
57 case STOP_ACTION:
58 THEMIM->stop(); break;
59 case OPEN_ACTION:
60 THEDP->openDialog(); break;
61 case PREVIOUS_ACTION:
62 THEMIM->prev(); break;
63 case NEXT_ACTION:
64 THEMIM->next(); break;
65 case SLOWER_ACTION:
66 THEMIM->getIM()->slower(); break;
67 case FASTER_ACTION:
68 THEMIM->getIM()->faster(); break;
69 case FULLSCREEN_ACTION:
70 fullscreen(); break;
71 case EXTENDED_ACTION:
72 THEDP->extendedDialog(); break;
73 case PLAYLIST_ACTION:
74 playlist(); break;
75 case SNAPSHOT_ACTION:
76 snapshot(); break;
77 case RECORD_ACTION:
78 record(); break;
79 case FRAME_ACTION:
80 frame(); break;
81 case ATOB_ACTION:
82 THEMIM->getIM()->setAtoB(); break;
83 case REVERSE_ACTION:
84 THEMIM->getIM()->reverse(); break;
85 case SKIP_BACK_ACTION:
86 skipBackward();
87 break;
88 case SKIP_FW_ACTION:
89 skipForward();
90 break;
91 case QUIT_ACTION:
92 THEDP->quit(); break;
93 case RANDOM_ACTION:
94 THEMIM->toggleRandom(); break;
95 case INFO_ACTION:
96 THEDP->mediaInfoDialog(); break;
97 case OPEN_SUB_ACTION:
98 THEDP->loadSubtitlesFile(); break;
99 case FULLWIDTH_ACTION:
100 if( p_intf->p_sys->p_mi )
101 p_intf->p_sys->p_mi->getFullscreenControllerWidget()->toggleFullwidth();
102 break;
103 default:
104 msg_Warn( p_intf, "Action not supported: %i", id_action );
105 break;
109 void ActionsManager::play()
111 if( THEPL->current.i_size == 0 && THEPL->items.i_size == 0 )
113 /* The playlist is empty, open a file requester */
114 THEDP->openFileDialog();
115 return;
117 THEMIM->togglePlayPause();
121 * TODO
122 * This functions toggle the fullscreen mode
123 * If there is no video, it should first activate Visualisations...
124 * This has also to be fixed in enableVideo()
126 void ActionsManager::fullscreen()
128 bool fs = var_ToggleBool( THEPL, "fullscreen" );
129 vout_thread_t *p_vout = THEMIM->getVout();
130 if( p_vout)
132 var_SetBool( p_vout, "fullscreen", fs );
133 vlc_object_release( p_vout );
137 void ActionsManager::snapshot()
139 vout_thread_t *p_vout = THEMIM->getVout();
140 if( p_vout )
142 var_TriggerCallback( p_vout, "video-snapshot" );
143 vlc_object_release( p_vout );
147 void ActionsManager::playlist()
149 if( p_intf->p_sys->p_mi )
150 p_intf->p_sys->p_mi->togglePlaylist();
153 void ActionsManager::record()
155 input_thread_t *p_input = THEMIM->getInput();
156 if( p_input )
158 /* This method won't work fine if the stream can't be cut anywhere */
159 var_ToggleBool( p_input, "record" );
160 #if 0
161 else
163 /* 'record' access-filter is not loaded, we open Save dialog */
164 input_item_t *p_item = input_GetItem( p_input );
165 if( !p_item )
166 return;
168 char *psz = input_item_GetURI( p_item );
169 if( psz )
170 THEDP->streamingDialog( NULL, qfu(psz), true );
172 #endif
176 void ActionsManager::frame()
178 input_thread_t *p_input = THEMIM->getInput();
179 if( p_input )
180 var_TriggerCallback( p_input, "frame-next" );
183 void ActionsManager::toggleMuteAudio()
185 playlist_MuteToggle( THEPL );
188 void ActionsManager::AudioUp()
190 playlist_VolumeUp( THEPL, 1, NULL );
193 void ActionsManager::AudioDown()
195 playlist_VolumeDown( THEPL, 1, NULL );
198 void ActionsManager::skipForward()
200 input_thread_t *p_input = THEMIM->getInput();
201 if( p_input )
202 THEMIM->getIM()->jumpFwd();
205 void ActionsManager::skipBackward()
207 input_thread_t *p_input = THEMIM->getInput();
208 if( p_input )
209 THEMIM->getIM()->jumpBwd();
212 bool ActionsManager::compareRenderers( const QVariant &obj, vlc_renderer_item_t* p_item )
214 if (!obj.canConvert<QVariantHash>())
215 return false;
216 QVariantHash qvh = obj.value<QVariantHash>();
217 if (!qvh.contains( "sout" ))
218 return false;
219 vlc_renderer_item_t* p_existing =
220 reinterpret_cast<vlc_renderer_item_t*>( qvh["sout"].value<void*>() );
221 return !strcasecmp(vlc_renderer_item_sout( p_existing ),
222 vlc_renderer_item_sout( p_item ) );
225 void ActionsManager::renderer_event_item_added( vlc_renderer_discovery_t*,
226 vlc_renderer_item_t *p_item )
228 QAction *firstSeparator = NULL;
230 foreach (QAction* action, VLCMenuBar::rendererMenu->actions())
232 if (action->isSeparator())
234 firstSeparator = action;
235 break;
237 QVariant v = action->data();
238 if (compareRenderers( action->data(), p_item ))
239 return; /* we already have this item */
242 QAction *action = new QAction( vlc_renderer_item_flags(p_item) & VLC_RENDERER_CAN_VIDEO ? QIcon( ":/sidebar/movie.svg" ) : QIcon( ":/sidebar/music.svg" ),
243 vlc_renderer_item_name(p_item), VLCMenuBar::rendererMenu );
244 action->setCheckable(true);
246 vlc_renderer_item_hold( p_item );
247 QVariantHash data;
248 data.insert( "sout", QVariant::fromValue( reinterpret_cast<void*>( p_item ) ) );
249 action->setData( data );
250 if (firstSeparator != NULL)
252 VLCMenuBar::rendererMenu->insertAction( firstSeparator, action );
253 VLCMenuBar::rendererGroup->addAction(action);
257 void ActionsManager::renderer_event_item_removed(
258 vlc_renderer_discovery_t *, vlc_renderer_item_t *p_item )
260 foreach (QAction* action, VLCMenuBar::rendererMenu->actions())
262 if (action->isSeparator())
263 continue;
264 if (compareRenderers( action->data(), p_item ))
266 VLCMenuBar::rendererMenu->removeAction( action );
267 VLCMenuBar::rendererGroup->removeAction( action );
268 vlc_renderer_item_release( p_item );
269 return;
274 void ActionsManager::ScanRendererAction(bool checked)
276 if (checked == !m_rds.empty())
277 return; /* nothing changed */
279 if (checked)
281 /* reset the list of renderers */
282 foreach (QAction* action, VLCMenuBar::rendererMenu->actions())
284 QVariant data = action->data();
285 if (!data.canConvert<QVariantHash>())
286 continue;
287 VLCMenuBar::rendererMenu->removeAction(action);
288 VLCMenuBar::rendererGroup->removeAction(action);
291 /* SD subnodes */
292 char **ppsz_longnames;
293 char **ppsz_names;
294 if( vlc_rd_get_names( THEPL, &ppsz_names, &ppsz_longnames ) != VLC_SUCCESS )
295 return;
297 struct vlc_renderer_discovery_owner owner =
299 p_intf,
300 renderer_event_item_added,
301 renderer_event_item_removed,
304 char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
305 for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
307 msg_Dbg( p_intf, "starting renderer discovery service %s", *ppsz_longname );
308 vlc_renderer_discovery_t* p_rd = vlc_rd_new( VLC_OBJECT(p_intf), *ppsz_name, &owner );
309 if( p_rd != NULL )
310 m_rds.push_back( p_rd );
311 free( *ppsz_name );
312 free( *ppsz_longname );
314 free( ppsz_names );
315 free( ppsz_longnames );
317 else
319 foreach ( vlc_renderer_discovery_t* p_rd, m_rds )
320 vlc_rd_release( p_rd );
321 m_rds.clear();
325 void ActionsManager::RendererSelected( QAction *selected )
327 QVariant data = selected->data();
328 vlc_renderer_item_t *p_item = NULL;
329 if (data.canConvert<QVariantHash>())
331 QVariantHash hash = data.value<QVariantHash>();
332 if ( hash.contains( "sout" ) )
333 p_item = reinterpret_cast<vlc_renderer_item_t*>(
334 hash["sout"].value<void*>() );
336 // If we failed to convert the action data to a vlc_renderer_item_t,
337 // assume the selected item was invalid, or most likely that "Local" was selected
338 playlist_SetRenderer( THEPL, p_item );