~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kscreensaver/libkscreensaver/main.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE libraries
 
2
 
 
3
    Copyright (c) 2001  Martin R. Jones <mjones@kde.org>
 
4
 
 
5
    This library is free software; you can redistribute it and/or
 
6
    modify it under the terms of the GNU Library General Public
 
7
    License as published by the Free Software Foundation; either
 
8
    version 2 of the License, or (at your option) any later version.
 
9
 
 
10
    This library is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
    Library General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU Library General Public License
 
16
    along with this library; see the file COPYING.LIB.  If not, write to
 
17
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
18
    Boston, MA 02110-1301, USA.
 
19
*/
 
20
#include "kscreensaver.h"
 
21
#include "kscreensaver_vroot.h"
 
22
 
 
23
#include <config-workspace.h>
 
24
 
 
25
#include <stdlib.h>
 
26
#include <unistd.h>
 
27
#include <stdio.h>
 
28
#include <signal.h>
 
29
 
 
30
#include <QDialog>
 
31
#include <QEvent>
 
32
#include <QKeyEvent>
 
33
#include <QSocketNotifier>
 
34
 
 
35
#include <klocale.h>
 
36
#include <kglobal.h>
 
37
#include <kdebug.h>
 
38
#include <kcmdlineargs.h>
 
39
#include <kicon.h>
 
40
#include <kapplication.h>
 
41
#include <kcrash.h>
 
42
#include <kaboutdata.h>
 
43
 
 
44
#include <QtGui/QX11Info>
 
45
 
 
46
static void crashHandler( int  )
 
47
{
 
48
#ifdef SIGABRT
 
49
    signal (SIGABRT, SIG_DFL);
 
50
#endif
 
51
    abort();
 
52
}
 
53
 
 
54
extern "C" {
 
55
 
 
56
static int termPipe[2];
 
57
 
 
58
static void termHandler( int )
 
59
{
 
60
    write( termPipe[1], "", 1 );
 
61
}
 
62
 
 
63
}
 
64
 
 
65
//----------------------------------------------------------------------------
 
66
 
 
67
class DemoWindow : public QWidget
 
68
{
 
69
public:
 
70
    DemoWindow() : QWidget()
 
71
    {
 
72
        setFixedSize(600, 420);
 
73
    }
 
74
 
 
75
protected:
 
76
    virtual bool eventFilter( QObject *, QEvent *e )
 
77
    {
 
78
        if (e->type() == QEvent::KeyPress) {
 
79
            keyPressEvent( (QKeyEvent *)e );
 
80
            return true;
 
81
        } else if( e->type() == QEvent::Close ) {
 
82
            // In demo mode, screensaver's QWidget does create()
 
83
            // with winId of the DemoWidget, which results in two QWidget's
 
84
            // sharing the same winId and Qt delivering events only to one of them.
 
85
            qApp->quit();
 
86
        }
 
87
        return false;
 
88
    }
 
89
 
 
90
    virtual void keyPressEvent(QKeyEvent *e)
 
91
    {
 
92
        if (e->text() == QLatin1String("q"))
 
93
        {
 
94
            qApp->quit();
 
95
        }
 
96
    }
 
97
 
 
98
    virtual void closeEvent( QCloseEvent * )
 
99
    {
 
100
        qApp->quit();
 
101
    }
 
102
};
 
103
 
 
104
 
 
105
//----------------------------------------------------------------------------
 
106
#if defined(Q_WS_QWS) || defined(Q_WS_MACX)
 
107
typedef WId Window;
 
108
#endif
 
109
 
 
110
#ifdef Q_WS_X11
 
111
extern "C" {
 
112
 
 
113
static int (*oldXErrorHandler)(Display *, XErrorEvent *);
 
114
 
 
115
static int xErrorHandler(Display *dpy, XErrorEvent *err)
 
116
{
 
117
    if (getppid() == 1)
 
118
        kFatal() << "Got X error after loss of parent process. Terminating.";
 
119
    return oldXErrorHandler(dpy, err);
 
120
}
 
121
 
 
122
}
 
123
#endif
 
124
 
 
125
int kScreenSaverMain( int argc, char** argv, KScreenSaverInterface& screenSaverInterface )
 
126
{
 
127
    KLocale::setMainCatalog("libkscreensaver");
 
128
    KCmdLineArgs::init(argc, argv, screenSaverInterface.aboutData());
 
129
 
 
130
 
 
131
    KCmdLineOptions options;
 
132
 
 
133
    options.add("setup", ki18n("Setup screen saver"));
 
134
 
 
135
    options.add("window-id wid", ki18n("Run in the specified XWindow"));
 
136
 
 
137
    options.add("root", ki18n("Run in the root XWindow"));
 
138
 
 
139
    options.add("demo", ki18n("Start screen saver in demo mode"), "default");
 
140
 
 
141
    KCmdLineArgs::addCmdLineOptions(options);
 
142
 
 
143
    KApplication app;
 
144
 
 
145
    // Set a useful default icon.
 
146
    app.setWindowIcon(KIcon("preferences-desktop-screensaver"));
 
147
 
 
148
    if (!pipe(termPipe))
 
149
    {
 
150
        struct sigaction sa;
 
151
        sa.sa_handler = termHandler;
 
152
        sigemptyset(&sa.sa_mask);
 
153
        sa.sa_flags = 0;
 
154
        sigaction(SIGTERM, &sa, 0);
 
155
        QSocketNotifier *sn = new QSocketNotifier(termPipe[0], QSocketNotifier::Read, &app);
 
156
        QObject::connect(sn, SIGNAL(activated(int)), &app, SLOT(quit()));
 
157
    }
 
158
 
 
159
#ifdef Q_WS_X11
 
160
    oldXErrorHandler = XSetErrorHandler(xErrorHandler);
 
161
#endif
 
162
    KCrash::setCrashHandler( crashHandler );
 
163
    KGlobal::locale()->insertCatalog("klock");
 
164
    KGlobal::locale()->insertCatalog("kscreensaver");
 
165
 
 
166
    DemoWindow *demoWidget = 0;
 
167
    Window saveWin = 0;
 
168
    KScreenSaver *target;
 
169
 
 
170
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
 
171
 
 
172
    if (args->isSet("setup"))
 
173
    {
 
174
       QDialog *dlg = screenSaverInterface.setup();
 
175
       args->clear();
 
176
       dlg->exec();
 
177
       delete dlg;
 
178
       return 0;
 
179
    }
 
180
 
 
181
    if (args->isSet("window-id"))
 
182
    {
 
183
        saveWin = args->getOption("window-id").toInt();
 
184
    }
 
185
 
 
186
#ifdef Q_WS_X11 //FIXME
 
187
    if (args->isSet("root"))
 
188
    {
 
189
                QX11Info inf;
 
190
        saveWin = RootWindow(QX11Info::display(), inf.screen());
 
191
    }
 
192
#endif
 
193
 
 
194
    if (args->isSet("demo"))
 
195
    {
 
196
        saveWin = 0;
 
197
    }
 
198
 
 
199
    if (saveWin == 0)
 
200
    {
 
201
        demoWidget = new DemoWindow();
 
202
        demoWidget->setAttribute(Qt::WA_NoSystemBackground);
 
203
        demoWidget->setAttribute(Qt::WA_PaintOnScreen);
 
204
        demoWidget->show();
 
205
        app.processEvents();
 
206
        saveWin = demoWidget->winId();
 
207
    }
 
208
 
 
209
    target = screenSaverInterface.create( saveWin );
 
210
    target->setAttribute(Qt::WA_PaintOnScreen);
 
211
    target->show();
 
212
 
 
213
    if (demoWidget)
 
214
    {
 
215
        target->installEventFilter( demoWidget );
 
216
    }
 
217
 
 
218
    args->clear();
 
219
    app.exec();
 
220
 
 
221
    delete target;
 
222
    delete demoWidget;
 
223
 
 
224
    return 0;
 
225
}
 
226