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