]> git.sesse.net Git - vlc/blob - modules/gui/qt4/qt4.cpp
Pull cancellation into (most) remaining interfaces
[vlc] / modules / gui / qt4 / qt4.cpp
1 /*****************************************************************************
2  * qt4.cpp : QT4 interface
3  ****************************************************************************
4  * Copyright © 2006-2008 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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <QApplication>
29 #include <QLocale>
30 #include <QTranslator>
31 #include <QDate>
32 #include <QMutex>
33 #include <QMutexLocker>
34 #include <QWaitCondition>
35 #include <QPointer>
36
37 #include "qt4.hpp"
38 #include "dialogs_provider.hpp"
39 #include "input_manager.hpp"
40 #include "main_interface.hpp"
41 #include "dialogs/help.hpp" /* update */
42
43 #ifdef HAVE_X11_XLIB_H
44 #include <X11/Xlib.h>
45 #endif
46
47 #include "../../../share/vlc32x32.xpm"
48 #include "../../../share/vlc32x32-christmas.xpm"
49 #include <vlc_plugin.h>
50
51 /*****************************************************************************
52  * Local prototypes.
53  *****************************************************************************/
54 static int  Open         ( vlc_object_t * );
55 static void Close        ( vlc_object_t * );
56 static int  OpenDialogs  ( vlc_object_t * );
57 static int  WindowOpen   ( vlc_object_t * );
58 static void WindowClose  ( vlc_object_t * );
59 static void Run          ( intf_thread_t * );
60 static void *Init        ( vlc_object_t * );
61 static void ShowDialog   ( intf_thread_t *, int, int, intf_dialog_args_t * );
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66 #define ADVANCED_PREFS_TEXT N_( "Show advanced preferences over simple ones" )
67 #define ADVANCED_PREFS_LONGTEXT N_( "Show advanced preferences and not simple "\
68                                     "preferences when opening the preferences "\
69                                     "dialog." )
70
71 #define SYSTRAY_TEXT N_( "Systray icon" )
72 #define SYSTRAY_LONGTEXT N_( "Show an icon in the systray " \
73                              "allowing you to control VLC media player " \
74                              "for basic actions." )
75
76 #define MINIMIZED_TEXT N_( "Start VLC with only a systray icon" )
77 #define MINIMIZED_LONGTEXT N_( "VLC will start with just an icon in " \
78                                "your taskbar" )
79
80 #define TITLE_TEXT N_( "Show playing item name in window title" )
81 #define TITLE_LONGTEXT N_( "Show the name of the song or video in the " \
82                            "controler window title." )
83
84 #define FILEDIALOG_PATH_TEXT N_( "Path to use in openfile dialog" )
85
86 #define NOTIFICATION_TEXT N_( "Show notification popup on track change" )
87 #define NOTIFICATION_LONGTEXT N_( \
88     "Show a notification popup with the artist and track name when " \
89     "the current playlist item changes, when VLC is minimized or hidden." )
90
91 #define ADVANCED_OPTIONS_TEXT N_( "Advanced options" )
92 #define ADVANCED_OPTIONS_LONGTEXT N_( "Show all the advanced options " \
93                                       "in the dialogs." )
94
95 #define OPACITY_TEXT N_( "Windows opacity between 0.1 and 1." )
96 #define OPACITY_LONGTEXT N_( "Sets the windows opacity between 0.1 and 1 " \
97                              "for main interface, playlist and extended panel."\
98                              " This option only works with Windows and " \
99                              "X11 with composite extensions." )
100
101 #define ERROR_TEXT N_( "Show unimportant error and warnings dialogs" )
102
103 #define UPDATER_TEXT N_( "Activate the updates availability notification" )
104 #define UPDATER_LONGTEXT N_( "Activate the automatic notification of new " \
105                             "versions of the software. It runs once every " \
106                             "two weeks." )
107 #define UPDATER_DAYS_TEXT N_("Number of days between two update checks")
108
109 #define COMPLETEVOL_TEXT N_( "Allow the volume to be set to 400%" )
110 #define COMPLETEVOL_LONGTEXT N_( "Allow the volume to have range from 0% to " \
111                                  "400%, instead of 0% to 200%. This option " \
112                                  "can distort the audio, since it uses " \
113                                  "software amplification." )
114
115 #define SAVEVOL_TEXT N_( "Automatically save the volume on exit" )
116
117 #define BLING_TEXT N_( "Use non native buttons and volume slider" )
118
119 #define PRIVACY_TEXT N_( "Ask for network policy at start" )
120
121 #define SLIDERCOL_TEXT N_( "Define the colors of the volume slider " )
122 #define SLIDERCOL_LONGTEXT N_( "Define the colors of the volume slider\n" \
123                        "By specifying the 12 numbers separated by a ';'\n" \
124             "Default is '255;255;255;20;226;20;255;176;15;235;30;20'\n" \
125             "An alternative can be '30;30;50;40;40;100;50;50;160;150;150;255' ")
126
127 #define QT_MODE_TEXT N_( "Selection of the starting mode and look " )
128 #define QT_MODE_LONGTEXT N_( "Start VLC with:\n" \
129                              " - normal mode\n"  \
130                              " - a zone always present to show information " \
131                                   "as lyrics, album arts...\n" \
132                              " - minimal mode with limited controls" )
133
134 #define QT_NORMAL_MODE_TEXT N_( "Classic look" )
135 #define QT_ALWAYS_VIDEO_MODE_TEXT N_( "Complete look with information area" )
136 #define QT_MINIMAL_MODE_TEXT N_( "Minimal look with no menus" )
137
138 #define QT_FULLSCREEN_TEXT N_( "Show a controller in fullscreen mode" )
139
140 static const int i_mode_list[] =
141     { QT_NORMAL_MODE, QT_ALWAYS_VIDEO_MODE, QT_MINIMAL_MODE };
142 static const char *const psz_mode_list_text[] =
143     { QT_NORMAL_MODE_TEXT, QT_ALWAYS_VIDEO_MODE_TEXT, QT_MINIMAL_MODE_TEXT };
144
145 vlc_module_begin();
146     set_shortname( "Qt" );
147     set_description( N_("Qt interface") );
148     set_category( CAT_INTERFACE ) ;
149     set_subcategory( SUBCAT_INTERFACE_MAIN );
150     set_capability( "interface", 151 );
151     set_callbacks( Open, Close );
152
153     add_shortcut("qt");
154
155     add_submodule();
156         set_description( "Dialogs provider" );
157         set_capability( "dialogs provider", 51 );
158
159         add_integer( "qt-display-mode", QT_NORMAL_MODE, NULL,
160                      QT_MODE_TEXT, QT_MODE_LONGTEXT, false );
161             change_integer_list( i_mode_list, psz_mode_list_text, NULL );
162
163         add_bool( "qt-notification", true, NULL, NOTIFICATION_TEXT,
164                   NOTIFICATION_LONGTEXT, false );
165
166         add_float_with_range( "qt-opacity", 1., 0.1, 1., NULL, OPACITY_TEXT,
167                   OPACITY_LONGTEXT, false );
168         add_bool( "qt-blingbling", true, NULL, BLING_TEXT,
169                   BLING_TEXT, false );
170
171         add_bool( "qt-system-tray", true, NULL, SYSTRAY_TEXT,
172                 SYSTRAY_LONGTEXT, false);
173         add_bool( "qt-start-minimized", false, NULL, MINIMIZED_TEXT,
174                 MINIMIZED_LONGTEXT, true);
175         add_bool( "qt-name-in-title", true, NULL, TITLE_TEXT,
176                   TITLE_LONGTEXT, false );
177         add_bool( "qt-fs-controller", true, NULL, QT_FULLSCREEN_TEXT,
178                   QT_FULLSCREEN_TEXT, false );
179
180         add_bool( "qt-volume-complete", false, NULL, COMPLETEVOL_TEXT,
181                 COMPLETEVOL_LONGTEXT, true );
182         add_bool( "qt-autosave-volume", false, NULL, SAVEVOL_TEXT,
183                 SAVEVOL_TEXT, true );
184         add_string( "qt-filedialog-path", NULL, NULL, FILEDIALOG_PATH_TEXT,
185                 FILEDIALOG_PATH_TEXT, true );
186             change_autosave();
187             change_internal();
188
189         add_bool( "qt-adv-options", false, NULL, ADVANCED_OPTIONS_TEXT,
190                   ADVANCED_OPTIONS_LONGTEXT, true );
191         add_bool( "qt-advanced-pref", false, NULL, ADVANCED_PREFS_TEXT,
192                 ADVANCED_PREFS_LONGTEXT, false );
193         add_bool( "qt-error-dialogs", true, NULL, ERROR_TEXT,
194                 ERROR_TEXT, false );
195 #ifdef UPDATE_CHECK
196         add_bool( "qt-updates-notif", true, NULL, UPDATER_TEXT,
197                 UPDATER_LONGTEXT, false );
198         add_integer( "qt-updates-days", 7, NULL, UPDATER_DAYS_TEXT,
199                 UPDATER_DAYS_TEXT, false );
200 #endif
201         add_string( "qt-slider-colours",
202                 "255;255;255;20;226;20;255;176;15;235;30;20",
203                 NULL, SLIDERCOL_TEXT, SLIDERCOL_LONGTEXT, false );
204
205         add_bool( "qt-privacy-ask", true, NULL, PRIVACY_TEXT, PRIVACY_TEXT,
206                 false );
207             change_internal();
208
209         set_callbacks( OpenDialogs, Close );
210
211     add_submodule();
212         set_capability( "vout window", 50 );
213         set_callbacks( WindowOpen, WindowClose );
214 vlc_module_end();
215
216 /*****************************************************************************
217  * Module callbacks
218  *****************************************************************************/
219 static int Open( vlc_object_t *p_this )
220 {
221     intf_thread_t *p_intf = (intf_thread_t *)p_this;
222
223 #if defined Q_WS_X11 && defined HAVE_X11_XLIB_H
224     /* Thanks for libqt4 calling exit() in QApplication::QApplication()
225      * instead of returning an error, we have to check the X11 display */
226     Display *p_display = XOpenDisplay( NULL );
227     if( !p_display )
228     {
229         msg_Err( p_intf, "Could not connect to X server" );
230         return VLC_EGENERIC;
231     }
232     XCloseDisplay( p_display );
233 #endif
234
235     /* Allocations */
236     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
237     if( !p_intf->p_sys )
238         return VLC_ENOMEM;
239     memset( p_intf->p_sys, 0, sizeof( intf_sys_t ) );
240
241     p_intf->pf_run = Run;
242     p_intf->p_sys->p_mi = NULL;
243
244     /* Access to the playlist */
245     p_intf->p_sys->p_playlist = pl_Yield( p_intf );
246     /* Listen to the messages */
247     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
248     /* one settings to rule them all */
249
250     var_Create( p_this, "window_widget", VLC_VAR_ADDRESS );
251     return VLC_SUCCESS;
252 }
253
254 static int OpenDialogs( vlc_object_t *p_this )
255 {
256     intf_thread_t *p_intf = (intf_thread_t *)p_this;
257     int val = Open( p_this );
258     if( val )
259         return val;
260
261     p_intf->pf_show_dialog = ShowDialog;
262     return VLC_SUCCESS;
263 }
264
265 static void Close( vlc_object_t *p_this )
266 {
267     intf_thread_t *p_intf = (intf_thread_t *)p_this;
268
269     vlc_object_lock( p_intf );
270     p_intf->b_dead = true;
271     vlc_object_unlock( p_intf );
272
273     if( p_intf->p_sys->b_isDialogProvider )
274     {
275         if( DialogsProvider::isAlive() )
276         {
277             msg_Dbg( p_intf, "Asking the DP to quit nicely" );
278             DialogEvent *event = new DialogEvent( INTF_DIALOG_EXIT, 0, NULL );
279             QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
280         }
281         vlc_thread_join( p_intf );
282     }
283
284     vlc_object_release( p_intf->p_sys->p_playlist );
285     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
286     free( p_intf->p_sys );
287 }
288
289
290 /*****************************************************************************
291  * Initialize the interface or the dialogs provider
292  *****************************************************************************/
293 static void Run( intf_thread_t *p_intf )
294 {
295     if( p_intf->pf_show_dialog )
296     {
297         if( vlc_thread_create( p_intf, "Qt dialogs", Init, 0, true ) )
298             msg_Err( p_intf, "failed to create Qt dialogs thread" );
299     }
300     else
301     {
302         int canc = vlc_savecancel ();
303         Init( VLC_OBJECT(p_intf) );
304         vlc_restorecancel( canc );
305     }
306 }
307
308 static QMutex windowLock;
309 static QWaitCondition windowWait;
310
311 static void *Init( vlc_object_t *obj )
312 {
313     intf_thread_t *p_intf = (intf_thread_t *)obj;
314     vlc_value_t val;
315     char dummy[] = "";
316     char *argv[] = { dummy };
317     int argc = 1;
318     int canc = vlc_savecancel ();
319
320     Q_INIT_RESOURCE( vlc );
321
322 #if !defined(WIN32) && !defined(__APPLE__)
323     /* KLUDGE:
324      * disables icon theme use because that makes Cleanlooks style bug
325      * because it asks gconf for some settings that timeout because of threads
326      * see commits 21610 21622 21654 for reference */
327
328     /* If you don't have a gconftool-2 binary, you should comment this line */
329     if( strcmp( qVersion(), "4.4.0" ) < 0 ) /* fixed in Qt 4.4.0 */
330         QApplication::setDesktopSettingsAware( false );
331 #endif
332
333     /* Start the QApplication here */
334     QApplication *app = new QApplication( argc, argv , true );
335     p_intf->p_sys->p_app = app;
336
337     p_intf->p_sys->mainSettings = new QSettings(
338 #ifdef WIN32
339             QSettings::IniFormat,
340 #else
341             QSettings::NativeFormat,
342 #endif
343             QSettings::UserScope, "vlc", "vlc-qt-interface" );
344
345     /* Icon setting */
346     if( QDate::currentDate().dayOfYear() >= 354 )
347         app->setWindowIcon( QIcon( QPixmap(vlc_christmas_xpm) ) );
348     else
349         app->setWindowIcon( QIcon( QPixmap(vlc_xpm) ) );
350
351     /* Initialize timers and the Dialog Provider */
352     DialogsProvider::getInstance( p_intf );
353
354     QPointer<MainInterface> *miP = NULL;
355
356 #ifdef UPDATE_CHECK
357     /* Checking for VLC updates */
358     if( config_GetInt( p_intf, "qt-updates-notif" ) &&
359         !config_GetInt( p_intf, "qt-privacy-ask" ) )
360     {
361         int interval = config_GetInt( p_intf, "qt-updates-days" );
362         if( QDate::currentDate() >
363              getSettings()->value( "updatedate" ).toDate().addDays( interval ) )
364         {
365             /* The constructor of the update Dialog will do the 1st request */
366             UpdateDialog::getInstance( p_intf );
367             getSettings()->setValue( "updatedate", QDate::currentDate() );
368         }
369     }
370 #endif
371
372     /* Create the normal interface in non-DP mode */
373     if( !p_intf->pf_show_dialog )
374     {
375         p_intf->p_sys->p_mi = new MainInterface( p_intf );
376         /* We don't show it because it is done in the MainInterface constructor
377         p_mi->show(); */
378         p_intf->p_sys->b_isDialogProvider = false;
379
380         miP = new QPointer<MainInterface> (p_intf->p_sys->p_mi);
381         val.p_address = miP;
382         QMutexLocker locker (&windowLock);
383         var_Set (p_intf, "window_widget", val);
384         windowWait.wakeAll ();
385     }
386     else
387     {
388         vlc_thread_ready( p_intf );
389         p_intf->p_sys->b_isDialogProvider = true;
390     }
391
392     /* Explain to the core how to show a dialog :D */
393     p_intf->pf_show_dialog = ShowDialog;
394
395 #ifdef ENABLE_NLS
396     // Translation - get locale
397 #   if defined (WIN32) || defined (__APPLE__)
398     char* psz_tmp = config_GetPsz( p_intf, "language" );
399     QString lang = qfu( psz_tmp );
400     free( psz_tmp);
401     if (lang == "auto")
402         lang = QLocale::system().name();
403 #   else
404     QString lang = QLocale::system().name();
405 #   endif
406     // Translations for qt's own dialogs
407     QTranslator qtTranslator( 0 );
408     // Let's find the right path for the translation file
409 #if !defined( WIN32 )
410     QString path =  QString( QT4LOCALEDIR );
411 #else
412     QString path = QString( QString(config_GetDataDir()) + DIR_SEP +
413                             "locale" + DIR_SEP + "qt4" + DIR_SEP );
414 #endif
415     // files depending on locale
416     bool b_loaded = qtTranslator.load( path + "qt_" + lang );
417     if (!b_loaded)
418         msg_Dbg( p_intf, "Error while initializing qt-specific localization" );
419     app->installTranslator( &qtTranslator );
420 #endif  //ENABLE_NLS
421
422     /* Last settings */
423     app->setQuitOnLastWindowClosed( false );
424
425     /* Retrieve last known path used in file browsing */
426     char *psz_path = config_GetPsz( p_intf, "qt-filedialog-path" );
427     p_intf->p_sys->psz_filepath = EMPTY_STR( psz_path ) ? psz_path
428                                                         : config_GetHomeDir();
429
430     /* Launch */
431     app->exec();
432
433     /* And quit */
434     msg_Dbg( p_intf, "Quitting the Qt4 Interface" );
435
436     if (miP)
437     {
438         QMutexLocker locker (&windowLock);
439
440         /* We need to warn to detach from any vout before
441          * deleting miP (WindowClose will not be called after it) */
442         p_intf->p_sys->p_mi->releaseVideo( NULL );
443
444         val.p_address = NULL;
445         var_Set (p_intf, "window_widget", val);
446         delete miP;
447     }
448
449     /* Destroy first the main interface because it is connected to some slots
450        in the MainInputManager */
451     delete p_intf->p_sys->p_mi;
452
453     /* Destroy all remaining windows,
454        because some are connected to some slots
455        in the MainInputManager
456        Settings must be destroyed after that.
457      */
458     DialogsProvider::killInstance();
459
460     /* Delete the configuration. Application has to be deleted after that. */
461     delete p_intf->p_sys->mainSettings;
462
463     /* Destroy the MainInputManager */
464     MainInputManager::killInstance();
465
466     /* Delete the application */
467     delete app;
468
469     /* Save the path */
470     config_PutPsz( p_intf, "qt-filedialog-path", p_intf->p_sys->psz_filepath );
471     free( psz_path );
472     vlc_restorecancel (canc);
473     return NULL;
474 }
475
476 /*****************************************************************************
477  * Callback to show a dialog
478  *****************************************************************************/
479 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
480                         intf_dialog_args_t *p_arg )
481 {
482     DialogEvent *event = new DialogEvent( i_dialog_event, i_arg, p_arg );
483     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
484 }
485
486 /*****************************************************************************
487  * PopupMenuCB: callback to show the popupmenu.
488  *  We don't show the menu directly here because we don't want the
489  *  caller to block for a too long time.
490  *****************************************************************************/
491 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
492                         vlc_value_t old_val, vlc_value_t new_val, void *param )
493 {
494     intf_thread_t *p_intf = (intf_thread_t *)param;
495     ShowDialog( p_intf, INTF_DIALOG_POPUPMENU, new_val.b_bool, 0 );
496     return VLC_SUCCESS;
497 }
498
499 /**
500  * Video output window provider
501  */
502 #include <vlc_window.h>
503
504 static int WindowControl (vout_window_t *, int, va_list);
505
506 static int WindowOpen (vlc_object_t *obj)
507 {
508     vout_window_t *wnd = (vout_window_t *)obj;
509
510     if (config_GetInt (obj, "embedded-video") <= 0)
511         return VLC_EGENERIC;
512
513     intf_thread_t *intf = (intf_thread_t *)
514         vlc_object_find_name (obj, "qt4", FIND_ANYWHERE);
515     if (intf == NULL)
516         return VLC_EGENERIC; /* Qt4 not in use */
517     assert (intf->i_object_type == VLC_OBJECT_INTF);
518
519     var_Create (intf, "window_widget", VLC_VAR_ADDRESS);
520
521     vlc_value_t ptrval;
522
523     windowLock.lock ();
524     msg_Dbg (obj, "waiting for interface...");
525     for (;;)
526     {
527         var_Get (intf, "window_widget", &ptrval);
528         if (ptrval.p_address != NULL)
529             break;
530         windowWait.wait (&windowLock);
531     }
532
533     msg_Dbg (obj, "requesting window...");
534     QPointer<MainInterface> *miP = (QPointer<MainInterface> *)ptrval.p_address;
535     miP = new QPointer<MainInterface> (*miP); /* create our own copy */
536     vlc_object_release (intf);
537
538     if (miP->isNull ())
539         return VLC_EGENERIC;
540
541     wnd->handle = (*miP)->requestVideo (wnd->vout, &wnd->pos_x, &wnd->pos_y,
542                                         &wnd->width, &wnd->height);
543     windowLock.unlock ();
544
545     if (!wnd->handle)
546         return VLC_EGENERIC;
547
548     wnd->control = WindowControl;
549     wnd->p_private = miP;
550     return VLC_SUCCESS;
551 }
552
553 static int WindowControl (vout_window_t *wnd, int query, va_list args)
554 {
555     QPointer<MainInterface> *miP = (QPointer<MainInterface> *)wnd->p_private;
556     QMutexLocker locker (&windowLock);
557
558     if (miP->isNull ())
559         return VLC_EGENERIC;
560     return (*miP)->controlVideo (wnd->handle, query, args);
561 }
562
563 static void WindowClose (vlc_object_t *obj)
564 {
565     vout_window_t *wnd = (vout_window_t *)obj;
566     QPointer<MainInterface> *miP = (QPointer<MainInterface> *)wnd->p_private;
567     QMutexLocker locker (&windowLock);
568
569     if (!miP->isNull ())
570         (*miP)->releaseVideo( wnd->handle );
571     delete miP;
572 }