* Added use of channel server for skins: still experimental
[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.6 2003/04/14 10:00:38 karibu 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
27 //--- VLC -------------------------------------------------------------------
28 #include <vlc/vlc.h>
29 #include <vlc/intf.h>
30 #include <vlc/aout.h>
31 #include <vlc/vout.h>
32 #include <netutils.h>
33
34 //--- SKIN ------------------------------------------------------------------
35 #include "os_api.h"
36 #include "dialog.h"
37 #include "os_dialog.h"
38 #include "event.h"
39 #include "banks.h"
40 #include "theme.h"
41 #include "os_theme.h"
42 #include "themeloader.h"
43 #include "window.h"
44 #include "vlcproc.h"
45 #include "skin_common.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<Window *>::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<Window *>::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->p_theme->ShowLog( evt->GetParam2() );
133             return true;
134
135         case VLC_LOG_CLEAR:
136             p_intf->p_sys->p_theme->ClearLog();
137             return true;
138
139         case VLC_INTF_REFRESH:
140             InterfaceRefresh( (bool)evt->GetParam2() );
141             return true;
142
143         case VLC_TEST_ALL_CLOSED:
144             return EventProcEnd();
145
146         case VLC_QUIT:
147             return false;
148
149         case VLC_CHANGE_TRAY:
150             p_intf->p_sys->p_theme->ChangeTray();
151             return true;
152
153         case VLC_CHANGE_TASKBAR:
154             p_intf->p_sys->p_theme->ChangeTaskbar();
155             return true;
156
157         case VLC_NET_ADDUDP:
158             AddNetworkUDP( (int)evt->GetParam2() );
159             return true;
160
161         case VLC_NET_ADDCS:
162             AddNetworkChannelServer( (char *)evt->GetParam1() );
163             return true;
164
165         default:
166             return true;
167     }
168 }
169 //---------------------------------------------------------------------------
170 bool VlcProc::EventProcEnd()
171 {
172     if( p_intf->p_sys->b_all_win_closed )
173         return true;
174
175     list<Window *>::const_iterator win;
176
177     // If a window has been closed, test if all are closed !
178     for( win = p_intf->p_sys->p_theme->WindowList.begin();
179          win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
180     {
181         if( !(*win)->IsHidden() )   // Not all windows closed
182         {
183             return true;
184         }
185     }
186
187     // All window are closed
188     switch( p_intf->p_sys->i_close_status )
189     {
190         case VLC_QUIT:
191             // Save config before exiting
192             p_intf->p_sys->p_theme->SaveConfig();
193             break;
194     }
195
196     // Send specified event
197     OSAPI_PostMessage( NULL, p_intf->p_sys->i_close_status, 0, 0 );
198
199     // Reset values
200     p_intf->p_sys->i_close_status = VLC_NOTHING;
201     p_intf->p_sys->b_all_win_closed = true;
202
203     // Return true
204     return true;
205 }
206 //---------------------------------------------------------------------------
207 bool VlcProc::IsClosing()
208 {
209     if( p_intf->b_die && p_intf->p_sys->i_close_status != VLC_QUIT )
210     {
211         p_intf->p_sys->i_close_status = VLC_QUIT;
212         OSAPI_PostMessage( NULL, VLC_HIDE, VLC_QUIT, 0 );
213     }
214     return true;
215 }
216 //---------------------------------------------------------------------------
217
218
219
220
221 //---------------------------------------------------------------------------
222 // Private methods
223 //---------------------------------------------------------------------------
224 void VlcProc::InterfaceRefresh( bool All )
225 {
226     // Shortcut pointers
227     intf_sys_t  *Sys      = p_intf->p_sys;
228     Theme       *Thema    = Sys->p_theme;
229     playlist_t  *PlayList = Sys->p_playlist;
230
231
232     // Refresh
233     if( PlayList != NULL && Sys->p_input != NULL &&
234         Sys->p_input->stream.b_seekable )
235     {
236         // Refresh stream control controls ! :)
237         switch( PlayList->i_status )
238         {
239             case PLAYLIST_STOPPED:
240                 EnabledEvent( "time", false );
241                 EnabledEvent( "stop", false );
242                 EnabledEvent( "play", true );
243                 EnabledEvent( "pause", false );
244                 break;
245             case PLAYLIST_RUNNING:
246                 EnabledEvent( "time", true );
247                 EnabledEvent( "stop", true );
248                 EnabledEvent( "play", false );
249                 EnabledEvent( "pause", true );
250                 break;
251             case PLAYLIST_PAUSED:
252                 EnabledEvent( "time", true );
253                 EnabledEvent( "stop", true );
254                 EnabledEvent( "play", true );
255                 EnabledEvent( "pause", false );
256                 break;
257         }
258
259         // Refresh next and prev buttons
260         if( PlayList->i_index == 0 || PlayList->i_size == 1 )
261             EnabledEvent( "prev", false );
262         else
263             EnabledEvent( "prev", true );
264
265         if( PlayList->i_index == PlayList->i_size - 1 || PlayList->i_size == 1 )
266             EnabledEvent( "next", false );
267         else
268             EnabledEvent( "next", true );
269
270
271         // Update file name text
272         if( PlayList->i_index != Sys->i_index )
273         {
274             Thema->EvtBank->Get( "file_name" )->PostTextMessage(
275                 PlayList->pp_items[PlayList->i_index]->psz_name );
276         }
277
278         // Update playlists
279         if( PlayList->i_index != Sys->i_index ||
280             PlayList->i_size != Sys->i_size )
281         {
282             Thema->EvtBank->Get( "playlist_refresh" )->PostSynchroMessage();
283             Sys->i_size  = PlayList->i_size;
284             Sys->i_index = PlayList->i_index;
285         }
286     }
287     else
288     {
289         EnabledEvent( "time", false );
290         EnabledEvent( "stop",  false );
291         EnabledEvent( "play",  false );
292         EnabledEvent( "pause", false );
293         EnabledEvent( "prev",  false );
294         EnabledEvent( "next",  false );
295
296         // Update playlists
297         if( Sys->i_size > 0 )
298         {
299             Thema->EvtBank->Get( "playlist_refresh" )->PostSynchroMessage();
300             Sys->i_size  = 0;
301         }
302     }
303
304 }
305 //---------------------------------------------------------------------------
306 void VlcProc::EnabledEvent( string type, bool state )
307 {
308     OSAPI_PostMessage( NULL, CTRL_ENABLED, (unsigned int)
309         p_intf->p_sys->p_theme->EvtBank->Get( type ), (int)state );
310 }
311 //---------------------------------------------------------------------------
312
313
314
315 //---------------------------------------------------------------------------
316 // Common VLC procedures
317 //---------------------------------------------------------------------------
318 void VlcProc::LoadSkin()
319 {
320     if( p_intf->p_sys->p_new_theme_file == NULL )
321     {
322         // Initialize file structure
323         OpenFileDialog *OpenFile;
324         OpenFile = (OpenFileDialog *)new OSOpenFileDialog( p_intf,
325             _("Change skin - Open new file"), false );
326         OpenFile->AddFilter( _("Skin files"), "*.vlt" );
327         OpenFile->AddFilter( _("Skin files"), "*.xml" );
328         OpenFile->AddFilter( _("All files"), "*.*" );
329
330         // Open dialog box
331         if( OpenFile->Open() )
332         {
333             p_intf->p_sys->p_new_theme_file =
334                 new char[OpenFile->FileList.front().length()];
335
336             strcpy( p_intf->p_sys->p_new_theme_file,
337                     OpenFile->FileList.front().c_str() );
338
339             // Tell vlc to change skin after hiding interface
340             OSAPI_PostMessage( NULL, VLC_HIDE, VLC_LOAD_SKIN, 0 );
341         }
342
343         // Free memory
344         delete OpenFile;
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     // Initialize file structure
387     OpenFileDialog *OpenFile;
388     if( play )
389     {
390         OpenFile = (OpenFileDialog *)new OSOpenFileDialog( p_intf,
391             _("Open file"), false );
392     }
393     else
394     {
395         OpenFile = (OpenFileDialog *)new OSOpenFileDialog( p_intf,
396             _("Add file"), true );
397     }
398     OpenFile->AddFilter( _("All files"), "*.*" );
399
400     // Check if palylist is available
401     playlist_t *p_playlist = p_intf->p_sys->p_playlist;
402     if( p_playlist == NULL )
403         return;
404
405     // Open dialog box and launch file
406     if( OpenFile->Open() )
407     {
408         list<string>::const_iterator file;
409         for( file = OpenFile->FileList.begin();
410              file != OpenFile->FileList.end(); file++ )
411         {
412             playlist_Add( p_playlist, (*file).c_str(), PLAYLIST_APPEND,
413                           PLAYLIST_END );
414         }
415
416         // Refreshing
417         if( play )
418         {
419             // Play
420             p_intf->p_sys->p_theme->EvtBank->Get( "play" )->SendEvent();
421         }
422
423         // Refresh interface !
424         p_intf->p_sys->p_theme->EvtBank->Get( "playlist_refresh" )
425             ->PostSynchroMessage();
426         InterfaceRefresh();
427     }
428
429     // Free memory
430     delete OpenFile;
431 }
432 //---------------------------------------------------------------------------
433 void VlcProc::DropFile( unsigned int param )
434 {
435     // Get pointer to file
436     char *FileName = (char *)param;
437
438     // Add the new file to the playlist
439     if( p_intf->p_sys->p_playlist != NULL )
440     {
441         if( config_GetInt( p_intf, "enqueue" ) )
442         {
443             playlist_Add( p_intf->p_sys->p_playlist, FileName,
444                           PLAYLIST_APPEND, PLAYLIST_END );
445         }
446         else
447         {
448             playlist_Add( p_intf->p_sys->p_playlist, FileName,
449                           PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
450         }
451     }
452
453     // VLC_DROP must be called with a pointer to a char else it will
454     // ******** SEGFAULT ********
455     // The delete is here because the processus in asynchronous
456     delete[] FileName;
457
458     // Refresh interface
459     InterfaceRefresh();
460
461 }
462 //---------------------------------------------------------------------------
463
464
465
466
467 //---------------------------------------------------------------------------
468 // Stream Control
469 //---------------------------------------------------------------------------
470 void VlcProc::PauseStream()
471 {
472     if( p_intf->p_sys->p_input == NULL )
473         return;
474     input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PAUSE );
475
476     // Refresh interface
477     InterfaceRefresh();
478 }
479 //---------------------------------------------------------------------------
480 void VlcProc::PlayStream()
481 {
482     if( p_intf->p_sys->p_playlist == NULL )
483         return;
484     if( !p_intf->p_sys->p_playlist->i_size )
485         return;
486
487     playlist_Play( p_intf->p_sys->p_playlist );
488
489     // Refresh interface
490     InterfaceRefresh();
491 }
492 //---------------------------------------------------------------------------
493 void VlcProc::StopStream()
494 {
495     if( p_intf->p_sys->p_playlist == NULL )
496         return;
497     playlist_Stop( p_intf->p_sys->p_playlist );
498
499     // Refresh interface
500     InterfaceRefresh();
501 }
502 //---------------------------------------------------------------------------
503 void VlcProc::NextStream()
504 {
505     if( p_intf->p_sys->p_playlist == NULL )
506         return;
507
508     playlist_Next( p_intf->p_sys->p_playlist );
509
510     // Refresh interface
511     InterfaceRefresh();
512 }
513 //---------------------------------------------------------------------------
514 void VlcProc::PrevStream()
515 {
516     if( p_intf->p_sys->p_playlist == NULL )
517         return;
518
519     playlist_Prev( p_intf->p_sys->p_playlist );
520
521     // Refresh interface
522     InterfaceRefresh();
523 }
524 //---------------------------------------------------------------------------
525 void VlcProc::MoveStream( long Pos )
526 {
527     if( p_intf->p_sys->p_input == NULL )
528         return;
529
530     off_t i_seek = (off_t)(Pos *
531         p_intf->p_sys->p_input->stream.p_selected_area->i_size
532         / SLIDER_RANGE);
533
534     input_Seek( p_intf->p_sys->p_input, i_seek, INPUT_SEEK_SET );
535
536     // Refresh interface
537     InterfaceRefresh();
538 }
539 //---------------------------------------------------------------------------
540
541
542
543 //---------------------------------------------------------------------------
544 // Fullscreen
545 //---------------------------------------------------------------------------
546 void VlcProc::FullScreen()
547 {
548     vout_thread_t *p_vout;
549
550     if( p_intf->p_sys->p_input == NULL )
551         return;
552
553     p_vout = (vout_thread_t *)vlc_object_find( p_intf->p_sys->p_input,
554                                                VLC_OBJECT_VOUT, FIND_CHILD );
555     if( p_vout == NULL )
556         return;
557
558     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
559     vlc_object_release( p_vout );
560 }
561 //---------------------------------------------------------------------------
562
563
564
565 //---------------------------------------------------------------------------
566 // Volume Control
567 //---------------------------------------------------------------------------
568 void VlcProc::ChangeVolume( unsigned int msg, long param )
569 {
570     audio_volume_t volume;
571     switch( msg )
572     {
573         case VLC_VOLUME_MUTE:
574             aout_VolumeMute( p_intf, NULL );
575             break;
576         case VLC_VOLUME_UP:
577             aout_VolumeUp( p_intf, 1, NULL );
578             break;
579         case VLC_VOLUME_DOWN:
580             aout_VolumeDown( p_intf, 1, NULL );
581             break;
582         case VLC_VOLUME_SET:
583             aout_VolumeSet( p_intf, param * AOUT_VOLUME_MAX / SLIDER_RANGE );
584             break;
585     }
586     aout_VolumeGet( p_intf, &volume );
587
588 /* FIXME: kludge */
589 #ifdef WIN32
590     PostMessage( NULL, CTRL_SET_SLIDER,
591         (unsigned int)
592             p_intf->p_sys->p_theme->EvtBank->Get( "volume_refresh" ),
593         (int)( volume * SLIDER_RANGE / AOUT_VOLUME_MAX ) );
594 #else
595   fprintf(stderr, "WARNING: FIXME in vlcproc.cpp !!!");
596 #endif
597 }
598 //---------------------------------------------------------------------------
599
600
601 //---------------------------------------------------------------------------
602 // Network
603 //---------------------------------------------------------------------------
604 void VlcProc::AddNetworkUDP( int port )
605 {
606     config_PutInt( p_intf, "network-channel", VLC_FALSE );
607
608     // Build source name
609     char *s_port = new char[5];
610     sprintf( s_port, "%i", port );
611     string source = "udp:@:" + (string)s_port;
612     delete[] s_port;
613
614     playlist_Add( p_intf->p_sys->p_playlist, (char *)source.c_str(),
615         PLAYLIST_APPEND, PLAYLIST_END );
616
617     // Refresh interface !
618     p_intf->p_sys->p_theme->EvtBank->Get( "playlist_refresh" )
619         ->PostSynchroMessage();
620     InterfaceRefresh();
621 }
622 //---------------------------------------------------------------------------
623 void VlcProc::AddNetworkChannelServer( char *server )
624 {
625     char *name = new char[MAX_PARAM_SIZE];
626     int   port = 0;
627
628     // Scan the server address
629     int scan = sscanf( server, "%[^:]:%i", name, port );
630
631     if( scan != 2)
632     {
633         msg_Err( p_intf, "Invalid channel server: %s", server );
634         delete[] name;
635         return;
636     }
637
638     config_PutInt( p_intf, "network-channel", VLC_TRUE );
639     config_PutPsz( p_intf, "channel-server", name );
640     config_PutInt( p_intf, "channel-port", port );
641
642     if( p_intf->p_vlc->p_channel == NULL )
643     {
644         network_ChannelCreate( p_intf );
645     }
646
647     delete[] name;
648
649 }
650 //---------------------------------------------------------------------------
651