rd: simplify events handling
[vlc.git] / modules / gui / qt / actions_manager.cpp
blobf81d9ec7d88bacea231e9fe2649abe6fefc9e9d1
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_keys.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 , p_rd( NULL )
44 { }
46 ActionsManager::~ActionsManager()
48 if ( p_rd != NULL )
49 vlc_rd_release( p_rd );
52 void ActionsManager::doAction( int id_action )
54 switch( id_action )
56 case PLAY_ACTION:
57 play(); break;
58 case STOP_ACTION:
59 THEMIM->stop(); break;
60 case OPEN_ACTION:
61 THEDP->openDialog(); break;
62 case PREVIOUS_ACTION:
63 THEMIM->prev(); break;
64 case NEXT_ACTION:
65 THEMIM->next(); break;
66 case SLOWER_ACTION:
67 THEMIM->getIM()->slower(); break;
68 case FASTER_ACTION:
69 THEMIM->getIM()->faster(); break;
70 case FULLSCREEN_ACTION:
71 fullscreen(); break;
72 case EXTENDED_ACTION:
73 THEDP->extendedDialog(); break;
74 case PLAYLIST_ACTION:
75 playlist(); break;
76 case SNAPSHOT_ACTION:
77 snapshot(); break;
78 case RECORD_ACTION:
79 record(); break;
80 case FRAME_ACTION:
81 frame(); break;
82 case ATOB_ACTION:
83 THEMIM->getIM()->setAtoB(); break;
84 case REVERSE_ACTION:
85 THEMIM->getIM()->reverse(); break;
86 case SKIP_BACK_ACTION:
87 skipBackward();
88 break;
89 case SKIP_FW_ACTION:
90 skipForward();
91 break;
92 case QUIT_ACTION:
93 THEDP->quit(); break;
94 case RANDOM_ACTION:
95 THEMIM->toggleRandom(); break;
96 case INFO_ACTION:
97 THEDP->mediaInfoDialog(); break;
98 case OPEN_SUB_ACTION:
99 THEDP->loadSubtitlesFile(); break;
100 case FULLWIDTH_ACTION:
101 if( p_intf->p_sys->p_mi )
102 p_intf->p_sys->p_mi->getFullscreenControllerWidget()->toggleFullwidth();
103 break;
104 default:
105 msg_Warn( p_intf, "Action not supported: %i", id_action );
106 break;
110 void ActionsManager::play()
112 if( THEPL->current.i_size == 0 && THEPL->items.i_size == 0 )
114 /* The playlist is empty, open a file requester */
115 THEDP->openFileDialog();
116 return;
118 THEMIM->togglePlayPause();
122 * TODO
123 * This functions toggle the fullscreen mode
124 * If there is no video, it should first activate Visualisations...
125 * This has also to be fixed in enableVideo()
127 void ActionsManager::fullscreen()
129 bool fs = var_ToggleBool( THEPL, "fullscreen" );
130 vout_thread_t *p_vout = THEMIM->getVout();
131 if( p_vout)
133 var_SetBool( p_vout, "fullscreen", fs );
134 vlc_object_release( p_vout );
138 void ActionsManager::snapshot()
140 vout_thread_t *p_vout = THEMIM->getVout();
141 if( p_vout )
143 var_TriggerCallback( p_vout, "video-snapshot" );
144 vlc_object_release( p_vout );
148 void ActionsManager::playlist()
150 if( p_intf->p_sys->p_mi )
151 p_intf->p_sys->p_mi->togglePlaylist();
154 void ActionsManager::record()
156 input_thread_t *p_input = THEMIM->getInput();
157 if( p_input )
159 /* This method won't work fine if the stream can't be cut anywhere */
160 var_ToggleBool( p_input, "record" );
161 #if 0
162 else
164 /* 'record' access-filter is not loaded, we open Save dialog */
165 input_item_t *p_item = input_GetItem( p_input );
166 if( !p_item )
167 return;
169 char *psz = input_item_GetURI( p_item );
170 if( psz )
171 THEDP->streamingDialog( NULL, qfu(psz), true );
173 #endif
177 void ActionsManager::frame()
179 input_thread_t *p_input = THEMIM->getInput();
180 if( p_input )
181 var_TriggerCallback( p_input, "frame-next" );
184 void ActionsManager::toggleMuteAudio()
186 playlist_MuteToggle( THEPL );
189 void ActionsManager::AudioUp()
191 playlist_VolumeUp( THEPL, 1, NULL );
194 void ActionsManager::AudioDown()
196 playlist_VolumeDown( THEPL, 1, NULL );
199 void ActionsManager::skipForward()
201 input_thread_t *p_input = THEMIM->getInput();
202 if( p_input )
203 THEMIM->getIM()->jumpFwd();
206 void ActionsManager::skipBackward()
208 input_thread_t *p_input = THEMIM->getInput();
209 if( p_input )
210 THEMIM->getIM()->jumpBwd();
213 void ActionsManager::PPaction( QAction *a )
215 int i_q = -1;
216 if( a != NULL )
217 i_q = a->data().toInt();
219 ExtVideo::setPostprocessing( p_intf, i_q );
222 bool ActionsManager::isItemSout( QVariant & m_obj, const char *psz_sout, bool as_output )
224 if ( psz_sout == NULL )
225 return false;
226 if (!m_obj.canConvert<QVariantHash>())
227 return false;
229 QVariantHash hash = m_obj.value<QVariantHash>();
230 QString renderer(psz_sout);
231 if ( as_output && renderer.at(0) == '#' )
232 renderer = renderer.right( renderer.length() - 1 );
233 return QString::compare( hash["sout"].toString(), renderer, Qt::CaseInsensitive) == 0;
236 void ActionsManager::renderer_event_item_added(
237 vlc_renderer_discovery_t *rd, vlc_renderer_item_t *p_item )
239 intf_thread_t *p_intf = reinterpret_cast<intf_thread_t*>(rd->owner.sys);
240 QAction *firstSeparator = NULL;
242 foreach (QAction* action, VLCMenuBar::rendererMenu->actions())
244 if (action->isSeparator())
246 firstSeparator = action;
247 break;
249 QVariant v = action->data();
250 if ( isItemSout( v, vlc_renderer_item_sout( p_item ), false ) )
251 return; /* we already have this item */
254 QHash<QString,QVariant> itemData;
255 itemData.insert("sout", vlc_renderer_item_sout( p_item ));
256 itemData.insert("filter", vlc_renderer_item_demux_filter( p_item ));
257 QVariant data(itemData);
259 QAction *action = new QAction( vlc_renderer_item_flags(p_item) & VLC_RENDERER_CAN_VIDEO ? QIcon( ":/sidebar/movie" ) : QIcon( ":/sidebar/music" ),
260 vlc_renderer_item_name(p_item), VLCMenuBar::rendererMenu );
261 action->setCheckable(true);
262 action->setData(data);
263 if (firstSeparator != NULL)
265 VLCMenuBar::rendererMenu->insertAction( firstSeparator, action );
266 VLCMenuBar::rendererGroup->addAction(action);
269 char *psz_renderer = var_InheritString( THEPL, "sout" );
270 if ( psz_renderer != NULL )
272 if ( isItemSout( data, psz_renderer, true ) )
273 action->setChecked( true );
274 free( psz_renderer );
278 void ActionsManager::renderer_event_item_removed(
279 vlc_renderer_discovery_t *rd, vlc_renderer_item_t *p_item )
281 (void) rd; (void) p_item;
284 void ActionsManager::ScanRendererAction(bool checked)
286 if (checked == (p_rd != NULL))
287 return; /* nothing changed */
289 if (checked)
291 /* reset the list of renderers */
292 foreach (QAction* action, VLCMenuBar::rendererMenu->actions())
294 QVariant data = action->data();
295 if (!data.canConvert<QVariantHash>())
296 continue;
297 VLCMenuBar::rendererMenu->removeAction(action);
298 VLCMenuBar::rendererGroup->removeAction(action);
300 char *psz_renderer = var_InheritString( THEPL, "sout" );
301 if ( psz_renderer == NULL || *psz_renderer == '\0' )
303 foreach (QAction* action, VLCMenuBar::rendererMenu->actions())
305 if (!action->data().canConvert<QVariantHash>())
306 continue;
307 if (!action->isSeparator())
308 action->setChecked(true);
309 break;
312 free( psz_renderer );
314 /* SD subnodes */
315 char **ppsz_longnames;
316 char **ppsz_names;
317 if( vlc_rd_get_names( THEPL, &ppsz_names, &ppsz_longnames ) != VLC_SUCCESS )
318 return;
320 struct vlc_renderer_discovery_owner owner =
322 p_intf,
323 renderer_event_item_added,
324 renderer_event_item_removed,
327 char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
328 for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
330 /* TODO launch all discovery services for renderers */
331 msg_Dbg( p_intf, "starting renderer discovery service %s", *ppsz_longname );
332 if ( p_rd == NULL )
334 p_rd = vlc_rd_new( VLC_OBJECT(p_intf), *ppsz_name, &owner );
335 if( !p_rd )
336 msg_Err( p_intf, "Could not start renderer discovery services" );
338 break;
340 free( ppsz_names );
341 free( ppsz_longnames );
343 if ( p_rd != NULL )
345 if( vlc_rd_start( p_rd ) != VLC_SUCCESS )
347 vlc_rd_release( p_rd );
348 p_rd = NULL;
352 else
354 if ( p_rd != NULL )
356 vlc_rd_release( p_rd );
357 p_rd = NULL;
362 void ActionsManager::RendererSelected( QAction *selected )
364 QString s_sout, s_demux_filter;
365 QVariant data = selected->data();
366 if (data.canConvert<QVariantHash>())
368 QVariantHash hash = data.value<QVariantHash>();
369 s_sout.append('#');
370 s_sout.append(hash["sout"].toString());
371 s_demux_filter.append(hash["filter"].toString());
373 msg_Dbg( p_intf, "using sout: '%s'", s_sout.toUtf8().constData() );
374 var_SetString( THEPL, "sout", s_sout.toUtf8().constData() );
375 var_SetString( THEPL, "demux-filter", s_demux_filter.toUtf8().constData() );