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 /*****************************************************************************
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 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")
105 int i_score = getenv( "DISPLAY" ) == NULL ? 15 : 150;
107 set_shortname( (char*) _("wxWindows"));
108 set_description( (char *) _("wxWindows 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 set_program( "wxvlc" );
118 add_bool( "wxwin-embed", 1, NULL,
119 EMBED_TEXT, EMBED_LONGTEXT, VLC_FALSE );
120 add_bool( "wxwin-bookmarks", 0, NULL,
121 BOOKMARKS_TEXT, BOOKMARKS_LONGTEXT, VLC_FALSE );
122 add_bool( "wxwin-taskbar", 1, NULL,
123 TASKBAR_TEXT, TASKBAR_LONGTEXT, VLC_FALSE );
124 add_bool( "wxwin-minimal", 0, NULL,
125 MINIMAL_TEXT, MINIMAL_LONGTEXT, VLC_TRUE );
126 add_bool( "wxwin-size-to-video", 1, NULL,
127 SIZE_TO_VIDEO_TEXT, SIZE_TO_VIDEO_LONGTEXT, VLC_TRUE );
128 #ifdef wxHAS_TASK_BAR_ICON
129 add_bool( "wxwin-systray", 0, NULL,
130 SYSTRAY_TEXT, SYSTRAY_LONGTEXT, VLC_FALSE );
132 add_string( "wxwin-config-last", NULL, NULL,
133 "last config", "last config", VLC_TRUE );
136 set_description( _("wxWindows dialogs provider") );
137 set_capability( "dialogs provider", 50 );
138 set_callbacks( OpenDialogs, Close );
141 linked_with_a_crap_library_which_uses_atexit();
145 /*****************************************************************************
146 * Open: initialize and create window
147 *****************************************************************************/
148 static int Open( vlc_object_t *p_this )
150 intf_thread_t *p_intf = (intf_thread_t *)p_this;
152 /* Allocate instance and initialize some members */
153 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
154 if( p_intf->p_sys == NULL )
156 msg_Err( p_intf, "out of memory" );
159 memset( p_intf->p_sys, 0, sizeof( intf_sys_t ) );
161 p_intf->pf_run = Run;
163 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
165 /* Initialize wxWindows thread */
166 p_intf->p_sys->b_playing = 0;
168 p_intf->p_sys->p_input = NULL;
169 p_intf->p_sys->i_playing = -1;
170 p_intf->p_sys->b_slider_free = 1;
171 p_intf->p_sys->i_slider_pos = p_intf->p_sys->i_slider_oldpos = 0;
173 p_intf->p_sys->p_popup_menu = NULL;
174 p_intf->p_sys->p_video_window = NULL;
176 p_intf->pf_show_dialog = NULL;
178 /* We support play on start */
179 p_intf->b_play = VLC_TRUE;
182 p_intf->p_sys->p_window_settings = new WindowSettings( p_intf );
187 static int OpenDialogs( vlc_object_t *p_this )
189 intf_thread_t *p_intf = (intf_thread_t *)p_this;
190 int i_ret = Open( p_this );
192 p_intf->pf_show_dialog = ShowDialog;
197 /*****************************************************************************
198 * Close: destroy interface window
199 *****************************************************************************/
200 static void Close( vlc_object_t *p_this )
202 intf_thread_t *p_intf = (intf_thread_t *)p_this;
204 vlc_mutex_lock( &p_intf->object_lock );
205 p_intf->b_dead = VLC_TRUE;
206 vlc_mutex_unlock( &p_intf->object_lock );
208 if( p_intf->pf_show_dialog )
210 /* We must destroy the dialogs thread */
211 wxCommandEvent event( wxEVT_DIALOG, INTF_DIALOG_EXIT );
212 p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
213 vlc_thread_join( p_intf );
216 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
219 delete p_intf->p_sys->p_window_settings;
221 /* Destroy structure */
222 free( p_intf->p_sys );
225 /*****************************************************************************
226 * Run: wxWindows thread
227 *****************************************************************************/
229 //when is this called?
230 #if !defined(__BUILTIN__) && defined( WIN32 )
231 HINSTANCE hInstance = 0;
232 extern "C" BOOL WINAPI
233 DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
235 hInstance = (HINSTANCE)hModule;
240 static void Run( intf_thread_t *p_intf )
242 if( p_intf->pf_show_dialog )
244 /* The module is used in dialog provider mode */
246 /* Create a new thread for wxWindows */
247 if( vlc_thread_create( p_intf, "Skins Dialogs Thread",
248 Init, 0, VLC_TRUE ) )
250 msg_Err( p_intf, "cannot create Skins Dialogs Thread" );
251 p_intf->pf_show_dialog = NULL;
256 /* The module is used in interface mode */
261 static void Init( intf_thread_t *p_intf )
263 #if !defined( WIN32 )
264 static char *p_args[] = { "" };
268 /* Hack to pass the p_intf pointer to the new wxWindow Instance object */
270 wxApp::SetInstance( new Instance( p_intf ) );
272 wxTheApp = new Instance( p_intf );
276 #if !defined(__BUILTIN__)
278 //because no one knows when DllMain is called
279 if (hInstance == NULL)
280 hInstance = GetModuleHandle(NULL);
282 wxEntry( hInstance/*GetModuleHandle(NULL)*/, NULL, NULL, SW_SHOW );
284 wxEntry( GetModuleHandle(NULL), NULL, NULL, SW_SHOW );
287 wxEntry( i_args, p_args );
291 /* following functions are local */
293 /*****************************************************************************
295 *****************************************************************************/
296 Instance::Instance( )
300 Instance::Instance( intf_thread_t *_p_intf )
306 IMPLEMENT_APP_NO_MAIN(Instance)
308 /*****************************************************************************
309 * Instance::OnInit: the parent interface execution starts here
310 *****************************************************************************
311 * This is the "main program" equivalent, the program execution will
313 *****************************************************************************/
314 bool Instance::OnInit()
316 /* Initialization of i18n stuff.
317 * Usefull for things we don't have any control over, like wxWindows
318 * provided facilities (eg. open file dialog) */
319 locale.Init( wxLANGUAGE_DEFAULT );
321 /* FIXME: The stream output mrl parsing uses ',' already so we want to
322 * keep the default '.' for floating point numbers. */
323 setlocale( LC_NUMERIC, "C" );
325 /* Make an instance of your derived frame. Passing NULL (the default value
326 * of Frame's constructor is NULL) as the frame doesn't have a parent
327 * since it is the first window */
329 if( !p_intf->pf_show_dialog )
331 /* The module is used in interface mode */
332 long style = wxDEFAULT_FRAME_STYLE;
333 if ( ! config_GetInt( p_intf, "wxwin-taskbar" ) )
335 style = wxDEFAULT_FRAME_STYLE|wxFRAME_NO_TASKBAR;
338 Interface *MainInterface = new Interface( p_intf, style );
339 p_intf->p_sys->p_wxwindow = MainInterface;
341 /* Show the interface */
342 MainInterface->Show( TRUE );
343 SetTopWindow( MainInterface );
344 MainInterface->Raise();
347 /* Creates the dialogs provider */
348 p_intf->p_sys->p_wxwindow =
349 CreateDialogsProvider( p_intf, p_intf->pf_show_dialog ?
350 NULL : p_intf->p_sys->p_wxwindow );
352 p_intf->p_sys->pf_show_dialog = ShowDialog;
354 /* OK, initialization is over */
355 vlc_thread_ready( p_intf );
357 /* Check if we need to start playing */
358 if( !p_intf->pf_show_dialog && p_intf->b_play )
360 playlist_t *p_playlist =
361 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
365 playlist_Play( p_playlist );
366 vlc_object_release( p_playlist );
370 /* Return TRUE to tell program to continue (FALSE would terminate) */
374 /*****************************************************************************
375 * Instance::OnExit: called when the interface execution stops
376 *****************************************************************************/
377 int Instance::OnExit()
379 if( p_intf->pf_show_dialog )
381 /* We need to manually clean up the dialogs class */
382 if( p_intf->p_sys->p_wxwindow ) delete p_intf->p_sys->p_wxwindow;
387 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
388 intf_dialog_args_t *p_arg )
390 wxCommandEvent event( wxEVT_DIALOG, i_dialog_event );
391 event.SetInt( i_arg );
392 event.SetClientData( p_arg );
395 SendMessage( (HWND)p_intf->p_sys->p_wxwindow->GetHandle(),
396 WM_CANCELMODE, 0, 0 );
398 if( i_dialog_event == INTF_DIALOG_POPUPMENU && i_arg == 0 ) return;
400 /* Hack to prevent popup events to be enqueued when
401 * one is already active */
402 if( i_dialog_event != INTF_DIALOG_POPUPMENU ||
403 !p_intf->p_sys->p_popup_menu )
405 p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
409 WindowSettings::WindowSettings( intf_thread_t *_p_intf )
411 char *psz_org = NULL;
419 for( i = 0; i < ID_MAX; i++ )
423 position[i] = wxDefaultPosition;
424 size[i] = wxDefaultSize;
426 b_shown[ID_MAIN] = true;
428 /* Parse the configuration */
429 psz_org = psz = config_GetPsz( p_intf, "wxwin-config-last" );
430 if( !psz || *psz == '\0' )
433 msg_Dbg( p_intf, "Using last windows config '%s'", psz );
441 psz = strchr( psz, '(' );
447 id = strtol( psz, &psz, 0 );
448 if( *psz != ',' ) /* broken cfg */
452 for( i = 0; i < 4; i++ )
454 v[i] = strtol( psz, &psz, 0 );
468 if( id == ID_SCREEN )
473 else if( id >= 0 && id < ID_MAX )
477 position[id] = wxPoint( v[0], v[1] );
478 size[id] = wxSize( v[2], v[3] );
480 msg_Dbg( p_intf, "id=%d p=(%d,%d) s=(%d,%d)",
481 id, position[id].x, position[id].y,
482 size[id].x, size[id].y );
485 psz = strchr( psz, ')' );
489 if( i_screen_w <= 0 || i_screen_h <= 0 )
492 for( i = 0; i < ID_MAX; i++ )
496 if( position[i].x < 0 || position[i].y < 0 )
498 if( size[i].x <= 0 || size[i].y <= 0 )
502 if( psz_org ) free( psz_org );
506 msg_Dbg( p_intf, "last windows config is invalid (ignored)" );
507 for( i = 0; i < ID_MAX; i++ )
511 position[i] = wxDefaultPosition;
512 size[i] = wxDefaultSize;
514 if( psz_org ) free( psz_org );
518 WindowSettings::~WindowSettings( )
523 sCfg = wxString::Format( wxT("(%d,0,0,%d,%d)"), ID_SCREEN,
524 wxSystemSettings::GetMetric( wxSYS_SCREEN_X ),
525 wxSystemSettings::GetMetric( wxSYS_SCREEN_Y ) );
526 for( i = 0; i < ID_MAX; i++ )
528 if( !b_valid[i] || !b_shown[i] )
531 sCfg += wxString::Format( wxT("(%d,%d,%d,%d,%d)"),
532 i, position[i].x, position[i].y,
533 size[i].x, size[i].y );
536 config_PutPsz( p_intf, "wxwin-config-last", sCfg.mb_str() );
537 config_SaveConfigFile( p_intf, "wxwindows" );
539 void WindowSettings::SetScreen( int i_screen_w, int i_screen_h )
543 for( i = 0; i < ID_MAX; i++ )
547 if( position[i].x >= i_screen_w || position[i].y >= i_screen_h )
553 for( i = 0; i < ID_MAX; i++ )
557 position[i] = wxDefaultPosition;
558 size[i] = wxDefaultSize;
562 void WindowSettings::SetSettings( int id, bool _b_shown, wxPoint p, wxSize s )
564 if( id < 0 || id >= ID_MAX )
568 b_shown[id] = _b_shown;
574 bool WindowSettings::GetSettings( int id, bool& _b_shown, wxPoint& p, wxSize& s)
576 if( id < 0 || id >= ID_MAX )
582 _b_shown = b_shown[id];