]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/wxwidgets.cpp
Remove stdlib.h
[vlc] / modules / gui / wxwidgets / wxwidgets.cpp
1 /*****************************************************************************
2  * wxwidgets.cpp : wxWidgets plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>                                                 /* ENOMEM */
28 #include <string.h>                                            /* strerror() */
29 #include <stdio.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc_interface.h>
33
34 #ifdef HAVE_LOCALE_H
35 #   include <locale.h>
36 #endif
37
38 #include "interface.hpp"
39
40 /* Temporary hack */
41 #if defined(WIN32) && defined(_WX_INIT_H_)
42 #if (wxMAJOR_VERSION <= 2) && (wxMINOR_VERSION <= 5) && (wxRELEASE_NUMBER < 3)
43 /* Hack to detect wxWidgets 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 #endif
48
49 /*****************************************************************************
50  * Local prototypes.
51  *****************************************************************************/
52 static int  Open         ( vlc_object_t * );
53 static void Close        ( vlc_object_t * );
54 static int  OpenDialogs  ( vlc_object_t * );
55
56 static void Run          ( intf_thread_t * );
57 static void Init         ( intf_thread_t * );
58
59 static void ShowDialog   ( intf_thread_t *, int, int, intf_dialog_args_t * );
60
61 #if (wxCHECK_VERSION(2,5,0))
62 void *wxClassInfo_sm_classTable_BUGGY = 0;
63 #endif
64
65 /*****************************************************************************
66  * Local classes declarations.
67  *****************************************************************************/
68 class Instance: public wxApp
69 {
70 public:
71     Instance();
72     Instance( intf_thread_t *_p_intf );
73
74     bool OnInit();
75     int  OnExit();
76
77 private:
78     intf_thread_t *p_intf;
79     wxLocale locale;                                /* locale we'll be using */
80 };
81
82 /*****************************************************************************
83  * Module descriptor
84  *****************************************************************************/
85 #define EMBED_TEXT N_("Embed video in interface")
86 #define EMBED_LONGTEXT N_("Embed the video inside the interface instead " \
87     "of having it in a separate window.")
88 #define BOOKMARKS_TEXT N_("Bookmarks dialog")
89 #define BOOKMARKS_LONGTEXT N_("Show bookmarks dialog at startup" )
90 #define EXTENDED_TEXT N_("Extended GUI")
91 #define EXTENDED_LONGTEXT N_("Show extended GUI (equalizer, image adjust, "  \
92               "video filters...) at startup"  )
93 #define TASKBAR_TEXT N_("Taskbar")
94 #define TASKBAR_LONGTEXT N_("Show VLC on the taskbar")
95 #define MINIMAL_TEXT N_("Minimal interface")
96 #define MINIMAL_LONGTEXT N_("Use minimal interface, with no toolbar and " \
97                 "fewer menus.")
98 #define SIZE_TO_VIDEO_TEXT N_("Size to video")
99 #define SIZE_TO_VIDEO_LONGTEXT N_("Resize VLC to match the video resolution.")
100 #define SYSTRAY_TEXT N_("Systray icon")
101 #define SYSTRAY_LONGTEXT N_("Show a systray icon for VLC")
102 #define LABEL_TEXT N_("Show labels in toolbar")
103 #define LABEL_LONGTEXT N_("Show labels below the icons in the toolbar.")
104
105 #define PLAYLIST_TEXT N_("Playlist view" )
106 #define PLAYLIST_LONGTEXT N_("There are two possible playlist views in the " \
107                 "interface : the normal playlist (separate window), or an " \
108                 "embedded playlist (within the main interface, but with " \
109                 "less features). You can select which one will be available " \
110                 "on the toolbar (or both)." )
111
112 static int pi_playlist_views[] = { 0,1,2 };
113 static const char *psz_playlist_views[] = { N_("Normal" ), N_("Embedded" ) ,
114                                             N_("Both") };
115
116 vlc_module_begin();
117     int i_score = 150;
118     set_shortname( (char*) "wxWidgets" );
119     set_description( (char *) _("wxWidgets interface module") );
120     set_category( CAT_INTERFACE );
121     set_subcategory( SUBCAT_INTERFACE_MAIN );
122     set_capability( "interface", i_score );
123     set_callbacks( Open, Close );
124     add_shortcut( "wxwindows" );
125     add_shortcut( "wxwin" );
126     add_shortcut( "wx" );
127     add_shortcut( "wxwidgets" );
128     set_program( "wxvlc" );
129
130     add_bool( "wx-embed", 1, NULL,
131               EMBED_TEXT, EMBED_LONGTEXT, VLC_FALSE );
132         add_deprecated_alias( "wxwin-enbed" ); /*Deprecated since 0.8.4*/
133     add_bool( "wx-bookmarks", 0, NULL,
134               BOOKMARKS_TEXT, BOOKMARKS_LONGTEXT, VLC_FALSE );
135         add_deprecated_alias( "wxwin-bookmarks" ); /*Deprecated since 0.8.4*/
136     add_bool( "wx-taskbar", 1, NULL,
137               TASKBAR_TEXT, TASKBAR_LONGTEXT, VLC_FALSE );
138         add_deprecated_alias( "wxwin-taskbar" ); /*Deprecated since 0.8.4*/
139     add_bool( "wx-extended", 0, NULL,
140               EXTENDED_TEXT, EXTENDED_LONGTEXT, VLC_FALSE );
141     add_bool( "wx-minimal", 0, NULL,
142               MINIMAL_TEXT, MINIMAL_LONGTEXT, VLC_TRUE );
143         add_deprecated_alias( "wxwin-minimal" ); /*Deprecated since 0.8.4*/
144     add_bool( "wx-autosize", 1, NULL,
145               SIZE_TO_VIDEO_TEXT, SIZE_TO_VIDEO_LONGTEXT, VLC_TRUE );
146         add_deprecated_alias( "wxwin-autosize" ); /*Deprecated since 0.8.4*/
147     add_integer( "wx-playlist-view", 0, NULL, PLAYLIST_TEXT, PLAYLIST_LONGTEXT,
148              VLC_FALSE );
149         change_integer_list( pi_playlist_views, psz_playlist_views, 0 );
150 /* wxCocoa pretends to support this, but at least 2.6.x doesn't */
151 #ifndef __APPLE__
152 #ifdef wxHAS_TASK_BAR_ICON
153     add_bool( "wx-systray", 0, NULL,
154               SYSTRAY_TEXT, SYSTRAY_LONGTEXT, VLC_FALSE );
155         add_deprecated_alias( "wxwin-systray" ); /*Deprecated since 0.8.4*/
156 #endif
157 #endif
158     add_bool( "wx-labels", 0, NULL, LABEL_TEXT, LABEL_LONGTEXT, VLC_TRUE);
159     add_string( "wx-config-last", NULL, NULL,
160                 N_("last config"), N_("last config"), VLC_TRUE );
161         change_autosave();
162         change_internal();
163         add_deprecated_alias( "wxwin-config-last" ); /*Deprecated since 0.8.4*/
164
165     add_submodule();
166     set_description( _("wxWidgets dialogs provider") );
167     set_capability( "dialogs provider", 50 );
168     set_callbacks( OpenDialogs, Close );
169
170 #if !defined(WIN32)
171     linked_with_a_crap_library_which_uses_atexit();
172 #endif
173 vlc_module_end();
174
175 /*****************************************************************************
176  * Open: initialize and create window
177  *****************************************************************************/
178 static int Open( vlc_object_t *p_this )
179 {
180     intf_thread_t *p_intf = (intf_thread_t *)p_this;
181     /* Test in we have an X*/
182 #if defined HAVE_GETENV && (defined __WXGTK__ || defined __WXX11) 
183     if( !getenv( "DISPLAY" ) )
184     {
185         msg_Err( p_intf, "no X server");
186         return VLC_EGENERIC;
187     }
188 #endif
189     /* Allocate instance and initialize some members */
190     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
191     if( p_intf->p_sys == NULL )
192     {
193         msg_Err( p_intf, "out of memory" );
194         return VLC_ENOMEM;
195     }
196     memset( p_intf->p_sys, 0, sizeof( intf_sys_t ) );
197
198     p_intf->pf_run = Run;
199
200     p_intf->p_sys->p_sub = msg_Subscribe( p_intf, MSG_QUEUE_NORMAL );
201
202     /* Initialize wxWidgets thread */
203     p_intf->p_sys->b_playing = 0;
204
205     p_intf->p_sys->p_input = NULL;
206     p_intf->p_sys->i_playing = -1;
207
208     p_intf->p_sys->p_popup_menu = NULL;
209     p_intf->p_sys->p_video_window = NULL;
210
211     p_intf->pf_show_dialog = NULL;
212
213     /* We support play on start */
214     p_intf->b_play = VLC_TRUE;
215
216     p_intf->p_sys->b_video_autosize =
217         config_GetInt( p_intf, "wx-autosize" );
218
219     return VLC_SUCCESS;
220 }
221
222 static int OpenDialogs( vlc_object_t *p_this )
223 {
224     intf_thread_t *p_intf = (intf_thread_t *)p_this;
225     int i_ret = Open( p_this );
226
227     p_intf->pf_show_dialog = ShowDialog;
228
229     return i_ret;
230 }
231
232 /*****************************************************************************
233  * Close: destroy interface window
234  *****************************************************************************/
235 static void Close( vlc_object_t *p_this )
236 {
237     intf_thread_t *p_intf = (intf_thread_t *)p_this;
238
239     vlc_mutex_lock( &p_intf->object_lock );
240     p_intf->b_dead = VLC_TRUE;
241     vlc_mutex_unlock( &p_intf->object_lock );
242
243     if( p_intf->pf_show_dialog )
244     {
245         /* We must destroy the dialogs thread */
246         wxCommandEvent event( wxEVT_DIALOG, INTF_DIALOG_EXIT );
247         p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
248         vlc_thread_join( p_intf );
249     }
250
251     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
252
253     /* */
254     delete p_intf->p_sys->p_window_settings;
255
256 #if (wxCHECK_VERSION(2,5,0))
257     wxClassInfo::sm_classTable = (wxHashTable*)wxClassInfo_sm_classTable_BUGGY;
258 #endif
259
260     /* Destroy structure */
261     free( p_intf->p_sys );
262 }
263
264 /*****************************************************************************
265  * Run: wxWidgets thread
266  *****************************************************************************/
267
268 //when is this called?
269 #if !defined(__BUILTIN__) && defined( WIN32 )
270 HINSTANCE hInstance = 0;
271 extern "C" BOOL WINAPI
272 DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
273 {
274     hInstance = (HINSTANCE)hModule;
275     return TRUE;
276 }
277 #endif
278
279 static void Run( intf_thread_t *p_intf )
280 {
281     if( p_intf->pf_show_dialog )
282     {
283         /* The module is used in dialog provider mode */
284
285         /* Create a new thread for wxWidgets */
286         if( vlc_thread_create( p_intf, "Skins Dialogs Thread",
287                                Init, 0, VLC_TRUE ) )
288         {
289             msg_Err( p_intf, "cannot create Skins Dialogs Thread" );
290             p_intf->pf_show_dialog = NULL;
291         }
292     }
293     else
294     {
295         /* The module is used in interface mode */
296         Init( p_intf );
297     }
298 }
299
300 static void Init( intf_thread_t *p_intf )
301 {
302 #if !defined( WIN32 )
303     static char  *p_args[] = { "" };
304     int i_args = 1;
305 #endif
306
307     /* Hack to pass the p_intf pointer to the new wxWidgets Instance object */
308 #ifdef wxTheApp
309     wxApp::SetInstance( new Instance( p_intf ) );
310 #else
311     wxTheApp = new Instance( p_intf );
312 #endif
313
314 #if defined( WIN32 )
315 #if !defined(__BUILTIN__)
316
317     //because no one knows when DllMain is called
318     if (hInstance == NULL)
319       hInstance = GetModuleHandle(NULL);
320
321     wxEntry( hInstance/*GetModuleHandle(NULL)*/, NULL, NULL, SW_SHOW );
322 #else
323     wxEntry( GetModuleHandle(NULL), NULL, NULL, SW_SHOW );
324 #endif
325 #else
326     wxEntry( i_args, p_args );
327 #endif
328 }
329
330 /* following functions are local */
331
332 /*****************************************************************************
333  * Constructors.
334  *****************************************************************************/
335 Instance::Instance( )
336 {
337 }
338
339 Instance::Instance( intf_thread_t *_p_intf )
340 {
341     /* Initialization */
342     p_intf = _p_intf;
343 }
344
345 IMPLEMENT_APP_NO_MAIN(Instance)
346
347 /*****************************************************************************
348  * Instance::OnInit: the parent interface execution starts here
349  *****************************************************************************
350  * This is the "main program" equivalent, the program execution will
351  * start here.
352  *****************************************************************************/
353 bool Instance::OnInit()
354 {
355     /* Initialization of i18n stuff.
356      * Usefull for things we don't have any control over, like wxWidgets
357      * provided facilities (eg. open file dialog) */
358     locale.Init( wxLANGUAGE_DEFAULT, wxLOCALE_LOAD_DEFAULT );
359
360     /* Load saved window settings */
361     p_intf->p_sys->p_window_settings = new WindowSettings( p_intf );
362
363     /* Make an instance of your derived frame. Passing NULL (the default value
364      * of Frame's constructor is NULL) as the frame doesn't have a parent
365      * since it is the first window */
366
367     if( !p_intf->pf_show_dialog )
368     {
369         /* The module is used in interface mode */
370         long style = wxDEFAULT_FRAME_STYLE;
371         if ( ! config_GetInt( p_intf, "wx-taskbar" ) )
372         {
373             style = wxDEFAULT_FRAME_STYLE|wxFRAME_NO_TASKBAR;
374         }
375
376         Interface *MainInterface = new Interface( p_intf, style );
377         p_intf->p_sys->p_wxwindow = MainInterface;
378
379         /* Show the interface */
380         MainInterface->Show( TRUE );
381         SetTopWindow( MainInterface );
382         MainInterface->Raise();
383     }
384
385     /* Creates the dialogs provider */
386     p_intf->p_sys->p_wxwindow =
387         CreateDialogsProvider( p_intf, p_intf->pf_show_dialog ?
388                                NULL : p_intf->p_sys->p_wxwindow );
389
390     p_intf->p_sys->pf_show_dialog = ShowDialog;
391
392     /* OK, initialization is over */
393     vlc_thread_ready( p_intf );
394
395     /* Check if we need to start playing */
396     if( !p_intf->pf_show_dialog && p_intf->b_play )
397     {
398         playlist_t *p_playlist =
399             (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
400                                            FIND_ANYWHERE );
401         if( p_playlist )
402         {
403             playlist_Control( p_playlist, PLAYLIST_AUTOPLAY, VLC_FALSE );
404             vlc_object_release( p_playlist );
405         }
406     }
407
408     /* Return TRUE to tell program to continue (FALSE would terminate) */
409     return TRUE;
410 }
411
412 /*****************************************************************************
413  * Instance::OnExit: called when the interface execution stops
414  *****************************************************************************/
415 int Instance::OnExit()
416 {
417     if( p_intf->pf_show_dialog )
418     {
419          /* We need to manually clean up the dialogs class */
420          if( p_intf->p_sys->p_wxwindow ) delete p_intf->p_sys->p_wxwindow;
421     }
422
423 #if (wxCHECK_VERSION(2,5,0))
424     wxClassInfo_sm_classTable_BUGGY = wxClassInfo::sm_classTable;
425     wxClassInfo::sm_classTable = 0;
426 #endif
427
428     return 0;
429 }
430
431 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
432                         intf_dialog_args_t *p_arg )
433 {
434     wxCommandEvent event( wxEVT_DIALOG, i_dialog_event );
435     event.SetInt( i_arg );
436     event.SetClientData( p_arg );
437
438 #ifdef WIN32
439     SendMessage( (HWND)p_intf->p_sys->p_wxwindow->GetHandle(),
440                  WM_CANCELMODE, 0, 0 );
441 #endif
442     if( i_dialog_event == INTF_DIALOG_POPUPMENU && i_arg == 0 ) return;
443
444     /* Hack to prevent popup events to be enqueued when
445      * one is already active */
446     if( i_dialog_event != INTF_DIALOG_POPUPMENU ||
447         !p_intf->p_sys->p_popup_menu )
448     {
449         p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
450     }
451 }
452
453 /*****************************************************************************
454  * WindowSettings utility class
455  *****************************************************************************/
456 WindowSettings::WindowSettings( intf_thread_t *_p_intf )
457 {
458     char *psz_org = NULL;
459     char *psz;
460     int i;
461
462     /* */
463     p_intf = _p_intf;
464
465     /* */
466     for( i = 0; i < ID_MAX; i++ )
467     {
468         b_valid[i] = false;
469         b_shown[i] = false;
470         position[i] = wxDefaultPosition;
471         size[i] = wxDefaultSize;
472     }
473     b_shown[ID_MAIN] = true;
474
475     if( p_intf->pf_show_dialog ) return;
476
477     /* Parse the configuration */
478     psz_org = psz = config_GetPsz( p_intf, "wx-config-last" );
479     if( !psz || *psz == '\0' ) return;
480
481     msg_Dbg( p_intf, "Using last windows config '%s'", psz );
482
483     i_screen_w = 0;
484     i_screen_h = 0;
485     while( psz && *psz )
486     {
487         int id, v[4];
488
489         psz = strchr( psz, '(' );
490
491         if( !psz )
492             break;
493         psz++;
494
495         id = strtol( psz, &psz, 0 );
496         if( *psz != ',' ) /* broken cfg */
497         {
498             goto invalid;
499         }
500         psz++;
501
502         for( i = 0; i < 4; i++ )
503         {
504             v[i] = strtol( psz, &psz, 0 );
505
506             if( i < 3 )
507             {
508                 if( *psz != ',' )
509                 {
510                     goto invalid;
511                 }
512                 psz++;
513             }
514             else
515             {
516                 if( *psz != ')' )
517                 {
518                     goto invalid;
519                 }
520             }
521         }
522         if( id == ID_SCREEN )
523         {
524             i_screen_w = v[2];
525             i_screen_h = v[3];
526         }
527         else if( id >= 0 && id < ID_MAX )
528         {
529             b_valid[id] = true;
530             b_shown[id] = true;
531             position[id] = wxPoint( v[0], v[1] );
532             size[id] = wxSize( v[2], v[3] );
533
534             msg_Dbg( p_intf, "id=%d p=(%d,%d) s=(%d,%d)",
535                      id, position[id].x, position[id].y,
536                          size[id].x, size[id].y );
537         }
538
539         psz = strchr( psz, ')' );
540         if( psz ) psz++;
541     }
542
543     if( i_screen_w <= 0 || i_screen_h <= 0 )
544     {
545         goto invalid;
546     }
547
548     for( i = 0; i < ID_MAX; i++ )
549     {
550         if( !b_valid[i] )
551             continue;
552         if( position[i].x < 0 || position[i].y < 0 )
553         {
554             goto invalid;
555         }
556         if( i != ID_SMALL_PLAYLIST && (size[i].x <= 0 || size[i].y <= 0)  )
557         {
558             goto invalid;
559         }
560     }
561
562     if( psz_org ) free( psz_org );
563     return;
564
565 invalid:
566     msg_Dbg( p_intf, "last windows config is invalid (ignored)" );
567     for( i = 0; i < ID_MAX; i++ )
568     {
569         b_valid[i] = false;
570         b_shown[i] = false;
571         position[i] = wxDefaultPosition;
572         size[i] = wxDefaultSize;
573     }
574     if( psz_org ) free( psz_org );
575 }
576
577
578 WindowSettings::~WindowSettings( )
579 {
580     wxString sCfg;
581
582     if( p_intf->pf_show_dialog ) return;
583
584     sCfg = wxString::Format( wxT("(%d,0,0,%d,%d)"), ID_SCREEN,
585                              wxSystemSettings::GetMetric( wxSYS_SCREEN_X ),
586                              wxSystemSettings::GetMetric( wxSYS_SCREEN_Y ) );
587     for( int i = 0; i < ID_MAX; i++ )
588     {
589         if( !b_valid[i] || !b_shown[i] )
590             continue;
591
592         sCfg += wxString::Format( wxT("(%d,%d,%d,%d,%d)"),
593                                   i, position[i].x, position[i].y,
594                                      size[i].x, size[i].y );
595     }
596
597     config_PutPsz( p_intf, "wx-config-last", sCfg.mb_str(wxConvUTF8) );
598 }
599
600 void WindowSettings::SetScreen( int i_screen_w, int i_screen_h )
601 {
602     int i;
603
604     for( i = 0; i < ID_MAX; i++ )
605     {
606         if( !b_valid[i] )
607             continue;
608         if( position[i].x >= i_screen_w || position[i].y >= i_screen_h )
609             goto invalid;
610     }
611     return;
612
613 invalid:
614     for( i = 0; i < ID_MAX; i++ )
615     {
616         b_valid[i] = false;
617         b_shown[i] = false;
618         position[i] = wxDefaultPosition;
619         size[i] = wxDefaultSize;
620     }
621 }
622
623 void WindowSettings::SetSettings( int id, bool _b_shown, wxPoint p, wxSize s )
624 {
625     if( id < 0 || id >= ID_MAX )
626         return;
627
628     b_valid[id] = true;
629     b_shown[id] = _b_shown;
630
631     position[id] = p;
632     size[id] = s;
633 }
634
635 bool WindowSettings::GetSettings( int id, bool& _b_shown, wxPoint& p, wxSize& s)
636 {
637     if( id < 0 || id >= ID_MAX )
638         return false;
639
640     if( !b_valid[id] )
641         return false;
642
643     _b_shown = b_shown[id];
644     p = position[id];
645     s = size[id];
646
647     return true;
648 }