steam-deck-tools/PowerControl/Controller.cs

353 lines
11 KiB
C#
Raw Normal View History

using CommonHelpers;
using ExternalHelpers;
using Microsoft.VisualBasic.Logging;
using PowerControl.External;
2022-11-18 21:00:52 +01:00
using PowerControl.Helpers;
using RTSSSharedMemoryNET;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.AxHost;
namespace PowerControl
{
internal class Controller : IDisposable
{
public const String Title = "Power Control";
public readonly String TitleWithVersion = Title + " v" + Application.ProductVersion.ToString();
2022-11-15 22:47:30 +01:00
public const int KeyPressRepeatTime = 300;
public const int KeyPressNextRepeatTime = 150;
Container components = new Container();
System.Windows.Forms.NotifyIcon notifyIcon;
StartupManager startupManager = new StartupManager(Title);
Menu.MenuRoot rootMenu = MenuStack.Root;
OSD osd;
System.Windows.Forms.Timer osdDismissTimer;
hidapi.HidDevice neptuneDevice = new hidapi.HidDevice(0x28de, 0x1205, 64);
SDCInput neptuneDeviceState = new SDCInput();
2022-11-15 22:47:30 +01:00
DateTime? neptuneDeviceNextKey;
System.Windows.Forms.Timer neptuneTimer;
2022-11-20 15:42:21 +01:00
SharedData<PowerControlSetting> sharedData = SharedData<PowerControlSetting>.CreateNew();
public Controller()
{
Instance.RunOnce(TitleWithVersion, "Global\\PowerControl");
var contextMenu = new System.Windows.Forms.ContextMenuStrip(components);
contextMenu.Opening += delegate (object? sender, CancelEventArgs e)
{
rootMenu.Update();
};
rootMenu.Visible = false;
rootMenu.Update();
rootMenu.CreateMenu(contextMenu.Items);
rootMenu.VisibleChanged = delegate ()
{
2022-11-15 21:33:08 +01:00
updateOSD();
};
contextMenu.Items.Add(new ToolStripSeparator());
if (startupManager.IsAvailable)
{
var startupItem = new ToolStripMenuItem("Run On Startup");
startupItem.Checked = startupManager.Startup;
startupItem.Click += delegate
{
startupManager.Startup = !startupManager.Startup;
startupItem.Checked = startupManager.Startup;
};
contextMenu.Items.Add(startupItem);
}
var helpItem = contextMenu.Items.Add("&Help");
helpItem.Click += delegate
{
System.Diagnostics.Process.Start("explorer.exe", "https://steam-deck-tools.ayufan.dev");
};
contextMenu.Items.Add(new ToolStripSeparator());
var exitItem = contextMenu.Items.Add("&Exit");
exitItem.Click += ExitItem_Click;
notifyIcon = new System.Windows.Forms.NotifyIcon(components);
notifyIcon.Icon = Resources.traffic_light_outline;
notifyIcon.Text = TitleWithVersion;
notifyIcon.Visible = true;
notifyIcon.ContextMenuStrip = contextMenu;
osdDismissTimer = new System.Windows.Forms.Timer(components);
osdDismissTimer.Interval = 3000;
osdDismissTimer.Tick += delegate (object? sender, EventArgs e)
{
hideOSD();
};
var osdTimer = new System.Windows.Forms.Timer(components);
osdTimer.Tick += OsdTimer_Tick;
osdTimer.Interval = 250;
osdTimer.Enabled = true;
GlobalHotKey.RegisterHotKey(Settings.Default.MenuUpKey, () =>
{
2022-11-18 21:00:52 +01:00
if (!RTSS.IsOSDForeground())
2022-11-16 08:28:17 +01:00
return;
rootMenu.Prev();
2022-11-15 21:33:08 +01:00
setDismissTimer();
2022-11-16 08:28:17 +01:00
dismissNeptuneInput();
}, true);
GlobalHotKey.RegisterHotKey(Settings.Default.MenuDownKey, () =>
{
2022-11-18 21:00:52 +01:00
if (!RTSS.IsOSDForeground())
2022-11-16 08:28:17 +01:00
return;
rootMenu.Next();
2022-11-15 21:33:08 +01:00
setDismissTimer();
2022-11-16 08:28:17 +01:00
dismissNeptuneInput();
}, true);
GlobalHotKey.RegisterHotKey(Settings.Default.MenuLeftKey, () =>
{
2022-11-18 21:00:52 +01:00
if (!RTSS.IsOSDForeground())
2022-11-16 08:28:17 +01:00
return;
rootMenu.SelectPrev();
2022-11-15 21:33:08 +01:00
setDismissTimer();
2022-11-16 08:28:17 +01:00
dismissNeptuneInput();
});
GlobalHotKey.RegisterHotKey(Settings.Default.MenuRightKey, () =>
{
2022-11-18 21:00:52 +01:00
if (!RTSS.IsOSDForeground())
2022-11-16 08:28:17 +01:00
return;
rootMenu.SelectNext();
2022-11-15 21:33:08 +01:00
setDismissTimer();
2022-11-16 08:28:17 +01:00
dismissNeptuneInput();
});
if (Settings.Default.EnableNeptuneController)
{
neptuneTimer = new System.Windows.Forms.Timer(components);
neptuneTimer.Interval = 1000 / 60;
neptuneTimer.Tick += NeptuneTimer_Tick;
neptuneTimer.Enabled = true;
neptuneDevice.OnInputReceived += NeptuneDevice_OnInputReceived;
neptuneDevice.OpenDevice();
neptuneDevice.BeginRead();
}
2022-11-16 08:28:17 +01:00
if (Settings.Default.EnableVolumeControls)
{
GlobalHotKey.RegisterHotKey("VolumeUp", () =>
{
if (neptuneDeviceState.buttons5.HasFlag(SDCButton5.BTN_QUICK_ACCESS))
2022-11-16 08:28:17 +01:00
rootMenu.SelectNext("Brightness");
else
rootMenu.SelectNext("Volume");
setDismissTimer();
dismissNeptuneInput();
});
GlobalHotKey.RegisterHotKey("VolumeDown", () =>
{
if (neptuneDeviceState.buttons5.HasFlag(SDCButton5.BTN_QUICK_ACCESS))
2022-11-16 08:28:17 +01:00
rootMenu.SelectPrev("Brightness");
else
rootMenu.SelectPrev("Volume");
setDismissTimer();
dismissNeptuneInput();
});
}
}
private void OsdTimer_Tick(object? sender, EventArgs e)
{
try
{
notifyIcon.Text = TitleWithVersion + ". RTSS Version: " + OSD.Version;
notifyIcon.Icon = Resources.traffic_light_outline;
}
catch
{
notifyIcon.Text = TitleWithVersion + ". RTSS Not Available.";
notifyIcon.Icon = Resources.traffic_light_outline_red;
}
updateOSD();
}
private void NeptuneDevice_OnInputReceived(object? sender, hidapi.HidDeviceInputReceivedEventArgs e)
{
var input = SDCInput.FromBuffer(e.Buffer);
2022-11-15 22:47:30 +01:00
var filteredInput = new SDCInput()
{
buttons0 = input.buttons0,
buttons1 = input.buttons1,
buttons2 = input.buttons2,
buttons3 = input.buttons3,
buttons4 = input.buttons4,
buttons5 = input.buttons5
};
2022-11-15 22:47:30 +01:00
if (!neptuneDeviceState.Equals(filteredInput))
{
neptuneDeviceState = filteredInput;
neptuneDeviceNextKey = null;
}
// Consume only some events to avoid under-running SWICD
if (!neptuneDeviceState.buttons5.HasFlag(SDCButton5.BTN_QUICK_ACCESS))
Thread.Sleep(50);
}
2022-11-16 08:28:17 +01:00
private void dismissNeptuneInput()
{
neptuneDeviceNextKey = DateTime.UtcNow.AddDays(1);
}
private void NeptuneTimer_Tick(object? sender, EventArgs e)
{
2022-11-15 22:47:30 +01:00
var input = neptuneDeviceState;
if (neptuneDeviceNextKey == null)
neptuneDeviceNextKey = DateTime.UtcNow.AddMilliseconds(KeyPressRepeatTime);
else if (neptuneDeviceNextKey < DateTime.UtcNow)
neptuneDeviceNextKey = DateTime.UtcNow.AddMilliseconds(KeyPressNextRepeatTime);
else
return; // otherwise it did not yet trigger
2022-11-18 16:14:29 +01:00
// Reset sequence: 3 dots + L4|R4|L5|R5
if (input.buttons0 == SDCButton0.BTN_L5 &&
input.buttons1 == SDCButton1.BTN_R5 &&
2022-11-18 16:14:29 +01:00
input.buttons2 == 0 &&
input.buttons3 == 0 &&
input.buttons4 == (SDCButton4.BTN_L4 | SDCButton4.BTN_R4) &&
input.buttons5 == SDCButton5.BTN_QUICK_ACCESS)
2022-11-18 16:14:29 +01:00
{
rootMenu.Show();
rootMenu.Reset();
notifyIcon.ShowBalloonTip(3000, TitleWithVersion, "Settings were reset to default.", ToolTipIcon.Info);
return;
}
if (!neptuneDeviceState.buttons5.HasFlag(SDCButton5.BTN_QUICK_ACCESS) || !RTSS.IsOSDForeground())
{
2022-11-16 08:28:17 +01:00
// schedule next repeat far in the future
dismissNeptuneInput();
hideOSD();
return;
}
rootMenu.Show();
2022-11-15 21:33:08 +01:00
setDismissTimer(false);
if (input.buttons1 != 0 || input.buttons2 != 0 || input.buttons3 != 0 || input.buttons4 != 0)
{
return;
}
else if (input.buttons0 == SDCButton0.BTN_DPAD_LEFT)
{
rootMenu.SelectPrev();
}
else if (input.buttons0 == SDCButton0.BTN_DPAD_RIGHT)
{
rootMenu.SelectNext();
}
else if (input.buttons0 == SDCButton0.BTN_DPAD_UP)
{
rootMenu.Prev();
}
else if (input.buttons0 == SDCButton0.BTN_DPAD_DOWN)
{
rootMenu.Next();
}
}
2022-11-15 21:33:08 +01:00
private void setDismissTimer(bool enabled = true)
{
osdDismissTimer.Stop();
2022-11-15 21:33:08 +01:00
if (enabled)
osdDismissTimer.Start();
}
private void hideOSD()
{
if (!rootMenu.Visible)
return;
2022-11-16 08:28:17 +01:00
Trace.WriteLine("Hide OSD");
rootMenu.Visible = false;
osdDismissTimer.Stop();
updateOSD();
}
public void updateOSD()
{
2022-11-20 15:42:21 +01:00
sharedData.SetValue(new PowerControlSetting()
{
Current = rootMenu.Visible ? PowerControlVisible.Yes : PowerControlVisible.No
});
if (!rootMenu.Visible)
{
osdClose();
return;
}
try
{
2022-11-16 01:10:44 +01:00
// recreate OSD if index 0
if (OSDHelpers.OSDIndex("Power Control") == 0 && OSD.GetOSDCount() > 1)
osdClose();
if (osd == null)
2022-11-16 08:28:17 +01:00
{
osd = new OSD("Power Control");
2022-11-16 08:28:17 +01:00
Trace.WriteLine("Show OSD");
}
osd.Update(rootMenu.Render(null));
}
catch (SystemException)
{
}
}
private void ExitItem_Click(object? sender, EventArgs e)
{
Application.Exit();
}
public void Dispose()
{
components.Dispose();
osdClose();
}
private void osdClose()
{
try
{
if (osd != null)
2022-11-16 08:28:17 +01:00
{
osd.Dispose();
2022-11-16 08:28:17 +01:00
Trace.WriteLine("Close OSD");
}
osd = null;
}
catch (SystemException)
{
}
}
}
}