main.cpp
  1  #include "pch.h"
  2  
  3  #include <WorkspacesLib/JsonUtils.h>
  4  #include <WorkspacesLib/IPCHelper.h>
  5  #include <WorkspacesLib/utils.h>
  6  
  7  #include <common/utils/gpo.h>
  8  #include <common/utils/logger_helper.h>
  9  #include <common/utils/UnhandledExceptionHandler.h>
 10  #include <common/utils/window.h>
 11  
 12  #include <WindowArranger.h>
 13  
 14  const std::wstring moduleName = L"Workspaces\\WorkspacesWindowArranger";
 15  const std::wstring internalPath = L"";
 16  
 17  int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cmdShow)
 18  {
 19      LoggerHelpers::init_logger(moduleName, internalPath, LogSettings::workspacesWindowArrangerLoggerName);
 20      InitUnhandledExceptionHandler();  
 21  
 22      if (powertoys_gpo::getConfiguredWorkspacesEnabledValue() == powertoys_gpo::gpo_rule_configured_disabled)
 23      {
 24          Logger::warn(L"Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator.");
 25          return 0;
 26      }
 27  
 28      SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
 29      
 30      std::wstring commandLine{ GetCommandLineW() };
 31      if (commandLine.empty())
 32      {
 33          Logger::warn("Empty command line arguments");
 34          return 1;
 35      }
 36  
 37      auto args = split(commandLine, L" ");
 38      if (args.workspaceId.empty())
 39      {
 40          Logger::warn("Incorrect command line arguments: no workspace id");
 41          return 1;
 42      }
 43  
 44      // read workspaces
 45      std::vector<WorkspacesData::WorkspacesProject> workspaces;
 46      WorkspacesData::WorkspacesProject projectToLaunch{};
 47  
 48      // check the temp file in case the project is just created and not saved to the workspaces.json yet
 49      if (std::filesystem::exists(WorkspacesData::TempWorkspacesFile()))
 50      {
 51          auto file = WorkspacesData::TempWorkspacesFile();
 52          auto res = JsonUtils::ReadSingleWorkspace(file);
 53          if (res.isOk() && res.value().id == args.workspaceId)
 54          {
 55              projectToLaunch = res.getValue();
 56          }
 57          else if (res.isError())
 58          {
 59              Logger::error(L"Error reading temp file");
 60              return 1;
 61          }
 62      }
 63      
 64      if (projectToLaunch.id.empty())
 65      {
 66          auto file = WorkspacesData::WorkspacesFile();
 67          auto res = JsonUtils::ReadWorkspaces(file);
 68          if (res.isOk())
 69          {
 70              workspaces = res.getValue();
 71          }
 72          else
 73          {
 74              return 1;
 75          }
 76  
 77          for (const auto& proj : workspaces)
 78          {
 79              if (proj.id == args.workspaceId)
 80              {
 81                  projectToLaunch = proj;
 82                  break;
 83              }
 84          }
 85      }
 86  
 87      if (projectToLaunch.id.empty())
 88      {
 89          Logger::critical(L"Workspace {} not found", args.workspaceId);
 90          return 1;
 91      }
 92      
 93      // arrange windows
 94      Logger::info(L"Arrange windows from Workspace {} : {}", projectToLaunch.name, projectToLaunch.id);
 95      WindowArranger windowArranger(projectToLaunch);
 96      //run_message_loop();
 97      
 98      Logger::debug(L"Arranger finished");
 99      
100      CoUninitialize();
101      return 0;
102  }