main.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <csignal>
  2. #include <iostream>
  3. #include "WallpaperEngine/Application/ApplicationContext.h"
  4. #include "WallpaperEngine/Application/WallpaperApplication.h"
  5. #include "WallpaperEngine/Logging/Log.h"
  6. WallpaperEngine::Application::WallpaperApplication* app;
  7. void signalhandler (const int sig) {
  8. if (app == nullptr)
  9. return;
  10. app->signal (sig);
  11. }
  12. void initLogging () {
  13. sLog.addOutput (new std::ostream (std::cout.rdbuf ()));
  14. sLog.addError (new std::ostream (std::cerr.rdbuf ()));
  15. }
  16. int main (int argc, char* argv[]) {
  17. try {
  18. // if type parameter is specified, this is a subprocess, so no logging should be enabled from our side
  19. bool enableLogging = true;
  20. const std::string typeZygote = "--type=zygote";
  21. const std::string typeUtility = "--type=utility";
  22. for (int i = 1; i < argc; i ++) {
  23. if (strncmp (typeZygote.c_str(), argv[i], typeZygote.size()) == 0) {
  24. enableLogging = false;
  25. break;
  26. }
  27. if (strncmp (typeUtility.c_str(), argv[i], typeUtility.size()) == 0) {
  28. enableLogging = false;
  29. break;
  30. }
  31. }
  32. if (enableLogging) {
  33. initLogging ();
  34. }
  35. WallpaperEngine::Application::ApplicationContext appContext (argc, argv);
  36. app = new WallpaperEngine::Application::WallpaperApplication (appContext);
  37. // halt if the list-properties option was specified
  38. if (appContext.settings.general.onlyListProperties) {
  39. delete app;
  40. return 0;
  41. }
  42. // attach signals to gracefully stop
  43. std::signal (SIGINT, signalhandler);
  44. std::signal (SIGTERM, signalhandler);
  45. std::signal (SIGKILL, signalhandler);
  46. // show the wallpaper application
  47. app->show ();
  48. // remove signal handlers before destroying app
  49. std::signal (SIGINT, SIG_DFL);
  50. std::signal (SIGTERM, SIG_DFL);
  51. std::signal (SIGKILL, SIG_DFL);
  52. delete app;
  53. return 0;
  54. } catch (const std::exception& e) {
  55. std::cerr << e.what () << std::endl;
  56. return 1;
  57. }
  58. }