]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/wxwindows.cpp
e6af11efd857ac251d9c62ffa5e09a921c70024f
[vlc] / modules / gui / wxwindows / wxwindows.cpp
1 /*****************************************************************************
2  * wxwindows.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2005 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 #define TASKBAR_TEXT N_("Show taskbar entry")
93 #define TASKBAR_LONGTEXT N_("Show taskbar entry")
94 #define MINIMAL_TEXT N_("Minimal interface")
95 #define MINIMAL_LONGTEXT N_("Use minimal interface, no toolbar, few menus")
96 #define SIZE_TO_VIDEO_TEXT N_("Size to video")
97 #define SIZE_TO_VIDEO_LONGTEXT N_("Resize VLC to match the video resolution")
98 #define SYSTRAY_TEXT N_("Show systray icon")
99 #define SYSTRAY_LONGTEXT N_("Show systray icon")
100
101 vlc_module_begin();
102 #ifdef WIN32
103     int i_score = 150;
104 #else
105     int i_score = getenv( "DISPLAY" ) == NULL ? 15 : 150;
106 #endif
107     set_shortname( (char*) _("wxWindows"));
108     set_description( (char *) _("wxWindows interface module") );
109     set_category( CAT_INTERFACE );
110     set_subcategory( SUBCAT_INTERFACE_GENERAL );
111     set_capability( "interface", i_score );
112     set_callbacks( Open, Close );
113     add_shortcut( "wxwindows" );
114     add_shortcut( "wxwin" );
115     add_shortcut( "wx" );
116     set_program( "wxvlc" );
117
118     add_bool( "wxwin-embed", 1, NULL,
119               EMBED_TEXT, EMBED_LONGTEXT, VLC_FALSE );
120     add_bool( "wxwin-bookmarks", 0, NULL,
121               BOOKMARKS_TEXT, BOOKMARKS_LONGTEXT, VLC_FALSE );
122     add_bool( "wxwin-taskbar", 1, NULL,
123               TASKBAR_TEXT, TASKBAR_LONGTEXT, VLC_FALSE );
124     add_bool( "wxwin-minimal", 0, NULL,
125               MINIMAL_TEXT, MINIMAL_LONGTEXT, VLC_TRUE );
126     add_bool( "wxwin-size-to-video", 1, NULL,
127               SIZE_TO_VIDEO_TEXT, SIZE_TO_VIDEO_LONGTEXT, VLC_TRUE );
128 #ifdef wxHAS_TASK_BAR_ICON
129     add_bool( "wxwin-systray", 0, NULL,
130               SYSTRAY_TEXT, SYSTRAY_LONGTEXT, VLC_FALSE );
131 #endif
132     add_string( "wxwin-config-last", NULL, NULL,
133                 "last config", "last config", VLC_TRUE );
134
135     add_submodule();
136     set_description( _("wxWindows dialogs provider") );
137     set_capability( "dialogs provider", 50 );
138     set_callbacks( OpenDialogs, Close );
139
140 #if !defined(WIN32)
141     linked_with_a_crap_library_which_uses_atexit();
142 #endif
143 vlc_module_end();
144
145 /*****************************************************************************
146  * Open: initialize and create window
147  *****************************************************************************/
148 static int Open( vlc_object_t *p_this )
149 {
150     intf_thread_t *p_intf = (intf_thread_t *)p_this;
151
152     /* Allocate instance and initialize some members */
153     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
154     if( p_intf->p_sys == NULL )
155     {
156         msg_Err( p_intf, "out of memory" );
157         return VLC_ENOMEM;
158     }
159     memset( p_intf->p_sys, 0, sizeof( intf_sys_t ) );
160
161     p_intf->pf_run = Run;
162
163     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
164
165     /* Initialize wxWindows thread */
166     p_intf->p_sys->b_playing = 0;
167
168     p_intf->p_sys->p_input = NULL;
169     p_intf->p_sys->i_playing = -1;
170     p_intf->p_sys->b_slider_free = 1;
171     p_intf->p_sys->i_slider_pos = p_intf->p_sys->i_slider_oldpos = 0;
172
173     p_intf->p_sys->p_popup_menu = NULL;
174     p_intf->p_sys->p_video_window = NULL;
175
176     p_intf->pf_show_dialog = NULL;
177
178     /* We support play on start */
179     p_intf->b_play = VLC_TRUE;
180
181     /* */
182     p_intf->p_sys->p_window_settings = new WindowSettings( p_intf );
183
184     return VLC_SUCCESS;
185 }
186
187 static int OpenDialogs( vlc_object_t *p_this )
188 {
189     intf_thread_t *p_intf = (intf_thread_t *)p_this;
190     int i_ret = Open( p_this );
191
192     p_intf->pf_show_dialog = ShowDialog;
193
194     return i_ret;
195 }
196
197 /*****************************************************************************
198  * Close: destroy interface window
199  *****************************************************************************/
200 static void Close( vlc_object_t *p_this )
201 {
202     intf_thread_t *p_intf = (intf_thread_t *)p_this;
203
204     vlc_mutex_lock( &p_intf->object_lock );
205     p_intf->b_dead = VLC_TRUE;
206     vlc_mutex_unlock( &p_intf->object_lock );
207
208     if( p_intf->pf_show_dialog )
209     {
210         /* We must destroy the dialogs thread */
211         wxCommandEvent event( wxEVT_DIALOG, INTF_DIALOG_EXIT );
212         p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
213         vlc_thread_join( p_intf );
214     }
215
216     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
217
218     /* */
219     delete p_intf->p_sys->p_window_settings;
220
221     /* Destroy structure */
222     free( p_intf->p_sys );
223 }
224
225 /*****************************************************************************
226  * Run: wxWindows thread
227  *****************************************************************************/
228
229 //when is this called?
230 #if !defined(__BUILTIN__) && defined( WIN32 )
231 HINSTANCE hInstance = 0;
232 extern "C" BOOL WINAPI
233 DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
234 {
235     hInstance = (HINSTANCE)hModule;
236     return TRUE;
237 }
238 #endif
239
240 static void Run( intf_thread_t *p_intf )
241 {
242     if( p_intf->pf_show_dialog )
243     {
244         /* The module is used in dialog provider mode */
245
246         /* Create a new thread for wxWindows */
247         if( vlc_thread_create( p_intf, "Skins Dialogs Thread",
248                                Init, 0, VLC_TRUE ) )
249         {
250             msg_Err( p_intf, "cannot create Skins Dialogs Thread" );
251             p_intf->pf_show_dialog = NULL;
252         }
253     }
254     else
255     {
256         /* The module is used in interface mode */
257         Init( p_intf );
258     }
259 }
260
261 static void Init( intf_thread_t *p_intf )
262 {
263 #if !defined( WIN32 )
264     static char  *p_args[] = { "" };
265     int i_args = 1;
266 #endif
267
268     /* Hack to pass the p_intf pointer to the new wxWindow Instance object */
269 #ifdef wxTheApp
270     wxApp::SetInstance( new Instance( p_intf ) );
271 #else
272     wxTheApp = new Instance( p_intf );
273 #endif
274
275 #if defined( WIN32 )
276 #if !defined(__BUILTIN__)
277
278     //because no one knows when DllMain is called
279     if (hInstance == NULL)
280       hInstance = GetModuleHandle(NULL);
281
282     wxEntry( hInstance/*GetModuleHandle(NULL)*/, NULL, NULL, SW_SHOW );
283 #else
284     wxEntry( GetModuleHandle(NULL), NULL, NULL, SW_SHOW );
285 #endif
286 #else
287     wxEntry( i_args, p_args );
288 #endif
289 }
290
291 /* following functions are local */
292
293 /*****************************************************************************
294  * Constructors.
295  *****************************************************************************/
296 Instance::Instance( )
297 {
298 }
299
300 Instance::Instance( intf_thread_t *_p_intf )
301 {
302     /* Initialization */
303     p_intf = _p_intf;
304 }
305
306 IMPLEMENT_APP_NO_MAIN(Instance)
307
308 /*****************************************************************************
309  * Instance::OnInit: the parent interface execution starts here
310  *****************************************************************************
311  * This is the "main program" equivalent, the program execution will
312  * start here.
313  *****************************************************************************/
314 bool Instance::OnInit()
315 {
316     /* Initialization of i18n stuff.
317      * Usefull for things we don't have any control over, like wxWindows
318      * provided facilities (eg. open file dialog) */
319     locale.Init( wxLANGUAGE_DEFAULT );
320
321     /* FIXME: The stream output mrl parsing uses ',' already so we want to
322      * keep the default '.' for floating point numbers. */
323     setlocale( LC_NUMERIC, "C" );
324
325     /* Make an instance of your derived frame. Passing NULL (the default value
326      * of Frame's constructor is NULL) as the frame doesn't have a parent
327      * since it is the first window */
328
329     if( !p_intf->pf_show_dialog )
330     {
331         /* The module is used in interface mode */
332         long style = wxDEFAULT_FRAME_STYLE;
333         if ( ! config_GetInt( p_intf, "wxwin-taskbar" ) )
334         {
335             style = wxDEFAULT_FRAME_STYLE|wxFRAME_NO_TASKBAR;
336         }
337
338         Interface *MainInterface = new Interface( p_intf, style );
339         p_intf->p_sys->p_wxwindow = MainInterface;
340
341         /* Show the interface */
342         MainInterface->Show( TRUE );
343         SetTopWindow( MainInterface );
344         MainInterface->Raise();
345     }
346
347     /* Creates the dialogs provider */
348     p_intf->p_sys->p_wxwindow =
349         CreateDialogsProvider( p_intf, p_intf->pf_show_dialog ?
350                                NULL : p_intf->p_sys->p_wxwindow );
351
352     p_intf->p_sys->pf_show_dialog = ShowDialog;
353
354     /* OK, initialization is over */
355     vlc_thread_ready( p_intf );
356
357     /* Check if we need to start playing */
358     if( !p_intf->pf_show_dialog && p_intf->b_play )
359     {
360         playlist_t *p_playlist =
361             (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
362                                            FIND_ANYWHERE );
363         if( p_playlist )
364         {
365             playlist_Play( p_playlist );
366             vlc_object_release( p_playlist );
367         }
368     }
369
370     /* Return TRUE to tell program to continue (FALSE would terminate) */
371     return TRUE;
372 }
373
374 /*****************************************************************************
375  * Instance::OnExit: called when the interface execution stops
376  *****************************************************************************/
377 int Instance::OnExit()
378 {
379     if( p_intf->pf_show_dialog )
380     {
381          /* We need to manually clean up the dialogs class */
382          if( p_intf->p_sys->p_wxwindow ) delete p_intf->p_sys->p_wxwindow;
383     }
384     return 0;
385 }
386
387 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
388                         intf_dialog_args_t *p_arg )
389 {
390     wxCommandEvent event( wxEVT_DIALOG, i_dialog_event );
391     event.SetInt( i_arg );
392     event.SetClientData( p_arg );
393
394 #ifdef WIN32
395     SendMessage( (HWND)p_intf->p_sys->p_wxwindow->GetHandle(),
396                  WM_CANCELMODE, 0, 0 );
397 #endif
398     if( i_dialog_event == INTF_DIALOG_POPUPMENU && i_arg == 0 ) return;
399
400     /* Hack to prevent popup events to be enqueued when
401      * one is already active */
402     if( i_dialog_event != INTF_DIALOG_POPUPMENU ||
403         !p_intf->p_sys->p_popup_menu )
404     {
405         p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
406     }
407 }
408
409 WindowSettings::WindowSettings( intf_thread_t *_p_intf )
410 {
411     char *psz_org = NULL;
412     char *psz;
413     int i;
414
415     /* */
416     p_intf = _p_intf;
417
418     /* */
419     for( i = 0; i < ID_MAX; i++ )
420     {
421         b_valid[i] = false;
422         b_shown[i] = false;
423         position[i] = wxDefaultPosition;
424         size[i] = wxDefaultSize;
425     }
426     b_shown[ID_MAIN] = true;
427
428     /* Parse the configuration */
429     psz_org = psz = config_GetPsz( p_intf, "wxwin-config-last" );
430     if( !psz || *psz == '\0' )
431         return;
432
433     msg_Dbg( p_intf, "Using last windows config '%s'", psz );
434
435     i_screen_w = 0;
436     i_screen_h = 0;
437     while( psz && *psz )
438     {
439         int id, v[4];
440
441         psz = strchr( psz, '(' );
442
443         if( !psz )
444             break;
445         psz++;
446
447         id = strtol( psz, &psz, 0 );
448         if( *psz != ',' ) /* broken cfg */
449             goto invalid;
450         psz++;
451
452         for( i = 0; i < 4; i++ )
453         {
454             v[i] = strtol( psz, &psz, 0 );
455
456             if( i < 3 )
457             {
458                 if( *psz != ',' )
459                     goto invalid;
460                 psz++;
461             }
462             else
463             {
464                 if( *psz != ')' )
465                     goto invalid;
466             }
467         }
468         if( id == ID_SCREEN )
469         {
470             i_screen_w = v[2];
471             i_screen_h = v[3];
472         }
473         else if( id >= 0 && id < ID_MAX )
474         {
475             b_valid[id] = true;
476             b_shown[id] = true;
477             position[id] = wxPoint( v[0], v[1] );
478             size[id] = wxSize( v[2], v[3] );
479
480             msg_Dbg( p_intf, "id=%d p=(%d,%d) s=(%d,%d)",
481                      id, position[id].x, position[id].y,
482                          size[id].x, size[id].y );
483         }
484
485         psz = strchr( psz, ')' );
486         if( psz ) psz++;
487     }
488
489     if( i_screen_w <= 0 || i_screen_h <= 0 )
490         goto invalid;
491
492     for( i = 0; i < ID_MAX; i++ )
493     {
494         if( !b_valid[i] )
495             continue;
496         if( position[i].x < 0 || position[i].y < 0 )
497             goto invalid;
498         if( size[i].x <= 0 || size[i].y <= 0 )
499             goto invalid;
500     }
501
502     if( psz_org ) free( psz_org );
503     return;
504
505 invalid:
506     msg_Dbg( p_intf, "last windows config is invalid (ignored)" );
507     for( i = 0; i < ID_MAX; i++ )
508     {
509         b_valid[i] = false;
510         b_shown[i] = false;
511         position[i] = wxDefaultPosition;
512         size[i] = wxDefaultSize;
513     }
514     if( psz_org ) free( psz_org );
515 }
516
517
518 WindowSettings::~WindowSettings( )
519 {
520     wxString sCfg;
521     int i;
522
523     sCfg = wxString::Format( wxT("(%d,0,0,%d,%d)"), ID_SCREEN,
524                              wxSystemSettings::GetMetric( wxSYS_SCREEN_X ),
525                              wxSystemSettings::GetMetric( wxSYS_SCREEN_Y ) );
526     for( i = 0; i < ID_MAX; i++ )
527     {
528         if( !b_valid[i] || !b_shown[i] )
529             continue;
530
531         sCfg += wxString::Format( wxT("(%d,%d,%d,%d,%d)"),
532                                   i, position[i].x, position[i].y,
533                                      size[i].x, size[i].y );
534     }
535
536     config_PutPsz( p_intf, "wxwin-config-last", sCfg.mb_str() );
537     config_SaveConfigFile( p_intf, "wxwindows" );
538 }
539 void WindowSettings::SetScreen( int i_screen_w, int i_screen_h )
540 {
541     int i;
542
543     for( i = 0; i < ID_MAX; i++ )
544     {
545         if( !b_valid[i] )
546             continue;
547         if( position[i].x >= i_screen_w || position[i].y >= i_screen_h )
548             goto invalid;
549     }
550     return;
551
552 invalid:
553     for( i = 0; i < ID_MAX; i++ )
554     {
555         b_valid[i] = false;
556         b_shown[i] = false;
557         position[i] = wxDefaultPosition;
558         size[i] = wxDefaultSize;
559     }
560 }
561
562 void WindowSettings::SetSettings( int id, bool _b_shown, wxPoint p, wxSize s )
563 {
564     if( id < 0 || id >= ID_MAX )
565         return;
566
567     b_valid[id] = true;
568     b_shown[id] = _b_shown;
569
570     position[id] = p;
571     size[id] = s;
572 }
573
574 bool WindowSettings::GetSettings( int id, bool& _b_shown, wxPoint& p, wxSize& s)
575 {
576     if( id < 0 || id >= ID_MAX )
577         return false;
578
579     if( !b_valid[id] )
580         return false;
581
582     _b_shown = b_shown[id];
583     p = position[id];
584     s = size[id];
585
586     return true;
587 }
588