1 /*****************************************************************************
2 * wxwidgets.cpp : wxWidgets plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <errno.h> /* ENOMEM */
29 #include <string.h> /* strerror() */
39 #include "interface.hpp"
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 );
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Open ( vlc_object_t * );
54 static void Close ( vlc_object_t * );
55 static int OpenDialogs ( vlc_object_t * );
57 static void Run ( intf_thread_t * );
58 static void Init ( intf_thread_t * );
60 static void ShowDialog ( intf_thread_t *, int, int, intf_dialog_args_t * );
62 #if (wxCHECK_VERSION(2,5,0))
63 void *wxClassInfo_sm_classTable_BUGGY = 0;
66 /*****************************************************************************
67 * Local classes declarations.
68 *****************************************************************************/
69 class Instance: public wxApp
73 Instance( intf_thread_t *_p_intf );
79 intf_thread_t *p_intf;
80 wxLocale locale; /* locale we'll be using */
83 /*****************************************************************************
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 " \
92 #define EXTENDED_TEXT N_("Show extended GUI")
93 #define EXTENDED_LONGTEXT N_("Show extended GUI")
94 #define TASKBAR_TEXT N_("Show taskbar entry")
95 #define TASKBAR_LONGTEXT N_("Show taskbar entry")
96 #define MINIMAL_TEXT N_("Minimal interface")
97 #define MINIMAL_LONGTEXT N_("Use minimal interface, no toolbar, few menus")
98 #define SIZE_TO_VIDEO_TEXT N_("Size to video")
99 #define SIZE_TO_VIDEO_LONGTEXT N_("Resize VLC to match the video resolution")
100 #define SYSTRAY_TEXT N_("Show systray icon")
101 #define SYSTRAY_LONGTEXT N_("Show systray icon")
107 int i_score = getenv( "DISPLAY" ) == NULL ? 15 : 150;
109 set_shortname( (char*) "wxWidgets" );
110 set_description( (char *) _("wxWidgets interface module") );
111 set_category( CAT_INTERFACE );
112 set_subcategory( SUBCAT_INTERFACE_GENERAL );
113 set_capability( "interface", i_score );
114 set_callbacks( Open, Close );
115 add_shortcut( "wxwindows" );
116 add_shortcut( "wxwin" );
117 add_shortcut( "wx" );
118 add_shortcut( "wxwidgets" );
119 set_program( "wxvlc" );
121 add_bool( "wx-embed", 1, NULL,
122 EMBED_TEXT, EMBED_LONGTEXT, VLC_FALSE );
123 add_deprecated( "wxwin-enbed", VLC_FALSE); /*Deprecated since 0.8.4*/
124 add_bool( "wx-bookmarks", 0, NULL,
125 BOOKMARKS_TEXT, BOOKMARKS_LONGTEXT, VLC_FALSE );
126 add_deprecated( "wxwin-bookmarks", VLC_FALSE); /*Deprecated since 0.8.4*/
127 add_bool( "wx-taskbar", 1, NULL,
128 TASKBAR_TEXT, TASKBAR_LONGTEXT, VLC_FALSE );
129 add_deprecated( "wxwin-taskbar", VLC_FALSE); /*Deprecated since 0.8.4*/
130 add_bool( "wx-extended", 0, NULL,
131 EXTENDED_TEXT, EXTENDED_LONGTEXT, VLC_FALSE );
132 add_bool( "wx-minimal", 0, NULL,
133 MINIMAL_TEXT, MINIMAL_LONGTEXT, VLC_TRUE );
134 add_deprecated( "wxwin-minimal", VLC_FALSE); /*Deprecated since 0.8.4*/
135 add_bool( "wx-autosize", 1, NULL,
136 SIZE_TO_VIDEO_TEXT, SIZE_TO_VIDEO_LONGTEXT, VLC_TRUE );
137 add_deprecated( "wxwin-autosize", VLC_FALSE); /*Deprecated since 0.8.4*/
138 #ifdef wxHAS_TASK_BAR_ICON
139 add_bool( "wx-systray", 0, NULL,
140 SYSTRAY_TEXT, SYSTRAY_LONGTEXT, VLC_FALSE );
141 add_deprecated( "wxwin-systray", VLC_FALSE); /*Deprecated since 0.8.4*/
143 add_string( "wx-config-last", NULL, NULL,
144 "last config", "last config", VLC_TRUE );
146 add_deprecated( "wxwin-config-last", VLC_FALSE); /*Deprecated since 0.8.4*/
149 set_description( _("wxWidgets dialogs provider") );
150 set_capability( "dialogs provider", 50 );
151 set_callbacks( OpenDialogs, Close );
154 linked_with_a_crap_library_which_uses_atexit();
158 /*****************************************************************************
159 * Open: initialize and create window
160 *****************************************************************************/
161 static int Open( vlc_object_t *p_this )
163 intf_thread_t *p_intf = (intf_thread_t *)p_this;
165 /* Allocate instance and initialize some members */
166 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
167 if( p_intf->p_sys == NULL )
169 msg_Err( p_intf, "out of memory" );
172 memset( p_intf->p_sys, 0, sizeof( intf_sys_t ) );
174 p_intf->pf_run = Run;
176 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
178 /* Initialize wxWidgets thread */
179 p_intf->p_sys->b_playing = 0;
181 p_intf->p_sys->p_input = NULL;
182 p_intf->p_sys->i_playing = -1;
183 p_intf->p_sys->b_slider_free = 1;
184 p_intf->p_sys->i_slider_pos = p_intf->p_sys->i_slider_oldpos = 0;
186 p_intf->p_sys->p_popup_menu = NULL;
187 p_intf->p_sys->p_video_window = NULL;
189 p_intf->pf_show_dialog = NULL;
191 /* We support play on start */
192 p_intf->b_play = VLC_TRUE;
194 p_intf->p_sys->b_video_autosize =
195 config_GetInt( p_intf, "wx-autosize" );
200 static int OpenDialogs( vlc_object_t *p_this )
202 intf_thread_t *p_intf = (intf_thread_t *)p_this;
203 int i_ret = Open( p_this );
205 p_intf->pf_show_dialog = ShowDialog;
210 /*****************************************************************************
211 * Close: destroy interface window
212 *****************************************************************************/
213 static void Close( vlc_object_t *p_this )
215 intf_thread_t *p_intf = (intf_thread_t *)p_this;
217 vlc_mutex_lock( &p_intf->object_lock );
218 p_intf->b_dead = VLC_TRUE;
219 vlc_mutex_unlock( &p_intf->object_lock );
221 if( p_intf->pf_show_dialog )
223 /* We must destroy the dialogs thread */
224 wxCommandEvent event( wxEVT_DIALOG, INTF_DIALOG_EXIT );
225 p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
226 vlc_thread_join( p_intf );
229 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
232 delete p_intf->p_sys->p_window_settings;
234 #if (wxCHECK_VERSION(2,5,0))
235 wxClassInfo::sm_classTable = (wxHashTable*)wxClassInfo_sm_classTable_BUGGY;
238 /* Destroy structure */
239 free( p_intf->p_sys );
242 /*****************************************************************************
243 * Run: wxWidgets thread
244 *****************************************************************************/
246 //when is this called?
247 #if !defined(__BUILTIN__) && defined( WIN32 )
248 HINSTANCE hInstance = 0;
249 extern "C" BOOL WINAPI
250 DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
252 hInstance = (HINSTANCE)hModule;
257 static void Run( intf_thread_t *p_intf )
259 if( p_intf->pf_show_dialog )
261 /* The module is used in dialog provider mode */
263 /* Create a new thread for wxWidgets */
264 if( vlc_thread_create( p_intf, "Skins Dialogs Thread",
265 Init, 0, VLC_TRUE ) )
267 msg_Err( p_intf, "cannot create Skins Dialogs Thread" );
268 p_intf->pf_show_dialog = NULL;
273 /* The module is used in interface mode */
278 static void Init( intf_thread_t *p_intf )
280 #if !defined( WIN32 )
281 static char *p_args[] = { "" };
285 /* Hack to pass the p_intf pointer to the new wxWidgets Instance object */
287 wxApp::SetInstance( new Instance( p_intf ) );
289 wxTheApp = new Instance( p_intf );
293 #if !defined(__BUILTIN__)
295 //because no one knows when DllMain is called
296 if (hInstance == NULL)
297 hInstance = GetModuleHandle(NULL);
299 wxEntry( hInstance/*GetModuleHandle(NULL)*/, NULL, NULL, SW_SHOW );
301 wxEntry( GetModuleHandle(NULL), NULL, NULL, SW_SHOW );
304 wxEntry( i_args, p_args );
306 setlocale( LC_NUMERIC, "C" );
309 /* following functions are local */
311 /*****************************************************************************
313 *****************************************************************************/
314 Instance::Instance( )
318 Instance::Instance( intf_thread_t *_p_intf )
324 IMPLEMENT_APP_NO_MAIN(Instance)
326 /*****************************************************************************
327 * Instance::OnInit: the parent interface execution starts here
328 *****************************************************************************
329 * This is the "main program" equivalent, the program execution will
331 *****************************************************************************/
332 bool Instance::OnInit()
334 /* Initialization of i18n stuff.
335 * Usefull for things we don't have any control over, like wxWidgets
336 * provided facilities (eg. open file dialog) */
337 locale.Init( wxLANGUAGE_DEFAULT, wxLOCALE_LOAD_DEFAULT );
338 setlocale( LC_NUMERIC, "C" );
340 /* Load saved window settings */
341 p_intf->p_sys->p_window_settings = new WindowSettings( p_intf );
343 /* Make an instance of your derived frame. Passing NULL (the default value
344 * of Frame's constructor is NULL) as the frame doesn't have a parent
345 * since it is the first window */
347 if( !p_intf->pf_show_dialog )
349 /* The module is used in interface mode */
350 long style = wxDEFAULT_FRAME_STYLE;
351 if ( ! config_GetInt( p_intf, "wx-taskbar" ) )
353 style = wxDEFAULT_FRAME_STYLE|wxFRAME_NO_TASKBAR;
356 Interface *MainInterface = new Interface( p_intf, style );
357 p_intf->p_sys->p_wxwindow = MainInterface;
359 /* Show the interface */
360 MainInterface->Show( TRUE );
361 SetTopWindow( MainInterface );
362 MainInterface->Raise();
365 /* Creates the dialogs provider */
366 p_intf->p_sys->p_wxwindow =
367 CreateDialogsProvider( p_intf, p_intf->pf_show_dialog ?
368 NULL : p_intf->p_sys->p_wxwindow );
370 p_intf->p_sys->pf_show_dialog = ShowDialog;
372 /* OK, initialization is over */
373 vlc_thread_ready( p_intf );
375 /* Check if we need to start playing */
376 if( !p_intf->pf_show_dialog && p_intf->b_play )
378 playlist_t *p_playlist =
379 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
383 playlist_LockControl( p_playlist, PLAYLIST_AUTOPLAY );
384 vlc_object_release( p_playlist );
388 /* Return TRUE to tell program to continue (FALSE would terminate) */
392 /*****************************************************************************
393 * Instance::OnExit: called when the interface execution stops
394 *****************************************************************************/
395 int Instance::OnExit()
397 if( p_intf->pf_show_dialog )
399 /* We need to manually clean up the dialogs class */
400 if( p_intf->p_sys->p_wxwindow ) delete p_intf->p_sys->p_wxwindow;
403 #if (wxCHECK_VERSION(2,5,0))
404 wxClassInfo_sm_classTable_BUGGY = wxClassInfo::sm_classTable;
405 wxClassInfo::sm_classTable = 0;
411 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
412 intf_dialog_args_t *p_arg )
414 wxCommandEvent event( wxEVT_DIALOG, i_dialog_event );
415 event.SetInt( i_arg );
416 event.SetClientData( p_arg );
419 SendMessage( (HWND)p_intf->p_sys->p_wxwindow->GetHandle(),
420 WM_CANCELMODE, 0, 0 );
422 if( i_dialog_event == INTF_DIALOG_POPUPMENU && i_arg == 0 ) return;
424 /* Hack to prevent popup events to be enqueued when
425 * one is already active */
426 if( i_dialog_event != INTF_DIALOG_POPUPMENU ||
427 !p_intf->p_sys->p_popup_menu )
429 p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
433 /*****************************************************************************
434 * WindowSettings utility class
435 *****************************************************************************/
436 WindowSettings::WindowSettings( intf_thread_t *_p_intf )
438 char *psz_org = NULL;
446 for( i = 0; i < ID_MAX; i++ )
450 position[i] = wxDefaultPosition;
451 size[i] = wxDefaultSize;
453 b_shown[ID_MAIN] = true;
455 if( p_intf->pf_show_dialog ) return;
457 /* Parse the configuration */
458 psz_org = psz = config_GetPsz( p_intf, "wx-config-last" );
459 if( !psz || *psz == '\0' ) return;
461 msg_Dbg( p_intf, "Using last windows config '%s'", psz );
469 psz = strchr( psz, '(' );
475 id = strtol( psz, &psz, 0 );
476 if( *psz != ',' ) /* broken cfg */
480 for( i = 0; i < 4; i++ )
482 v[i] = strtol( psz, &psz, 0 );
496 if( id == ID_SCREEN )
501 else if( id >= 0 && id < ID_MAX )
505 position[id] = wxPoint( v[0], v[1] );
506 size[id] = wxSize( v[2], v[3] );
508 msg_Dbg( p_intf, "id=%d p=(%d,%d) s=(%d,%d)",
509 id, position[id].x, position[id].y,
510 size[id].x, size[id].y );
513 psz = strchr( psz, ')' );
517 if( i_screen_w <= 0 || i_screen_h <= 0 )
520 for( i = 0; i < ID_MAX; i++ )
524 if( position[i].x < 0 || position[i].y < 0 )
526 if( size[i].x <= 0 || size[i].y <= 0 )
530 if( psz_org ) free( psz_org );
534 msg_Dbg( p_intf, "last windows config is invalid (ignored)" );
535 for( i = 0; i < ID_MAX; i++ )
539 position[i] = wxDefaultPosition;
540 size[i] = wxDefaultSize;
542 if( psz_org ) free( psz_org );
546 WindowSettings::~WindowSettings( )
550 if( p_intf->pf_show_dialog ) return;
552 sCfg = wxString::Format( wxT("(%d,0,0,%d,%d)"), ID_SCREEN,
553 wxSystemSettings::GetMetric( wxSYS_SCREEN_X ),
554 wxSystemSettings::GetMetric( wxSYS_SCREEN_Y ) );
555 for( int i = 0; i < ID_MAX; i++ )
557 if( !b_valid[i] || !b_shown[i] )
560 sCfg += wxString::Format( wxT("(%d,%d,%d,%d,%d)"),
561 i, position[i].x, position[i].y,
562 size[i].x, size[i].y );
565 config_PutPsz( p_intf, "wx-config-last", sCfg.mb_str() );
568 void WindowSettings::SetScreen( int i_screen_w, int i_screen_h )
572 for( i = 0; i < ID_MAX; i++ )
576 if( position[i].x >= i_screen_w || position[i].y >= i_screen_h )
582 for( i = 0; i < ID_MAX; i++ )
586 position[i] = wxDefaultPosition;
587 size[i] = wxDefaultSize;
591 void WindowSettings::SetSettings( int id, bool _b_shown, wxPoint p, wxSize s )
593 if( id < 0 || id >= ID_MAX )
597 b_shown[id] = _b_shown;
603 bool WindowSettings::GetSettings( int id, bool& _b_shown, wxPoint& p, wxSize& s)
605 if( id < 0 || id >= ID_MAX )
611 _b_shown = b_shown[id];