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