add more spacing
[personal-kdebase.git] / workspace / kscreensaver / libkscreensaver / main.cpp
blob40ce5fe13ef909d0fcc67fb1f622077986548da0
1 /* This file is part of the KDE libraries
3 Copyright (c) 2001 Martin R. Jones <mjones@kde.org>
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.
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.
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.
20 #include "kscreensaver.h"
21 #include "kscreensaver_vroot.h"
23 #include <config-workspace.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <signal.h>
30 #include <QDialog>
31 #include <QEvent>
32 #include <QKeyEvent>
33 #include <QSocketNotifier>
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>
44 #include <QtGui/QX11Info>
46 static void crashHandler( int )
48 #ifdef SIGABRT
49 signal (SIGABRT, SIG_DFL);
50 #endif
51 abort();
54 extern "C" {
56 static int termPipe[2];
58 static void termHandler( int )
60 write( termPipe[1], "", 1 );
65 //----------------------------------------------------------------------------
67 class DemoWindow : public QWidget
69 public:
70 DemoWindow() : QWidget()
72 setFixedSize(600, 420);
75 protected:
76 virtual bool eventFilter( QObject *, QEvent *e )
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();
87 return false;
90 virtual void keyPressEvent(QKeyEvent *e)
92 if (e->text() == QLatin1String("q"))
94 qApp->quit();
98 virtual void closeEvent( QCloseEvent * )
100 qApp->quit();
105 //----------------------------------------------------------------------------
106 #if defined(Q_WS_QWS) || defined(Q_WS_MACX)
107 typedef WId Window;
108 #endif
110 int kScreenSaverMain( int argc, char** argv, KScreenSaverInterface& screenSaverInterface )
112 KLocale::setMainCatalog("libkscreensaver");
113 KCmdLineArgs::init(argc, argv, screenSaverInterface.aboutData());
116 KCmdLineOptions options;
118 options.add("setup", ki18n("Setup screen saver"));
120 options.add("window-id wid", ki18n("Run in the specified XWindow"));
122 options.add("root", ki18n("Run in the root XWindow"));
124 options.add("demo", ki18n("Start screen saver in demo mode"), "default");
126 KCmdLineArgs::addCmdLineOptions(options);
128 KApplication app;
130 // Set a useful default icon.
131 app.setWindowIcon(KIcon("preferences-desktop-screensaver"));
133 if (!pipe(termPipe))
135 struct sigaction sa;
136 sa.sa_handler = termHandler;
137 sigemptyset(&sa.sa_mask);
138 sa.sa_flags = 0;
139 sigaction(SIGTERM, &sa, 0);
140 QSocketNotifier *sn = new QSocketNotifier(termPipe[0], QSocketNotifier::Read, &app);
141 QObject::connect(sn, SIGNAL(activated(int)), &app, SLOT(quit()));
144 KCrash::setCrashHandler( crashHandler );
145 KGlobal::locale()->insertCatalog("klock");
146 KGlobal::locale()->insertCatalog("kscreensaver");
148 DemoWindow *demoWidget = 0;
149 Window saveWin = 0;
150 KScreenSaver *target;
152 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
154 if (args->isSet("setup"))
156 QDialog *dlg = screenSaverInterface.setup();
157 args->clear();
158 dlg->exec();
159 delete dlg;
160 return 0;
163 if (args->isSet("window-id"))
165 saveWin = args->getOption("window-id").toInt();
168 #ifdef Q_WS_X11 //FIXME
169 if (args->isSet("root"))
171 QX11Info inf;
172 saveWin = RootWindow(QX11Info::display(), inf.screen());
174 #endif
176 if (args->isSet("demo"))
178 saveWin = 0;
181 if (saveWin == 0)
183 demoWidget = new DemoWindow();
184 demoWidget->setAttribute(Qt::WA_NoSystemBackground);
185 demoWidget->setAttribute(Qt::WA_PaintOnScreen);
186 demoWidget->show();
187 app.processEvents();
188 saveWin = demoWidget->winId();
191 target = screenSaverInterface.create( saveWin );
192 target->setAttribute(Qt::WA_PaintOnScreen);
193 target->show();
195 if (demoWidget)
197 target->installEventFilter( demoWidget );
200 args->clear();
201 app.exec();
203 delete target;
204 delete demoWidget;
206 return 0;