From 5d1defb9892cf1c92344c0d0a0bfc2f20ff56c99 Mon Sep 17 00:00:00 2001 From: Dan Dennedy Date: Tue, 1 Apr 2014 21:45:42 -0700 Subject: [PATCH] Refactor QApplication creation and fix lifetime of its args. --- src/modules/qt/Makefile | 3 +- src/modules/qt/common.cpp | 49 ++++++++++++++++++++++++ src/modules/qt/common.h | 26 +++++++++++++ src/modules/qt/consumer_qglsl.cpp | 23 +++-------- src/modules/qt/kdenlivetitle_wrapper.cpp | 31 ++------------- src/modules/qt/producer_qtext.cpp | 30 +-------------- src/modules/qt/qimage_wrapper.cpp | 33 ++-------------- src/modules/qt/transition_vqm.cpp | 29 +++----------- 8 files changed, 98 insertions(+), 126 deletions(-) create mode 100644 src/modules/qt/common.cpp create mode 100644 src/modules/qt/common.h diff --git a/src/modules/qt/Makefile b/src/modules/qt/Makefile index 7a6c60c2..20262b8e 100644 --- a/src/modules/qt/Makefile +++ b/src/modules/qt/Makefile @@ -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 index 00000000..9cd4cac5 --- /dev/null +++ b/src/modules/qt/common.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2014 Dan Dennedy + * + * 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 +#include + +#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) +#include +#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 index 00000000..de1ed4e9 --- /dev/null +++ b/src/modules/qt/common.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2014 Dan Dennedy + * + * 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 + +bool createQApplicationIfNeeded(mlt_service service); + +#endif // COMMON_H diff --git a/src/modules/qt/consumer_qglsl.cpp b/src/modules/qt/consumer_qglsl.cpp index ee841e6b..5a791c47 100644 --- a/src/modules/qt/consumer_qglsl.cpp +++ b/src/modules/qt/consumer_qglsl.cpp @@ -17,18 +17,14 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include "common.h" #include #include -#include #include #include #include #include -#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) -#include -#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); diff --git a/src/modules/qt/kdenlivetitle_wrapper.cpp b/src/modules/qt/kdenlivetitle_wrapper.cpp index 2d5bc4d6..2b089d61 100755 --- a/src/modules/qt/kdenlivetitle_wrapper.cpp +++ b/src/modules/qt/kdenlivetitle_wrapper.cpp @@ -19,11 +19,11 @@ */ #include "kdenlivetitle_wrapper.h" +#include "common.h" #include #include #include -#include #include #include #include @@ -47,7 +47,6 @@ #include #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" ); - } scene = new QGraphicsScene(); scene->setItemIndexMethod( QGraphicsScene::NoIndex ); scene->setSceneRect(0, 0, mlt_properties_get_int( properties, "width" ), mlt_properties_get_int( properties, "height" )); diff --git a/src/modules/qt/producer_qtext.cpp b/src/modules/qt/producer_qtext.cpp index ed746647..e324be36 100644 --- a/src/modules/qt/producer_qtext.cpp +++ b/src/modules/qt/producer_qtext.cpp @@ -18,45 +18,19 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include "common.h" #include #include #include #include -#include #include #include -#include #include #include #include #include #include -/** 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( 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; diff --git a/src/modules/qt/qimage_wrapper.cpp b/src/modules/qt/qimage_wrapper.cpp index 4fb64545..ee5db0b1 100644 --- a/src/modules/qt/qimage_wrapper.cpp +++ b/src/modules/qt/qimage_wrapper.cpp @@ -22,6 +22,7 @@ */ #include "qimage_wrapper.h" +#include "common.h" #ifdef USE_KDE4 #include @@ -29,11 +30,9 @@ #include #include -#include #include #include #include -#include #ifdef USE_EXIF #include @@ -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; diff --git a/src/modules/qt/transition_vqm.cpp b/src/modules/qt/transition_vqm.cpp index c926b165..1fb7db4b 100644 --- a/src/modules/qt/transition_vqm.cpp +++ b/src/modules/qt/transition_vqm.cpp @@ -18,21 +18,18 @@ * along with this program. If not, see */ +#include "common.h" #include #include #include #include -#include #include #include -#include #include #include #include #include -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 ); -- 2.39.2