src/hbservers/hbsplashgenerator/main.cpp
changeset 2 06ff229162e9
parent 1 f7ac710697a9
child 3 11d3954df52a
equal deleted inserted replaced
1:f7ac710697a9 2:06ff229162e9
    29 #include "hbsplashgenerator_p.h"
    29 #include "hbsplashgenerator_p.h"
    30 #include "hbsplashdefs_p.h"
    30 #include "hbsplashdefs_p.h"
    31 
    31 
    32 #if defined(Q_OS_SYMBIAN)
    32 #if defined(Q_OS_SYMBIAN)
    33 #include "hbsplashgen_server_symbian_p.h"
    33 #include "hbsplashgen_server_symbian_p.h"
       
    34 #include "hbsplashdefs_p.h"
    34 #include <e32std.h>
    35 #include <e32std.h>
    35 #include <eikenv.h>
    36 #include <eikenv.h>
    36 #include <apgwgnam.h>
    37 #include <apgwgnam.h>
    37 CApaApplication *appFactory()
    38 CApaApplication *appFactory()
    38 {
    39 {
    39     return new HbSplashGenApplication;
    40     return new HbSplashGenApplication;
    40 }
    41 }
    41 #endif
    42 #endif
    42 
    43 
    43 int main(int argc, char **argv)
    44 int runMain(int argc, char **argv, void *mutexToSignal)
    44 {
    45 {
    45 #if defined(Q_OS_SYMBIAN)
    46 #if defined(Q_OS_SYMBIAN)
    46     HbApplication app(appFactory, argc, argv, Hb::NoSplash);
    47     HbApplication app(appFactory, argc, argv, Hb::NoSplash);
    47     CEikonEnv *env = CEikonEnv::Static();
    48     CEikonEnv *env = CEikonEnv::Static();
    48     if (env) {
    49     if (env) {
    55         wgName->SetWindowGroupName(env->RootWin());
    56         wgName->SetWindowGroupName(env->RootWin());
    56         CleanupStack::PopAndDestroy();
    57         CleanupStack::PopAndDestroy();
    57         RThread::RenameMe(hbsplash_server_name);
    58         RThread::RenameMe(hbsplash_server_name);
    58     }
    59     }
    59 #else
    60 #else
       
    61     Q_UNUSED(mutexToSignal);
    60     HbApplication app(argc, argv, Hb::NoSplash);
    62     HbApplication app(argc, argv, Hb::NoSplash);
    61 #endif
    63 #endif
    62 
    64 
    63     qDebug("[hbsplashgenerator] initializing generator");
    65     qDebug("[hbsplashgenerator] initializing generator");
    64     HbSplashGenerator gen;
    66     HbSplashGenerator gen;
    75     app.setQuitOnLastWindowClosed(false);
    77     app.setQuitOnLastWindowClosed(false);
    76 #else
    78 #else
    77     qDebug("[hbsplashgenerator] initializing ui");
    79     qDebug("[hbsplashgenerator] initializing ui");
    78     QMainWindow mw;
    80     QMainWindow mw;
    79     QPushButton *btnRegen = new QPushButton("Regenerate");
    81     QPushButton *btnRegen = new QPushButton("Regenerate");
    80     gen.connect(btnRegen, SIGNAL(clicked()), SLOT(regenerate()));
    82     gen.connect(btnRegen, SIGNAL(clicked()), SLOT(uncachedRegenerate()));
    81     mw.setCentralWidget(btnRegen);
    83     mw.setCentralWidget(btnRegen);
    82     mw.show();
    84     mw.show();
    83 #endif
    85 #endif
    84 
    86 
    85     // The server must be initialized before calling HbSplashGenerator::start().
    87     // The server must be initialized before calling HbSplashGenerator::start().
    86 #ifdef Q_OS_SYMBIAN
    88 #ifdef Q_OS_SYMBIAN
    87     qDebug("[hbsplashgenerator] starting server");
    89     qDebug("[hbsplashgenerator] starting server");
    88     HbSplashGenServer server(&gen);
    90     HbSplashGenServer server(&gen);
       
    91     // If there was an error (or an instance is already running (it is
       
    92     // possible in certain race conditions)) then exit right away.
       
    93     if (!server.startupSuccess()) {
       
    94         qDebug("[hbsplashgenerator] exiting due to failed server startup");
       
    95         return 0;
       
    96     }
    89 #endif
    97 #endif
    90 
    98 
    91     qDebug("[hbsplashgenerator] starting generator");
    99     qDebug("[hbsplashgenerator] starting generator");
    92     gen.start(forceRegen);
   100     gen.start(forceRegen);
       
   101 
       
   102 #if defined(Q_OS_SYMBIAN)
       
   103     if (mutexToSignal) {
       
   104         qDebug("[hbsplashgenerator] signaling mutex");
       
   105         static_cast<RMutex *>(mutexToSignal)->Signal();
       
   106     }
       
   107 #endif
    93 
   108 
    94     qDebug("[hbsplashgenerator] entering event loop");
   109     qDebug("[hbsplashgenerator] entering event loop");
    95     int ret = app.exec();
   110     int ret = app.exec();
    96 
   111 
    97     qDebug("[hbsplashgenerator] exiting");
   112     qDebug("[hbsplashgenerator] exiting");
    98     return ret;
   113     return ret;
    99 }
   114 }
       
   115 
       
   116 int main(int argc, char **argv)
       
   117 {
       
   118     bool canStart = true;
       
   119     void *mutexToSignal = 0;
       
   120 #if defined(Q_OS_SYMBIAN)
       
   121     RMutex mutex;
       
   122     _LIT(KGlobalLockName, "hbsplgen");
       
   123     bool mutexOk = mutex.OpenGlobal(KGlobalLockName) == KErrNone;
       
   124     if (!mutexOk) {
       
   125         mutexOk = mutex.CreateGlobal(KGlobalLockName) == KErrNone;
       
   126     }
       
   127     if (mutexOk) {
       
   128         mutex.Wait();
       
   129         mutexToSignal = &mutex;
       
   130     } else {
       
   131         qDebug("[hbsplashgenerator] failed to open/create mutex");
       
   132     }
       
   133     // Due to the mutex-based synchronization we should now be able to
       
   134     // use TFindServer to safely check if there is another instance
       
   135     // already running.
       
   136     TFindServer findServer(hbsplash_server_name);
       
   137     TFullName serverName;
       
   138     if (findServer.Next(serverName) == KErrNone) {
       
   139         qDebug("[hbsplashgenerator] found another instance, exiting");
       
   140         canStart = false;
       
   141         RProcess::Rendezvous(KErrAlreadyExists);
       
   142     }
       
   143 #endif
       
   144 
       
   145     int result = 0;
       
   146     if (canStart) {
       
   147         try {
       
   148             result = runMain(argc, argv, mutexToSignal);
       
   149         } catch (...) {
       
   150             qDebug("[hbsplashgenerator] exiting due to exception in main()");
       
   151         }
       
   152     }
       
   153 
       
   154 #if defined(Q_OS_SYMBIAN)
       
   155     if (mutexOk) {
       
   156         if (mutex.IsHeld()) {
       
   157             mutex.Signal();
       
   158         }
       
   159         mutex.Close();
       
   160     }
       
   161 #endif
       
   162 
       
   163     return result;
       
   164 }