Skip to content

Commit

Permalink
- More robust operations when RDP remote host
Browse files Browse the repository at this point in the history
- Option to power off devices during RDP
- Some additional help texts in the options dialog
- Bugfixes and optimisations
  • Loading branch information
JPersson77 committed Jun 30, 2022
1 parent 63d552c commit 7bb6a3e
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 82 deletions.
61 changes: 60 additions & 1 deletion LGTV Companion Service/Service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,13 @@ DWORD SvcCtrlHandler(DWORD dwCtrl, DWORD dwEventType, LPVOID lpEventData, LPVOI
EventCallbackStatus = NULL;;
Log("** System resumed from low power state (Automatic).");
DispatchSystemPowerEvent(SYSTEM_EVENT_RESUMEAUTO);
Prefs.DisplayIsCurrentlyRequestedPoweredOnByWindows = true;
break;
case PBT_APMRESUMESUSPEND:
EventCallbackStatus = NULL;;
Log("** System resumed from low power state.");
DispatchSystemPowerEvent(SYSTEM_EVENT_RESUME);
Prefs.DisplayIsCurrentlyRequestedPoweredOnByWindows = true;
break;
case PBT_APMSUSPEND:

Expand All @@ -427,16 +429,19 @@ DWORD SvcCtrlHandler(DWORD dwCtrl, DWORD dwEventType, LPVOID lpEventData, LPVOI
{
Log("** System is shutting down (low power mode).");
DispatchSystemPowerEvent(SYSTEM_EVENT_SUSPEND);
Prefs.DisplayIsCurrentlyRequestedPoweredOnByWindows = false;
}
else if (EventCallbackStatus == SYSTEM_EVENT_UNSURE)
{
Log("WARNING! Unable to determine if system is shutting down or restarting. Please check 'additional settings' in the UI.");
DispatchSystemPowerEvent(SYSTEM_EVENT_UNSURE);
Prefs.DisplayIsCurrentlyRequestedPoweredOnByWindows = false;
}
else
{
Log("** System is suspending to a low power state.");
DispatchSystemPowerEvent(SYSTEM_EVENT_SUSPEND);
Prefs.DisplayIsCurrentlyRequestedPoweredOnByWindows = false;
}
break;
case PBT_POWERSETTINGCHANGE:
Expand All @@ -451,16 +456,19 @@ DWORD SvcCtrlHandler(DWORD dwCtrl, DWORD dwEventType, LPVOID lpEventData, LPVOI
{
Log("** System requests displays OFF.");
DispatchSystemPowerEvent(SYSTEM_EVENT_DISPLAYOFF);
Prefs.DisplayIsCurrentlyRequestedPoweredOnByWindows = false;
}
else if (PBS->Data[0] == 2)
{
Log("** System requests displays OFF(DIMMED).");
DispatchSystemPowerEvent(SYSTEM_EVENT_DISPLAYDIMMED);
Prefs.DisplayIsCurrentlyRequestedPoweredOnByWindows = false;
}
else
{
Log("** System requests displays ON.");
DispatchSystemPowerEvent(SYSTEM_EVENT_DISPLAYON);
Prefs.DisplayIsCurrentlyRequestedPoweredOnByWindows = true;
}
}
else
Expand Down Expand Up @@ -493,17 +501,20 @@ DWORD SvcCtrlHandler(DWORD dwCtrl, DWORD dwEventType, LPVOID lpEventData, LPVOI
{
Log("** System is shutting down.");
DispatchSystemPowerEvent(SYSTEM_EVENT_SHUTDOWN);
Prefs.DisplayIsCurrentlyRequestedPoweredOnByWindows = false;
}
else if (EventCallbackStatus == SYSTEM_EVENT_UNSURE)
{
Log("WARNING! Unable to determine if system is shutting down or restarting. Please check 'additional settings in the UI.");
DispatchSystemPowerEvent(SYSTEM_EVENT_UNSURE);
Prefs.DisplayIsCurrentlyRequestedPoweredOnByWindows = false;
}
else
{
//This does happen sometimes, probably for timing reasons when shutting down the system.
Log("WARNING! The application did not receive an Event Subscription Callback prior to system shutting down. Unable to determine if system is shutting down or restarting.");
DispatchSystemPowerEvent(SYSTEM_EVENT_UNSURE);
Prefs.DisplayIsCurrentlyRequestedPoweredOnByWindows = false;
}

ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 20000);
Expand Down Expand Up @@ -693,6 +704,10 @@ bool ReadConfigFile()
if (!j.empty() && j.is_number())
Prefs.BlankScreenWhenIdleDelay = j.get<int>();

j = jsonPrefs[JSON_PREFS_NODE][JSON_RDP_POWEROFF];
if (!j.empty() && j.is_boolean())
Prefs.PowerOffDuringRDP = j.get<bool>();

Log(st);
Log("Configuration file successfully read");
Log(ty);
Expand Down Expand Up @@ -1064,7 +1079,51 @@ void IPCThread(void)
Log("IPC, User is idle.");
DispatchSystemPowerEvent(SYSTEM_EVENT_USERIDLE);
}

else if (param == "remoteconnect_busy")
{
if (Prefs.PowerOffDuringRDP)
{
Log("IPC, Remote session connected. User idle management disabled, Powering off managed displays.");
DispatchSystemPowerEvent(SYSTEM_EVENT_DISPLAYOFF);
}
else
Log("IPC, Remote session connected. User idle management disabled.");
}
else if (param == "remoteconnect_idle")
{
if (Prefs.PowerOffDuringRDP)
{
Log("IPC, Remote session connected. User idle management disabled. Powering off managed displays.");
DispatchSystemPowerEvent(SYSTEM_EVENT_DISPLAYOFF);
}
else
{
Log("IPC, Remote session connected. User idle management disabled");
DispatchSystemPowerEvent(SYSTEM_EVENT_UNBLANK);
}
}
else if (param == "remoteconnect")
{
if (Prefs.PowerOffDuringRDP)
{
Log("IPC, Remote session connected. Powering off managed displays.");
DispatchSystemPowerEvent(SYSTEM_EVENT_DISPLAYOFF);
}
else
Log("IPC, Remote session connected.");
}
else if (param == "remotedisconnect")
{
if (Prefs.DisplayIsCurrentlyRequestedPoweredOnByWindows)
{
Log("IPC, Remote session disconnected. Powering on managed displays.");
DispatchSystemPowerEvent(SYSTEM_EVENT_DISPLAYON);
}
else
{
Log("IPC, Remote session disconnected.");
}
}
}
else
{
Expand Down
12 changes: 9 additions & 3 deletions LGTV Companion Service/Service.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#pragma comment(lib, "Iphlpapi.lib")

#define APPNAME L"LGTV Companion"
#define APPVERSION L"1.6.2"
#define APPVERSION L"1.7.0"
#define SVCNAME L"LGTVsvc"
#define SVCDISPLAYNAME L"LGTV Companion Service"
#define SERVICE_PORT "3000"
Expand All @@ -52,6 +52,7 @@
#define DEFAULT_SHUTDOWN {"shutdown","power off"}
#define JSON_IDLEBLANK "BlankWhenIdle"
#define JSON_IDLEBLANKDELAY "BlankWhenIdleDelay"
#define JSON_RDP_POWEROFF "PowerOffDuringRDP"

#define SERVICE_DEPENDENCIES L"Dhcp\0Dnscache\0LanmanServer\0\0"
#define SERVICE_ACCOUNT NULL //L"NT AUTHORITY\\LocalService"
Expand All @@ -75,6 +76,7 @@
#define SYSTEM_EVENT_USERIDLE 14
#define SYSTEM_EVENT_FORCESETHDMI 15
#define SYSTEM_EVENT_BOOT 16
#define SYSTEM_EVENT_UNBLANK 17

#define APP_CMDLINE_ON 1
#define APP_CMDLINE_OFF 2
Expand Down Expand Up @@ -116,6 +118,9 @@ struct PREFS {
int PowerOnTimeout = 40;
bool BlankWhenIdle = false;
int BlankScreenWhenIdleDelay = 10;
bool PowerOffDuringRDP = false;
bool DisplayIsCurrentlyRequestedPoweredOnByWindows = false;

};

struct SESSIONPARAMETERS {
Expand All @@ -134,6 +139,7 @@ struct SESSIONPARAMETERS {
int BlankScreenWhenIdleDelay = 10;
bool SetHDMIInputOnResume = false;
int SetHDMIInputOnResumeToNumber = 1;

};

class CSession {
Expand All @@ -152,7 +158,7 @@ class CSession {
bool ThreadedOpDisplayOff = false;
bool ThreadedOpDisplaySetHdmiInput = false;
time_t ThreadedOpDisplayOffTime = 0;
void TurnOnDisplay(void);
void TurnOnDisplay(bool SendWOL);
void TurnOffDisplay(bool forced, bool dimmed, bool blankscreen);
void SetDisplayHdmiInput(int HdmiInput);

Expand All @@ -177,7 +183,7 @@ void Log(std::string);
DWORD WINAPI SubCallback(EVT_SUBSCRIBE_NOTIFY_ACTION Action, PVOID UserContext, EVT_HANDLE Event);
std::wstring widen(std::string);
std::string narrow(std::wstring);
void DisplayPowerOnThread(SESSIONPARAMETERS *, bool *, int);
void DisplayPowerOnThread(SESSIONPARAMETERS *, bool *, int, bool);
void DisplayPowerOffThread(SESSIONPARAMETERS*, bool *, bool, bool);
void SetDisplayHdmiInputThread(SESSIONPARAMETERS*, bool*, int, int);

Expand Down
23 changes: 15 additions & 8 deletions LGTV Companion Service/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void CSession::SetDisplayHdmiInput(int HdmiInput)
Log(s);
return;
}
void CSession::TurnOnDisplay()
void CSession::TurnOnDisplay(bool SendWOL)
{
string s;

Expand All @@ -175,7 +175,7 @@ void CSession::TurnOnDisplay()
ThreadedOpDisplayOn = true;
// ThreadedOperationsTimeStamp = time(0);

thread thread_obj(DisplayPowerOnThread, &Parameters, &ThreadedOpDisplayOn, Parameters.PowerOnTimeout);
thread thread_obj(DisplayPowerOnThread, &Parameters, &ThreadedOpDisplayOn, Parameters.PowerOnTimeout, SendWOL);
thread_obj.detach();
}
else
Expand Down Expand Up @@ -233,7 +233,7 @@ void CSession::SystemEvent(DWORD dwMsg, int param)
{
// forced events are always processed, i.e. the user has issued this command.
if (dwMsg == SYSTEM_EVENT_FORCEON)
TurnOnDisplay();
TurnOnDisplay(true);
if (dwMsg == SYSTEM_EVENT_FORCEOFF)
TurnOffDisplay(true, false, false);
if (dwMsg == SYSTEM_EVENT_FORCESCREENOFF)
Expand Down Expand Up @@ -279,7 +279,7 @@ void CSession::SystemEvent(DWORD dwMsg, int param)

case SYSTEM_EVENT_DISPLAYON:
{
TurnOnDisplay();
TurnOnDisplay(true);
}break;
case SYSTEM_EVENT_DISPLAYOFF:
{
Expand All @@ -297,18 +297,22 @@ void CSession::SystemEvent(DWORD dwMsg, int param)
case SYSTEM_EVENT_USERBUSY:
{
if(Parameters.BlankWhenIdle)
TurnOnDisplay();
TurnOnDisplay(true);
}break;
case SYSTEM_EVENT_BOOT:
{
if (Parameters.SetHDMIInputOnResume)
SetDisplayHdmiInput(Parameters.SetHDMIInputOnResumeToNumber);
}break;
case SYSTEM_EVENT_UNBLANK:
{
TurnOnDisplay(false);
}break;
default:break;
}
}
// THREAD: Spawned when the device should power ON. This thread manages the pairing key from the display and verifies that the display has been powered on
void DisplayPowerOnThread(SESSIONPARAMETERS * CallingSessionParameters, bool * CallingSessionThreadRunning, int Timeout)
void DisplayPowerOnThread(SESSIONPARAMETERS * CallingSessionParameters, bool * CallingSessionThreadRunning, int Timeout, bool SendWOL)
{
string screenonmess = "{ \"id\":\"2\",\"type\" : \"request\",\"uri\" : \"ssap://com.webos.service.tvpower/power/turnOnScreen\"}";

Expand All @@ -323,8 +327,11 @@ void DisplayPowerOnThread(SESSIONPARAMETERS * CallingSessionParameters, bool * C
string handshake;
time_t origtim = time(0);

thread wolthread(WOLthread, CallingSessionParameters, CallingSessionThreadRunning, Timeout);
wolthread.detach();
if (SendWOL)
{
thread wolthread(WOLthread, CallingSessionParameters, CallingSessionThreadRunning, Timeout);
wolthread.detach();
}

// build the appropriate WebOS handshake
if (key == "")
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="135A627A-1B23-4D62-9E93-5C827E59BEC1" Name="LGTV Companion" Language="1033" Version="1.6.2" Manufacturer="J Persson" UpgradeCode="0BA17E5B-11CE-491D-B1A1-05DD2D9F610A">
<Product Id="9AD205C9-ACBF-4A46-9D1F-80A95DF34F2B" Name="LGTV Companion" Language="1033" Version="1.7.0" 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
Loading

0 comments on commit 7bb6a3e

Please sign in to comment.