1 /*****************************************************************************
2 * wxwindows.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: wxwindows.cpp,v 1.33 2003/10/04 14:59:38 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 );
71 intf_thread_t *p_intf;
72 wxLocale locale; /* locale we'll be using */
75 /*****************************************************************************
77 *****************************************************************************/
82 int i_score = getenv( "DISPLAY" ) == NULL ? 15 : 150;
84 set_description( (char *) _("wxWindows interface module") );
85 set_capability( "interface", i_score );
86 set_callbacks( Open, Close );
87 add_shortcut( "wxwindows" );
88 add_shortcut( "wxwin" );
90 set_program( "wxvlc" );
93 set_description( _("wxWindows dialogs provider") );
94 set_capability( "dialogs provider", 50 );
95 set_callbacks( OpenDialogs, Close );
98 linked_with_a_crap_library_which_uses_atexit();
102 /*****************************************************************************
103 * Open: initialize and create window
104 *****************************************************************************/
105 static int Open( vlc_object_t *p_this )
107 intf_thread_t *p_intf = (intf_thread_t *)p_this;
109 /* Allocate instance and initialize some members */
110 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
111 if( p_intf->p_sys == NULL )
113 msg_Err( p_intf, "out of memory" );
117 p_intf->pf_run = Run;
119 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
121 /* Initialize wxWindows thread */
122 p_intf->p_sys->b_playing = 0;
124 p_intf->p_sys->p_input = NULL;
125 p_intf->p_sys->i_playing = -1;
126 p_intf->p_sys->b_slider_free = 1;
127 p_intf->p_sys->i_slider_pos = p_intf->p_sys->i_slider_oldpos = 0;
129 p_intf->p_sys->p_popup_menu = NULL;
131 p_intf->pf_show_dialog = NULL;
136 static int OpenDialogs( vlc_object_t *p_this )
138 intf_thread_t *p_intf = (intf_thread_t *)p_this;
139 int i_ret = Open( p_this );
141 p_intf->pf_show_dialog = ShowDialog;
146 /*****************************************************************************
147 * Close: destroy interface window
148 *****************************************************************************/
149 static void Close( vlc_object_t *p_this )
151 intf_thread_t *p_intf = (intf_thread_t *)p_this;
153 if( p_intf->p_sys->p_input )
155 vlc_object_release( p_intf->p_sys->p_input );
158 if( p_intf->pf_show_dialog )
160 /* We must destroy the dialogs thread */
161 wxCommandEvent event( wxEVT_DIALOG, INTF_DIALOG_EXIT );
162 p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
163 vlc_thread_join( p_intf );
166 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
168 /* Destroy structure */
169 free( p_intf->p_sys );
172 /*****************************************************************************
173 * Run: wxWindows thread
174 *****************************************************************************/
175 #if !defined(__BUILTIN__) && defined( WIN32 )
176 HINSTANCE hInstance = 0;
177 extern "C" BOOL WINAPI
178 DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
180 hInstance = (HINSTANCE)hModule;
185 static void Run( intf_thread_t *p_intf )
187 if( p_intf->pf_show_dialog )
189 /* The module is used in dialog provider mode */
191 /* Create a new thread for wxWindows */
192 if( vlc_thread_create( p_intf, "Skins Dialogs Thread",
193 Init, 0, VLC_TRUE ) )
195 msg_Err( p_intf, "cannot create Skins Dialogs Thread" );
196 p_intf->pf_show_dialog = NULL;
201 /* The module is used in interface mode */
206 static void Init( intf_thread_t *p_intf )
208 #if !defined( WIN32 )
209 static char *p_args[] = { "" };
213 /* Hack to pass the p_intf pointer to the new wxWindow Instance object */
215 wxApp::SetInstance( new Instance( p_intf ) );
217 wxTheApp = new Instance( p_intf );
221 #if !defined(__BUILTIN__)
222 wxEntry( hInstance/*GetModuleHandle(NULL)*/, NULL, NULL, SW_SHOW );
224 wxEntry( GetModuleHandle(NULL), NULL, NULL, SW_SHOW );
227 wxEntry( i_args, p_args );
231 /* following functions are local */
233 /*****************************************************************************
235 *****************************************************************************/
236 Instance::Instance( )
240 Instance::Instance( intf_thread_t *_p_intf )
246 IMPLEMENT_APP_NO_MAIN(Instance)
248 /*****************************************************************************
249 * Instance::OnInit: the parent interface execution starts here
250 *****************************************************************************
251 * This is the "main program" equivalent, the program execution will
253 *****************************************************************************/
254 bool Instance::OnInit()
256 /* Initialization of i18n stuff.
257 * Usefull for things we don't have any control over, like wxWindows
258 * provided facilities (eg. open file dialog) */
259 locale.Init( wxLANGUAGE_DEFAULT );
261 /* Make an instance of your derived frame. Passing NULL (the default value
262 * of Frame's constructor is NULL) as the frame doesn't have a parent
263 * since it is the first window */
265 if( !p_intf->pf_show_dialog )
267 /* The module is used in interface mode */
268 Interface *MainInterface = new Interface( p_intf );
269 p_intf->p_sys->p_wxwindow = MainInterface;
271 /* Show the interface */
272 MainInterface->Show( TRUE );
274 SetTopWindow( MainInterface );
277 new Timer( p_intf, MainInterface );
280 /* Creates the dialogs provider */
281 p_intf->p_sys->p_wxwindow =
282 new DialogsProvider( p_intf, p_intf->pf_show_dialog ?
283 NULL : p_intf->p_sys->p_wxwindow );
285 p_intf->p_sys->pf_show_dialog = ShowDialog;
287 /* OK, initialization is over */
288 vlc_thread_ready( p_intf );
290 /* Return TRUE to tell program to continue (FALSE would terminate) */
294 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
295 intf_dialog_args_t *p_arg )
297 wxCommandEvent event( wxEVT_DIALOG, i_dialog_event );
298 event.SetInt( i_arg );
299 event.SetClientData( p_arg );
302 SendMessage( (HWND)p_intf->p_sys->p_wxwindow->GetHandle(),
303 WM_CANCELMODE, 0, 0 );
305 if( i_dialog_event == INTF_DIALOG_POPUPMENU && i_arg == 0 ) return;
307 /* Hack to prevent popup events to be enqueued when
308 * one is already active */
309 if( i_dialog_event != INTF_DIALOG_POPUPMENU ||
310 !p_intf->p_sys->p_popup_menu )
312 p_intf->p_sys->p_wxwindow->AddPendingEvent( event );