ApplicationContext.cpp 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  1. #include "ApplicationContext.h"
  2. #include "Steam/FileSystem/FileSystem.h"
  3. #include "WallpaperEngine/Data/JSON.h"
  4. #include "WallpaperEngine/Logging/Log.h"
  5. #include "WallpaperEngine/Render/CFBO.h"
  6. #include "WallpaperEngine/Render/Wallpapers/CVideo.h"
  7. #include <algorithm>
  8. #include <charconv>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <fstream>
  12. #include <functional>
  13. #include <iostream>
  14. #include <optional>
  15. #include <sstream>
  16. #include <stdexcept>
  17. #include <string_view>
  18. #include <argparse/argparse.hpp>
  19. #define WORKSHOP_APP_ID 431960
  20. #define APP_DIRECTORY "wallpaper_engine"
  21. using namespace WallpaperEngine::Application;
  22. using WallpaperEngine::Data::Model::ImageAdjustments;
  23. namespace {
  24. // CEF and --web-host children re-run the whole argument parsing, the parent already reported all of it
  25. bool isHelperProcess (int argc, char** argv, bool webHost) {
  26. if (webHost) {
  27. return true;
  28. }
  29. for (int i = 1; i < argc; i++) {
  30. if (std::string_view (argv[i]).starts_with ("--type=")) {
  31. return true;
  32. }
  33. }
  34. return false;
  35. }
  36. } // namespace
  37. using WallpaperEngine::Data::JSON::JSON;
  38. std::filesystem::path ApplicationContext::resolvePlaylistItemPath (const std::string& raw) const {
  39. if (raw.empty ()) {
  40. return {};
  41. }
  42. std::string cleaned = raw;
  43. constexpr std::string_view windowsPrefix = "\\\\?\\";
  44. if (cleaned.rfind (windowsPrefix, 0) == 0) {
  45. cleaned = cleaned.substr (windowsPrefix.length ());
  46. }
  47. std::replace (cleaned.begin (), cleaned.end (), '\\', '/');
  48. if (cleaned.size () > 1 && cleaned[1] == ':') {
  49. cleaned = cleaned.substr (2);
  50. }
  51. if (!cleaned.empty () && cleaned.front () != '/') {
  52. cleaned.insert (cleaned.begin (), '/');
  53. }
  54. std::filesystem::path path = std::filesystem::path (cleaned).lexically_normal ();
  55. if (std::filesystem::is_regular_file (path)) {
  56. path = path.parent_path ();
  57. }
  58. return path;
  59. }
  60. std::filesystem::path ApplicationContext::configFilePath () const {
  61. try {
  62. return Steam::FileSystem::appDirectory (APP_DIRECTORY, "") / "config.json";
  63. } catch (std::runtime_error&) {
  64. sLog.exception ("Cannot locate wallpaper engine installation to read config.json");
  65. return {};
  66. }
  67. }
  68. std::optional<JSON> ApplicationContext::parseConfigJson (const std::filesystem::path& path) const {
  69. if (path.empty ()) {
  70. return std::nullopt;
  71. }
  72. std::ifstream configFile (path);
  73. if (!configFile.is_open ()) {
  74. sLog.exception ("Cannot open wallpaper engine config file at ", path);
  75. return std::nullopt;
  76. }
  77. try {
  78. return JSON::parse (configFile);
  79. } catch (const std::exception& e) {
  80. sLog.error ("Failed parsing wallpaper engine config.json: ", e.what ());
  81. return std::nullopt;
  82. }
  83. }
  84. std::optional<ApplicationContext::PlaylistDefinition>
  85. ApplicationContext::buildPlaylistDefinition (const JSON& playlistJson, const std::string& fallbackName) const {
  86. PlaylistDefinition definition;
  87. definition.name = playlistJson.optional<std::string> ("name", fallbackName);
  88. definition.settings = this->parsePlaylistSettings (playlistJson);
  89. definition.items
  90. = this->collectPlaylistItems (playlistJson, definition.name.empty () ? fallbackName : definition.name);
  91. if (definition.items.empty ()) {
  92. return std::nullopt;
  93. }
  94. if (definition.name.empty ()) {
  95. if (fallbackName.empty ()) {
  96. sLog.error ("Skipping playlist with no name");
  97. return std::nullopt;
  98. }
  99. definition.name = fallbackName;
  100. }
  101. return definition;
  102. }
  103. ApplicationContext::PlaylistSettings ApplicationContext::parsePlaylistSettings (const JSON& playlistJson) const {
  104. PlaylistSettings settings;
  105. const auto settingsJson = playlistJson.optional ("settings");
  106. settings.delayMinutes = settingsJson ? settingsJson->optional<uint32_t> ("delay", 60) : 60;
  107. settings.mode = settingsJson ? settingsJson->optional<std::string> ("mode", "timer") : "timer";
  108. settings.order = settingsJson ? settingsJson->optional<std::string> ("order", "sequential") : "sequential";
  109. settings.updateOnPause = settingsJson ? settingsJson->optional<bool> ("updateonpause", false) : false;
  110. settings.videoSequence = settingsJson ? settingsJson->optional<bool> ("videosequence", false) : false;
  111. return settings;
  112. }
  113. std::vector<std::filesystem::path>
  114. ApplicationContext::collectPlaylistItems (const JSON& playlistJson, const std::string& name) const {
  115. std::vector<std::filesystem::path> items;
  116. const auto jsonItems = playlistJson.optional ("items");
  117. if (!jsonItems.has_value () || !jsonItems->is_array ()) {
  118. sLog.error ("Skipping playlist ", name, ": missing items");
  119. return items;
  120. }
  121. for (const auto& rawItem : *jsonItems) {
  122. if (!rawItem.is_string ()) {
  123. continue;
  124. }
  125. auto resolvedPath = this->resolvePlaylistItemPath (rawItem.get<std::string> ());
  126. if (resolvedPath.empty ()) {
  127. continue;
  128. }
  129. if (!std::filesystem::exists (resolvedPath)) {
  130. sLog.error ("Skipping playlist item not found: ", resolvedPath.string ());
  131. continue;
  132. }
  133. items.push_back (resolvedPath);
  134. }
  135. if (items.empty ()) {
  136. sLog.error ("Skipping playlist ", name, ": no usable items found");
  137. }
  138. return items;
  139. }
  140. void ApplicationContext::registerPlaylist (PlaylistDefinition&& definition) {
  141. this->m_configPlaylists.insert_or_assign (definition.name, std::move (definition));
  142. }
  143. void ApplicationContext::loadPlaylistsFromConfig () {
  144. if (this->m_loadedConfigPlaylists) {
  145. return;
  146. }
  147. this->m_loadedConfigPlaylists = true;
  148. const auto configPath = this->configFilePath ();
  149. const auto root = this->parseConfigJson (configPath);
  150. if (!root.has_value ()) {
  151. return;
  152. }
  153. const auto steamUser = root->optional ("steamuser");
  154. if (!steamUser.has_value ()) {
  155. sLog.exception ("Cannot find steamuser section in config.json");
  156. }
  157. auto addPlaylist = [this] (const JSON& playlistJson, const std::string& fallbackName) {
  158. auto definition = this->buildPlaylistDefinition (playlistJson, fallbackName);
  159. if (!definition) {
  160. return;
  161. }
  162. this->registerPlaylist (std::move (*definition));
  163. };
  164. if (const auto general = steamUser->optional ("general")) {
  165. if (const auto playlists = general->optional ("playlists")) {
  166. for (const auto& playlist : *playlists) {
  167. try {
  168. addPlaylist (playlist, playlist.optional<std::string> ("name", ""));
  169. } catch (const std::exception& e) {
  170. sLog.error ("Failed parsing playlist: ", e.what ());
  171. }
  172. }
  173. }
  174. }
  175. if (const auto wallpaperConfig = steamUser->optional ("wallpaperconfig")) {
  176. if (const auto selected = wallpaperConfig->optional ("selectedwallpapers")) {
  177. for (const auto& entry : selected->items ()) {
  178. const auto playlist = entry.value ().optional ("playlist");
  179. if (!playlist.has_value ()) {
  180. continue;
  181. }
  182. try {
  183. addPlaylist (*playlist, entry.key ());
  184. } catch (const std::exception& e) {
  185. sLog.error ("Failed parsing playlist for ", entry.key (), ": ", e.what ());
  186. }
  187. }
  188. }
  189. }
  190. }
  191. const ApplicationContext::PlaylistDefinition& ApplicationContext::getPlaylistFromConfig (const std::string& name) {
  192. if (!this->m_loadedConfigPlaylists) {
  193. this->loadPlaylistsFromConfig ();
  194. }
  195. const auto cur = this->m_configPlaylists.find (name);
  196. if (cur == this->m_configPlaylists.end ()) {
  197. std::string available;
  198. for (auto it = this->m_configPlaylists.begin (); it != this->m_configPlaylists.end (); ++it) {
  199. available += it->first;
  200. if (std::next (it) != this->m_configPlaylists.end ()) {
  201. available += ", ";
  202. }
  203. }
  204. const std::string availableText = available.empty () ? "" : std::string (". Available: ") + available;
  205. sLog.exception ("Playlist not found in config.json: ", name, availableText);
  206. throw std::runtime_error ("Playlist not found: " + name);
  207. }
  208. return cur->second;
  209. }
  210. ApplicationContext::ApplicationContext (int argc, char* argv[]) : m_argc (argc), m_argv (argv) { }
  211. namespace {
  212. bool matchesObjectToken (const std::string& token, int id, const std::string& name) {
  213. if (token == name) {
  214. return true;
  215. }
  216. int parsed = 0;
  217. const auto* end = token.data () + token.size ();
  218. const auto result = std::from_chars (token.data (), end, parsed);
  219. return result.ec == std::errc () && result.ptr == end && parsed == id;
  220. }
  221. } // namespace
  222. std::optional<bool> ApplicationContext::resolveObjectVisibility (int id, const std::string& name) const {
  223. for (const auto& token : this->settings.general.disabledObjects) {
  224. if (matchesObjectToken (token, id, name)) {
  225. return false;
  226. }
  227. }
  228. for (const auto& token : this->settings.general.enabledObjects) {
  229. if (matchesObjectToken (token, id, name)) {
  230. return true;
  231. }
  232. }
  233. return std::nullopt;
  234. }
  235. std::optional<bool> ApplicationContext::resolveEffectVisibility (int id, const std::string& name) const {
  236. for (const auto& token : this->settings.general.disabledEffects) {
  237. if (matchesObjectToken (token, id, name)) {
  238. return false;
  239. }
  240. }
  241. for (const auto& token : this->settings.general.enabledEffects) {
  242. if (matchesObjectToken (token, id, name)) {
  243. return true;
  244. }
  245. }
  246. return std::nullopt;
  247. }
  248. std::optional<float> ApplicationContext::resolveAudioSensitivity (int id, const std::string& name) const {
  249. // "*" is a wildcard default, checked last so a specific id/name match always wins
  250. // regardless of the map's (alphabetically ordered) iteration order.
  251. std::optional<float> wildcard;
  252. for (const auto& [token, multiplier] : this->settings.general.audioSensitivity) {
  253. if (token == "*") {
  254. wildcard = multiplier;
  255. continue;
  256. }
  257. if (matchesObjectToken (token, id, name)) {
  258. return multiplier;
  259. }
  260. }
  261. return wildcard;
  262. }
  263. std::optional<ApplicationContext::HRTF_MODE> ApplicationContext::parseHrtfMode (const std::string& value) {
  264. if (value == "auto") {
  265. return HRTF_AUTO;
  266. }
  267. if (value == "on") {
  268. return HRTF_ON;
  269. }
  270. if (value == "off") {
  271. return HRTF_OFF;
  272. }
  273. return std::nullopt;
  274. }
  275. std::optional<float> ApplicationContext::resolveSoundVolume (int id, const std::string& name) const {
  276. // "*" is a wildcard default, checked last so a specific id/name match always wins (same rule as
  277. // resolveAudioSensitivity).
  278. std::optional<float> wildcard;
  279. for (const auto& [token, volume] : this->settings.general.soundVolume) {
  280. if (token == "*") {
  281. wildcard = volume;
  282. continue;
  283. }
  284. if (matchesObjectToken (token, id, name)) {
  285. return volume;
  286. }
  287. }
  288. return wildcard;
  289. }
  290. void ApplicationContext::loadSettingsFromArgv () {
  291. std::string lastScreen;
  292. argparse::ArgumentParser program ("linux-wallpaperengine", "0.0", argparse::default_arguments::help);
  293. auto& backgroundGroup = program.add_group ("Background options");
  294. auto& backgroundMode = backgroundGroup.add_mutually_exclusive_group (false);
  295. backgroundGroup.add_argument ("background id")
  296. .help ("The background to use as default for screens with no background specified")
  297. .default_value ("")
  298. .action ([this] (const std::string& value) -> void {
  299. if (!value.empty ()) {
  300. this->settings.general.defaultBackground = translateBackground (value);
  301. }
  302. });
  303. // Internal, hidden: marks a self-re-exec as a disposable CEF host for one Web wallpaper.
  304. backgroundGroup.add_argument ("--web-host").flag ().hidden ().store_into (this->settings.general.webHost);
  305. backgroundGroup.add_argument ("--web-host-shm")
  306. .default_value ("")
  307. .hidden ()
  308. .store_into (this->settings.general.webHostShm);
  309. backgroundGroup.add_argument ("--web-host-width")
  310. .default_value (0)
  311. .hidden ()
  312. .action ([this] (const std::string& value) -> void {
  313. try {
  314. this->settings.general.webHostWidth = static_cast<uint32_t> (std::stoul (value));
  315. } catch (const std::exception&) {
  316. sLog.exception ("Invalid --web-host-width value: ", value);
  317. }
  318. });
  319. backgroundGroup.add_argument ("--web-host-height")
  320. .default_value (0)
  321. .hidden ()
  322. .action ([this] (const std::string& value) -> void {
  323. try {
  324. this->settings.general.webHostHeight = static_cast<uint32_t> (std::stoul (value));
  325. } catch (const std::exception&) {
  326. sLog.exception ("Invalid --web-host-height value: ", value);
  327. }
  328. });
  329. backgroundMode.add_argument ("-w", "--window")
  330. .help ("Window geometry to use for the given screen")
  331. .action ([this] (const std::string& value) -> void {
  332. if (this->settings.render.mode == DESKTOP_BACKGROUND) {
  333. sLog.exception ("Cannot run in both background and window mode");
  334. }
  335. if (this->settings.render.mode == EXPLICIT_WINDOW) {
  336. sLog.exception ("Only one window at a time can be specified in explicit window mode");
  337. }
  338. this->settings.render.mode = EXPLICIT_WINDOW;
  339. if (value.empty ()) {
  340. sLog.exception ("Window geometry cannot be empty");
  341. }
  342. const char* str = value.c_str ();
  343. const char* delim1 = strchr (str, 'x');
  344. const char* delim2 = delim1 ? strchr (delim1 + 1, 'x') : nullptr;
  345. const char* delim3 = delim2 ? strchr (delim2 + 1, 'x') : nullptr;
  346. if (delim1 == nullptr || delim2 == nullptr || delim3 == nullptr) {
  347. sLog.exception ("Window geometry must be in the format: XxYxWxH");
  348. }
  349. this->settings.render.window.geometry.x = strtol (str, nullptr, 10);
  350. this->settings.render.window.geometry.y = strtol (delim1 + 1, nullptr, 10);
  351. this->settings.render.window.geometry.z = strtol (delim2 + 1, nullptr, 10);
  352. this->settings.render.window.geometry.w = strtol (delim3 + 1, nullptr, 10);
  353. })
  354. .append ();
  355. backgroundMode.add_argument ("-r", "--screen-root")
  356. .help ("The screen the following settings will have an effect on")
  357. .action ([this, &lastScreen] (const std::string& value) -> void {
  358. if (this->settings.general.screenBackgrounds.find (value)
  359. != this->settings.general.screenBackgrounds.end ()) {
  360. sLog.exception ("Cannot specify the same screen more than once: ", value);
  361. }
  362. for (const auto& group : this->settings.general.spanGroups) {
  363. if (std::find (group.screens.begin (), group.screens.end (), value) != group.screens.end ()) {
  364. sLog.exception ("--screen-root: screen '", value, "' already belongs to a span group");
  365. }
  366. }
  367. if (this->settings.render.mode == EXPLICIT_WINDOW) {
  368. sLog.exception ("Cannot run in both background and window mode");
  369. }
  370. this->settings.render.mode = DESKTOP_BACKGROUND;
  371. lastScreen = value;
  372. this->settings.general.screenBackgrounds[lastScreen] = "";
  373. this->settings.general.screenScalings[lastScreen] = this->settings.render.window.scalingMode;
  374. this->settings.general.screenClamps[lastScreen] = this->settings.render.window.clamp;
  375. })
  376. .append ();
  377. backgroundGroup.add_argument ("--screen-span")
  378. .help ("Comma-separated list of screens to span a single wallpaper across")
  379. .action ([this, &lastScreen] (const std::string& value) -> void {
  380. if (this->settings.render.mode == EXPLICIT_WINDOW) {
  381. sLog.exception ("Cannot run in both background and window mode");
  382. }
  383. this->settings.render.mode = DESKTOP_BACKGROUND;
  384. SpanGroup group;
  385. std::string screen;
  386. std::istringstream ss (value);
  387. while (std::getline (ss, screen, ',')) {
  388. if (screen.empty ()) {
  389. continue;
  390. }
  391. if (this->settings.general.screenBackgrounds.find (screen)
  392. != this->settings.general.screenBackgrounds.end ()) {
  393. sLog.exception ("--screen-span: screen '", screen, "' is already configured individually");
  394. }
  395. if (std::find (group.screens.begin (), group.screens.end (), screen) != group.screens.end ()) {
  396. sLog.exception ("--screen-span: duplicate screen name '", screen, "'");
  397. }
  398. for (const auto& existing : this->settings.general.spanGroups) {
  399. if (std::find (existing.screens.begin (), existing.screens.end (), screen)
  400. != existing.screens.end ()) {
  401. sLog.exception ("--screen-span: screen '", screen, "' already belongs to another span group");
  402. }
  403. }
  404. group.screens.push_back (screen);
  405. }
  406. if (group.screens.size () < 2) {
  407. sLog.exception ("--screen-span requires at least two comma-separated screen names");
  408. }
  409. group.scaling = this->settings.render.window.scalingMode;
  410. group.clamp = this->settings.render.window.clamp;
  411. group.alignment = this->settings.render.window.alignment;
  412. this->settings.general.spanGroups.push_back (std::move (group));
  413. // synthetic "span:" name lets --bg/--scaling/--clamp target this group
  414. lastScreen = "span:" + value;
  415. this->settings.general.screenBackgrounds[lastScreen] = "";
  416. })
  417. .append ();
  418. backgroundGroup.add_argument ("-b", "--bg")
  419. .help ("After --screen-root or --screen-span, specifies the background to use")
  420. .action ([this, &lastScreen] (const std::string& value) -> void {
  421. this->settings.general.screenBackgrounds[lastScreen] = translateBackground (value);
  422. this->settings.general.defaultBackground = translateBackground (value);
  423. if (lastScreen.rfind ("span:", 0) == 0 && !this->settings.general.spanGroups.empty ()) {
  424. this->settings.general.spanGroups.back ().background = translateBackground (value);
  425. }
  426. })
  427. .append ();
  428. backgroundGroup.add_argument ("--playlist")
  429. .help (
  430. "Uses a playlist from wallpaper engine's config.json. If used after --screen-root it is applied to that "
  431. "screen, otherwise it is used in window mode."
  432. )
  433. .action ([this, &lastScreen] (const std::string& value) -> void {
  434. const auto& playlist = this->getPlaylistFromConfig (value);
  435. if (lastScreen.empty ()) {
  436. this->settings.general.defaultPlaylist = playlist;
  437. if (this->settings.general.defaultBackground.empty () && !playlist.items.empty ()) {
  438. this->settings.general.defaultBackground = playlist.items.front ();
  439. }
  440. } else {
  441. this->settings.general.screenPlaylists[lastScreen] = playlist;
  442. if (!playlist.items.empty ()) {
  443. this->settings.general.screenBackgrounds[lastScreen] = playlist.items.front ();
  444. }
  445. if (this->settings.general.defaultBackground.empty () && !playlist.items.empty ()) {
  446. this->settings.general.defaultBackground = playlist.items.front ();
  447. }
  448. }
  449. })
  450. .append ();
  451. backgroundGroup.add_argument ("--scaling")
  452. .help (
  453. "Scaling mode to use when rendering the background, this applies to the previous --window, --screen-root, "
  454. "or --screen-span output, or the default background if no other background is specified"
  455. )
  456. .choices ("stretch", "fit", "fill", "center", "free", "default")
  457. .action ([this, &lastScreen] (const std::string& value) -> void {
  458. const auto parsed = WallpaperEngine::Render::WallpaperState::parseScalingMode (value);
  459. if (!parsed.has_value ()) {
  460. sLog.exception ("Invalid scaling mode: ", value);
  461. }
  462. const auto mode = *parsed;
  463. if (this->settings.render.mode == DESKTOP_BACKGROUND) {
  464. this->settings.general.screenScalings[lastScreen] = mode;
  465. if (lastScreen.rfind ("span:", 0) == 0 && !this->settings.general.spanGroups.empty ()) {
  466. this->settings.general.spanGroups.back ().scaling = mode;
  467. }
  468. } else {
  469. this->settings.render.window.scalingMode = mode;
  470. }
  471. })
  472. .append ();
  473. backgroundGroup.add_argument ("--clamp")
  474. .help (
  475. "Clamp mode to use when rendering the background, this applies to the previous --window, --screen-root, "
  476. "or --screen-span output, or the default background if no other background is specified"
  477. )
  478. .choices ("clamp", "border", "repeat")
  479. .action ([this, &lastScreen] (const std::string& value) -> void {
  480. TextureFlags flags;
  481. if (value == "clamp") {
  482. flags = TextureFlags_ClampUVs;
  483. } else if (value == "border") {
  484. flags = TextureFlags_ClampUVsBorder;
  485. } else if (value == "repeat") {
  486. flags = TextureFlags_NoFlags;
  487. } else {
  488. sLog.exception ("Invalid clamp mode: ", value);
  489. }
  490. if (this->settings.render.mode == DESKTOP_BACKGROUND) {
  491. this->settings.general.screenClamps[lastScreen] = flags;
  492. if (lastScreen.rfind ("span:", 0) == 0 && !this->settings.general.spanGroups.empty ()) {
  493. this->settings.general.spanGroups.back ().clamp = flags;
  494. }
  495. } else {
  496. this->settings.render.window.clamp = flags;
  497. }
  498. })
  499. .append ();
  500. backgroundGroup.add_argument ("--zoom")
  501. .help (
  502. "Manual zoom factor layered on top of --scaling (e.g. 1.5 zooms in 50%, 0.5 zooms out to half size), "
  503. "this applies to the previous --window, --screen-root, or --screen-span output, or the default "
  504. "background if no other background is specified"
  505. )
  506. .action ([this, &lastScreen] (const std::string& value) -> void {
  507. float zoom;
  508. try {
  509. zoom = std::stof (value);
  510. } catch (const std::exception&) {
  511. sLog.exception ("Invalid zoom value: ", value);
  512. }
  513. if (this->settings.render.mode == DESKTOP_BACKGROUND) {
  514. this->settings.general.screenZooms[lastScreen] = zoom;
  515. if (lastScreen.rfind ("span:", 0) == 0 && !this->settings.general.spanGroups.empty ()) {
  516. this->settings.general.spanGroups.back ().zoom = zoom;
  517. }
  518. } else {
  519. this->settings.render.window.zoom = zoom;
  520. }
  521. })
  522. .append ();
  523. backgroundGroup.add_argument ("--offset")
  524. .help (
  525. "Re-centers the visible crop window a cropping scaling mode (e.g. --scaling fill) or --zoom produces, "
  526. "as \"X,Y\" with each axis in [-1, 1] (0,0 is centered/default, e.g. -1,0 shows as much of the left "
  527. "edge as the crop allows, 0,1 shows as much of the top edge as the crop allows). Has no visible "
  528. "effect where nothing is being cropped. This applies to the previous --window, --screen-root, or "
  529. "--screen-span output, or the default background if no other background is specified"
  530. )
  531. // "-1,0" would otherwise look like another option to argparse
  532. .allow_optional_like_value ()
  533. .action ([this, &lastScreen] (const std::string& value) -> void {
  534. const auto comma = value.find (',');
  535. if (comma == std::string::npos) {
  536. sLog.exception ("Offset must be in the format: X,Y");
  537. }
  538. glm::vec2 offset;
  539. try {
  540. offset.x = std::stof (value.substr (0, comma));
  541. offset.y = std::stof (value.substr (comma + 1));
  542. } catch (const std::exception&) {
  543. sLog.exception ("Invalid offset value: ", value);
  544. }
  545. if (this->settings.render.mode == DESKTOP_BACKGROUND) {
  546. this->settings.general.screenOffsets[lastScreen] = offset;
  547. if (lastScreen.rfind ("span:", 0) == 0 && !this->settings.general.spanGroups.empty ()) {
  548. this->settings.general.spanGroups.back ().offset = offset;
  549. }
  550. } else {
  551. this->settings.render.window.offset = offset;
  552. }
  553. })
  554. .append ();
  555. // WE's alignment settings in its own units (sub_140181F30 divides them by 100)
  556. const auto addAlignmentArgument = [this, &backgroundGroup, &lastScreen] (
  557. const char* name, const char* help, const float maximum,
  558. float WallpaperEngine::Render::WallpaperState::Alignment::* field
  559. ) {
  560. backgroundGroup.add_argument (name)
  561. .help (help)
  562. .action ([this, &lastScreen, name, maximum, field] (const std::string& value) -> void {
  563. float parsed;
  564. try {
  565. parsed = std::stof (value);
  566. } catch (const std::exception&) {
  567. sLog.exception ("Invalid ", name, " value: ", value);
  568. }
  569. if (parsed < 0.0f || parsed > maximum) {
  570. sLog.exception (name, " must be between 0 and ", maximum, ": ", value);
  571. }
  572. parsed /= 100.0f;
  573. if (this->settings.render.mode == DESKTOP_BACKGROUND) {
  574. auto& alignment = this->settings.general.screenAlignments
  575. .try_emplace (lastScreen, this->settings.render.window.alignment)
  576. .first->second;
  577. alignment.*field = parsed;
  578. if (lastScreen.rfind ("span:", 0) == 0 && !this->settings.general.spanGroups.empty ()) {
  579. this->settings.general.spanGroups.back ().alignment.*field = parsed;
  580. }
  581. } else {
  582. this->settings.render.window.alignment.*field = parsed;
  583. }
  584. })
  585. .append ();
  586. };
  587. addAlignmentArgument (
  588. "--alignment-position",
  589. "Wallpaper Engine's alignment position, 0-100 (default 50): where --scaling fill/default crops (0 keeps the "
  590. "right or top edge) and --scaling fit places the letterboxed wallpaper (0 = left or top). This applies to the "
  591. "previous --window, --screen-root, or --screen-span output, or the default background",
  592. 100.0f, &WallpaperEngine::Render::WallpaperState::Alignment::position
  593. );
  594. addAlignmentArgument (
  595. "--alignment-x",
  596. "Wallpaper Engine's horizontal alignment for --scaling center and free, 0-100 (default 50, 0 = left). This "
  597. "applies to the previous --window, --screen-root, or --screen-span output, or the default background",
  598. 100.0f, &WallpaperEngine::Render::WallpaperState::Alignment::x
  599. );
  600. addAlignmentArgument (
  601. "--alignment-y",
  602. "Wallpaper Engine's vertical alignment for --scaling center and free, 0-100 (default 50, 0 = top). This "
  603. "applies to the previous --window, --screen-root, or --screen-span output, or the default background",
  604. 100.0f, &WallpaperEngine::Render::WallpaperState::Alignment::y
  605. );
  606. addAlignmentArgument (
  607. "--alignment-zoom",
  608. "Wallpaper Engine's zoom for --scaling free, 0-200 (default 100 = native size, higher zooms in). This applies "
  609. "to the previous --window, --screen-root, or --screen-span output, or the default background",
  610. 200.0f, &WallpaperEngine::Render::WallpaperState::Alignment::zoom
  611. );
  612. backgroundGroup.add_argument ("--corner-color")
  613. .help (
  614. "Color to show outside the wallpaper's bounds (Center/Fit letterboxing, zoomed-out scaling) when "
  615. "--clamp is border, as a hex RGB or RGBA value (e.g. \"000000\" or \"#1a1a1aff\"). Default: opaque "
  616. "black. This applies to the previous --window, --screen-root, or --screen-span output, or the "
  617. "default background if no other background is specified"
  618. )
  619. .action ([this, &lastScreen] (const std::string& value) -> void {
  620. const auto color = WallpaperEngine::Render::CFBO::parseColor (value);
  621. if (!color.has_value ()) {
  622. sLog.exception ("Invalid corner color: ", value);
  623. }
  624. if (this->settings.render.mode == DESKTOP_BACKGROUND) {
  625. this->settings.general.screenCornerColors[lastScreen] = *color;
  626. if (lastScreen.rfind ("span:", 0) == 0 && !this->settings.general.spanGroups.empty ()) {
  627. this->settings.general.spanGroups.back ().cornerColor = *color;
  628. }
  629. } else {
  630. this->settings.render.window.cornerColor = *color;
  631. }
  632. })
  633. .append ();
  634. // Wallpaper Engine's own image settings (its properties panel), every one of them applies to the previous
  635. // --window, --screen-root or --screen-span output like --zoom does
  636. const auto updateImageAdjustments = [this, &lastScreen] (const std::function<void (ImageAdjustments&)>& update) {
  637. if (this->settings.render.mode == DESKTOP_BACKGROUND) {
  638. update (this->settings.general.screenImageAdjustments[lastScreen]);
  639. if (lastScreen.rfind ("span:", 0) == 0 && !this->settings.general.spanGroups.empty ()) {
  640. update (this->settings.general.spanGroups.back ().imageAdjustments);
  641. }
  642. } else {
  643. update (this->settings.render.window.imageAdjustments);
  644. }
  645. };
  646. const auto parseSlider = [] (const std::string& name, const std::string& value) -> float {
  647. float result;
  648. try {
  649. result = std::stof (value);
  650. } catch (const std::exception&) {
  651. sLog.exception ("Invalid ", name, " value: ", value);
  652. }
  653. if (result < 0.0f || result > 100.0f) {
  654. sLog.exception (name, " must be between 0 and 100: ", value);
  655. }
  656. return result;
  657. };
  658. backgroundGroup.add_argument ("--image-filter")
  659. .help (
  660. "Applies one of Wallpaper Engine's image filters (a color lookup table from the assets' materials/lut "
  661. "folder, e.g. \"lutx32_amber\" or \"k23_b\"), \"none\" removes it. Scene and video wallpapers only. "
  662. "This applies to the previous --window, --screen-root, or --screen-span output, or the default "
  663. "background if no other background is specified"
  664. )
  665. .action ([updateImageAdjustments] (const std::string& value) -> void {
  666. updateImageAdjustments ([&value] (ImageAdjustments& adjustments) {
  667. adjustments.filter = value == "none" ? "" : value;
  668. });
  669. })
  670. .append ();
  671. backgroundGroup.add_argument ("--image-filter-strength")
  672. .help ("How strongly --image-filter applies, 0-100. Default: 100")
  673. .action ([updateImageAdjustments, parseSlider] (const std::string& value) -> void {
  674. const float strength = parseSlider ("image filter strength", value);
  675. updateImageAdjustments ([strength] (ImageAdjustments& adjustments) {
  676. adjustments.filterStrength = strength;
  677. });
  678. })
  679. .append ();
  680. for (const auto& [option, name] : std::initializer_list<std::pair<const char*, const char*>> {
  681. { "--brightness", "brightness" },
  682. { "--contrast", "contrast" },
  683. { "--saturation", "saturation" },
  684. { "--hue", "hue" },
  685. }) {
  686. backgroundGroup.add_argument (option)
  687. .help (
  688. std::string ("Wallpaper Engine's ") + name
  689. + " color option, 0-100 with 50 leaving the image unchanged. Scene and video wallpapers only"
  690. )
  691. .action ([updateImageAdjustments, parseSlider, name] (const std::string& value) -> void {
  692. const float amount = parseSlider (name, value);
  693. const std::string_view which = name;
  694. updateImageAdjustments ([amount, which] (ImageAdjustments& adjustments) {
  695. adjustments.colorEnabled = true;
  696. if (which == "brightness") {
  697. adjustments.brightness = amount;
  698. } else if (which == "contrast") {
  699. adjustments.contrast = amount;
  700. } else if (which == "saturation") {
  701. adjustments.saturation = amount;
  702. } else {
  703. adjustments.hue = amount;
  704. }
  705. });
  706. })
  707. .append ();
  708. }
  709. backgroundGroup.add_argument ("--color-options")
  710. .help (
  711. "Turns Wallpaper Engine's color options (--brightness and friends, or the ones a preset carries) on or "
  712. "off. Giving any of them turns them on"
  713. )
  714. .choices ("on", "off")
  715. .action ([updateImageAdjustments] (const std::string& value) -> void {
  716. updateImageAdjustments ([&value] (ImageAdjustments& adjustments) {
  717. adjustments.colorEnabled = value == "on";
  718. });
  719. })
  720. .append ();
  721. backgroundGroup.add_argument ("--flip")
  722. .help (
  723. "Mirrors the wallpaper horizontally (Wallpaper Engine's \"Flip\" option), \"on\" or \"off\". Scene and "
  724. "video wallpapers only"
  725. )
  726. .choices ("on", "off")
  727. .action ([updateImageAdjustments] (const std::string& value) -> void {
  728. updateImageAdjustments ([&value] (ImageAdjustments& adjustments) {
  729. adjustments.flipHorizontal = value == "on";
  730. });
  731. })
  732. .append ();
  733. backgroundGroup.add_argument ("--hdr")
  734. .help (
  735. "Wayland-only: sends HDR (PQ, BT.2020) to monitors running in HDR mode and plays HDR videos in HDR, "
  736. "like Wallpaper Engine's \"HDR display\" setting. Needs a compositor with the color management "
  737. "protocol (wp_color_manager_v1, recent KDE Plasma); other monitors keep getting the usual SDR image"
  738. )
  739. .flag ()
  740. .store_into (this->settings.render.hdr);
  741. backgroundGroup.add_argument ("--layer")
  742. .help (
  743. "Wayland-only: which wlr-layer-shell layer to anchor the wallpaper to "
  744. "(background, bottom, top, overlay). Default: bottom. On KDE 'background' shares a layer with "
  745. "plasmashell's desktop window, which covers the wallpaper whenever it maps after it (e.g. at login). "
  746. "Use 'background' on niri to pair with the `place-within-backdrop` layer-rule, "
  747. "otherwise the wallpaper will be cloned to every workspace in the overview."
  748. )
  749. .choices ("background", "bottom", "top", "overlay")
  750. .default_value (std::string ("bottom"))
  751. .action ([this] (const std::string& value) -> void {
  752. if (value == "background") {
  753. this->settings.render.wayland.layer = WAYLAND_LAYER_BACKGROUND;
  754. } else if (value == "bottom") {
  755. this->settings.render.wayland.layer = WAYLAND_LAYER_BOTTOM;
  756. } else if (value == "top") {
  757. this->settings.render.wayland.layer = WAYLAND_LAYER_TOP;
  758. } else if (value == "overlay") {
  759. this->settings.render.wayland.layer = WAYLAND_LAYER_OVERLAY;
  760. } else {
  761. sLog.exception ("Invalid wlr-layer-shell layer: ", value);
  762. }
  763. });
  764. auto& performanceGroup = program.add_group ("Performance options");
  765. performanceGroup.add_argument ("-f", "--fps")
  766. .help ("Limits the FPS to the given number, useful to keep battery consumption low")
  767. .default_value (60)
  768. .store_into (this->settings.render.maximumFPS);
  769. performanceGroup.add_argument ("--speed")
  770. .help (
  771. "Global playback speed multiplier for animations, particles and effects (e.g. 0.5 for half speed, "
  772. "2.0 for double speed). Useful to calm down backgrounds with overly fast particle effects"
  773. )
  774. .action ([this] (const std::string& value) -> void {
  775. float speed;
  776. try {
  777. speed = std::stof (value);
  778. } catch (const std::exception&) {
  779. sLog.exception ("Invalid speed value: ", value);
  780. }
  781. if (speed <= 0.0f) {
  782. sLog.exception ("--speed must be a positive number");
  783. }
  784. this->settings.render.playbackSpeed = speed;
  785. });
  786. performanceGroup.add_argument ("--hwdec")
  787. .help (
  788. "Hardware video decoding backend handed to mpv for video wallpapers and video textures: auto (default), "
  789. "no, vaapi, vaapi-copy, nvdec, nvdec-copy, cuda, vulkan or any other mpv --hwdec value. Try no or a -copy "
  790. "variant if videos crash, stay black or fall back to software decoding. Control file: hwdec=<backend>"
  791. )
  792. .action ([this] (const std::string& value) -> void {
  793. if (!WallpaperEngine::VideoPlayback::MPV::GLPlayer::isValidHwdec (value)) {
  794. sLog.exception ("Invalid --hwdec value: ", value);
  795. }
  796. this->settings.render.hwdec = value;
  797. });
  798. performanceGroup.add_argument ("--no-fullscreen-pause")
  799. .help ("Prevents the background pausing when an app is fullscreen")
  800. .flag ()
  801. .action ([this] (const std::string& value) -> void { this->settings.render.pauseOnFullscreen = false; });
  802. performanceGroup.add_argument ("--fullscreen-pause-only-active")
  803. .help ("Wayland only: pause only when a fullscreen window is active (activated)")
  804. .flag ()
  805. .action ([this] (const std::string& value) -> void {
  806. this->settings.render.pauseOnFullscreenOnlyWhenActive = true;
  807. });
  808. performanceGroup.add_argument ("--pause-on-focus")
  809. .help (
  810. "Also pause while another application has the focus (Wallpaper Engine's \"Other application focused\" "
  811. "set to pause; on GNOME only the outputs it is on pause)"
  812. )
  813. .flag ()
  814. .action ([this] (const std::string& value) -> void { this->settings.render.pauseOnFocus = true; });
  815. performanceGroup.add_argument ("--pause-on-maximized")
  816. .help (
  817. "wlroots compositors: also pause the outputs a maximized window is on (KDE and GNOME always do, like "
  818. "Wallpaper Engine)"
  819. )
  820. .flag ()
  821. .action ([this] (const std::string& value) -> void { this->settings.render.pauseOnMaximized = true; });
  822. performanceGroup.add_argument ("--fullscreen-pause-ignore-appid")
  823. .help ("Wayland only: ignore fullscreen windows whose app_id contains this value (repeatable)")
  824. .action ([this] (const std::string& value) -> void {
  825. if (!value.empty ()) {
  826. this->settings.render.fullscreenPauseIgnoreAppIds.push_back (value);
  827. }
  828. })
  829. .append ();
  830. auto& audioGroup = program.add_group ("Sound settings");
  831. auto& audioSettingsGroup = audioGroup.add_mutually_exclusive_group (false);
  832. audioSettingsGroup.add_argument ("-v", "--volume")
  833. .help ("Volume for all the sounds in the background")
  834. .default_value (15)
  835. .store_into (this->settings.audio.volume);
  836. audioSettingsGroup.add_argument ("-s", "--silent")
  837. .help ("Mutes all the sound the wallpaper might produce")
  838. .flag ()
  839. .action ([this] (const std::string& value) -> void { this->settings.audio.enabled = false; });
  840. audioGroup.add_argument ("--automute")
  841. .help ("Mutes the wallpaper's audio when another app is playing sound")
  842. .flag ()
  843. .action ([this] (const std::string& value) -> void { this->settings.audio.automute = true; });
  844. audioGroup.add_argument ("--noautomute")
  845. .help ("Disables the automute when an app is playing sound (default)")
  846. .flag ()
  847. .action ([this] (const std::string& value) -> void { this->settings.audio.automute = false; });
  848. audioGroup.add_argument ("--no-audio-processing")
  849. .help ("Disables audio processing for backgrounds")
  850. .flag ()
  851. .action ([this] (const std::string& value) -> void { this->settings.audio.audioprocessing = false; });
  852. audioGroup.add_argument ("--audio-screen")
  853. .help (
  854. "Only the background on this screen (matches --screen-root names) produces audio; every other screen "
  855. "is muted regardless of its own sound. Default: no restriction, every screen can produce sound."
  856. )
  857. .action ([this] (const std::string& value) -> void {
  858. if (!value.empty ()) {
  859. this->settings.audio.audioScreen = value;
  860. }
  861. });
  862. audioGroup.add_argument ("--ambient-volume")
  863. .help (
  864. "Separate volume (0-128) for non-video backgrounds (scene sound objects and web wallpapers), instead "
  865. "of --volume; video wallpapers always keep using --volume. Web wallpapers only support mute (0) vs "
  866. "unmuted (any other value), since CEF exposes no analog volume control. Default: same as --volume."
  867. )
  868. .action ([this] (const std::string& value) -> void {
  869. try {
  870. this->settings.audio.ambientVolume = std::stoi (value);
  871. } catch (const std::exception&) {
  872. sLog.exception ("Invalid --ambient-volume value: ", value);
  873. }
  874. });
  875. audioGroup.add_argument ("--hrtf")
  876. .help (
  877. "Headphones (HRTF) rendering of scene sound objects, like Wallpaper Engine's OpenAL: auto (default) "
  878. "uses it while the default output is headphones or a headset, on and off force it. Control file: "
  879. "hrtf=<mode>"
  880. )
  881. .action ([this] (const std::string& value) -> void {
  882. const auto mode = parseHrtfMode (value);
  883. if (!mode.has_value ()) {
  884. sLog.exception ("Invalid --hrtf value: ", value);
  885. }
  886. this->settings.audio.hrtf = *mode;
  887. });
  888. auto& screenshotGroup = program.add_group ("Screenshot options");
  889. screenshotGroup.add_argument ("--screenshot")
  890. .help ("Takes a screenshot of the background for it's use with tools like PyWAL")
  891. .default_value ("")
  892. .action ([this] (const std::string& value) -> void {
  893. this->settings.screenshot.take = true;
  894. this->settings.screenshot.path = value;
  895. });
  896. screenshotGroup.add_argument ("--screenshot-delay")
  897. .help ("Frames to wait before taking the screenshot")
  898. .default_value<uint32_t> (5)
  899. .store_into (this->settings.screenshot.delay);
  900. auto& contentGroup = program.add_group ("Content options");
  901. contentGroup.add_argument ("--assets-dir")
  902. .help ("Folder where the assets are stored")
  903. .default_value ("")
  904. .action ([this] (const std::string& value) -> void { this->settings.general.assets = value; });
  905. auto& configurationGroup = program.add_group ("Wallpaper configuration options");
  906. const auto parseVideoTime = [] (const char* name, const std::string& value) -> double {
  907. const auto seconds = WallpaperEngine::Render::Wallpapers::CVideo::parseTime (value);
  908. if (!seconds.has_value ()) {
  909. sLog.exception ("Invalid ", name, " value (seconds, m:ss or h:mm:ss): ", value);
  910. }
  911. return *seconds;
  912. };
  913. const auto singleSegment = [this] () -> WallpaperEngine::VideoPlayback::VideoSegment& {
  914. auto& segments = this->settings.render.videoSegments;
  915. if (segments.size () != 1) {
  916. segments.assign (1, {});
  917. }
  918. return segments.front ();
  919. };
  920. configurationGroup.add_argument ("--video-start")
  921. .help (
  922. "Video wallpapers only: where the part of the video that loops starts, in seconds, m:ss or h:mm:ss (e.g. "
  923. "\"3:00\"). Without --video-end it loops from here to the end of the video"
  924. )
  925. .action ([parseVideoTime, singleSegment] (const std::string& value) -> void {
  926. singleSegment ().start = parseVideoTime ("--video-start", value);
  927. });
  928. configurationGroup.add_argument ("--video-end")
  929. .help (
  930. "Video wallpapers only: where the part of the video that loops ends, in seconds, m:ss or h:mm:ss (e.g. "
  931. "\"4:00\"). Without --video-start it loops from the beginning up to here"
  932. )
  933. .action ([parseVideoTime, singleSegment] (const std::string& value) -> void {
  934. singleSegment ().end = parseVideoTime ("--video-end", value);
  935. });
  936. configurationGroup.add_argument ("--video-segments")
  937. .help (
  938. "Video wallpapers only: plays just these parts one after the other and skips the rest, as a comma "
  939. "separated list of start-end times in seconds, m:ss or h:mm:ss (e.g. \"2:00-3:00,4:00-5:00\"). An empty "
  940. "side means the start or end of the video (\"4:00-\")"
  941. )
  942. .action ([this] (const std::string& value) -> void {
  943. const auto segments = WallpaperEngine::Render::Wallpapers::CVideo::parseSegments (value);
  944. if (!segments.has_value ()) {
  945. sLog.exception ("Invalid --video-segments value (start-end,start-end,...): ", value);
  946. }
  947. this->settings.render.videoSegments = *segments;
  948. });
  949. configurationGroup.add_argument ("--control-file")
  950. .help (
  951. "File the SIGUSR1 hotswap request is read from. Give every engine its own when several run at once, "
  952. "otherwise they all read the same one. Default: $XDG_RUNTIME_DIR/lwe-control"
  953. )
  954. .action ([this] (const std::string& value) -> void {
  955. if (!value.empty ()) {
  956. this->settings.general.controlFile = value;
  957. }
  958. });
  959. configurationGroup.add_argument ("--disable-particles")
  960. .help ("Disables particles for the backgrounds")
  961. .flag ()
  962. .action ([this] (const std::string& value) -> void { this->settings.general.disableParticles = true; });
  963. configurationGroup.add_argument ("--expand-canvas")
  964. .help (
  965. "Grows a scene's render canvas so image layers that extend past the camera's projection (a tall or wide "
  966. "picture the author only meant to be revealed by parallax) are shown in full instead of cropped. Layout "
  967. "stays centered on the original canvas, so it only ever adds room around it. Combine with "
  968. "--disable-parallax and --scaling fit to see the whole picture"
  969. )
  970. .flag ()
  971. .action ([this] (const std::string& value) -> void { this->settings.general.expandCanvas = true; });
  972. configurationGroup.add_argument ("--repair-assets")
  973. .help (
  974. "Loads assets of broken wallpapers the way their author meant where Wallpaper Engine itself shows nothing "
  975. "useful, e.g. an image whose material names its texture \"\" (WE draws its missing texture checker, this "
  976. "loads materials/.tex). Off by default to match Wallpaper Engine"
  977. )
  978. .flag ()
  979. .action ([this] (const std::string& value) -> void { this->settings.general.repairAssets = true; });
  980. configurationGroup.add_argument ("--post-processing")
  981. .help (
  982. "Wallpaper Engine's post processing quality: \"disabled\" (no bloom), \"enabled\" (default), \"ultra\" or "
  983. "\"displayhdr\". With ultra, scenes that turn on both bloom and hdr render in HDR with Wallpaper Engine's "
  984. "HDR bloom; displayhdr "
  985. "also lets their highlights go up to an HDR output's peak brightness (with --hdr)"
  986. )
  987. .choices ("disabled", "enabled", "ultra", "displayhdr")
  988. .action ([this] (const std::string& value) -> void {
  989. this->settings.general.postProcessingDisabled = value == "disabled";
  990. this->settings.general.ultraPostProcessing = value == "ultra" || value == "displayhdr";
  991. this->settings.general.displayHDR = value == "displayhdr";
  992. });
  993. // same names and numbers as Wallpaper Engine's quality settings (sub_14010DAD0)
  994. const auto qualityLevel = [] (const std::string& value) {
  995. return value == "disabled" ? 0 : value == "low" ? 1 : value == "high" ? 3 : value == "ultra" ? 4 : 2;
  996. };
  997. configurationGroup.add_argument ("--volumetrics")
  998. .help ("Wallpaper Engine's volumetric lighting quality: disabled, low, medium (default), high or ultra")
  999. .choices ("disabled", "low", "medium", "high", "ultra")
  1000. .action ([this, qualityLevel] (const std::string& value) -> void {
  1001. this->settings.general.volumetricsQuality = qualityLevel (value);
  1002. });
  1003. configurationGroup.add_argument ("--shadows")
  1004. .help ("Wallpaper Engine's shadow quality: disabled, low, medium (default), high or ultra")
  1005. .choices ("disabled", "low", "medium", "high", "ultra")
  1006. .action ([this, qualityLevel] (const std::string& value) -> void {
  1007. this->settings.general.shadowQuality = qualityLevel (value);
  1008. });
  1009. configurationGroup.add_argument ("--msaa")
  1010. .help (
  1011. "Wallpaper Engine's anti-aliasing setting: none (default), x2, x4 or x8. Like Wallpaper Engine it only "
  1012. "applies "
  1013. "to scenes with a 3D model object, which then draw their objects multisampled"
  1014. )
  1015. .choices ("none", "x2", "x4", "x8")
  1016. .action ([this] (const std::string& value) -> void {
  1017. this->settings.general.msaaSamples = value == "x2" ? 2 : value == "x4" ? 4 : value == "x8" ? 8 : 0;
  1018. });
  1019. configurationGroup.add_argument ("--ssaa")
  1020. .help (
  1021. "Supersampling for scenes: none (default), x2, x3 or x4. The scene renders at that many times the output "
  1022. "size per axis and is averaged down, so it costs that factor squared in GPU time and memory. Effects "
  1023. "measured in pixels (blur, bloom) cover the same pixels as on a screen that much bigger"
  1024. )
  1025. .choices ("none", "x2", "x3", "x4")
  1026. .action ([this] (const std::string& value) -> void {
  1027. this->settings.general.supersampling = value == "x2" ? 2 : value == "x3" ? 3 : value == "x4" ? 4 : 1;
  1028. });
  1029. configurationGroup.add_argument ("--disable-animations")
  1030. .help (
  1031. "Freezes all scene animation (scripts, particles, effects and puppet meshes) at its current frame. "
  1032. "Control file: disable-animations=on/off"
  1033. )
  1034. .flag ()
  1035. .action ([this] (const std::string& value) -> void { this->settings.render.freezeAnimations = true; });
  1036. configurationGroup.add_argument ("--disable-mouse")
  1037. .help ("Disables mouse interaction with the backgrounds")
  1038. .flag ()
  1039. .action ([this] (const std::string& value) -> void { this->settings.mouse.enabled = false; });
  1040. configurationGroup.add_argument ("--input-file")
  1041. .help (
  1042. "File a desktop shell writes the left mouse button to, as {\"leftDown\": true} refreshed while the button "
  1043. "is held (Wayland). For shells that cover the background surface, which then gets no pointer buttons. "
  1044. "A file older than 2 seconds counts as released, so a shell that died cannot leave the button pressed"
  1045. )
  1046. .action ([this] (const std::string& value) -> void { this->settings.mouse.inputFile = value; });
  1047. configurationGroup.add_argument ("--disable-parallax")
  1048. .help ("Disables parallax effect for the backgrounds")
  1049. .flag ()
  1050. .action ([this] (const std::string& value) -> void { this->settings.mouse.disableparallax = true; });
  1051. configurationGroup.add_argument ("--allow-parallax-overflow")
  1052. .help (
  1053. "Lets parallax push an image past its own edges, exposing black corners on layers too small for the "
  1054. "movement. Disabled by default, which clamps parallax displacement to each image's own size; images "
  1055. "whose texture tiles/repeats are always exempt from the clamp, since going past their edges is seamless"
  1056. )
  1057. .flag ()
  1058. .action ([this] (const std::string& value) -> void { this->settings.mouse.clampParallaxToImageSize = false; });
  1059. configurationGroup.add_argument ("-l", "--list-properties")
  1060. .help ("List all the available properties and their configuration")
  1061. .flag ()
  1062. .store_into (this->settings.general.onlyListProperties);
  1063. configurationGroup.add_argument ("--set-property", "--property")
  1064. .help ("Overrides the default value of the given property")
  1065. .action ([this] (const std::string& value) -> void {
  1066. const std::string::size_type equals = value.find ('=');
  1067. // properties without a value are treated as booleans for now
  1068. if (equals == std::string::npos) {
  1069. this->settings.general.properties[value] = "1";
  1070. } else {
  1071. this->settings.general.properties[value.substr (0, equals)] = value.substr (equals + 1);
  1072. }
  1073. })
  1074. .append ();
  1075. configurationGroup.add_argument ("--list-objects")
  1076. .help ("List all the objects/layers a background has, with their id, name and type")
  1077. .flag ()
  1078. .store_into (this->settings.general.onlyListObjects);
  1079. configurationGroup.add_argument ("--disable-object")
  1080. .help ("Hides an object/layer (parallax layer, clock, particles, etc), matched by id or name. Can be repeated")
  1081. .action ([this] (const std::string& value) -> void {
  1082. this->settings.general.disabledObjects.push_back (value);
  1083. })
  1084. .append ();
  1085. configurationGroup.add_argument ("--enable-object")
  1086. .help (
  1087. "Forces an object/layer to show even if the background hides it by default, matched by id or name. "
  1088. "Can be repeated"
  1089. )
  1090. .action ([this] (const std::string& value) -> void { this->settings.general.enabledObjects.push_back (value); })
  1091. .append ();
  1092. configurationGroup.add_argument ("--list-effects")
  1093. .help (
  1094. "List all effects (bloom, blur, glow, etc) attached to a background's objects, with their id, editor "
  1095. "name and category"
  1096. )
  1097. .flag ()
  1098. .store_into (this->settings.general.onlyListEffects);
  1099. configurationGroup.add_argument ("--disable-effect")
  1100. .help (
  1101. "Disables an object's visual effect (bloom, blur, glow, etc), matched by effect id or editor name. "
  1102. "Can be repeated. Control file: effects=1 plus disable-effect=<id> lines"
  1103. )
  1104. .action ([this] (const std::string& value) -> void {
  1105. this->settings.general.disabledEffects.push_back (value);
  1106. })
  1107. .append ();
  1108. configurationGroup.add_argument ("--enable-effect")
  1109. .help (
  1110. "Forces an effect to show even if the scene hides it by default, matched by effect id or editor name. "
  1111. "Can be repeated. Control file: effects=1 plus enable-effect=<id> lines"
  1112. )
  1113. .action ([this] (const std::string& value) -> void { this->settings.general.enabledEffects.push_back (value); })
  1114. .append ();
  1115. configurationGroup.add_argument ("--list-audio-objects")
  1116. .help (
  1117. "List objects/properties whose script reacts to music (via engine.registerAudioBuffers), with their "
  1118. "current minvalue/maxvalue/frequency/smoothing"
  1119. )
  1120. .flag ()
  1121. .store_into (this->settings.general.onlyListAudioObjects);
  1122. configurationGroup.add_argument ("--audio-sensitivity")
  1123. .help (
  1124. "Scales how much an audio-reactive object's music-driven properties swing around their authored "
  1125. "midpoint, matched by id or name: 0 locks it (no pulse), 1 is the wallpaper's original behavior, "
  1126. ">1 exaggerates it. Use \"*\" as the id to set a default for every audio-reactive object with no "
  1127. "more specific match. Format: <id-or-name-or-*>=<multiplier>. Can be repeated"
  1128. )
  1129. .action ([this] (const std::string& value) -> void {
  1130. const std::string::size_type equals = value.find ('=');
  1131. if (equals == std::string::npos) {
  1132. sLog.exception ("--audio-sensitivity expects <id-or-name>=<multiplier>, got '" + value + "'");
  1133. }
  1134. const std::string target = value.substr (0, equals);
  1135. const std::string multiplierStr = value.substr (equals + 1);
  1136. try {
  1137. this->settings.general.audioSensitivity[target] = std::stof (multiplierStr);
  1138. } catch (const std::exception&) {
  1139. sLog.exception ("--audio-sensitivity: '" + multiplierStr + "' is not a valid number");
  1140. }
  1141. })
  1142. .append ();
  1143. configurationGroup.add_argument ("--sound-volume")
  1144. .help (
  1145. "Sets a Sound object's own volume (0-1), independent of the global volume - lets a wallpaper with "
  1146. "several alternate music tracks play only one, matched by id or name. Use \"*\" as the id to set a "
  1147. "default for every Sound object with no more specific match. Format: <id-or-name-or-*>=<volume>. Can "
  1148. "be repeated"
  1149. )
  1150. .action ([this] (const std::string& value) -> void {
  1151. const std::string::size_type equals = value.find ('=');
  1152. if (equals == std::string::npos) {
  1153. sLog.exception ("--sound-volume expects <id-or-name>=<volume>, got '" + value + "'");
  1154. }
  1155. const std::string target = value.substr (0, equals);
  1156. const std::string volumeStr = value.substr (equals + 1);
  1157. try {
  1158. this->settings.general.soundVolume[target] = std::stof (volumeStr);
  1159. } catch (const std::exception&) {
  1160. sLog.exception ("--sound-volume: '" + volumeStr + "' is not a valid number");
  1161. }
  1162. })
  1163. .append ();
  1164. auto& debuggingGroup = program.add_group ("Debugging options");
  1165. debuggingGroup.add_argument ("-z", "--dump-structure")
  1166. .help ("Dumps the structure of the backgrounds")
  1167. .flag ()
  1168. .store_into (this->settings.general.dumpStructure);
  1169. debuggingGroup.add_argument ("--no-shader-cache")
  1170. .help (
  1171. "Translates and links every shader from scratch instead of reusing the ones cached in "
  1172. "~/.cache/linux-wallpaperengine/shaders (nothing is written there either)"
  1173. )
  1174. .flag ()
  1175. .store_into (this->settings.general.noShaderCache);
  1176. debuggingGroup.add_argument ("--render-debug")
  1177. .help (
  1178. "Scene render debug mode: base-only, no-solid-final, pass-log, brightness-log, no-puppet-animation, "
  1179. "object=<id>, "
  1180. "skip-object=<id>, or skip-effect=<id>. Can be repeated."
  1181. )
  1182. .action ([this] (const std::string& value) -> void {
  1183. const auto parseDebugId = [&value] (const std::string& prefix) -> std::optional<int> {
  1184. try {
  1185. return std::stoi (value.substr (prefix.length ()));
  1186. } catch (const std::invalid_argument&) {
  1187. sLog.exception ("Invalid numeric value for --render-debug ", value);
  1188. } catch (const std::out_of_range&) {
  1189. sLog.exception ("Out-of-range numeric value for --render-debug ", value);
  1190. }
  1191. return std::nullopt;
  1192. };
  1193. if (value == "base-only") {
  1194. this->settings.render.debug.baseOnly = true;
  1195. } else if (value == "no-solid-final") {
  1196. this->settings.render.debug.noSolidFinal = true;
  1197. } else if (value == "pass-log") {
  1198. this->settings.render.debug.passLog = true;
  1199. } else if (value == "brightness-log") {
  1200. this->settings.render.debug.brightnessLog = true;
  1201. } else if (value == "no-puppet-animation") {
  1202. this->settings.render.debug.noPuppetAnimation = true;
  1203. } else if (value.rfind ("object=", 0) == 0) {
  1204. this->settings.render.debug.objectFilter = parseDebugId ("object=");
  1205. } else if (value.rfind ("skip-object=", 0) == 0) {
  1206. if (const auto id = parseDebugId ("skip-object="); id.has_value ()) {
  1207. this->settings.render.debug.skipObjects.emplace_back (*id);
  1208. }
  1209. } else if (value.rfind ("skip-effect=", 0) == 0) {
  1210. if (const auto id = parseDebugId ("skip-effect="); id.has_value ()) {
  1211. this->settings.render.debug.skipEffects.emplace_back (*id);
  1212. }
  1213. } else {
  1214. sLog.exception ("Invalid render debug mode: ", value);
  1215. }
  1216. })
  1217. .append ();
  1218. program.add_epilog (
  1219. "Usage examples:\n"
  1220. " linux-wallpaperengine --screen-root HDMI-1 --bg 2317494988 --scaling fill --clamp border\n"
  1221. " Runs the background 2317494988 on screen HDMI-1, scaling it to fill the screen and clamping the UVs to "
  1222. "the border\n\n"
  1223. " linux-wallpaperengine 2317494988\n"
  1224. " Previews the background 2317494988 on a window\n\n"
  1225. " linux-wallpaperengine --screen-root HDMI-1 --bg 2317494988 --screen-root HDMI-2 --bg 1108150151\n"
  1226. " Runs two backgrounds on two screens, one on HDMI-1 and the other on HDMI-2\n\n"
  1227. " linux-wallpaperengine --screen-root HDMI-1 --screen-root HDMI-2 2317494988\n"
  1228. " Runs the background 2317494988 on two screens, one on HDMI-1 and the other on HDMI-2\n\n"
  1229. " linux-wallpaperengine --screen-span HDMI-1,HDMI-2 --bg 2317494988 --scaling fill\n"
  1230. " Spans the background 2317494988 across HDMI-1 and HDMI-2 as a single stretched wallpaper\n\n"
  1231. );
  1232. try {
  1233. program.parse_known_args (this->m_argc, this->m_argv);
  1234. if (this->settings.general.defaultBackground.empty ()) {
  1235. throw std::runtime_error ("At least one background ID must be specified");
  1236. }
  1237. this->settings.audio.volume = std::max (0, std::min (this->settings.audio.volume, 128));
  1238. if (this->settings.audio.ambientVolume.has_value ()) {
  1239. this->settings.audio.ambientVolume = std::max (0, std::min (*this->settings.audio.ambientVolume, 128));
  1240. }
  1241. this->settings.screenshot.delay
  1242. = std::max<uint32_t> (0, std::min<uint32_t> (this->settings.screenshot.delay, 5000));
  1243. // std::cout directly, in case logging is disabled, so this is still visible
  1244. std::stringbuf buffer;
  1245. std::ostream bufferStream (&buffer);
  1246. bufferStream << "Running with: ";
  1247. for (int i = 0; i < this->m_argc; i++) {
  1248. bufferStream << this->m_argv[i];
  1249. bufferStream << " ";
  1250. }
  1251. if (!isHelperProcess (this->m_argc, this->m_argv, this->settings.general.webHost)) {
  1252. std::cout << buffer.str () << std::endl;
  1253. }
  1254. this->validateAssets ();
  1255. this->validateScreenshot ();
  1256. this->state.general.keepRunning = true;
  1257. this->state.audio.enabled = this->settings.audio.enabled;
  1258. this->state.audio.volume = this->settings.audio.volume;
  1259. this->state.mouse.enabled = this->settings.mouse.enabled;
  1260. #if DEMOMODE
  1261. sLog.error ("WARNING: RUNNING IN DEMO MODE WILL STOP WALLPAPERS AFTER 5 SECONDS SO VIDEO CAN BE RECORDED");
  1262. this->settings.render.maximumFPS = 30;
  1263. this->settings.screenshot.take = false;
  1264. this->settings.render.pauseOnFullscreen = false;
  1265. #endif /* DEMOMODE */
  1266. } catch (const std::runtime_error& e) {
  1267. throw std::runtime_error (
  1268. std::string (e.what ()) + ". Use " + std::string (this->m_argv[0]) + " --help for more information"
  1269. );
  1270. }
  1271. }
  1272. int ApplicationContext::getArgc () const { return this->m_argc; }
  1273. char** ApplicationContext::getArgv () const { return this->m_argv; }
  1274. std::filesystem::path ApplicationContext::translateBackground (const std::string& bgIdOrPath) {
  1275. if (bgIdOrPath.find ('/') == std::string::npos) {
  1276. return Steam::FileSystem::workshopDirectory (WORKSHOP_APP_ID, bgIdOrPath);
  1277. }
  1278. return bgIdOrPath;
  1279. }
  1280. void ApplicationContext::validateAssets () {
  1281. if (!this->settings.general.assets.empty ()) {
  1282. if (!isHelperProcess (this->m_argc, this->m_argv, this->settings.general.webHost)) {
  1283. sLog.out (
  1284. "Using wallpaper engine's assets at ", this->settings.general.assets, " based on --assets-dir parameter"
  1285. );
  1286. }
  1287. return;
  1288. }
  1289. try {
  1290. this->settings.general.assets = Steam::FileSystem::appDirectory (APP_DIRECTORY, "assets");
  1291. } catch (std::runtime_error&) {
  1292. this->settings.general.assets = std::filesystem::canonical ("/proc/self/exe").parent_path () / "assets";
  1293. }
  1294. }
  1295. void ApplicationContext::validateScreenshot () const {
  1296. if (!this->settings.screenshot.take) {
  1297. return;
  1298. }
  1299. if (!this->settings.screenshot.path.has_extension ()) {
  1300. sLog.exception ("Cannot determine screenshot format");
  1301. }
  1302. const std::string extension = this->settings.screenshot.path.extension ();
  1303. if (extension != ".bmp" && extension != ".png" && extension != ".jpeg" && extension != ".jpg") {
  1304. sLog.exception ("Cannot determine screenshot format, unknown extension ", extension);
  1305. }
  1306. }