main.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <csignal>
  2. #include <iostream>
  3. #include "WallpaperEngine/Application/CApplicationContext.h"
  4. #include "WallpaperEngine/Application/CWallpaperApplication.h"
  5. #include "WallpaperEngine/Logging/CLog.h"
  6. WallpaperEngine::Application::CWallpaperApplication* app;
  7. void signalhandler(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. std::string typeZygote = "--type=zygote";
  21. 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. } else if (strncmp (typeUtility.c_str(), argv[i], typeUtility.size()) == 0) {
  27. enableLogging = false;
  28. break;
  29. }
  30. }
  31. if (enableLogging) {
  32. initLogging ();
  33. }
  34. WallpaperEngine::Application::CApplicationContext appContext (argc, argv);
  35. // halt if the list-properties option was specified
  36. if (appContext.settings.general.onlyListProperties)
  37. return 0;
  38. app = new WallpaperEngine::Application::CWallpaperApplication (appContext);
  39. // attach signals to gracefully stop
  40. std::signal (SIGINT, signalhandler);
  41. std::signal (SIGTERM, signalhandler);
  42. std::signal (SIGKILL, signalhandler);
  43. // show the wallpaper application
  44. app->show ();
  45. // remove signal handlers before destroying app
  46. std::signal (SIGINT, SIG_DFL);
  47. std::signal (SIGTERM, SIG_DFL);
  48. std::signal (SIGKILL, SIG_DFL);
  49. delete app;
  50. return 0;
  51. } catch (const std::exception& e) {
  52. std::cerr << e.what () << std::endl;
  53. return 1;
  54. }
  55. }