1 //-----------------------------------------------------------------------------
3 // KDE xscreensaver launcher
5 // Copyright (c) Martin R. Jones <mjones@kde.org> 1999
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public
9 // License as published by the Free Software Foundation;
10 // version 2 of the License.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; see the file COPYING. If not, write to
19 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21 #include <config-kxsconfig.h>
28 #include <q3ptrlist.h>
30 #include <qfileinfo.h>
33 #include <kapplication.h>
35 #include <kstandarddirs.h>
37 #include <kcmdlineargs.h>
44 template class Q3PtrList
<KXSConfigItem
>;
46 //===========================================================================
47 static const char appName
[] = "kxsrun";
49 static const char description
[] = I18N_NOOP("KDE X Screen Saver Launcher");
51 static const char version
[] = "3.0.0";
53 static const KCmdLineOptions options
[] =
55 {"+screensaver", I18N_NOOP("Filename of the screen saver to start"), 0},
56 {"+-- [options]", I18N_NOOP("Extra options to pass to the screen saver"), 0},
60 int main(int argc
, char *argv
[])
62 KLocale::setMainCatalog("kxsconfig");
63 KCmdLineArgs::init(argc
, argv
, appName
, I18N_NOOP("KXSRun"), description
, version
);
65 KCmdLineArgs::addCmdLineOptions(options
);
67 KApplication
app( false );
69 KCmdLineArgs
*args
= KCmdLineArgs::parsedArgs();
74 QString filename
= args
->arg(0);
75 QString
configFile(filename
);
77 // Get the config filename
78 int slash
= filename
.findRev('/');
80 configFile
= filename
.mid(slash
+1);
82 QString exeName
= configFile
;
85 // read configuration args
86 KConfig
config(configFile
);
88 Q3PtrList
<KXSConfigItem
> configItemList
;
90 QString xmlFile
= "/doesntexist";
91 #ifdef XSCREENSAVER_CONFIG_DIR
92 xmlFile
= XSCREENSAVER_CONFIG_DIR
;
94 xmlFile
+= "/" + exeName
+ ".xml";
95 if ( QFile::exists( xmlFile
) ) {
96 // We can use the xscreensaver xml config files.
98 xmlParser
.parse(xmlFile
);
99 configItemList
= *xmlParser
.items();
100 Q3PtrListIterator
<KXSConfigItem
> it( configItemList
);
102 while ( (item
= it
.current()) != 0 ) {
104 item
->read( config
);
107 // fall back to KDE's old config files.
111 QString group
= QString("Arg%1").arg(idx
);
112 if (config
.hasGroup(group
)) {
113 config
.setGroup(group
);
114 QString type
= config
.readEntry("Type");
115 if (type
== "Range") {
116 KXSRangeItem
*rc
= new KXSRangeItem(group
, config
);
117 configItemList
.append(rc
);
118 } else if (type
== "DoubleRange") {
119 KXSDoubleRangeItem
*rc
= new KXSDoubleRangeItem(group
, config
);
120 configItemList
.append(rc
);
121 } else if (type
== "Check") {
122 KXSBoolItem
*cc
= new KXSBoolItem(group
, config
);
123 configItemList
.append(cc
);
124 } else if (type
== "DropList") {
125 KXSSelectItem
*si
= new KXSSelectItem(group
, config
);
126 configItemList
.append(si
);
135 // find the xscreensaver executable
136 //work around a KStandarDirs::findExe() "feature" where it looks in $KDEDIR/bin first no matter what and sometimes finds the wrong executable
138 QString saverdir
= QString("%1/%2").arg(XSCREENSAVER_HACKS_DIR
).arg(filename
);
139 kDebug() << "saverdir is" << saverdir
<< endl
;
141 checkExe
.setFile(saverdir
);
142 if (checkExe
.exists() && checkExe
.isExecutable() && checkExe
.isFile())
148 if (!exeFile
.isEmpty()) {
149 char *sargs
[MAX_ARGS
];
150 sargs
[0] = new char [strlen(filename
.ascii())+1];
151 strcpy(sargs
[0], filename
.ascii());
153 // add the command line options
156 for (i
= 1; i
< (unsigned)args
->count(); i
++)
157 cmd
+= " " + QString(args
->arg(i
));
159 // add the config options
161 for (item
= configItemList
.first(); item
!= 0; item
= configItemList
.next()) {
162 cmd
+= " " + item
->command();
165 // put into char * array for execv
169 bool inQuotes
= false;
170 while (i
< cmd
.length() && si
< MAX_ARGS
-1) {
172 while ( cmd
[i
].isSpace() && i
< cmd
.length() ) i
++;
173 while ( (!cmd
[i
].isSpace() || inQuotes
) && i
< cmd
.length() ) {
174 if ( cmd
[i
] == '\"' ) {
175 inQuotes
= !inQuotes
;
181 if (!word
.isEmpty()) {
182 sargs
[si
] = new char [strlen(word
.ascii())+1];
183 strcpy(sargs
[si
], word
.ascii());
191 execv(exeFile
.ascii(), sargs
);