1 /*****************************************************************************
2 * wxwindows.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2005 VideoLAN
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 "wxwindows.h"
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 );
51 int wxEntry( int argc, char *argv[] , bool enterLoop = TRUE );
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Open ( vlc_object_t * );
58 static void Close ( vlc_object_t * );
59 static int OpenDialogs ( vlc_object_t * );
61 static void Run ( intf_thread_t * );
62 static void Init ( intf_thread_t * );
64 static void ShowDialog ( intf_thread_t *, int, int, intf_dialog_args_t * );
66 #if (wxCHECK_VERSION(2,5,0))
67 void *wxClassInfo_sm_classTable_BUGGY = 0;
70 /*****************************************************************************
71 * Local classes declarations.
72 *****************************************************************************/
73 class Instance: public wxApp
77 Instance( intf_thread_t *_p_intf );
83 intf_thread_t *p_intf;
84 wxLocale locale; /* locale we'll be using */
87 /*****************************************************************************
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 " \
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")
109 int i_score = getenv( "DISPLAY" ) == NULL ? 15 : 150;
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" );
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 );
136 add_string( "wxwin-config-last", NULL, NULL,
137 "last config", "last config", VLC_TRUE );
141 set_description( _("wxWindows dialogs provider") );
142 set_capability( "dialogs provider", 50 );
143 set_callbacks( OpenDialogs, Close );
146 linked_with_a_crap_library_which_uses_atexit();
150 /*****************************************************************************
151 * Open: initialize and create window
152 *****************************************************************************/
153 static int Open( vlc_object_t *p_this )
155 intf_thread_t *p_intf = (intf_thread_t *)p_this;
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 )
161 msg_Err( p_intf, "out of memory" );
164 memset( p_intf->p_sys, 0, sizeof( intf_sys_t ) );
166 p_intf->pf_run = Run;
168 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
170 /* Initialize wxWindows thread */
171 p_intf->p_sys->b_playing = 0;
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;
178 p_intf->p_sys->p_popup_menu = NULL;
179 p_intf->p_sys->p_video_window = NULL;
181 p_intf->pf_show_dialog = NULL;
183 /* We support play on start */
184 p_intf->b_play = VLC_TRUE;
186 p_intf->p_sys->b_video_autosize =
187 config_GetInt( p_intf, "wxwin-autosize" );
192 static int OpenDialogs( vlc_object_t *p_this )
194 intf_thread_t *p_intf = (intf_thread_t *)p_this;
195 int i_ret = Open( p_this );
197 p_intf->pf_show_dialog = ShowDialog;
202 /*****************************************************************************
203 * Close: destroy interface window
204 *****************************************************************************/
205 static void Close( vlc_object_t *p_this )
207 intf_thread_t *p_intf = (intf_thread_t *)p_this;
209 vlc_mutex_lock( &p_intf->object_lock );
210 p_intf->b_dead = VLC_TRUE;
211 vlc_mutex_unlock( &p_intf->object_lock );
213 if( p_intf->pf_show_dialog )
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 );
221 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
224 delete p_intf->p_sys->p_window_settings;
226 #if (wxCHECK_VERSION(2,5,0))
227 wxClassInfo::sm_classTable = wxClassInfo_sm_classTable_BUGGY;
230 /* Destroy structure */
231 free( p_intf->p_sys );
234 /*****************************************************************************
235 * Run: wxWindows thread
236 *****************************************************************************/
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)
244 hInstance = (HINSTANCE)hModule;
249 static void Run( intf_thread_t *p_intf )
251 if( p_intf->pf_show_dialog )
253 /* The module is used in dialog provider mode */
255 /* Create a new thread for wxWindows */
256 if( vlc_thread_create( p_intf, "Skins Dialogs Thread",
257 Init, 0, VLC_TRUE ) )
259 msg_Err( p_intf, "cannot create Skins Dialogs Thread" );
260 p_intf->pf_show_dialog = NULL;
265 /* The module is used in interface mode */
270 static void Init( intf_thread_t *p_intf )
272 #if !defined( WIN32 )
273 static char *p_args[] = { "" };
277 /* Hack to pass the p_intf pointer to the new wxWindow Instance object */
279 wxApp::SetInstance( new Instance( p_intf ) );
281 wxTheApp = new Instance( p_intf );
285 #if !defined(__BUILTIN__)
287 //because no one knows when DllMain is called
288 if (hInstance == NULL)
289 hInstance = GetModuleHandle(NULL);
291 wxEntry( hInstance/*GetModuleHandle(NULL)*/, NULL, NULL, SW_SHOW );
293 wxEntry( GetModuleHandle(NULL), NULL, NULL, SW_SHOW );
296 wxEntry( i_args, p_args );
300 /* following functions are local */
302 /*****************************************************************************
304 *****************************************************************************/
305 Instance::Instance( )
309 Instance::Instance( intf_thread_t *_p_intf )
315 IMPLEMENT_APP_NO_MAIN(Instance)
317 /*****************************************************************************
318 * Instance::OnInit: the parent interface execution starts here
319 *****************************************************************************
320 * This is the "main program" equivalent, the program execution will
322 *****************************************************************************/
323 bool Instance::OnInit()
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 );
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" );
334 /* Load saved window settings */
335 p_intf->p_sys->p_window_settings = new WindowSettings( p_intf );
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 */
341 if( !p_intf->pf_show_dialog )
343 /* The module is used in interface mode */
344 long style = wxDEFAULT_FRAME_STYLE;
345 if ( ! config_GetInt( p_intf, "wxwin-taskbar" ) )
347 style = wxDEFAULT_FRAME_STYLE|wxFRAME_NO_TASKBAR;
350 Interface *MainInterface = new Interface( p_intf, style );
351 p_intf->p_sys->p_wxwindow = MainInterface;
353 /* Show the interface */
354 MainInterface->Show( TRUE );
355 SetTopWindow( MainInterface );
356 MainInterface->Raise();
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 );
364 p_intf->p_sys->pf_show_dialog = ShowDialog;
366 /* OK, initialization is over */
367 vlc_thread_ready( p_intf );
369 /* Check if we need to start playing */
370 if( !p_intf->pf_show_dialog && p_intf->b_play )
372 playlist_t *p_playlist =
373 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
377 playlist_LockControl( p_playlist, PLAYLIST_AUTOPLAY );
378 vlc_object_release( p_playlist );
382 /* Return TRUE to tell program to continue (FALSE would terminate) */
386 /*****************************************************************************
387 * Instance::OnExit: called when the interface execution stops
388 *****************************************************************************/
389 int Instance::OnExit()
391 if( p_intf->pf_show_dialog )
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;
397 #if (wxCHECK_VERSION(2,5,0))
398 wxClassInfo_sm_classTable_BUGGY = wxClassInfo::sm_classTable;
399 wxClassInfo::sm_classTable = 0;
405 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
406 intf_dialog_args_t *p_arg )
408 wxCommandEvent event( wxEVT_DIALOG, i_dialog_event );
409 event.SetInt( i_arg );
410 event.SetClientData( p_arg );
413 SendMessage( (HWND)p_intf->p_sys->p_wxwindow->GetHandle(),
414 WM_CANCELMODE, 0, 0 );
416 if( i_dialog_event == INTF_DIALOG_POPUPMENU && i_arg == 0 ) return;
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 )
423 p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
427 /*****************************************************************************
428 * WindowSettings utility class
429 *****************************************************************************/
430 WindowSettings::WindowSettings( intf_thread_t *_p_intf )
432 char *psz_org = NULL;
440 for( i = 0; i < ID_MAX; i++ )
444 position[i] = wxDefaultPosition;
445 size[i] = wxDefaultSize;
447 b_shown[ID_MAIN] = true;
449 if( p_intf->pf_show_dialog ) return;
451 /* Parse the configuration */
452 psz_org = psz = config_GetPsz( p_intf, "wxwin-config-last" );
453 if( !psz || *psz == '\0' ) return;
455 msg_Dbg( p_intf, "Using last windows config '%s'", psz );
463 psz = strchr( psz, '(' );
469 id = strtol( psz, &psz, 0 );
470 if( *psz != ',' ) /* broken cfg */
474 for( i = 0; i < 4; i++ )
476 v[i] = strtol( psz, &psz, 0 );
490 if( id == ID_SCREEN )
495 else if( id >= 0 && id < ID_MAX )
499 position[id] = wxPoint( v[0], v[1] );
500 size[id] = wxSize( v[2], v[3] );
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 );
507 psz = strchr( psz, ')' );
511 if( i_screen_w <= 0 || i_screen_h <= 0 )
514 for( i = 0; i < ID_MAX; i++ )
518 if( position[i].x < 0 || position[i].y < 0 )
520 if( size[i].x <= 0 || size[i].y <= 0 )
524 if( psz_org ) free( psz_org );
528 msg_Dbg( p_intf, "last windows config is invalid (ignored)" );
529 for( i = 0; i < ID_MAX; i++ )
533 position[i] = wxDefaultPosition;
534 size[i] = wxDefaultSize;
536 if( psz_org ) free( psz_org );
540 WindowSettings::~WindowSettings( )
544 if( p_intf->pf_show_dialog ) return;
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++ )
551 if( !b_valid[i] || !b_shown[i] )
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 );
559 config_PutPsz( p_intf, "wxwin-config-last", sCfg.mb_str() );
562 void WindowSettings::SetScreen( int i_screen_w, int i_screen_h )
566 for( i = 0; i < ID_MAX; i++ )
570 if( position[i].x >= i_screen_w || position[i].y >= i_screen_h )
576 for( i = 0; i < ID_MAX; i++ )
580 position[i] = wxDefaultPosition;
581 size[i] = wxDefaultSize;
585 void WindowSettings::SetSettings( int id, bool _b_shown, wxPoint p, wxSize s )
587 if( id < 0 || id >= ID_MAX )
591 b_shown[id] = _b_shown;
597 bool WindowSettings::GetSettings( int id, bool& _b_shown, wxPoint& p, wxSize& s)
599 if( id < 0 || id >= ID_MAX )
605 _b_shown = b_shown[id];