summaryrefslogtreecommitdiffstats
path: root/kscreensaver/random.cpp
blob: dd839022600ded3f78c06f3c7dfef23ecca0dcd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
 //-----------------------------------------------------------------------------
//
// Screen savers for KDE
//
// Copyright (c)  Martin R. Jones 1999
//
// This is an extremely simple program that starts a random screensaver.
//

#include <config.h>

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>

#include <tqtextstream.h>
#include <tqlayout.h>
#include <tqframe.h>
#include <tqcheckbox.h>
#include <tqwidget.h>
#include <tqfileinfo.h>

#include <kapplication.h>
#include <kstandarddirs.h>
#include <kglobal.h>
#include <klocale.h>
#include <kdesktopfile.h>
#include <krandomsequence.h>
#include <kdebug.h>
#include <kcmdlineargs.h>
#include <kdialogbase.h>
#include <kconfig.h>

#include "kscreensaver_vroot.h"
#include "random.h"

#define MAX_ARGS    20

static void usage(char *name)
{
	puts(i18n("Usage: %1 [-setup] [args]\n"
				"Starts a random screen saver.\n"
				"Any arguments (except -setup) are passed on to the screen saver.").arg( name ).local8Bit().data());
}

static const char appName[] = "random";

static const char description[] = I18N_NOOP("Start a random TDE screen saver");

static const char version[] = "2.0.0";

static const KCmdLineOptions options[] =
{
	{ "setup", I18N_NOOP("Setup screen saver"), 0 },
	{ "window-id wid", I18N_NOOP("Run in the specified XWindow"), 0 },
	{ "root", I18N_NOOP("Run in the root XWindow"), 0 },
	//  { "+-- [options]", I18N_NOOP("Options to pass to the screen saver"), 0 }
	KCmdLineLastOption
};

//----------------------------------------------------------------------------

#ifdef HAVE_GLXCHOOSEVISUAL
#include <GL/glx.h>
#endif

//-------------------------------------
bool hasDirectRendering () {
    Display *dpy = TQApplication::desktop()->x11Display();

#ifdef HAVE_GLXCHOOSEVISUAL
    int attribSingle[] = {
        GLX_RGBA,
        GLX_RED_SIZE,   1,
        GLX_GREEN_SIZE, 1,
        GLX_BLUE_SIZE,  1,
        None
    };
    XVisualInfo* visinfo = glXChooseVisual (
        dpy, TQApplication::desktop()->primaryScreen(), attribSingle
    );
    if (visinfo) {
        GLXContext ctx = glXCreateContext ( dpy, visinfo, NULL, True );
        if (glXIsDirect(dpy, ctx)) {
            glXDestroyContext (dpy,ctx);
            return true;
        }
        glXDestroyContext (dpy,ctx);
        return false;
    } else {
        return false;
    }
#else
#error no GL?
    return false;
#endif

}

int main(int argc, char *argv[])
{
	KLocale::setMainCatalogue("kscreensaver");
	TDECmdLineArgs::init(argc, argv, appName, I18N_NOOP("Random screen saver"), description, version);

	TDECmdLineArgs::addCmdLineOptions(options);

	TDEApplication app;

	Window windowId = 0;

	TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();

	if (args->isSet("setup"))
	{
		KRandomSetup setup;
		setup.exec();
		exit(0);
	}

	if (args->isSet("window-id"))
	{
		windowId = atol(args->getOption("window-id"));
	}

	if (args->isSet("root"))
	{
		windowId = RootWindow(tqt_xdisplay(), tqt_xscreen());
	}

	KGlobal::dirs()->addResourceType("scrsav",
			KGlobal::dirs()->kde_default("apps") +
			"apps/ScreenSavers/");
	KGlobal::dirs()->addResourceType("scrsav",
			KGlobal::dirs()->kde_default("apps") +
			"System/ScreenSavers/");
	TQStringList tempSaverFileList = KGlobal::dirs()->findAllResources("scrsav",
			"*.desktop", false, true);

	TQStringList saverFileList;

	KConfig type("krandom.kssrc");
	type.setGroup("Settings");
	bool opengl = type.readBoolEntry("OpenGL", hasDirectRendering());
        kdDebug() << "hasOPEN " << opengl << endl;
	bool manipulatescreen = type.readBoolEntry("ManipulateScreen");
        bool fortune = !KStandardDirs::findExe("fortune").isEmpty();
        TQStringList defaults = type.readListEntry( "Defaults" );
        TQMap<TQString, int> def_numbers;
        for ( TQStringList::ConstIterator it = defaults.begin(); it != defaults.end(); ++it ) {
            int index = ( *it ).find( ':' );
            if ( index == -1 )
                def_numbers[*it] = 1;
            else
                def_numbers[( *it ).left( index )] = ( *it ).mid( index + 1 ).toInt();
        }

	for (uint i = 0; i < tempSaverFileList.count(); i++)
	{
                int howoften = 1;
                if ( defaults.count() != 0 ) {
                    TQFileInfo fi( tempSaverFileList[i] );
                    if ( def_numbers.contains( fi.fileName() ) )
                        howoften = def_numbers[fi.fileName()];
                    else
                        howoften = 0;
                }

		KDesktopFile saver(tempSaverFileList[i], true);
                if (!saver.tryExec())
                    continue;
		TQString saverType = saver.readEntry("X-TDE-Type");
		if (!saverType.isEmpty()) // no X-TDE-Type defined so must be OK
                {
			TQStringList saverTypes = TQStringList::split(";", saverType);
			for (TQStringList::ConstIterator it =  saverTypes.begin(); it != saverTypes.end(); ++it )
			{
				if (*it == "ManipulateScreen")
				{
					if (!manipulatescreen)
                                            howoften = 0;
				}
				else
				if (*it == "OpenGL")
				{
					if (!opengl)
                                            howoften = 0;
				}
				if (*it == "Fortune")
				{
					if (!fortune)
                                            howoften = 0;
				}

			}
		}
                for ( int j = 0; j < howoften; ++j )
                    saverFileList.append(tempSaverFileList[i]);
	}
        kdDebug() << "final " << saverFileList << endl;

	KRandomSequence rnd;
	int indx = rnd.getLong(saverFileList.count());
	TQString filename = *(saverFileList.at(indx));

	KDesktopFile config(filename, true);

	TQString cmd;
	if (windowId && config.hasActionGroup("InWindow"))
	{
		config.setActionGroup("InWindow");
	}
	else if ((windowId == 0) && config.hasActionGroup("Root"))
	{
		config.setActionGroup("Root");
	}
	cmd = config.readPathEntry("Exec");

	TQTextStream ts(&cmd, IO_ReadOnly);
	TQString word;
	ts >> word;
	TQString exeFile = KStandardDirs::findExe(word);

	if (!exeFile.isEmpty())
	{
		char *sargs[MAX_ARGS];
		sargs[0] = new char [strlen(word.ascii())+1];
		strcpy(sargs[0], word.ascii());

		int i = 1;
		while (!ts.atEnd() && i < MAX_ARGS-1)
		{
			ts >> word;
			if (word == "%w")
			{
				word = word.setNum(windowId);
			}

			sargs[i] = new char [strlen(word.ascii())+1];
			strcpy(sargs[i], word.ascii());
			kdDebug() << "word is " << word.ascii() << endl;

			i++;
		}

		sargs[i] = 0;

		execv(exeFile.ascii(), sargs);
	}

	// If we end up here then we couldn't start a saver.
	// If we have been supplied a window id or root window then blank it.
	Window win = windowId ? windowId : RootWindow(tqt_xdisplay(), tqt_xscreen());
	XSetWindowBackground(tqt_xdisplay(), win,
			BlackPixel(tqt_xdisplay(), tqt_xscreen()));
	XClearWindow(tqt_xdisplay(), win);
}


KRandomSetup::KRandomSetup( TQWidget *parent, const char *name )
	: KDialogBase( parent, name, true, i18n( "Setup Random Screen Saver" ),
			Ok|Cancel, Ok, true )
{

	TQFrame *main = makeMainWidget();
	TQGridLayout *grid = new TQGridLayout(main, 4, 2, 0, spacingHint() );

	openGL = new TQCheckBox( i18n("Use OpenGL screen savers"), main );
	grid->addWidget(openGL, 0, 0);

	manipulateScreen = new TQCheckBox(i18n("Use screen savers that manipulate the screen"), main);
	grid->addWidget(manipulateScreen, 1, 0);

	setMinimumSize( sizeHint() );

	KConfig config("krandom.kssrc");
	config.setGroup("Settings");
	openGL->setChecked(config.readBoolEntry("OpenGL", hasDirectRendering()));
	manipulateScreen->setChecked(config.readBoolEntry("ManipulateScreen", true));
}

void KRandomSetup::slotOk()
{
	KConfig config("krandom.kssrc");
	config.setGroup("Settings");
	config.writeEntry("OpenGL", openGL->isChecked());
	config.writeEntry("ManipulateScreen", manipulateScreen->isChecked());

	accept();
}

#include "random.moc"