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