Skip to content

Commit

Permalink
send key presses to the active window when none of the predefined (wm…
Browse files Browse the repository at this point in the history
…c/kodi) ones are active
  • Loading branch information
opdenkamp committed Mar 18, 2015
1 parent d9a4131 commit 7c8d21e
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/LibCecTray/LibCECTray.csproj
Expand Up @@ -123,6 +123,7 @@
<Compile Include="controller\applications\internal\KodiControllerUI.Designer.cs">
<DependentUpon>KodiControllerUI.cs</DependentUpon>
</Compile>
<Compile Include="controller\applications\internal\ForegroundAppController.cs" />
<Compile Include="controller\CECController.cs" />
<Compile Include="settings\CECSettingBool.cs" />
<Compile Include="settings\CECSettingByte.cs" />
Expand Down
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.

7 changes: 5 additions & 2 deletions src/LibCecTray/Properties/Resources.resx
Expand Up @@ -112,10 +112,10 @@
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="could_not_connect_try_again" xml:space="preserve">
<value>Could not detect to any CEC adapter. Please check your configuration. Do you want to try again?</value>
Expand Down Expand Up @@ -623,4 +623,7 @@ CEC will not work (properly) if the TV does not support CEC, or has CEC disabled
<data name="global_stop_tv_standby" xml:space="preserve">
<value>Stop playback when the TV goes into standby</value>
</data>
<data name="application_foreground" xml:space="preserve">
<value>Foreground application</value>
</data>
</root>
7 changes: 4 additions & 3 deletions src/LibCecTray/controller/CECController.cs
Expand Up @@ -39,6 +39,7 @@
using LibCECTray.settings;
using LibCECTray.ui;
using Microsoft.Win32;
using LibCECTray.controller.applications.@internal;

namespace LibCECTray.controller
{
Expand Down Expand Up @@ -315,7 +316,7 @@ public void SetControlsEnabled(bool val)
{
Settings.Enabled = val;
foreach (var app in _applications)
app.UiControl.SetEnabled(val);
app.UiControl.SetEnabled(app.ProcessName != "");
_gui.SetControlsEnabled(val);
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(PowerModeChanged);
}
Expand Down Expand Up @@ -374,9 +375,10 @@ public override int ReceiveCommand(CecCommand command)

public override int ReceiveKeypress(CecKeypress key)
{
bool keySent = false;
foreach (var app in _applications)
{
bool keySent = app.SendKey(key, app.UiName == _gui.SelectedTabName);
keySent = app.SendKey(key, app.UiName == _gui.SelectedTabName);

if (keySent)
{
Expand All @@ -385,7 +387,6 @@ public override int ReceiveKeypress(CecKeypress key)
break;
}
}

return 1;
}

Expand Down
10 changes: 6 additions & 4 deletions src/LibCecTray/controller/applications/ApplicationController.cs
Expand Up @@ -146,7 +146,8 @@ public void BindButtonConfiguration(DataGridView gridView, BindingSource binding
/// <returns>True when running, false otherwise</returns>
public virtual bool IsRunning()
{
return FindInstance() != IntPtr.Zero;
return ProcessName == "" ||
FindInstance() != IntPtr.Zero;
}

/// <summary>
Expand Down Expand Up @@ -204,6 +205,7 @@ public virtual bool Start(bool bExitAfterStarting)
/// </summary>
public virtual void Initialise()
{
if (ProcessName == "") return;
Timer timer = new Timer { Interval = 1000, AutoReset = true };
timer.Elapsed += delegate { CheckApplicationEnabled(); };
timer.Start();
Expand Down Expand Up @@ -247,8 +249,8 @@ public virtual bool SendKey(CecKeypress key, bool isSelectedTab)
if (mappedButton == null || mappedButton.Value.Empty())
return false;

var controlWindow = FindInstance();
if (controlWindow != IntPtr.Zero && (key.Duration == 0 || key.Duration > 500))
var controlWindow = ProcessName == "" ? IntPtr.Zero : FindInstance();
if ((ProcessName == "" || controlWindow != IntPtr.Zero) && (key.Duration == 0 || key.Duration > 500))
return mappedButton.Value.Transmit(controlWindow);

return false;
Expand Down Expand Up @@ -379,7 +381,7 @@ public CECSettingBool StartFullScreen
protected ControllerTabPage UIControlInternal;
public virtual ControllerTabPage UiControl
{
get { return UIControlInternal ?? (UIControlInternal = new ApplicationControllerUI(this)); }
get { return UIControlInternal ?? (UIControlInternal = new ApplicationControllerUI(this, ProcessName == "")); }
}

private CecButtonConfig _buttonConfig;
Expand Down
20 changes: 15 additions & 5 deletions src/LibCecTray/controller/applications/ApplicationControllerUI.cs
Expand Up @@ -41,20 +41,30 @@ public ApplicationControllerUI()
InitializeComponent();
}

public ApplicationControllerUI(ApplicationController controller)
public ApplicationControllerUI(ApplicationController controller, bool hideAppButtons)
{
_controller = controller;
InitializeComponent();
Name = controller.UiName;
Text = controller.UiName;

_controller.BindButtonConfiguration(dgButtonConfig, buttonBindingSource);
_controller.AutoStartApplication.ReplaceControls(this, Controls, cbAutoStartApplication);
_controller.ControlApplication.ReplaceControls(this, Controls, cbControlApplication);
_controller.SuppressKeypressWhenSelected.ReplaceControls(this, Controls, cbSuppressKeypress);
_controller.StartFullScreen.ReplaceControls(this, Controls, cbStartFullScreen);

bConfigure.Enabled = _controller.CanConfigureProcess;
if (hideAppButtons)
{
cbAutoStartApplication.Visible = false;
cbControlApplication.Visible = false;
cbStartFullScreen.Visible = false;
bConfigure.Visible = false;
}
else
{
bConfigure.Enabled = _controller.CanConfigureProcess;
_controller.AutoStartApplication.ReplaceControls(this, Controls, cbAutoStartApplication);
_controller.ControlApplication.ReplaceControls(this, Controls, cbControlApplication);
_controller.StartFullScreen.ReplaceControls(this, Controls, cbStartFullScreen);
}
}

public override sealed string Text
Expand Down
2 changes: 2 additions & 0 deletions src/LibCecTray/controller/applications/Applications.cs
Expand Up @@ -104,6 +104,8 @@ public static List<ApplicationController> GetAll()
defaultValues.Add(wmcController.ProcessName, wmcController);
KodiController xbmcController = new KodiController(_controller);
defaultValues.Add(xbmcController.ProcessName, xbmcController);
ForegroundAppController fgaController = new ForegroundAppController(_controller);
defaultValues.Add(fgaController.ProcessName, fgaController);

return defaultValues;
}
Expand Down
9 changes: 6 additions & 3 deletions src/LibCecTray/controller/applications/WindowsAPI.cs
Expand Up @@ -414,9 +414,12 @@ public static bool ForceForeground(IntPtr windowHandle)
/// <returns>True when sent false otherwise</returns>
public static bool SendInputTo(IntPtr windowHandle, uint numberOfInputs, Input[] input, int structSize)
{
return ShowWindowAsync(windowHandle, (int)ShowType.ShowNormal) &&
ForceForeground(windowHandle) &&
SendInput(numberOfInputs, input, structSize) == 0;
if (windowHandle != IntPtr.Zero)
{
if (ShowWindowAsync(windowHandle, (int)ShowType.ShowNormal) || ForceForeground(windowHandle))
return false;
}
return SendInput(numberOfInputs, input, structSize) > 0;
}

/// <summary>
Expand Down

0 comments on commit 7c8d21e

Please sign in to comment.