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