main.cpp 6.4 KB

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