]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/wxwindows.cpp
* include/vlc/vlc.h: added a b_play parameter to the libvlc VLC_AddIntf() prototype.
[vlc] / modules / gui / wxwindows / wxwindows.cpp
1 /*****************************************************************************
2  * wxwindows.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/intf.h>
34
35 #ifdef HAVE_LOCALE_H
36 #   include <locale.h>
37 #endif
38
39 #include "wxwindows.h"
40
41 /* Temporary hack */
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 );
46 #endif
47 #ifdef SYS_DARWIN
48 int wxEntry( int argc, char *argv[] , bool enterLoop = TRUE );
49 #endif
50
51 /*****************************************************************************
52  * Local prototypes.
53  *****************************************************************************/
54 static int  Open         ( vlc_object_t * );
55 static void Close        ( vlc_object_t * );
56 static int  OpenDialogs  ( vlc_object_t * );
57
58 static void Run          ( intf_thread_t * );
59 static void Init         ( intf_thread_t * );
60
61 static void ShowDialog   ( intf_thread_t *, int, int, intf_dialog_args_t * );
62
63 /*****************************************************************************
64  * Local classes declarations.
65  *****************************************************************************/
66 class Instance: public wxApp
67 {
68 public:
69     Instance();
70     Instance( intf_thread_t *_p_intf );
71
72     bool OnInit();
73     int  OnExit();
74
75 private:
76     intf_thread_t *p_intf;
77     wxLocale locale;                                /* locale we'll be using */
78 };
79
80 /*****************************************************************************
81  * Module descriptor
82  *****************************************************************************/
83 #define EMBED_TEXT N_("Embed video in interface")
84 #define EMBED_LONGTEXT N_("Embed the video window inside the interface. The "\
85     "default behaviour is to have video windows separate from the interface.")
86 #define BOOKMARKS_TEXT N_("Show bookmarks dialog")
87 #define BOOKMARKS_LONGTEXT N_("Show bookmarks dialog when the interface " \
88     "starts.")
89
90 vlc_module_begin();
91 #ifdef WIN32
92     int i_score = 150;
93 #else
94     int i_score = getenv( "DISPLAY" ) == NULL ? 15 : 150;
95 #endif
96     set_description( (char *) _("wxWindows interface module") );
97     set_capability( "interface", i_score );
98     set_callbacks( Open, Close );
99     add_shortcut( "wxwindows" );
100     add_shortcut( "wxwin" );
101     add_shortcut( "wx" );
102     set_program( "wxvlc" );
103
104     add_bool( "wxwin-embed", 0, NULL,
105               EMBED_TEXT, EMBED_LONGTEXT, VLC_FALSE );
106     add_bool( "wxwin-bookmarks", 0, NULL,
107               BOOKMARKS_TEXT, BOOKMARKS_LONGTEXT, VLC_FALSE );
108
109     add_submodule();
110     set_description( _("wxWindows dialogs provider") );
111     set_capability( "dialogs provider", 50 );
112     set_callbacks( OpenDialogs, Close );
113
114 #if !defined(WIN32)
115     linked_with_a_crap_library_which_uses_atexit();
116 #endif
117 vlc_module_end();
118
119 /*****************************************************************************
120  * Open: initialize and create window
121  *****************************************************************************/
122 static int Open( vlc_object_t *p_this )
123 {
124     intf_thread_t *p_intf = (intf_thread_t *)p_this;
125
126     /* Allocate instance and initialize some members */
127     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
128     if( p_intf->p_sys == NULL )
129     {
130         msg_Err( p_intf, "out of memory" );
131         return VLC_ENOMEM;
132     }
133
134     p_intf->pf_run = Run;
135
136     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
137
138     /* Initialize wxWindows thread */
139     p_intf->p_sys->b_playing = 0;
140
141     p_intf->p_sys->p_input = NULL;
142     p_intf->p_sys->i_playing = -1;
143     p_intf->p_sys->b_slider_free = 1;
144     p_intf->p_sys->i_slider_pos = p_intf->p_sys->i_slider_oldpos = 0;
145
146     p_intf->p_sys->p_popup_menu = NULL;
147     p_intf->p_sys->p_video_window = NULL;
148
149     p_intf->pf_show_dialog = NULL;
150
151     /* We support play on start */
152     p_intf->b_play = VLC_TRUE;
153
154     return VLC_SUCCESS;
155 }
156
157 static int OpenDialogs( vlc_object_t *p_this )
158 {
159     intf_thread_t *p_intf = (intf_thread_t *)p_this;
160     int i_ret = Open( p_this );
161
162     p_intf->pf_show_dialog = ShowDialog;
163
164     return i_ret;
165 }
166
167 /*****************************************************************************
168  * Close: destroy interface window
169  *****************************************************************************/
170 static void Close( vlc_object_t *p_this )
171 {
172     intf_thread_t *p_intf = (intf_thread_t *)p_this;
173
174     if( p_intf->p_sys->p_input )
175     {
176         vlc_object_release( p_intf->p_sys->p_input );
177     }
178
179     if( p_intf->pf_show_dialog )
180     {
181         /* We must destroy the dialogs thread */
182         wxCommandEvent event( wxEVT_DIALOG, INTF_DIALOG_EXIT );
183         p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
184         vlc_thread_join( p_intf );
185     }
186
187     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
188
189     /* Destroy structure */
190     free( p_intf->p_sys );
191 }
192
193 /*****************************************************************************
194  * Run: wxWindows thread
195  *****************************************************************************/
196 #if !defined(__BUILTIN__) && defined( WIN32 )
197 HINSTANCE hInstance = 0;
198 extern "C" BOOL WINAPI
199 DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
200 {
201     hInstance = (HINSTANCE)hModule;
202     return TRUE;
203 }
204 #endif
205
206 static void Run( intf_thread_t *p_intf )
207 {
208     if( p_intf->pf_show_dialog )
209     {
210         /* The module is used in dialog provider mode */
211
212         /* Create a new thread for wxWindows */
213         if( vlc_thread_create( p_intf, "Skins Dialogs Thread",
214                                Init, 0, VLC_TRUE ) )
215         {
216             msg_Err( p_intf, "cannot create Skins Dialogs Thread" );
217             p_intf->pf_show_dialog = NULL;
218         }
219     }
220     else
221     {
222         /* The module is used in interface mode */
223         Init( p_intf );
224     }
225 }
226
227 static void Init( intf_thread_t *p_intf )
228 {
229 #if !defined( WIN32 )
230     static char  *p_args[] = { "" };
231     int i_args = 1;
232 #endif
233
234     /* Hack to pass the p_intf pointer to the new wxWindow Instance object */
235 #ifdef wxTheApp
236     wxApp::SetInstance( new Instance( p_intf ) );
237 #else
238     wxTheApp = new Instance( p_intf );
239 #endif
240
241 #if defined( WIN32 )
242 #if !defined(__BUILTIN__)
243     wxEntry( hInstance/*GetModuleHandle(NULL)*/, NULL, NULL, SW_SHOW );
244 #else
245     wxEntry( GetModuleHandle(NULL), NULL, NULL, SW_SHOW );
246 #endif
247 #else
248     wxEntry( i_args, p_args );
249 #endif
250 }
251
252 /* following functions are local */
253
254 /*****************************************************************************
255  * Constructors.
256  *****************************************************************************/
257 Instance::Instance( )
258 {
259 }
260
261 Instance::Instance( intf_thread_t *_p_intf )
262 {
263     /* Initialization */
264     p_intf = _p_intf;
265 }
266
267 IMPLEMENT_APP_NO_MAIN(Instance)
268
269 /*****************************************************************************
270  * Instance::OnInit: the parent interface execution starts here
271  *****************************************************************************
272  * This is the "main program" equivalent, the program execution will
273  * start here.
274  *****************************************************************************/
275 bool Instance::OnInit()
276 {
277     /* Initialization of i18n stuff.
278      * Usefull for things we don't have any control over, like wxWindows
279      * provided facilities (eg. open file dialog) */
280     locale.Init( wxLANGUAGE_DEFAULT );
281
282     /* FIXME: The stream output mrl parsing uses ',' already so we want to
283      * keep the default '.' for floating point numbers. */
284     setlocale( LC_NUMERIC, "C" );
285
286     /* Make an instance of your derived frame. Passing NULL (the default value
287      * of Frame's constructor is NULL) as the frame doesn't have a parent
288      * since it is the first window */
289
290     if( !p_intf->pf_show_dialog )
291     {
292         /* The module is used in interface mode */
293         Interface *MainInterface = new Interface( p_intf );
294         p_intf->p_sys->p_wxwindow = MainInterface;
295
296         /* Show the interface */
297         MainInterface->Show( TRUE );
298         SetTopWindow( MainInterface );
299         MainInterface->Raise();
300     }
301
302     /* Creates the dialogs provider */
303     p_intf->p_sys->p_wxwindow =
304         CreateDialogsProvider( p_intf, p_intf->pf_show_dialog ?
305                                NULL : p_intf->p_sys->p_wxwindow );
306
307     p_intf->p_sys->pf_show_dialog = ShowDialog;
308
309     /* OK, initialization is over */
310     vlc_thread_ready( p_intf );
311
312     /* Check if we need to start playing */
313     if( p_intf->b_play )
314     {
315         playlist_t *p_playlist =
316             (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
317                                            FIND_ANYWHERE );
318         if( p_playlist )
319         {
320             playlist_Play( p_playlist );
321             vlc_object_release( p_playlist );
322         }
323     }
324
325     /* Return TRUE to tell program to continue (FALSE would terminate) */
326     return TRUE;
327 }
328
329 /*****************************************************************************
330  * Instance::OnExit: called when the interface execution stops
331  *****************************************************************************/
332 int Instance::OnExit()
333 {
334     if( p_intf->pf_show_dialog )
335     {
336          /* We need to manually clean up the dialogs class */
337          if( p_intf->p_sys->p_wxwindow ) delete p_intf->p_sys->p_wxwindow;
338     }
339     return 0;
340 }
341
342 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
343                         intf_dialog_args_t *p_arg )
344 {
345     wxCommandEvent event( wxEVT_DIALOG, i_dialog_event );
346     event.SetInt( i_arg );
347     event.SetClientData( p_arg );
348
349 #ifdef WIN32
350     SendMessage( (HWND)p_intf->p_sys->p_wxwindow->GetHandle(),
351                  WM_CANCELMODE, 0, 0 );
352 #endif
353     if( i_dialog_event == INTF_DIALOG_POPUPMENU && i_arg == 0 ) return;
354
355     /* Hack to prevent popup events to be enqueued when
356      * one is already active */
357     if( i_dialog_event != INTF_DIALOG_POPUPMENU ||
358         !p_intf->p_sys->p_popup_menu )
359     {
360         p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
361     }
362 }