main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <csignal>
  2. #include <iostream>
  3. #include "WallpaperEngine/Application/CApplicationContext.h"
  4. #include "WallpaperEngine/Application/CWallpaperApplication.h"
  5. #include "WallpaperEngine/Core/Wallpapers/CWeb.h"
  6. #include "common.h"
  7. WallpaperEngine::Application::CWallpaperApplication* appPointer;
  8. void signalhandler(int sig)
  9. {
  10. if (appPointer == nullptr)
  11. return;
  12. appPointer->signal (sig);
  13. }
  14. void initLogging ()
  15. {
  16. sLog.addOutput (new std::ostream (std::cout.rdbuf ()));
  17. sLog.addError (new std::ostream (std::cerr.rdbuf ()));
  18. }
  19. static void CEFsetUp(int argc, char** argv)
  20. {
  21. // This function should be called from the application entry point function to
  22. // execute a secondary process. It can be used to run secondary processes from
  23. // the browser client executable (default behavior) or from a separate
  24. // executable specified by the CefSettings.browser_subprocess_path value. If
  25. // called for the browser process (identified by no "type" command-line value)
  26. // it will return immediately with a value of -1. If called for a recognized
  27. // secondary process it will block until the process should exit and then return
  28. // the process exit code. The |application| parameter may be empty. The
  29. // |windows_sandbox_info| parameter is only used on Windows and may be NULL (see
  30. // cef_sandbox_win.h for details).
  31. CefMainArgs args(argc, argv);
  32. int exit_code = CefExecuteProcess(args, nullptr, nullptr);//Spawned processes will terminate here(see CefIninitilize below). Maybe implementing settings.browser_subprocess_path will allow it to work not in main function.
  33. if (exit_code >= 0)
  34. {
  35. // Sub proccess has endend, so exit
  36. exit(exit_code);
  37. }
  38. else if (exit_code == -1)
  39. {
  40. // If called for the browser process (identified by no "type" command-line value)
  41. // it will return immediately with a value of -1
  42. }
  43. // Configurate Chromium
  44. CefSettings settings;
  45. //CefString(&settings.locales_dir_path) = "OffScreenCEF/godot/locales";
  46. //CefString(&settings.resources_dir_path) = "OffScreenCEF/godot/";
  47. //CefString(&settings.framework_dir_path) = "OffScreenCEF/godot/";
  48. //CefString(&settings.cache_path) = "OffScreenCEF/godot/";
  49. // CefString(&settings.browser_subprocess_path) = "path/to/client"
  50. settings.windowless_rendering_enabled = true;
  51. #if defined(CEF_NO_SANDBOX)
  52. settings.no_sandbox = true;
  53. #endif
  54. bool result = CefInitialize(args, settings, nullptr, nullptr); //Spawn 2 new processes; Can be moved to Core::CWeb
  55. if (!result)
  56. {
  57. std::cerr << "CefInitialize: failed" << std::endl;
  58. exit(-2);
  59. }
  60. }
  61. bool g_CEFused=false;//Will be set to true if wallpaper has "web" type
  62. int main (int argc, char* argv[]) {
  63. //START of CEF init block(it will run 3 times)
  64. char** argv2 = new char*[argc]; //Cef modify argv on CefInit, copy it before that
  65. for(int i=0; i<argc; ++i)
  66. {
  67. argv2[i] = new char[strlen(argv[i])+1];
  68. strcpy(argv2[i],argv[i]);
  69. }
  70. CEFsetUp(argc,argv);//Cef will launch new process with main(argc,argv) twice. If we won't pass argc and argv from main, we will create fork bomb and system will freeze until reboot.
  71. //END of CEF init block
  72. initLogging ();
  73. WallpaperEngine::Application::CApplicationContext appContext (argc, argv2);
  74. WallpaperEngine::Application::CWallpaperApplication app (appContext);
  75. // halt if the list-properties option was specified
  76. if (appContext.settings.general.onlyListProperties)
  77. return 0;
  78. appPointer = &app;
  79. // attach signals to gracefully stop
  80. std::signal (SIGINT, signalhandler);
  81. std::signal (SIGTERM, signalhandler);
  82. if(!g_CEFused){
  83. sLog.debug("No web wallpapers, shutting down CEF");
  84. CefShutdown();
  85. }
  86. // show the wallpaper application
  87. app.show ();
  88. if(g_CEFused){
  89. sLog.debug("Shutting down CEF");
  90. CefShutdown();
  91. }
  92. appPointer = nullptr;
  93. return 0;
  94. }