1 /*****************************************************************************
2 * wxwindows.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2004 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 /* Hack to detect wxWindows 2.5 which has a different wxEntry() prototype */
44 extern int wxEntry( HINSTANCE hInstance, HINSTANCE hPrevInstance = NULL,
45 char *pCmdLine = NULL, int nCmdShow = SW_NORMAL );
48 int wxEntry( int argc, char *argv[] , bool enterLoop = TRUE );
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Open ( vlc_object_t * );
55 static void Close ( vlc_object_t * );
56 static int OpenDialogs ( vlc_object_t * );
58 static void Run ( intf_thread_t * );
59 static void Init ( intf_thread_t * );
61 static void ShowDialog ( intf_thread_t *, int, int, intf_dialog_args_t * );
63 /*****************************************************************************
64 * Local classes declarations.
65 *****************************************************************************/
66 class Instance: public wxApp
70 Instance( intf_thread_t *_p_intf );
76 intf_thread_t *p_intf;
77 wxLocale locale; /* locale we'll be using */
80 /*****************************************************************************
82 *****************************************************************************/
87 int i_score = getenv( "DISPLAY" ) == NULL ? 15 : 150;
89 set_description( (char *) _("wxWindows interface module") );
90 set_capability( "interface", i_score );
91 set_callbacks( Open, Close );
92 add_shortcut( "wxwindows" );
93 add_shortcut( "wxwin" );
95 set_program( "wxvlc" );
98 set_description( _("wxWindows dialogs provider") );
99 set_capability( "dialogs provider", 50 );
100 set_callbacks( OpenDialogs, Close );
103 linked_with_a_crap_library_which_uses_atexit();
107 /*****************************************************************************
108 * Open: initialize and create window
109 *****************************************************************************/
110 static int Open( vlc_object_t *p_this )
112 intf_thread_t *p_intf = (intf_thread_t *)p_this;
114 /* Allocate instance and initialize some members */
115 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
116 if( p_intf->p_sys == NULL )
118 msg_Err( p_intf, "out of memory" );
122 p_intf->pf_run = Run;
124 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
126 /* Initialize wxWindows thread */
127 p_intf->p_sys->b_playing = 0;
129 p_intf->p_sys->p_input = NULL;
130 p_intf->p_sys->i_playing = -1;
131 p_intf->p_sys->b_slider_free = 1;
132 p_intf->p_sys->i_slider_pos = p_intf->p_sys->i_slider_oldpos = 0;
134 p_intf->p_sys->p_popup_menu = NULL;
136 p_intf->pf_show_dialog = NULL;
141 static int OpenDialogs( vlc_object_t *p_this )
143 intf_thread_t *p_intf = (intf_thread_t *)p_this;
144 int i_ret = Open( p_this );
146 p_intf->pf_show_dialog = ShowDialog;
151 /*****************************************************************************
152 * Close: destroy interface window
153 *****************************************************************************/
154 static void Close( vlc_object_t *p_this )
156 intf_thread_t *p_intf = (intf_thread_t *)p_this;
158 if( p_intf->p_sys->p_input )
160 vlc_object_release( p_intf->p_sys->p_input );
163 if( p_intf->pf_show_dialog )
165 /* We must destroy the dialogs thread */
166 wxCommandEvent event( wxEVT_DIALOG, INTF_DIALOG_EXIT );
167 p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
168 vlc_thread_join( p_intf );
171 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
173 /* Destroy structure */
174 free( p_intf->p_sys );
177 /*****************************************************************************
178 * Run: wxWindows thread
179 *****************************************************************************/
180 #if !defined(__BUILTIN__) && defined( WIN32 )
181 HINSTANCE hInstance = 0;
182 extern "C" BOOL WINAPI
183 DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
185 hInstance = (HINSTANCE)hModule;
190 static void Run( intf_thread_t *p_intf )
192 if( p_intf->pf_show_dialog )
194 /* The module is used in dialog provider mode */
196 /* Create a new thread for wxWindows */
197 if( vlc_thread_create( p_intf, "Skins Dialogs Thread",
198 Init, 0, VLC_TRUE ) )
200 msg_Err( p_intf, "cannot create Skins Dialogs Thread" );
201 p_intf->pf_show_dialog = NULL;
206 /* The module is used in interface mode */
211 static void Init( intf_thread_t *p_intf )
213 #if !defined( WIN32 )
214 static char *p_args[] = { "" };
218 /* Hack to pass the p_intf pointer to the new wxWindow Instance object */
220 wxApp::SetInstance( new Instance( p_intf ) );
222 wxTheApp = new Instance( p_intf );
226 #if !defined(__BUILTIN__)
227 wxEntry( hInstance/*GetModuleHandle(NULL)*/, NULL, NULL, SW_SHOW );
229 wxEntry( GetModuleHandle(NULL), NULL, NULL, SW_SHOW );
232 wxEntry( i_args, p_args );
236 /* following functions are local */
238 /*****************************************************************************
240 *****************************************************************************/
241 Instance::Instance( )
245 Instance::Instance( intf_thread_t *_p_intf )
251 IMPLEMENT_APP_NO_MAIN(Instance)
253 /*****************************************************************************
254 * Instance::OnInit: the parent interface execution starts here
255 *****************************************************************************
256 * This is the "main program" equivalent, the program execution will
258 *****************************************************************************/
259 bool Instance::OnInit()
261 /* Initialization of i18n stuff.
262 * Usefull for things we don't have any control over, like wxWindows
263 * provided facilities (eg. open file dialog) */
264 locale.Init( wxLANGUAGE_DEFAULT );
266 /* FIXME: The stream output mrl parsing uses ',' already so we want to
267 * keep the default '.' for floating point numbers. */
268 setlocale( LC_NUMERIC, "C" );
270 /* Make an instance of your derived frame. Passing NULL (the default value
271 * of Frame's constructor is NULL) as the frame doesn't have a parent
272 * since it is the first window */
274 if( !p_intf->pf_show_dialog )
276 /* The module is used in interface mode */
277 Interface *MainInterface = new Interface( p_intf );
278 p_intf->p_sys->p_wxwindow = MainInterface;
280 /* Show the interface */
281 MainInterface->Show( TRUE );
282 SetTopWindow( MainInterface );
283 MainInterface->Raise();
286 /* Creates the dialogs provider */
287 p_intf->p_sys->p_wxwindow =
288 new DialogsProvider( p_intf, p_intf->pf_show_dialog ?
289 NULL : p_intf->p_sys->p_wxwindow );
291 p_intf->p_sys->pf_show_dialog = ShowDialog;
293 /* OK, initialization is over */
294 vlc_thread_ready( p_intf );
296 /* Return TRUE to tell program to continue (FALSE would terminate) */
300 /*****************************************************************************
301 * Instance::OnExit: called when the interface execution stops
302 *****************************************************************************/
303 int Instance::OnExit()
305 if( p_intf->pf_show_dialog )
307 /* We need to manually clean up the dialogs class */
308 if( p_intf->p_sys->p_wxwindow ) delete p_intf->p_sys->p_wxwindow;
313 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
314 intf_dialog_args_t *p_arg )
316 wxCommandEvent event( wxEVT_DIALOG, i_dialog_event );
317 event.SetInt( i_arg );
318 event.SetClientData( p_arg );
321 SendMessage( (HWND)p_intf->p_sys->p_wxwindow->GetHandle(),
322 WM_CANCELMODE, 0, 0 );
324 if( i_dialog_event == INTF_DIALOG_POPUPMENU && i_arg == 0 ) return;
326 /* Hack to prevent popup events to be enqueued when
327 * one is already active */
328 if( i_dialog_event != INTF_DIALOG_POPUPMENU ||
329 !p_intf->p_sys->p_popup_menu )
331 p_intf->p_sys->p_wxwindow->AddPendingEvent( event );