2022-11-15 21:10:27 +01:00
|
|
|
|
using CommonHelpers;
|
2022-11-20 15:12:12 +01:00
|
|
|
|
using ExternalHelpers;
|
2022-11-14 11:06:03 +01:00
|
|
|
|
using Microsoft.VisualBasic.Logging;
|
|
|
|
|
|
using PowerControl.External;
|
2022-11-18 21:00:52 +01:00
|
|
|
|
using PowerControl.Helpers;
|
2022-11-14 11:06:03 +01:00
|
|
|
|
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;
|
2022-11-14 11:06:03 +01:00
|
|
|
|
|
|
|
|
|
|
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;
|
2022-11-14 11:06:03 +01:00
|
|
|
|
System.Windows.Forms.Timer neptuneTimer;
|
|
|
|
|
|
|
2022-11-20 15:42:21 +01:00
|
|
|
|
SharedData<PowerControlSetting> sharedData = SharedData<PowerControlSetting>.CreateNew();
|
|
|
|
|
|
|
2022-11-14 11:06:03 +01:00
|
|
|
|
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();
|
2022-11-14 11:06:03 +01:00
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
|
|
|
{
|
2022-12-10 10:21:10 +01:00
|
|
|
|
System.Diagnostics.Process.Start("explorer.exe", "https://steam-deck-tools.ayufan.dev");
|
2022-11-14 11:06:03 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
contextMenu.Items.Add(new ToolStripSeparator());
|
|
|
|
|
|
|
|
|
|
|
|
var exitItem = contextMenu.Items.Add("&Exit");
|
|
|
|
|
|
exitItem.Click += ExitItem_Click;
|
|
|
|
|
|
|
|
|
|
|
|
notifyIcon = new System.Windows.Forms.NotifyIcon(components);
|
2022-11-16 22:02:57 +01:00
|
|
|
|
notifyIcon.Icon = Resources.traffic_light_outline;
|
2022-11-14 11:06:03 +01:00
|
|
|
|
notifyIcon.Text = TitleWithVersion;
|
|
|
|
|
|
notifyIcon.Visible = true;
|
|
|
|
|
|
notifyIcon.ContextMenuStrip = contextMenu;
|
|
|
|
|
|
|
|
|
|
|
|
osdDismissTimer = new System.Windows.Forms.Timer(components);
|
|
|
|
|
|
osdDismissTimer.Interval = 3000;
|
2022-11-24 22:37:24 +01:00
|
|
|
|
osdDismissTimer.Tick += delegate (object? sender, EventArgs e)
|
2022-11-14 11:06:03 +01:00
|
|
|
|
{
|
|
|
|
|
|
hideOSD();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var osdTimer = new System.Windows.Forms.Timer(components);
|
2022-11-15 21:09:53 +01:00
|
|
|
|
osdTimer.Tick += OsdTimer_Tick;
|
2022-11-14 11:06:03 +01:00
|
|
|
|
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;
|
2022-11-14 11:06:03 +01:00
|
|
|
|
rootMenu.Prev();
|
2022-11-15 21:33:08 +01:00
|
|
|
|
setDismissTimer();
|
2022-11-16 08:28:17 +01:00
|
|
|
|
dismissNeptuneInput();
|
|
|
|
|
|
}, true);
|
2022-11-14 11:06:03 +01:00
|
|
|
|
|
|
|
|
|
|
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;
|
2022-11-14 11:06:03 +01:00
|
|
|
|
rootMenu.Next();
|
2022-11-15 21:33:08 +01:00
|
|
|
|
setDismissTimer();
|
2022-11-16 08:28:17 +01:00
|
|
|
|
dismissNeptuneInput();
|
|
|
|
|
|
}, true);
|
2022-11-14 11:06:03 +01:00
|
|
|
|
|
|
|
|
|
|
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;
|
2022-11-14 11:06:03 +01:00
|
|
|
|
rootMenu.SelectPrev();
|
2022-11-15 21:33:08 +01:00
|
|
|
|
setDismissTimer();
|
2022-11-16 08:28:17 +01:00
|
|
|
|
dismissNeptuneInput();
|
2022-11-14 11:06:03 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2022-11-14 11:06:03 +01:00
|
|
|
|
rootMenu.SelectNext();
|
2022-11-15 21:33:08 +01:00
|
|
|
|
setDismissTimer();
|
2022-11-16 08:28:17 +01:00
|
|
|
|
dismissNeptuneInput();
|
2022-11-14 11:06:03 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (Settings.Default.EnableNeptuneController)
|
|
|
|
|
|
{
|
|
|
|
|
|
neptuneTimer = new System.Windows.Forms.Timer(components);
|
2022-12-05 18:42:22 +01:00
|
|
|
|
neptuneTimer.Interval = 1000 / 60;
|
2022-11-14 11:06:03 +01:00
|
|
|
|
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", () =>
|
|
|
|
|
|
{
|
2022-11-24 22:37:24 +01:00
|
|
|
|
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", () =>
|
|
|
|
|
|
{
|
2022-11-24 22:37:24 +01:00
|
|
|
|
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();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-11-14 11:06:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-15 21:09:53 +01:00
|
|
|
|
private void OsdTimer_Tick(object? sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
notifyIcon.Text = TitleWithVersion + ". RTSS Version: " + OSD.Version;
|
2022-11-16 22:02:57 +01:00
|
|
|
|
notifyIcon.Icon = Resources.traffic_light_outline;
|
2022-11-15 21:09:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
notifyIcon.Text = TitleWithVersion + ". RTSS Not Available.";
|
2022-11-16 22:02:57 +01:00
|
|
|
|
notifyIcon.Icon = Resources.traffic_light_outline_red;
|
2022-11-15 21:09:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
updateOSD();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-14 11:06:03 +01:00
|
|
|
|
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()
|
2022-11-14 11:06:03 +01:00
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-14 11:06:03 +01:00
|
|
|
|
// Consume only some events to avoid under-running SWICD
|
2022-12-05 18:42:22 +01:00
|
|
|
|
if (!neptuneDeviceState.buttons5.HasFlag(SDCButton5.BTN_QUICK_ACCESS))
|
|
|
|
|
|
Thread.Sleep(50);
|
2022-11-14 11:06:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-16 08:28:17 +01:00
|
|
|
|
private void dismissNeptuneInput()
|
|
|
|
|
|
{
|
|
|
|
|
|
neptuneDeviceNextKey = DateTime.UtcNow.AddDays(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-14 11:06:03 +01:00
|
|
|
|
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-14 11:06:03 +01:00
|
|
|
|
|
2022-11-18 16:14:29 +01:00
|
|
|
|
// Reset sequence: 3 dots + L4|R4|L5|R5
|
2022-11-24 22:37:24 +01:00
|
|
|
|
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 &&
|
2022-11-24 22:37:24 +01:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-24 22:37:24 +01:00
|
|
|
|
if (!neptuneDeviceState.buttons5.HasFlag(SDCButton5.BTN_QUICK_ACCESS) || !RTSS.IsOSDForeground())
|
2022-11-14 11:06:03 +01:00
|
|
|
|
{
|
2022-11-16 08:28:17 +01:00
|
|
|
|
// schedule next repeat far in the future
|
|
|
|
|
|
dismissNeptuneInput();
|
2022-11-14 11:06:03 +01:00
|
|
|
|
hideOSD();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
rootMenu.Show();
|
2022-11-15 21:33:08 +01:00
|
|
|
|
setDismissTimer(false);
|
2022-11-14 11:06:03 +01:00
|
|
|
|
|
|
|
|
|
|
if (input.buttons1 != 0 || input.buttons2 != 0 || input.buttons3 != 0 || input.buttons4 != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-11-24 22:37:24 +01:00
|
|
|
|
else if (input.buttons0 == SDCButton0.BTN_DPAD_LEFT)
|
2022-11-14 11:06:03 +01:00
|
|
|
|
{
|
|
|
|
|
|
rootMenu.SelectPrev();
|
|
|
|
|
|
}
|
2022-11-24 22:37:24 +01:00
|
|
|
|
else if (input.buttons0 == SDCButton0.BTN_DPAD_RIGHT)
|
2022-11-14 11:06:03 +01:00
|
|
|
|
{
|
|
|
|
|
|
rootMenu.SelectNext();
|
|
|
|
|
|
}
|
2022-11-24 22:37:24 +01:00
|
|
|
|
else if (input.buttons0 == SDCButton0.BTN_DPAD_UP)
|
2022-11-14 11:06:03 +01:00
|
|
|
|
{
|
|
|
|
|
|
rootMenu.Prev();
|
|
|
|
|
|
}
|
2022-11-24 22:37:24 +01:00
|
|
|
|
else if (input.buttons0 == SDCButton0.BTN_DPAD_DOWN)
|
2022-11-14 11:06:03 +01:00
|
|
|
|
{
|
|
|
|
|
|
rootMenu.Next();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-15 21:33:08 +01:00
|
|
|
|
private void setDismissTimer(bool enabled = true)
|
2022-11-14 11:06:03 +01:00
|
|
|
|
{
|
|
|
|
|
|
osdDismissTimer.Stop();
|
2022-11-15 21:33:08 +01:00
|
|
|
|
if (enabled)
|
|
|
|
|
|
osdDismissTimer.Start();
|
2022-11-14 11:06:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void hideOSD()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!rootMenu.Visible)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
2022-11-16 08:28:17 +01:00
|
|
|
|
Trace.WriteLine("Hide OSD");
|
2022-11-14 11:06:03 +01:00
|
|
|
|
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
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-11-14 11:06:03 +01:00
|
|
|
|
if (!rootMenu.Visible)
|
|
|
|
|
|
{
|
2022-11-15 21:10:27 +01:00
|
|
|
|
osdClose();
|
2022-11-14 11:06:03 +01:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2022-11-16 01:10:44 +01:00
|
|
|
|
// recreate OSD if index 0
|
|
|
|
|
|
if (OSDHelpers.OSDIndex("Power Control") == 0 && OSD.GetOSDCount() > 1)
|
2022-11-15 21:10:27 +01:00
|
|
|
|
osdClose();
|
2022-11-14 11:06:03 +01:00
|
|
|
|
if (osd == null)
|
2022-11-16 08:28:17 +01:00
|
|
|
|
{
|
2022-11-14 11:06:03 +01:00
|
|
|
|
osd = new OSD("Power Control");
|
2022-11-16 08:28:17 +01:00
|
|
|
|
Trace.WriteLine("Show OSD");
|
|
|
|
|
|
}
|
2022-11-14 11:06:03 +01:00
|
|
|
|
osd.Update(rootMenu.Render(null));
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (SystemException)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ExitItem_Click(object? sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Application.Exit();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
components.Dispose();
|
2022-11-15 21:10:27 +01:00
|
|
|
|
osdClose();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void osdClose()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (osd != null)
|
2022-11-16 08:28:17 +01:00
|
|
|
|
{
|
2022-11-15 21:10:27 +01:00
|
|
|
|
osd.Dispose();
|
2022-11-16 08:28:17 +01:00
|
|
|
|
Trace.WriteLine("Close OSD");
|
|
|
|
|
|
}
|
2022-11-15 21:10:27 +01:00
|
|
|
|
osd = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (SystemException)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
2022-11-14 11:06:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|