* modules/gui/skins/*: fix for msvc .dsp generation, replaced the BASIC_SKINS macro...
[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.35 2003/06/11 10:42:34 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 //--- 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 //---------------------------------------------------------------------------
52 VlcProc::~VlcProc()
53 {
54 }
55 //---------------------------------------------------------------------------
56 bool VlcProc::EventProc( Event *evt )
57 {
58     switch( evt->GetMessage() )
59     {
60         case VLC_STREAMPOS:
61             MoveStream( evt->GetParam2() );
62             return true;
63
64         case VLC_VOLUME_CHANGE:
65             ChangeVolume( evt->GetParam1(), evt->GetParam2() );
66             return true;
67
68         case VLC_FULLSCREEN:
69             FullScreen();
70             return true;
71
72         case VLC_HIDE:
73             for( list<SkinWindow *>::const_iterator win =
74                     p_intf->p_sys->p_theme->WindowList.begin();
75                  win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
76             {
77                 (*win)->OnStartThemeVisible = !(*win)->IsHidden();
78             }
79             p_intf->p_sys->i_close_status = (int)evt->GetParam1();
80             OSAPI_PostMessage( NULL, WINDOW_CLOSE, 1, 0 );
81             return true;
82
83         case VLC_SHOW:
84             for( list<SkinWindow *>::const_iterator win =
85                     p_intf->p_sys->p_theme->WindowList.begin();
86                  win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
87             {
88                 if( (*win)->OnStartThemeVisible )
89                     OSAPI_PostMessage( (*win), WINDOW_OPEN, 1, 0 );
90             }
91             p_intf->p_sys->b_all_win_closed = false;
92             return true;
93
94         case VLC_OPEN:
95             p_intf->p_sys->p_dialogs->ShowOpen( true );
96             InterfaceRefresh();
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             p_intf->p_sys->p_dialogs->ShowOpen( false );
129             InterfaceRefresh();
130             return true;
131
132         case VLC_LOG_SHOW:
133             p_intf->p_sys->p_dialogs->ShowMessages();
134             return true;
135
136         case VLC_LOG_CLEAR:
137             return true;
138
139         case VLC_PREFS_SHOW:
140             p_intf->p_sys->p_dialogs->ShowPrefs();
141             return true;
142
143         case VLC_INFO_SHOW:
144             p_intf->p_sys->p_dialogs->ShowFileInfo();
145             return true;
146
147         case VLC_INTF_REFRESH:
148             InterfaceRefresh( (bool)evt->GetParam2() );
149             return true;
150
151         case VLC_TEST_ALL_CLOSED:
152             return EventProcEnd();
153
154         case VLC_QUIT:
155             return false;
156
157         case VLC_CHANGE_TRAY:
158             p_intf->p_sys->p_theme->ChangeTray();
159             return true;
160
161         case VLC_CHANGE_TASKBAR:
162             p_intf->p_sys->p_theme->ChangeTaskbar();
163             return true;
164
165         case VLC_NET_ADDUDP:
166             AddNetworkUDP( (int)evt->GetParam2() );
167             return true;
168
169         default:
170             return true;
171     }
172 }
173 //---------------------------------------------------------------------------
174 bool VlcProc::EventProcEnd()
175 {
176     if( p_intf->p_sys->b_all_win_closed )
177         return true;
178
179     list<SkinWindow *>::const_iterator win;
180
181     // If a window has been closed, test if all are closed !
182     for( win = p_intf->p_sys->p_theme->WindowList.begin();
183          win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
184     {
185         if( !(*win)->IsHidden() )   // Not all windows closed
186         {
187             return true;
188         }
189     }
190
191     // All window are closed
192     switch( p_intf->p_sys->i_close_status )
193     {
194         case VLC_QUIT:
195             // Save config before exiting
196             p_intf->p_sys->p_theme->SaveConfig();
197             break;
198     }
199
200     // Send specified event
201     OSAPI_PostMessage( NULL, p_intf->p_sys->i_close_status, 0, 0 );
202
203     // Reset values
204     p_intf->p_sys->i_close_status = VLC_NOTHING;
205     p_intf->p_sys->b_all_win_closed = true;
206
207     // Return true
208     return true;
209 }
210 //---------------------------------------------------------------------------
211 bool VlcProc::IsClosing()
212 {
213     if( p_intf->b_die && p_intf->p_sys->i_close_status != VLC_QUIT )
214     {
215         p_intf->p_sys->i_close_status = VLC_QUIT;
216         OSAPI_PostMessage( NULL, VLC_HIDE, VLC_QUIT, 0 );
217     }
218     return true;
219 }
220 //---------------------------------------------------------------------------
221
222
223
224
225 //---------------------------------------------------------------------------
226 // Private methods
227 //---------------------------------------------------------------------------
228 void VlcProc::InterfaceRefresh( bool All )
229 {
230     // Shortcut pointers
231     intf_sys_t  *Sys      = p_intf->p_sys;
232     Theme       *Thema    = Sys->p_theme;
233     playlist_t  *PlayList = Sys->p_playlist;
234
235     // Refresh
236     if( PlayList != NULL )
237     {
238         // Refresh stream control controls ! :)
239         switch( PlayList->i_status )
240         {
241             case PLAYLIST_STOPPED:
242                 EnabledEvent( "time", false );
243                 EnabledEvent( "stop", false );
244                 EnabledEvent( "play", true );
245                 EnabledEvent( "pause", false );
246                 break;
247             case PLAYLIST_RUNNING:
248                 EnabledEvent( "time", true );
249                 EnabledEvent( "stop", true );
250                 EnabledEvent( "play", false );
251                 EnabledEvent( "pause", true );
252                 break;
253             case PLAYLIST_PAUSED:
254                 EnabledEvent( "time", true );
255                 EnabledEvent( "stop", true );
256                 EnabledEvent( "play", true );
257                 EnabledEvent( "pause", false );
258                 break;
259         }
260
261         // Refresh next and prev buttons
262         if( PlayList->i_index == 0 || PlayList->i_size == 1 )
263             EnabledEvent( "prev", false );
264         else
265             EnabledEvent( "prev", true );
266
267         if( PlayList->i_index == PlayList->i_size - 1 || PlayList->i_size == 1 )
268             EnabledEvent( "next", false );
269         else
270             EnabledEvent( "next", true );
271
272
273         // Update file name text
274         if( PlayList->i_index != Sys->i_index )
275         {
276             Thema->EvtBank->Get( "file_name" )->PostTextMessage(
277                 PlayList->pp_items[PlayList->i_index]->psz_name );
278         }
279
280         // Update playlists
281         if( PlayList->i_index != Sys->i_index ||
282             PlayList->i_size != Sys->i_size )
283         {
284             Thema->EvtBank->Get( "playlist_refresh" )->PostSynchroMessage();
285             Sys->i_size  = PlayList->i_size;
286             Sys->i_index = PlayList->i_index;
287         }
288     }
289     else
290     {
291         EnabledEvent( "time", false );
292         EnabledEvent( "stop",  false );
293         EnabledEvent( "play",  false );
294         EnabledEvent( "pause", false );
295         EnabledEvent( "prev",  false );
296         EnabledEvent( "next",  false );
297
298         // Update playlists
299         if( Sys->i_size > 0 )
300         {
301             Thema->EvtBank->Get( "playlist_refresh" )->PostSynchroMessage();
302             Sys->i_size  = 0;
303         }
304     }
305
306 }
307 //---------------------------------------------------------------------------
308 void VlcProc::EnabledEvent( string type, bool state )
309 {
310     OSAPI_PostMessage( NULL, CTRL_ENABLED, (unsigned int)
311         p_intf->p_sys->p_theme->EvtBank->Get( type ), (int)state );
312 }
313 //---------------------------------------------------------------------------
314
315
316 //---------------------------------------------------------------------------
317 // Common VLC procedures
318 //---------------------------------------------------------------------------
319 void VlcProc::LoadSkin()
320 {
321     if( p_intf->p_sys->p_new_theme_file == NULL )
322     {
323         p_intf->p_sys->p_dialogs->ShowOpenSkin();
324     }
325     else
326     {
327         // Place a new theme in the global structure, because it will
328         // be filled by the parser
329         // We save the old one to restore it in case of problem
330         Theme *oldTheme = p_intf->p_sys->p_theme;
331         p_intf->p_sys->p_theme = (Theme *)new OSTheme( p_intf );
332
333         // Run the XML parser
334         ThemeLoader *Loader = new ThemeLoader( p_intf );
335         if( Loader->Load( p_intf->p_sys->p_new_theme_file ) )
336         {
337             // Everything went well
338             msg_Dbg( p_intf, "New theme successfully loaded" );
339             delete (OSTheme *)oldTheme;
340
341             // Show the theme
342             p_intf->p_sys->p_theme->InitTheme();
343             p_intf->p_sys->p_theme->ShowTheme();
344         }
345         else
346         {
347             msg_Warn( p_intf, "A problem occurred when loading the new theme,"
348                       " restoring the previous one" );
349             delete (OSTheme *)p_intf->p_sys->p_theme;
350             p_intf->p_sys->p_theme = oldTheme;
351
352             // Show the theme
353             p_intf->p_sys->p_theme->ShowTheme();
354         }
355         delete Loader;
356
357         // Uninitialize new theme
358         delete (char *)p_intf->p_sys->p_new_theme_file;
359         p_intf->p_sys->p_new_theme_file = NULL;
360     }
361 }
362 //---------------------------------------------------------------------------
363
364 void VlcProc::DropFile( unsigned int param )
365 {
366     // Get pointer to file
367     char *FileName = (char *)param;
368
369     // Add the new file to the playlist
370     if( p_intf->p_sys->p_playlist != NULL )
371     {
372         if( config_GetInt( p_intf, "enqueue" ) )
373         {
374             playlist_Add( p_intf->p_sys->p_playlist, FileName,
375                           PLAYLIST_APPEND, PLAYLIST_END );
376         }
377         else
378         {
379             playlist_Add( p_intf->p_sys->p_playlist, FileName,
380                           PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
381         }
382     }
383
384     // VLC_DROP must be called with a pointer to a char else it will
385     // ******** SEGFAULT ********
386     // The delete is here because the processus in asynchronous
387     delete[] FileName;
388
389     // Refresh interface
390     InterfaceRefresh();
391
392 }
393 //---------------------------------------------------------------------------
394
395
396
397
398 //---------------------------------------------------------------------------
399 // Stream Control
400 //---------------------------------------------------------------------------
401 void VlcProc::PauseStream()
402 {
403     if( p_intf->p_sys->p_input == NULL )
404         return;
405     input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PAUSE );
406
407     // Refresh interface
408     InterfaceRefresh();
409 }
410 //---------------------------------------------------------------------------
411 void VlcProc::PlayStream()
412 {
413     if( p_intf->p_sys->p_playlist == NULL )
414         return;
415
416     if( !p_intf->p_sys->p_playlist->i_size )
417     {
418         p_intf->p_sys->p_dialogs->ShowOpen( true );
419         InterfaceRefresh();
420         return;
421     }
422
423     playlist_Play( p_intf->p_sys->p_playlist );
424
425     // Refresh interface
426     InterfaceRefresh();
427 }
428 //---------------------------------------------------------------------------
429 void VlcProc::StopStream()
430 {
431     if( p_intf->p_sys->p_playlist == NULL )
432         return;
433     playlist_Stop( p_intf->p_sys->p_playlist );
434
435     // Refresh interface
436     InterfaceRefresh();
437 }
438 //---------------------------------------------------------------------------
439 void VlcProc::NextStream()
440 {
441     if( p_intf->p_sys->p_playlist == NULL )
442         return;
443
444     playlist_Next( p_intf->p_sys->p_playlist );
445
446     // Refresh interface
447     InterfaceRefresh();
448 }
449 //---------------------------------------------------------------------------
450 void VlcProc::PrevStream()
451 {
452     if( p_intf->p_sys->p_playlist == NULL )
453         return;
454
455     playlist_Prev( p_intf->p_sys->p_playlist );
456
457     // Refresh interface
458     InterfaceRefresh();
459 }
460 //---------------------------------------------------------------------------
461 void VlcProc::MoveStream( long Pos )
462 {
463     if( p_intf->p_sys->p_input == NULL )
464         return;
465
466     off_t i_seek = (off_t)(Pos *
467         p_intf->p_sys->p_input->stream.p_selected_area->i_size
468         / SLIDER_RANGE);
469
470     input_Seek( p_intf->p_sys->p_input, i_seek, INPUT_SEEK_SET );
471
472     // Refresh interface
473     InterfaceRefresh();
474 }
475 //---------------------------------------------------------------------------
476
477
478
479 //---------------------------------------------------------------------------
480 // Fullscreen
481 //---------------------------------------------------------------------------
482 void VlcProc::FullScreen()
483 {
484     vout_thread_t *p_vout;
485
486     if( p_intf->p_sys->p_input == NULL )
487         return;
488
489     p_vout = (vout_thread_t *)vlc_object_find( p_intf->p_sys->p_input,
490                                                VLC_OBJECT_VOUT, FIND_CHILD );
491     if( p_vout == NULL )
492         return;
493
494     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
495     vlc_object_release( p_vout );
496 }
497 //---------------------------------------------------------------------------
498
499
500
501 //---------------------------------------------------------------------------
502 // Volume Control
503 //---------------------------------------------------------------------------
504 void VlcProc::ChangeVolume( unsigned int msg, long param )
505 {
506     audio_volume_t volume;
507     switch( msg )
508     {
509         case VLC_VOLUME_MUTE:
510             aout_VolumeMute( p_intf, NULL );
511             break;
512         case VLC_VOLUME_UP:
513             aout_VolumeUp( p_intf, 1, NULL );
514             break;
515         case VLC_VOLUME_DOWN:
516             aout_VolumeDown( p_intf, 1, NULL );
517             break;
518         case VLC_VOLUME_SET:
519             aout_VolumeSet( p_intf, param * AOUT_VOLUME_MAX / SLIDER_RANGE );
520             break;
521     }
522     aout_VolumeGet( p_intf, &volume );
523
524 }
525 //---------------------------------------------------------------------------
526
527
528 //---------------------------------------------------------------------------
529 // Network
530 //---------------------------------------------------------------------------
531 void VlcProc::AddNetworkUDP( int port )
532 {
533     // Build source name
534     char *s_port = new char[5];
535     sprintf( s_port, "%i", port );
536     string source = "udp:@:" + (string)s_port;
537     delete[] s_port;
538
539     playlist_Add( p_intf->p_sys->p_playlist, (char *)source.c_str(),
540         PLAYLIST_APPEND, PLAYLIST_END );
541
542     // Refresh interface !
543     p_intf->p_sys->p_theme->EvtBank->Get( "playlist_refresh" )
544         ->PostSynchroMessage();
545     InterfaceRefresh();
546 }
547 //---------------------------------------------------------------------------