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