TDE base libraries and programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
7.7KB

  1. //-----------------------------------------------------------------------------
  2. //
  3. // Screen savers for KDE
  4. //
  5. // Copyright (c) Martin R. Jones 1999
  6. //
  7. // This is an extremely simple program that starts a random screensaver.
  8. //
  9. #include <config.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include <unistd.h>
  14. #include <tqtextstream.h>
  15. #include <tqlayout.h>
  16. #include <tqframe.h>
  17. #include <tqcheckbox.h>
  18. #include <tqwidget.h>
  19. #include <tqfileinfo.h>
  20. #include <tdeapplication.h>
  21. #include <kstandarddirs.h>
  22. #include <tdeglobal.h>
  23. #include <tdelocale.h>
  24. #include <kdesktopfile.h>
  25. #include <krandomsequence.h>
  26. #include <kdebug.h>
  27. #include <tdecmdlineargs.h>
  28. #include <kdialogbase.h>
  29. #include <tdeconfig.h>
  30. #include "tdescreensaver_vroot.h"
  31. #include "random.h"
  32. #define MAX_ARGS 20
  33. static void usage(char *name)
  34. {
  35. puts(i18n("Usage: %1 [-setup] [args]\n"
  36. "Starts a random screen saver.\n"
  37. "Any arguments (except -setup) are passed on to the screen saver.").arg( name ).local8Bit().data());
  38. }
  39. static const char appName[] = "random";
  40. static const char description[] = I18N_NOOP("Start a random TDE screen saver");
  41. static const char version[] = "2.0.0";
  42. static const TDECmdLineOptions options[] =
  43. {
  44. { "setup", I18N_NOOP("Setup screen saver"), 0 },
  45. { "window-id wid", I18N_NOOP("Run in the specified XWindow"), 0 },
  46. { "root", I18N_NOOP("Run in the root XWindow"), 0 },
  47. // { "+-- [options]", I18N_NOOP("Options to pass to the screen saver"), 0 }
  48. TDECmdLineLastOption
  49. };
  50. //----------------------------------------------------------------------------
  51. #ifdef HAVE_GLXCHOOSEVISUAL
  52. #include <GL/glx.h>
  53. #endif
  54. //-------------------------------------
  55. bool hasDirectRendering () {
  56. Display *dpy = TQApplication::desktop()->x11Display();
  57. #ifdef HAVE_GLXCHOOSEVISUAL
  58. int attribSingle[] = {
  59. GLX_RGBA,
  60. GLX_RED_SIZE, 1,
  61. GLX_GREEN_SIZE, 1,
  62. GLX_BLUE_SIZE, 1,
  63. None
  64. };
  65. XVisualInfo* visinfo = glXChooseVisual (
  66. dpy, TQApplication::desktop()->primaryScreen(), attribSingle
  67. );
  68. if (visinfo) {
  69. GLXContext ctx = glXCreateContext ( dpy, visinfo, NULL, True );
  70. if (glXIsDirect(dpy, ctx)) {
  71. glXDestroyContext (dpy,ctx);
  72. return true;
  73. }
  74. glXDestroyContext (dpy,ctx);
  75. return false;
  76. } else {
  77. return false;
  78. }
  79. #else
  80. // no GL
  81. return false;
  82. #endif
  83. }
  84. int main(int argc, char *argv[])
  85. {
  86. TDELocale::setMainCatalogue("tdescreensaver");
  87. TDECmdLineArgs::init(argc, argv, appName, I18N_NOOP("Random screen saver"), description, version);
  88. TDECmdLineArgs::addCmdLineOptions(options);
  89. TDEApplication app;
  90. Window windowId = 0;
  91. TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
  92. if (args->isSet("setup"))
  93. {
  94. KRandomSetup setup;
  95. setup.exec();
  96. exit(0);
  97. }
  98. if (args->isSet("window-id"))
  99. {
  100. windowId = atol(args->getOption("window-id"));
  101. }
  102. if (args->isSet("root"))
  103. {
  104. windowId = RootWindow(tqt_xdisplay(), tqt_xscreen());
  105. }
  106. TDEGlobal::dirs()->addResourceType("scrsav",
  107. TDEGlobal::dirs()->kde_default("apps") +
  108. "apps/ScreenSavers/");
  109. TDEGlobal::dirs()->addResourceType("scrsav",
  110. TDEGlobal::dirs()->kde_default("apps") +
  111. "System/ScreenSavers/");
  112. TQStringList tempSaverFileList = TDEGlobal::dirs()->findAllResources("scrsav",
  113. "*.desktop", false, true);
  114. TQStringList saverFileList;
  115. TDEConfig type("krandom.kssrc");
  116. type.setGroup("Settings");
  117. bool opengl = type.readBoolEntry("OpenGL", hasDirectRendering());
  118. kdDebug() << "hasOPEN " << opengl << endl;
  119. bool manipulatescreen = type.readBoolEntry("ManipulateScreen");
  120. bool fortune = !TDEStandardDirs::findExe("fortune").isEmpty();
  121. TQStringList defaults = type.readListEntry( "Defaults" );
  122. TQMap<TQString, int> def_numbers;
  123. for ( TQStringList::ConstIterator it = defaults.begin(); it != defaults.end(); ++it ) {
  124. int index = ( *it ).find( ':' );
  125. if ( index == -1 )
  126. def_numbers[*it] = 1;
  127. else
  128. def_numbers[( *it ).left( index )] = ( *it ).mid( index + 1 ).toInt();
  129. }
  130. for (uint i = 0; i < tempSaverFileList.count(); i++)
  131. {
  132. int howoften = 1;
  133. if ( defaults.count() != 0 ) {
  134. TQFileInfo fi( tempSaverFileList[i] );
  135. if ( def_numbers.contains( fi.fileName() ) )
  136. howoften = def_numbers[fi.fileName()];
  137. else
  138. howoften = 0;
  139. }
  140. KDesktopFile saver(tempSaverFileList[i], true);
  141. if (!saver.tryExec())
  142. continue;
  143. TQString saverType = saver.readEntry("X-TDE-Type");
  144. if (!saverType.isEmpty()) // no X-TDE-Type defined so must be OK
  145. {
  146. TQStringList saverTypes = TQStringList::split(";", saverType);
  147. for (TQStringList::ConstIterator it = saverTypes.begin(); it != saverTypes.end(); ++it )
  148. {
  149. if (*it == "ManipulateScreen")
  150. {
  151. if (!manipulatescreen)
  152. howoften = 0;
  153. }
  154. else
  155. if (*it == "OpenGL")
  156. {
  157. if (!opengl)
  158. howoften = 0;
  159. }
  160. if (*it == "Fortune")
  161. {
  162. if (!fortune)
  163. howoften = 0;
  164. }
  165. }
  166. }
  167. for ( int j = 0; j < howoften; ++j )
  168. saverFileList.append(tempSaverFileList[i]);
  169. }
  170. kdDebug() << "final " << saverFileList << endl;
  171. KRandomSequence rnd;
  172. int indx = rnd.getLong(saverFileList.count());
  173. TQString filename = *(saverFileList.at(indx));
  174. KDesktopFile config(filename, true);
  175. TQString cmd;
  176. if (windowId && config.hasActionGroup("InWindow"))
  177. {
  178. config.setActionGroup("InWindow");
  179. }
  180. else if ((windowId == 0) && config.hasActionGroup("Root"))
  181. {
  182. config.setActionGroup("Root");
  183. }
  184. cmd = config.readPathEntry("Exec");
  185. TQTextStream ts(&cmd, IO_ReadOnly);
  186. TQString word;
  187. ts >> word;
  188. TQString exeFile = TDEStandardDirs::findExe(word);
  189. if (!exeFile.isEmpty())
  190. {
  191. char *sargs[MAX_ARGS];
  192. sargs[0] = new char [strlen(word.ascii())+1];
  193. strcpy(sargs[0], word.ascii());
  194. int i = 1;
  195. while (!ts.atEnd() && i < MAX_ARGS-1)
  196. {
  197. ts >> word;
  198. if (word == "%w")
  199. {
  200. word = word.setNum(windowId);
  201. }
  202. sargs[i] = new char [strlen(word.ascii())+1];
  203. strcpy(sargs[i], word.ascii());
  204. kdDebug() << "word is " << word.ascii() << endl;
  205. i++;
  206. }
  207. sargs[i] = 0;
  208. execv(exeFile.ascii(), sargs);
  209. }
  210. // If we end up here then we couldn't start a saver.
  211. // If we have been supplied a window id or root window then blank it.
  212. Window win = windowId ? windowId : RootWindow(tqt_xdisplay(), tqt_xscreen());
  213. XSetWindowBackground(tqt_xdisplay(), win,
  214. BlackPixel(tqt_xdisplay(), tqt_xscreen()));
  215. XClearWindow(tqt_xdisplay(), win);
  216. }
  217. KRandomSetup::KRandomSetup( TQWidget *parent, const char *name )
  218. : KDialogBase( parent, name, true, i18n( "Setup Random Screen Saver" ),
  219. Ok|Cancel, Ok, true )
  220. {
  221. TQFrame *main = makeMainWidget();
  222. TQGridLayout *grid = new TQGridLayout(main, 4, 2, 0, spacingHint() );
  223. openGL = new TQCheckBox( i18n("Use OpenGL screen savers"), main );
  224. grid->addWidget(openGL, 0, 0);
  225. manipulateScreen = new TQCheckBox(i18n("Use screen savers that manipulate the screen"), main);
  226. grid->addWidget(manipulateScreen, 1, 0);
  227. setMinimumSize( sizeHint() );
  228. TDEConfig config("krandom.kssrc");
  229. config.setGroup("Settings");
  230. openGL->setChecked(config.readBoolEntry("OpenGL", hasDirectRendering()));
  231. manipulateScreen->setChecked(config.readBoolEntry("ManipulateScreen", true));
  232. }
  233. void KRandomSetup::slotOk()
  234. {
  235. TDEConfig config("krandom.kssrc");
  236. config.setGroup("Settings");
  237. config.writeEntry("OpenGL", openGL->isChecked());
  238. config.writeEntry("ManipulateScreen", manipulateScreen->isChecked());
  239. accept();
  240. }
  241. #include "random.moc"