Home · All Classes · Main Classes · Deprecated

mcomponentcache.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  **
00003  ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004  ** All rights reserved.
00005  ** Contact: Nokia Corporation (directui@nokia.com)
00006  **
00007  ** This file is part of libmeegotouch.
00008  **
00009  ** If you have questions regarding the use of this file, please contact
00010  ** Nokia at directui@nokia.com.
00011  **
00012  ** This library is free software; you can redistribute it and/or
00013  ** modify it under the terms of the GNU Lesser General Public
00014  ** License version 2.1 as published by the Free Software Foundation
00015  ** and appearing in the file LICENSE.LGPL included in the packaging
00016  ** of this file.
00017  **
00018  ****************************************************************************/
00019 
00020 #ifdef QT_OPENGL_LIB
00021 #include "mgles2renderer.h"
00022 #endif
00023 
00024 #include "mcomponentcache.h"
00025 #include "mcomponentcache_p.h"
00026 #include "mapplication.h"
00027 #include "mapplicationservice.h"
00028 #include "mapplicationwindow.h"
00029 #include "mcomponentdata.h"
00030 #include "mgraphicssystemhelper.h"
00031 
00032 #include "mthemedaemon.h"
00033 
00034 #ifdef QT_OPENGL_LIB
00035 #include <QGLFormat>
00036 #include <QGLWidget>
00037 #endif
00038 
00039 #ifdef Q_WS_X11
00040 #include <X11/Xlib.h>
00041 #endif
00042 
00043 MComponentCachePrivate * const MComponentCache::d_ptr = new MComponentCachePrivate;
00044 const int MComponentCachePrivate::ARGV_LIMIT = 32;
00045 
00046 MComponentCachePrivate::MComponentCachePrivate() :
00047     mApplicationInstance(0),
00048     mApplicationWindowInstance(0),
00049     glWidgetOfmApplicationWindowInstance(0),
00050     cacheBeingPopulated(false),
00051     initialArgc(ARGV_LIMIT),
00052     initialArgv(new char* [initialArgc])
00053 {
00054 }
00055 
00056 MComponentCachePrivate::~MComponentCachePrivate()
00057 {
00058 #ifdef QT_OPENGL_LIB
00059     delete glWidgetOfmApplicationWindowInstance;
00060 #endif
00061     delete mApplicationWindowInstance;
00062     delete[] initialArgv;
00063 
00064     MComponentCache::cleanupCache();
00065 }
00066 
00067 bool MComponentCachePrivate::populating()
00068 {
00069     return cacheBeingPopulated;
00070 }
00071 
00072 void MComponentCachePrivate::populateForMApplication()
00073 {
00074     static const char *const appName = "componentcache_pre_initialized_mapplication";
00075     static const char *const emptyString = "";
00076 
00077     cacheBeingPopulated = true;
00078 
00079     // We support at most ARGV_LIMIT arguments in QCoreApplication. These will be set when real
00080     // arguments are known (in MComponentCachePrivate::mApplication). 
00081     initialArgv[0] = const_cast<char *>(appName);
00082     for (int i = 1; i < initialArgc; i++) {
00083         initialArgv[i] = const_cast<char *>(emptyString);
00084     }
00085 
00086     if (mApplicationInstance == 0) {
00087         mApplicationInstance = new MApplication(initialArgc, initialArgv, 0);
00088     } else {
00089         cacheBeingPopulated = false;
00090         qFatal("MComponentCache::populateForMApplication() - Cache is already populated.");
00091     }
00092 
00093     if (MGraphicsSystemHelper::isRunningNativeGraphicssystem()) {
00094         glWidgetOfmApplicationWindowInstance = createNewGlWidget();
00095     }
00096 
00097     mApplicationWindowInstance = new MApplicationWindow();
00098 
00099     cacheBeingPopulated = false;
00100 }
00101 
00102 void MComponentCachePrivate::populateForWRTApplication()
00103 {
00104     cacheBeingPopulated = true; // reset and unset in populateForMApplication
00105     MApplication::setGraphicsSystem("raster");
00106     populateForMApplication();
00107 }
00108 
00109 QGLWidget* MComponentCachePrivate::createNewGlWidget(const QGLFormat* format)
00110 {
00111 #ifdef QT_OPENGL_LIB
00112     QGLFormat fmt;
00113 
00114     if (!format) {
00115         // disable multisampling, is enabled by default in Qt
00116         fmt.setSampleBuffers(false);
00117         fmt.setSamples(0);
00118     }
00119     else {
00120         fmt = *format;
00121     }
00122 
00123     QGLWidget* shareWidget = NULL;
00124     foreach(FormatWidgetPair pair, shareWidgetsCache) {
00125         if (pair.first == fmt)  {
00126             shareWidget = pair.second;
00127             break;
00128         }
00129     }
00130 
00131     if (!shareWidget) {
00132        shareWidget = new QGLWidget(fmt);
00133        shareWidgetsCache.append(qMakePair(fmt, shareWidget));
00134     }
00135 
00136     return new QGLWidget(fmt, NULL, shareWidget);
00137 #else
00138     Q_UNUSED(format);
00139     return 0;
00140 #endif
00141 }
00142 
00143 MApplication* MComponentCachePrivate::mApplication(int &argc, char **argv, const QString &appIdentifier, MApplicationService *service)
00144 {
00145     if (mApplicationInstance == 0) {
00146         mApplicationInstance = new MApplication(argc, argv, appIdentifier, service);
00147     } else {
00148         if (canUseCachedApp(argc, argv, appIdentifier)) {
00149             if(argc > ARGV_LIMIT) {
00150                 qWarning("MComponentCache: QCoreApplication::arguments() will not contain all arguments.");
00151             }
00152 
00153             // Copy arguments to QCoreApplication 
00154             for (int i = 0; i < qMin(argc, ARGV_LIMIT); i++) {
00155                 qApp->argv()[i] = argv[i];
00156             }
00157 
00158             // This changes argc in QCoreApplication
00159             initialArgc = qMin(argc, ARGV_LIMIT);
00160 
00161             MComponentData::instance()->reinit(argc, argv, appIdentifier, service);
00162 
00163 #ifdef Q_WS_X11
00164             // reinit WM_COMMAND X11 property
00165             if (mApplicationWindowInstance) {
00166                 Display *display = QX11Info::display();
00167                 if (display) {
00168                     XSetCommand(display, mApplicationWindowInstance->effectiveWinId(), argv, argc);
00169                 }
00170             }
00171 #endif
00172             /* Unregister MApplicationWindow for the case that boosted
00173                application instantiates and uses an MApplicationWindow
00174                of its own. In that case application's window must be
00175                MApplication::activeWindow() as well. On the other
00176                hand, if application takes mApplicationWindowInstance
00177                from the cache, it will be re-registered then.
00178              */
00179             MComponentData::unregisterWindow(mApplicationWindowInstance);
00180 
00181         } else {
00182             /* Clean up cache.
00183                
00184                Deleting mApplicationInstance without deleting
00185                mApplicationWindowInstance first causes a
00186                crash. Therefore we cannot save the existing
00187                mApplicationWindowInstance to be used in new
00188                mApplication. 
00189             */
00190             if (mApplicationWindowInstance) {
00191                 delete mApplicationWindowInstance;
00192                 mApplicationWindowInstance = 0;
00193             }
00194 
00195             delete mApplicationInstance;
00196             mApplicationInstance = new MApplication(argc, argv, appIdentifier, service);
00197         }
00198     }
00199     return mApplicationInstance;
00200 }
00201 
00202 bool MComponentCachePrivate::canUseCachedApp(int &argc, char **argv, const QString &appIdentifier)
00203 {
00204     if ( hasExtraParams(argc, argv, appIdentifier) )
00205         return false;
00206 
00207     return true;
00208 }
00209 
00210 bool MComponentCachePrivate::hasExtraParams(int &argc, char **argv, const QString &appIdentifier)
00211 {
00212     Q_UNUSED(appIdentifier);
00213 
00214     for (int i = 1; i < argc; ++i) 
00215     {
00216         QString s(argv[i]);
00217 
00218         if (
00219             /* QApplication command options */
00220             (s == "-style")                       ||
00221             (s == "-stylesheet")                  ||
00222             (s == "-session")                     ||
00223             (s == "-widgetcount")                 ||
00224             (s == "-reverse")                     ||
00225             (s == "-graphicssystem")              ||
00226 
00227             /* X11 options */
00228             (s == "-display")                     ||
00229             (s == "-geometry" )                   ||
00230             (s == "-fn" )                         ||
00231             (s == "-font")                        ||
00232             (s == "-bg")                          ||
00233             (s == "-background")                  ||
00234             (s == "-fg")                          ||
00235             (s == "-foreground")                  ||
00236             (s == "-btn")                         ||
00237             (s == "-button")                      ||
00238             (s == "-name" )                       ||
00239             (s == "-title" )                      ||
00240             (s == "-visual" )                     ||
00241             (s == "-ncols")                       ||
00242             (s == "-cmap")                        ||
00243             (s == "-im")                          ||
00244             (s == "-inputstyle" )                 ||
00245 
00246             /* help options, application will not be started */
00247             (s == "-v")                           ||
00248             (s.startsWith("-version"))            ||
00249             (s.startsWith("--version"))           ||
00250             (s == "-h")                           ||
00251             (s.startsWith("-help"))               ||
00252             (s.startsWith("--help"))
00253            )
00254             return true;
00255     }
00256     return false;
00257 }
00258 
00259 MApplicationWindow* MComponentCachePrivate::mApplicationWindow()
00260 {
00261     MApplicationWindow *returnValue;
00262     if (mApplicationWindowInstance != 0) {
00263         returnValue = mApplicationWindowInstance;
00264         mApplicationWindowInstance = 0;
00265         MComponentData::registerWindow(returnValue);
00266     } else {
00267         returnValue = new MApplicationWindow();
00268     }
00269     return returnValue;
00270 }
00271 
00272 QGLWidget* MComponentCachePrivate::glWidget(const QGLFormat* format)
00273 {
00274 #ifdef QT_OPENGL_LIB
00275     QGLWidget *returnValue;
00276     if (glWidgetOfmApplicationWindowInstance != 0
00277         && (!format || glWidgetOfmApplicationWindowInstance->format() == *format))
00278     {
00279         returnValue = glWidgetOfmApplicationWindowInstance;
00280         glWidgetOfmApplicationWindowInstance = 0;
00281     } else {
00282         returnValue = createNewGlWidget(format);
00283     }
00284     return returnValue;
00285 #else
00286     Q_UNUSED(format);
00287     return 0;
00288 #endif
00289 }
00290 
00291 MComponentCache::MComponentCache()
00292 {
00293 }
00294 
00295 void MComponentCache::populateForMApplication()
00296 {
00297     d_ptr->populateForMApplication();
00298 }
00299 
00300 void MComponentCache::populateForWRTApplication()
00301 {
00302     d_ptr->populateForWRTApplication();
00303 }
00304 
00305 bool MComponentCache::populating()
00306 {
00307     return d_ptr->populating();
00308 }
00309 
00310 void MComponentCache::cleanupCache()
00311 {
00312 #ifdef QT_OPENGL_LIB
00313     while (!d_ptr->shareWidgetsCache.isEmpty()) {
00314         MComponentCachePrivate::FormatWidgetPair pair = d_ptr->shareWidgetsCache.takeFirst();
00315         delete pair.second;
00316     }
00317 #endif
00318 }
00319 
00320 MApplication* MComponentCache::mApplication(int &argc, char **argv, const QString &appIdentifier, MApplicationService *service)
00321 {    
00322     return d_ptr->mApplication(argc, argv, appIdentifier, service);
00323 }
00324 
00325 MApplicationWindow* MComponentCache::mApplicationWindow()
00326 {
00327     return d_ptr->mApplicationWindow();
00328 }
00329 
00330 QGLWidget* MComponentCache::glWidget()
00331 {
00332     return d_ptr->glWidget();
00333 }
00334 
00335 QGLWidget* MComponentCache::glWidget(const QGLFormat& format)
00336 {
00337     return d_ptr->glWidget(&format);
00338 }

Copyright © 2010 Nokia Corporation Generated on Thu Nov 4 2010 18:14:20 (PDT)
Doxygen 1.7.1
MeeGo Touch