Bug 693422 - Unify QApplication creation. r=tatiana
authorOleg Romashin <romaxa@gmail.com>
Mon, 17 Oct 2011 17:53:37 -0700
changeset 80219 09c76e77d57ccd8dbea396fb61d38c6d1fb2a6cf
parent 80218 b1561460dffb8f9c1e4062eb613e0d267c06679e
child 80220 9e14543feb118e317535af6b85da405bfe7223b0
push idunknown
push userunknown
push dateunknown
reviewerstatiana
bugs693422
milestone10.0a1
Bug 693422 - Unify QApplication creation. r=tatiana
toolkit/xre/nsAppRunner.cpp
toolkit/xre/nsQAppInstance.cpp
toolkit/xre/nsQAppInstance.h
widget/src/qt/moziqwidget.h
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -45,23 +45,19 @@
 // os2safe.h has to be included before os2.h, needed for high mem
 #include <os2safe.h>
 #endif
 
 #define XPCOM_TRANSLATE_NSGM_ENTRY_POINT 1
 
 #if defined(MOZ_WIDGET_QT)
 #include <QtGui/QApplication>
-#include <QtCore/QScopedPointer>
+#include "nsQAppInstance.h"
 #include <QtGui/QInputContextFactory>
 #include <QtGui/QInputContext>
-#ifdef MOZ_ENABLE_MEEGOTOUCH
-#include <MComponentData>
-#include <MozMeegoAppService.h>
-#endif // MOZ_ENABLE_MEEGOTOUCH
 #endif // MOZ_WIDGET_QT
 
 #include "mozilla/dom/ContentParent.h"
 #include "mozilla/dom/ContentChild.h"
 
 #include "mozilla/Util.h"
 
 #include "nsAppRunner.h"
@@ -3017,41 +3013,31 @@ XRE_main(int argc, char* argv[], const n
     const char* desktopStartupIDEnv = PR_GetEnv("DESKTOP_STARTUP_ID");
     nsCAutoString desktopStartupID;
     if (desktopStartupIDEnv) {
       desktopStartupID.Assign(desktopStartupIDEnv);
     }
 #endif
 
 #if defined(MOZ_WIDGET_QT)
-    const char* qgraphicssystemARG = NULL;
-    ar = CheckArg("graphicssystem", true, &qgraphicssystemARG, false);
-    if (ar == ARG_FOUND)
-      PR_SetEnv(PR_smprintf("MOZ_QT_GRAPHICSSYSTEM=%s", qgraphicssystemARG));
-
-    QScopedPointer<QApplication> app(new QApplication(gArgc, gArgv));
-#ifdef MOZ_ENABLE_MEEGOTOUCH
-    gArgv[gArgc] = strdup("-software");
-    gArgc++;
-    QScopedPointer<MComponentData> meegotouch(new MComponentData(gArgc, gArgv,"", new  MApplicationService("")));
-#endif
+    nsQAppInstance::AddRef(gArgc, gArgv, true);
 
 #if MOZ_PLATFORM_MAEMO > 5
     if (XRE_GetProcessType() == GeckoProcessType_Default) {
       // try to get the MInputContext if possible to support the MeeGo VKB
-      QInputContext* inputContext = app->inputContext();
+      QInputContext* inputContext = qApp->inputContext();
       if (inputContext && inputContext->identifierName() != "MInputContext") {
           QInputContext* context = QInputContextFactory::create("MInputContext",
-                                                                app.data());
+                                                                qApp);
           if (context)
-              app->setInputContext(context);
+              qApp->setInputContext(context);
       }
     }
 #endif
-    QStringList nonQtArguments = app->arguments();
+    QStringList nonQtArguments = qApp->arguments();
     gQtOnlyArgc = 1;
     gQtOnlyArgv = (char**) malloc(sizeof(char*) 
                   * (gRestartArgc - nonQtArguments.size() + 2));
 
     // copy binary path
     gQtOnlyArgv[0] = gRestartArgv[0];
 
     for (int i = 1; i < gRestartArgc; ++i) {
@@ -3613,16 +3599,20 @@ XRE_main(int argc, char* argv[], const n
 
       }
     }
 
     // unlock the profile after ScopedXPCOMStartup object (xpcom) 
     // has gone out of scope.  see bug #386739 for more details
     profileLock->Unlock();
 
+#if defined(MOZ_WIDGET_QT)
+    nsQAppInstance::Release();
+#endif
+
     // Restart the app after XPCOM has been shut down cleanly. 
     if (appInitiatedRestart) {
       RestoreStateForAppInitiatedRestart();
 
       // Ensure that these environment variables are set:
       SaveFileToEnvIfUnset("XRE_PROFILE_PATH", profD);
       SaveFileToEnvIfUnset("XRE_PROFILE_LOCAL_PATH", profLD);
       SaveWordToEnvIfUnset("XRE_PROFILE_NAME", profileName);
--- a/toolkit/xre/nsQAppInstance.cpp
+++ b/toolkit/xre/nsQAppInstance.cpp
@@ -33,41 +33,69 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "nsQAppInstance.h"
 #include <QApplication>
+#ifdef MOZ_ENABLE_MEEGOTOUCH
+#include <MComponentData>
+#include <MApplicationService>
+#endif
 #include "prenv.h"
-
-// declared in nsAppRunner.cpp
-extern int    gArgc;
-extern char **gArgv;
+#include <stdlib.h>
 
 QApplication *nsQAppInstance::sQAppInstance = NULL;
+#ifdef MOZ_ENABLE_MEEGOTOUCH
+MComponentData* nsQAppInstance::sMComponentData = NULL;
+#endif
 int nsQAppInstance::sQAppRefCount = 0;
 
-void nsQAppInstance::AddRef(void) {
-  if (qApp) return;
+void nsQAppInstance::AddRef(int& aArgc, char** aArgv, bool aDefaultProcess) {
+  if (qApp)
+    return;
   if (!sQAppInstance) {
-    const char *graphicsSystem = PR_GetEnv("MOZ_QT_GRAPHICSSYSTEM");
-    if (graphicsSystem)
+    const char *graphicsSystem = getenv("MOZ_QT_GRAPHICSSYSTEM");
+    if (graphicsSystem) {
       QApplication::setGraphicsSystem(QString(graphicsSystem));
+    }
 #if (MOZ_PLATFORM_MAEMO == 6)
-    QApplication::setStyle(QLatin1String("windows"));
-    if (!gArgc) {
-      gArgv[gArgc] = strdup("nsQAppInstance");
-      gArgc++;
+    // Should create simple windows style for non chrome process
+    // otherwise meegotouch style initialize and crash happen
+    // because we don't initialize MComponent for child process
+    if (!aDefaultProcess) {
+      QApplication::setStyle(QLatin1String("windows"));
+    }
+    if (!aArgc) {
+      aArgv[aArgc] = strdup("nsQAppInstance");
+      aArgc++;
     }
 #endif
-    sQAppInstance = new QApplication(gArgc, gArgv);
+    sQAppInstance = new QApplication(aArgc, aArgv);
+#ifdef MOZ_ENABLE_MEEGOTOUCH
+    if (aDefaultProcess) {
+      // GLContext created by meegotouch will be under meego graphics
+      // system control, and will drop GL context automatically in background mode
+      // In order to use that GLContext we need to implement
+      // LayerManager switch in runtime from SW<->HW
+      // That not yet implemented so we need to control GL context,
+      // force software mode for, and create our own QGLWidget
+      gArgv[gArgc] = strdup("-software");
+      gArgc++;
+      sMComponentData = new MComponentData(aArgc, aArgv, "", new MApplicationService(""));
+    }
+#endif
   }
   sQAppRefCount++;
 }
 
 void nsQAppInstance::Release(void) {
   if (sQAppInstance && !--sQAppRefCount) {
+#ifdef MOZ_ENABLE_MEEGOTOUCH
+    delete sMComponentData;
+    sMComponentData = NULL;
+#endif
     delete sQAppInstance;
     sQAppInstance = NULL;
   }
 }
--- a/toolkit/xre/nsQAppInstance.h
+++ b/toolkit/xre/nsQAppInstance.h
@@ -34,22 +34,29 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #ifndef nsQAppInstance_h
 #define nsQAppInstance_h
 
-#include <QApplication>
+// declared in nsAppRunner.cpp
+extern int    gArgc;
+extern char **gArgv;
 
+class QApplication;
+class MComponentData;
 class nsQAppInstance
 {
 public:
-  static void AddRef(void);
+  static void AddRef(int& aArgc = gArgc,
+                     char** aArgv = gArgv,
+                     bool aDefaultProcess = false);
   static void Release(void);
 
 private:
   static QApplication *sQAppInstance;
+  static MComponentData* sMComponentData;
   static int sQAppRefCount;
 };
 
 #endif /* nsQAppInstance_h */