main.cpp
1 #include "pch.h" 2 3 #include <chrono> 4 5 #include <workspaces-common/GuidUtils.h> 6 #include <workspaces-common/MonitorUtils.h> 7 8 #include <WorkspacesLib/JsonUtils.h> 9 #include <WorkspacesLib/WorkspacesData.h> 10 11 #include <SnapshotUtils.h> 12 13 #include <common/utils/gpo.h> 14 #include <common/utils/logger_helper.h> 15 #include <common/utils/UnhandledExceptionHandler.h> 16 #include <WorkspacesLib/utils.h> 17 18 const std::wstring moduleName = L"Workspaces\\WorkspacesSnapshotTool"; 19 const std::wstring internalPath = L""; 20 21 int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdLine, int cmdShow) 22 { 23 LoggerHelpers::init_logger(moduleName, internalPath, LogSettings::workspacesSnapshotToolLoggerName); 24 InitUnhandledExceptionHandler(); 25 26 if (powertoys_gpo::getConfiguredWorkspacesEnabledValue() == powertoys_gpo::gpo_rule_configured_disabled) 27 { 28 Logger::warn(L"Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator."); 29 return 0; 30 } 31 32 SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); 33 34 HRESULT comInitHres = CoInitializeEx(0, COINIT_MULTITHREADED); 35 if (FAILED(comInitHres)) 36 { 37 Logger::error(L"Failed to initialize COM library. {}", comInitHres); 38 return -1; 39 } 40 41 // Set general COM security levels. 42 comInitHres = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); 43 if (FAILED(comInitHres)) 44 { 45 Logger::error(L"Failed to initialize security. Error code: {}", get_last_error_or_default(comInitHres)); 46 CoUninitialize(); 47 return -1; 48 } 49 50 std::wstring cmdLineStr{ GetCommandLineW() }; 51 auto cmdArgs = split(cmdLineStr, L" "); 52 53 // create new project 54 time_t creationTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); 55 WorkspacesData::WorkspacesProject project{ .id = CreateGuidString(), .creationTime = creationTime }; 56 Logger::trace(L"Creating workspace {}:{}", project.name, project.id); 57 58 project.monitors = MonitorUtils::IdentifyMonitors(); 59 bool isGuidNeeded = cmdArgs.invokePoint == InvokePoint::LaunchAndEdit; 60 project.apps = SnapshotUtils::GetApps(isGuidNeeded, [&](HWND window) -> unsigned int { 61 auto windowMonitor = MonitorFromWindow(window, MONITOR_DEFAULTTOPRIMARY); 62 unsigned int monitorNumber = 0; 63 for (const auto& monitor : project.monitors) 64 { 65 if (monitor.monitor == windowMonitor) 66 { 67 monitorNumber = monitor.number; 68 break; 69 } 70 } 71 72 return monitorNumber; 73 }, [&](unsigned int monitorId) -> WorkspacesData::WorkspacesProject::Monitor::MonitorRect { 74 for (const auto& monitor : project.monitors) 75 { 76 if (monitor.number == monitorId) 77 { 78 return monitor.monitorRectDpiUnaware; 79 } 80 } 81 return project.monitors[0].monitorRectDpiUnaware; }); 82 83 JsonUtils::Write(WorkspacesData::TempWorkspacesFile(), project); 84 Logger::trace(L"WorkspacesProject {}:{} created", project.name, project.id); 85 86 CoUninitialize(); 87 return 0; 88 }