main.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include <iostream>
  2. #include <getopt.h>
  3. #include <unistd.h>
  4. #include <SDL_mixer.h>
  5. #include <SDL.h>
  6. #include <FreeImage.h>
  7. #include <sys/stat.h>
  8. #include <GL/glew.h>
  9. #include <GL/glx.h>
  10. #include <filesystem>
  11. #include "GLFW/glfw3.h"
  12. #include "WallpaperEngine/Core/CProject.h"
  13. #include "WallpaperEngine/Render/CWallpaper.h"
  14. #include "WallpaperEngine/Render/CContext.h"
  15. #include "WallpaperEngine/Render/CScene.h"
  16. #include "WallpaperEngine/Render/CVideo.h"
  17. #include "WallpaperEngine/Assets/CPackage.h"
  18. #include "WallpaperEngine/Assets/CDirectory.h"
  19. #include "WallpaperEngine/Assets/CCombinedContainer.h"
  20. float g_Time;
  21. using namespace WallpaperEngine::Core::Types;
  22. void print_help (const char* route)
  23. {
  24. std::cout
  25. << "Usage:" << route << " [options] background_path" << std::endl
  26. << "options:" << std::endl
  27. << " --silent\t\tMutes all the sound the wallpaper might produce" << 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. {
  33. if (s.empty () == true)
  34. return s;
  35. std::string str (s);
  36. // remove single-quotes from the arguments
  37. if (str [0] == '\'' && str [str.size() - 1] == '\'')
  38. str
  39. .erase (str.size() - 1, 1)
  40. .erase (0, 1);
  41. // ensure there's a slash at the end of the path
  42. if (str [str.size() - 1] != '/')
  43. str += '/';
  44. return std::move (str);
  45. }
  46. int main (int argc, char* argv[])
  47. {
  48. std::vector <std::string> screens;
  49. int maximumFPS = 30;
  50. bool shouldEnableAudio = true;
  51. std::string path;
  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, nullptr);
  64. if (c == -1)
  65. break;
  66. switch (c)
  67. {
  68. case 'r':
  69. screens.emplace_back (optarg);
  70. break;
  71. case 'p':
  72. case 'd':
  73. std::cerr << "--dir/--pkg is deprecated and not used anymore" << std::endl;
  74. path = stringPathFixes (optarg);
  75. break;
  76. case 's':
  77. shouldEnableAudio = false;
  78. break;
  79. case 'h':
  80. print_help (argv [0]);
  81. break;
  82. case 'f':
  83. maximumFPS = atoi (optarg);
  84. break;
  85. default:
  86. break;
  87. }
  88. }
  89. // increment the option index (useful for when no options were found)
  90. // option_index ++;
  91. if (path.empty () == true)
  92. {
  93. if (optind < argc && strlen (argv [optind]) > 0)
  94. {
  95. path = argv [optind];
  96. }
  97. else
  98. {
  99. print_help (argv [0]);
  100. return 0;
  101. }
  102. }
  103. char* finalPath = realpath(path.c_str (), nullptr);
  104. if (finalPath == nullptr)
  105. {
  106. if (errno == ENAMETOOLONG)
  107. throw std::runtime_error ("Cannot get wallpaper's folder, path is too long");
  108. else
  109. throw std::runtime_error ("Cannot find the specified folder");
  110. }
  111. // ensure the path points to a folder
  112. struct stat pathinfo;
  113. if (stat (finalPath, &pathinfo) != 0)
  114. throw std::runtime_error ("Cannot find the specified wallpaper folder");
  115. if (!S_ISDIR (pathinfo.st_mode))
  116. throw std::runtime_error ("The specified path is not a folder");
  117. // ensure the path has a trailing slash
  118. // first of all, initialize the window
  119. if (glfwInit () == GLFW_FALSE)
  120. {
  121. fprintf (stderr, "Failed to initialize GLFW\n");
  122. return 1;
  123. }
  124. // initialize freeimage
  125. FreeImage_Initialise (TRUE);
  126. // set some window hints (opengl version to be used)
  127. glfwWindowHint (GLFW_SAMPLES, 4);
  128. glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 2);
  129. glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 1);
  130. // will hide the window if we are drawing to X
  131. if (!screens.empty())
  132. glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
  133. auto containers = new WallpaperEngine::Assets::CCombinedContainer ();
  134. // update the used path with the full one
  135. path = finalPath;
  136. path += "/";
  137. // the background's path is required to load project.json regardless of the type of background we're using
  138. containers->add (new WallpaperEngine::Assets::CDirectory (path));
  139. // check if scene.pkg exists and add it to the list
  140. try
  141. {
  142. std::string scene_path = path + "scene.pkg";
  143. // add the package to the list
  144. containers->add (new WallpaperEngine::Assets::CPackage (scene_path));
  145. std::cout << "Detected scene.pkg file at " << scene_path << ". Adding to list of searchable paths" << std::endl;
  146. }
  147. catch(std::filesystem::filesystem_error ex)
  148. {
  149. // ignore this error, the package file was not found
  150. std::cout << "No scene.pkg file found at " << path << ". Defaulting to normal folder storage" << std::endl;
  151. }
  152. catch (std::runtime_error ex)
  153. {
  154. // the package was found but there was an error loading it (wrong header or something)
  155. fprintf (stderr, "Failed to load scene.pkg file: %s\n", ex.what());
  156. return 4;
  157. }
  158. // add containers to the list
  159. containers->add (new WallpaperEngine::Assets::CDirectory ("./assets/"));
  160. // parse the project.json file
  161. auto project = WallpaperEngine::Core::CProject::fromFile ("project.json", containers);
  162. WallpaperEngine::Render::CWallpaper* wallpaper;
  163. // initialize custom context class
  164. WallpaperEngine::Render::CContext* context = new WallpaperEngine::Render::CContext (screens);
  165. // auto projection = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ()->getOrthogonalProjection ();
  166. // create the window!
  167. // TODO: DO WE NEED TO PASS MONITOR HERE OR ANYTHING?
  168. // TODO: FIGURE OUT HOW TO PUT THIS WINDOW IN THE BACKGROUND
  169. GLFWwindow* window = glfwCreateWindow (1920, 1080, "WallpaperEngine", NULL, NULL);
  170. if (window == nullptr)
  171. {
  172. fprintf (stderr, "Failed to open a GLFW window");
  173. glfwTerminate ();
  174. return 2;
  175. }
  176. // initialize inputs
  177. CMouseInput* mouseInput = new CMouseInput (window);
  178. context->setMouse (mouseInput);
  179. glfwMakeContextCurrent (window);
  180. // TODO: FIGURE THESE OUT BASED ON THE SCREEN
  181. int windowWidth = 1920;
  182. int windowHeight = 1080;
  183. // get the real framebuffer size
  184. glfwGetFramebufferSize (window, &windowWidth, &windowHeight);
  185. // set the default viewport
  186. context->setDefaultViewport ({0, 0, windowWidth, windowHeight});
  187. if (glewInit () != GLEW_OK)
  188. {
  189. fprintf (stderr, "Failed to initialize GLEW");
  190. glfwTerminate ();
  191. return 3;
  192. }
  193. if (project->getType () == "scene")
  194. {
  195. WallpaperEngine::Core::CScene* scene = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ();
  196. wallpaper = new WallpaperEngine::Render::CScene (scene, containers, context);
  197. }
  198. else if (project->getType () == "video")
  199. {
  200. // special steps, running a video needs a root directory change, files are not loaded from the container classes
  201. // as they're streamed from disk
  202. chdir (path.c_str ());
  203. WallpaperEngine::Core::CVideo* video = project->getWallpaper ()->as <WallpaperEngine::Core::CVideo> ();
  204. wallpaper = new WallpaperEngine::Render::CVideo (video, containers, context);
  205. }
  206. else
  207. {
  208. throw std::runtime_error ("Unsupported wallpaper type");
  209. }
  210. // ensure the context knows what wallpaper to render
  211. context->setWallpaper (wallpaper);
  212. if (shouldEnableAudio == true)
  213. {
  214. int mixer_flags = MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_OGG;
  215. if (SDL_Init (SDL_INIT_AUDIO) < 0 || mixer_flags != Mix_Init (mixer_flags))
  216. {
  217. // Mix_GetError is an alias for SDL_GetError, so calling it directly will yield the correct result
  218. // it doesn't matter if SDL_Init or Mix_Init failed, both report the errors through the same functions
  219. fprintf (stderr, "Cannot initialize SDL audio system, SDL_GetError: %s", SDL_GetError ());
  220. return 2;
  221. }
  222. // initialize audio engine
  223. Mix_OpenAudio (22050, AUDIO_S16SYS, 2, 640);
  224. }
  225. // TODO: FIGURE OUT THE REQUIRED INPUT MODE, AS SOME WALLPAPERS USE THINGS LIKE MOUSE POSITION
  226. // glfwSetInputMode (window, GLFW_STICKY_KEYS, GL_TRUE);
  227. // enable depth text
  228. glEnable (GL_DEPTH_TEST);
  229. glDepthFunc (GL_LESS);
  230. // cull anything that doesn't look at the camera (might be useful to disable in the future)
  231. glDisable (GL_CULL_FACE);
  232. double minimumTime = 1.0 / maximumFPS;
  233. double startTime = 0.0;
  234. double endTime = 0.0;
  235. while (glfwWindowShouldClose (window) == 0)
  236. {
  237. // get the real framebuffer size
  238. glfwGetFramebufferSize (window, &windowWidth, &windowHeight);
  239. // set the default viewport
  240. context->setDefaultViewport ({0, 0, windowWidth, windowHeight});
  241. // calculate the current time value
  242. g_Time = (float) glfwGetTime ();
  243. // get the start time of the frame
  244. startTime = glfwGetTime ();
  245. // update our inputs first
  246. mouseInput->update ();
  247. // render the scene
  248. context->render ();
  249. // do buffer swapping
  250. glfwSwapBuffers (window);
  251. // poll for events (like closing the window)
  252. glfwPollEvents ();
  253. // get the end time of the frame
  254. endTime = glfwGetTime ();
  255. // ensure the frame time is correct to not overrun FPS
  256. if ((endTime - startTime) < minimumTime)
  257. usleep ((minimumTime - (endTime - startTime)) * CLOCKS_PER_SEC);
  258. }
  259. // terminate gl
  260. glfwTerminate ();
  261. // terminate SDL
  262. SDL_Quit ();
  263. // terminate free image
  264. FreeImage_DeInitialise ();
  265. // free paths
  266. free(finalPath);
  267. return 0;
  268. }