main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include <iostream>
  2. #include <irrlicht/irrlicht.h>
  3. #include <sstream>
  4. #include <WallpaperEngine/Irrlicht/CPkgReader.h>
  5. #include <getopt.h>
  6. #include <SDL_mixer.h>
  7. #include <SDL.h>
  8. #include <X11/Xlib.h>
  9. #include <X11/extensions/Xrandr.h>
  10. #include "WallpaperEngine/Render/Shaders/Compiler.h"
  11. #include "WallpaperEngine/Irrlicht/CImageLoaderTEX.h"
  12. #include "WallpaperEngine/Core/CProject.h"
  13. #include "WallpaperEngine/Irrlicht/CContext.h"
  14. #include "WallpaperEngine/Render/CScene.h"
  15. bool IsRootWindow = false;
  16. std::vector<std::string> Screens;
  17. std::vector<irr::core::rect<irr::s32>> Viewports;
  18. irr::SIrrlichtCreationParameters _irr_params;
  19. irr::f32 g_Time = 0;
  20. WallpaperEngine::Irrlicht::CContext* IrrlichtContext;
  21. void initialize_viewports ()
  22. {
  23. if (IsRootWindow == false || Screens.size () == 0)
  24. return;
  25. Display* display = XOpenDisplay (NULL);
  26. int xrandr_result, xrandr_error;
  27. if (!XRRQueryExtension (display, &xrandr_result, &xrandr_error))
  28. {
  29. std::cerr << "XRandr is not present, cannot detect specified screens, running in window mode" << std::endl;
  30. return;
  31. }
  32. XRRScreenResources* screenResources = XRRGetScreenResources (display, DefaultRootWindow (display));
  33. // there are some situations where xrandr returns null (like screen not using the extension)
  34. if (screenResources == nullptr)
  35. return;
  36. for (int i = 0; i < screenResources->noutput; i ++)
  37. {
  38. XRROutputInfo* info = XRRGetOutputInfo (display, screenResources, screenResources->outputs [i]);
  39. // there are some situations where xrandr returns null (like screen not using the extension)
  40. if (info == nullptr)
  41. continue;
  42. std::vector<std::string>::iterator cur = Screens.begin ();
  43. std::vector<std::string>::iterator end = Screens.end ();
  44. for (; cur != end; cur ++)
  45. {
  46. if (info->connection == RR_Connected && strcmp (info->name, (*cur).c_str ()) == 0)
  47. {
  48. XRRCrtcInfo* crtc = XRRGetCrtcInfo (display, screenResources, info->crtc);
  49. std::cout << "Found requested screen: " << info->name << " -> " << crtc->x << "x" << crtc->y << ":" << crtc->width << "x" << crtc->height << std::endl;
  50. irr::core::rect<irr::s32> viewport;
  51. viewport.UpperLeftCorner.X = crtc->x;
  52. viewport.UpperLeftCorner.Y = crtc->y;
  53. viewport.LowerRightCorner.X = crtc->x + crtc->width;
  54. viewport.LowerRightCorner.Y = crtc->y + crtc->height;
  55. Viewports.push_back (viewport);
  56. XRRFreeCrtcInfo (crtc);
  57. }
  58. }
  59. XRRFreeOutputInfo (info);
  60. }
  61. XRRFreeScreenResources (screenResources);
  62. _irr_params.WindowId = reinterpret_cast<void*> (DefaultRootWindow (display));
  63. }
  64. int init_irrlicht()
  65. {
  66. IrrlichtContext = new WallpaperEngine::Irrlicht::CContext ();
  67. // prepare basic configuration for irrlicht
  68. _irr_params.AntiAlias = 8;
  69. _irr_params.Bits = 16;
  70. // _irr_params.DeviceType = Irrlicht::EIDT_X11;
  71. _irr_params.DriverType = irr::video::EDT_OPENGL;
  72. _irr_params.Doublebuffer = false;
  73. _irr_params.EventReceiver = nullptr;
  74. _irr_params.Fullscreen = false;
  75. _irr_params.HandleSRGB = false;
  76. _irr_params.IgnoreInput = true;
  77. _irr_params.Stencilbuffer = true;
  78. _irr_params.UsePerformanceTimer = false;
  79. _irr_params.Vsync = false;
  80. _irr_params.WithAlphaChannel = false;
  81. _irr_params.ZBufferBits = 24;
  82. _irr_params.LoggingLevel = irr::ELL_DEBUG;
  83. initialize_viewports ();
  84. IrrlichtContext->setDevice (irr::createDeviceEx (_irr_params));
  85. if (IrrlichtContext->getDevice () == nullptr)
  86. {
  87. return 1;
  88. }
  89. IrrlichtContext->getDevice ()->setWindowCaption (L"Test game");
  90. // check for ps and vs support
  91. if (
  92. IrrlichtContext->getDevice ()->getVideoDriver()->queryFeature (irr::video::EVDF_PIXEL_SHADER_1_1) == false &&
  93. IrrlichtContext->getDevice ()->getVideoDriver()->queryFeature (irr::video::EVDF_ARB_FRAGMENT_PROGRAM_1) == false)
  94. {
  95. IrrlichtContext->getDevice ()->getLogger ()->log ("WARNING: Pixel shaders disabled because of missing driver/hardware support");
  96. }
  97. if (
  98. IrrlichtContext->getDevice ()->getVideoDriver()->queryFeature (irr::video::EVDF_VERTEX_SHADER_1_1) == false &&
  99. IrrlichtContext->getDevice ()->getVideoDriver()->queryFeature (irr::video::EVDF_ARB_VERTEX_PROGRAM_1) == false)
  100. {
  101. IrrlichtContext->getDevice ()->getLogger ()->log ("WARNING: Vertex shaders disabled because of missing driver/hardware support");
  102. }
  103. return 0;
  104. }
  105. void preconfigure_wallpaper_engine ()
  106. {
  107. // load the assets from wallpaper engine
  108. IrrlichtContext->getDevice ()->getFileSystem ()->addFileArchive ("assets.zip", true, false);
  109. // register custom loaders
  110. IrrlichtContext->getDevice ()->getVideoDriver()->addExternalImageLoader (
  111. new WallpaperEngine::Irrlicht::CImageLoaderTex (IrrlichtContext)
  112. );
  113. IrrlichtContext->getDevice ()->getFileSystem ()->addArchiveLoader (
  114. new WallpaperEngine::Irrlicht::CArchiveLoaderPkg (IrrlichtContext)
  115. );
  116. }
  117. void print_help (const char* route)
  118. {
  119. std::cout
  120. << "Usage:" << route << " [options] " << std::endl
  121. << "options:" << std::endl
  122. << " --silent\t\tMutes all the sound the wallpaper might produce" << std::endl
  123. << " --dir <folder>\tLoads an uncompressed background from the given <folder>" << std::endl
  124. << " --pkg <folder>\tLoads a scene.pkg file from the given <folder>" << std::endl
  125. << " --screen-root <screen name>\tDisplay as screen's background" << std::endl
  126. << " --fps <maximum-fps>\tLimits the FPS to the given number, useful to keep battery consumption low" << std::endl;
  127. }
  128. std::string stringPathFixes(const std::string& s){
  129. std::string str(s);
  130. if(str.empty())
  131. return s;
  132. if(str[0] == '\'' && str[str.size() - 1] == '\''){
  133. str.erase(str.size() - 1, 1);
  134. str.erase(0,1);
  135. }
  136. if(str[str.size() - 1] != '/')
  137. str += '/';
  138. return std::move(str);
  139. }
  140. int main (int argc, char* argv[])
  141. {
  142. int mode = 0;
  143. int max_fps = 30;
  144. bool audio_support = true;
  145. std::string path;
  146. int option_index = 0;
  147. static struct option long_options [] = {
  148. {"screen-root", required_argument, 0, 'r'},
  149. {"pkg", required_argument, 0, 'p'},
  150. {"dir", required_argument, 0, 'd'},
  151. {"silent", no_argument, 0, 's'},
  152. {"help", no_argument, 0, 'h'},
  153. {"fps", required_argument, 0, 'f'},
  154. {nullptr, 0, 0, 0}
  155. };
  156. while (true)
  157. {
  158. int c = getopt_long (argc, argv, "r:p:d:shf:", long_options, &option_index);
  159. if (c == -1)
  160. break;
  161. switch (c)
  162. {
  163. case 'r':
  164. IsRootWindow = true;
  165. Screens.push_back (optarg);
  166. break;
  167. case 'p':
  168. mode = 1;
  169. path = optarg;
  170. break;
  171. case 'd':
  172. mode = 2;
  173. path = optarg;
  174. break;
  175. case 's':
  176. audio_support = false;
  177. break;
  178. case 'h':
  179. print_help (argv [0]);
  180. return 0;
  181. case 'f':
  182. max_fps = atoi (optarg);
  183. break;
  184. default:
  185. break;
  186. }
  187. }
  188. if (init_irrlicht ())
  189. {
  190. return 1;
  191. }
  192. preconfigure_wallpaper_engine ();
  193. irr::io::path wallpaper_path;
  194. irr::io::path project_path;
  195. irr::io::path scene_path;
  196. switch (mode)
  197. {
  198. case 0:
  199. print_help (argv [0]);
  200. return 0;
  201. // pkg mode
  202. case 1:
  203. path = stringPathFixes(path);
  204. wallpaper_path = IrrlichtContext->getDevice ()->getFileSystem ()->getAbsolutePath (path.c_str ());
  205. project_path = wallpaper_path + "project.json";
  206. scene_path = wallpaper_path + "scene.pkg";
  207. IrrlichtContext->getDevice ()->getFileSystem ()->addFileArchive (scene_path, true, false); // add the pkg file to the lookup list
  208. break;
  209. // folder mode
  210. case 2:
  211. path = stringPathFixes(path);
  212. wallpaper_path = IrrlichtContext->getDevice ()->getFileSystem ()->getAbsolutePath (path.c_str ());
  213. project_path = wallpaper_path + "project.json";
  214. // set our working directory
  215. IrrlichtContext->getDevice ()->getFileSystem ()->changeWorkingDirectoryTo (wallpaper_path);
  216. break;
  217. default:
  218. break;
  219. }
  220. if (audio_support == true)
  221. {
  222. int mixer_flags = MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_OGG;
  223. if (SDL_Init (SDL_INIT_AUDIO) < 0 || mixer_flags != Mix_Init (mixer_flags))
  224. {
  225. IrrlichtContext->getDevice ()->getLogger ()->log ("Cannot initialize SDL audio system", irr::ELL_ERROR);
  226. return -1;
  227. }
  228. // initialize audio engine
  229. Mix_OpenAudio (22050, AUDIO_S16SYS, 2, 640);
  230. }
  231. WallpaperEngine::Core::CProject* project = WallpaperEngine::Core::CProject::fromFile (project_path);
  232. WallpaperEngine::Render::CScene* sceneRender = new WallpaperEngine::Render::CScene (project, IrrlichtContext);
  233. irr::u32 lastTime = 0;
  234. irr::u32 minimumTime = 1000 / max_fps;
  235. irr::u32 currentTime = 0;
  236. irr::u32 startTime = 0;
  237. irr::u32 endTime = 0;
  238. while (IrrlichtContext && IrrlichtContext->getDevice () && IrrlichtContext->getDevice ()->run ())
  239. {
  240. if (IrrlichtContext->getDevice ()->getVideoDriver () == nullptr)
  241. continue;
  242. // if (device->isWindowActive ())
  243. {
  244. currentTime = startTime = IrrlichtContext->getDevice ()->getTimer ()->getTime ();
  245. g_Time = currentTime / 1000.0f;
  246. if (Viewports.size () > 0)
  247. {
  248. std::vector<irr::core::rect<irr::s32>>::iterator cur = Viewports.begin ();
  249. std::vector<irr::core::rect<irr::s32>>::iterator end = Viewports.end ();
  250. for (; cur != end; cur ++)
  251. {
  252. // change viewport to render to the correct portion of the display
  253. IrrlichtContext->getDevice ()->getVideoDriver ()->setViewPort (*cur);
  254. IrrlichtContext->getDevice ()->getVideoDriver ()->beginScene (false, true, sceneRender->getScene ()->getClearColor ().toSColor());
  255. IrrlichtContext->getDevice ()->getSceneManager ()->drawAll ();
  256. IrrlichtContext->getDevice ()->getVideoDriver ()->endScene ();
  257. }
  258. }
  259. else
  260. {
  261. IrrlichtContext->getDevice ()->getVideoDriver ()->beginScene (true, true, sceneRender->getScene ()->getClearColor ().toSColor());
  262. IrrlichtContext->getDevice ()->getSceneManager ()->drawAll ();
  263. IrrlichtContext->getDevice ()->getVideoDriver ()->endScene ();
  264. }
  265. endTime = IrrlichtContext->getDevice ()->getTimer ()->getTime ();
  266. IrrlichtContext->getDevice ()->sleep (minimumTime - (endTime - startTime), false);
  267. }
  268. }
  269. SDL_Quit ();
  270. return 0;
  271. }