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