1 /*****************************************************************************
2 * wxwindows.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: wxwindows.cpp,v 1.35 2003/10/15 12:24:14 gbazin Exp $
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() */
35 #include "wxwindows.h"
38 #if defined(WIN32) && defined(_WX_INIT_H_)
39 /* Hack to detect wxWindows 2.5 which has a different wxEntry() prototype */
40 extern int wxEntry( HINSTANCE hInstance, HINSTANCE hPrevInstance = NULL,
41 char *pCmdLine = NULL, int nCmdShow = SW_NORMAL );
44 int wxEntry( int argc, char *argv[] , bool enterLoop = TRUE );
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Open ( vlc_object_t * );
51 static void Close ( vlc_object_t * );
52 static int OpenDialogs ( vlc_object_t * );
54 static void Run ( intf_thread_t * );
55 static void Init ( intf_thread_t * );
57 static void ShowDialog ( intf_thread_t *, int, int, intf_dialog_args_t * );
59 /*****************************************************************************
60 * Local classes declarations.
61 *****************************************************************************/
62 class Instance: public wxApp
66 Instance( intf_thread_t *_p_intf );
72 intf_thread_t *p_intf;
73 wxLocale locale; /* locale we'll be using */
76 /*****************************************************************************
78 *****************************************************************************/
83 int i_score = getenv( "DISPLAY" ) == NULL ? 15 : 150;
85 set_description( (char *) _("wxWindows interface module") );
86 set_capability( "interface", i_score );
87 set_callbacks( Open, Close );
88 add_shortcut( "wxwindows" );
89 add_shortcut( "wxwin" );
91 set_program( "wxvlc" );
94 set_description( _("wxWindows dialogs provider") );
95 set_capability( "dialogs provider", 50 );
96 set_callbacks( OpenDialogs, Close );
99 linked_with_a_crap_library_which_uses_atexit();
103 /*****************************************************************************
104 * Open: initialize and create window
105 *****************************************************************************/
106 static int Open( vlc_object_t *p_this )
108 intf_thread_t *p_intf = (intf_thread_t *)p_this;
110 /* Allocate instance and initialize some members */
111 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
112 if( p_intf->p_sys == NULL )
114 msg_Err( p_intf, "out of memory" );
118 p_intf->pf_run = Run;
120 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
122 /* Initialize wxWindows thread */
123 p_intf->p_sys->b_playing = 0;
125 p_intf->p_sys->p_input = NULL;
126 p_intf->p_sys->i_playing = -1;
127 p_intf->p_sys->b_slider_free = 1;
128 p_intf->p_sys->i_slider_pos = p_intf->p_sys->i_slider_oldpos = 0;
130 p_intf->p_sys->p_popup_menu = NULL;
132 p_intf->pf_show_dialog = NULL;
137 static int OpenDialogs( vlc_object_t *p_this )
139 intf_thread_t *p_intf = (intf_thread_t *)p_this;
140 int i_ret = Open( p_this );
142 p_intf->pf_show_dialog = ShowDialog;
147 /*****************************************************************************
148 * Close: destroy interface window
149 *****************************************************************************/
150 static void Close( vlc_object_t *p_this )
152 intf_thread_t *p_intf = (intf_thread_t *)p_this;
154 if( p_intf->p_sys->p_input )
156 vlc_object_release( p_intf->p_sys->p_input );
159 if( p_intf->pf_show_dialog )
161 /* We must destroy the dialogs thread */
162 wxCommandEvent event( wxEVT_DIALOG, INTF_DIALOG_EXIT );
163 p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
164 vlc_thread_join( p_intf );
167 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
169 /* Destroy structure */
170 free( p_intf->p_sys );
173 /*****************************************************************************
174 * Run: wxWindows thread
175 *****************************************************************************/
176 #if !defined(__BUILTIN__) && defined( WIN32 )
177 HINSTANCE hInstance = 0;
178 extern "C" BOOL WINAPI
179 DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
181 hInstance = (HINSTANCE)hModule;
186 static void Run( intf_thread_t *p_intf )
188 if( p_intf->pf_show_dialog )
190 /* The module is used in dialog provider mode */
192 /* Create a new thread for wxWindows */
193 if( vlc_thread_create( p_intf, "Skins Dialogs Thread",
194 Init, 0, VLC_TRUE ) )
196 msg_Err( p_intf, "cannot create Skins Dialogs Thread" );
197 p_intf->pf_show_dialog = NULL;
202 /* The module is used in interface mode */
207 static void Init( intf_thread_t *p_intf )
209 #if !defined( WIN32 )
210 static char *p_args[] = { "" };
214 /* Hack to pass the p_intf pointer to the new wxWindow Instance object */
216 wxApp::SetInstance( new Instance( p_intf ) );
218 wxTheApp = new Instance( p_intf );
222 #if !defined(__BUILTIN__)
223 wxEntry( hInstance/*GetModuleHandle(NULL)*/, NULL, NULL, SW_SHOW );
225 wxEntry( GetModuleHandle(NULL), NULL, NULL, SW_SHOW );
228 wxEntry( i_args, p_args );
232 /* following functions are local */
234 /*****************************************************************************
236 *****************************************************************************/
237 Instance::Instance( )
241 Instance::Instance( intf_thread_t *_p_intf )
247 IMPLEMENT_APP_NO_MAIN(Instance)
249 /*****************************************************************************
250 * Instance::OnInit: the parent interface execution starts here
251 *****************************************************************************
252 * This is the "main program" equivalent, the program execution will
254 *****************************************************************************/
255 bool Instance::OnInit()
257 /* Initialization of i18n stuff.
258 * Usefull for things we don't have any control over, like wxWindows
259 * provided facilities (eg. open file dialog) */
260 locale.Init( wxLANGUAGE_DEFAULT );
262 /* Make an instance of your derived frame. Passing NULL (the default value
263 * of Frame's constructor is NULL) as the frame doesn't have a parent
264 * since it is the first window */
266 if( !p_intf->pf_show_dialog )
268 /* The module is used in interface mode */
269 Interface *MainInterface = new Interface( p_intf );
270 p_intf->p_sys->p_wxwindow = MainInterface;
272 /* Show the interface */
273 MainInterface->Show( TRUE );
274 SetTopWindow( MainInterface );
275 MainInterface->Raise();
278 /* Creates the dialogs provider */
279 p_intf->p_sys->p_wxwindow =
280 new DialogsProvider( p_intf, p_intf->pf_show_dialog ?
281 NULL : p_intf->p_sys->p_wxwindow );
283 p_intf->p_sys->pf_show_dialog = ShowDialog;
285 /* OK, initialization is over */
286 vlc_thread_ready( p_intf );
288 /* Return TRUE to tell program to continue (FALSE would terminate) */
292 /*****************************************************************************
293 * Instance::OnExit: called when the interface execution stops
294 *****************************************************************************/
295 int Instance::OnExit()
297 if( p_intf->pf_show_dialog )
299 /* We need to manually clean up the dialogs class */
300 if( p_intf->p_sys->p_wxwindow ) delete p_intf->p_sys->p_wxwindow;
305 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
306 intf_dialog_args_t *p_arg )
308 wxCommandEvent event( wxEVT_DIALOG, i_dialog_event );
309 event.SetInt( i_arg );
310 event.SetClientData( p_arg );
313 SendMessage( (HWND)p_intf->p_sys->p_wxwindow->GetHandle(),
314 WM_CANCELMODE, 0, 0 );
316 if( i_dialog_event == INTF_DIALOG_POPUPMENU && i_arg == 0 ) return;
318 /* Hack to prevent popup events to be enqueued when
319 * one is already active */
320 if( i_dialog_event != INTF_DIALOG_POPUPMENU ||
321 !p_intf->p_sys->p_popup_menu )
323 p_intf->p_sys->p_wxwindow->AddPendingEvent( event );