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