]> git.sesse.net Git - vlc/blob - modules/gui/qt4/qt4.cpp
Qt4: fail cleanly if started multiple times (instead of crashing)
[vlc] / modules / gui / qt4 / qt4.cpp
1 /*****************************************************************************
2  * qt4.cpp : QT4 interface
3  ****************************************************************************
4  * Copyright © 2006-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <QApplication>
30 #include <QDate>
31
32 #include "qt4.hpp"
33
34 #include "input_manager.hpp"    /* THEMIM creation */
35 #include "dialogs_provider.hpp" /* THEDP creation */
36 #include "main_interface.hpp"   /* MainInterface creation */
37 #include "dialogs/help.hpp"     /* Launch Update */
38 #include "recents.hpp"          /* Recents Item destruction */
39 #include "util/qvlcapp.hpp"     /* QVLCApplication definition */
40
41 #ifdef Q_WS_X11
42  #include <X11/Xlib.h>
43 #endif
44
45 #include "../../../share/vlc32x32.xpm"
46 #include "../../../share/vlc32x32-christmas.xpm"
47 #include <vlc_plugin.h>
48
49 #ifdef WIN32 /* For static builds */
50  #include <QtPlugin>
51  Q_IMPORT_PLUGIN(qjpeg)
52  Q_IMPORT_PLUGIN(qtaccessiblewidgets)
53 #endif
54
55 /*****************************************************************************
56  * Local prototypes.
57  *****************************************************************************/
58 static int  OpenIntf     ( vlc_object_t * );
59 static int  OpenDialogs  ( vlc_object_t * );
60 static int  Open         ( vlc_object_t *, bool );
61 static void Close        ( vlc_object_t * );
62 static int  WindowOpen   ( vlc_object_t * );
63 static void WindowClose  ( vlc_object_t * );
64 static void *Thread      ( void * );
65 static void ShowDialog   ( intf_thread_t *, int, int, intf_dialog_args_t * );
66
67 /*****************************************************************************
68  * Module descriptor
69  *****************************************************************************/
70 #define ADVANCED_PREFS_TEXT N_( "Show advanced preferences over simple ones" )
71 #define ADVANCED_PREFS_LONGTEXT N_( "Show advanced preferences and not simple "\
72                                     "preferences when opening the preferences "\
73                                     "dialog." )
74
75 #define SYSTRAY_TEXT N_( "Systray icon" )
76 #define SYSTRAY_LONGTEXT N_( "Show an icon in the systray " \
77                              "allowing you to control VLC media player " \
78                              "for basic actions." )
79
80 #define MINIMIZED_TEXT N_( "Start VLC with only a systray icon" )
81 #define MINIMIZED_LONGTEXT N_( "VLC will start with just an icon in " \
82                                "your taskbar" )
83
84 #define KEEPSIZE_TEXT N_( "Resize interface to the native video size" )
85 #define KEEPSIZE_LONGTEXT N_( "You have two choices:\n" \
86             " - The interface will resize to the native video size\n" \
87             " - The video will fit to the interface size\n " \
88             "By default, interface resize to the native video size." )
89
90 #define TITLE_TEXT N_( "Show playing item name in window title" )
91 #define TITLE_LONGTEXT N_( "Show the name of the song or video in the " \
92                            "controler window title." )
93
94 #define NOTIFICATION_TEXT N_( "Show notification popup on track change" )
95 #define NOTIFICATION_LONGTEXT N_( \
96     "Show a notification popup with the artist and track name when " \
97     "the current playlist item changes, when VLC is minimized or hidden." )
98
99 #define ADVANCED_OPTIONS_TEXT N_( "Advanced options" )
100 #define ADVANCED_OPTIONS_LONGTEXT N_( "Show all the advanced options " \
101                                       "in the dialogs." )
102
103 #define OPACITY_TEXT N_( "Windows opacity between 0.1 and 1" )
104 #define OPACITY_LONGTEXT N_( "Sets the windows opacity between 0.1 and 1 " \
105                              "for main interface, playlist and extended panel."\
106                              " This option only works with Windows and " \
107                              "X11 with composite extensions." )
108
109 #define OPACITY_FS_TEXT N_( "Fullscreen controller opacity opacity between 0.1 and 1" )
110 #define OPACITY_FS_LONGTEXT N_( "Sets the fullscreen controller opacity between 0.1 and 1 " \
111                              "for main interface, playlist and extended panel."\
112                              " This option only works with Windows and " \
113                              "X11 with composite extensions." )
114
115
116 #define ERROR_TEXT N_( "Show unimportant error and warnings dialogs" )
117
118 #define UPDATER_TEXT N_( "Activate the updates availability notification" )
119 #define UPDATER_LONGTEXT N_( "Activate the automatic notification of new " \
120                             "versions of the software. It runs once every " \
121                             "two weeks." )
122 #define UPDATER_DAYS_TEXT N_("Number of days between two update checks")
123
124 #define COMPLETEVOL_TEXT N_( "Allow the volume to be set to 400%" )
125 #define COMPLETEVOL_LONGTEXT N_( "Allow the volume to have range from 0% to " \
126                                  "400%, instead of 0% to 200%. This option " \
127                                  "can distort the audio, since it uses " \
128                                  "software amplification." )
129
130 #define SAVEVOL_TEXT N_( "Automatically save the volume on exit" )
131
132 #define PRIVACY_TEXT N_( "Ask for network policy at start" )
133
134 #define RECENTPLAY_TEXT N_( "Save the recently played items in the menu" )
135
136 #define RECENTPLAY_FILTER_TEXT N_( "List of words separated by | to filter" )
137 #define RECENTPLAY_FILTER_LONGTEXT N_( "Regular expression used to filter " \
138         "the recent items played in the player" )
139
140 #define SLIDERCOL_TEXT N_( "Define the colors of the volume slider " )
141 #define SLIDERCOL_LONGTEXT N_( "Define the colors of the volume slider\n" \
142                        "By specifying the 12 numbers separated by a ';'\n" \
143             "Default is '255;255;255;20;226;20;255;176;15;235;30;20'\n" \
144             "An alternative can be '30;30;50;40;40;100;50;50;160;150;150;255' ")
145
146 #define QT_MODE_TEXT N_( "Selection of the starting mode and look " )
147 #define QT_MODE_LONGTEXT N_( "Start VLC with:\n" \
148                              " - normal mode\n"  \
149                              " - a zone always present to show information " \
150                                   "as lyrics, album arts...\n" \
151                              " - minimal mode with limited controls" )
152
153 #define QT_NORMAL_MODE_TEXT N_( "Classic" )
154 #define QT_ALWAYS_VIDEO_MODE_TEXT N_( "Complete (with information area)" )
155 #define QT_MINIMAL_MODE_TEXT N_( "Minimal (without menu)" )
156
157 #define QT_FULLSCREEN_TEXT N_( "Show a controller in fullscreen mode" )
158 #define QT_NATIVEOPEN_TEXT N_( "Embed the file browser in open dialog" )
159
160 #define FULLSCREEN_NUMBER_TEXT N_( "Define which screen fullscreen goes" )
161 #define FULLSCREEN_NUMBER_LONGTEXT N_( "Screennumber of fullscreen, instead of" \
162                                        "same screen where interface is" )
163
164 #define QT_AUTOLOAD_EXTENSIONS_TEXT N_( "Load extensions on startup" )
165 #define QT_AUTOLOAD_EXTENSIONS_LONGTEXT N_( "Automatically load the "\
166                                             "extensions module on startup" )
167
168 /* Various modes definition */
169 static const int i_mode_list[] =
170     { QT_NORMAL_MODE, QT_ALWAYS_VIDEO_MODE, QT_MINIMAL_MODE };
171 static const char *const psz_mode_list_text[] =
172     { QT_NORMAL_MODE_TEXT, QT_ALWAYS_VIDEO_MODE_TEXT, QT_MINIMAL_MODE_TEXT };
173
174
175 /**********************************************************************/
176 vlc_module_begin ()
177     set_shortname( "Qt" )
178     set_description( N_("Qt interface") )
179     set_category( CAT_INTERFACE )
180     set_subcategory( SUBCAT_INTERFACE_MAIN )
181     set_capability( "interface", 151 )
182     set_callbacks( OpenIntf, Close )
183
184     add_shortcut("qt")
185     add_integer( "qt-display-mode", QT_NORMAL_MODE, NULL,
186                  QT_MODE_TEXT, QT_MODE_LONGTEXT, false )
187         change_integer_list( i_mode_list, psz_mode_list_text, NULL )
188
189     add_bool( "qt-notification", true, NULL, NOTIFICATION_TEXT,
190               NOTIFICATION_LONGTEXT, false )
191
192     add_float_with_range( "qt-opacity", 1., 0.1, 1., NULL, OPACITY_TEXT,
193                           OPACITY_LONGTEXT, false )
194     add_float_with_range( "qt-fs-opacity", 0.8, 0.1, 1., NULL, OPACITY_FS_TEXT,
195                           OPACITY_FS_LONGTEXT, false )
196
197     add_bool( "qt-system-tray", true, NULL, SYSTRAY_TEXT,
198               SYSTRAY_LONGTEXT, false)
199     add_bool( "qt-start-minimized", false, NULL, MINIMIZED_TEXT,
200               MINIMIZED_LONGTEXT, true)
201     add_bool( "qt-video-autoresize", true, NULL, KEEPSIZE_TEXT,
202               KEEPSIZE_LONGTEXT, false )
203     add_bool( "qt-name-in-title", true, NULL, TITLE_TEXT,
204               TITLE_LONGTEXT, false )
205     add_bool( "qt-fs-controller", true, NULL, QT_FULLSCREEN_TEXT,
206               QT_FULLSCREEN_TEXT, false )
207
208     add_bool( "qt-volume-complete", false, NULL, COMPLETEVOL_TEXT,
209               COMPLETEVOL_LONGTEXT, true )
210     add_bool( "qt-autosave-volume", false, NULL, SAVEVOL_TEXT,
211               SAVEVOL_TEXT, true )
212
213     add_bool( "qt-embedded-open", false, NULL, QT_NATIVEOPEN_TEXT,
214                QT_NATIVEOPEN_TEXT, false )
215     add_bool( "qt-recentplay", true, NULL, RECENTPLAY_TEXT,
216               RECENTPLAY_TEXT, false )
217     add_string( "qt-recentplay-filter", "", NULL,
218                 RECENTPLAY_FILTER_TEXT, RECENTPLAY_FILTER_LONGTEXT, false )
219
220     add_bool( "qt-adv-options", false, NULL, ADVANCED_OPTIONS_TEXT,
221               ADVANCED_OPTIONS_LONGTEXT, true )
222     add_bool( "qt-advanced-pref", false, NULL, ADVANCED_PREFS_TEXT,
223               ADVANCED_PREFS_LONGTEXT, false )
224     add_bool( "qt-error-dialogs", true, NULL, ERROR_TEXT,
225               ERROR_TEXT, false )
226 #ifdef UPDATE_CHECK
227     add_bool( "qt-updates-notif", true, NULL, UPDATER_TEXT,
228               UPDATER_LONGTEXT, false )
229     add_integer( "qt-updates-days", 7, NULL, UPDATER_DAYS_TEXT,
230                  UPDATER_DAYS_TEXT, false )
231 #endif
232     add_string( "qt-slider-colours",
233                 "255;255;255;20;210;20;255;199;15;245;39;29",
234                 NULL, SLIDERCOL_TEXT, SLIDERCOL_LONGTEXT, false )
235
236     add_bool( "qt-privacy-ask", true, NULL, PRIVACY_TEXT, PRIVACY_TEXT,
237               false )
238         change_internal ()
239
240     add_integer( "qt-fullscreen-screennumber", -1, NULL, FULLSCREEN_NUMBER_TEXT,
241                FULLSCREEN_NUMBER_LONGTEXT, false );
242
243     add_bool( "qt-autoload-extensions", true, NULL,
244               QT_AUTOLOAD_EXTENSIONS_TEXT, QT_AUTOLOAD_EXTENSIONS_LONGTEXT,
245               false )
246
247     add_obsolete_bool( "qt-blingbling" ) /* Suppressed since 1.0.0 */
248
249     add_submodule ()
250         set_description( "Dialogs provider" )
251         set_capability( "dialogs provider", 51 )
252
253         set_callbacks( OpenDialogs, Close )
254
255 #if defined(Q_WS_X11) || defined(Q_WS_WIN)
256     add_submodule ()
257 #if defined(Q_WS_X11)
258         set_capability( "vout window xid", 50 )
259 #elif defined(Q_WS_WIN)
260         set_capability( "vout window hwnd", 50 )
261 #endif
262         set_callbacks( WindowOpen, WindowClose )
263 #endif
264
265 vlc_module_end ()
266
267 /*****************************************/
268
269 /* Ugly, but the Qt4 interface assumes single instance anyway */
270 static vlc_sem_t ready;
271 #ifdef Q_WS_X11
272 static char *x11_display = NULL;
273 #endif
274 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
275
276 /*****************************************************************************
277  * Module callbacks
278  *****************************************************************************/
279
280 /* Open Interface */
281 static int Open( vlc_object_t *p_this, bool isDialogProvider )
282 {
283     intf_thread_t *p_intf = (intf_thread_t *)p_this;
284
285 #ifdef Q_WS_X11
286     x11_display = var_CreateGetNonEmptyString( p_intf, "x11-display" );
287     Display *p_display = XOpenDisplay( x11_display );
288     if( !p_display )
289     {
290         msg_Err( p_intf, "Could not connect to X server" );
291         return VLC_EGENERIC;
292     }
293     XCloseDisplay( p_display );
294     putenv( (char *)"XLIB_SKIP_ARGB_VISUALS=1" );
295 #endif
296
297     if (vlc_mutex_trylock (&lock))
298     {
299         msg_Err (p_this, "cannot start Qt4 multiple times");
300         return VLC_EGENERIC;
301     }
302
303     /* Allocations of p_sys */
304     intf_sys_t *p_sys = p_intf->p_sys = new intf_sys_t;
305     p_intf->p_sys->b_isDialogProvider = isDialogProvider;
306     p_sys->p_popup_menu = NULL;
307     p_sys->p_mi = NULL;
308     p_sys->p_playlist = pl_Get( p_intf );
309
310     /* */
311     vlc_sem_init (&ready, 0);
312     if( vlc_clone( &p_sys->thread, Thread, p_intf, VLC_THREAD_PRIORITY_LOW ) )
313     {
314         delete p_sys;
315         vlc_mutex_unlock (&lock);
316         return VLC_ENOMEM;
317     }
318
319     /* */
320     vlc_sem_wait (&ready);
321     vlc_sem_destroy (&ready);
322
323     if( !p_sys->b_isDialogProvider )
324     {
325         vlc_value_t val;
326         var_Create (p_this->p_libvlc, "qt4-iface", VLC_VAR_ADDRESS);
327         val.p_address = p_this;
328         var_Set (p_this->p_libvlc, "qt4-iface", val);
329     }
330     return VLC_SUCCESS;
331 }
332
333 /* Open qt4 interface */
334 static int OpenIntf( vlc_object_t *p_this )
335 {
336     return Open( p_this, false );
337 }
338
339 /* Open Dialog Provider */
340 static int OpenDialogs( vlc_object_t *p_this )
341 {
342     return Open( p_this, true );
343 }
344
345 static void Close( vlc_object_t *p_this )
346 {
347     intf_thread_t *p_intf = (intf_thread_t *)p_this;
348     intf_sys_t *p_sys = p_intf->p_sys;
349
350     if( !p_sys->b_isDialogProvider )
351         var_Destroy (p_this->p_libvlc, "qt4-iface");
352
353     QVLCApp::triggerQuit();
354
355     vlc_join (p_sys->thread, NULL);
356     delete p_sys;
357     vlc_mutex_unlock (&lock);
358 #ifdef Q_WS_X11
359     free (x11_display);
360 #endif
361 }
362
363 static void *Thread( void *obj )
364 {
365     intf_thread_t *p_intf = (intf_thread_t *)obj;
366     MainInterface *p_mi;
367     char dummy[] = "vlc"; /* for WM_CLASS */
368     char *argv[4] = { dummy, NULL, };
369     int argc = 1;
370
371     Q_INIT_RESOURCE( vlc );
372
373     /* Start the QApplication here */
374 #ifdef Q_WS_X11
375     if( x11_display != NULL )
376     {
377         argv[argc++] = const_cast<char *>("-display");
378         argv[argc++] = x11_display;
379         argv[argc] = NULL;
380     }
381 #endif
382
383     QVLCApp app( argc, argv );
384
385     p_intf->p_sys->p_app = &app;
386
387
388     /* All the settings are in the .conf/.ini style */
389     p_intf->p_sys->mainSettings = new QSettings(
390 #ifdef WIN32
391             QSettings::IniFormat,
392 #else
393             QSettings::NativeFormat,
394 #endif
395             QSettings::UserScope, "vlc", "vlc-qt-interface" );
396
397     /* Icon setting */
398     if( QDate::currentDate().dayOfYear() >= 352 ) /* One Week before Xmas */
399         app.setWindowIcon( QIcon(vlc_christmas_xpm) );
400     else
401         app.setWindowIcon( QIcon(vlc_xpm) );
402
403     /* Initialize timers and the Dialog Provider */
404     DialogsProvider::getInstance( p_intf );
405
406     /* Detect screensize for small screens like TV or EEEpc*/
407     p_intf->p_sys->i_screenHeight =
408         app.QApplication::desktop()->availableGeometry().height();
409
410 #ifdef UPDATE_CHECK
411     /* Checking for VLC updates */
412     if( var_InheritBool( p_intf, "qt-updates-notif" ) &&
413         !var_InheritBool( p_intf, "qt-privacy-ask" ) )
414     {
415         int interval = var_InheritInteger( p_intf, "qt-updates-days" );
416         if( QDate::currentDate() >
417              getSettings()->value( "updatedate" ).toDate().addDays( interval ) )
418         {
419             /* The constructor of the update Dialog will do the 1st request */
420             UpdateDialog::getInstance( p_intf );
421             getSettings()->setValue( "updatedate", QDate::currentDate() );
422         }
423     }
424 #endif
425
426     /* Create the normal interface in non-DP mode */
427     if( !p_intf->p_sys->b_isDialogProvider )
428         p_mi = new MainInterface( p_intf );
429     else
430         p_mi = NULL;
431
432     /* Explain how to show a dialog :D */
433     p_intf->pf_show_dialog = ShowDialog;
434
435     /* */
436     p_intf->p_sys->p_mi = p_mi;
437     vlc_sem_post (&ready);
438
439     /* Last settings */
440     app.setQuitOnLastWindowClosed( false );
441
442     /* Retrieve last known path used in file browsing */
443     p_intf->p_sys->filepath =
444          getSettings()->value( "filedialog-path", QVLCUserDir( VLC_HOME_DIR ) ).toString();
445
446     /* Loads and tries to apply the preferred QStyle */
447     QString s_style = getSettings()->value( "MainWindow/QtStyle", "" ).toString();
448     if( s_style.compare("") != 0 )
449         QApplication::setStyle( s_style );
450
451     /* Launch */
452     app.exec();
453
454     /* And quit */
455     msg_Dbg( p_intf, "Quitting the Qt4 Interface" );
456
457     QApplication::closeAllWindows();
458
459     if (p_mi != NULL)
460     {
461         /* FIXME: are we sure that video window is already destroyed? */
462
463         msg_Dbg (p_intf, "destroying the main Qt4 interface");
464         p_intf->p_sys->p_mi = NULL;
465         /* Destroy first the main interface because it is connected to some
466            slots in the MainInputManager */
467         delete p_mi;
468     }
469
470     /* Destroy all remaining windows,
471        because some are connected to some slots
472        in the MainInputManager
473        Settings must be destroyed after that.
474      */
475     DialogsProvider::killInstance();
476
477     /* Delete the recentsMRL object before the configuration */
478     RecentsMRL::killInstance();
479
480     /* Save the path */
481     getSettings()->setValue( "filedialog-path", p_intf->p_sys->filepath );
482
483     /* Delete the configuration. Application has to be deleted after that. */
484     delete p_intf->p_sys->mainSettings;
485
486     /* Destroy the MainInputManager */
487     MainInputManager::killInstance();
488
489
490     /* Delete the application automatically */
491 #ifdef Q_WS_X11
492     free( x11_display );
493 #endif
494     return NULL;
495 }
496
497 /*****************************************************************************
498  * Callback to show a dialog
499  *****************************************************************************/
500 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
501                         intf_dialog_args_t *p_arg )
502 {
503     VLC_UNUSED( p_intf );
504     DialogEvent *event = new DialogEvent( i_dialog_event, i_arg, p_arg );
505     QApplication::postEvent( THEDP, event );
506 }
507
508 /**
509  * Video output window provider
510  *
511  * TODO move it out of here ?
512  */
513 #include <vlc_vout_window.h>
514
515 static int WindowControl( vout_window_t *, int i_query, va_list );
516
517 static int WindowOpen( vlc_object_t *p_obj )
518 {
519     vout_window_t *p_wnd = (vout_window_t*)p_obj;
520
521     /* */
522     if( p_wnd->cfg->is_standalone )
523         return VLC_EGENERIC;
524
525     vlc_value_t val;
526
527     if( var_Get( p_obj->p_libvlc, "qt4-iface", &val ) )
528         val.p_address = NULL;
529
530     intf_thread_t *p_intf = (intf_thread_t *)val.p_address;
531
532     if( !p_intf )
533     {   /* If another interface is used, this plugin cannot work */
534         msg_Dbg( p_obj, "Qt4 interface not found" );
535         return VLC_EGENERIC;
536     }
537
538     MainInterface *p_mi = p_intf->p_sys->p_mi;
539     msg_Dbg( p_obj, "requesting video..." );
540
541     int i_x = p_wnd->cfg->x;
542     int i_y = p_wnd->cfg->y;
543     unsigned i_width = p_wnd->cfg->width;
544     unsigned i_height = p_wnd->cfg->height;
545
546 #if defined (Q_WS_X11)
547     p_wnd->handle.xid = p_mi->getVideo( &i_x, &i_y, &i_width, &i_height );
548     if( !p_wnd->handle.xid )
549         return VLC_EGENERIC;
550     p_wnd->display.x11 = x11_display;
551
552 #elif defined (Q_WS_WIN)
553     p_wnd->handle.hwnd = p_mi->getVideo( &i_x, &i_y, &i_width, &i_height );
554     if( !p_wnd->handle.hwnd )
555         return VLC_EGENERIC;
556 #else
557 # error FIXME
558 #endif
559
560     p_wnd->control = WindowControl;
561     p_wnd->sys = (vout_window_sys_t*)p_mi;
562     return VLC_SUCCESS;
563 }
564
565 static int WindowControl( vout_window_t *p_wnd, int i_query, va_list args )
566 {
567     MainInterface *p_mi = (MainInterface *)p_wnd->sys;
568     return p_mi->controlVideo( i_query, args );
569 }
570
571 static void WindowClose( vlc_object_t *p_obj )
572 {
573     vout_window_t *p_wnd = (vout_window_t*)p_obj;
574     MainInterface *p_mi = (MainInterface *)p_wnd->sys;
575
576     msg_Dbg( p_obj, "releasing video..." );
577     p_mi->releaseVideo();
578 }
579