]> git.sesse.net Git - mlt/commitdiff
Refactor QApplication creation and fix lifetime of its args.
authorDan Dennedy <dan@dennedy.org>
Wed, 2 Apr 2014 04:45:42 +0000 (21:45 -0700)
committerDan Dennedy <dan@dennedy.org>
Wed, 2 Apr 2014 04:45:42 +0000 (21:45 -0700)
src/modules/qt/Makefile
src/modules/qt/common.cpp [new file with mode: 0644]
src/modules/qt/common.h [new file with mode: 0644]
src/modules/qt/consumer_qglsl.cpp
src/modules/qt/kdenlivetitle_wrapper.cpp
src/modules/qt/producer_qtext.cpp
src/modules/qt/qimage_wrapper.cpp
src/modules/qt/transition_vqm.cpp

index 7a6c60c2bf771c3f528f553565d2b565e81ea8a9..20262b8e2a5152b39f7c94dbff58d93cf56b9442 100644 (file)
@@ -8,7 +8,8 @@ include config.mak
 TARGET = ../libmltqt$(LIBSUF)
 
 OBJS = factory.o producer_qimage.o producer_kdenlivetitle.o
-CPPOBJS = qimage_wrapper.o \
+CPPOBJS = common.o \
+       qimage_wrapper.o \
        kdenlivetitle_wrapper.o \
        consumer_qglsl.o \
        producer_qtext.o
diff --git a/src/modules/qt/common.cpp b/src/modules/qt/common.cpp
new file mode 100644 (file)
index 0000000..9cd4cac
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2014 Dan Dennedy <dan@dennedy.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "common.h"
+#include <QApplication>
+#include <QLocale>
+
+#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
+#include <X11/Xlib.h>
+#endif
+
+bool createQApplicationIfNeeded(mlt_service service)
+{
+       if (!qApp) {
+#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
+               XInitThreads();
+               if (getenv("DISPLAY") == 0) {
+                       mlt_log_error(service,
+                               "The MLT Qt module requires a X11 environment.\n"
+                               "Please either run melt from an X session or use a fake X server like xvfb:\n"
+                               "xvfb-run -a melt (...)\n" );
+                       return false;
+               }
+#endif
+               if (!mlt_properties_get(mlt_global_properties(), "qt_argv"))
+                       mlt_properties_set(mlt_global_properties(), "qt_argv", "MLT");
+               static int argc = 1;
+               static char* argv[] = { mlt_properties_get(mlt_global_properties(), "Qt argv") };
+               new QApplication(argc, argv);
+               const char *localename = mlt_properties_get_lcnumeric(MLT_SERVICE_PROPERTIES(service));
+               QLocale::setDefault(QLocale(localename));
+       }
+       return true;
+}
diff --git a/src/modules/qt/common.h b/src/modules/qt/common.h
new file mode 100644 (file)
index 0000000..de1ed4e
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2014 Dan Dennedy <dan@dennedy.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef COMMON_H
+#define COMMON_H
+
+#include <framework/mlt.h>
+
+bool createQApplicationIfNeeded(mlt_service service);
+
+#endif // COMMON_H
index ee841e6bc0f531e379af0b7a7994dbb86ed7cfb4..5a791c478d536dedaaf96971b31c899d0c2b2774 100644 (file)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include "common.h"
 #include <framework/mlt.h>
 #include <QApplication>
-#include <QLocale>
 #include <QGLWidget>
 #include <QMutex>
 #include <QWaitCondition>
 #include <QtGlobal>
 
-#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
-#include <X11/Xlib.h>
-#endif
-
 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
 
 class GLWidget : public QGLWidget
@@ -203,19 +199,10 @@ mlt_consumer consumer_qglsl_init( mlt_profile profile, mlt_service_type type, co
                        mlt_events_listen(properties, consumer, "consumer-thread-started", (mlt_listener) onThreadStarted);
                        mlt_events_listen(properties, consumer, "consumer-thread-stopped", (mlt_listener) onThreadStopped);
                        mlt_events_listen(properties, consumer, "consumer-cleanup", (mlt_listener) onCleanup);
-#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
-                       XInitThreads();
-                       if ( getenv("DISPLAY") == 0 ) {
-                               mlt_log_error(MLT_CONSUMER_SERVICE(consumer), "The qglsl consumer requires a X11 environment.\nPlease either run melt from an X session or use a fake X server like xvfb:\nxvfb-run -a melt (...)\n" );
-                       } else
-#endif
-                       if (!qApp) {
-                               int argc = 1;
-                               char* argv[1];
-                               argv[0] = (char*) "MLT qglsl consumer";
-                               new QApplication(argc, argv);
-                               const char *localename = mlt_properties_get_lcnumeric(properties);
-                               QLocale::setDefault(QLocale(localename));
+                       if (!createQApplicationIfNeeded(MLT_CONSUMER_SERVICE(consumer))) {
+                               mlt_filter_close(filter);
+                               mlt_consumer_close(consumer);
+                               return NULL;
                        }
 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
                        mlt_properties_set_data(properties, "GLWidget", new GLWidget, 0, NULL, NULL);
index 2d5bc4d6bf0df243cebfbc52348a552213771f4a..2b089d61288411807b2e46c1087985c68b8464e4 100755 (executable)
  */
 
 #include "kdenlivetitle_wrapper.h"
+#include "common.h"
 
 #include <QImage>
 #include <QPainter>
 #include <QDebug>
-#include <QApplication>
 #include <QMutex>
 #include <QGraphicsScene>
 #include <QGraphicsTextItem>
@@ -47,7 +47,6 @@
 #include <QGraphicsDropShadowEffect>
 #endif
 
-static QApplication *app = NULL;
 Q_DECLARE_METATYPE(QTextCursor);
 
 class ImageItem: public QGraphicsItem
@@ -380,32 +379,10 @@ void drawKdenliveTitle( producer_ktitle self, mlt_frame frame, int width, int he
 
                if ( scene == NULL )
                {
-                       int argc = 1;
-                       char* argv[1];
-                       argv[0] = (char*) "xxx";
-                       
-                       // Warning: all Qt graphic objects (QRect, ...) must be initialized AFTER 
-                       // the QApplication is created, otherwise their will be NULL
-                       
-                       if ( app == NULL ) {
-                               if ( qApp ) {
-                                       app = qApp;
-                               }
-                               else {
-#ifdef linux
-                                       if ( getenv("DISPLAY") == 0 )
-                                       {
-                                               mlt_log_panic( MLT_PRODUCER_SERVICE( producer ), "Error, cannot render titles without an X11 environment.\nPlease either run melt from an X session or use a fake X server like xvfb:\nxvfb-run -a melt (...)\n" );
-                                               pthread_mutex_unlock( &self->mutex );
-                                               return;
-                                       }
-#endif
-                                       app = new QApplication( argc, argv );                           
-                                       const char *localename = mlt_properties_get_lcnumeric( MLT_SERVICE_PROPERTIES( MLT_PRODUCER_SERVICE( producer ) ) );
-                                       QLocale::setDefault( QLocale( localename ) );
-                               }
+                       if ( !createQApplicationIfNeeded( MLT_PRODUCER_SERVICE(producer) ) )
+                               return;
+                       if ( QMetaType::UnknownType == QMetaType::type("QTextCursor") )
                                qRegisterMetaType<QTextCursor>( "QTextCursor" );
-                       }
                        scene = new QGraphicsScene();
                        scene->setItemIndexMethod( QGraphicsScene::NoIndex );
                         scene->setSceneRect(0, 0, mlt_properties_get_int( properties, "width" ), mlt_properties_get_int( properties, "height" ));
index ed7466473d89bf1a148be227169f0988acb119d7..e324be36ea40270221dec2e142768ce9713fe7c0 100644 (file)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include "common.h"
 #include <framework/mlt.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <QApplication>
 #include <QImage>
 #include <QColor>
-#include <QLocale>
 #include <QPainter>
 #include <QFont>
 #include <QString>
 #include <QTextCodec>
 #include <QTextDecoder>
 
-/** Init QT (if necessary)
-*/
-
-static bool init_qt( mlt_producer producer )
-{
-       int argc = 1;
-       char* argv[1];
-       argv[0] = (char*) "xxx";
-
-       if ( !qApp )
-       {
-#ifdef linux
-               if ( getenv("DISPLAY") == 0 )
-               {
-                       mlt_log_panic( MLT_PRODUCER_SERVICE( producer ), "Error, cannot render titles without an X11 environment.\nPlease either run melt from an X session or use a fake X server like xvfb:\nxvfb-run -a melt (...)\n" );
-                       return false;
-               }
-#endif
-               new QApplication( argc, argv );
-               const char *localename = mlt_properties_get_lcnumeric( MLT_SERVICE_PROPERTIES( MLT_PRODUCER_SERVICE( producer ) ) );
-               QLocale::setDefault( QLocale( localename ) );
-       }
-       return true;
-}
-
 static void close_qimg( void* qimg )
 {
        delete static_cast<QImage*>( qimg );
@@ -429,7 +403,7 @@ mlt_producer producer_qtext_init( mlt_profile profile, mlt_service_type type, co
        // Initialize the producer
        if ( producer )
        {
-               if( init_qt( producer ) == false )
+               if ( !createQApplicationIfNeeded( MLT_PRODUCER_SERVICE(producer) ) )
                {
                        mlt_producer_close( producer );
                        return NULL;
index 4fb645455f9d36e7e77d1ff74d5eb6605a6922ef..ee5db0b13dd0d0aab0af3b9741a8c88d7d52bee9 100644 (file)
@@ -22,6 +22,7 @@
  */
 
 #include "qimage_wrapper.h"
+#include "common.h"
 
 #ifdef USE_KDE4
 #include <kcomponentdata.h>
 
 #include <QImage>
 #include <QSysInfo>
-#include <QApplication>
 #include <QMutex>
 #include <QtEndian>
 #include <QTemporaryFile>
-#include <QLocale>
 
 #ifdef USE_EXIF
 #include <libexif/exif-data.h>
@@ -51,8 +50,6 @@ extern "C" {
 static KComponentData *instance = 0L;
 #endif
 
-static QApplication *app = NULL;
-
 static void qimage_delete( void *data )
 {
        QImage *image = ( QImage * )data;
@@ -163,31 +160,9 @@ int refresh_qimage( producer_qimage self, mlt_frame frame )
        sprintf( image_key, "%d", image_idx );
 
        int disable_exif = mlt_properties_get_int( producer_props, "disable_exif" );
-       
-       
-       if ( app == NULL ) 
-       {
-               if ( qApp ) 
-               {
-                       app = qApp;
-               }
-               else 
-               {
-#ifdef linux
-                       if ( getenv("DISPLAY") == 0 )
-                       {
-                               mlt_log_panic( MLT_PRODUCER_SERVICE( producer ), "Error, cannot render titles without an X11 environment.\nPlease either run melt from an X session or use a fake X server like xvfb:\nxvfb-run -a melt (...)\n" );
-                               return -1;
-                       }
-#endif
-                       int argc = 1;
-                       char* argv[1];
-                       argv[0] = (char*) "xxx";
-                       app = new QApplication( argc, argv );
-                       const char *localename = mlt_properties_get_lcnumeric( MLT_SERVICE_PROPERTIES( MLT_PRODUCER_SERVICE( producer ) ) );
-                       QLocale::setDefault( QLocale( localename ) );
-               }
-       }
+
+       if ( !createQApplicationIfNeeded( MLT_PRODUCER_SERVICE(producer) ) )
+               return -1;
 
        if ( image_idx != self->qimage_idx )
                self->qimage = NULL;
index c926b165803c98b3862d300c212883bb9450a50e..1fb7db4b0b3aea49254063ecfe3affe9ed5bb97b 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>
  */
 
+#include "common.h"
 #include <framework/mlt.h>
 #include <string.h>
 #include <math.h>
 #include <stdio.h>
-#include <QApplication>
 #include <QImage>
 #include <QColor>
-#include <QLocale>
 #include <QPainter>
 #include <QPalette>
 #include <QFont>
 #include <QString>
 
-static QApplication *app = 0;
-
 static double calc_psnr( const uint8_t *a, const uint8_t *b, int size, int bpp )
 {
        double mse = 0.0;
@@ -160,25 +157,6 @@ static int get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *form
                }
        }
 
-       // create QApplication, if needed
-       if ( !app )
-       {
-               if ( qApp )
-               {
-                       app = qApp;
-               }
-               else
-               {
-                       int argc = 1;
-                       char* argv[] = { strdup( "unknown" ) };
-
-                       app = new QApplication( argc, argv );
-                       const char *localename = mlt_properties_get_lcnumeric( MLT_TRANSITION_PROPERTIES(transition) );
-                       QLocale::setDefault( QLocale( localename ) );
-                       free( argv[0] );
-               }
-       }
-
        // setup Qt drawing
        QPainter painter;
        painter.begin( &img );
@@ -248,6 +226,11 @@ mlt_transition transition_vqm_init( mlt_profile profile, mlt_service_type type,
        {
                mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
 
+               if ( !createQApplicationIfNeeded( MLT_TRANSITION_SERVICE(transition) ) )
+               {
+                       mlt_transition_close( transition );
+                       return NULL;
+               }
                transition->process = process;
                mlt_properties_set_int( properties, "_transition_type", 1 ); // video only
                mlt_properties_set_int( properties, "window_size", 8 );