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