Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

main.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 ** Copyright (C) 2003-2005 The Opie Team <opie-devel@handhelds.org>
00004 **
00005 ** This file may be distributed and/or modified under the terms of the
00006 ** GNU General Public License version 2 as published by the Free Software
00007 ** Foundation and appearing in the file LICENSE.GPL included in the
00008 ** packaging of this file.
00009 **
00010 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00011 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00012 **********************************************************************/
00013 
00014 #ifndef QTOPIA_INTERNAL_FILEOPERATIONS
00015 #define QTOPIA_INTERNAL_FILEOPERATIONS
00016 #endif
00017 #ifdef QT_QWS_LOGIN
00018 #include "../login/qdmdialogimpl.h"
00019 #endif
00020 #include "calibrate.h"
00021 #include "server.h"
00022 #include "serverapp.h"
00023 #include "stabmon.h"
00024 #include "firstuse.h"
00025 
00026 /* OPIE */
00027 #include <opie2/odebug.h>
00028 #include <opie2/odevice.h>
00029 #include <opie2/oglobal.h>
00030 #include <qtopia/network.h>
00031 #include <qtopia/alarmserver.h>
00032 using namespace Opie::Core;
00033 
00034 /* QT */
00035 #include <qdir.h>
00036 #include <qmessagebox.h>
00037 #ifdef QWS
00038 #include <qwindowsystem_qws.h>
00039 #include <qtopia/qcopenvelope_qws.h>
00040 #endif
00041 #ifdef Q_WS_QWS
00042 #include <qkeyboard_qws.h>
00043 #endif
00044 
00045 /* STD */
00046 #include <stdlib.h>
00047 #include <stdio.h>
00048 #include <signal.h>
00049 #include <unistd.h>
00050 #include <errno.h>
00051 #include <string.h>
00052 
00053 void create_pidfile();
00054 void remove_pidfile();
00055 
00056 static void cleanup()
00057 {
00058     QDir dir( "/tmp", "qcop-msg-*" );
00059 
00060     QStringList stale = dir.entryList();
00061     QStringList::Iterator it;
00062     for ( it = stale.begin(); it != stale.end(); ++it ) {
00063     dir.remove( *it );
00064     }
00065 }
00066 
00067 void initEnvironment()
00068 {
00069     Config config("locale");
00070     config.setGroup( "Location" );
00071     QString tz = config.readEntry( "Timezone", getenv("TZ") ).stripWhiteSpace();
00072 
00073     // timezone
00074     if (tz.isNull() || tz.isEmpty()) tz = "America/New_York";
00075     setenv( "TZ", tz, 1 );
00076     config.writeEntry( "Timezone", tz);
00077 
00078     // language
00079     config.setGroup( "Language" );
00080     QString lang = config.readEntry( "Language", getenv("LANG") ).stripWhiteSpace();
00081     if( lang.isNull() || lang.isEmpty()) lang = "en_US";
00082     setenv( "LANG", lang, 1 );
00083     config.writeEntry("Language", lang);
00084     config.write();
00085 
00086     // rotation
00087     int t = ODevice::inst()->rotation();
00088     odebug << "ODevice reports transformation to be " << t << oendl;
00089 
00090     QString env( getenv("QWS_DISPLAY") );
00091     if ( env.isEmpty() )
00092     {
00093             int rot = ODevice::inst()->rotation() * 90;
00094             QString qws_display = QString( "%1:Rot%2:0").arg(ODevice::inst()->qteDriver()).arg(rot);
00095             odebug << "setting QWS_DISPLAY to '" << qws_display << "'" << oendl;
00096             setenv("QWS_DISPLAY", (const char*) qws_display, 1);
00097     }
00098     else
00099         odebug << "QWS_DISPLAY already set as '" << env << "' - overriding ODevice transformation" << oendl;
00100 
00101     QPEApplication::defaultRotation(); /* to ensure deforient matches reality */
00102 }
00103 
00104 static void initKeyboard()
00105 {
00106     Config config("qpe");
00107 
00108     config.setGroup( "Keyboard" );
00109 
00110     int ard = config.readNumEntry( "RepeatDelay" );
00111     int arp = config.readNumEntry( "RepeatPeriod" );
00112     if ( ard > 0 && arp > 0 )
00113     qwsSetKeyboardAutoRepeat( ard, arp );
00114 
00115     QString layout = config.readEntry( "Layout", "us101" );
00116     Server::setKeyboardLayout( layout );
00117 }
00118 
00119 static bool firstUse()
00120 {
00121     bool needFirstUse = FALSE;
00122     if ( QWSServer::mouseHandler() &&
00123          QWSServer::mouseHandler() ->inherits("QCalibratedMouseHandler") ) {
00124         if ( !QFile::exists( "/etc/pointercal" ) )
00125             needFirstUse = TRUE;
00126     }
00127 
00128     {
00129     Config config( "qpe" );
00130     config.setGroup( "Startup" );
00131     needFirstUse |= config.readBoolEntry( "FirstUse", TRUE );
00132     }
00133 
00134     if ( !needFirstUse )
00135     return FALSE;
00136 
00137     FirstUse *fu = new FirstUse();
00138     fu->exec();
00139     bool rs = fu->restartNeeded();
00140     delete fu;
00141     return rs;
00142 }
00143 
00144 int initApplication( int argc, char ** argv )
00145 {
00146     cleanup();
00147     initEnvironment();
00148 
00149 #ifdef QWS
00150     QWSServer::setDesktopBackground( QImage() );
00151 #endif
00152     ServerApplication a( argc, argv, QApplication::GuiServer );
00153     initKeyboard();
00154 
00155     if ( firstUse() )
00156     {
00157         a.restart();
00158         return 0;
00159     }
00160 
00161     {
00162         QCopEnvelope e("QPE/System", "setBacklight(int)" );
00163         e << -3; // Forced on
00164     }
00165 
00166     AlarmServer::initialize();
00167     Server *s = new Server();
00168     new SysFileMonitor(s);
00169 #ifdef QWS
00170     Network::createServer(s);
00171 #endif
00172     s->show();
00173 
00174 #if 0
00175     if ( QDate::currentDate().year() < 2005 )
00176     {
00177         if ( QMessageBox::information ( 0, ServerApplication::tr( "Information" ),
00178              ServerApplication::tr( "<p>The system date doesn't seem to be valid.\n(%1)</p><p>Do you want to correct the clock ?</p>" )
00179                                .arg( TimeString::dateString( QDate::currentDate())), QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )
00180             {
00181                 QCopEnvelope e ( "QPE/Application/systemtime", "setDocument(QString)" );
00182                 e << QString ( );
00183             }
00184     }
00185 #endif
00186 
00187     create_pidfile();
00188     odebug << "--> mainloop in" << oendl;
00189     int rv = a.exec();
00190     odebug << "<-- mainloop out" << oendl;
00191     remove_pidfile();
00192     odebug << "removing server object..." << oendl;
00193     delete s;
00194 
00195     odebug << "returning from qpe/initapplication..." << oendl;
00196     return rv;
00197 }
00198 
00199 static const char *pidfile_path = "/var/run/opie.pid";
00200 
00201 void create_pidfile()
00202 {
00203     FILE *f;
00204 
00205     if (( f = ::fopen( pidfile_path, "w" ))) {
00206         ::fprintf( f, "%d", getpid ( ));
00207         ::fclose( f );
00208     }
00209     else
00210     {
00211         odebug << "couldn't create pidfile: " << strerror( errno ) << oendl;
00212     }
00213 }
00214 
00215 void remove_pidfile()
00216 {
00217     ::unlink( pidfile_path );
00218 }
00219 
00220 void handle_sigterm( int sig )
00221 {
00222     qDebug( "D'oh! QPE Server process got SIGNAL %d. Trying to exit gracefully...", sig );
00223     ::signal( SIGCHLD, SIG_IGN );
00224     ::signal( SIGSEGV, SIG_IGN );
00225     ::signal( SIGBUS, SIG_IGN );
00226     ::signal( SIGILL, SIG_IGN );
00227     ::signal( SIGTERM, SIG_IGN );
00228     ::signal ( SIGINT, SIG_IGN );
00229     if ( qApp ) qApp->quit();
00230 }
00231 
00232 int main( int argc, char ** argv )
00233 {
00234     ::signal( SIGCHLD, SIG_IGN );
00235     ::signal( SIGSEGV, handle_sigterm );
00236     ::signal( SIGBUS, handle_sigterm );
00237     ::signal( SIGILL, handle_sigterm );
00238     ::signal( SIGTERM, handle_sigterm );
00239     ::signal ( SIGINT, handle_sigterm );
00240     ::setsid();
00241     ::setpgid( 0, 0 );
00242 
00243     ::atexit( remove_pidfile );
00244 
00245     int retVal = initApplication( argc, argv );
00246 
00247     // Have we been asked to restart?
00248     if ( ServerApplication::doRestart )
00249     {
00250         for ( int fd = 3; fd < 100; fd++ ) close( fd );
00251         execl( (QPEApplication::qpeDir()+"bin/qpe").latin1(), "qpe", 0 );
00252     }
00253 
00254     return retVal;
00255 }

Generated on Sat Nov 5 16:15:25 2005 for OPIE by  doxygen 1.4.2