Skip to content

Add a feature to power on the TV when the PC is no longer idle. #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[*]
end_of_line = crlf
insert_final_newline = true
indent_style = space
indent_size = 2
4 changes: 2 additions & 2 deletions src/CecSharpTester/netfx/CecSharpTester.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CecSharpTester</RootNamespace>
<AssemblyName>CecSharpTester</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<FileUpgradeFlags>
</FileUpgradeFlags>
Expand Down Expand Up @@ -95,4 +95,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
2 changes: 1 addition & 1 deletion src/CecSharpTester/netfx/app.config
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/></startup></configuration>
6 changes: 3 additions & 3 deletions src/LibCecTray/LibCECTray.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
<DebugSymbols>true</DebugSymbols>
<OutputPath>..\..\..\..\build\$(Configuration)\$(Platform)\</OutputPath>
Expand Down Expand Up @@ -43,7 +43,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>LibCECTray</RootNamespace>
<AssemblyName>cec-tray</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SignAssembly>false</SignAssembly>
<PublishUrl>publish\</PublishUrl>
Expand Down Expand Up @@ -234,4 +234,4 @@
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
</Project>
</Project>
9 changes: 9 additions & 0 deletions src/LibCecTray/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/LibCecTray/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,9 @@ CEC will not work (properly) if the TV does not support CEC, or has CEC disabled
<data name="global_tv_auto_power_on" xml:space="preserve">
<value>Power on the TV when the PC starts</value>
</data>
<data name="global_tv_power_on_with_activity" xml:space="preserve">
<value>Power on the TV when the PC detects user activity</value>
</data>
<data name="global_hdmi_port" xml:space="preserve">
<value>HDMI port of the {0}</value>
</data>
Expand Down
2 changes: 1 addition & 1 deletion src/LibCecTray/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<configuration>
<startup>

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/></startup>
</configuration>
2 changes: 2 additions & 0 deletions src/LibCecTray/controller/Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ private void ProcessEventHandler(object src, UpdateEvent updateEvent)
_controller.Hide(true);
}

_controller.InitializationCompleted();

break;
case UpdateEventType.ExitApplication:
SuppressUpdates = true;
Expand Down
59 changes: 48 additions & 11 deletions src/LibCecTray/controller/CECController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,22 +436,53 @@ public void Hide(bool val)
{
_gui.SafeHide(val);
}

public void InitializationCompleted()
{
CecPowerStatus tvPowerStatus = Lib.GetDevicePowerStatus(CecLogicalAddress.Tv);
TvIsOn = tvPowerStatus == CecPowerStatus.On || tvPowerStatus == CecPowerStatus.InTransitionStandbyToOn;
}
#endregion

#region Callbacks called by libCEC
public override int ReceiveCommand(CecCommand command)
{
if (command.Opcode == CecOpcode.Standby &&
(command.Destination == CecLogicalAddress.Broadcast || command.Destination == _lib.GetLogicalAddresses().Primary))
if (Settings.StopTvStandby.Value)
{
var key = new CecKeypress(CecUserControlCode.Stop, 0);
foreach (var app in _applications)
app.SendKey(key, false);
Lib.DisableCallbacks();
Application.SetSuspendState(PowerState.Suspend, false, false);
}
return 0;
switch (command.Opcode)
{
case CecOpcode.Standby:
{
if (command.Initiator == CecLogicalAddress.Tv)
{
TvIsOn = false;
}

if (command.Destination == CecLogicalAddress.Broadcast || command.Destination == _lib.GetLogicalAddresses().Primary)
{
if (Settings.StopTvStandby.Value)
{
var key = new CecKeypress(CecUserControlCode.Stop, 0);
foreach (var app in _applications)
app.SendKey(key, false);
Lib.DisableCallbacks();
Application.SetSuspendState(PowerState.Suspend, false, false);
}
}

break;
}

case CecOpcode.GiveDevicePowerStatus:
{
if (command.Initiator == CecLogicalAddress.Tv)
{
TvIsOn = true;
}

break;
}
}

return 0;
}

public override int ReceiveKeypress(CecKeypress key)
Expand Down Expand Up @@ -681,6 +712,12 @@ public LibCecSharp Lib
}
private LibCecSharp _lib;

public bool TvIsOn
{
get;
private set;
}

private readonly CECTray _gui;
public Actions CECActions;
private bool _deviceChangeWarningDisplayed;
Expand Down
46 changes: 30 additions & 16 deletions src/LibCecTray/controller/SystemIdleMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public class SystemIdleMonitor
{
private SystemIdleMonitor()
{
_eventFired = false;
_refreshThread = new Thread(Refresh);
_refreshThread.Start();
}
Expand Down Expand Up @@ -151,22 +150,22 @@ private void RefreshIdleTime()
{
int lastIdleTimeSeconds = IdleTimeSeconds;
IdleTimeSeconds = WindowsAPI.SystemIdleSeconds();
if (IdleTimeoutSeconds <= 0)
{
return;
}
IdleTimeChange change = new IdleTimeChange(IdleTimeSeconds, IdleTimeoutSeconds);
SystemActivity?.Invoke(this, change);
bool lastIsIdle = IsIdle;
IsIdle = IdleTimeSeconds > 0;

if (IdleTimeSeconds < lastIdleTimeSeconds && _eventFired)
if (!IsIdle)
{
SystemIdle?.Invoke(this, new IdleChange(false));
_eventFired = false;
IdleTimeChange change = new IdleTimeChange(IdleTimeSeconds, IdleTimeoutSeconds);
SystemActivity?.Invoke(this, change);
}
else if ((IdleTimeSeconds > lastIdleTimeSeconds) && (IdleTimeSeconds >= IdleTimeoutSeconds) && !_eventFired)

if (lastIsIdle != IsIdle)
{
SystemIdle?.Invoke(this, new IdleChange(true));
_eventFired = true;
bool shouldStandby =
(IdleTimeoutSeconds > 0) &&
(IdleTimeSeconds > lastIdleTimeSeconds) &&
(IdleTimeSeconds >= IdleTimeoutSeconds);
SystemIdle?.Invoke(this, new IdleChange(IsIdle, shouldStandby));
}
}

Expand All @@ -178,6 +177,15 @@ public int IdleTimeSeconds {
private set;
}

/// <summary>
/// Is the system currently idle?
/// </summary>
public bool IsIdle
{
get;
private set;
}

public int IdleTimeoutSeconds {
get {
return Program.Instance.Controller.Settings.StandbyScreen.AsSettingIdleTime.Value.Seconds;
Expand Down Expand Up @@ -208,7 +216,6 @@ public static SystemIdleMonitor Instance {
public event EventHandler<ScreensaverChange> ScreensaverActivated;
public event EventHandler<SystemPowerChange> PowerStatusChanged;

private bool _eventFired;
private static SystemIdleMonitor _instance = null;
private bool _stopMonitor = false;
private Thread _refreshThread = null;
Expand Down Expand Up @@ -247,17 +254,24 @@ public ScreensaverChange(bool active)

public class IdleChange : EventArgs
{
public IdleChange(bool idle)
public IdleChange(bool idle, bool shouldStandby)
{
Idle = idle;
ShouldStandby = shouldStandby;
}

public bool Idle {
get;
private set;
}

public bool ShouldStandby
{
get;
private set;
}
}

public class IdleTimeChange : EventArgs
{
public IdleTimeChange(int idleTimeSeconds, int idleTimeoutSeconds)
Expand Down
15 changes: 15 additions & 0 deletions src/LibCecTray/settings/CECSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class CECSettings
public static string KeyStopTvStandby = "global_stop_tv_standby";
public static string KeyStandbyScreen = "global_standby_screen";
public static string KeyTVAutoPowerOn = "global_tv_auto_power_on";
public static string KeyTVPowerOnWithActivity = "global_tv_power_on_with_activity";
public static string KeyDetectPhysicalAddress = "global_detect_physical_address";
#endregion

Expand Down Expand Up @@ -363,6 +364,20 @@ public CECSettingBool TVAutoPowerOn {
}
}

public CECSettingBool TVPowerOnWithActivity
{
get {
if (!_settings.ContainsKey(KeyTVPowerOnWithActivity))
{
CECSettingBool setting = new CECSettingBool(KeyTVPowerOnWithActivity, Resources.global_tv_power_on_with_activity, true, _changedHandler);
_settings[KeyTVPowerOnWithActivity] = setting;
setting.Load();
setting.SettingChanged += OnSettingChanged;
}
return _settings[KeyTVPowerOnWithActivity].AsSettingBool;
}
}

public CECSettingIdleTime StandbyScreen {
get {
if (!_settings.ContainsKey(KeyStandbyScreen))
Expand Down
14 changes: 14 additions & 0 deletions src/LibCecTray/ui/CECTray.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading