]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/wxwidgets.cpp
9344cbd0bc4e41fe3d40b61c42788f6acfb63596
[vlc] / modules / gui / wxwidgets / wxwidgets.cpp
1 /*****************************************************************************
2  * wxwidgets.cpp : wxWidgets plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>                                                 /* ENOMEM */
28
29 #include <vlc/vlc.h>
30 #include <vlc_interface.h>
31
32 #ifdef HAVE_LOCALE_H
33 #   include <locale.h>
34 #endif
35
36 #include "interface.hpp"
37
38 /* Temporary hack */
39 #if defined(WIN32) && defined(_WX_INIT_H_)
40 #if (wxMAJOR_VERSION <= 2) && (wxMINOR_VERSION <= 5) && (wxRELEASE_NUMBER < 3)
41 /* Hack to detect wxWidgets 2.5 which has a different wxEntry() prototype */
42 extern int wxEntry( HINSTANCE hInstance, HINSTANCE hPrevInstance = NULL,
43                     char *pCmdLine = NULL, int nCmdShow = SW_NORMAL );
44 #endif
45 #endif
46
47 /*****************************************************************************
48  * Local prototypes.
49  *****************************************************************************/
50 static int  Open         ( vlc_object_t * );
51 static void Close        ( vlc_object_t * );
52 static int  OpenDialogs  ( vlc_object_t * );
53
54 static void Run          ( intf_thread_t * );
55 static void Init         ( intf_thread_t * );
56
57 static void ShowDialog   ( intf_thread_t *, int, int, intf_dialog_args_t * );
58
59 #if (wxCHECK_VERSION(2,5,0))
60 void *wxClassInfo_sm_classTable_BUGGY = 0;
61 #endif
62
63 /*****************************************************************************
64  * Local classes declarations.
65  *****************************************************************************/
66 class Instance: public wxApp
67 {
68 public:
69     Instance();
70     Instance( intf_thread_t *_p_intf );
71
72     bool OnInit();
73     int  OnExit();
74
75 private:
76     intf_thread_t *p_intf;
77     wxLocale locale;                                /* locale we'll be using */
78 };
79
80 /*****************************************************************************
81  * Module descriptor
82  *****************************************************************************/
83 #define EMBED_TEXT N_("Embed video in interface")
84 #define EMBED_LONGTEXT N_("Embed the video inside the interface instead " \
85     "of having it in a separate window.")
86 #define BOOKMARKS_TEXT N_("Bookmarks dialog")
87 #define BOOKMARKS_LONGTEXT N_("Show bookmarks dialog at startup" )
88 #define EXTENDED_TEXT N_("Extended GUI")
89 #define EXTENDED_LONGTEXT N_("Show extended GUI (equalizer, image adjust, "  \
90               "video filters...) at startup"  )
91 #define TASKBAR_TEXT N_("Taskbar")
92 #define TASKBAR_LONGTEXT N_("Show VLC on the taskbar")
93 #define MINIMAL_TEXT N_("Minimal interface")
94 #define MINIMAL_LONGTEXT N_("Use minimal interface, with no toolbar and " \
95                 "fewer menus.")
96 #define SIZE_TO_VIDEO_TEXT N_("Size to video")
97 #define SIZE_TO_VIDEO_LONGTEXT N_("Resize VLC to match the video resolution.")
98 #define SYSTRAY_TEXT N_("Systray icon")
99 #define SYSTRAY_LONGTEXT N_("Show a systray icon for VLC")
100 #define LABEL_TEXT N_("Show labels in toolbar")
101 #define LABEL_LONGTEXT N_("Show labels below the icons in the toolbar.")
102
103 #define PLAYLIST_TEXT N_("Playlist view" )
104 #define PLAYLIST_LONGTEXT N_("There are two possible playlist views in the " \
105                 "interface : the normal playlist (separate window), or an " \
106                 "embedded playlist (within the main interface, but with " \
107                 "less features). You can select which one will be available " \
108                 "on the toolbar (or both)." )
109
110 static int pi_playlist_views[] = { 0,1,2 };
111 static const char *psz_playlist_views[] = { N_("Normal" ), N_("Embedded" ) ,
112                                             N_("Both") };
113
114 vlc_module_begin();
115     int i_score = 150;
116     set_shortname( (char*) "wxWidgets" );
117     set_description( (char *) _("wxWidgets interface module") );
118     set_category( CAT_INTERFACE );
119     set_subcategory( SUBCAT_INTERFACE_MAIN );
120     set_capability( "interface", i_score );
121     set_callbacks( Open, Close );
122     add_shortcut( "wxwindows" );
123     add_shortcut( "wxwin" );
124     add_shortcut( "wx" );
125     add_shortcut( "wxwidgets" );
126     set_program( "wxvlc" );
127
128     add_bool( "wx-embed", 1, NULL,
129               EMBED_TEXT, EMBED_LONGTEXT, VLC_FALSE );
130         add_deprecated_alias( "wxwin-enbed" ); /*Deprecated since 0.8.4*/
131     add_bool( "wx-bookmarks", 0, NULL,
132               BOOKMARKS_TEXT, BOOKMARKS_LONGTEXT, VLC_FALSE );
133         add_deprecated_alias( "wxwin-bookmarks" ); /*Deprecated since 0.8.4*/
134     add_bool( "wx-taskbar", 1, NULL,
135               TASKBAR_TEXT, TASKBAR_LONGTEXT, VLC_FALSE );
136         add_deprecated_alias( "wxwin-taskbar" ); /*Deprecated since 0.8.4*/
137     add_bool( "wx-extended", 0, NULL,
138               EXTENDED_TEXT, EXTENDED_LONGTEXT, VLC_FALSE );
139     add_bool( "wx-minimal", 0, NULL,
140               MINIMAL_TEXT, MINIMAL_LONGTEXT, VLC_TRUE );
141         add_deprecated_alias( "wxwin-minimal" ); /*Deprecated since 0.8.4*/
142     add_bool( "wx-autosize", 1, NULL,
143               SIZE_TO_VIDEO_TEXT, SIZE_TO_VIDEO_LONGTEXT, VLC_TRUE );
144         add_deprecated_alias( "wxwin-autosize" ); /*Deprecated since 0.8.4*/
145     add_integer( "wx-playlist-view", 0, NULL, PLAYLIST_TEXT, PLAYLIST_LONGTEXT,
146              VLC_FALSE );
147         change_integer_list( pi_playlist_views, psz_playlist_views, 0 );
148 /* wxCocoa pretends to support this, but at least 2.6.x doesn't */
149 #ifndef __APPLE__
150 #ifdef wxHAS_TASK_BAR_ICON
151     add_bool( "wx-systray", 0, NULL,
152               SYSTRAY_TEXT, SYSTRAY_LONGTEXT, VLC_FALSE );
153         add_deprecated_alias( "wxwin-systray" ); /*Deprecated since 0.8.4*/
154 #endif
155 #endif
156     add_bool( "wx-labels", 0, NULL, LABEL_TEXT, LABEL_LONGTEXT, VLC_TRUE);
157     add_string( "wx-config-last", NULL, NULL,
158                 N_("last config"), N_("last config"), VLC_TRUE );
159         change_autosave();
160         change_internal();
161         add_deprecated_alias( "wxwin-config-last" ); /*Deprecated since 0.8.4*/
162
163     add_submodule();
164     set_description( _("wxWidgets dialogs provider") );
165     set_capability( "dialogs provider", 50 );
166     set_callbacks( OpenDialogs, Close );
167
168 #if !defined(WIN32)
169     linked_with_a_crap_library_which_uses_atexit();
170 #endif
171 vlc_module_end();
172
173 /*****************************************************************************
174  * Open: initialize and create window
175  *****************************************************************************/
176 static int Open( vlc_object_t *p_this )
177 {
178     intf_thread_t *p_intf = (intf_thread_t *)p_this;
179     /* Test in we have an X*/
180 #if defined HAVE_GETENV && (defined __WXGTK__ || defined __WXX11) 
181     if( !getenv( "DISPLAY" ) )
182     {
183         msg_Err( p_intf, "no X server");
184         return VLC_EGENERIC;
185     }
186 #endif
187     /* Allocate instance and initialize some members */
188     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
189     if( p_intf->p_sys == NULL )
190     {
191         msg_Err( p_intf, "out of memory" );
192         return VLC_ENOMEM;
193     }
194     memset( p_intf->p_sys, 0, sizeof( intf_sys_t ) );
195
196     p_intf->pf_run = Run;
197
198     p_intf->p_sys->p_sub = msg_Subscribe( p_intf, MSG_QUEUE_NORMAL );
199
200     /* Initialize wxWidgets thread */
201     p_intf->p_sys->b_playing = 0;
202
203     p_intf->p_sys->p_input = NULL;
204     p_intf->p_sys->i_playing = -1;
205
206     p_intf->p_sys->p_popup_menu = NULL;
207     p_intf->p_sys->p_video_window = NULL;
208
209     p_intf->pf_show_dialog = NULL;
210
211     /* We support play on start */
212     p_intf->b_play = VLC_TRUE;
213
214     p_intf->p_sys->b_video_autosize =
215         config_GetInt( p_intf, "wx-autosize" );
216
217     return VLC_SUCCESS;
218 }
219
220 static int OpenDialogs( vlc_object_t *p_this )
221 {
222     intf_thread_t *p_intf = (intf_thread_t *)p_this;
223     int i_ret = Open( p_this );
224
225     p_intf->pf_show_dialog = ShowDialog;
226
227     return i_ret;
228 }
229
230 /*****************************************************************************
231  * Close: destroy interface window
232  *****************************************************************************/
233 static void Close( vlc_object_t *p_this )
234 {
235     intf_thread_t *p_intf = (intf_thread_t *)p_this;
236
237     vlc_mutex_lock( &p_intf->object_lock );
238     p_intf->b_dead = VLC_TRUE;
239     vlc_mutex_unlock( &p_intf->object_lock );
240
241     if( p_intf->pf_show_dialog )
242     {
243         /* We must destroy the dialogs thread */
244         wxCommandEvent event( wxEVT_DIALOG, INTF_DIALOG_EXIT );
245         p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
246         vlc_thread_join( p_intf );
247     }
248
249     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
250
251     /* */
252     delete p_intf->p_sys->p_window_settings;
253
254 #if (wxCHECK_VERSION(2,5,0))
255     wxClassInfo::sm_classTable = (wxHashTable*)wxClassInfo_sm_classTable_BUGGY;
256 #endif
257
258     /* Destroy structure */
259     free( p_intf->p_sys );
260 }
261
262 /*****************************************************************************
263  * Run: wxWidgets thread
264  *****************************************************************************/
265
266 //when is this called?
267 #if !defined(__BUILTIN__) && defined( WIN32 )
268 HINSTANCE hInstance = 0;
269 extern "C" BOOL WINAPI
270 DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
271 {
272     hInstance = (HINSTANCE)hModule;
273     return TRUE;
274 }
275 #endif
276
277 static void Run( intf_thread_t *p_intf )
278 {
279     if( p_intf->pf_show_dialog )
280     {
281         /* The module is used in dialog provider mode */
282
283         /* Create a new thread for wxWidgets */
284         if( vlc_thread_create( p_intf, "Skins Dialogs Thread",
285                                Init, 0, VLC_TRUE ) )
286         {
287             msg_Err( p_intf, "cannot create Skins Dialogs Thread" );
288             p_intf->pf_show_dialog = NULL;
289         }
290     }
291     else
292     {
293         /* The module is used in interface mode */
294         Init( p_intf );
295     }
296 }
297
298 static void Init( intf_thread_t *p_intf )
299 {
300 #if !defined( WIN32 )
301     static char  *p_args[] = { "" };
302     int i_args = 1;
303 #endif
304
305     /* Hack to pass the p_intf pointer to the new wxWidgets Instance object */
306 #ifdef wxTheApp
307     wxApp::SetInstance( new Instance( p_intf ) );
308 #else
309     wxTheApp = new Instance( p_intf );
310 #endif
311
312 #if defined( WIN32 )
313 #if !defined(__BUILTIN__)
314
315     //because no one knows when DllMain is called
316     if (hInstance == NULL)
317       hInstance = GetModuleHandle(NULL);
318
319     wxEntry( hInstance/*GetModuleHandle(NULL)*/, NULL, NULL, SW_SHOW );
320 #else
321     wxEntry( GetModuleHandle(NULL), NULL, NULL, SW_SHOW );
322 #endif
323 #else
324     wxEntry( i_args, p_args );
325 #endif
326 }
327
328 /* following functions are local */
329
330 /*****************************************************************************
331  * Constructors.
332  *****************************************************************************/
333 Instance::Instance( )
334 {
335 }
336
337 Instance::Instance( intf_thread_t *_p_intf )
338 {
339     /* Initialization */
340     p_intf = _p_intf;
341 }
342
343 IMPLEMENT_APP_NO_MAIN(Instance)
344
345 /*****************************************************************************
346  * Instance::OnInit: the parent interface execution starts here
347  *****************************************************************************
348  * This is the "main program" equivalent, the program execution will
349  * start here.
350  *****************************************************************************/
351 bool Instance::OnInit()
352 {
353     /* Initialization of i18n stuff.
354      * Usefull for things we don't have any control over, like wxWidgets
355      * provided facilities (eg. open file dialog) */
356     locale.Init( wxLANGUAGE_DEFAULT, wxLOCALE_LOAD_DEFAULT );
357
358     /* Load saved window settings */
359     p_intf->p_sys->p_window_settings = new WindowSettings( p_intf );
360
361     /* Make an instance of your derived frame. Passing NULL (the default value
362      * of Frame's constructor is NULL) as the frame doesn't have a parent
363      * since it is the first window */
364
365     if( !p_intf->pf_show_dialog )
366     {
367         /* The module is used in interface mode */
368         long style = wxDEFAULT_FRAME_STYLE;
369         if ( ! config_GetInt( p_intf, "wx-taskbar" ) )
370         {
371             style = wxDEFAULT_FRAME_STYLE|wxFRAME_NO_TASKBAR;
372         }
373
374         Interface *MainInterface = new Interface( p_intf, style );
375         p_intf->p_sys->p_wxwindow = MainInterface;
376
377         /* Show the interface */
378         MainInterface->Show( TRUE );
379         SetTopWindow( MainInterface );
380         MainInterface->Raise();
381     }
382
383     /* Creates the dialogs provider */
384     p_intf->p_sys->p_wxwindow =
385         CreateDialogsProvider( p_intf, p_intf->pf_show_dialog ?
386                                NULL : p_intf->p_sys->p_wxwindow );
387
388     p_intf->p_sys->pf_show_dialog = ShowDialog;
389
390     /* OK, initialization is over */
391     vlc_thread_ready( p_intf );
392
393     /* Check if we need to start playing */
394     if( !p_intf->pf_show_dialog && p_intf->b_play )
395     {
396         playlist_t *p_playlist =
397             (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
398                                            FIND_ANYWHERE );
399         if( p_playlist )
400         {
401             playlist_Control( p_playlist, PLAYLIST_AUTOPLAY, VLC_FALSE );
402             vlc_object_release( p_playlist );
403         }
404     }
405
406     /* Return TRUE to tell program to continue (FALSE would terminate) */
407     return TRUE;
408 }
409
410 /*****************************************************************************
411  * Instance::OnExit: called when the interface execution stops
412  *****************************************************************************/
413 int Instance::OnExit()
414 {
415     if( p_intf->pf_show_dialog )
416     {
417          /* We need to manually clean up the dialogs class */
418          if( p_intf->p_sys->p_wxwindow ) delete p_intf->p_sys->p_wxwindow;
419     }
420
421 #if (wxCHECK_VERSION(2,5,0))
422     wxClassInfo_sm_classTable_BUGGY = wxClassInfo::sm_classTable;
423     wxClassInfo::sm_classTable = 0;
424 #endif
425
426     return 0;
427 }
428
429 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
430                         intf_dialog_args_t *p_arg )
431 {
432     wxCommandEvent event( wxEVT_DIALOG, i_dialog_event );
433     event.SetInt( i_arg );
434     event.SetClientData( p_arg );
435
436 #ifdef WIN32
437     SendMessage( (HWND)p_intf->p_sys->p_wxwindow->GetHandle(),
438                  WM_CANCELMODE, 0, 0 );
439 #endif
440     if( i_dialog_event == INTF_DIALOG_POPUPMENU && i_arg == 0 ) return;
441
442     /* Hack to prevent popup events to be enqueued when
443      * one is already active */
444     if( i_dialog_event != INTF_DIALOG_POPUPMENU ||
445         !p_intf->p_sys->p_popup_menu )
446     {
447         p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
448     }
449 }
450
451 /*****************************************************************************
452  * WindowSettings utility class
453  *****************************************************************************/
454 WindowSettings::WindowSettings( intf_thread_t *_p_intf )
455 {
456     char *psz_org = NULL;
457     char *psz;
458     int i;
459
460     /* */
461     p_intf = _p_intf;
462
463     /* */
464     for( i = 0; i < ID_MAX; i++ )
465     {
466         b_valid[i] = false;
467         b_shown[i] = false;
468         position[i] = wxDefaultPosition;
469         size[i] = wxDefaultSize;
470     }
471     b_shown[ID_MAIN] = true;
472
473     if( p_intf->pf_show_dialog ) return;
474
475     /* Parse the configuration */
476     psz_org = psz = config_GetPsz( p_intf, "wx-config-last" );
477     if( !psz || *psz == '\0' ) return;
478
479     msg_Dbg( p_intf, "Using last windows config '%s'", psz );
480
481     i_screen_w = 0;
482     i_screen_h = 0;
483     while( psz && *psz )
484     {
485         int id, v[4];
486
487         psz = strchr( psz, '(' );
488
489         if( !psz )
490             break;
491         psz++;
492
493         id = strtol( psz, &psz, 0 );
494         if( *psz != ',' ) /* broken cfg */
495         {
496             goto invalid;
497         }
498         psz++;
499
500         for( i = 0; i < 4; i++ )
501         {
502             v[i] = strtol( psz, &psz, 0 );
503
504             if( i < 3 )
505             {
506                 if( *psz != ',' )
507                 {
508                     goto invalid;
509                 }
510                 psz++;
511             }
512             else
513             {
514                 if( *psz != ')' )
515                 {
516                     goto invalid;
517                 }
518             }
519         }
520         if( id == ID_SCREEN )
521         {
522             i_screen_w = v[2];
523             i_screen_h = v[3];
524         }
525         else if( id >= 0 && id < ID_MAX )
526         {
527             b_valid[id] = true;
528             b_shown[id] = true;
529             position[id] = wxPoint( v[0], v[1] );
530             size[id] = wxSize( v[2], v[3] );
531
532             msg_Dbg( p_intf, "id=%d p=(%d,%d) s=(%d,%d)",
533                      id, position[id].x, position[id].y,
534                          size[id].x, size[id].y );
535         }
536
537         psz = strchr( psz, ')' );
538         if( psz ) psz++;
539     }
540
541     if( i_screen_w <= 0 || i_screen_h <= 0 )
542     {
543         goto invalid;
544     }
545
546     for( i = 0; i < ID_MAX; i++ )
547     {
548         if( !b_valid[i] )
549             continue;
550         if( position[i].x < 0 || position[i].y < 0 )
551         {
552             goto invalid;
553         }
554         if( i != ID_SMALL_PLAYLIST && (size[i].x <= 0 || size[i].y <= 0)  )
555         {
556             goto invalid;
557         }
558     }
559
560     if( psz_org ) free( psz_org );
561     return;
562
563 invalid:
564     msg_Dbg( p_intf, "last windows config is invalid (ignored)" );
565     for( i = 0; i < ID_MAX; i++ )
566     {
567         b_valid[i] = false;
568         b_shown[i] = false;
569         position[i] = wxDefaultPosition;
570         size[i] = wxDefaultSize;
571     }
572     if( psz_org ) free( psz_org );
573 }
574
575
576 WindowSettings::~WindowSettings( )
577 {
578     wxString sCfg;
579
580     if( p_intf->pf_show_dialog ) return;
581
582     sCfg = wxString::Format( wxT("(%d,0,0,%d,%d)"), ID_SCREEN,
583                              wxSystemSettings::GetMetric( wxSYS_SCREEN_X ),
584                              wxSystemSettings::GetMetric( wxSYS_SCREEN_Y ) );
585     for( int i = 0; i < ID_MAX; i++ )
586     {
587         if( !b_valid[i] || !b_shown[i] )
588             continue;
589
590         sCfg += wxString::Format( wxT("(%d,%d,%d,%d,%d)"),
591                                   i, position[i].x, position[i].y,
592                                      size[i].x, size[i].y );
593     }
594
595     config_PutPsz( p_intf, "wx-config-last", sCfg.mb_str(wxConvUTF8) );
596 }
597
598 void WindowSettings::SetScreen( int i_screen_w, int i_screen_h )
599 {
600     int i;
601
602     for( i = 0; i < ID_MAX; i++ )
603     {
604         if( !b_valid[i] )
605             continue;
606         if( position[i].x >= i_screen_w || position[i].y >= i_screen_h )
607             goto invalid;
608     }
609     return;
610
611 invalid:
612     for( i = 0; i < ID_MAX; i++ )
613     {
614         b_valid[i] = false;
615         b_shown[i] = false;
616         position[i] = wxDefaultPosition;
617         size[i] = wxDefaultSize;
618     }
619 }
620
621 void WindowSettings::SetSettings( int id, bool _b_shown, wxPoint p, wxSize s )
622 {
623     if( id < 0 || id >= ID_MAX )
624         return;
625
626     b_valid[id] = true;
627     b_shown[id] = _b_shown;
628
629     position[id] = p;
630     size[id] = s;
631 }
632
633 bool WindowSettings::GetSettings( int id, bool& _b_shown, wxPoint& p, wxSize& s)
634 {
635     if( id < 0 || id >= ID_MAX )
636         return false;
637
638     if( !b_valid[id] )
639         return false;
640
641     _b_shown = b_shown[id];
642     p = position[id];
643     s = size[id];
644
645     return true;
646 }