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