]> git.sesse.net Git - vlc/blob - modules/gui/skins/src/vlcproc.cpp
* all: show/hide the interface with middle-click on the vout
[vlc] / modules / gui / skins / src / vlcproc.cpp
1 /*****************************************************************************
2  * vlcproc.cpp: VlcProc class
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: vlcproc.cpp,v 1.40 2003/06/24 22:26:01 asmax Exp $
6  *
7  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8  *          Emmanuel Puig    <karibu@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111,
23  * USA.
24  *****************************************************************************/
25
26 //--- VLC -------------------------------------------------------------------
27 #include <vlc/vlc.h>
28 #include <vlc/intf.h>
29 #include <vlc/aout.h>
30 #include <vlc/vout.h>
31
32 //--- SKIN ------------------------------------------------------------------
33 #include "../os_api.h"
34 #include "event.h"
35 #include "banks.h"
36 #include "theme.h"
37 #include "../os_theme.h"
38 #include "themeloader.h"
39 #include "window.h"
40 #include "vlcproc.h"
41 #include "skin_common.h"
42 #include "dialogs.h"
43
44 //---------------------------------------------------------------------------
45 // VlcProc
46 //---------------------------------------------------------------------------
47 VlcProc::VlcProc( intf_thread_t *_p_intf )
48 {
49     p_intf = _p_intf; 
50     
51     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf, 
52         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
53     if( p_playlist != NULL )
54     {
55         // We want to be noticed of playlit changes
56         var_AddCallback( p_playlist, "intf-change", RefreshCallback, this );
57         
58         // Raise/lower interface with middle click on vout
59         var_AddCallback( p_playlist, "intf-show", IntfShowCallback, this );
60         
61         vlc_object_release( p_playlist );   
62     }
63 }
64 //---------------------------------------------------------------------------
65 VlcProc::~VlcProc()
66 {
67     // Remove the refresh callback
68     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf, 
69         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
70     if( p_playlist != NULL )
71     {
72         var_DelCallback( p_playlist, "intf-change", RefreshCallback, this );
73         vlc_object_release( p_playlist );
74     }
75 }
76 //---------------------------------------------------------------------------
77 bool VlcProc::EventProc( Event *evt )
78 {
79     switch( evt->GetMessage() )
80     {
81         case VLC_STREAMPOS:
82             MoveStream( evt->GetParam2() );
83             return true;
84
85         case VLC_VOLUME_CHANGE:
86             ChangeVolume( evt->GetParam1(), evt->GetParam2() );
87             return true;
88
89         case VLC_FULLSCREEN:
90             FullScreen();
91             return true;
92
93         case VLC_HIDE:
94             for( list<SkinWindow *>::const_iterator win =
95                     p_intf->p_sys->p_theme->WindowList.begin();
96                  win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
97             {
98                 (*win)->OnStartThemeVisible = !(*win)->IsHidden();
99             }
100             p_intf->p_sys->i_close_status = (int)evt->GetParam1();
101             OSAPI_PostMessage( NULL, WINDOW_CLOSE, 1, 0 );
102             return true;
103
104         case VLC_SHOW:
105             for( list<SkinWindow *>::const_iterator win =
106                     p_intf->p_sys->p_theme->WindowList.begin();
107                  win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
108             {
109                 if( (*win)->OnStartThemeVisible )
110                     OSAPI_PostMessage( (*win), WINDOW_OPEN, 1, 0 );
111             }
112             p_intf->p_sys->b_all_win_closed = false;
113             return true;
114
115         case VLC_OPEN:
116             p_intf->p_sys->p_dialogs->ShowOpen( true );
117             InterfaceRefresh();
118             return true;
119
120         case VLC_LOAD_SKIN:
121             LoadSkin();
122             return true;
123
124         case VLC_DROP:
125             DropFile( evt->GetParam1() );
126             return true;
127
128         case VLC_PLAY:
129             PlayStream();
130             return true;
131
132         case VLC_PAUSE:
133             PauseStream();
134             return true;
135
136         case VLC_STOP:
137             StopStream();
138             return true;
139
140         case VLC_NEXT:
141             NextStream();
142             return true;
143
144         case VLC_PREV:
145             PrevStream();
146             return true;
147
148         case VLC_PLAYLIST_ADD_FILE:
149             p_intf->p_sys->p_dialogs->ShowOpen( false );
150             InterfaceRefresh();
151             return true;
152
153         case VLC_LOG_SHOW:
154             p_intf->p_sys->p_dialogs->ShowMessages();
155             return true;
156
157         case VLC_LOG_CLEAR:
158             return true;
159
160         case VLC_PREFS_SHOW:
161             p_intf->p_sys->p_dialogs->ShowPrefs();
162             return true;
163
164         case VLC_INFO_SHOW:
165             p_intf->p_sys->p_dialogs->ShowFileInfo();
166             return true;
167
168         case VLC_INTF_REFRESH:
169             InterfaceRefresh();
170             return true;
171
172         case VLC_TEST_ALL_CLOSED:
173             return EventProcEnd();
174
175         case VLC_QUIT:
176             return false;
177
178         case VLC_CHANGE_TRAY:
179             p_intf->p_sys->p_theme->ChangeTray();
180             return true;
181
182         case VLC_CHANGE_TASKBAR:
183             p_intf->p_sys->p_theme->ChangeTaskbar();
184             return true;
185
186         case VLC_NET_ADDUDP:
187             AddNetworkUDP( (int)evt->GetParam2() );
188             return true;
189
190         default:
191             return true;
192     }
193 }
194 //---------------------------------------------------------------------------
195 bool VlcProc::EventProcEnd()
196 {
197     if( p_intf->p_sys->b_all_win_closed )
198         return true;
199
200     list<SkinWindow *>::const_iterator win;
201
202     // If a window has been closed, test if all are closed !
203     for( win = p_intf->p_sys->p_theme->WindowList.begin();
204          win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
205     {
206         if( !(*win)->IsHidden() )   // Not all windows closed
207         {
208             return true;
209         }
210     }
211
212     // All window are closed
213     switch( p_intf->p_sys->i_close_status )
214     {
215         case VLC_QUIT:
216             // Save config before exiting
217             p_intf->p_sys->p_theme->SaveConfig();
218             break;
219     }
220
221     // Send specified event
222     OSAPI_PostMessage( NULL, p_intf->p_sys->i_close_status, 0, 0 );
223
224     // Reset values
225     p_intf->p_sys->i_close_status = VLC_NOTHING;
226     p_intf->p_sys->b_all_win_closed = true;
227
228     // Return true
229     return true;
230 }
231 //---------------------------------------------------------------------------
232 bool VlcProc::IsClosing()
233 {
234     if( p_intf->b_die && p_intf->p_sys->i_close_status != VLC_QUIT )
235     {
236         p_intf->p_sys->i_close_status = VLC_QUIT;
237         OSAPI_PostMessage( NULL, VLC_HIDE, VLC_QUIT, 0 );
238     }
239     return true;
240 }
241 //---------------------------------------------------------------------------
242
243
244
245
246 //---------------------------------------------------------------------------
247 // Private methods
248 //---------------------------------------------------------------------------
249
250 // Refresh callback
251 int VlcProc::RefreshCallback( vlc_object_t *p_this, const char *psz_variable,
252         vlc_value_t old_val, vlc_value_t new_val, void *param )
253 {
254     ( (VlcProc*)param )->InterfaceRefresh();
255     return VLC_SUCCESS;
256 }
257
258 // Interface show/hide callback
259 int VlcProc::IntfShowCallback( vlc_object_t *p_this, const char *psz_variable,
260         vlc_value_t old_val, vlc_value_t new_val, void *param )
261 {
262     if( new_val.b_bool == VLC_TRUE )
263     {
264         OSAPI_PostMessage( NULL, VLC_SHOW, 1, 0 );
265     }
266     else
267     {
268         OSAPI_PostMessage( NULL, VLC_HIDE, 1, 0 );
269     }
270     return VLC_SUCCESS;
271 }
272
273 void VlcProc::InterfaceRefresh()
274 {
275     // Shortcut pointers
276     intf_sys_t  *Sys      = p_intf->p_sys;
277     Theme       *Thema    = Sys->p_theme;
278     playlist_t  *PlayList = Sys->p_playlist;
279
280     // Refresh
281     if( PlayList != NULL )
282     {
283         // Refresh stream control controls ! :)
284         switch( PlayList->i_status )
285         {
286             case PLAYLIST_STOPPED:
287                 EnabledEvent( "time", false );
288                 EnabledEvent( "stop", false );
289                 EnabledEvent( "play", true );
290                 EnabledEvent( "pause", false );
291                 break;
292             case PLAYLIST_RUNNING:
293                 EnabledEvent( "time", true );
294                 EnabledEvent( "stop", true );
295                 EnabledEvent( "play", false );
296                 EnabledEvent( "pause", true );
297                 break;
298             case PLAYLIST_PAUSED:
299                 EnabledEvent( "time", true );
300                 EnabledEvent( "stop", true );
301                 EnabledEvent( "play", true );
302                 EnabledEvent( "pause", false );
303                 break;
304         }
305
306         // Refresh next and prev buttons
307         if( PlayList->i_index == 0 || PlayList->i_size == 1 )
308             EnabledEvent( "prev", false );
309         else
310             EnabledEvent( "prev", true );
311
312         if( PlayList->i_index == PlayList->i_size - 1 || PlayList->i_size == 1 )
313             EnabledEvent( "next", false );
314         else
315             EnabledEvent( "next", true );
316
317         // Update file name
318         if( PlayList->i_index != Sys->i_index )
319         {
320             string long_name = PlayList->pp_items[PlayList->i_index]->psz_name;
321             int pos = long_name.rfind( DIRECTORY_SEPARATOR, long_name.size() );
322
323             // Complete file name
324             Thema->EvtBank->Get( "file_name" )->PostTextMessage(
325                 PlayList->pp_items[PlayList->i_index]->psz_name );
326             // File name without path
327             Thema->EvtBank->Get( "title" )->PostTextMessage(
328                 PlayList->pp_items[PlayList->i_index]->psz_name + pos + 1 );
329         }
330
331         // Update playlists
332         if( PlayList->i_index != Sys->i_index ||
333             PlayList->i_size != Sys->i_size )
334         {
335             Thema->EvtBank->Get( "playlist_refresh" )->PostSynchroMessage();
336             Sys->i_size  = PlayList->i_size;
337             Sys->i_index = PlayList->i_index;
338         }
339     }
340     else
341     {
342         EnabledEvent( "time", false );
343         EnabledEvent( "stop",  false );
344         EnabledEvent( "play",  false );
345         EnabledEvent( "pause", false );
346         EnabledEvent( "prev",  false );
347         EnabledEvent( "next",  false );
348
349         // Update playlists
350         if( Sys->i_size > 0 )
351         {
352             Thema->EvtBank->Get( "playlist_refresh" )->PostSynchroMessage();
353             Sys->i_size  = 0;
354         }
355     }
356 }
357 //---------------------------------------------------------------------------
358 void VlcProc::EnabledEvent( string type, bool state )
359 {
360     OSAPI_PostMessage( NULL, CTRL_ENABLED, (unsigned int)
361         p_intf->p_sys->p_theme->EvtBank->Get( type ), (int)state );
362 }
363 //---------------------------------------------------------------------------
364
365
366 //---------------------------------------------------------------------------
367 // Common VLC procedures
368 //---------------------------------------------------------------------------
369 void VlcProc::LoadSkin()
370 {
371     if( p_intf->p_sys->p_new_theme_file == NULL )
372     {
373         p_intf->p_sys->p_dialogs->ShowOpenSkin();
374     }
375     else
376     {
377         // Place a new theme in the global structure, because it will
378         // be filled by the parser
379         // We save the old one to restore it in case of problem
380         Theme *oldTheme = p_intf->p_sys->p_theme;
381         p_intf->p_sys->p_theme = (Theme *)new OSTheme( p_intf );
382
383         // Run the XML parser
384         ThemeLoader *Loader = new ThemeLoader( p_intf );
385         if( Loader->Load( p_intf->p_sys->p_new_theme_file ) )
386         {
387             // Everything went well
388             msg_Dbg( p_intf, "New theme successfully loaded" );
389             delete (OSTheme *)oldTheme;
390
391             // Show the theme
392             p_intf->p_sys->p_theme->InitTheme();
393             p_intf->p_sys->p_theme->ShowTheme();
394         }
395         else
396         {
397             msg_Warn( p_intf, "A problem occurred when loading the new theme,"
398                       " restoring the previous one" );
399             delete (OSTheme *)p_intf->p_sys->p_theme;
400             p_intf->p_sys->p_theme = oldTheme;
401
402             // Show the theme
403             p_intf->p_sys->p_theme->ShowTheme();
404         }
405         delete Loader;
406
407         // Uninitialize new theme
408         delete[] p_intf->p_sys->p_new_theme_file;
409         p_intf->p_sys->p_new_theme_file = NULL;
410     }
411 }
412 //---------------------------------------------------------------------------
413
414 void VlcProc::DropFile( unsigned int param )
415 {
416     // Get pointer to file
417     char *FileName = (char *)param;
418
419     // Add the new file to the playlist
420     if( p_intf->p_sys->p_playlist != NULL )
421     {
422         if( config_GetInt( p_intf, "enqueue" ) )
423         {
424             playlist_Add( p_intf->p_sys->p_playlist, FileName,
425                           PLAYLIST_APPEND, PLAYLIST_END );
426         }
427         else
428         {
429             playlist_Add( p_intf->p_sys->p_playlist, FileName,
430                           PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
431         }
432     }
433
434     // VLC_DROP must be called with a pointer to a char else it will
435     // ******** SEGFAULT ********
436     // The delete is here because the processus in asynchronous
437     delete[] FileName;
438
439     // Refresh interface
440     InterfaceRefresh();
441
442 }
443 //---------------------------------------------------------------------------
444
445
446
447
448 //---------------------------------------------------------------------------
449 // Stream Control
450 //---------------------------------------------------------------------------
451 void VlcProc::PauseStream()
452 {
453     if( p_intf->p_sys->p_input == NULL )
454         return;
455     input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PAUSE );
456
457     // Refresh interface
458     InterfaceRefresh();
459 }
460 //---------------------------------------------------------------------------
461 void VlcProc::PlayStream()
462 {
463     if( p_intf->p_sys->p_playlist == NULL )
464         return;
465
466     if( !p_intf->p_sys->p_playlist->i_size )
467     {
468         p_intf->p_sys->p_dialogs->ShowOpen( true );
469         InterfaceRefresh();
470         return;
471     }
472
473     playlist_Play( p_intf->p_sys->p_playlist );
474
475     // Refresh interface
476     InterfaceRefresh();
477 }
478 //---------------------------------------------------------------------------
479 void VlcProc::StopStream()
480 {
481     if( p_intf->p_sys->p_playlist == NULL )
482         return;
483     playlist_Stop( p_intf->p_sys->p_playlist );
484
485     // Refresh interface
486     InterfaceRefresh();
487 }
488 //---------------------------------------------------------------------------
489 void VlcProc::NextStream()
490 {
491     if( p_intf->p_sys->p_playlist == NULL )
492         return;
493
494     playlist_Next( p_intf->p_sys->p_playlist );
495
496     // Refresh interface
497     InterfaceRefresh();
498 }
499 //---------------------------------------------------------------------------
500 void VlcProc::PrevStream()
501 {
502     if( p_intf->p_sys->p_playlist == NULL )
503         return;
504
505     playlist_Prev( p_intf->p_sys->p_playlist );
506
507     // Refresh interface
508     InterfaceRefresh();
509 }
510 //---------------------------------------------------------------------------
511 void VlcProc::MoveStream( long Pos )
512 {
513     if( p_intf->p_sys->p_input == NULL )
514         return;
515
516     off_t i_seek = (off_t)(Pos *
517         p_intf->p_sys->p_input->stream.p_selected_area->i_size
518         / SLIDER_RANGE);
519
520     input_Seek( p_intf->p_sys->p_input, i_seek, INPUT_SEEK_SET );
521
522     // Refresh interface
523     InterfaceRefresh();
524 }
525 //---------------------------------------------------------------------------
526
527
528
529 //---------------------------------------------------------------------------
530 // Fullscreen
531 //---------------------------------------------------------------------------
532 void VlcProc::FullScreen()
533 {
534     vout_thread_t *p_vout;
535
536     if( p_intf->p_sys->p_input == NULL )
537         return;
538
539     p_vout = (vout_thread_t *)vlc_object_find( p_intf->p_sys->p_input,
540                                                VLC_OBJECT_VOUT, FIND_CHILD );
541     if( p_vout == NULL )
542         return;
543
544     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
545     vlc_object_release( p_vout );
546 }
547 //---------------------------------------------------------------------------
548
549
550
551 //---------------------------------------------------------------------------
552 // Volume Control
553 //---------------------------------------------------------------------------
554 void VlcProc::ChangeVolume( unsigned int msg, long param )
555 {
556     audio_volume_t volume;
557     switch( msg )
558     {
559         case VLC_VOLUME_MUTE:
560             aout_VolumeMute( p_intf, NULL );
561             break;
562         case VLC_VOLUME_UP:
563             aout_VolumeUp( p_intf, 1, NULL );
564             break;
565         case VLC_VOLUME_DOWN:
566             aout_VolumeDown( p_intf, 1, NULL );
567             break;
568         case VLC_VOLUME_SET:
569             aout_VolumeSet( p_intf, param * AOUT_VOLUME_MAX / SLIDER_RANGE );
570             break;
571     }
572     aout_VolumeGet( p_intf, &volume );
573
574 }
575 //---------------------------------------------------------------------------
576
577
578 //---------------------------------------------------------------------------
579 // Network
580 //---------------------------------------------------------------------------
581 void VlcProc::AddNetworkUDP( int port )
582 {
583     // Build source name
584     char *s_port = new char[5];
585     sprintf( s_port, "%i", port );
586     string source = "udp:@:" + (string)s_port;
587     delete[] s_port;
588
589     playlist_Add( p_intf->p_sys->p_playlist, (char *)source.c_str(),
590         PLAYLIST_APPEND, PLAYLIST_END );
591
592     // Refresh interface !
593     p_intf->p_sys->p_theme->EvtBank->Get( "playlist_refresh" )
594         ->PostSynchroMessage();
595     InterfaceRefresh();
596 }
597 //---------------------------------------------------------------------------