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>
33 #include <QSocketNotifier>
38 #include <kcmdlineargs.h>
40 #include <kapplication.h>
42 #include <kaboutdata.h>
44 #include <QtGui/QX11Info>
46 static void crashHandler( int )
49 signal (SIGABRT
, SIG_DFL
);
56 static int termPipe
[2];
58 static void termHandler( int )
60 write( termPipe
[1], "", 1 );
65 //----------------------------------------------------------------------------
67 class DemoWindow
: public QWidget
70 DemoWindow() : QWidget()
72 setFixedSize(600, 420);
76 virtual bool eventFilter( QObject
*, QEvent
*e
)
78 if (e
->type() == QEvent::KeyPress
) {
79 keyPressEvent( (QKeyEvent
*)e
);
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.
90 virtual void keyPressEvent(QKeyEvent
*e
)
92 if (e
->text() == QLatin1String("q"))
98 virtual void closeEvent( QCloseEvent
* )
105 //----------------------------------------------------------------------------
106 #if defined(Q_WS_QWS) || defined(Q_WS_MACX)
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
);
130 // Set a useful default icon.
131 app
.setWindowIcon(KIcon("preferences-desktop-screensaver"));
136 sa
.sa_handler
= termHandler
;
137 sigemptyset(&sa
.sa_mask
);
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;
150 KScreenSaver
*target
;
152 KCmdLineArgs
*args
= KCmdLineArgs::parsedArgs();
154 if (args
->isSet("setup"))
156 QDialog
*dlg
= screenSaverInterface
.setup();
163 if (args
->isSet("window-id"))
165 saveWin
= args
->getOption("window-id").toInt();
168 #ifdef Q_WS_X11 //FIXME
169 if (args
->isSet("root"))
172 saveWin
= RootWindow(QX11Info::display(), inf
.screen());
176 if (args
->isSet("demo"))
183 demoWidget
= new DemoWindow();
184 demoWidget
->setAttribute(Qt::WA_NoSystemBackground
);
185 demoWidget
->setAttribute(Qt::WA_PaintOnScreen
);
188 saveWin
= demoWidget
->winId();
191 target
= screenSaverInterface
.create( saveWin
);
192 target
->setAttribute(Qt::WA_PaintOnScreen
);
197 target
->installEventFilter( demoWidget
);