bc52f04df038b671e5324099463ed3da0bd7cee0
[vlc.git] / modules / gui / skins / src / vlcproc.cpp
1 /*****************************************************************************
2  * vlcproc.cpp: VlcProc class
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: vlcproc.cpp,v 1.15 2003/04/22 17:56:44 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 #include <wx/wx.h>
27
28 //--- VLC -------------------------------------------------------------------
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31 #include <vlc/aout.h>
32 #include <vlc/vout.h>
33 #include <netutils.h>
34
35 //--- SKIN ------------------------------------------------------------------
36 #include "../os_api.h"
37 #include "event.h"
38 #include "banks.h"
39 #include "theme.h"
40 #include "../os_theme.h"
41 #include "themeloader.h"
42 #include "window.h"
43 #include "vlcproc.h"
44 #include "skin_common.h"
45 #include "wxdialogs.h"
46
47
48
49 //---------------------------------------------------------------------------
50 // VlcProc
51 //---------------------------------------------------------------------------
52 VlcProc::VlcProc( intf_thread_t *_p_intf )
53 {
54     p_intf = _p_intf;
55 }
56 //---------------------------------------------------------------------------
57 bool VlcProc::EventProc( Event *evt )
58 {
59     switch( evt->GetMessage() )
60     {
61         case VLC_STREAMPOS:
62             MoveStream( evt->GetParam2() );
63             return true;
64
65         case VLC_VOLUME_CHANGE:
66             ChangeVolume( evt->GetParam1(), evt->GetParam2() );
67             return true;
68
69         case VLC_FULLSCREEN:
70             FullScreen();
71             return true;
72
73         case VLC_HIDE:
74             for( list<SkinWindow *>::const_iterator win =
75                     p_intf->p_sys->p_theme->WindowList.begin();
76                  win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
77             {
78                 (*win)->OnStartThemeVisible = !(*win)->IsHidden();
79             }
80             p_intf->p_sys->i_close_status = (int)evt->GetParam1();
81             OSAPI_PostMessage( NULL, WINDOW_CLOSE, 1, 0 );
82             return true;
83
84         case VLC_SHOW:
85             for( list<SkinWindow *>::const_iterator win =
86                     p_intf->p_sys->p_theme->WindowList.begin();
87                  win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
88             {
89                 if( (*win)->OnStartThemeVisible )
90                     OSAPI_PostMessage( (*win), WINDOW_OPEN, 1, 0 );
91             }
92             p_intf->p_sys->b_all_win_closed = false;
93             return true;
94
95         case VLC_OPEN:
96             OpenFile( true );
97             return true;
98
99         case VLC_LOAD_SKIN:
100             LoadSkin();
101             return true;
102
103         case VLC_DROP:
104             DropFile( evt->GetParam1() );
105             return true;
106
107         case VLC_PLAY:
108             PlayStream();
109             return true;
110
111         case VLC_PAUSE:
112             PauseStream();
113             return true;
114
115         case VLC_STOP:
116             StopStream();
117             return true;
118
119         case VLC_NEXT:
120             NextStream();
121             return true;
122
123         case VLC_PREV:
124             PrevStream();
125             return true;
126
127         case VLC_PLAYLIST_ADD_FILE:
128             OpenFile( false );
129             return true;
130
131         case VLC_LOG_SHOW:
132             p_intf->p_sys->MessagesDlg->Show(
133                 !p_intf->p_sys->MessagesDlg->IsShown() );
134             return true;
135
136         case VLC_LOG_CLEAR:
137             return true;
138
139         case VLC_PREFS_SHOW:
140             p_intf->p_sys->PrefsDlg->Show(
141                 !p_intf->p_sys->PrefsDlg->IsShown() );
142             return true;
143
144         case VLC_INFO_SHOW:
145             p_intf->p_sys->InfoDlg->Show(
146                 !p_intf->p_sys->InfoDlg->IsShown() );
147             return true;
148
149         case VLC_INTF_REFRESH:
150             InterfaceRefresh( (bool)evt->GetParam2() );
151             return true;
152
153         case VLC_TEST_ALL_CLOSED:
154             return EventProcEnd();
155
156         case VLC_QUIT:
157             return false;
158
159         case VLC_CHANGE_TRAY:
160             p_intf->p_sys->p_theme->ChangeTray();
161             return true;
162
163         case VLC_CHANGE_TASKBAR:
164             p_intf->p_sys->p_theme->ChangeTaskbar();
165             return true;
166
167         case VLC_NET_ADDUDP:
168             AddNetworkUDP( (int)evt->GetParam2() );
169             return true;
170
171         case VLC_NET_ADDCS:
172             AddNetworkChannelServer( (char *)evt->GetParam1() );
173             return true;
174
175         default:
176             return true;
177     }
178 }
179 //---------------------------------------------------------------------------
180 bool VlcProc::EventProcEnd()
181 {
182     if( p_intf->p_sys->b_all_win_closed )
183         return true;
184
185     list<SkinWindow *>::const_iterator win;
186
187     // If a window has been closed, test if all are closed !
188     for( win = p_intf->p_sys->p_theme->WindowList.begin();
189          win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
190     {
191         if( !(*win)->IsHidden() )   // Not all windows closed
192         {
193             return true;
194         }
195     }
196
197     // All window are closed
198     switch( p_intf->p_sys->i_close_status )
199     {
200         case VLC_QUIT:
201             // Save config before exiting
202             p_intf->p_sys->p_theme->SaveConfig();
203             break;
204     }
205
206     // Send specified event
207     OSAPI_PostMessage( NULL, p_intf->p_sys->i_close_status, 0, 0 );
208
209     // Reset values
210     p_intf->p_sys->i_close_status = VLC_NOTHING;
211     p_intf->p_sys->b_all_win_closed = true;
212
213     // Return true
214     return true;
215 }
216 //---------------------------------------------------------------------------
217 bool VlcProc::IsClosing()
218 {
219     if( p_intf->b_die && p_intf->p_sys->i_close_status != VLC_QUIT )
220     {
221         p_intf->p_sys->i_close_status = VLC_QUIT;
222         OSAPI_PostMessage( NULL, VLC_HIDE, VLC_QUIT, 0 );
223     }
224     return true;
225 }
226 //---------------------------------------------------------------------------
227
228
229
230
231 //---------------------------------------------------------------------------
232 // Private methods
233 //---------------------------------------------------------------------------
234 void VlcProc::InterfaceRefresh( bool All )
235 {
236     // Shortcut pointers
237     intf_sys_t  *Sys      = p_intf->p_sys;
238     Theme       *Thema    = Sys->p_theme;
239     playlist_t  *PlayList = Sys->p_playlist;
240
241     // Refresh
242     if( PlayList != NULL )
243     {
244         // Refresh stream control controls ! :)
245         switch( PlayList->i_status )
246         {
247             case PLAYLIST_STOPPED:
248                 EnabledEvent( "time", false );
249                 EnabledEvent( "stop", false );
250                 EnabledEvent( "play", true );
251                 EnabledEvent( "pause", false );
252                 break;
253             case PLAYLIST_RUNNING:
254                 EnabledEvent( "time", true );
255                 EnabledEvent( "stop", true );
256                 EnabledEvent( "play", false );
257                 EnabledEvent( "pause", true );
258                 break;
259             case PLAYLIST_PAUSED:
260                 EnabledEvent( "time", true );
261                 EnabledEvent( "stop", true );
262                 EnabledEvent( "play", true );
263                 EnabledEvent( "pause", false );
264                 break;
265         }
266
267         // Refresh next and prev buttons
268         if( PlayList->i_index == 0 || PlayList->i_size == 1 )
269             EnabledEvent( "prev", false );
270         else
271             EnabledEvent( "prev", true );
272
273         if( PlayList->i_index == PlayList->i_size - 1 || PlayList->i_size == 1 )
274             EnabledEvent( "next", false );
275         else
276             EnabledEvent( "next", true );
277
278
279         // Update file name text
280         if( PlayList->i_index != Sys->i_index )
281         {
282             Thema->EvtBank->Get( "file_name" )->PostTextMessage(
283                 PlayList->pp_items[PlayList->i_index]->psz_name );
284         }
285
286         // Update playlists
287         if( PlayList->i_index != Sys->i_index ||
288             PlayList->i_size != Sys->i_size )
289         {
290             Thema->EvtBank->Get( "playlist_refresh" )->PostSynchroMessage();
291             Sys->i_size  = PlayList->i_size;
292             Sys->i_index = PlayList->i_index;
293         }
294     }
295     else
296     {
297         EnabledEvent( "time", false );
298         EnabledEvent( "stop",  false );
299         EnabledEvent( "play",  false );
300         EnabledEvent( "pause", false );
301         EnabledEvent( "prev",  false );
302         EnabledEvent( "next",  false );
303
304         // Update playlists
305         if( Sys->i_size > 0 )
306         {
307             Thema->EvtBank->Get( "playlist_refresh" )->PostSynchroMessage();
308             Sys->i_size  = 0;
309         }
310     }
311
312 }
313 //---------------------------------------------------------------------------
314 void VlcProc::EnabledEvent( string type, bool state )
315 {
316     OSAPI_PostMessage( NULL, CTRL_ENABLED, (unsigned int)
317         p_intf->p_sys->p_theme->EvtBank->Get( type ), (int)state );
318 }
319 //---------------------------------------------------------------------------
320
321
322
323 //---------------------------------------------------------------------------
324 // Common VLC procedures
325 //---------------------------------------------------------------------------
326 void VlcProc::LoadSkin()
327 {
328     if( p_intf->p_sys->p_new_theme_file == NULL )
329     {
330         wxFileDialog dialog( NULL, _("Open a skin file"), "", "",
331             "Skin files (*.vlt)|*.vlt|Skin files (*.xml)|*.xml|All files|*.*",
332             wxOPEN );
333
334         if( dialog.ShowModal() == wxID_OK )
335         {
336             p_intf->p_sys->p_new_theme_file =
337                 new char[dialog.GetPath().Length()];
338
339             strcpy( p_intf->p_sys->p_new_theme_file,
340                     dialog.GetPath().c_str() );
341
342             // Tell vlc to change skin after hiding interface
343             OSAPI_PostMessage( NULL, VLC_HIDE, VLC_LOAD_SKIN, 0 );
344         }
345     }
346     else
347     {
348         // Place a new theme in the global structure, because it will
349         // be filled by the parser
350         // We save the old one to restore it in case of problem
351         Theme *oldTheme = p_intf->p_sys->p_theme;
352         p_intf->p_sys->p_theme = (Theme *)new OSTheme( p_intf );
353
354         // Run the XML parser
355         ThemeLoader *Loader = new ThemeLoader( p_intf );
356         if( Loader->Load( p_intf->p_sys->p_new_theme_file ) )
357         {
358             // Everything went well
359             msg_Dbg( p_intf, "New theme successfully loaded" );
360             delete (OSTheme *)oldTheme;
361
362             // Show the theme
363             p_intf->p_sys->p_theme->InitTheme();
364             p_intf->p_sys->p_theme->ShowTheme();
365         }
366         else
367         {
368             msg_Warn( p_intf, "A problem occurred when loading the new theme,"
369                       " restoring the previous one" );
370             delete (OSTheme *)p_intf->p_sys->p_theme;
371             p_intf->p_sys->p_theme = oldTheme;
372
373             // Show the theme
374             p_intf->p_sys->p_theme->ShowTheme();
375         }
376         delete Loader;
377
378         // Uninitialize new theme
379         delete (char *)p_intf->p_sys->p_new_theme_file;
380         p_intf->p_sys->p_new_theme_file = NULL;
381     }
382 }
383 //---------------------------------------------------------------------------
384 void VlcProc::OpenFile( bool play )
385 {
386     if( p_intf->p_sys->OpenDlg->ShowModal() != wxID_OK )
387     {
388         return;
389     }
390
391     // Check if playlist is available
392     playlist_t *p_playlist = p_intf->p_sys->p_playlist;
393     if( p_playlist == NULL )
394     {
395         return;
396     }
397
398     if( play )
399     {
400         // Append and play
401         playlist_Add( p_playlist,
402                       (char *)p_intf->p_sys->OpenDlg->mrl.c_str(),
403                       PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
404
405         p_intf->p_sys->p_theme->EvtBank->Get( "play" )->SendEvent();
406     }
407     else
408     {
409         // Append only
410         playlist_Add( p_playlist,
411                         (char *)p_intf->p_sys->OpenDlg->mrl.c_str(),
412                         PLAYLIST_APPEND, PLAYLIST_END );
413     }
414
415     // Refresh interface !
416     p_intf->p_sys->p_theme->EvtBank->Get( "playlist_refresh" )
417         ->PostSynchroMessage();
418     InterfaceRefresh();
419 }
420 //---------------------------------------------------------------------------
421 void VlcProc::DropFile( unsigned int param )
422 {
423     // Get pointer to file
424     char *FileName = (char *)param;
425
426     // Add the new file to the playlist
427     if( p_intf->p_sys->p_playlist != NULL )
428     {
429         if( config_GetInt( p_intf, "enqueue" ) )
430         {
431             playlist_Add( p_intf->p_sys->p_playlist, FileName,
432                           PLAYLIST_APPEND, PLAYLIST_END );
433         }
434         else
435         {
436             playlist_Add( p_intf->p_sys->p_playlist, FileName,
437                           PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
438         }
439     }
440
441     // VLC_DROP must be called with a pointer to a char else it will
442     // ******** SEGFAULT ********
443     // The delete is here because the processus in asynchronous
444     delete[] FileName;
445
446     // Refresh interface
447     InterfaceRefresh();
448
449 }
450 //---------------------------------------------------------------------------
451
452
453
454
455 //---------------------------------------------------------------------------
456 // Stream Control
457 //---------------------------------------------------------------------------
458 void VlcProc::PauseStream()
459 {
460     if( p_intf->p_sys->p_input == NULL )
461         return;
462     input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PAUSE );
463
464     // Refresh interface
465     InterfaceRefresh();
466 }
467 //---------------------------------------------------------------------------
468 void VlcProc::PlayStream()
469 {
470     if( p_intf->p_sys->p_playlist == NULL )
471         return;
472     if( !p_intf->p_sys->p_playlist->i_size )
473         return;
474
475     playlist_Play( p_intf->p_sys->p_playlist );
476
477     // Refresh interface
478     InterfaceRefresh();
479 }
480 //---------------------------------------------------------------------------
481 void VlcProc::StopStream()
482 {
483     if( p_intf->p_sys->p_playlist == NULL )
484         return;
485     playlist_Stop( p_intf->p_sys->p_playlist );
486
487     // Refresh interface
488     InterfaceRefresh();
489 }
490 //---------------------------------------------------------------------------
491 void VlcProc::NextStream()
492 {
493     if( p_intf->p_sys->p_playlist == NULL )
494         return;
495
496     playlist_Next( p_intf->p_sys->p_playlist );
497
498     // Refresh interface
499     InterfaceRefresh();
500 }
501 //---------------------------------------------------------------------------
502 void VlcProc::PrevStream()
503 {
504     if( p_intf->p_sys->p_playlist == NULL )
505         return;
506
507     playlist_Prev( p_intf->p_sys->p_playlist );
508
509     // Refresh interface
510     InterfaceRefresh();
511 }
512 //---------------------------------------------------------------------------
513 void VlcProc::MoveStream( long Pos )
514 {
515     if( p_intf->p_sys->p_input == NULL )
516         return;
517
518     off_t i_seek = (off_t)(Pos *
519         p_intf->p_sys->p_input->stream.p_selected_area->i_size
520         / SLIDER_RANGE);
521
522     input_Seek( p_intf->p_sys->p_input, i_seek, INPUT_SEEK_SET );
523
524     // Refresh interface
525     InterfaceRefresh();
526 }
527 //---------------------------------------------------------------------------
528
529
530
531 //---------------------------------------------------------------------------
532 // Fullscreen
533 //---------------------------------------------------------------------------
534 void VlcProc::FullScreen()
535 {
536     vout_thread_t *p_vout;
537
538     if( p_intf->p_sys->p_input == NULL )
539         return;
540
541     p_vout = (vout_thread_t *)vlc_object_find( p_intf->p_sys->p_input,
542                                                VLC_OBJECT_VOUT, FIND_CHILD );
543     if( p_vout == NULL )
544         return;
545
546     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
547     vlc_object_release( p_vout );
548 }
549 //---------------------------------------------------------------------------
550
551
552
553 //---------------------------------------------------------------------------
554 // Volume Control
555 //---------------------------------------------------------------------------
556 void VlcProc::ChangeVolume( unsigned int msg, long param )
557 {
558     audio_volume_t volume;
559     switch( msg )
560     {
561         case VLC_VOLUME_MUTE:
562             aout_VolumeMute( p_intf, NULL );
563             break;
564         case VLC_VOLUME_UP:
565             aout_VolumeUp( p_intf, 1, NULL );
566             break;
567         case VLC_VOLUME_DOWN:
568             aout_VolumeDown( p_intf, 1, NULL );
569             break;
570         case VLC_VOLUME_SET:
571             aout_VolumeSet( p_intf, param * AOUT_VOLUME_MAX / SLIDER_RANGE );
572             break;
573     }
574     aout_VolumeGet( p_intf, &volume );
575
576 }
577 //---------------------------------------------------------------------------
578
579
580 //---------------------------------------------------------------------------
581 // Network
582 //---------------------------------------------------------------------------
583 void VlcProc::AddNetworkUDP( int port )
584 {
585     config_PutInt( p_intf, "network-channel", VLC_FALSE );
586
587     // Build source name
588     char *s_port = new char[5];
589     sprintf( s_port, "%i", port );
590     string source = "udp:@:" + (string)s_port;
591     delete[] s_port;
592
593     playlist_Add( p_intf->p_sys->p_playlist, (char *)source.c_str(),
594         PLAYLIST_APPEND, PLAYLIST_END );
595
596     // Refresh interface !
597     p_intf->p_sys->p_theme->EvtBank->Get( "playlist_refresh" )
598         ->PostSynchroMessage();
599     InterfaceRefresh();
600 }
601 //---------------------------------------------------------------------------
602 void VlcProc::AddNetworkChannelServer( char *server )
603 {
604     char *name = new char[MAX_PARAM_SIZE];
605     int  port = 0;
606
607     // Scan the server address
608     int scan = sscanf( server, "%[^:]:%i", name, &port );
609
610     if( scan != 2)
611     {
612         msg_Err( p_intf, "Invalid channel server: %s", server );
613         delete[] name;
614         return;
615     }
616
617     config_PutInt( p_intf, "network-channel", VLC_TRUE );
618     config_PutPsz( p_intf, "channel-server", name );
619     config_PutInt( p_intf, "channel-port", port );
620
621     if( p_intf->p_vlc->p_channel == NULL )
622     {
623         network_ChannelCreate( p_intf );
624     }
625
626     delete[] name;
627
628 }
629 //---------------------------------------------------------------------------
630