Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / kscreensaver / libkscreensaver / main.cpp
blobb3158cd495e0f1860ff0ec769e50c20486152afb
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 <kapplication.h>
40 #include <kcrash.h>
41 #include <kaboutdata.h>
43 #include <QtGui/QX11Info>
45 static void crashHandler( int )
47 #ifdef SIGABRT
48 signal (SIGABRT, SIG_DFL);
49 #endif
50 abort();
53 extern "C" {
55 static int termPipe[2];
57 static void termHandler( int )
59 write( termPipe[1], "", 1 );
64 //----------------------------------------------------------------------------
66 class DemoWindow : public QWidget
68 public:
69 DemoWindow() : QWidget()
71 setFixedSize(600, 420);
74 protected:
75 virtual bool eventFilter( QObject *, QEvent *e )
77 if (e->type() == QEvent::KeyPress) {
78 keyPressEvent( (QKeyEvent *)e );
79 return true;
81 return false;
84 virtual void keyPressEvent(QKeyEvent *e)
86 if (e->text() == QLatin1String("q"))
88 qApp->quit();
92 virtual void closeEvent( QCloseEvent * )
94 qApp->quit();
99 //----------------------------------------------------------------------------
100 #if defined(Q_WS_QWS) || defined(Q_WS_MACX)
101 typedef WId Window;
102 #endif
104 int kScreenSaverMain( int argc, char** argv, KScreenSaverInterface& screenSaverInterface )
106 KLocale::setMainCatalog("libkscreensaver");
107 KCmdLineArgs::init(argc, argv, screenSaverInterface.aboutData());
110 KCmdLineOptions options;
112 options.add("setup", ki18n("Setup screen saver"));
114 options.add("window-id wid", ki18n("Run in the specified XWindow"));
116 options.add("root", ki18n("Run in the root XWindow"));
118 options.add("demo", ki18n("Start screen saver in demo mode"), "default");
120 KCmdLineArgs::addCmdLineOptions(options);
122 KApplication app;
124 if (!pipe(termPipe))
126 struct sigaction sa;
127 sa.sa_handler = termHandler;
128 sigemptyset(&sa.sa_mask);
129 sa.sa_flags = 0;
130 sigaction(SIGTERM, &sa, 0);
131 QSocketNotifier *sn = new QSocketNotifier(termPipe[0], QSocketNotifier::Read, &app);
132 QObject::connect(sn, SIGNAL(activated(int)), &app, SLOT(quit()));
135 KCrash::setCrashHandler( crashHandler );
136 KGlobal::locale()->insertCatalog("klock");
137 KGlobal::locale()->insertCatalog("kscreensaver");
139 DemoWindow *demoWidget = 0;
140 Window saveWin = 0;
141 KScreenSaver *target;
143 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
145 if (args->isSet("setup"))
147 QDialog *dlg = screenSaverInterface.setup();
148 args->clear();
149 dlg->exec();
150 delete dlg;
151 return 0;
154 if (args->isSet("window-id"))
156 saveWin = args->getOption("window-id").toInt();
159 #ifdef Q_WS_X11 //FIXME
160 if (args->isSet("root"))
162 QX11Info inf;
163 saveWin = RootWindow(QX11Info::display(), inf.screen());
165 #endif
167 if (args->isSet("demo"))
169 saveWin = 0;
172 if (saveWin == 0)
174 demoWidget = new DemoWindow();
175 demoWidget->setAttribute(Qt::WA_NoSystemBackground);
176 demoWidget->setAttribute(Qt::WA_PaintOnScreen);
177 demoWidget->show();
178 app.processEvents();
179 saveWin = demoWidget->winId();
182 target = screenSaverInterface.create( saveWin );
183 target->show();
185 if (demoWidget)
187 target->installEventFilter( demoWidget );
190 args->clear();
191 app.exec();
193 delete target;
194 delete demoWidget;
196 return 0;