1 //-----------------------------------------------------------------------------
3 // Screen savers for KDE
5 // Copyright (c) Martin R. Jones 1999
7 // This is an extremely simple program that starts a random screensaver.
10 #include <config-workspace.h>
17 #include <QTextStream>
22 #include <QGridLayout>
24 #include <kapplication.h>
25 #include <kstandarddirs.h>
28 #include <kdesktopfile.h>
29 #include <krandomsequence.h>
31 #include <kcmdlineargs.h>
35 #include <kdeversion.h>
36 #include "kscreensaver_vroot.h"
40 #include <kservicetypetrader.h>
43 void usage(char *name
)
45 puts(i18n("Usage: %1 [-setup] [args]\n"
46 "Starts a random screen saver.\n"
47 "Any arguments (except -setup) are passed on to the screen saver.", name
).toLocal8Bit().data());
50 static const char appName
[] = "random";
52 static const char description
[] = I18N_NOOP("Start a random KDE screen saver");
54 static QString
exeFromActionGroup(const QList
<KServiceAction
>& actions
, const char* name
)
56 foreach(const KServiceAction
& action
, actions
) {
57 if (action
.name() == name
)
63 //----------------------------------------------------------------------------
65 int main(int argc
, char *argv
[])
67 KCmdLineArgs::init(argc
, argv
, appName
, "kscreensaver", ki18n("Random screen saver"),
68 KDE_VERSION_STRING
, ki18n(description
));
71 KCmdLineOptions options
;
73 options
.add("setup", ki18n("Setup screen saver"));
75 options
.add("window-id wid", ki18n("Run in the specified XWindow"));
77 options
.add("root", ki18n("Run in the root XWindow"));
79 KCmdLineArgs::addCmdLineOptions(options
);
85 KCmdLineArgs
*args
= KCmdLineArgs::parsedArgs();
87 if (args
->isSet("setup"))
94 if (args
->isSet("window-id"))
96 windowId
= args
->getOption("window-id").toInt();
99 if (args
->isSet("root"))
102 windowId
= RootWindow(QX11Info::display(), info
.screen());
105 const KService::List lst
= KServiceTypeTrader::self()->query( "ScreenSaver");
106 KService::List availableSavers
;
108 KConfig
type("krandom.kssrc", KConfig::NoGlobals
);
109 const KConfigGroup configGroup
= type
.group("Settings");
110 const bool opengl
= configGroup
.readEntry("OpenGL", false);
111 const bool manipulatescreen
= configGroup
.readEntry("ManipulateScreen", false);
112 // TODO replace this with TryExec=fortune in the desktop files
113 const bool fortune
= !KStandardDirs::findExe("fortune").isEmpty();
114 foreach( const KService::Ptr
& service
, lst
) {
115 //QString file = KStandardDirs::locate("services", service->entryPath());
116 //kDebug() << "Looking at " << file;
117 const QString saverType
= service
->property("X-KDE-Type").toString();
118 if (saverType
.isEmpty()) { // no X-KDE-Type defined so must be OK
119 availableSavers
.append(service
);
121 const QStringList saverTypes
= saverType
.split( ";");
122 for (QStringList::ConstIterator it
= saverTypes
.begin(); it
!= saverTypes
.end(); ++it
) {
123 kDebug() << "saverTypes is "<< *it
;
124 if (*it
== "ManipulateScreen") {
125 if (manipulatescreen
) {
126 availableSavers
.append(service
);
128 } else if (*it
== "OpenGL") {
130 availableSavers
.append(service
);
132 } else if (*it
== "Fortune") {
134 availableSavers
.append(service
);
142 const int indx
= rnd
.getLong(availableSavers
.count());
143 const KService::Ptr service
= availableSavers
.at(indx
);
144 const QList
<KServiceAction
> actions
= service
->actions();
148 cmd
= exeFromActionGroup(actions
, "InWindow");
149 if (cmd
.isEmpty() && windowId
== 0)
150 cmd
= exeFromActionGroup(actions
, "Root");
152 cmd
= service
->exec();
154 QTextStream
ts(&cmd
, QIODevice::ReadOnly
);
157 QString exeFile
= KStandardDirs::findExe(word
);
159 if (!exeFile
.isEmpty())
161 char *sargs
[MAX_ARGS
];
162 sargs
[0] = new char [strlen(word
.toAscii())+1];
163 strcpy(sargs
[0], word
.toAscii());
166 while (!ts
.atEnd() && i
< MAX_ARGS
-1)
171 word
= word
.setNum(windowId
);
174 sargs
[i
] = new char [strlen(word
.toAscii())+1];
175 strcpy(sargs
[i
], word
.toAscii());
176 kDebug() << "word is " << word
.toAscii();
183 execv(exeFile
.toAscii(), sargs
);
186 // If we end up here then we couldn't start a saver.
187 // If we have been supplied a window id or root window then blank it.
189 Window win
= windowId
? windowId
: RootWindow(QX11Info::display(), info
.screen());
190 XSetWindowBackground(QX11Info::display(), win
,
191 BlackPixel(QX11Info::display(), info
.screen()));
192 XClearWindow(QX11Info::display(), win
);
196 KRandomSetup::KRandomSetup( QWidget
*parent
, const char *name
)
199 setObjectName( name
);
201 setCaption( i18n( "Setup Random Screen Saver" ) );
202 setButtons( Ok
| Cancel
);
203 showButtonSeparator( true );
205 QFrame
*main
= new QFrame( this );
206 setMainWidget( main
);
207 QGridLayout
*grid
= new QGridLayout(main
);
208 grid
->setSpacing( spacingHint() );
210 openGL
= new QCheckBox( i18n("Use OpenGL screen savers"), main
);
211 grid
->addWidget(openGL
, 0, 0);
213 manipulateScreen
= new QCheckBox(i18n("Use screen savers that manipulate the screen"), main
);
214 grid
->addWidget(manipulateScreen
, 1, 0);
216 setMinimumSize( sizeHint() );
218 KConfig
config("krandom.kssrc", KConfig::NoGlobals
);
219 const KConfigGroup configGroup
= config
.group("Settings");
220 openGL
->setChecked(configGroup
.readEntry("OpenGL", true));
221 manipulateScreen
->setChecked(configGroup
.readEntry("ManipulateScreen", true));
223 connect( this, SIGNAL( okClicked() ), SLOT( slotOk() ) );
226 void KRandomSetup::slotOk()
228 KConfig
config("krandom.kssrc");
229 KConfigGroup configGroup
= config
.group("Settings");
230 configGroup
.writeEntry("OpenGL", openGL
->isChecked());
231 configGroup
.writeEntry("ManipulateScreen", manipulateScreen
->isChecked());
236 #include "random.moc"