CApplicationContext.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "CApplicationContext.h"
  2. #include "Steam/FileSystem/FileSystem.h"
  3. #include "WallpaperEngine/Logging/CLog.h"
  4. #include <cstring>
  5. #include <getopt.h>
  6. #include <glm/common.hpp>
  7. #include <string>
  8. #define WORKSHOP_APP_ID 431960
  9. #define APP_DIRECTORY "wallpaper_engine"
  10. using namespace WallpaperEngine::Application;
  11. struct option long_options [] = {
  12. {"screen-root", required_argument, 0, 'r'},
  13. {"pkg", required_argument, 0, 'p'},
  14. {"dir", required_argument, 0, 'd'},
  15. {"silent", no_argument, 0, 's'},
  16. {"volume", required_argument, 0, 'v'},
  17. {"help", no_argument, 0, 'h'},
  18. {"fps", required_argument, 0, 'f'},
  19. {"assets-dir", required_argument, 0, 'a'},
  20. {"screenshot", required_argument, 0, 'c'},
  21. {"list-properties", no_argument, 0, 'l'},
  22. {"set-property", required_argument, 0, 'o'},
  23. {nullptr, 0, 0, 0}
  24. };
  25. std::string stringPathFixes(const std::string& s)
  26. {
  27. if (s.empty () == true)
  28. return s;
  29. std::string str (s);
  30. // remove single-quotes from the arguments
  31. if (str [0] == '\'' && str [str.size() - 1] == '\'')
  32. str
  33. .erase (str.size() - 1, 1)
  34. .erase (0, 1);
  35. return std::move (str);
  36. }
  37. CApplicationContext::CApplicationContext (int argc, char* argv[]) :
  38. takeScreenshot (false),
  39. maximumFPS (30),
  40. audioVolume (128),
  41. audioEnabled (true),
  42. onlyListProperties (false)
  43. {
  44. int c;
  45. while ((c = getopt_long (argc, argv, "r:p:d:shf:a:", long_options, nullptr)) != -1)
  46. {
  47. switch (c)
  48. {
  49. case 'o':
  50. {
  51. std::string value = optarg;
  52. std::string::size_type equals = value.find ('=');
  53. // properties without value are treated as booleans for now
  54. if (equals == std::string::npos)
  55. this->properties.insert_or_assign (value, "1");
  56. else
  57. this->properties.insert_or_assign (
  58. value.substr (0, equals),
  59. value.substr (equals + 1)
  60. );
  61. }
  62. break;
  63. case 'l':
  64. this->onlyListProperties = true;
  65. break;
  66. case 'r':
  67. screens.emplace_back (optarg);
  68. break;
  69. case 'p':
  70. case 'd':
  71. sLog.error ("--dir/--pkg is deprecated and not used anymore");
  72. this->background = stringPathFixes (optarg);
  73. break;
  74. case 's':
  75. this->audioEnabled = false;
  76. break;
  77. case 'h':
  78. this->printHelp (argv [0]);
  79. break;
  80. case 'f':
  81. maximumFPS = atoi (optarg);
  82. break;
  83. case 'a':
  84. this->assets = stringPathFixes (optarg);
  85. break;
  86. case 'v':
  87. this->audioVolume = glm::clamp (atoi (optarg), 0, 128);
  88. break;
  89. case 'c':
  90. this->takeScreenshot = true;
  91. this->screenshot = stringPathFixes (optarg);
  92. break;
  93. }
  94. }
  95. if (this->background.empty () == true)
  96. {
  97. if (optind < argc && strlen (argv [optind]) > 0)
  98. {
  99. this->background = argv [optind];
  100. }
  101. else
  102. {
  103. this->printHelp (argv [0]);
  104. }
  105. }
  106. // perform some extra validation on the inputs
  107. this->validatePath ();
  108. this->validateAssets ();
  109. this->validateScreenshot ();
  110. }
  111. void CApplicationContext::validatePath ()
  112. {
  113. if (this->background.find ('/') != std::string::npos)
  114. return;
  115. this->background = Steam::FileSystem::workshopDirectory (WORKSHOP_APP_ID, this->background);
  116. }
  117. void CApplicationContext::validateAssets ()
  118. {
  119. if (this->assets.empty () == false)
  120. {
  121. sLog.out ("Using wallpaper engine's assets at ", this->assets, " based on --assets-dir parameter");
  122. return;
  123. }
  124. try
  125. {
  126. this->assets = Steam::FileSystem::appDirectory (APP_DIRECTORY, "assets");
  127. }
  128. catch (std::runtime_error&)
  129. {
  130. // set current path as assets' folder
  131. std::filesystem::path directory = std::filesystem::canonical ("/proc/self/exe")
  132. .parent_path () / "assets";
  133. }
  134. }
  135. void CApplicationContext::validateScreenshot ()
  136. {
  137. if (this->takeScreenshot == false)
  138. return;
  139. if (this->screenshot.has_extension () == false)
  140. sLog.exception ("Cannot determine screenshot format");
  141. std::string extension = this->screenshot.extension ();
  142. if (extension == ".bmp")
  143. this->screenshotFormat = FIF_BMP;
  144. else if (extension == ".png")
  145. this->screenshotFormat = FIF_PNG;
  146. else if (extension == ".jpg" || extension == ".jpeg")
  147. this->screenshotFormat = FIF_JPEG;
  148. else
  149. sLog.exception ("Cannot determine screenshot format, unknown extension ", extension);
  150. }
  151. void CApplicationContext::printHelp (const char* route)
  152. {
  153. sLog.out ("Usage: ", route, " [options] background_path/background_id");
  154. sLog.out ("");
  155. sLog.out ("where background_path/background_id can be:");
  156. sLog.out ("\tthe ID of the background (for autodetection on your steam installation)");
  157. sLog.out ("\ta full path to the background's folder");
  158. sLog.out ("");
  159. sLog.out ("options:");
  160. sLog.out ("\t--silent\t\t\t\t\tMutes all the sound the wallpaper might produce");
  161. sLog.out ("\t--volume <amount>\t\t\tSets the volume for all the sounds in the background (0 - 128)");
  162. sLog.out ("\t--screen-root <screen name>\tDisplay as screen's background");
  163. sLog.out ("\t--fps <maximum-fps>\t\t\tLimits the FPS to the given number, useful to keep battery consumption low");
  164. sLog.out ("\t--assets-dir <path>\t\t\tFolder where the assets are stored");
  165. sLog.out ("\t--screenshot\t\t\t\tTakes a screenshot of the background");
  166. sLog.out ("\t--list-properties\t\t\tList all the available properties and their possible values");
  167. sLog.out ("\t--set-property <name=value>\tOverrides the default value of the given property");
  168. }