Skip to content

Commit

Permalink
Add "delayed" shutdown timing
Browse files Browse the repository at this point in the history
  • Loading branch information
JPersson77 committed Jul 1, 2024
1 parent 2a890b1 commit 7deeaa0
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Common/common_app_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// common application definitions
#define APPNAME L"LGTV Companion"
#define APP_VERSION L"4.0.3"
#define APP_VERSION L"4.0.5"
#define CONFIG_FILE L"config.json"
#define LOG_FILE L"log.txt"
#define WINDOW_CLASS_UNIQUE L"YOLOx0x0x0181818"
Expand Down
8 changes: 6 additions & 2 deletions Common/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ using json = nlohmann::json;
#define JSON_EXTERNAL_API "ExternalAPI"
#define JSON_MUTE_SPEAKERS "MuteSpeakers"
#define JSON_TIMING_PRESHUTDOWN "TimingPreshutdown"
#define JSON_TIMING_SHUTDOWN "TimingShutdown"
#define JSON_DEVICE_NAME "Name"
#define JSON_DEVICE_IP "IP"
#define JSON_DEVICE_UNIQUEKEY "UniqueDeviceKey"
Expand Down Expand Up @@ -224,7 +225,10 @@ Preferences::Preferences(std::wstring configuration_file_name)
// Shutdown timing
j = jsonPrefs[JSON_PREFS_NODE][JSON_TIMING_PRESHUTDOWN];
if (!j.empty() && j.is_boolean())
preshutdown_timing_ = j.get<bool>();
shutdown_timing_ = j.get<bool>() ? 1 : 0;
j = jsonPrefs[JSON_PREFS_NODE][JSON_TIMING_SHUTDOWN];
if (!j.empty() && j.is_number())
shutdown_timing_ = j.get<int>();
// User idle mode whitelist
j = jsonPrefs[JSON_PREFS_NODE][JSON_WHITELIST];
if (!j.empty() && j.size() > 0)
Expand Down Expand Up @@ -412,7 +416,7 @@ bool Preferences::Preferences::writeToDisk(void)
prefs[JSON_PREFS_NODE][JSON_REMOTESTREAM_MODE] = (bool)remote_streaming_host_prefer_power_off_;
prefs[JSON_PREFS_NODE][JSON_EXTERNAL_API] = (bool)external_api_support_;
prefs[JSON_PREFS_NODE][JSON_MUTE_SPEAKERS] = (bool)user_idle_mode_mute_speakers_;
prefs[JSON_PREFS_NODE][JSON_TIMING_PRESHUTDOWN] = (bool)preshutdown_timing_;
prefs[JSON_PREFS_NODE][JSON_TIMING_SHUTDOWN] = (int)shutdown_timing_;

if (event_log_restart_strings_custom_.size() > 0)
for (auto& item : event_log_restart_strings_custom_)
Expand Down
6 changes: 5 additions & 1 deletion Common/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include <nlohmann/json.hpp>
#include "../Common/device.h"

#define PREFS_SHUTDOWN_TIMING_DEFAULT 0
#define PREFS_SHUTDOWN_TIMING_EARLY 1
#define PREFS_SHUTDOWN_TIMING_DELAYED 2

// Preferences management
class Preferences : public std::enable_shared_from_this<Preferences>
{
Expand Down Expand Up @@ -42,7 +46,7 @@ class Preferences : public std::enable_shared_from_this<Preferences>
bool remote_streaming_host_support_ = false;
bool remote_streaming_host_prefer_power_off_ = true;
bool external_api_support_ = false;
bool preshutdown_timing_ = false;
int shutdown_timing_ = PREFS_SHUTDOWN_TIMING_DEFAULT;
std::string data_path_;
std::vector<Device> devices_;
nlohmann::json lg_api_commands_json;
Expand Down
2 changes: 2 additions & 0 deletions Common/restart_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ R"(
{
"Restart" : [
"restart",
"Redémarrer",
"redémarrer",
"starta om",
"reiniciar",
Expand Down Expand Up @@ -40,6 +41,7 @@ R"(
"shutdown",
"power off",
"arrêter",
"sarrêter.",
"se mettre hors tension",
"se mettre hors tension.",
"stäng av",
Expand Down
4 changes: 2 additions & 2 deletions LGTV Companion Service/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ VOID SvcReportStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHi
context.service_status.dwWaitHint = dwWaitHint;
if (dwCurrentState == SERVICE_START_PENDING)
context.service_status.dwControlsAccepted = 0;
else context.service_status.dwControlsAccepted = context.prefs->preshutdown_timing_ ? SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PRESHUTDOWN | SERVICE_ACCEPT_POWEREVENT : SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_POWEREVENT;
else context.service_status.dwControlsAccepted = context.prefs->shutdown_timing_ == PREFS_SHUTDOWN_TIMING_EARLY ? SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PRESHUTDOWN | SERVICE_ACCEPT_POWEREVENT : SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_POWEREVENT;
if ((dwCurrentState == SERVICE_RUNNING) ||
(dwCurrentState == SERVICE_STOPPED))
context.service_status.dwCheckPoint = 0;
Expand Down Expand Up @@ -399,7 +399,7 @@ VOID WINAPI SvcMain(DWORD dwArgc, LPTSTR* lpszArgv)
&context, //Context
(EVT_SUBSCRIBE_CALLBACK)SvcEventLogSubscribeCallback, //callback
EvtSubscribeToFutureEvents);
SetProcessShutdownParameters(prefs.preshutdown_timing_ ? 0x100 : 0x3FF, SHUTDOWN_NORETRY);
SetProcessShutdownParameters(prefs.shutdown_timing_ == PREFS_SHUTDOWN_TIMING_EARLY? 0x100 : 0x3FF, SHUTDOWN_NORETRY);
SvcReportStatus(SERVICE_RUNNING, NO_ERROR, 0, context);
SvcReportEvent(EVENTLOG_INFORMATION_TYPE, L"The service has started.");
lgtv_companion.systemEvent(EVENT_SYSTEM_BOOT);
Expand Down
2 changes: 1 addition & 1 deletion LGTV Companion Setup/Product.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
<?define LGTV Companion Service_TargetDir=$(var.LGTV Companion Service.TargetDir)?><?define LGTV Companion UI_TargetDir=$(var.LGTV Companion UI.TargetDir)?>
<Product Id="524C2902-608C-467D-8A87-0F2638E5888A" Name="LGTV Companion" Language="1033" Version="4.0.3" Manufacturer="J Persson" UpgradeCode="0BA17E5B-11CE-491D-B1A1-05DD2D9F610A">
<Product Id="D86DF48E-4ECE-4248-9C66-C971B8C44FB7" Name="LGTV Companion" Language="1033" Version="4.0.5" Manufacturer="J Persson" UpgradeCode="0BA17E5B-11CE-491D-B1A1-05DD2D9F610A">
<Package Id="*" InstallerVersion="301" Compressed="yes" InstallScope="perMachine" Platform='x64' Description="LGTV Companion installer" InstallPrivileges="elevated" AdminImage="yes"/>
<Media Id="1" Cabinet="LGTVapp.cab" EmbedCab="yes" />

Expand Down
Binary file modified LGTV Companion UI/LGTV Companion UI.rc
Binary file not shown.
1 change: 1 addition & 0 deletions LGTV Companion UI/LGTV Companion UI.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\Common\ipc.cpp" />
<ClCompile Include="..\Common\log.cpp" />
<ClCompile Include="..\Common\preferences.cpp" />
<ClCompile Include="..\Common\tools.cpp" />
<ClCompile Include="lgtv_companion_ui.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions LGTV Companion UI/LGTV Companion UI.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
<ClCompile Include="..\Common\tools.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Common\log.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="LGTV Companion UI.rc">
Expand Down
33 changes: 27 additions & 6 deletions LGTV Companion UI/lgtv_companion_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ COPYRIGHT
#include <Iphlpapi.h>
#include <ws2tcpip.h>
#include "resource.h"
#include "../Common/log.h"

#pragma comment(lib, "Comctl32.lib")
#pragma comment(lib, "wevtapi.lib")
Expand Down Expand Up @@ -161,6 +162,7 @@ bool ResetAPIkeys = false;
std::vector<Preferences::ProcessList> WhitelistTemp;
std::vector<Preferences::ProcessList> ExclusionsTemp;
std::shared_ptr<IpcClient> pPipeClient;
std::shared_ptr<Logging> logger;

Preferences Prefs(CONFIG_FILE);

Expand Down Expand Up @@ -209,6 +211,10 @@ int APIENTRY wWinMain(_In_ HINSTANCE Instance,
}
Prefs.topology_support_ = bTop ? Prefs.topology_support_ : false;

std::wstring file = tools::widen(Prefs.data_path_);
file += L"ui_log.txt";
logger = std::make_shared<Logging>(0, file);

// Initiate PipeClient IPC
pPipeClient = std::make_shared<IpcClient>(PIPENAME, ipcCallback, (LPVOID)NULL);

Expand Down Expand Up @@ -1429,7 +1435,9 @@ LRESULT CALLBACK WndOptionsProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l
SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMING), (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)s.c_str());
s = L"Early";
SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMING), (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)s.c_str());
SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMING), (UINT)CB_SETCURSEL, (WPARAM)Prefs.preshutdown_timing_ ? 1 : 0, (LPARAM)0);
s = L"Delayed";
SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMING), (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)s.c_str());
SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMING), (UINT)CB_SETCURSEL, (WPARAM)Prefs.shutdown_timing_, (LPARAM)0);

SendMessage(GetDlgItem(hWnd, IDC_COMBO_LOG), (UINT)CB_RESETCONTENT, (WPARAM)0, (LPARAM)0);
s = L"Off";
Expand Down Expand Up @@ -1681,10 +1689,12 @@ LRESULT CALLBACK WndOptionsProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l
Prefs.remote_streaming_host_prefer_power_off_ = true;

int selection_timing = (int)(SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMING), (UINT)CB_GETCURSEL, (WPARAM)0, (LPARAM)0));
if (selection_timing == 1)
Prefs.preshutdown_timing_ = true;
if (selection_timing == 2)
Prefs.shutdown_timing_ = PREFS_SHUTDOWN_TIMING_DELAYED;
else if (selection_timing == 1)
Prefs.shutdown_timing_ = PREFS_SHUTDOWN_TIMING_EARLY;
else
Prefs.preshutdown_timing_ = false;
Prefs.shutdown_timing_ = PREFS_SHUTDOWN_TIMING_DEFAULT;

int count = ListView_GetItemCount(GetDlgItem(hWnd, IDC_LIST));
Prefs.event_log_restart_strings_custom_.clear();
Expand Down Expand Up @@ -3060,6 +3070,7 @@ static BOOL CALLBACK meproc(HMONITOR hMonitor, HDC hdc, LPRECT lprcMonitor, LPAR
ZeroMemory(&mi, sizeof(mi));
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor, &mi);
logger->debug("Topology", "callback...");
isError = GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &requiredPaths, &requiredModes);
if (isError)
{
Expand All @@ -3069,6 +3080,7 @@ static BOOL CALLBACK meproc(HMONITOR hMonitor, HDC hdc, LPRECT lprcMonitor, LPAR
paths.resize(requiredPaths);
modes.resize(requiredModes);

logger->debug("Topology", "QueryDisplayConfig");
isError = QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &requiredPaths, paths.data(), &requiredModes, modes.data(), NULL);
if (isError)
{
Expand All @@ -3077,6 +3089,7 @@ static BOOL CALLBACK meproc(HMONITOR hMonitor, HDC hdc, LPRECT lprcMonitor, LPAR
}
paths.resize(requiredPaths);
modes.resize(requiredModes);
logger->debug("Topology", "Iterate paths");

for (auto& p : paths)
{
Expand All @@ -3088,6 +3101,12 @@ static BOOL CALLBACK meproc(HMONITOR hMonitor, HDC hdc, LPRECT lprcMonitor, LPAR
sourceName.header.id = p.sourceInfo.id;

DisplayConfigGetDeviceInfo(&sourceName.header);
std::wstring logmessage = L"monitorinfo: ";
logmessage += mi.szDevice;
logger->debug("Topology", tools::narrow(logmessage));
logmessage = L"source: ";
logmessage += sourceName.viewGdiDeviceName;
logger->debug("Topology", tools::narrow(logmessage));
if (wcscmp(mi.szDevice, sourceName.viewGdiDeviceName) == 0)
{
DISPLAYCONFIG_TARGET_DEVICE_NAME name;
Expand All @@ -3096,11 +3115,13 @@ static BOOL CALLBACK meproc(HMONITOR hMonitor, HDC hdc, LPRECT lprcMonitor, LPAR
name.header.adapterId = p.sourceInfo.adapterId;
name.header.id = p.targetInfo.id;
DisplayConfigGetDeviceInfo(&name.header);

std::wstring FriendlyName = tools::tolower(name.monitorFriendlyDeviceName);
// transform(FriendlyName.begin(), FriendlyName.end(), FriendlyName.begin(), ::tolower);
logmessage = L"friendly name: ";
logmessage += FriendlyName;
logger->debug("Topology", tools::narrow(logmessage));
if (FriendlyName.find(L"lg tv") != std::wstring::npos)
{
logger->debug("Topology", "Match!");
DisplayInfo di;
di.monitorinfo = mi;
di.hMonitor = hMonitor;
Expand Down
6 changes: 6 additions & 0 deletions LGTV Companion User/Daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,12 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
default:break;
}
}break;
case WM_QUERYENDSESSION:
{
if(Prefs.shutdown_timing_ == PREFS_SHUTDOWN_TIMING_DELAYED)
Sleep(1500);
return false; // because it is a dialog
}break;
case WM_POWERBROADCAST:
{
switch (wParam)
Expand Down

0 comments on commit 7deeaa0

Please sign in to comment.