summaryrefslogtreecommitdiff
path: root/core/launcher/main.cpp
blob: 69a7feaadb3d8da3fa1dc86ba4a37baec40ca93f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include "desktop.h"
#include "taskbar.h"
#include "stabmon.h"

#include <qpe/qpeapplication.h>
#include <qpe/network.h>
#include <qpe/config.h>
#if defined( QT_QWS_SL5XXX ) || defined( QT_QWS_IPAQ ) || defined (QT_QWS_RAMSES)
#include <qpe/custom.h>
#endif

#include <opie/odevice.h>

#include <qmessagebox.h>
#include <qfile.h>
#include <qimage.h>
#include <qwindowsystem_qws.h>
#include <qwsmouse_qws.h>
#include <qpe/qcopenvelope_qws.h>
#include <qpe/alarmserver.h>

#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>

#include "../calibrate/calibrate.h"

using namespace Opie;

void initEnvironment()
{
    int rot;
    Config config("locale");
    
    config.setGroup( "Location" );
    QString tz = config.readEntry( "Timezone", getenv("TZ") );

  // if not timezone set, pick New York
    if (tz.isNull())
        tz = "America/New_York";

    setenv( "TZ", tz, 1 );
    config.writeEntry( "Timezone", tz);

    config.setGroup( "Language" );
    QString lang = config.readEntry( "Language", getenv("LANG") );
    if ( !lang.isNull() )
        setenv( "LANG", lang, 1 );

#if !defined(QT_QWS_CASSIOPEIA) && !defined(QT_QWS_IPAQ) && !defined(QT_QWS_SL5XXX)
    setenv( "QWS_SIZE", "240x320", 0 );
#endif

    QString env(getenv("QWS_DISPLAY"));
    if (env.contains("Transformed")) {
        // transformed driver default rotation is controlled by the hardware.
        Config config("qpe");
        config.setGroup( "Rotation" );
        if ( ( rot = config.readNumEntry( "Rot", -1 ) ) == -1 )
            rot = ODevice::inst ( )-> rotation ( ) * 90;

        setenv("QWS_DISPLAY", QString("Transformed:Rot%1:0").arg(rot), 1);
        QPEApplication::defaultRotation ( ); /* to ensure deforient matches reality */
    }
}


int initApplication( int argc, char ** argv )
{
    initEnvironment();

    //Don't flicker at startup:
    QWSServer::setDesktopBackground( QImage() );

    DesktopApplication a( argc, argv, QApplication::GuiServer );

    ODevice::inst ( )-> setSoftSuspend ( true );

    { // init backlight
        QCopEnvelope e("QPE/System", "setBacklight(int)" );
  	e << -3; // Forced on
    }

    AlarmServer::initialize();

    Desktop *d = new Desktop();

    QObject::connect( &a, SIGNAL(power()),   d, SLOT(togglePower()) );
    QObject::connect( &a, SIGNAL(backlight()),   d, SLOT(toggleLight()) );
    QObject::connect( &a, SIGNAL(symbol()),   d, SLOT(toggleSymbolInput()) );
    QObject::connect( &a, SIGNAL(numLockStateToggle()),   d, SLOT(toggleNumLockState()) );
    QObject::connect( &a, SIGNAL(capsLockStateToggle()),   d, SLOT(toggleCapsLockState()) );
    QObject::connect( &a, SIGNAL(prepareForRestart()),   d, SLOT(terminateServers()) );

    (void)new SysFileMonitor(d);
    Network::createServer(d);

    if ( QWSServer::mouseHandler() ->inherits("QCalibratedMouseHandler") ) {
	if ( !QFile::exists( "/etc/pointercal" ) ) {
	    // Make sure calibration widget starts on top.
	    Calibrate *cal = new Calibrate;
	    cal->exec();
	    delete cal;
	}
    }

    d->show();

	if ( QDate::currentDate ( ). year ( ) < 2000 ) {
		if ( QMessageBox::information ( 0, DesktopApplication::tr( "Information" ), DesktopApplication::tr( "<p>The system date doesn't seem to be valid.\n(%1)</p><p>Do you want to correct the clock ?</p>" ). arg( TimeString::dateString ( QDate::currentDate ( ))), QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes ) {
			QCopEnvelope e ( "QPE/Application/systemtime", "setDocument(QString)" );
			e << QString ( );		                              
		}
	}

    int rv =  a.exec();

    delete d;

	ODevice::inst ( )-> setSoftSuspend ( false );

    return rv;
}

static const char *pidfile_path = "/var/run/opie.pid";

void create_pidfile ( )
{
	FILE *f;

	if (( f = ::fopen ( pidfile_path, "w" ))) {
		::fprintf ( f, "%d", getpid ( ));
		::fclose ( f );
	}
}

void remove_pidfile ( )
{
	::unlink ( pidfile_path );
}

void handle_sigterm ( int /* sig */ )
{
	if ( qApp )
		qApp-> quit ( );
}

int main( int argc, char ** argv )
{
    ::signal ( SIGCHLD, SIG_IGN );

	::signal ( SIGTERM, handle_sigterm );
	::signal ( SIGINT, handle_sigterm );

	::setsid ( );
	::setpgid ( 0, 0 );

	::atexit ( remove_pidfile );
	create_pidfile ( );

    int retVal = initApplication ( argc, argv );

    // Kill them. Kill them all.
    ::kill ( 0, SIGTERM );
    ::sleep ( 1 );
    ::kill ( 0, SIGKILL );

    return retVal;
}