main.cpp 9.2 KB

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