main.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <iostream>
  2. #include <irrlicht/irrlicht.h>
  3. #include <getopt.h>
  4. #include <SDL_mixer.h>
  5. #include <SDL.h>
  6. #include "WallpaperEngine/Core/CProject.h"
  7. #include "WallpaperEngine/Irrlicht/CContext.h"
  8. #include "WallpaperEngine/Render/CWallpaper.h"
  9. #include "WallpaperEngine/Render/CScene.h"
  10. #include "WallpaperEngine/Render/CVideo.h"
  11. enum BACKGROUND_RUN_MODE
  12. {
  13. RUN_MODE_UNKNOWN = 0,
  14. RUN_MODE_HELP = 1,
  15. RUN_MODE_DIRECTORY = 2,
  16. RUN_MODE_PACKAGE = 3
  17. };
  18. WallpaperEngine::Irrlicht::CContext* IrrlichtContext = nullptr;
  19. void print_help (const char* route)
  20. {
  21. std::cout
  22. << "Usage:" << route << " [options] " << std::endl
  23. << "options:" << std::endl
  24. << " --silent\t\tMutes all the sound the wallpaper might produce" << std::endl
  25. << " --dir <folder>\tLoads an uncompressed background from the given <folder>" << std::endl
  26. << " --pkg <folder>\tLoads a scene.pkg file from the given <folder>" << std::endl
  27. << " --screen-root <screen name>\tDisplay as screen's background" << std::endl
  28. << " --fps <maximum-fps>\tLimits the FPS to the given number, useful to keep battery consumption low" << std::endl;
  29. }
  30. std::string stringPathFixes(const std::string& s){
  31. std::string str(s);
  32. if(str.empty())
  33. return s;
  34. if(str[0] == '\'' && str[str.size() - 1] == '\''){
  35. str.erase(str.size() - 1, 1);
  36. str.erase(0,1);
  37. }
  38. if(str[str.size() - 1] != '/')
  39. str += '/';
  40. return std::move(str);
  41. }
  42. int main (int argc, char* argv[])
  43. {
  44. std::vector <std::string> screens;
  45. bool isRootWindow = false;
  46. int mode = RUN_MODE_UNKNOWN;
  47. int maximumFPS = 30;
  48. bool shouldEnableAudio = true;
  49. std::string path;
  50. int option_index = 0;
  51. static struct option long_options [] = {
  52. {"screen-root", required_argument, 0, 'r'},
  53. {"pkg", required_argument, 0, 'p'},
  54. {"dir", required_argument, 0, 'd'},
  55. {"silent", no_argument, 0, 's'},
  56. {"help", no_argument, 0, 'h'},
  57. {"fps", required_argument, 0, 'f'},
  58. {nullptr, 0, 0, 0}
  59. };
  60. while (true)
  61. {
  62. int c = getopt_long (argc, argv, "r:p:d:shf:", long_options, &option_index);
  63. if (c == -1)
  64. break;
  65. switch (c)
  66. {
  67. case 'r':
  68. isRootWindow = true;
  69. screens.emplace_back (optarg);
  70. break;
  71. case 'p':
  72. if (mode == RUN_MODE_UNKNOWN)
  73. mode = RUN_MODE_PACKAGE;
  74. path = optarg;
  75. break;
  76. case 'd':
  77. if (mode == RUN_MODE_UNKNOWN)
  78. mode = RUN_MODE_DIRECTORY;
  79. path = optarg;
  80. break;
  81. case 's':
  82. shouldEnableAudio = false;
  83. break;
  84. case 'h':
  85. mode = RUN_MODE_HELP;
  86. break;
  87. case 'f':
  88. maximumFPS = atoi (optarg);
  89. break;
  90. default:
  91. break;
  92. }
  93. }
  94. if (mode == RUN_MODE_UNKNOWN || mode == RUN_MODE_HELP)
  95. {
  96. print_help (argv [0]);
  97. return 0;
  98. }
  99. try
  100. {
  101. IrrlichtContext = new WallpaperEngine::Irrlicht::CContext (screens, isRootWindow);
  102. IrrlichtContext->initializeContext ();
  103. }
  104. catch (std::runtime_error& ex)
  105. {
  106. std::cerr << ex.what () << std::endl;
  107. return 1;
  108. }
  109. path = stringPathFixes (path);
  110. irr::io::path wallpaper_path = IrrlichtContext->getDevice ()->getFileSystem ()->getAbsolutePath (path.c_str ());
  111. irr::io::path project_path = wallpaper_path + "project.json";
  112. if (mode == RUN_MODE_PACKAGE)
  113. {
  114. irr::io::path scene_path = wallpaper_path + "scene.pkg";
  115. // add the package file to the lookup list
  116. IrrlichtContext->getDevice ()->getFileSystem ()->addFileArchive (scene_path, true, false);
  117. }
  118. else if (mode == RUN_MODE_DIRECTORY)
  119. {
  120. project_path = wallpaper_path + "project.json";
  121. // set the working directory to the project folder
  122. IrrlichtContext->getDevice ()->getFileSystem ()->changeWorkingDirectoryTo (wallpaper_path);
  123. }
  124. if (shouldEnableAudio == true)
  125. {
  126. int mixer_flags = MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_OGG;
  127. if (SDL_Init (SDL_INIT_AUDIO) < 0 || mixer_flags != Mix_Init (mixer_flags))
  128. {
  129. // Mix_GetError is an alias for SDL_GetError, so calling it directly will yield the correct result
  130. // it doesn't matter if SDL_Init or Mix_Init failed, both report the errors through the same functions
  131. IrrlichtContext->getDevice ()->getLogger ()->log ("Cannot initialize SDL audio system", SDL_GetError(),irr::ELL_ERROR);
  132. return 2;
  133. }
  134. // initialize audio engine
  135. Mix_OpenAudio (22050, AUDIO_S16SYS, 2, 640);
  136. }
  137. WallpaperEngine::Core::CProject* project = WallpaperEngine::Core::CProject::fromFile (project_path);
  138. WallpaperEngine::Render::CWallpaper* wallpaper;
  139. if (project->getType () == "scene")
  140. {
  141. WallpaperEngine::Core::CScene* scene = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ();
  142. wallpaper = new WallpaperEngine::Render::CScene (scene, IrrlichtContext);
  143. IrrlichtContext->getDevice ()->getSceneManager ()->setAmbientLight (
  144. scene->getAmbientColor ().toSColor ()
  145. );
  146. }
  147. else if (project->getType () == "video")
  148. {
  149. wallpaper = new WallpaperEngine::Render::CVideo (
  150. project->getWallpaper ()->as <WallpaperEngine::Core::CVideo> (),
  151. IrrlichtContext
  152. );
  153. }
  154. else
  155. {
  156. throw std::runtime_error ("Unsupported wallpaper type");
  157. }
  158. irr::u32 minimumTime = 1000 / maximumFPS;
  159. irr::u32 startTime = 0;
  160. irr::u32 endTime = 0;
  161. while (IrrlichtContext && IrrlichtContext->getDevice () && IrrlichtContext->getDevice ()->run ())
  162. {
  163. if (IrrlichtContext->getDevice ()->getVideoDriver () == nullptr)
  164. continue;
  165. startTime = IrrlichtContext->getDevice ()->getTimer ()->getTime ();
  166. IrrlichtContext->renderFrame (wallpaper);
  167. endTime = IrrlichtContext->getDevice ()->getTimer ()->getTime ();
  168. IrrlichtContext->getDevice ()->sleep (minimumTime - (endTime - startTime), false);
  169. }
  170. SDL_Quit ();
  171. return 0;
  172. }